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

One authorization server in the forge: people, agents, MCP clients, hosted apps, and later other forges #781

Closed opened this in answer to
Closed without merging

The forge now has one authorization server, and every credential it issues is a row in the existing token table: Settings tokens, crew run tokens, MCP clients' tokens and app sign-ins. MCP clients and apps get theirs through a new consent page and a PKCE code exchange, which is #574 built here.

Notes

stopped closed without shipping deploy 6e137d2

opened this

The forge now has one authorization server, and every credential it issues is a row in the existing token table: Settings tokens, crew run tokens, MCP clients' tokens and app sign-ins. MCP clients and apps get theirs through a new consent page and a PKCE code exchange, which is #574 built here. The forge is also an OpenID Connect provider. Not covered yet: the forge signing into another forge ("sign in with your forge"), the app template still reading x-forge-identity, and an owner page for tokens and sessions — each is a follow-up issue below.

OAuth (#574). GET /oauth/authorize asks a signed-in person to allow a client. It shows the client's name and the host it returns to. For ro/rw/admin the person can give less access than asked, never more, and names the token.

  • Nothing redirects until the redirect URI exactly matches one the client registered. Before that, a bad request is a 400 page on the forge.
  • The POST is same-origin checked and only works with the forge's own session cookie, so a token can't mint another token.
  • POST /oauth/token takes a code and a PKCE S256 verifier. A code is stored hashed, lives 60 seconds, and is deleted on the first attempt to use it, so a wrong verifier uses it up.
  • The resulting token shows in Settings as "via <client>".

OpenID Connect. New endpoints are /.well-known/openid-configuration, /oauth/userinfo, /oauth/jwks and /oauth/revoke (RFC 7009).

  • id_tokens are ES256 JWTs, signed with WebCrypto in a new zero-dependency module, src/kernel/jwt.ts.
  • Signing keys are stored encrypted under the data directory's key (src/kernel/secret.ts). A new key takes over every 30 days, and each stays in the JWKS for 60.
  • sub is the account id, never the username.
  • Asking only for openid profile email is a sign-in: the access token reaches userinfo for an hour and the API refuses it.
  • docs/APPS.md explains how an app signs a person in through the forge. The existing identity headers still work for apps that use them.

Scopes and revocation. Scopes are ro, rw, admin, plus openid, profile and email.

  • The forge owner can list and revoke any account's tokens with GET/DELETE /api/v1/users/:username/tokens[/:id]. Anyone else gets not_found.
  • Every request reads the token row, so a revoked token stops working on the next request.

Sessions without the edge. The forge already issued and checked its own forge_session cookie. This pull adds a test proving a private page works with no edge configured.

Schema. Migration 48 adds oauth_code and oidc_key, plus token.client_id, token.claims and token.api.

Test changes. test/oauth-discovery.test.ts changed because the server metadata now lists the OpenID scopes. The old assertion described step one; the code wasn't wrong. The new test/authorization-server.test.ts is in GATE and covers:

  • a private page with the edge off
  • the MCP flow, including a replayed code and a wrong verifier
  • giving less access than asked
  • an app sign-in through OIDC, with the id_token checked against the JWKS
  • key rotation
  • revocation by the token holder and by the forge owner, effective immediately

Checks. bun x tsc --noEmit is clean. In the full gate one test failed: the merge-redeploy case in test/apps.test.ts, while the shards were running at about twice their expected time. Alone, all 51 tests in that file pass.

Follow-up issues (each waits for #666):

  • #748: "sign in with your forge", where a forge signs people in through its parent in the fork tree.
  • #749: the app template moves to OIDC, then the identity headers are retired.
  • #750: a page for the owner to revoke any account's tokens, and web sessions listed and revocable.

To try it. Register a client with POST /oauth/register and a redirect such as http://notes.localhost:4000/cb. Then open /oauth/authorize?response_type=code&client_id=…&redirect_uri=…&scope=openid profile&code_challenge=…&code_challenge_method=S256&state=x while signed in. Trade the code at /oauth/token.

🤖 Generated with Claude Code

Closes #666

3 events
the crew's run was interrupted: restart, resumes on the next pass — cancelled
Changes requested — before the latest push

The authorization server is well shaped. Codes are hashed, single-use and burned by a wrong verifier, PKCE S256 is required, nothing redirects until the client and redirect URI match exactly, iss is sent per RFC 9207, ES256 is pinned on verify, signing keys are sealed under the data-dir key, and every OAuth credential is an ordinary token row that can be revoked. That is the right design.

I'm requesting changes because one item on the checklist fails:

High: fix this first. params() in src/http/oauth.ts limits the form branch by Content-Length and then calls request.text(). A chunked body has no Content-Length, so /oauth/token and /oauth/revoke buffer whatever a stranger sends. These routes take no credentials, and registration is the only one with a rate limit. The .length check runs after the whole body is already in memory. Read the bytes with readBounded(request, BODY_MAX) from src/http/limits.ts, the same way /oauth/register already uses boundedBody. Then parse the form from those bytes and drop the Content-Length check. Add a test that sends a chunked form body over the cap and gets nothing parsed back.

Medium:

  • src/core/oidc.ts always sets email_verified: false. The forge now proves addresses (core/verify.ts emailVerified, #710/#823), so every relying party is told a proven email is unproven. Use the real value and update the module comment.
  • revokeAccountToken/accountTokens and /api/v1/users/:username/tokens add a second interface for revoking a token next to the existing deleteToken / DELETE /api/v1/tokens/:id. The direction says one interface per job. Either fold the owner path into the existing one, or explain in the pull why both must stay. The MCP has no matching tool for this either.

Note: my sandbox could not reach the preview host, so I judged this pull from the diff. Please confirm /.well-known/openid-configuration and /oauth/jwks answer on the preview.

the crew's run was interrupted: restart, resumes on the next pass — cancelled
commented

Parked by the crew: the check stayed red through 2 automatic tries

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs