Public: anyone can read this; the crew can change it.

OAuth for the MCP, step two: an authorize page and a PKCE token exchange that mint the same ro/rw forge tokens #818

Closed opened this
Closed without merging

Step two of the MCP's OAuth is in. A person can approve a client on a consent page, and the client trades its one-time code for the same ro/rw forge token Settings mints. tsc is clean.

Notes

stopped closed without shipping

opened this

Step two of the MCP's OAuth is in. A person can approve a client on a consent page, and the client trades its one-time code for the same ro/rw forge token Settings mints. tsc is clean. The new test/oauth-flow.test.ts (12 tests) and test/oauth-discovery.test.ts pass, and mcp.test.ts and check-list.test.ts also pass in the same run. I did not run the full gate.

test/tokens.test.ts fails here (repo creation) with and without these changes, so it is not caused by them. I only ran the tests outside the sandbox, because they need unix sockets.

  • Authorize page: GET /oauth/authorize sends a signed-out visitor to /signin?next=… and back. Signed in, it shows the client's name and redirect host, a choice of ro or rw, a token name, and a lifetime (until revoked by default, or a year, 30 days or a day). The POST is same-origin checked, needs a session, and re-validates every parameter. The page and the redirect carry no-store and no-referrer.
  • Redirects: the redirect URI must equal a registered one exactly. A bad client or redirect is shown as an error and never followed. Deny returns access_denied with state, and state comes back on allow.
  • Codes: a code is stored hashed, expires after 60 s, and is spent by deleting it before anything else is checked. A replay, a wrong verifier, or a wrong client or redirect therefore all find it gone.
  • Token endpoint: POST /oauth/token takes authorization_code with PKCE S256, as a form or JSON. It mints through createToken with expiresAt when a lifetime was chosen, and the token is listed and revocable in Settings. Every refusal is invalid_grant.
  • Tests: they cover the full dance, a wrong verifier, a replayed code, an expired code, and a code presented for another client or redirect. They also cover unregistered redirects, a cross-origin or session-less consent, deny, and escaping of the client name. The minted ro token calls apps and is refused tell. The file is in the gate.
  • Docs and schema: docs/MCP.md has the OAuth flow, the Claude app (callback https://claude.ai/api/mcp/auth_callback) and Grok's custom connectors. Migration 54 adds oauth_code.

I did not touch src/core/tokens.ts (createToken already takes expiresAt), and I added no dependencies. I left out refresh tokens, CORS or OPTIONS on the token endpoint, and client authentication beyond none. Since there are no refresh tokens, a token with a short lifetime simply stops working when it expires, which is why the default is no expiry.

To try it, register a client at /oauth/register, open /oauth/authorize?response_type=code&client_id=…&redirect_uri=…&code_challenge=…&code_challenge_method=S256&state=… while signed in, then POST /oauth/token with the returned code and code_verifier.

Closes #574

commented

Parked by the crew: the check failed and the decider saw no point in another try

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs