Skip to content

providers/oauth2: token exchange - #23900

Merged
BeryJu merged 8 commits into
goauthentik:mainfrom
aubinmora:providers/oauth2-token-exchange
Jul 10, 2026
Merged

BeryJu merged 8 commits into
goauthentik:mainfrom
aubinmora:providers/oauth2-token-exchange

Conversation

@aubinmora

@aubinmora aubinmora commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Details

What does this PR change?

Adds RFC 8693 token exchange (urn:ietf:params:oauth:grant-type:token-exchange) to the OAuth2 provider, implementing impersonation: a client trades a subject token it already holds for an access token issued by another authentik provider, carrying the same user's identity.

  • New GrantType.TOKEN_EXCHANGE, opt-in per provider (the migration does not enable it on existing providers).
  • Subject tokens are verified through the existing JWT federation trust (jwt_federation_providers / jwt_federation_sources) — same mechanism as the machine-to-machine JWT flow. No new trust model, no new model fields.
  • Supported token types: access_token and jwt (authentik access tokens are JWTs, so both mean the same thing). requested_token_type defaults to access_token.
  • Confidential clients must authenticate; the subject token is not a substitute for client credentials.

Some parameters are refused rather than silently ignored, so a client is never misled about a restriction:

  • actor_token → invalid_request (delegation not implemented, see below)
  • audience / resource → invalid_target (token targeting not implemented, RFC 8693 §2.2.2)

Why is this change needed?

Token exchange is the standard way for service A to call service B on behalf of the user who called A, without forwarding a token B was never issued for. authentik already has the primitives (federated provider trust, JWT access tokens, scope mappings, policies), so this is mostly wiring.

How was this tested?

New test_token_exchange.py (one file per grant, following the existing convention): success path, rejection of actor_token/audience/resource, unsupported token types, bad/expired/untrusted subject tokens, and the client-auth requirement. Full providers/oauth2 suite passes (15 new tests, no regressions); mypy --strict, bandit, and the web build/typecheck are clean.

Linked issues


Note: delegation is out of scope

This does impersonation only. Delegation (actor_token + the act claim) is a clean follow-up — actor_token is optional in the spec and act is additive, so nothing here blocks it. The open question is what authorizes an actor to act for a subject (honor may_act, an explicit allowlist, or a policy). Happy to take direction before building it.

Three deliberate choices, flagging for review

  1. No exp check in the handler — unlike the sibling client_credentials JWT flow. PyJWT.decode already verifies exp, so an expired token fails during signature verification and never reaches such a check (the sibling's block is dead code). Covered by test_expired_subject_token.
  2. No id_token in the response — RFC 8693 §2.2.1 defines no such field, and minting an auth receipt for a non-interactive client seemed wrong. Easy to add for consistency if preferred.
  3. The new msg() string has no explicit id — web/AGENTS.md asks for one, but no entry under web/src/admin/providers/ has one; I matched the neighbours rather than be the lone exception. Will add if you give me the ID.

Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)

aubinmora added 6 commits July 9, 2026 22:51
Add the RFC 8693 grant type identifier, the token type identifiers for
access tokens and JWTs, and the invalid_target error code. The grant is
added to the available choices only; existing providers are not opted in.

Signed-off-by: Aubin Morand <[email protected]>
Add the token exchange grant to the token endpoint, implementing the
impersonation case: a subject token issued by a federated provider or
source is traded for an access token that speaks for the same user.

Subject tokens are verified through the existing JWT federation trust,
so a provider may only exchange tokens it already trusts for
machine-to-machine authentication. Confidential clients must
authenticate; the subject token is not a substitute for client
credentials.

Delegation is out of scope, so an actor token is refused rather than
ignored. Token targeting is likewise unimplemented, so audience and
resource are refused with invalid_target as required by RFC 8693
section 2.2.2. Both would otherwise leave a client believing a
restriction had been applied when it had not.

Signed-off-by: Aubin Morand <[email protected]>
Cover the impersonation path, the rejection of delegation and token
targeting, unsupported token types, untrusted and unverifiable subject
tokens, and the client authentication requirement.

Signed-off-by: Aubin Morand <[email protected]>
Describe the trust configuration the grant relies on, the supported
token type identifiers, and the parameters authentik refuses rather
than silently ignores.

Signed-off-by: Aubin Morand <[email protected]>
Add the grant to the selectable list. It is deliberately absent from the
default grant types, so the grant stays opt-in for new providers.

Signed-off-by: Aubin Morand <[email protected]>
@aubinmora
aubinmora requested review from a team as code owners July 9, 2026 23:20
@netlify

netlify Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-storybook ready!

Name Link
🔨 Latest commit 123cdd0
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/6a50f89eb586c10008f52b7b
😎 Deploy Preview https://deploy-preview-23900--authentik-storybook.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jul 9, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit ad2a901
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6a502cdc037aab000853e7d9
😎 Deploy Preview https://deploy-preview-23900--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@BeryJu BeryJu changed the title Providers/oauth2 token exchange providers/oauth2: token exchange Jul 10, 2026
Signed-off-by: Jens Langhammer <[email protected]>
@codecov

codecov Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.83784% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.20%. Comparing base (91d3b1e) to head (123cdd0).

Files with missing lines Patch % Lines
authentik/providers/oauth2/views/token.py 92.72% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23900      +/-   ##
==========================================
- Coverage   93.23%   93.20%   -0.03%     
==========================================
  Files        1043     1044       +1     
  Lines       60990    61174     +184     
  Branches      424      424              
==========================================
+ Hits        56862    57019     +157     
- Misses       4128     4155      +27     
Flag Coverage Δ
conformance 36.22% <6.48%> (-0.10%) ⬇️
e2e 41.40% <6.48%> (-0.11%) ⬇️
integration 32.60% <6.48%> (-0.13%) ⬇️
rust 0.00% <ø> (ø)
unit 92.31% <97.83%> (+0.01%) ⬆️
unit-migrate 92.35% <97.83%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@BeryJu
BeryJu merged commit 37669f0 into goauthentik:main Jul 10, 2026
112 of 115 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants