ADR-0002: Scoped, revocable, hashed API tokens (agent-surface prerequisite)
- Status: Accepted — implemented 2026-08-10 (workstream 002: migration
000024,repository.APITokenRepository, scope-gate middleware,/api/v1/account/tokensendpoints; JWT lifetime shortened to 7 days; legacyusers.api_keyhashed in place per §5) - Date: 2026-07-11
- Deciders: Cardsite platform team (from REVIEW-001 F-1/deferred items, REVIEW-002 A.2.1)
Context
Today Cardsite has two credential types, both all-or-nothing:
- JWT — full user powers, non-revocable until expiry.
- API key — full user powers, long-lived, stored in plaintext and
matched by direct equality (
WHERE api_key = $1).
REVIEW-002 identifies scoped, revocable, hashed tokens as a prerequisite for exposing an agent/MCP surface: an agent given a credential today receives the entire account (links, billing, account deletion). REVIEW-001 separately lists plaintext API keys and non-revocable JWTs as known deferred items.
Decision
Introduce a first-class token model, delivered in v1.1 ahead of any programmatic / MCP surface:
api_tokenstable —id,user_id,name,token_hash(SHA-256 of a high-entropy secret; the plaintext is shown once at creation and never stored),scopes(text[]),last_used_at,expires_at(nullable),revoked_at(nullable).- Scopes, deny-by-default — e.g.
links:read,links:write,biolinks:write,ai:invoke,billing:read. A token with no scope for a route is rejected. - Scope enforcement middleware — sits alongside the existing
RequireAdmingate; each route group declares the scope it requires. - Revocation — auth checks
revoked_at IS NULL AND (expires_at IS NULL OR expires_at > now()). This also retires the "JWTs are non-revocable" item: short-lived JWTs + revocable tokens replace long-lived bearer credentials. - Hashing — tokens are looked up by
token_hash, closing the plaintext storage item. Legacyusers.api_keyis migrated: existing keys are hashed in place and the column is treated as a single implicit full-scope token until users rotate to the new model.
Consequences
- A user can grant an agent a narrow capability (manage links, not billing) without handing over the account.
- The same token model is what the MCP/SDK surface authenticates against, so it is a true prerequisite, not polish.
- Requires a migration, new endpoints (
POST/GET/DELETE /api/v1/account/tokens), middleware, and tests before the agent surface ships.
Why not in v1.0.0
This is a security-critical auth subsystem; shipping it hurried and under-tested would be worse than sequencing it deliberately. v1.0.0 instead closes the copy-paste auth-bypass path (F-1) and the payment-path findings (F-2), which are the actual release blockers. The token work is the first item gating the agent surface (see ROADMAP "Agent surface").
Alternatives considered
- OAuth2 scopes via a third-party IdP — heavier than needed for a single-product API; revisit if SSO becomes a requirement.
- Per-route API keys without scopes — doesn't express partial capability; rejected.
Source: docs/decisions/0002-scoped-revocable-tokens.md