Skip to content

Share the authorization_details reader - #7217

Open
jhrozek wants to merge 2 commits into
mainfrom
tokenexchange-refactor-1-details-reader
Open

jhrozek wants to merge 2 commits into
mainfrom
tokenexchange-refactor-1-details-reader

Conversation

@jhrozek

@jhrozek jhrozek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Part of #7061 (Authorization server: narrow a JWT-bearer access token to one MCP call with authorization_details).

Why. The shape check for the authorization_details form parameter is copied by hand in two places (token exchange and exchange-grant opening). The hook authorizationdetails.Subject is built field by field in two places. The two Subject literals have already drifted: token exchange sets External, exchange-grant opening does not.

The next steps for #7061 need the same reading and the same Subject in two more places. This PR adds one helper for each, so those steps reuse them instead of adding a third and fourth copy:

Later change Uses What it does with it
ID-JAG redemption refuses the authorization_details parameter (the entries come from the assertion's claim) readDetailsParam Absent or a single "" passes. One value, or any repeat (["",""] included), is refused. "Repeated" means the same thing as in token exchange.
ID-JAG redemption calls the hook for the assertion's entries detailsSubject Builds the Subject from the assertion's claims, then sets External: true and removes the authorization_details key from Claims. This is why detailsSubject returns a value, not a pointer.
ID-JAG minting signs the requested entries into the ID-JAG readDetailsParam Replaces minting's current rule, under which ["",""] counts as absent. After that, every reader of the parameter agrees on what "repeated" means.

Without the helpers, each of those rows is another hand-written copy. The two copies that already exist have drifted.

What.

  • readDetailsParam(form) (raw, present, err) only reports the shape: absent (or one ""), repeated (errRepeatedDetailsParam), or one value. Each caller keeps its own policy and wire error. Token exchange ignores an absent parameter. Exchange-grant opening requires it.
  • detailsSubject(claims) builds the hook Subject and copies Audience and Claims, so a hook cannot change the validated claims.
  • Token exchange (parseAuthorizationDetails, grantAuthorizationDetails) and exchange-grant opening (parseExchangeGrantDetails, decideEntry) use them.
  • ID-JAG minting (checkIDJAGExtraParameters) is not changed in this PR.
  • New tests pin the behaviour that had no test: the hook Subject at exchange-grant opening, the reason=repeated and reason=malformed WARN fields, the exchange-grant refusal log line, and the ["",""] and ["","x"] shapes.

Type of change

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

Test plan

  • Unit tests (task test)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Ran go test -race -count=1 ./pkg/authserver/server/tokenexchange/... ./pkg/authserver/ on the branch head. Both packages pass.

Does this introduce a user-facing change?

No. Every shape of the parameter (absent, [""], ["",""], ["","x"], ["x"], ["x","y"], malformed, no registry configured) gives the same error code, description, status and log line as before.

Special notes for reviewers

  • The one semantic change. At exchange-grant opening, Subject.External is now set from the claims rather than left at its zero value. It is always false there, because checkLoginToken refuses tokens from an external issuer before the hook runs. That Subject goes only to ToolHive's internal exchange-grant shape hook, which doesn't read it. An embedder's registry is never called on this path. The new TestRefreshTokenHandlerHookSubject pins every field.
  • The new tests also pass on the old code. That is intended: they pin the behaviour the refactor must keep.
  • ID-JAG minting still treats ["",""] as absent. That difference already exists and stays until the minting change in the table above replaces it.

jhrozek and others added 2 commits November 22, 2026 22:56
The shape check for the authorization_details form parameter is copied
by hand in two places, and the hook Subject is built field by field in
two places. The two Subject literals have already drifted: the one for
token exchange sets External, the one for exchange-grant opening does
not.

The planned work to narrow a JWT-bearer access token to one MCP call
(#7061) needs the same reading and the same Subject in two more
places:

- ID-JAG redemption will refuse the authorization_details parameter,
  because the entries come from the assertion's claim instead. It
  reads the parameter with readDetailsParam, so a repeated parameter,
  two empty values included, is treated the same as here.
- ID-JAG redemption will build the hook Subject for the assertion with
  detailsSubject, then set External to true and remove the
  authorization_details key from Claims. detailsSubject returns a
  value so that the caller can change it.
- ID-JAG minting will read the parameter with readDetailsParam and
  sign the entries into the ID-JAG. That replaces its current rule,
  under which two empty values count as absent, so all readers then
  agree on what "repeated" means.

Without the helpers each of those adds another copy of the check and
of the Subject literal, and the existing copies show that they drift.

Add readDetailsParam, which only reports the shape (absent, repeated
or one value), and detailsSubject, which returns the hook Subject as a
value. Token exchange and exchange-grant opening use them and keep
their own policy and wire text. ID-JAG minting keeps its own rule for
now.

At exchange-grant opening External is now set from the claims. It is
always false there, because the login token check refuses external
tokens first, and only ToolHive's internal shape hook sees it. No
error, description, status or log line changes.

Add tests for the two-empty-values shape and for the repeated and
malformed WARN fields, which no test asserted before.

Co-authored-by: Mecatl <noreply@mecatl.dev>
The hook Subject at exchange-grant opening had no test, so a later
change to detailsSubject could drop a field or stop copying Claims or
Audience without any test failing. The later ID-JAG redemption change
builds on detailsSubject, so this is the pin that matters most.

Add a test that records the Request the exchange-grant hook gets and
checks every Subject field, that External is false, and that Claims
and Audience are copies. Add a test for the WARN line when an
exchange-grant shape is refused, rows for ["",""] and ["","x"] in the
token-exchange and exchange-grant tables, and require the token
exchange WARN line to appear exactly once.

Co-authored-by: Mecatl <noreply@mecatl.dev>
@jhrozek
jhrozek requested a review from ChrisJBurns as a code owner November 22, 2026 21:57
@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label Nov 22, 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: a behaviour-preserving refactor that removes real drift between the two copies.

Design: The authorization_details parameter check and the authorizationdetails.Subject construction both belong in pkg/authserver/server/tokenexchange, which owns the parameter and the claims, so the shared helpers sit in the right package. Both readers (parseAuthorizationDetails, parseExchangeGrantDetails) and both claim-based Subject builders now use them. decideChildEntry correctly keeps its own minimal Subject because there are no validated claims on that path, and checkIDJAGExtraParameters is deliberately left alone, as the PR says. The one semantic change, Subject.External now being set in decideEntry, is unreachable today because checkLoginToken refuses a non-empty ExternalIssuer before decideEntry runs. There is no wire or operator impact on upgrade.

Optional, not blocking:

  • authdetails.go:75 and exchange_grant.go:101 treat any error from readDetailsParam as "repeated". If it ever returns a second error kind, both callers will misreport it. errors.Is(err, errRepeatedDetailsParam) would keep that safe.
  • A one-line comment on decideChildEntry saying why it doesn't use the shared Subject helper would stop someone "fixing" it later.

🤖 Reviewed with Claude Code

@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.59%. Comparing base (112147d) to head (9110fb1).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7217   +/-   ##
=======================================
  Coverage   81.59%   81.59%           
=======================================
  Files         898      898           
  Lines       91621    91622    +1     
=======================================
+ Hits        74760    74761    +1     
  Misses      16856    16856           
  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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants