use crate::schema::{Assertion, EncryptedAssertion, Issuer, Status};
use crate::signature::Signature;
use crate::traits::ToXml;
use chrono::prelude::*;
use quick_xml::Writer;
use serde::Deserialize;
use std::io::{self, Write};
use std::str::FromStr;
const NAME: &str = "saml2p:Response";
const SCHEMA: (&str, &str) = ("xmlns:saml2p", "urn:oasis:names:tc:SAML:2.0:protocol");
#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Response {
#[serde(rename = "@ID")]
pub id: String,
#[serde(rename = "@InResponseTo")]
pub in_response_to: Option<String>,
#[serde(rename = "@Version")]
pub version: String,
#[serde(rename = "@IssueInstant")]
pub issue_instant: DateTime<Utc>,
#[serde(rename = "@Destination")]
pub destination: Option<String>,
#[serde(rename = "@Consent")]
pub consent: Option<String>,
#[serde(rename = "Issuer")]
pub issuer: Option<Issuer>,
#[serde(rename = "Signature")]
pub signature: Option<Signature>,
#[serde(rename = "Status")]
pub status: Option<Status>,
#[serde(rename = "EncryptedAssertion")]
pub encrypted_assertion: Option<EncryptedAssertion>,
#[serde(rename = "Assertion")]
pub assertion: Option<Assertion>,
}
impl FromStr for Response {
type Err = quick_xml::DeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
quick_xml::de::from_str(s)
}
}
impl ToXml for Response {
fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> io::Result<()> {
let issue_instant = self
.issue_instant
.to_rfc3339_opts(SecondsFormat::Millis, true);
writer
.create_element(NAME)
.with_attribute(SCHEMA)
.with_attribute(("ID", self.id.as_str()))
.with_attributes(self.in_response_to.as_deref().map(|v| ("InResponseTo", v)))
.with_attribute(("Version", self.version.as_str()))
.with_attribute(("IssueInstant", issue_instant.as_str()))
.with_attributes(self.destination.as_deref().map(|v| ("Destination", v)))
.with_attributes(self.consent.as_deref().map(|v| ("Consent", v)))
.write_inner_content(|writer| {
if let Some(issuer) = &self.issuer {
issuer.write_xml(writer)?;
}
if let Some(signature) = &self.signature {
signature.write_xml(writer)?;
}
if let Some(status) = &self.status {
status.write_xml(writer)?;
}
if let Some(assertion) = &self.assertion {
assertion.write_xml(writer)?;
}
if let Some(encrypted_assertion) = &self.encrypted_assertion {
encrypted_assertion.write_xml(writer)?;
}
Ok(())
})?;
Ok(())
}
}
#[cfg(test)]
mod test {
use super::Response;
use crate::traits::ToXml;
#[test]
fn test_deserialize_serialize_response() {
let response_xml = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test_vectors/response.xml",
));
let expected_response: Response =
response_xml.parse().expect("failed to parse response.xml");
let serialized_response = expected_response
.to_string()
.expect("failed to convert response to xml");
let actual_response: Response = serialized_response
.parse()
.expect("failed to re-parse response");
assert_eq!(expected_response, actual_response);
}
#[test]
fn test_deserialize_serialize_response_with_signed_assertion() {
let response_xml = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test_vectors/response_signed_assertion.xml",
));
let expected_response: Response = response_xml
.parse()
.expect("failed to parse response_signed_assertion.xml");
let serialized_response = expected_response
.to_string()
.expect("failed to convert response to xml");
let actual_response: Response = serialized_response
.parse()
.expect("failed to re-parse response");
assert_eq!(expected_response, actual_response);
}
#[test]
fn test_deserialize_serialize_signed_response() {
let response_xml = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test_vectors/response_signed.xml",
));
let expected_response: Response = response_xml
.parse()
.expect("failed to parse response_signed.xml");
let serialized_response = expected_response
.to_string()
.expect("failed to convert response to xml");
let actual_response: Response = serialized_response
.parse()
.expect("failed to re-parse response");
assert_eq!(expected_response, actual_response);
}
#[test]
fn test_deserialize_serialize_response_encrypted_assertion() {
let response_xml = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/test_vectors/response_encrypted.xml",
));
let expected_response: Response = response_xml
.parse()
.expect("failed to parse response_encrypted.xml");
let serialized_response = expected_response
.to_string()
.expect("failed to convert response to xml");
let actual_response: Response = serialized_response
.parse()
.expect("failed to re-parse response");
assert_eq!(expected_response, actual_response);
}
}