Prompt
How do I map SSO users to tenants and roles?
Latest observation
A common way to map SSO users to tenants and roles is to do it at login based on claims from your identity provider, then resolve those claims against your app’s internal authorization model.
Typical approach
1) Identify the user uniquely
Use a stable external identifier from the SSO token, such as:
sub(OIDC subject)emailif it’s guaranteed stable in your orgNameIDin SAML
Store a local user record keyed by that identifier.
2) Map the user to a tenant
There are a few common patterns:
A. Tenant in the token
The IdP includes a claim like:
tenant_idorg_idcustomer_idgroups
Then your app maps that claim directly to an internal tenant.
Example:
- SSO claim:
tenant_id = acme - Internal tenant record:
tenant_id = 42
B. Tenant inferred from domain or email
Example:
alice@acme.com→ Acme tenant
This is simple, but only safe if email domain ownership is strict and verified.
C. Tenant selected after login
If a user belongs to multiple tenants:
- Authenticate via SSO
- Fetch allowed tenants from your app DB
- Show tenant switcher
- Store selected tenant in session/context
This is very common in B2B SaaS.
3) Map SSO attributes/groups to app roles
Do not trust “role” names blindly unless they come from a controlled source.
Option A: Use IdP groups
Example token claim:
{
"sub": "12345",
"email": "alice@acme.com",
"groups": ["acme-admin", "billing-team"]
}
Your app maps:
acme-admin→tenant_adminbilling-team→billing_user
Option B: Use explicit role claims
Example:
{
"roles": ["admin", "editor"]
}
Then your app translates them into internal permissions.
Option C: Maintain roles locally
SSO only proves identity; your app stores role assignment per tenant:
- user X is
Adminin tenant A - user X is
Viewerin tenant B
This is usually the most flexible and secure model.
Recommended model
Use SSO for authentication, and your app/database for authorization.
Data model example
- User
idexternal_subjectemail
- Tenant
idname
- Membership
user_idtenant_idrole
On login:
- Verify SSO token
- Find or create user by
external_subject - Look up memberships
- Assign active tenant
- Apply role-based permissions for that tenant
Best practices
- Prefer immutable identifiers like
subover email. - Don’t rely on client-side tenant selection alone; validate tenant access on the server.
- Keep roles tenant-scoped, not global, unless you truly need global admin.
- If using group claims, define a clear mapping layer between IdP groups and app roles.
- Provision/deprovision users via SCIM if possible.
- Log tenant and role resolution for auditing.
Example flow
- User signs in with SSO.
- IdP returns:
sub = abc123email = bob@company.comgroups = [tenant-7-admin]
- App finds user
abc123. - App checks memberships:
- tenant 7 → admin
- tenant 9 → viewer
- If one tenant: auto-select it.
- If multiple tenants: prompt user to choose.
- Store active tenant in session and authorize requests accordingly.
If you want, I can also show:
- a database schema for tenant/role mapping,
- an OIDC/SAML claim mapping example, or
- an implementation example in Node, Python, .NET, or Java.