How do Principals authenticate, and what may each do? #54

Closed
opened 2026-11-23 16:05:24 -05:00 by bobby · 3 comments
Owner

Part of #44

Question

How does each kind of Principal prove identity to hAPI, and how is access controlled? Decide Person authentication (passkeys, OIDC with a self-hosted IdP, etc.), Bot authentication (API keys, client credentials, mTLS), token lifetimes and revocation, how the Owner manages Principals, and the authorization model (scopes per domain, per-resource checks).

Part of #44 ## Question How does each kind of Principal prove identity to hAPI, and how is access controlled? Decide Person authentication (passkeys, OIDC with a self-hosted IdP, etc.), Bot authentication (API keys, client credentials, mTLS), token lifetimes and revocation, how the Owner manages Principals, and the authorization model (scopes per domain, per-resource checks).
bobby added the wayfinder:grilling label 2026-11-23 16:05:24 -05:00
bobby added a new dependency 2026-11-23 16:05:35 -05:00
Author
Owner

Input from Who owns data: a Person, the household, or both?: authorization has to express Owner-given Household grants for Bots, Person-granted and revocable Bindings (Bot → one Person's Personal data), Household membership, and Owner authority over Principals that does not cover reading others' Personal data. See ADR-0001.

Input from [Who owns data: a Person, the household, or both?](https://git.bobbypalko.com/bobby/hapi/issues/50): authorization has to express Owner-given **Household grants** for Bots, Person-granted and revocable **Bindings** (Bot → one Person's Personal data), Household membership, and Owner authority over Principals that does *not* cover reading others' Personal data. See ADR-0001.
Author
Owner

Constraint from How is hAPI reachable on the network? (ADR-0002): Bots share the public surface with Persons and have no network-level gate, so Bot credentials must be per-Bot, scoped, individually revocable, and stored hashed. A long-lived JWT that can't be revoked is out; use an opaque API key, or trade a revocable secret for short-lived tokens. Owner administration (Principals, credentials, Household grants) is CLI-only inside the pod, not an HTTP route. Persons grant Bindings through the public API. Uptime Kuma is a Bot with a health-only scope.

Constraint from [How is hAPI reachable on the network?](https://git.bobbypalko.com/bobby/hapi/issues/45) ([ADR-0002](https://git.bobbypalko.com/bobby/hapi/src/branch/main/docs/adr/0002-network-exposure.md)): Bots share the public surface with Persons and have no network-level gate, so Bot credentials must be **per-Bot, scoped, individually revocable, and stored hashed**. A long-lived JWT that can't be revoked is out; use an opaque API key, or trade a revocable secret for short-lived tokens. Owner administration (Principals, credentials, Household grants) is **CLI-only inside the pod**, not an HTTP route. Persons grant Bindings through the public API. Uptime Kuma is a Bot with a health-only scope.
bobby self-assigned this 2026-11-24 09:04:00 -05:00
Author
Owner

Resolution

Decided in a grilling session on 2026-11-24 and recorded as ADR-0005. It amends ADR-0004 and adds glossary terms.

Authentication

  • Persons and Bots both use bearer Tokens in v1. There's no IdP and no interactive login, because there's no frontend. Interactive login is out of scope for v1 and expected to arrive with a future browser frontend, as a new credential kind that resolves to the same Principal.
  • Tokens are opaque: hapi_<public-id>_<256-bit secret>. Only a SHA-256 of the secret is stored, and each request looks it up in the DB, so revocation is instant. We chose this over JWT plus refresh tokens, and over mTLS.
  • Tokens expire after 365 days by default, configurable when issued. --no-expiry is allowed only for Bots, and the CLI warns about such Tokens. A Principal can hold several live Tokens for rotation. last_used_at is written about once an hour, and the CLI flags Tokens that are expiring or have been unused for 90 days.

Authorization

  • Persons are authorized by ownership alone (ADR-0001). Bots have no access until given a Scope (<domain>:read|write, plus health) through Household grants (from the Owner, via the CLI) or Bindings (from a Person, via the API, per domain).
  • Resources are flat and ownership is an attribute. Each record lives at /v1/<domain>/<things>/{id} and keeps that URI when it moves to the Household. Records expose owner and recorded_by. Lists filter with ?owner=me|household|<id>. Creating a record takes owner, which Bots must supply for Personal data. We rejected a header naming the acting Person and nested owner paths.
  • hapi.principal_id is always the authenticated Principal. RLS now covers Personal and Household data: owner, active Binding, Household member, or Household grant. USING checks the read Scope and WITH CHECK checks the write Scope. Routes also declare require_scope so callers get a clean 403.
  • On the public API the Owner is an ordinary Person. All Owner powers stay in the CLI.

Self-service, lifecycle, errors

  • Persons can list and revoke their own Tokens (but not mint them), manage their own Bindings, and list Bots.
  • Deactivating a Principal revokes its Tokens and makes its grants and Bindings inert, on either side. Household membership is a flag set in the CLI. The Owner can't be deactivated; ownership transfer is a CLI command.
  • Every auth failure returns the same 401. A missing Scope returns 403. A record the caller can't see returns 404. Auth decisions and changes to Tokens, grants and Bindings are logged; secrets never are. There's no app-level rate limiting (anything per-IP belongs at the Gateway).
  • Tests must cover wrong Person, wrong Bot, revoked Binding, and a read-only Scope attempting a write.

Glossary: added Token, Scope and Household grant; Binding is now per domain.

## Resolution Decided in a grilling session on 2026-11-24 and recorded as [ADR-0005](https://git.bobbypalko.com/bobby/hapi/src/branch/main/docs/adr/0005-authentication-and-authorization.md). It amends [ADR-0004](https://git.bobbypalko.com/bobby/hapi/src/branch/main/docs/adr/0004-database-access.md) and adds glossary terms. **Authentication** - Persons and Bots both use bearer **Tokens** in v1. There's no IdP and no interactive login, because there's no frontend. Interactive login is out of scope for v1 and expected to arrive with a future browser frontend, as a new credential `kind` that resolves to the same Principal. - Tokens are opaque: `hapi_<public-id>_<256-bit secret>`. Only a SHA-256 of the secret is stored, and each request looks it up in the DB, so revocation is instant. We chose this over JWT plus refresh tokens, and over mTLS. - Tokens expire after 365 days by default, configurable when issued. `--no-expiry` is allowed only for Bots, and the CLI warns about such Tokens. A Principal can hold several live Tokens for rotation. `last_used_at` is written about once an hour, and the CLI flags Tokens that are expiring or have been unused for 90 days. **Authorization** - Persons are authorized by ownership alone (ADR-0001). Bots have no access until given a **Scope** (`<domain>:read|write`, plus `health`) through **Household grants** (from the Owner, via the CLI) or **Bindings** (from a Person, via the API, per domain). - **Resources are flat and ownership is an attribute.** Each record lives at `/v1/<domain>/<things>/{id}` and keeps that URI when it moves to the Household. Records expose `owner` and `recorded_by`. Lists filter with `?owner=me|household|<id>`. Creating a record takes `owner`, which Bots must supply for Personal data. We rejected a header naming the acting Person and nested owner paths. - `hapi.principal_id` is always the authenticated Principal. **RLS now covers Personal and Household data**: owner, active Binding, Household member, or Household grant. `USING` checks the read Scope and `WITH CHECK` checks the write Scope. Routes also declare `require_scope` so callers get a clean 403. - On the public API the Owner is an ordinary Person. All Owner powers stay in the CLI. **Self-service, lifecycle, errors** - Persons can list and revoke their own Tokens (but not mint them), manage their own Bindings, and list Bots. - Deactivating a Principal revokes its Tokens and makes its grants and Bindings inert, on either side. Household membership is a flag set in the CLI. The Owner can't be deactivated; ownership transfer is a CLI command. - Every auth failure returns the same `401`. A missing Scope returns `403`. A record the caller can't see returns `404`. Auth decisions and changes to Tokens, grants and Bindings are logged; secrets never are. There's no app-level rate limiting (anything per-IP belongs at the Gateway). - Tests must cover wrong Person, wrong Bot, revoked Binding, and a read-only Scope attempting a write. **Glossary:** added Token, Scope and Household grant; Binding is now per domain.
bobby closed this issue 2026-11-24 10:41:06 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Reference: bobby/hapi#54