Skip to content

Storage for grants issued by token exchange #6895

Closed

Description

@jhrozek

Part of #6886. Needed by #6897.

Problem: refresh tokens issued by token exchange need state that fosite's refresh-token storage cannot hold: one grant per subject-token jti, written atomically with the replay marker; a parent link checked on every use; and revocation that leaves a record. They must also stay apart from ordinary refresh tokens, so grant_type=refresh_token can never redeem them.

Change: an optional storage capability, ExchangeGrantStorage, found the same way as DeviceCodeStorage. Startup fails when a client enables token-exchange refresh tokens and the storage lacks it. Memory and Redis backends.

Record (ExchangeGrant):

  • Signature: the refresh token's HMAC signature, also the grant's internal ID; never exposed;
  • ParentSignature: empty for a top-level grant;
  • ClientID, Subject, SubjectIdentifier (the sub_id JSON, if present), and UpstreamSubject (the subject token's https://toolhive.dev/upstream_subject claim: issuer, claim, and value, if present). A child copies both from its parent;
  • SourceJTI, SourceExpiresAt: the subject access token's jti and exp; empty for child grants;
  • IssuableTokenTypes: exactly one token type URN;
  • ExpiresAt, CreatedAt, RevokedAt (zero while live).

Operations:

  • CreateExchangeGrant: atomic. Refuses a reused (issuer, SourceJTI) with fosite.ErrJTIKnown and a duplicate signature with ErrAlreadyExists, and writes nothing on failure. The jti marker holds the grant's signature.
  • GetExchangeGrant: returns revoked grants too; ErrNotFound once the record has expired.
  • RevokeExchangeGrant: sets RevokedAt once; idempotent.

Rules:

  • A child is usable only while it is live and its parent exists and is live. A missing parent counts as revoked. Revocation marks one record: no children index, no cascade walk.
  • Depth is 1: a child cannot be a parent.
  • Record TTL is ExpiresAt; the jti marker TTL is SourceExpiresAt plus a margin. Time decisions are made in code (Live(now)); Redis TTL is only garbage collection.
  • Redis: one hash per record under the existing hash-tagged prefix. One Lua script creates a grant, running all checks before writing and touching only declared keys. Revoke is a single conditional write. No error is swallowed.

No user index, session key, owner, tombstone, or stored revocation reason: the client keeps those.

Acceptance, on both backends:

  • concurrent creates with one jti leave one grant;
  • revoking a child leaves its parent and siblings usable;
  • revoking a parent makes its children unusable;
  • an expired grant reads as not found;
  • store errors are returned, not logged and dropped;
  • a decorator that hides the capability makes startup fail.

Activity

  1. added
    needs-triage
    on Oct 21, 2026
  2. removed
    needs-triage
    on Oct 24, 2026
  3. self-assigned this
    on Oct 24, 2026
  4. changed the title [-]Storage for refresh tokens issued by token exchange[/-] [+]Storage for grants issued by token exchange[/+] on Oct 27, 2026
  5. jhrozek commented on Oct 30, 2026

    @jhrozek
    ContributorAuthor

    The storage capability, the memory and Redis backends and their contract tests are on main. The last pieces, the UpstreamSubject field on the grant record and the startup failure when a client enables the feature on a store without the capability, are in #7003. Closing here; the rest of this work is tracked in #6897.

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