Skip to content

Match access-token TTL to the II grant duration the user picks - #43

Merged
aterga merged 1 commit into
mainfrom
claude/mcp-session-expiration-1q2j64
Jul 9, 2026
Merged

aterga merged 1 commit into
mainfrom
claude/mcp-session-expiration-1q2j64

Conversation

@aterga

@aterga aterga commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

What & why

A user who set their MCP session to e.g. 1 week on Internet Identity's connect consent screen still had to reconnect after ~1 hour. Root cause: the OAuth access token was hard-capped at 1h (TOKEN_TTL) regardless of the grant duration chosen, and no refresh token is issued. So the client's token always died at ≤1h and — with nothing to refresh against — had to re-run the full interactive authorization-code flow (surfaced as "reconnect"). The longer II grant was effectively unusable by the client.

The chosen fix (no refresh tokens, no server-side persistence): make the access token's lifetime track the II grant instead of capping it at a fixed 1h.

Changes

  • src/auth.rs
    • capped_token_ttl → token_ttl: when the grant expiration is known, return its full remaining lifetime (the old .min(TOKEN_TTL) cap is gone), so the session duration the user picked (10 min up to 30 days) is exactly how long the client's token stays valid. An already-expired grant still yields a zero TTL (token born invalid → steer to re-auth).
    • TOKEN_TTL (1h) is retained only as a fallback for when the grant expiration is unknown at issue time (e.g. a dropped connect-completion POST). The II grant is the hard ceiling either way.
    • Updated the unit test (token_ttl_tracks_grant_expiration) to assert a long grant now mints a correspondingly long token.
  • README.md — Auth section now describes the token tracking the grant, and why refresh tokens stay out of scope (nothing to refresh against until the grant lapses).
  • src/main.rs — info-page copy: "lasts ~60 minutes" → "lasts for the session duration you choose when connecting (up to 30 days)".

Notes / trade-offs

  • The bearer token and the II grant now share the same lifetime and blast radius. A leaked token can act as the session (mint per-app delegations, each still ≤1h) only until the grant the user consented to expires — the same bound they already accepted on II's consent screen.
  • Access tokens are still stored in-memory, so a server restart continues to force a reconnect (unchanged; separate roadmap item).
  • The connect link still requests a 1h grant default (GRANT_TTL_SECS), which only seeds the picker's initial selection; a user who doesn't change it gets a 1h grant → 1h token. Bumping that default is a separate UX choice, left out of this change.

Testing

  • cargo test --bins — all 74 unit tests pass; crate compiles with no new warnings (only the pre-existing tool_router dead-code warning).

🤖 Generated with Claude Code

https://claude.ai/code/session_01RCkvMCzbnnFouKCAnuCpCz


Generated by Claude Code

The OAuth access token was hard-capped at 1h (TOKEN_TTL) regardless of the
session duration chosen on Internet Identity's connect consent screen, and no
refresh token is issued. So a user who selected e.g. "1 week" still had their
client's token expire after ~1h and — with nothing to refresh against — had to
re-run the full interactive authorization-code flow (surfaced as "reconnect").
The longer II grant was effectively unusable by the client.

Make the token's lifetime track the grant instead of capping it at a fixed 1h:
`token_ttl` (formerly `capped_token_ttl`) now returns the grant's full remaining
lifetime when known, so the session duration the user picks (10 minutes up to 30
days) is exactly how long the client's token stays valid. The token and the
grant now share the same lifetime and blast radius. A fixed default (1h) is kept
only as a fallback for when the grant expiration is unknown at issue time (e.g.
a dropped connect-completion POST); the grant remains the hard ceiling at II
either way. This keeps refresh tokens / server-side persistence out of scope.

Update the unit test, README Auth section, and the info-page copy accordingly.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RCkvMCzbnnFouKCAnuCpCz

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes premature reconnects by aligning the OAuth access-token TTL with the Internet Identity (II) grant duration the user selects, instead of hard-capping tokens at 1 hour when no refresh token exists.

Changes:

  • Removes the 1-hour cap when the II grant expiration is known, so access tokens last for the full remaining grant lifetime.
  • Keeps the 1-hour TOKEN_TTL as a fallback only when the grant expiration is unknown at issuance time.
  • Updates user-facing documentation/copy and adjusts the unit test to assert long grants mint long-lived tokens.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/auth.rs Updates token TTL calculation to track the II grant expiration (with fallback when unknown) and updates the associated unit test.
src/main.rs Updates user-facing info-page copy to reflect session duration can be up to 30 days.
README.md Updates auth documentation to describe token lifetime tracking the II grant and clarifies why refresh tokens remain out of scope.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@aterga
aterga marked this pull request as ready for review July 9, 2026 19:10
@aterga
aterga merged commit a7902e1 into main Jul 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants