feat(web): personal agent tokens (issue, verify, revoke) and the pilot allowlist (#200) #212

Open
maximus wants to merge 3 commits from issue-200-agent-tokens into issue-199-agent-schema

3 commits

Author SHA1 Message Date
le king fu
e2f18a5f0f refactor(web): make the shared origin list read-only (#200)
ALLOWED_ORIGINS was private to ws.ts; it is now exported by origin.ts and
read by the routes too. Typed `readonly string[]`, so that no module can add
an origin to it by mistake. Same two values, same behaviour.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:30 -05:00
le king fu
1b701d6fc5 docs(review): agent tokens in the authentication zone and in the high-level table (#200)
- Niveaux, table Haut, Authentification row: web/src/lib/{auth,logto,api}.ts
  becomes web/src/lib/{auth,logto,api,agentTokens,agentAccess,origin}.ts.
  Nothing is removed or relaxed: the three files are new.
- scripts/review-levels.py: the same pattern in HIGH_ALWAYS
  (`python3 scripts/review-levels.py --check` passes).
- Zone Authentification: agent tokens (hash only, named columns, cap under
  the account lock), the refusal of slmcp_ by requireAuth(), the management
  routes open to the site session only, the pilot, the shared origin list.
- The two bullets that said every API route calls requireAuth() now name the
  second guard, requireAssistantsSession(); the count of routes without a
  guard stays at five.
- The WebSocket bullet names origin.ts.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:30 -05:00
le king fu
88d8976445 feat(web): personal agent tokens, their management routes and the pilot (#200)
An AI assistant will reach an account through a personal token created by a
user signed in on the site. This commit issues, verifies and revokes those
tokens, and adds the pilot switch. The MCP route (#202) and the page (#205)
come later in the stack.

- origin.ts: the list of allowed origins, moved as is from ws.ts, and
  isAllowedOrigin(). ws.ts imports it; no origin check reads request.url,
  whose origin is the container's in production.
- accountLock.ts: lockAccount(tx, userId), a transaction advisory lock per
  account.
- agentAccess.ts: mcpAllowed() reads MCP_ALLOWED_SUBS at every call (absent
  or empty: nobody; `*`: everyone; else a comma-separated list).
  requireAssistantsSession() guards the management routes: any Authorization
  header answers 401 first, then 401 without a site session, then 404 outside
  the pilot.
- agentTokens.ts: slmcp_ + 43 base64url characters; only the SHA-256 and the
  last four characters are stored. Ten tokens per account, counted under the
  account lock; expiry of 90 days, one year (same UTC day) or never;
  verifyAgentToken() accepts the exact Bearer shape only, refuses an expired
  token and writes last_used_at at most once per minute.
- GET and POST /api/agent-tokens, DELETE /api/agent-tokens/[id]: guard, rate
  limit, Origin on writes (403), body, action. Only the 201 of the POST
  carries the token; the eleventh token answers 409 token_limit.
- requireAuth() answers 401 to a Bearer that starts with slmcp_, before any
  call to Logto.
- .env.template: MCP_ALLOWED_SUBS, empty.

Tests: the real requireAuth with next/headers replaced and fetch spied, the
real /api/sync route with an agent token, the three routes on PGlite (two
accounts, cap, Origin with a request URL on the container's host, any
Authorization header, pilot, what the responses and the columns hold), the
token and pilot modules, the shared origin list and the WebSocket upgrade.

Resolves #200

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-16 20:50:30 -05:00