|
| 1 | +use crate::schema::{AuthnContextClassRef, AuthnContextDeclRef}; |
| 2 | +use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; |
| 3 | +use quick_xml::Writer; |
| 4 | +use serde::Deserialize; |
| 5 | +use std::io::Cursor; |
| 6 | +use std::str::FromStr; |
| 7 | + |
| 8 | +const NAME: &str = "saml2p:RequestedAuthnContext"; |
| 9 | +const SCHEMA: (&str, &str) = ("xmlns:saml2", "urn:oasis:names:tc:SAML:2.0:assertion"); |
| 10 | + |
| 11 | +#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)] |
| 12 | +pub struct RequestedAuthnContext { |
| 13 | + #[serde(rename = "AuthnContextClassRef")] |
| 14 | + pub authn_context_class_refs: Option<Vec<AuthnContextClassRef>>, |
| 15 | + #[serde(rename = "AuthnContextDeclRef")] |
| 16 | + pub authn_context_decl_refs: Option<Vec<AuthnContextDeclRef>>, |
| 17 | + #[serde(rename = "@Comparison")] |
| 18 | + pub comparison: Option<AuthnContextComparison>, |
| 19 | +} |
| 20 | + |
| 21 | +impl TryFrom<RequestedAuthnContext> for Event<'_> { |
| 22 | + type Error = Box<dyn std::error::Error>; |
| 23 | + |
| 24 | + fn try_from(value: RequestedAuthnContext) -> Result<Self, Self::Error> { |
| 25 | + (&value).try_into() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl TryFrom<&RequestedAuthnContext> for Event<'_> { |
| 30 | + type Error = Box<dyn std::error::Error>; |
| 31 | + |
| 32 | + fn try_from(value: &RequestedAuthnContext) -> Result<Self, Self::Error> { |
| 33 | + let mut write_buf = Vec::new(); |
| 34 | + let mut writer = Writer::new(Cursor::new(&mut write_buf)); |
| 35 | + let mut root = BytesStart::from_content(NAME, NAME.len()); |
| 36 | + root.push_attribute(SCHEMA); |
| 37 | + |
| 38 | + if let Some(comparison) = &value.comparison { |
| 39 | + root.push_attribute(("Comparison", comparison.value())); |
| 40 | + } |
| 41 | + writer.write_event(Event::Start(root))?; |
| 42 | + |
| 43 | + if let Some(authn_context_class_refs) = &value.authn_context_class_refs { |
| 44 | + for authn_context_class_ref in authn_context_class_refs { |
| 45 | + let event: Event<'_> = authn_context_class_ref.try_into()?; |
| 46 | + writer.write_event(event)?; |
| 47 | + } |
| 48 | + } else if let Some(authn_context_decl_refs) = &value.authn_context_decl_refs { |
| 49 | + for authn_context_decl_ref in authn_context_decl_refs { |
| 50 | + let event: Event<'_> = authn_context_decl_ref.try_into()?; |
| 51 | + writer.write_event(event)?; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + writer.write_event(Event::End(BytesEnd::new(NAME)))?; |
| 56 | + Ok(Event::Text(BytesText::from_escaped(String::from_utf8( |
| 57 | + write_buf, |
| 58 | + )?))) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)] |
| 63 | +#[serde(rename_all = "lowercase")] |
| 64 | +pub enum AuthnContextComparison { |
| 65 | + Exact, |
| 66 | + Minimum, |
| 67 | + Maximum, |
| 68 | + Better, |
| 69 | +} |
| 70 | + |
| 71 | +impl AuthnContextComparison { |
| 72 | + pub fn value(&self) -> &'static str { |
| 73 | + match self { |
| 74 | + AuthnContextComparison::Exact => "exact", |
| 75 | + AuthnContextComparison::Minimum => "minimum", |
| 76 | + AuthnContextComparison::Maximum => "maximum", |
| 77 | + AuthnContextComparison::Better => "better", |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl FromStr for AuthnContextComparison { |
| 83 | + type Err = quick_xml::DeError; |
| 84 | + |
| 85 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 86 | + Ok(match s { |
| 87 | + "exact" => AuthnContextComparison::Exact, |
| 88 | + "minimum" => AuthnContextComparison::Minimum, |
| 89 | + "maximum" => AuthnContextComparison::Maximum, |
| 90 | + "better" => AuthnContextComparison::Better, |
| 91 | + _ => { |
| 92 | + return Err(quick_xml::DeError::Custom("Illegal comparison! Must be one of `exact`, `minimum`, `maximum` or `better`".to_string())); |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod test { |
| 100 | + use crate::traits::ToXml; |
| 101 | + |
| 102 | + use super::*; |
| 103 | + |
| 104 | + #[test] |
| 105 | + pub fn test_deserialize_serialize_requested_authn_context() { |
| 106 | + let xml_context = r#"<saml2p:RequestedAuthnContext xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" Comparison="exact"><saml2:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml2:AuthnContextClassRef></saml2p:RequestedAuthnContext>"#; |
| 107 | + |
| 108 | + let expected_context: RequestedAuthnContext = quick_xml::de::from_str(xml_context) |
| 109 | + .expect("failed to parse RequestedAuthnContext"); |
| 110 | + let serialized_context = expected_context |
| 111 | + .to_xml() |
| 112 | + .expect("failed to convert RequestedAuthnContext to xml"); |
| 113 | + let actual_context: RequestedAuthnContext = quick_xml::de::from_str(&serialized_context) |
| 114 | + .expect("failed to re-parse RequestedAuthnContext"); |
| 115 | + |
| 116 | + assert_eq!(expected_context, actual_context); |
| 117 | + } |
| 118 | +} |
0 commit comments