Skip to content

authserver: let embedders validate refresh-token grants, and type upstream refresh rejections #6901

Description

@tgrunnagle

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.

  1. Refresh-grant validation seam. Add an option to authserver.Config that registers extra fosite token endpoint handlers, appended after the built-in refresh handler. newAuthorizationServer already takes extraFactories internally, 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 with invalid_grant.
  2. Typed upstream refresh rejection. Today formatOAuth2Error (pkg/authserver/upstream/oauth2.go) turns an oauth2.RetrieveError into a plain string, so a caller cannot tell an IdP's invalid_grant from a network failure. Make a definitive rejection (the IdP answered invalid_grant) wrap a new exported sentinel. Carry it through the refresher and upstreamtoken.ErrRefreshFailed, so errors.Is reaches it from GetValidTokens and GetAllUpstreamCredentials callers.
  3. 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 a refresh_token grant, and returning fosite.ErrInvalidGrant from it yields HTTP 400 invalid_grant with 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_grant is distinguishable with errors.Is from GetValidTokens and GetAllUpstreamCredentials. 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 with invalid_grant and 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

  1. added
    needs-triage
    on Oct 21, 2026
  2. self-assigned this
    on Oct 21, 2026
  3. removed
    needs-triage
    on Oct 21, 2026
  4. tgrunnagle commented on Oct 22, 2026

    @tgrunnagle
    CollaboratorAuthor

    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.

    # Criterion Status Evidence
    1 Embedder handler via authserver.Config runs after the built-in refresh handler; fosite.ErrInvalidGrant gives 400 invalid_grant, no tokens issued, refresh token not rotated ✅ Met Config.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_RefreshGrantHandlers registers two handlers: when the second refuses, the response is 400 invalid_grant with no tokens, and the same refresh token still works afterwards. Handlers see the stored session's tsid, which shows they run after the built-in handler.
    2 Handler does not run on authorization_code, device-code, revocation or introspection ✅ Met Handlers run only when refresh_token is the sole grant type. The validator is registered only as a token endpoint handler, never as a revocation, introspection or authorize handler. Covered by TestRefreshGrantValidators_GrantTypes, TestRefreshGrantValidators_HandlerChain, and the authorization_code check in the integration test.
    3 IdP invalid_grant on upstream refresh is distinguishable with errors.Is from GetValidTokens and GetAllUpstreamCredentials; a network error or 5xx is not ✅ Met New sentinel upstream.ErrGrantRejected, classified by the existing oautherr.IsRejectedRefreshGrant. GetAllUpstreamCredentials now returns failed map[string]error, which is a breaking Go API change. Covered by TestUpstreamRefreshRejection_ReachesServiceCallers, which uses a real provider, refresher and service against a fake IdP, and TestFormatOAuth2Error_GrantRejected.
    4 Default guard refuses and revokes on an absent or rejected upstream; an idle session whose upstream refresh succeeds is granted ⏳ Deferred Not implemented; left for the follow-up.

    Notes for the follow-up (item 3)

    • Lost replica race: with an IdP that rotates refresh tokens, the replica that loses a cross-replica refresh race gets invalid_grant for 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 single ErrGrantRejected without accounting for this.
    • Plain errors: a plain Go error from a refresh-grant handler answers HTTP 500 with fosite's generic error code, not invalid_grant. The guard should return fosite.ErrInvalidGrant.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions