Course contentsShow
← System Design
Lesson 932 of 1,91924. Authentication and Authorization at ScaleFree lesson

Multi-Tenant OAuth2 and Identity Federation

Scaling OAuth2 across tenants and federating identities with external providers.

Multi-Tenant OAuth2 and Identity Federation

You'll learn how to scale OAuth2 authorization across multiple tenants while federating identities from external providers like Google, Azure AD, or Okta.

The Multi-Tenant Challenge

When building SaaS platforms, each tenant (organization) often has different identity requirements. Tenant A might use Google Workspace, Tenant B relies on Azure AD, and Tenant C manages users internally. Your OAuth2 system must support all these scenarios without building separate auth flows for each tenant.

Identity Federation: The Bridge Pattern

Identity federation means delegating authentication to external identity providers (IdPs) while maintaining your own authorization layer. Think of it like airport security: different countries issue passports (external IdPs authenticate users), but each airport decides what gates you can access (your system authorizes based on scopes and roles).

Key Design Elements

Tenant-specific IdP mapping: Store configurations per tenant—which IdP they use, OAuth2 client credentials, redirect URIs, and custom claim mappings.

Dynamic authorization code flow: When a user from Tenant X logs in, redirect them to their configured IdP. After successful authentication, map external tokens to your internal access tokens with tenant-scoped claims.

Token claim normalization: External IdPs return different claim structures. Your system must normalize these into consistent internal tokens—extracting email, roles, and tenant ID regardless of source format.

Federated token refresh: Store mappings between your refresh tokens and external IdP refresh tokens, allowing seamless session extension across federated boundaries.

Scaling Considerations

Store tenant configurations in a fast cache layer (Redis) to avoid database lookups on every auth request. Use a distributed token validation service to handle introspection at scale across regions.

Key Takeaway: Multi-tenant OAuth2 with identity federation decouples authentication (delegated to tenant-specific IdPs) from authorization (centralized in your platform), enabling scalable, flexible auth that adapts to each organization's identity infrastructure.