Repository navigation
Description
Problem
When the embedded auth server's bearer validator (pkg/auth/token.go) finds that a session's upstream credential cannot be refreshed, it answers the request with 401 invalid_token. Most OAuth clients answer a 401 by redeeming their refresh token first. The embedded auth server's refresh_token grant succeeds, and the new access token keeps the same token session (tsid). So it points at the same dead upstream credential and gets the same 401.
A client that never falls back to a full authorization loops with no delay against the protected resource and /oauth/token. The MCP TypeScript SDK (v1.26.0) bounds this on POST, but not on its GET/SSE stream (_startOrAuthSse → _authThenStart → _startOrAuthSse). Only an interactive authorization fixes the session, because it creates a new tsid with a fresh upstream login. The client starts one only after the refresh grant fails.
Proposal
Three additive changes in pkg/authserver.
- Refresh-grant validation seam. Add an option to
authserver.Configthat registers extra fosite token endpoint handlers, appended after the built-in refresh handler.newAuthorizationServeralready takesextraFactoriesinternally, so this exposes what exists. An embedder's handler then runs after fosite has authenticated the client, loaded the refresh session, and checked the token's expiry, scope and client binding. It can refuse the grant withinvalid_grant. - Typed upstream refresh rejection. Today
formatOAuth2Error(pkg/authserver/upstream/oauth2.go) turns anoauth2.RetrieveErrorinto a plain string, so a caller cannot tell an IdP'sinvalid_grantfrom a network failure. Make a definitive rejection (the IdP answeredinvalid_grant) wrap a new exported sentinel. Carry it through the refresher andupstreamtoken.ErrRefreshFailed, soerrors.Isreaches it fromGetValidTokensandGetAllUpstreamCredentialscallers. - Default guard (optional, can be a follow-up). A built-in handler, on by default, that refuses the refresh grant and revokes the refresh token when the session's upstream credential is definitively dead: the row is absent, or its upstream refresh was rejected with the new sentinel. A transient upstream failure (5xx, network, storage) grants the refresh, since the validator answers those with 503 or a retryable 401 anyway.
The guard refreshes before deciding. Upstream refresh is lazy (only on a resource request), so an idle client's stored token is often expired while its upstream refresh token still works. Refusing on stored state alone would force every idle client to sign in again.
Acceptance criteria
- An embedder can register a token endpoint handler through
authserver.Config. It runs after the built-in refresh handler on arefresh_tokengrant, and returningfosite.ErrInvalidGrantfrom it yields HTTP 400invalid_grantwith no tokens issued and the refresh token not rotated. - A registered handler does not run on
authorization_code, device-code, revocation or introspection requests. - An upstream refresh the IdP rejects with
invalid_grantis distinguishable witherrors.IsfromGetValidTokensandGetAllUpstreamCredentials. A network error or a 5xx is not. - (If item 3 lands here) A refresh grant for a session whose upstream row is absent, or whose upstream refresh is rejected with
invalid_grant, is refused withinvalid_grantand its refresh token is revoked. An idle session whose upstream refresh succeeds is granted.
Context
An embedder (Stacklok's enterprise connector gateway) also challenges a session whose upstream ID token is unusable, and needs the seam to refuse the refresh grant for that cause too.
Activity
Let embedders refuse refresh grants on dead upstreams (#6902)
tgrunnagle commentedon Oct 22, 2026
Acceptance criteria status
#6902 (merged in 6cbec29) implements proposal items 1 and 2. Item 3, the default guard, is deferred to a follow-up, so this issue stays open for it.
authserver.Configruns after the built-in refresh handler;fosite.ErrInvalidGrantgives 400invalid_grant, no tokens issued, refresh token not rotatedConfig.RefreshGrantHandlers. All handlers are registered as one validator after the built-in factories, because fosite drops a second handler of the same type, and they run in order.TestIntegration_RefreshGrantHandlersregisters two handlers: when the second refuses, the response is 400invalid_grantwith no tokens, and the same refresh token still works afterwards. Handlers see the stored session'stsid, which shows they run after the built-in handler.authorization_code, device-code, revocation or introspectionrefresh_tokenis the sole grant type. The validator is registered only as a token endpoint handler, never as a revocation, introspection or authorize handler. Covered byTestRefreshGrantValidators_GrantTypes,TestRefreshGrantValidators_HandlerChain, and theauthorization_codecheck in the integration test.invalid_granton upstream refresh is distinguishable witherrors.IsfromGetValidTokensandGetAllUpstreamCredentials; a network error or 5xx is notupstream.ErrGrantRejected, classified by the existingoautherr.IsRejectedRefreshGrant.GetAllUpstreamCredentialsnow returnsfailed map[string]error, which is a breaking Go API change. Covered byTestUpstreamRefreshRejection_ReachesServiceCallers, which uses a real provider, refresher and service against a fake IdP, andTestFormatOAuth2Error_GrantRejected.Notes for the follow-up (item 3)
invalid_grantfor a credential that is still live. The refresher now re-reads storage and returns the winner's row. If the winner's write hasn't landed yet, the rejection still comes through. The guard should not revoke on a singleErrGrantRejectedwithout accounting for this.errorcode, notinvalid_grant. The guard should returnfosite.ErrInvalidGrant.Refuse refresh grants whose upstream credential is dead (#6930)