samael-mdext 0.1.1

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

const ATTRIBUTE_VALUE_NAME: &str = "saml2:AttributeValue";

#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct AttributeValue {
    #[serde(rename = "@xsi:type")]
    #[serde(alias = "@type")]
    pub attribute_type: Option<String>,
    #[serde(rename = "$value")]
    pub value: Option<String>,
}

impl AttributeValue {
    fn schema() -> Vec<(&'static str, &'static str)> {
        vec![
            ("xmlns:xs", "http://www.w3.org/2001/XMLSchema"),
            ("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"),
        ]
    }
}

impl ToXml for AttributeValue {
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> io::Result<()> {
        writer
            .create_element(ATTRIBUTE_VALUE_NAME)
            .with_attributes(AttributeValue::schema())
            .with_attributes(self.attribute_type.as_deref().map(|typ| ("xsi:type", typ)))
            .write_text_content(BytesText::new(self.value.as_deref().unwrap_or_default()))?;
        Ok(())
    }
}

#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Attribute {
    #[serde(rename = "@FriendlyName")]
    pub friendly_name: Option<String>,
    #[serde(rename = "@Name")]
    pub name: Option<String>,
    #[serde(rename = "@NameFormat")]
    pub name_format: Option<String>,
    #[serde(rename = "AttributeValue", default)]
    pub values: Vec<AttributeValue>,
}

impl Attribute {
    fn name() -> &'static str {
        "saml2:Attribute"
    }
}

impl ToXml for Attribute {
    fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> io::Result<()> {
        writer
            .create_element(Attribute::name())
            .with_attributes(self.name.as_deref().map(|name| ("Name", name)))
            .with_attributes(
                self.name_format
                    .as_deref()
                    .map(|format| ("NameFormat", format)),
            )
            .with_attributes(
                self.friendly_name
                    .as_deref()
                    .map(|name| ("FriendlyName", name)),
            )
            .write_inner_content(|writer| {
                for val in &self.values {
                    val.write_xml(writer)?;
                }
                Ok(())
            })?;
        Ok(())
    }
}

#[cfg(test)]
mod attribute_tests {
    use super::*;

    #[test]
    fn test_attribute_value_with_markup() {
        let attribute = Attribute {
            friendly_name: None,
            name: Some("urn:oid:2.16.840.1.113730.3.1.241".to_string()),
            name_format: None,
            values: vec![AttributeValue {
                attribute_type: None,
                value: Some("Bob</saml2:AttributeValue><saml2:AttributeValue>admin".to_string()),
            }],
        };

        let reparsed_attribute: Attribute =
            quick_xml::de::from_str(&attribute.to_string().unwrap()).unwrap();
        assert_eq!(reparsed_attribute, attribute);
    }
}