1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use chrono::{Duration, SecondsFormat, Utc};
use quick_xml::se::to_string as to_xml_string;
use serde::Serialize;

use crate::{get_cert_data, sp::ServiceProvider};

#[derive(Serialize)]
pub struct AssertionConsumerService {
    #[serde(rename = "Binding")]
    binding: String,
    #[serde(rename = "Location")]
    location: String,
    index: String,
}

#[derive(Serialize)]
pub struct SingleLogoutService {
    #[serde(rename = "Binding")]
    binding: String,
    #[serde(rename = "Location")]
    location: String,
}

#[derive(Serialize)]
pub struct X509Certificate {
    #[serde(rename = "$value")]
    value: String,
}

#[derive(Serialize)]
pub struct X509Data {
    #[serde(rename = "ds:X509Certificate")]
    x509_certificate: X509Certificate,
}

#[derive(Serialize)]
pub struct KeyInfo {
    #[serde(rename = "xmlns:ds")]
    ds: String,
    #[serde(rename = "ds:X509Data")]
    x509_data: X509Data,
}

#[derive(Serialize)]
pub struct KeyDescriptor {
    #[serde(rename = "use")]
    us: String,
    #[serde(rename = "ds:KeyInfo")]
    key_info: KeyInfo,
}

#[derive(Serialize)]
pub struct SPSSODescriptor {
    #[serde(rename = "protocolSupportEnumeration")]
    protocol_support_enumeration: String,
    #[serde(rename = "md:KeyDescriptor")]
    key_descriptor: Vec<KeyDescriptor>,
    #[serde(rename = "md:SingleLogoutService")]
    single_logout_service: SingleLogoutService,
    #[serde(rename = "md:AssertionConsumerService")]
    assertion_consumer_service: AssertionConsumerService,
}

#[derive(Serialize)]
#[serde(rename = "md:EntityDescriptor")]
pub struct EntityDescriptor {
    #[serde(rename = "xmlns:md")]
    md: String,
    #[serde(rename = "xmlns:ds")]
    ds: String,
    #[serde(rename = "entityID")]
    entity_id: String,
    #[serde(rename = "validUntil")]
    valid_util: String,
    #[serde(rename = "md:SPSSODescriptor")]
    sp_sso_descriptor: SPSSODescriptor,
}

impl ServiceProvider {
    pub fn metadata(&self) -> String {
        let now = Utc::now();
        let tomorrow = now + Duration::days(1);

        let cert_data = get_cert_data(&self.certificate);

        to_xml_string(&EntityDescriptor {
            md: "urn:oasis:names:tc:SAML:2.0:metadata".into(),
            ds: "http://www.w3.org/2000/09/xmldsig#".into(),
            entity_id: self.entity_id.to_string(),
            valid_util: tomorrow.to_rfc3339_opts(SecondsFormat::Millis, true),
            sp_sso_descriptor: SPSSODescriptor {
                protocol_support_enumeration:
                    "urn:oasis:names:tc:SAML:1.1:protocol urn:oasis:names:tc:SAML:2.0:protocol"
                        .into(),
                key_descriptor: vec![
                    KeyDescriptor {
                        us: "signing".into(),
                        key_info: KeyInfo {
                            ds: "http://www.w3.org/2000/09/xmldsig#".into(),
                            x509_data: X509Data {
                                x509_certificate: X509Certificate {
                                    value: cert_data.clone(),
                                },
                            },
                        },
                    },
                    KeyDescriptor {
                        us: "encryption".into(),
                        key_info: KeyInfo {
                            ds: "http://www.w3.org/2000/09/xmldsig#".into(),
                            x509_data: X509Data {
                                x509_certificate: X509Certificate { value: cert_data },
                            },
                        },
                    },
                ],
                single_logout_service: SingleLogoutService {
                    binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect".into(),
                    location: self.assert_logout.to_string(),
                },
                assertion_consumer_service: AssertionConsumerService {
                    binding: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST".into(),
                    location: self.assert_login.to_string(),
                    index: "0".into(),
                },
            },
        })
        .unwrap()
    }
}