saml_rs/api/mod.rs
1//! Typed high-level API contract for `saml-rs`.
2//!
3//! Artifact binding is not part of the high-level browser SSO request binding
4//! contract.
5//!
6//! ```compile_fail
7//! use saml_rs::SsoRequestBinding;
8//!
9//! let binding = SsoRequestBinding::Artifact;
10//! ```
11//!
12//! ```compile_fail
13//! use saml_rs::{AuthnRequest, Received, RespondSso, Saml, Sp, SpDescriptor, Subject};
14//!
15//! let sp: Saml<Sp> = unreachable!();
16//! let peer: SpDescriptor = unreachable!();
17//! let request: Received<AuthnRequest> = unreachable!();
18//! let subject: Subject = unreachable!();
19//!
20//! let _ = sp.respond_sso(&peer, &request, subject, RespondSso::post());
21//! ```
22//!
23//! ```compile_fail
24//! use saml_rs::{Idp, IdpDescriptor, Saml, StartSso};
25//!
26//! let idp: Saml<Idp> = unreachable!();
27//! let peer: IdpDescriptor = unreachable!();
28//!
29//! let _ = idp.start_sso(&peer, StartSso::post());
30//! ```
31
32mod idp;
33mod options;
34mod raw_mapping;
35mod slo;
36mod sp;
37
38use crate::config::{IdpConfig, SpConfig};
39use crate::entity::EntitySetting;
40use crate::error::SamlError as Error;
41use crate::idp::IdentityProvider;
42use crate::sp::ServiceProvider;
43
44use raw_mapping::{raw_idp_metadata_config, raw_sp_metadata_config};
45
46pub use options::{ForceAuthn, LogoutSigning, RespondSlo, RespondSso, StartSlo, StartSso};
47
48/// Typed SAML facade for high-level browser SSO/SLO flows.
49pub struct Saml<Role = Unknown>(Role);
50
51/// Marker role used before a facade has been configured as an SP or IdP.
52pub enum Unknown {}
53
54/// Marker role for a Service Provider facade.
55pub struct Sp {
56 service_provider: ServiceProvider,
57}
58
59/// Marker role for an Identity Provider facade.
60pub struct Idp {
61 identity_provider: IdentityProvider,
62}
63
64/// Error type returned by the typed SAML API.
65pub type SamlError = Error;
66
67impl Saml {
68 /// Build a typed Service Provider facade.
69 ///
70 /// # Errors
71 ///
72 /// Returns [`SamlError`] when the SP config cannot be converted into raw
73 /// settings or metadata, including missing metadata, missing keys, or
74 /// unsupported crypto configuration.
75 pub fn sp(config: SpConfig) -> Result<Saml<Sp>, SamlError> {
76 let setting = EntitySetting::try_from(&config)?;
77 let raw_config = raw_sp_metadata_config(&config);
78 let service_provider = ServiceProvider::from_config(&raw_config, setting)?;
79 Ok(Saml(Sp { service_provider }))
80 }
81
82 /// Build a typed Identity Provider facade.
83 ///
84 /// # Errors
85 ///
86 /// Returns [`SamlError`] when the IdP config cannot be converted into raw
87 /// settings or metadata, including missing metadata, missing keys, or
88 /// unsupported crypto configuration.
89 pub fn idp(config: IdpConfig) -> Result<Saml<Idp>, SamlError> {
90 let setting = EntitySetting::try_from(&config)?;
91 let raw_config = raw_idp_metadata_config(&config);
92 let identity_provider = IdentityProvider::from_config(&raw_config, setting)?;
93 Ok(Saml(Idp { identity_provider }))
94 }
95}