Implement Standard Token Exchange for Authorization
- Deciders: Julian Bäume, Maik Schäfer
- Date: 2025-10-07
- Replaces: ADR 0010
Status
Accepted
Context
ADR-0010 used OIDC Client Credentials Grant with a custom Keycloak mapper for user impersonation. This approach had significant drawbacks: custom JavaScript extensions required packaging and deployment, created maintenance burden across environments, posed upgrade risks, and tied us to Keycloak-specific implementation details.
The core requirement remains: Teams must authenticate with App Suite Middleware on behalf of users. Users authenticate via Core UI or Switchboard (receiving JWTs from these services), but Teams needs Keycloak-issued JWTs with proper audience and scopes to call MW APIs. This is a privileged operation requiring service authentication.
Token exchange with impersonation (RFC 8693) solves this by having Teams validate user tokens locally (from Core UI or Switchboard), obtain its own service account token from Keycloak, then exchange it for a user token using impersonation. Teams acts as the trust bridge between the user token issuer and Keycloak.
Alternative Approaches Considered
The only real alternative was to keep the custom mapper from ADR-0010, but this was rejected due to maintenance burden, non-standard approach, and coupling to Keycloak implementation details.
Decision
We will use RFC 8693 OAuth 2.0 Token Exchange with Impersonation with Keycloak instead of the custom mapper approach.
Teams validates user tokens locally (Core UI or Switchboard), obtains a service account token from Keycloak, then exchanges it for a user token using impersonation. This requires Keycloak with the token-exchange (v1) preview feature enabled for impersonation support, and the Teams service account must have impersonation and view-users roles.
Configuration is controlled via environment variables: TOKEN_EXCHANGE_ISSUER_URL enables token exchange when set, TOKEN_EXCHANGE_CLIENT_ID and TOKEN_EXCHANGE_CLIENT_SECRET for Keycloak service authentication, ISSUER_ALLOWLIST for user token validation (supports wildcards like *.example.com), and optional AUDIENCE for token scoping.
See Keycloak Setup Guide for detailed configuration steps.
Token Types
Only short-lived access tokens are used. Refresh tokens and offline tokens are not required since Teams can request new tokens via the service account at any time.
Consequences
Positive
- Standards-based approach using RFC 8693 (well-documented IETF standard)
- Eliminates custom Keycloak code and deployment complexity
- Better maintainability and upgrade safety with standard protocols
- Improved security with proper audit trails and automatic scope filtering
- Teams controls user token validation logic (supports multiple issuers via allowlist)
Negative
- Requires
token-exchange(v1) preview feature enabled in Keycloak (impersonation not yet supported in v2) - Existing deployments need reconfiguration
- User identities must match between user tokens (Core UI/Switchboard) and Keycloak usernames (or static mapping can be used, but nothing dynamic)
Technical Debt Reduction
Eliminates technical debt from ADR-0010: removes custom mapper code, packaging and deployment of Keycloak extensions, and aligns with OAuth 2.0 best practices.
Security
Token exchange requires valid user identity (more restrictive than service account), Keycloak logs all operations for audit, tokens scoped to specific audiences, and service account permissions granularly controlled via roles.
Teams runs in the same Kubernetes namespace as other App Suite services, with access to shared secrets (database credentials, Redis sessions, browser cookies). Token exchange does not grant additional privileges—it formalizes API access that would otherwise require direct database/session manipulation. The impersonation flow provides proper audit trails and follows OAuth 2.0 best practices.

