Add authedRoute wrapper and move token routes onto it [ABD-21]

Merged
FR
6d ago
+291-109

What & why

First step of ABD-21 (one request principal + one scoped DB client). Each API route used to authenticate by hand: open a viewer frame, call authenticateRequest with a scope, return a 401, and add CORS headers. This adds an authedRoute wrapper that does all of that, so a route only declares what it accepts:

export const POST = authedRoute({ tokenScope: "write", cors: true }, handlePOST);
  • AuthenticatedRequest is renamed to Principal: who the request acts for. Only authenticateRequest builds one, so holding one means the 2FA, completed-signup and token-scope checks passed.
  • authedRoute (src/lib/http/authed-route.ts) opens the viewer frame, authenticates with the declared tokenScope, answers 401 (or 500 if the lookup throws) before the handler runs, optionally adds CORS headers (the 401 included), and passes the handler its params plus the principal.
  • The six routes that called authenticateRequest now use it: mcp, imports/literal, imports/[id], items/from-url, items/notes, items/[id]/instagram-enrich.
  • token-route-access.test.ts now reads each scope from the authedRoute(...) call and fails if anything other than the wrapper calls authenticateRequest.

Behaviour differences: every wrapped route's 401 now carries WWW-Authenticate: Bearer realm="abode" (before, only MCP did), and an auth-lookup failure is reported under the request's method and path rather than a per-route label.

Thread: https://ampcode.com/threads/T-01a1e700-7a74-76b5-85a8-6c3a6faaad6c

Checklist

  • User-facing change → added/updated a PostHog event (and a funnel/insight for a new flow), or N/A — N/A, internal refactor with no new user action
  • User-facing or service change → docs updated (README features/roadmap/services, .env.example, in-app help, CONTRIBUTING.md, onboarding checklist), or N/A — N/A for user docs (no user-facing or service change); agent guidance (AGENTS.md, app/src/app/AGENTS.md, add-authed-page skill) now points token routes at authedRoute
  • New error paths report via captureServerException / the error boundary, or N/A — the wrapper's auth-failure 500 reports via captureServerException
  • New behavior is covered by tests, or N/A — authed-route.test.ts covers the wrapper's contract; existing route tests run through the real wrapper
  • User-facing change → demo video attached (record-pr-demo skill); refactor through a user flow → parity run, or N/A — N/A, API auth plumbing only; route tests and the cross-user isolation suite cover behaviour
  • If this fixes a recurring defect, considered a guardrail via the learn skill, or N/A — N/A, not a defect fix; the token-scope guardrail is kept and moved onto the wrapper

Notes

Verified locally:

  • bun run check:fix clean; full unit suite passes (4,303 tests).
  • New authed-route.test.ts: 401/500 without running the handler, the declared scope reaching authenticateRequest, params + principal inside a viewer frame, CORS on both the 401 and success responses.
  • token-route-access.test.ts fails when a route's scope is changed (checked by temporarily switching items/notes from write to read).
  • cross-user-isolation.integration.test.ts: 260/260 pass, including the viewer-frame check and the owner-with-RLS cases (run with placeholder Supabase env values).

Not done here (later ABD-21 steps): cookie-only routes still use getCompletedSignupUser / getUserWithMfa; pages, server actions, asSystem and turning RLS on are still to come.


Summary by cubic

First step of ABD-21 (one request principal + one scoped DB client). API routes used to authenticate by hand: open a viewer frame, call authenticateRequest with a scope, return a 401, and add CORS headers. Adds an authedRoute wrapper that does all of that, so a route only declares what it accepts on its export. Renames AuthenticatedRequest to Principal.

  • The six routes that previously called authenticateRequest by hand now declare their scope on the export: mcp, imports/literal, imports/[id], items/from-url, items/notes, and items/[id]/instagram-enrich.
  • authedRoute returns 401 (or 500 if the auth lookup throws) before running the handler, optionally adds CORS headers to the 401 and the response, and passes the handler its params plus the principal.
  • tokenScope only governs personal access tokens; cookie sessions and Supabase bearer tokens (the extension) authenticate regardless, and it must be a literal so the access test can read it.
  • Handlers without route params get a one-argument signature so Next's generated route types accept them.
  • Every wrapped route's 401 now carries WWW-Authenticate: Bearer realm="abode"; before, only mcp did.
  • Auth-lookup failures are reported under the request's method and path rather than a per-route label.
  • token-route-access.test.ts reads each scope from the authedRoute(...) call and fails if anything other than the wrapper calls authenticateRequest.
  • Auth guidance in AGENTS.md and the add-authed-page skill now points token routes at authedRoute, noting the literal scope, the 500 path, and that tokenScope governs PATs only.