new: authentication and authorization (ADR-0005)
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>
This commit is contained in:
1 parent
f82b8cbd82
commit
a8c7464e56
3 files changed
+41
-1
No files matched your search
+13
-1
@@ -22,10 +22,22 @@ _Avoid_: Service account, machine user, integration account
|
||||
The single Person with administrative authority over hAPI and every other Principal. That authority covers Principals and the platform, not other Persons' Personal data.
|
||||
_Avoid_: Admin, root, superuser
|
||||
|
||||
**Token**:
|
||||
A Principal's secret credential for the API.
|
||||
_Avoid_: API key, password
|
||||
|
||||
**Scope**:
|
||||
A domain paired with read or write access: the unit of access that Household grants and Bindings carry.
|
||||
_Avoid_: Permission, privilege
|
||||
|
||||
**Binding**:
|
||||
A Person's consent letting a specific Bot act on that Person's Personal data.
|
||||
A Person's consent letting a specific Bot act on that Person's Personal data in one domain, up to a given Scope.
|
||||
_Avoid_: Delegation, impersonation, link
|
||||
|
||||
**Household grant**:
|
||||
The Owner's permission for a specific Bot to act on one domain's Household data, up to a given Scope.
|
||||
_Avoid_: Permission, role
|
||||
|
||||
### Ownership
|
||||
|
||||
**Household**:
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
hAPI uses a **Linode Managed PostgreSQL 18** cluster (Shared 2 GB, 1 node) attached to the cluster's VPC with `public_access = false`, and its allowlist holds only the VPC subnet. Clients connect with `sslmode=verify-full` and scram-sha-256 passwords. Two roles share the `hapi` database. **`hapi_migrator`** owns every schema and table and runs `hapi migrate`. **`hapi_app`** has DML only; the running app and the admin CLI both connect as it. Personal-data tables have `ENABLE ROW LEVEL SECURITY` (not `FORCE`), with policies keyed on a per-transaction `SET LOCAL hapi.principal_id`, so a forgotten `WHERE` in a handler returns nothing instead of leaking another Person's data. `hapi_app` never owns a table and has no `BYPASSRLS`. That keeps ADR-0001's rule (not even the Owner reads others' Personal data) in the database for everything the app does. We accept that VPC for Managed Databases is still labelled beta, because ADR-0002 rules out a public database endpoint. It was verified working in `us-iad`.
|
||||
|
||||
**Amended by ADR-0005:** RLS also covers **Household data** tables. Their policies allow an active Household member or a Bot with a Household grant for that domain. Personal-data policies also allow a Bot that holds an active Binding from the record's owner. In both cases `USING` checks the read Scope and `WITH CHECK` checks the write Scope. `hapi.principal_id` is always the authenticated Principal, Person or Bot. The CI isolation check below covers every owned table, Personal or Household.
|
||||
|
||||
## Considered Options
|
||||
|
||||
- **Public endpoint with a node-IP allowlist.** Rejected: it contradicts ADR-0002 ("Never public: the database").
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# 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 exposes `owner` (a Person id or `household`) and `recorded_by`. Lists accept `?owner=me|household|<person-id>`, and without a filter they return everything the caller may see. When creating a record, `owner` is 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_id` is 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. `USING` checks the read Scope and `WITH CHECK` checks the write Scope. Routes also declare `require_scope(...)`, so a caller missing a Scope gets a clean `403` and 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 `401` with `WWW-Authenticate: Bearer`. A missing Scope or Binding returns `403`. A record the caller can't see returns `404`. 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-expiry` is 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_at` is written at most about once an hour, and `hapi token list` flags 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 is `api_token` in v1. A browser login (passkeys or OIDC) would add, say, a `session` kind that resolves to the same Person. Scopes, Bindings and RLS stay as they are. Cookie, CSRF and CORS handling arrive with that frontend.
|
||||
Reference in new issue
Block a user