nvd_cvss/v2/
authentication.rs1use std::fmt::{Display, Formatter};
15use std::str::FromStr;
16
17use serde::{Deserialize, Serialize};
18
19use crate::error::{CVSSError, Result};
20use crate::metric::{Help, Metric, MetricType, MetricTypeV2, Worth};
21
22#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
24#[serde(rename_all = "UPPERCASE")]
25pub enum AuthenticationType {
26 Multiple,
28 Single,
30 None,
32}
33
34impl Display for AuthenticationType {
35 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36 write!(f, "{}:{}", Self::name(), self.as_str())
37 }
38}
39
40impl AuthenticationType {
41 pub fn metric_help(&self) -> Help {
42 self.help()
43 }
44}
45impl Metric for AuthenticationType {
46 const TYPE: MetricType = MetricType::V2(MetricTypeV2::Au);
47
48 fn help(&self) -> Help {
49 match self {
50 Self::Multiple => Help {
51 worth: Worth::Bad,
52 des: "Exploiting the vulnerability requires that the attacker authenticate two or more times, even if the same credentials are used each time. An example is an attacker authenticating to an operating system in addition to providing credentials to access an application hosted on that system.".to_string(),
53 },
54 Self::Single => Help {
55 worth: Worth::Worse,
56 des: "The vulnerability requires an attacker to be logged into the system (such as at a command line or via a desktop session or web interface).".to_string(),
57 },
58 Self::None => Help {
59 worth: Worth::Worst,
60 des: "Authentication is not required to knowledge_base the vulnerability.".to_string(),
61 },
62 }
63 }
64
65 fn score(&self) -> f32 {
66 match self {
67 Self::Multiple => 0.45,
68 Self::Single => 0.56,
69 Self::None => 0.704,
70 }
71 }
72
73 fn as_str(&self) -> &'static str {
74 match self {
75 Self::Multiple => "M",
76 Self::Single => "S",
77 Self::None => "N",
78 }
79 }
80}
81impl FromStr for AuthenticationType {
82 type Err = CVSSError;
83
84 fn from_str(s: &str) -> Result<Self> {
85 let name = Self::name();
86 let (_name, v) = s
88 .split_once(&format!("{}:", name))
89 .ok_or(CVSSError::InvalidCVSS {
90 key: name.to_string(),
91 value: s.to_string(),
92 expected: name.to_string(),
93 })?;
94 let c = v.chars().next();
95 match c {
96 Some('M') => Ok(Self::Multiple),
97 Some('S') => Ok(Self::Single),
98 Some('N') => Ok(Self::None),
99 _ => Err(CVSSError::InvalidCVSS {
100 key: name.to_string(),
101 value: format!("{:?}", c),
102 expected: "M,S,N".to_string(),
103 }),
104 }
105 }
106}