Prompt
How do I add SSO and RBAC to a SaaS app?
Latest observation
Adding SSO and RBAC to a SaaS app usually means designing for enterprise identity and tenant-scoped permissions.
1) Start with the right concepts
- SSO (Single Sign-On): Users sign in with their company identity provider instead of a local password.
- IdP (Identity Provider): Azure AD / Entra ID, Okta, Google Workspace, Ping, Auth0, etc.
- RBAC (Role-Based Access Control): Permissions are granted by role, such as
Owner,Admin,Member,Viewer. - Tenant: A customer org/workspace/account in your SaaS.
- User: A person who may belong to one or more tenants.
2) Recommended authentication protocols
For B2B SaaS, support:
- OIDC (OpenID Connect) for modern login
- SAML 2.0 for enterprise compatibility
If you’re choosing one first:
- OIDC first if you can
- Add SAML when enterprise customers demand it
3) High-level architecture
Identity side
Your app should trust external IdPs for authentication, but keep authorization in your app.
Typical flow:
- User clicks “Sign in with Company”
- Redirect to IdP
- IdP authenticates user
- IdP sends auth response back to your app
- Your app verifies the response
- Your app creates/links a local user record
- Your app issues its own session/JWT
- Your app checks tenant + role on each request
Important principle
- Authentication: outsourced to IdP
- Authorization: enforced by your app
4) Data model you’ll likely need
At minimum:
users
idemailnamestatuscreated_at
tenants
idnameslugplancreated_at
memberships
Represents a user in a tenant.
iduser_idtenant_idrole_idorrolestatus
roles
idname(owner,admin,member,viewer)tenant_idnullable if you support custom tenant roles- or global roles if fixed
permissions
If you need finer-grained control than roles:
idkey(project:create,billing:update)description
role_permissions
role_idpermission_id
identity_connections
Links tenant/org to IdP config.
idtenant_idprovider(okta,azuread,google,saml)issuerclient_idmetadata_urlcertificatedomaincreated_at
external identities
Maps IdP user to local user.
iduser_idprovidersubject/name_idemailtenant_id
5) SSO onboarding flow for tenants
A good enterprise onboarding flow:
- Customer creates a tenant/workspace
- Admin goes to “SSO settings”
- They connect their IdP:
- OIDC: issuer, client id, client secret, redirect URI
- SAML: metadata URL or XML, ACS URL, certificate
- They verify a domain, e.g.
company.com - Optional: enable Just-In-Time provisioning
- Optional: enforce SSO only for that domain
- Test login with an admin account
- Turn SSO on for the tenant
Domain mapping
When a user logs in with alice@company.com, route them to the tenant tied to company.com.
6) Login and account linking strategy
Common options:
A. Just-in-time provisioning
If user authenticates via SSO and doesn’t exist yet:
- create user record
- create membership in the tenant
- assign default role
B. Pre-provisioning
Admin invites users first; when they sign in, you link them.
C. SCIM provisioning
Enterprise-grade user lifecycle automation.
- IdP can create/deactivate users in your app automatically
- Useful for large customers
If you can, plan for SCIM later.
7) RBAC design
Start simple
A common SaaS RBAC set:
- Owner: full access, billing, manage SSO, delete tenant
- Admin: manage users, settings, most app features
- Member: standard use
- Viewer: read-only
Enforce permissions centrally
Don’t scatter role checks everywhere. Create a single authorization layer:
- middleware
- policy service
- helper like
can(user, action, resource)
Example:
can(user, "project:create", tenant)can(user, "billing:manage", tenant)
Tenant-scoped checks are mandatory
Always verify:
- user belongs to tenant
- user has required role/permission
- resource belongs to same tenant
8) Session and token handling
After SSO login, your app should usually create:
- a server session cookie, or
- your own app JWT/session token
Best practice:
- Keep IdP tokens for verifying login or calling IdP APIs
- Use your own session for your app
- Don’t rely on raw IdP claims alone for authorization
If using JWTs, keep them short-lived and refresh carefully.
9) Multi-tenant edge cases
Watch out for:
- User belongs to multiple tenants
- Same email across tenants
- Different roles in different tenants
- Domain conflicts
- SP-initiated vs IdP-initiated SSO
- Tenant-level SSO enforcement
- Break-glass admin access if IdP is down
A common pattern:
- User logs in
- If multiple memberships exist, choose tenant or use last active tenant
- Authorization is always based on current tenant context
10) Security best practices
- Use OIDC/SAML libraries, not custom protocol code
- Validate signatures, audience, issuer, timestamps
- Use state and nonce for OIDC
- Protect against CSRF on login flows
- Use domain verification before auto-linking accounts
- Require admins to re-authenticate for sensitive actions
- Log auth events and permission changes
- Support account deactivation and session revocation
- Encrypt secrets and certificates
- Rotate SSO credentials and signing certs
11) Auditing and observability
Log:
- login success/failure
- IdP used
- tenant selected
- role changes
- permission denials
- SSO config changes
- SCIM events, if enabled
This is essential for enterprise customers.
12) Practical implementation plan
Phase 1
- Local user accounts
- Tenants
- Basic RBAC
- OIDC SSO for one provider
Phase 2
- SAML support
- Tenant-specific IdP config
- Domain mapping
- Role management UI
Phase 3
- SCIM provisioning
- Custom roles/permissions
- Audit logs
- SSO enforcement per tenant
13) A simple authorization pattern
Pseudo-logic:
if not authenticated:
deny
if not membership(user, tenant):
deny
if not has_permission(user, tenant, "billing:manage"):
deny
allow
14) Build vs buy
Consider using a provider if you want speed:
- Auth0
- Clerk
- WorkOS
- FusionAuth
- Supabase Auth (less enterprise-focused for SAML/SCIM)
If you need enterprise SSO quickly, WorkOS is often used for:
- SAML
- OIDC
- SCIM
- org discovery
15) A good default architecture
If you’re building from scratch:
- Auth: OIDC + SAML
- Tenant model: workspace/org
- RBAC: tenant-scoped roles
- Provisioning: JIT first, SCIM later
- Authorization: centralized policy layer
- Session: your own app session after SSO
- Audit: all security-sensitive actions
If you want, I can also give you:
- a database schema,
- a request flow diagram, or
- implementation examples in Node/Express, Django, or Rails.