Working with SAML Authentication

1. Understanding SAML 2.0 Protocol

AspectDetail
FormatXML-based assertions
UseEnterprise SSO, federation
SpecsSAML 2.0 (OASIS, 2005)
Modern AlternativeOIDC for new integrations

2. Understanding SAML Components

ComponentRole
IdP (Identity Provider)Authenticates user, issues assertions
SP (Service Provider)Consumes assertions, grants access
AssertionSigned XML statement about user
SubjectNameID identifying the user
MetadataXML describing endpoints + keys

3. Implementing SP-Initiated SSO Flow

1. User → SP (resource request)
2. SP → User (302 with SAML AuthnRequest)
3. User → IdP (POST/Redirect)
4. IdP authenticates user
5. IdP → User (POST SAML Response to SP ACS URL)
6. SP validates & logs user in
      

4. Implementing IdP-Initiated SSO Flow

StepDetail
StartUser clicks app in IdP portal
IdP issuesUnsolicited SAML Response to SP ACS
SP processesNo AuthnRequest to correlate
RiskCSRF — verify RelayState carefully

5. Creating SAML Assertions

Example: Assertion skeleton

<saml:Assertion ID="_abc" IssueInstant="2026-05-18T12:00:00Z" Version="2.0">
  <saml:Issuer>https://idp.example.com</saml:Issuer>
  <ds:Signature>...</ds:Signature>
  <saml:Subject>
    <saml:NameID Format="...emailAddress">alice@x.com</saml:NameID>
    <saml:SubjectConfirmation Method="...bearer">
      <saml:SubjectConfirmationData NotOnOrAfter="..." Recipient="..." InResponseTo="..."/>
    </saml:SubjectConfirmation>
  </saml:Subject>
  <saml:Conditions NotBefore="..." NotOnOrAfter="...">
    <saml:AudienceRestriction><saml:Audience>https://sp.example</saml:Audience></saml:AudienceRestriction>
  </saml:Conditions>
  <saml:AuthnStatement AuthnInstant="...">...</saml:AuthnStatement>
  <saml:AttributeStatement>...</saml:AttributeStatement>
</saml:Assertion>

6. Signing SAML Assertions

AspectDetail
AlgorithmRSA-SHA256 (xmldsig)
CanonicalizationExclusive c14n
Sign targetAssertion (preferred) or Response or both
KeyIdP signing cert, distributed via metadata

7. Validating SAML Assertions

CheckDetail
SignatureAgainst IdP metadata cert
IssuerExpected IdP entityID
AudienceEquals SP entityID
ConditionsNotBefore / NotOnOrAfter within now
RecipientEquals ACS URL
InResponseToMatches stored AuthnRequest ID
ReplayCache assertion ID for Conditions window
XSWUse namespace-aware, hardened parser
Warning: XML Signature Wrapping (XSW) attacks have plagued SAML libraries. Use well-maintained libraries (passport-saml, OneLogin python3-saml, Spring SAML) and update regularly.

8. Using SAML Bindings

BindingTransportUse
HTTP-RedirectURL query (deflated + base64)AuthnRequest (small)
HTTP-POSTForm POST (base64)Response (signed XML)
HTTP-ArtifactReference, resolved back-channelReduced front-channel size
SOAPSOAP envelopeBack-channel logout, queries

9. Implementing SAML Logout

FlowDetail
SP-initiatedSP sends LogoutRequest → IdP
IdP-initiatedIdP sends LogoutRequest to each SP
Single Logout (SLO)Cascade across all SSO sessions
ReliabilityBest-effort — SPs may be unreachable

10. Configuring SAML Metadata

ElementPurpose
EntityDescriptorentityID + role descriptors
KeyDescriptorsigning / encryption certs
SingleSignOnServiceIdP endpoints + bindings
AssertionConsumerServiceSP ACS endpoints
SingleLogoutServiceSLO endpoints
NameIDFormatSupported NameID types

11. Using SAML Attributes

Example: AttributeStatement

<saml:AttributeStatement>
  <saml:Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress">
    <saml:AttributeValue>alice@example.com</saml:AttributeValue>
  </saml:Attribute>
  <saml:Attribute Name="groups">
    <saml:AttributeValue>admin</saml:AttributeValue>
    <saml:AttributeValue>engineering</saml:AttributeValue>
  </saml:Attribute>
</saml:AttributeStatement>