Amends ADR-0004 (RLS on Household data, Binding-aware policies) and adds Token, Scope, and Household grant to the glossary. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5.8 KiB
Opaque per-Principal tokens; authorization by ownership, grants and row-level security
In v1, every Principal (Person or Bot) authenticates with a bearer Token. There is no identity provider and no interactive login, because there's no frontend: Persons use scripts, HTTP clients and Shortcuts, just as Bots do. A Token is opaque: hapi_<public-id>_<secret>, where the secret has 256 bits of entropy. hAPI stores only a SHA-256 of the secret and looks the Token up in Postgres on every request, so revoking a Token takes effect immediately, as ADR-0002 requires. Authorization keys off the Principal, never the credential. Persons are authorized purely by ownership (ADR-0001). Bots have no access until they're given a Scope (<domain>:read or <domain>:write, where write implies read, plus the special health scope). A Bot gets Scopes through Household grants, which the Owner gives through the admin CLI, and Bindings, which a Person gives through the API, one domain at a time. Enforcement sits in row-level security as well as in the app, so a forgotten check fails closed.
Considered Options
- OIDC with a self-hosted IdP (Authelia, Kanidm, Pocket ID). Rejected for v1. It's another public-facing service to run and secure, every request would depend on it, and refresh flows are painful for curl cron jobs.
- Passkeys served by hAPI. These need a web origin and a login page, which amounts to a frontend. Deferred until a frontend exists (see Consequences).
- Short-lived JWTs signed by hAPI, plus refresh tokens. Rejected. Revocation lags by up to a token lifetime, there's a signing key to manage, and every client needs refresh logic. At our load, one indexed lookup per request costs nothing, and it uses the request's own pooled connection.
- mTLS client certificates. Already rejected in ADR-0002.
- Per-resource or per-record-type Scopes (e.g.
finance.transactions:write). Rejected as premature. A domain Scope can later be split into finer ones without breaking anything. - Choosing whose Personal data a Bot means with a request header (
Hapi-Acting-For). Rejected because the same URI would then name different resources depending on the header. - Nested owner paths (
/persons/{id}/workouts,/household/transactions). Rejected. ADR-0001 lets a record move from Personal to Household, and nesting would change its URI when it does. Listing everything a Bot may see would also take one call per Person. - Owner powers on the public API (e.g. revoking a Bot from a phone). Rejected. If the Owner's Token leaks, it should do no more harm than any other Person's.
Consequences
- Resources are flat and ownership is an attribute. Each record has exactly one URI,
/v1/<domain>/<things>/{id}, and keeps it when it moves to the Household. Every record exposesowner(a Person id orhousehold) andrecorded_by. Lists accept?owner=me|household|<person-id>, and without a filter they return everything the caller may see. When creating a record,owneris required for a Bot writing Personal data. For a Person it defaults to themselves on Personal types and to the Household on Household types. hapi.principal_idis always the authenticated Principal. Personal-data policies allow rows where the owner is the principal, or the owner has an active Binding to the principal for that domain. Household-data policies (amending ADR-0004) allow an active Household member or a Bot with a Household grant for that domain.USINGchecks the read Scope andWITH CHECKchecks the write Scope. Routes also declarerequire_scope(...), so a caller missing a Scope gets a clean403and the Scope appears in the OpenAPI export. Recorded by is simply the principal.- Responses: any missing, malformed, unknown, expired or revoked Token gets the same
401withWWW-Authenticate: Bearer. A missing Scope or Binding returns403. A record the caller can't see returns404. Tests cover wrong Person, wrong Bot, a revoked Binding and a read-only Scope attempting a write. - Token lifecycle: Tokens expire after 365 days by default, with the lifetime configurable when issued.
--no-expiryis allowed only for Bots, and the CLI lists such Tokens in a warning. A Principal may hold several live Tokens, so rotation goes issue, swap, revoke.last_used_atis written at most about once an hour, andhapi token listflags Tokens that are near expiry or unused for 90 days. - Issuing stays in the CLI (ADR-0002). On the public API, a Person may list and revoke their own Tokens (but not mint them), grant, list, change and revoke their Bindings, and list Bots (id, name, description). The Owner cannot create a Binding on a Person's behalf.
- Deactivation revokes all of a Principal's Tokens and makes its Household grants and Bindings inert, whichever side of a Binding it was on. Household membership is a flag the Owner sets in the CLI. The Owner can't be deactivated; ownership is transferred through a CLI command that needs the current Owner's confirmation.
- Auth is audited in the logs. Every auth decision is logged as a structured event with the principal id, the credential's public id, the outcome and the reason. Changes to Tokens, grants and Bindings are logged as audit events. Secrets are never logged. There's no app-level rate limiting, since 256-bit secrets can't be brute-forced. Any per-IP limits belong at the Gateway.
- The Token prefix is deliberate. It lets secret scanners and push protection recognise a leaked Token.
- A future frontend adds a credential kind and changes nothing else. Credentials carry a
kind, which isapi_tokenin v1. A browser login (passkeys or OIDC) would add, say, asessionkind that resolves to the same Person. Scopes, Bindings and RLS stay as they are. Cookie, CSRF and CORS handling arrive with that frontend.