Prompt
How do I build SAML and OIDC support into one app?
Latest observation
To build both SAML and OIDC support in one app, the cleanest approach is to put an authentication abstraction layer in front of your app and implement each protocol as a separate adapter.
Recommended architecture
1) Create a common internal user model
Regardless of whether the user logs in via SAML or OIDC, convert the result into a single internal identity object, for example:
userIdemaildisplayNameroles/groupsidpType(samloroidc)idpIssuer/tenantexternalSubjectId
This lets the rest of your app stay protocol-agnostic.
2) Build two auth handlers
Implement separate login/callback flows:
OIDC flow
- Redirect user to the OIDC authorization endpoint
- Receive callback with
code - Exchange
codefor tokens at the token endpoint - Validate:
id_tokensignature- issuer
- audience
- nonce
- expiration
- Map OIDC claims to your internal user model
SAML flow
- Redirect user to the IdP with a signed AuthnRequest or accept IdP-initiated login
- Receive SAMLResponse at your ACS endpoint
- Validate:
- XML signature
- audience
- issuer
- destination
- conditions / timestamps
- Map SAML attributes to your internal user model
3) Normalize identities
Use a shared mapping layer so both protocols end up producing something like:
{
"userId": "internal-123",
"email": "alice@example.com",
"displayName": "Alice Smith",
"roles": ["admin"],
"idpType": "oidc",
"idpIssuer": "https://login.example.com"
}
4) Keep sessions separate from protocols
After authentication, issue your own app session:
- server-side session cookie, or
- your own JWT/session token
Don’t rely directly on SAML assertions or OIDC tokens for every request unless you intentionally design for stateless auth. Usually you:
- validate at login time
- create your own application session
- use that session for authorization in the app
Important design choices
Support multiple identity providers
You may need:
- several OIDC providers
- several SAML IdPs
- routing based on email domain, tenant, or user selection
Common patterns:
- IdP discovery by email domain
- tenant-specific config
- “Login with Enterprise SSO” page with provider selection
Separate protocol-specific configuration
For each IdP store:
- issuer / entity ID
- client ID / secret (OIDC)
- metadata URL or certificate (SAML)
- allowed redirect URIs / ACS URLs
- attribute/claim mapping rules
Be careful with account linking
A user may log in via SAML once and OIDC later. Decide whether to:
- auto-link by verified email
- require admin linking
- maintain separate accounts per IdP
Security requirements
OIDC
- Use PKCE for public clients
- Validate nonce
- Validate issuer, audience, signature, exp, azp as needed
- Restrict redirect URIs
- Use HTTPS only
SAML
- Validate XML signature properly
- Check assertion audience, recipient, destination
- Enforce clock skew limits
- Reject unsigned or improperly signed assertions
- Be cautious of XML parsing vulnerabilities
Practical implementation pattern
Option A: Build it yourself
Good if you need full control, but more work and risk.
Structure:
AuthControllerOidcStrategySamlStrategyIdentityMapperSessionServiceIdpConfigService
Option B: Use an identity library/framework
Many frameworks already support both, or make it easier:
- Java/Spring Security
- .NET / ASP.NET
- Node.js with passport-style strategies plus SAML/OIDC libraries
- Python with auth libraries for each protocol
Option C: Put an identity broker in front
If you want to avoid maintaining protocol complexity yourself, use:
- Keycloak
- Auth0
- Okta
- Azure AD B2C
- Ping
- Cognito (more OIDC-centric, SAML federation possible in some setups)
Then your app only speaks one protocol to the broker, and the broker handles SAML/OIDC federation upstream.
This is often the best choice if:
- you need many enterprise customers
- you want faster setup
- you want to reduce security/maintenance burden
Suggested flow for your app
- User hits login page
- App determines or asks for org/tenant
- App selects SAML or OIDC provider for that tenant
- Redirect to correct IdP
- Receive callback/assertion
- Validate response
- Map to internal user
- Create app session
- Proceed to app
Best practice recommendation
If you’re starting from scratch:
- Use OIDC as the default modern protocol
- Add SAML only for enterprise/legacy IdPs
- Implement a shared identity abstraction
- Consider using an identity broker if you expect multiple enterprise customers
If you want, I can also give you:
- a reference architecture diagram,
- a database schema for SAML/OIDC tenant config, or
- a sample implementation in a specific stack like Node, Python, Java, or .NET.