Repository navigation
Add a per-client access token lifespan for ID-JAG redemption - #7284
Conversation
A client that redeems ID-JAGs to call one MCP tool wants tokens that live seconds, while other clients keep the server default. The issue "Authorization server: narrow a JWT-bearer access token to one MCP call with authorization_details" (#7061) asks for a per-client lifetime for this. Delegate clients get an optional access_token_lifespan, a Go duration string like the server's own. Omitted means the server lifespan. A set value must be greater than 0 and at most 24h, checked when the configuration is loaded, so a bad value fails at startup and a zero never means unlimited. Startup also fails for a lifespan ID-JAG redemption could never apply: the JWT-bearer grant is off, or the client's grant_types omit it. The lifespans reach the ID-JAG handler through a new FactoryOption, WithClientAccessTokenLifespans. On ID-JAG redemption the token lives for the client's lifespan (or the server's), capped by the ID-JAG's remaining time and then by the delegated-access marker cap. A longer client lifespan is shortened silently. Plain JWT-bearer has no authenticated client and keeps the server lifespan; other grants are unchanged. Each factory now refuses an option it does not apply, so an option is never silently ignored. RunConfig only, no CRD field. Refs #7061 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7284 +/- ##
==========================================
+ Coverage 81.85% 81.90% +0.05%
==========================================
Files 915 919 +4
Lines 92853 93172 +319
==========================================
+ Hits 76001 76315 +314
- Misses 16847 16852 +5
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The grant check for access_token_lifespan had a direct unit test, but no test went through Config.Validate, so dropping the call there would have passed. Add a Validate row for a delegate client that sets a lifespan while the JWT-bearer grant is off. Co-Authored-By: Claude Opus 5.5 <[email protected]>
jhrozek
left a comment
There was a problem hiding this comment.
Thanks, this reads well and the lifetime order looks right to me. I couldn't find a way for a token to outlive the ID-JAG or the marker cap because of this change, and the fosite exp/expires_in/storage TTL all following the session expiry checks out.
A few things inline. The startup check one and the sub-second lifespan one are the ones I'd like to see fixed here; the rest are small.
Two questions that aren't tied to a line:
- A client lifespan can now be longer than the server access_token_lifespan, so the server value stops being a ceiling for ID-JAG tokens. I think that's intended given the PR body, just want to confirm that's what we want for #7061.
- The longer-than-server case only works because fosite takes exp and expires_in from the session expiry rather than the lifespan argument. The tests pin it, which is probably fine, but a fosite bump that changes this would quietly cap those tokens at the server lifespan. Worth a sentence in the issueBoundedAccessToken comment pointing at the test that guards it?
Refuse a per-client lifespan unless a trusted issuer accepts ID-JAG assertions. An enabled JWT-bearer grant whose issuers accept only plain assertions can never apply the lifespan, so startup now fails for it. Require a client lifespan of at least 2s. exp is signed in whole seconds, rounded down, so a sub-second lifespan could issue a token that is already expired after its single-use ID-JAG was consumed. Compute the token expiry once, from one clock reading, as the earliest of now plus the lifespan, the assertion's exp, and now plus the marker cap. Before, the remaining time was re-added to a later clock reading, so the token could outlive the ID-JAG by that gap. Also fix the expires_in rounding note in the docs, note the fosite behaviour the longer-than-server case relies on, and tidy test names and comments. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. On the two questions not tied to a line:
🤖 Addressed by Claude Code |
Summary
A client that redeems ID-JAGs to make one MCP call wants a token that lives for seconds, while every other client keeps the server's access-token lifespan. Today there is one lifespan for the whole server, so a narrow per-call token lives as long as any other token. "Authorization server: narrow a JWT-bearer access token to one MCP call with authorization_details" (#7061) asks for a per-client lifetime. This PR adds it; it is opt-in and changes nothing for a server that does not set it.
access_token_lifespan, a Go duration string. Omitted means the server lifespan. A set value must be at least2sand at most24h, checked when the configuration is loaded: smaller (including zero and negative) and larger values fail at startup, so a zero never means unlimited and a token is never dead on arrival.FactoryOption,WithClientAccessTokenLifespans, now accepted byIDJAGIssuanceFactory. The map stays on the ID-JAG handler and is copied into the redemption.redeemer.assertionBoundedExpiry(wasassertionBoundedLifetime) computes the token's expiry once, from one clock reading: the earliest of now plus the client's lifespan (the server lifespan when the client sets none), the ID-JAG's ownexp, and, for a marked token, now plus the marker cap.expandexpires_inboth follow it, so a token never outlives its ID-JAG.grant_typesomit the JWT-bearer grant) fails startup instead of being ignored.FactoryOptionit does not apply (the token-exchange factories refuse client lifespans; the ID-JAG factory refuses an authorization-details registry), so an option is never silently ignored.Part of #7061
Type of change
Test plan
task test)task test-e2e)task lint-fix)Also ran:
go test -race ./pkg/authserver/...,golangci-lint run ./pkg/authserver/...,task docs,task docs-verify,task sdk-test,task sdk-lint.New tests:
0, negative,100msand1999msrefused,2sand exactly24haccepted,24h+1srefused, unset falls back to the server lifespan. Checked on the RunConfig (TestDelegateClientRunConfigValidate), on a resolvedConfig(TestValidateResolvedDelegateClients_AccessTokenLifespan), inresolveDelegateClientsand at the ID-JAG factory; an unparsable value is refused too. A lifespan is refused when no trusted issuer accepts ID-JAG assertions (including aConfig.Validatecase whose only issuer accepts plain assertions) or the client'sgrant_typesomit the grant.TestRedeemer_ExpiryIsTheAssertionExpirypins that the expiry is the assertion'sexpexactly when that is the tighter bound.TestRedeemer_ClientAccessTokenLifespan): client lifespan shorter than the assertion, longer than the assertion (silently shortened), longer than the server lifespan, a client without a value gets the server lifespan, the plain grant ignores it. Each checks bothexpandexpires_in.TestRedeemer_DelegatedAccessLifetimerows with a client lifespan above and below the five-minute cap;TestIDJAGResolve_ClientLifespanUnderMarkerCapshows a resolved (marked) redemption carries the client lifespan and the cap still wins.access_token_lifespan: 20son AS-2's RunConfig and gets a token withexpires_inandexpof about 20 seconds from a real AS-1 ID-JAG.Changes
pkg/authserver/config.goDelegateClientRunConfig.AccessTokenLifespan,AccessTokenLifespanDuration, bounds check in both validators,DelegateClient.AccessTokenLifespanpkg/authserver/runner/embeddedauthserver.goresolveDelegateClientsparses the lifespanpkg/authserver/server_impl.godelegateAccessTokenLifespans; passes the option to the ID-JAG factorypkg/authserver/server/tokenexchange/factory.goWithClientAccessTokenLifespans; token-exchange factories refuse itpkg/authserver/server/tokenexchange/id_jag_handler.gooptions ...FactoryOptiononIDJAGIssuanceFactory,clientLifespans, bounds checkpkg/authserver/server/tokenexchange/id_jag_resolve.gopkg/authserver/server/tokenexchange/redeemer.goredemption.clientLifespan; lifetime order inassertionBoundedLifetimedocs/arch/17-token-exchange-delegation.mddocs/server/*,sdk/go/*Does this introduce a user-facing change?
Yes, for operators who opt in.
access_token_lifespan, the ID-JAG's remaining time, and (for a marked token) five minutes. There is no per-client setting.access_token_lifespan(for example30s). For that client's ID-JAG redemptions it replaces the server lifespan in that list; it can be shorter or longer than the server lifespan. The ID-JAG's remaining time and the marker cap still apply, so a longer client lifespan is shortened silently, with no error. Clients without it, plain JWT-bearer and every other grant behave as before. Startup fails for a value of0, a negative value or one above24h, and for a lifespan the client could never use (JWT-bearer grant not enabled, or the client'sgrant_typesomit it).Operator note: to get the lifespan you configure, set the AS-1 target's ID-JAG lifetime to at least that lifespan plus the expected redemption delay. A ToolHive AS-1 issues ID-JAGs for 60 seconds by default and at most 300 seconds, so a client lifespan above 300 seconds is never reached.
Special notes for reviewers
access_token_lifespanset,delegateAccessTokenLifespansreturns nil and the redeemer takes the server lifespan exactly as before.fosite'sIssueAccessTokentakesexpires_infrom the session expiry, not from the lifespan argumentpopulatepasses, so a client lifespan longer than the server lifespan is honored; the unit and integration tests pin this.IDJAGIssuanceFactorynow refusesWithAuthorizationDetailsRegistry. Nothing passes it today. The branch that adds theauthorization_detailsclaim to ID-JAG redemption replaces that refusal with real handling, so the registry cannot be wired in early and ignored.token_exchange.id_jagprecedent (errIDJAGRequiresTokenExchange): no trusted issuer accepts ID-JAG assertions (idJAGRedeemable; an enabled JWT-bearer grant whose issuers accept only plain assertions is not enough), or the client setsgrant_typeswithout the JWT-bearer grant. This check runs on the resolvedConfig(validateDelegationConfig), because both are decided from the resolved trusted issuers; the bounds are also checked atRunConfigvalidation.fositetakesexpandexpires_infrom the session expiry whenever it is set, so the server lifespan thatpopulatepasses toissueBoundedAccessTokenis only the fallback for a session with no expiry. That function's comment now says so; before this change the redeemer could not reach the case where the session expiry is later than that argument.WithClientAccessTokenLifespansdoes not refuse a key that names no configured client. The ID-JAG handler also serves DCR, CIMD and SPIFFE clients, so the factory has no client list to check against; the production map comes only fromdelegate_clients.delegate_clientscan set a lifespan; DCR, CIMD and SPIFFE-association clients that redeem ID-JAGs keep the server's.expis signed in whole seconds, rounded down, andexpires_inis truncated, so it is usually one second less than a whole-second lifespan. That is why a set value must be at least2s(server.MinClientAccessTokenLifespan): below that a token could be expired on arrival after its single-use ID-JAG was consumed.Confirmed in the issue discussion
The lifespan rule is confirmed in the issue discussion on #7061, item 7: at most 24h, checked when the configuration is loaded so a bad value fails at startup. Review raised the lower bound from greater than 0 to at least 2s, for the rounding reason above. The tests cover each case.
🤖 Generated with Claude Code