Overview
SAML (Security Assertion Markup Language) 2.0 enables enterprise Single Sign-On (SSO) integration with identity providers like Azure AD, Okta, OneLogin, and Auth0. SuperTokens Core implements both Service Provider (SP) and limited Identity Provider (IdP) capabilities. Implementation: Uses OpenSAML library Standards: SAML 2.0, SAML 2.0 Web Browser SSO ProfileSAML Client Management
Create or Update SAML Client
Configure a SAML identity provider. Implementation:io.supertokens.saml.SAML.createOrUpdateSAMLClient() - View source
API Endpoint:
POST /recipe/saml/client
Request Body:
- Extracts IdP SSO URL
- Extracts IdP entity ID
- Extracts signing certificate
- Validates SAML 2.0 compatibility
List SAML Clients
Implementation:io.supertokens.saml.SAML.getClients() - View source
API Endpoint:
GET /recipe/saml/clients
Response:
Remove SAML Client
Implementation:io.supertokens.saml.SAML.removeSAMLClient() - View source
API Endpoint:
POST /recipe/saml/client/remove
Request Body:
SSO Login Flow
Create SAML Login Redirect
Initiate SAML authentication. Implementation:io.supertokens.saml.SAML.createRedirectURL() - View source
API Endpoint:
POST /recipe/saml/authorize
Request Body:
- Validate client and redirect URI
- Build SAML AuthnRequest
- Sign request (if enabled)
- Deflate and base64 encode
- Generate relay state
- Store relay state with client/redirect info
- Return redirect URL
SAML AuthnRequest Structure
Implementation:io.supertokens.saml.SAML.buildAuthnRequest() - View source
Handle SAML Callback
Process SAML response from IdP. Implementation:io.supertokens.saml.SAML.handleCallback() - View source
API Endpoint:
POST /recipe/saml/callback
Request Body (Form):
- Decode SAML response
- Retrieve client by relay state or IdP entity ID
- Verify SAML response signature
- Validate timestamps (NotBefore, NotOnOrAfter)
- Validate audience restriction
- Extract user claims
- Generate authorization code
- Store claims with code
- Redirect to application callback
Get User Info
Exchange code for user claims. Implementation:io.supertokens.saml.SAML.getUserInfo() - View source
API Endpoint:
POST /recipe/saml/userinfo
Request Body:
Service Provider Metadata
Generate SP metadata for IdP configuration. Implementation:io.supertokens.saml.SAML.getMetadataXML() - View source
API Endpoint:
GET /recipe/saml/metadata
Response:
SAML Response Verification
Signature Verification
Implementation:io.supertokens.saml.SAML.verifySamlResponseSignature() - View source
Verification Steps:
- Check Response signature OR
- Check Assertion signature
- Verify using IdP’s public certificate
- Validate signature algorithm (RSA-SHA256)
- SAML Response level (preferred)
- SAML Assertion level
- At least one must be signed
Timestamp Validation
Implementation:io.supertokens.saml.SAML.validateSamlResponseTimestamps() - View source
Checks:
- Response IssueInstant (±5 minutes clock skew)
- Assertion NotBefore condition
- Assertion NotOnOrAfter condition
Audience Validation
Implementation:io.supertokens.saml.SAML.validateSamlResponseAudience() - View source
Process:
- Extract AudienceRestriction from Conditions
- Verify audience matches SP entity ID
- Reject if audience doesn’t match
Claims Extraction
Implementation:io.supertokens.saml.SAML.extractAllClaims() - View source
Extracted Claims:
NameID:
sub (subject) field is determined in this order:
- NameID value
http://schemas.microsoft.com/identity/claims/objectidentifierhttp://schemas.xmlsoap.org/ws/2005/05/identity/claims/name
email field is determined in this order:
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress- NameID (if it contains ’@‘)
IdP-Initiated Login
Allow IdP to initiate login without RelayState. Configuration:- User starts login from IdP portal
- IdP sends SAML response without RelayState
- SuperTokens identifies client by IdP entity ID
- Uses default redirect URI
- Generates code and redirects
Request Signing
Sign SAML authentication requests. Configuration:io.supertokens.saml.SAML.buildAuthnRequest() - View source
Process:
- Build AuthnRequest XML
- Create RSA-SHA256 signature
- Embed X.509 certificate
- Add Signature element to request
io.supertokens.saml.SAMLCertificate.
Configuration
Core Settings
Redirect URIs
All redirect URIs must:- Be HTTPS (except localhost)
- Match exactly (no wildcards)
- Be pre-registered with client
Security Features
Certificate Management
SuperTokens automatically manages SP certificates: Implementation:io.supertokens.saml.SAMLCertificate
Features:
- Auto-generated RSA 2048-bit keys
- Self-signed certificates
- Stored securely in database
- Per-tenant certificates
Relay State
Relay state prevents:- CSRF attacks
- State confusion
- Replay attacks
- Random UUID
- Time-limited (15 minutes)
- One-time use
- Links request to response
Code Exchange
Authorization codes:- Random UUID
- Time-limited (10 minutes)
- One-time use
- Store user claims
Provider Configuration
Azure AD (Microsoft Entra ID)
Setup:- Create Enterprise Application in Azure AD
- Configure SAML SSO
- Set Reply URL to your ACS URL
- Download Federation Metadata XML
- Create SAML client with metadata
Okta
Setup:- Create SAML 2.0 App Integration
- Configure Single Sign-On URL (ACS URL)
- Set Audience URI (SP Entity ID)
- Download Metadata
- Create SAML client with metadata
OneLogin
Setup:- Add SAML Application
- Configure ACS (Consumer) URL
- Set Entity ID
- Download SAML Metadata
- Create SAML client with metadata
Auth0
Setup:- Create Regular Web Application
- Enable SAML2 Web App addon
- Configure Callback URL
- Download Metadata
- Create SAML client with metadata
Error Handling
Common Errors
SAMLResponseVerificationFailed: Signature verification failedInvalidRelayState: Relay state not found or expiredInvalidClient: SAML client not configuredInvalidCode: Authorization code invalid or expiredIDPInitiatedLoginDisallowed: IdP-initiated login disabledMalformedSAMLMetadataXML: Invalid IdP metadata
Best Practices
- Request signing: Enable for enhanced security
- Disable IdP-initiated: Unless specifically required
- HTTPS only: All URLs must use HTTPS in production
- Short validity: Keep relay state and code validity short
- Validate audience: Always verify audience matches
- Check timestamps: Validate NotBefore and NotOnOrAfter
- Certificate rotation: Monitor certificate expiry
- Metadata updates: Keep IdP metadata current
- Error handling: Log SAML errors for debugging
- Testing: Test with IdP test environments first
Debugging
Verbose Logging
Enable detailed SAML logging:Common Issues
Signature Verification Failed:- Check IdP certificate is correct
- Verify clock synchronization
- Ensure metadata is up to date
- Verify SP entity ID matches IdP configuration
- Check case sensitivity
- Synchronize server clocks (use NTP)
- Check assertion validity period
Multi-Tenancy
SAML clients are tenant-specific:- Each tenant can have different IdP configurations
- SP certificates are per-tenant
- Entity IDs can vary by tenant
- Users are isolated per tenant
Legacy Support
SuperTokens provides legacy endpoints for backward compatibility:/recipe/saml/legacy/authorize/recipe/saml/legacy/callback/recipe/saml/legacy/token/recipe/saml/legacy/userinfo