Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extensions #49

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### Unreleased
- Add ability to serialize and deserialize `Extensions` in an `AuthnRequest`

### 0.0.15

- Updates dependencies
Expand Down
10 changes: 9 additions & 1 deletion src/schema/authn_request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::schema::{Conditions, Issuer, NameIdPolicy, Subject};
use crate::schema::{Conditions, Extensions, Issuer, NameIdPolicy, Subject};
use crate::signature::Signature;
use chrono::prelude::*;
use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event};
Expand Down Expand Up @@ -30,6 +30,9 @@ pub struct AuthnRequest {
pub issuer: Option<Issuer>,
#[serde(rename = "Signature")]
pub signature: Option<Signature>,
#[serde(rename = "Extensions")]
#[serde(skip_deserializing)]
pub extensions: Option<Extensions>,
#[serde(rename = "Subject")]
pub subject: Option<Subject>,
#[serde(rename = "NameIDPolicy")]
Expand Down Expand Up @@ -62,6 +65,7 @@ impl Default for AuthnRequest {
consent: None,
issuer: None,
signature: None,
extensions: None,
subject: None,
name_id_policy: None,
conditions: None,
Expand Down Expand Up @@ -207,6 +211,10 @@ impl TryFrom<&AuthnRequest> for Event<'_> {
let event: Event<'_> = signature.try_into()?;
writer.write_event(event)?;
}
if let Some(extensions) = &value.extensions {
let event: Event<'_> = extensions.try_into()?;
writer.write_event(event)?;
}
if let Some(subject) = &value.subject {
let event: Event<'_> = subject.try_into()?;
writer.write_event(event)?;
Expand Down
56 changes: 56 additions & 0 deletions src/schema/extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::io::{Cursor, Write};

use quick_xml::{
events::{BytesEnd, BytesStart, BytesText, Event},
Writer,
};
use serde::Deserialize;

const NAME: &str = "saml2p:Extensions";

#[derive(Clone, Debug, Deserialize, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Extensions(pub Vec<String>);

impl TryFrom<Extensions> for Event<'_> {
type Error = Box<dyn std::error::Error>;

fn try_from(value: Extensions) -> Result<Self, Self::Error> {
(&value).try_into()
}
}

impl TryFrom<&Extensions> for Event<'_> {
type Error = Box<dyn std::error::Error>;

fn try_from(value: &Extensions) -> Result<Self, Self::Error> {
let mut write_buf = Vec::new();
let mut writer = Writer::new(Cursor::new(&mut write_buf));
let root = BytesStart::from_content(NAME, NAME.len());
writer.write_event(Event::Start(root))?;

for extension in &value.0 {
writer.get_mut().write_all(extension.as_bytes())?;
}

writer.write_event(Event::End(BytesEnd::new(NAME)))?;
Ok(Event::Text(BytesText::from_escaped(String::from_utf8(
write_buf,
)?)))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::traits::ToXml;

#[test]
fn extensions_xml_serialization() {
assert_eq!(
r#"<saml2p:Extensions><qqq a="b"/></saml2p:Extensions>"#,
Extensions(vec![r#"<qqq a="b"/>"#.to_string()])
.to_xml()
.unwrap(),
)
}
}
2 changes: 2 additions & 0 deletions src/schema/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
pub mod authn_request;
mod conditions;
mod extensions;
mod issuer;
mod name_id_policy;
mod response;
mod subject;

pub use authn_request::AuthnRequest;
pub use conditions::*;
pub use extensions::Extensions;
pub use issuer::Issuer;
pub use name_id_policy::NameIdPolicy;
pub use response::Response;
Expand Down