Skip to content

Service provider

Accept a browser login. You already have an identity provider, an assertion consumer URL, and a signing credential.

Keep the Pending<_> value with the browser session until the person returns.

  1. Build SpConfig and pass it to ServiceProvider::new.
  2. Import the identity provider metadata, pinned to the certificate you trust.
  3. Call start_sso. Store started.pending, and send started.outbound to the browser.
  4. When the browser posts back, load that pending value and call finish_sso.
let sp = ServiceProvider::new(
SpConfig::builder(
EntityId::try_new("https://sp.example.com/metadata")?,
AcsEndpoint::post("https://sp.example.com/acs")?,
)
.credentials(credentials)
.build()?,
);
let idp = IdpDescriptor::from_metadata_xml(
EntityId::try_new("https://idp.example.com/metadata")?,
idp_metadata_xml,
MetadataTrustPolicy::RequireSignature {
trusted_certificates: &[metadata_signing_cert],
},
)?;
let started = sp.start_sso(&idp, StartSso::post())?;
store_with_session(started.pending.snapshot());
send_to_browser(started.outbound);
let validation = SamlValidationContext::new(
now,
ReplayPolicy::RequireCache(&mut replay_cache),
);
let pending = PendingAuthnRequest::from_snapshot(load_snapshot())?;
let session = sp.finish_sso(
&idp,
&pending,
Inbound::post(form_fields),
validation,
)?;
let name_id = session.name_id().map(NameId::value);

credentials, idp_metadata_xml, metadata_signing_cert, now, replay_cache, and form_fields come from your application. The block omits use and fn main. It is the shape of the calls, not a file that compiles alone. session is an SsoSession.

With the serde feature, the snapshot serializes to JSON for your session store. finish_sso trusts the snapshot to name the request the response answers, so keep it on the server, or authenticate it when the browser carries it. The stored format is in Cargo features.

session.name_id() is None when the assertion Subject has only confirmations. Identify the principal from its attributes then. Such a session has no logout_subject(), so it cannot start Single Logout, and an OutstandingLogout does not apply to it.

session.attributes() holds the attributes of the assertion. first_value("mail") returns the first value of the first attribute named mail. Attribute::name_format() is the attribute’s AttributeNameFormat, and is None when the identity provider sent no NameFormat. Attribute::friendly_name() is its FriendlyName, and get_by_friendly_name("mail") finds the first attribute with that label. The identity provider chooses the label, and two attributes can share it, so select an attribute that decides access by its Name and NameFormat. A persistent or transient name identifier is reported as it was sent. The rules are in SAML-defined identifiers.

session.subject_id() and session.pairwise_id() return the subject-id and pairwise-id attributes as a SubjectId and a PairwiseId. Each returns None when the assertion does not have the attribute, and SamlError::ProtocolProfile when the value is malformed. Two identifiers that differ only in case are equal. Another identity provider can send the same pairwise-id value for another subject, so key an account on the issuer together with the value.

Before you use an identifier, call session.scoped_attribute_violations() with the attribute’s SAML name and an issuer-specific policy. With local configuration, pass AttributeScopePolicy::Explicit with the configured issuer and allowed scopes. For federation metadata, import the descriptor with MetadataTrustPolicy::RequireRegistrationAuthoritySignature and pass it in AttributeScopePolicy::RegistrationAuthority. Pin the trusted authority’s certificate independently of the IdP. The policy compares literal scopes case-sensitively and reports each value to discard or reject before account lookup. See scoped attributes.

The paired identity provider guide publishes an HTTP-POST SSO endpoint. For another IdP, match the request binding to a SingleSignOnService in its metadata. Use StartSso::redirect() for HTTP-Redirect or StartSso::simple_sign() for HTTP-POST-SimpleSign. All three constructors enforce the producer rules in SSO generation.

After SSO succeeds, set your local session’s expiry to the earlier of your application’s deadline and the deadline from session.session_not_on_or_after(). The method returns the earliest SessionNotOnOrAfter in the consumed assertion as a SystemTime, before validation clock skew. If the identity provider sent no deadline, use your application’s deadline.

let expires_at = match session.session_not_on_or_after() {
Some(bound) => local_deadline.min(bound),
None => local_deadline,
};

local_deadline is a SystemTime. End the local session at expires_at, and require a new login after it. If expires_at has already passed, require a new login before you create a local session. Use session.not_on_or_after() for the assertion’s Conditions@NotOnOrAfter.

HTTP-POST SSO requires ReplayPolicy::RequireCache. Reuse the cache across requests. If several nodes accept logins, use shared storage that atomically records each identifier. A duplicate, a missing cache, or a storage failure prevents acceptance. The rules are in Metadata and replay.

A new validation context allows five minutes of clock skew. Pass ClockSkew::strict() for zero skew.

The builder uses SpValidationPolicy::default(). Set an individual field to require an assertion signature or select the RSA-SHA2 signature profile. See Validation.

finish_sso records embedded XML signature evidence on verified_xml_signatures(). binding_signature_algorithm_uri() is only the SigAlg of an HTTP-POST-SimpleSign signature, which travels next to the message instead of inside the XML.

Follow Run the example to run both sides from a checkout.

The same calls are in the crate-root SSO example. Run cargo doc -p saml-rs --open from the repository root to read the API reference for main.

Issue the response with Identity provider. End the session with Single Logout.