samael-mdext 0.1.0

samael with unreleased fixes for SAML metadata extensions and XML escaping
use crate::traits::ToXml;
use chrono::prelude::*;
use quick_xml::events::BytesText;
use quick_xml::Writer;
use serde::Deserialize;
use std::io::{self, Write};

const NAME: &str = "saml2:Conditions";

#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Conditions {
    #[serde(rename = "@NotBefore")]
    pub not_before: Option<DateTime<Utc>>,
    #[serde(rename = "@NotOnOrAfter")]
    pub not_on_or_after: Option<DateTime<Utc>>,
    #[serde(rename = "AudienceRestriction", default)]
    pub audience_restrictions: Option<Vec<AudienceRestriction>>,
    #[serde(rename = "OneTimeUse")]
    pub one_time_use: Option<OneTimeUse>,
    #[serde(rename = "ProxyRestriction")]
    pub proxy_restriction: Option<ProxyRestriction>,
}

impl ToXml for Conditions {
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> io::Result<()> {
        let not_before = self
            .not_before
            .map(|t| t.to_rfc3339_opts(SecondsFormat::Secs, true));
        let not_on_or_after = self
            .not_on_or_after
            .map(|t| t.to_rfc3339_opts(SecondsFormat::Secs, true));

        writer
            .create_element(NAME)
            .with_attributes(not_before.as_deref().map(|v| ("NotBefore", v)))
            .with_attributes(not_on_or_after.as_deref().map(|v| ("NotOnOrAfter", v)))
            .write_inner_content(|writer| {
                if let Some(audience_restrictions) = &self.audience_restrictions {
                    for restriction in audience_restrictions {
                        restriction.write_xml(writer)?;
                    }
                }
                if let Some(proxy_restriction) = &self.proxy_restriction {
                    proxy_restriction.write_xml(writer)?;
                }
                Ok(())
            })?;
        Ok(())
    }
}

const AUDIENCE_RESTRICTION_NAME: &str = "saml2:AudienceRestriction";
const AUDIENCE_NAME: &str = "saml2:Audience";

#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct AudienceRestriction {
    #[serde(rename = "Audience")]
    pub audience: Vec<String>,
}

impl ToXml for AudienceRestriction {
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> io::Result<()> {
        writer
            .create_element(AUDIENCE_RESTRICTION_NAME)
            .write_inner_content(|writer| {
                for aud in &self.audience {
                    writer
                        .create_element(AUDIENCE_NAME)
                        .write_text_content(BytesText::new(aud))?;
                }
                Ok(())
            })?;
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct OneTimeUse {}

const PROXY_RESTRICTION_NAME: &str = "saml2:ProxyRestriction";

#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct ProxyRestriction {
    #[serde(rename = "@Count")]
    pub count: Option<usize>,
    #[serde(rename = "Audience")]
    pub audiences: Option<Vec<String>>,
}

impl ToXml for ProxyRestriction {
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> io::Result<()> {
        let count = self.count.map(|c| c.to_string());

        writer
            .create_element(PROXY_RESTRICTION_NAME)
            .with_attributes(count.as_deref().map(|v| ("Count", v)))
            .write_inner_content(|writer| {
                if let Some(audiences) = &self.audiences {
                    for aud in audiences {
                        writer
                            .create_element(AUDIENCE_NAME)
                            .write_text_content(BytesText::new(aud))?;
                    }
                }
                Ok(())
            })?;
        Ok(())
    }
}