Skip to content

Extract the shared JWT-bearer redemption tail - #7249

Merged
jhrozek merged 1 commit into
mainfrom
tokenexchange-refactor-3-redeemer
Nov 23, 2026
Merged

jhrozek merged 1 commit into
mainfrom
tokenexchange-refactor-3-redeemer

Conversation

@jhrozek

@jhrozek jhrozek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

The plain RFC 7523 JWT-bearer handler and the ID-JAG handler each kept their own copy of the steps that run after their checks: consume the assertion, cap the token lifetime at the assertion's expiry, build the session, set the granted audience, and issue the token. On top of that, IDJAGHandler held a whole *JWTBearerHandler as core and reached into its fields (h.core.policies, h.core.consumer, h.core.config, h.core.validator, ...) to reuse them.

Several planned changes touch exactly those steps, so with two copies each of them would have to be written twice:

  • Signed authorization_details on the assertion: a registry hook has to run on the claim before the assertion is consumed, and the result has to land in the issued token. Before this PR, that is one edit in each handler's tail plus each of the two session builders. After it, one step at the top of redeemer.complete, and one new redemption field.
  • Subject mapping for ID-JAG (actor check, then resolving the subject to a local user): the resolved user ID and a delegated-access marker have to reach the session builder and the lifetime cap. After this PR they are two redemption fields that the ID-JAG front half fills in. Before, they would go through newIDJAGSession and h.core.assertionBoundedLifetime.
  • Per-client token lifespan: one change in redeemer.assertionBoundedLifetime, which both handlers now call, instead of a field on the plain handler that the ID-JAG handler reads through core.

What changed:

  • New unexported redeemer (redeemer.go) holding the replay consumer, the fosite config and the token helper. complete runs consume, lifetime, client replacement (plain only), audience and session. populate issues the token.
  • Each handler keeps its own front half: matching, client checks, validation, policy lookup, resource choice, subject binding and refusal mapping. It also keeps its own validator, token endpoint and policy map, filtered by its assertion type. It builds a redemption value and calls the redeemer.
  • IDJAGHandler no longer holds a JWTBearerHandler, and newIssuanceCore is gone. The redeemer does not keep the construction inputs, so the unfiltered trusted issuer list is not reachable at request time.
  • Both constructors run issuanceDeps.validate() first. That removes the runtime guards for a half-built handler ("The JWT-bearer grant is not fully configured.", "JWT-bearer token issuance is not configured."), which could no longer be reached, and the test-only constructor newJWTBearerHandler.

No response, error text, status, replay key or log line changes.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (pkg/authserver/server/tokenexchange/... and pkg/authserver)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

The tests for the shared steps moved into redeemer_test.go. They still run through each handler's HandleTokenEndpointRequest and PopulateTokenEndpointResponse, not against the redeemer directly. A gomock consumer pins the exact replay purpose, issuer, key and expiry per handler. They also cover a few things that were not tested before:

  • the plain handler does not copy act;
  • the plain handler replaces a public client that fosite resolved;
  • the no-jti replay key;
  • the ID-JAG handler accepts a requester that is not a *fosite.AccessRequest.

Changes

File Change
redeemer.go New: redeemer, redemption, and the consume, lifetime, session and populate steps moved from the plain handler
jwt_bearer_handler.go Front half only; drops newJWTBearerHandler, newIssuanceCore, the embedded HandleHelper and the nil guards
id_jag_handler.go Own validator, endpoint, policy map and audience strategy in place of core; newIDJAGSession folded into the redeemer
issuance_deps.go checkValidatorAndEndpoint inlined into validate(), same errors and order
docs/arch/17-token-exchange-delegation.md Lists redeemer.go and issuance_deps.go among the key files

Special notes for reviewers

  • One small reorder that changes nothing on the wire: the ID-JAG draft-departure log line now runs after the audience and session are set, not just before. It still runs only when the redemption succeeds, and nothing after the lifetime step can fail on the ID-JAG path, so the set of requests that log is the same.
  • The redeemer intentionally does not embed issuanceDeps. Embedding would put the unfiltered trusted issuer list one field away from both handlers at request time. Filtering that list per assertion type is what keeps a jwt_bearer-only issuer out of the ID-JAG grant, and the reverse.
  • redemption.syntheticClientID is one field, not a flag plus an ID, so the session's client_id cannot disagree with the client on the request. When it is set (plain), the request's client is replaced. When it is empty (ID-JAG), the authenticated client is used.

The ID-JAG handler held a whole plain JWT-bearer handler and reached
into its fields, and both handlers kept their own copy of the steps
after their checks. Per-client lifespans, the signed
authorization_details claim and subject mapping all change those
steps, and with one copy each lands once.

A new unexported redeemer holds the replay consumer, the fosite config
and the token helper. Each handler keeps its own checks, validator,
token endpoint and policy map filtered by its assertion type, builds a
redemption value, and calls the redeemer to finish and to issue the
token. The redeemer does not embed the construction dependencies, so
the unfiltered trusted issuer list stays out of reach at request time.

The constructors validate every dependency first, so the runtime guards
for a half-built handler and the test-only plain constructor go. No
response, error text or log line changes.

Co-authored-by: Mecatl <[email protected]>
@jhrozek
jhrozek requested a review from ChrisJBurns as a code owner November 23, 2026 14:25
@github-actions github-actions Bot added the size/XL Extra large PR: 1000+ lines changed label Nov 23, 2026

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve. This is a behaviour-preserving extraction and I found no regression.

Design: The post-validation tail (consume the assertion, cap the lifetime, install the session and audience, issue the token) is one invariant, and it now lives in a single unexported redeemer. Each handler keeps its own front half (matching, client checks, validation, policy lookup, refusal mapping). IDJAGHandler no longer borrows a whole JWTBearerHandler and reaches into its fields. The redeemer is a named field rather than an embedded type, so no fosite handler methods are promoted. No wire, CRD, config or exported-API change on upgrade. The removed nil guards are unreachable because issuanceDeps.validate() rejects those cases first. Check order, replay key and purpose, and error mapping are unchanged for both handlers. The only reorder is the ID-JAG draft-departure log, which still runs only on success.

🤖 Reviewed with Claude Code

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.66%. Comparing base (740e467) to head (09166f6).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7249      +/-   ##
==========================================
- Coverage   81.66%   81.66%   -0.01%     
==========================================
  Files         903      904       +1     
  Lines       91738    91829      +91     
==========================================
+ Hits        74915    74988      +73     
- Misses      16818    16836      +18     
  Partials        5        5              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhrozek
jhrozek merged commit 443b23f into main Nov 23, 2026
32 checks passed
@jhrozek
jhrozek deleted the tokenexchange-refactor-3-redeemer branch November 23, 2026 14:37
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Extra large PR: 1000+ lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants