@fuzdev/fuz_app 0.183.0 → 0.184.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/CLAUDE.md +535 -1223
- package/dist/actions/action_codegen.d.ts +1 -1
- package/dist/actions/action_codegen.d.ts.map +1 -1
- package/dist/actions/action_codegen.js +3 -2
- package/dist/actions/connection_closer.d.ts +11 -9
- package/dist/actions/connection_closer.d.ts.map +1 -1
- package/dist/actions/connection_closer.js +11 -9
- package/dist/actions/frontend_rpc_client.d.ts +1 -1
- package/dist/actions/register_action_ws.d.ts +14 -1
- package/dist/actions/register_action_ws.d.ts.map +1 -1
- package/dist/actions/register_action_ws.js +2 -1
- package/dist/actions/ws_socket_session.d.ts +9 -1
- package/dist/actions/ws_socket_session.d.ts.map +1 -1
- package/dist/actions/ws_socket_session.js +43 -19
- package/dist/auth/CLAUDE.md +430 -1048
- package/dist/auth/account_queries.d.ts +1 -1
- package/dist/auth/account_queries.js +1 -1
- package/dist/auth/account_routes.d.ts +4 -5
- package/dist/auth/account_routes.d.ts.map +1 -1
- package/dist/auth/account_routes.js +8 -10
- package/dist/auth/admin_actions.d.ts.map +1 -1
- package/dist/auth/admin_actions.js +10 -11
- package/dist/auth/api_token.d.ts +20 -3
- package/dist/auth/api_token.d.ts.map +1 -1
- package/dist/auth/api_token.js +28 -9
- package/dist/auth/api_token_queries.d.ts +2 -2
- package/dist/auth/api_token_queries.js +2 -2
- package/dist/auth/audit_log_schema.d.ts +15 -9
- package/dist/auth/audit_log_schema.d.ts.map +1 -1
- package/dist/auth/audit_log_schema.js +1 -2
- package/dist/auth/auth_guard_resolver.d.ts +1 -1
- package/dist/auth/auth_guard_resolver.js +1 -1
- package/dist/auth/bearer_auth.d.ts +2 -5
- package/dist/auth/bearer_auth.d.ts.map +1 -1
- package/dist/auth/bearer_auth.js +4 -13
- package/dist/auth/bootstrap_account.d.ts +13 -15
- package/dist/auth/bootstrap_account.d.ts.map +1 -1
- package/dist/auth/bootstrap_account.js +9 -11
- package/dist/auth/bootstrap_routes.d.ts +20 -21
- package/dist/auth/bootstrap_routes.d.ts.map +1 -1
- package/dist/auth/bootstrap_routes.js +21 -19
- package/dist/auth/bootstrap_token_store.d.ts +3 -4
- package/dist/auth/bootstrap_token_store.d.ts.map +1 -1
- package/dist/auth/bootstrap_token_store.js +1 -1
- package/dist/auth/daemon_token.d.ts +5 -1
- package/dist/auth/daemon_token.d.ts.map +1 -1
- package/dist/auth/daemon_token_middleware.d.ts +12 -8
- package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
- package/dist/auth/daemon_token_middleware.js +15 -21
- package/dist/auth/deps.d.ts +11 -0
- package/dist/auth/deps.d.ts.map +1 -1
- package/dist/auth/invite_queries.d.ts +51 -3
- package/dist/auth/invite_queries.d.ts.map +1 -1
- package/dist/auth/invite_queries.js +81 -6
- package/dist/auth/invite_schema.d.ts +2 -1
- package/dist/auth/invite_schema.d.ts.map +1 -1
- package/dist/auth/middleware.d.ts +2 -3
- package/dist/auth/middleware.d.ts.map +1 -1
- package/dist/auth/middleware.js +3 -3
- package/dist/auth/request_context.d.ts +3 -3
- package/dist/auth/request_context.js +3 -3
- package/dist/auth/role_grant_offer_actions.d.ts +2 -2
- package/dist/auth/role_grant_offer_actions.js +2 -2
- package/dist/auth/session_cookie.d.ts +82 -174
- package/dist/auth/session_cookie.d.ts.map +1 -1
- package/dist/auth/session_cookie.js +80 -130
- package/dist/auth/session_middleware.d.ts +8 -17
- package/dist/auth/session_middleware.d.ts.map +1 -1
- package/dist/auth/session_middleware.js +14 -23
- package/dist/auth/session_queries.d.ts +9 -14
- package/dist/auth/session_queries.d.ts.map +1 -1
- package/dist/auth/session_queries.js +8 -9
- package/dist/auth/signup_routes.d.ts.map +1 -1
- package/dist/auth/signup_routes.js +4 -6
- package/dist/db/CLAUDE.md +5 -3
- package/dist/dev/setup.d.ts +3 -1
- package/dist/dev/setup.d.ts.map +1 -1
- package/dist/dev/setup.js +3 -1
- package/dist/http/CLAUDE.md +315 -768
- package/dist/http/db_routes.d.ts +1 -1
- package/dist/http/db_routes.js +1 -1
- package/dist/http/origin.d.ts +7 -8
- package/dist/http/origin.d.ts.map +1 -1
- package/dist/http/origin.js +7 -8
- package/dist/http/pending_effects.d.ts +78 -4
- package/dist/http/pending_effects.d.ts.map +1 -1
- package/dist/http/pending_effects.js +82 -4
- package/dist/http/route_result.d.ts +1 -1
- package/dist/http/route_spec.d.ts +14 -11
- package/dist/http/route_spec.d.ts.map +1 -1
- package/dist/realtime/sse.d.ts +4 -23
- package/dist/realtime/sse.d.ts.map +1 -1
- package/dist/realtime/sse.js +4 -25
- package/dist/server/app_backend.d.ts +4 -5
- package/dist/server/app_backend.d.ts.map +1 -1
- package/dist/server/app_backend.js +3 -1
- package/dist/server/app_server.d.ts +53 -30
- package/dist/server/app_server.d.ts.map +1 -1
- package/dist/server/app_server.js +81 -53
- package/dist/server/app_server_context.d.ts +1 -2
- package/dist/server/app_server_context.d.ts.map +1 -1
- package/dist/server/env.js +2 -2
- package/dist/testing/CLAUDE.md +17 -17
- package/dist/testing/admin_integration.d.ts +2 -3
- package/dist/testing/admin_integration.d.ts.map +1 -1
- package/dist/testing/admin_integration.js +3 -3
- package/dist/testing/app_server.d.ts +3 -8
- package/dist/testing/app_server.d.ts.map +1 -1
- package/dist/testing/app_server.js +10 -9
- package/dist/testing/attack_surface.d.ts +1 -1
- package/dist/testing/attack_surface.d.ts.map +1 -1
- package/dist/testing/audit_completeness.d.ts +2 -3
- package/dist/testing/audit_completeness.d.ts.map +1 -1
- package/dist/testing/audit_completeness.js +1 -1
- package/dist/testing/cross_backend/capability_mounts.d.ts.map +1 -1
- package/dist/testing/cross_backend/capability_mounts.js +16 -11
- package/dist/testing/cross_backend/conformance_table.d.ts +5 -6
- package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
- package/dist/testing/cross_backend/conformance_table.js +4 -4
- package/dist/testing/cross_backend/cookie_attributes.js +1 -1
- package/dist/testing/cross_backend/default_backend_configs.d.ts +3 -3
- package/dist/testing/cross_backend/default_spine_surface.d.ts +0 -6
- package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
- package/dist/testing/cross_backend/default_spine_surface.js +4 -9
- package/dist/testing/cross_backend/differential.d.ts +2 -3
- package/dist/testing/cross_backend/differential.d.ts.map +1 -1
- package/dist/testing/cross_backend/differential.js +1 -1
- package/dist/testing/cross_backend/full_spine_mount.d.ts +1 -42
- package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
- package/dist/testing/cross_backend/full_spine_mount.js +45 -4
- package/dist/testing/cross_backend/in_process_setup.d.ts +6 -7
- package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/in_process_setup.js +9 -12
- package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts +3 -4
- package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
- package/dist/testing/cross_backend/rust_spine_consumer_surface.js +2 -2
- package/dist/testing/cross_backend/setup.d.ts +1 -1
- package/dist/testing/cross_backend/spine_suites.d.ts +2 -3
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +5 -5
- package/dist/testing/cross_backend/standard.d.ts +2 -3
- package/dist/testing/cross_backend/standard.d.ts.map +1 -1
- package/dist/testing/cross_backend/standard.js +3 -3
- package/dist/testing/cross_backend/testing_credential_read.d.ts +10 -11
- package/dist/testing/cross_backend/testing_credential_read.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_credential_read.js +12 -12
- package/dist/testing/cross_backend/testing_reset_actions.d.ts +35 -30
- package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_reset_actions.js +39 -31
- package/dist/testing/cross_backend/testing_server_core.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_server_core.js +2 -3
- package/dist/testing/daemon_token_rotation.d.ts.map +1 -1
- package/dist/testing/daemon_token_rotation.js +2 -3
- package/dist/testing/integration.d.ts +2 -3
- package/dist/testing/integration.d.ts.map +1 -1
- package/dist/testing/integration.js +4 -4
- package/dist/testing/rate_limiting.d.ts +2 -3
- package/dist/testing/rate_limiting.d.ts.map +1 -1
- package/dist/testing/rate_limiting.js +3 -3
- package/dist/testing/rpc_helpers.d.ts +1 -2
- package/dist/testing/rpc_helpers.d.ts.map +1 -1
- package/dist/testing/rpc_helpers.js +3 -3
- package/dist/testing/rpc_round_trip.d.ts +2 -3
- package/dist/testing/rpc_round_trip.d.ts.map +1 -1
- package/dist/testing/rpc_round_trip.js +1 -1
- package/dist/testing/sse_round_trip.d.ts +2 -3
- package/dist/testing/sse_round_trip.d.ts.map +1 -1
- package/dist/testing/sse_round_trip.js +2 -2
- package/dist/testing/stubs.d.ts +5 -6
- package/dist/testing/stubs.d.ts.map +1 -1
- package/dist/testing/stubs.js +10 -8
- package/dist/testing/test_app.d.ts +5 -3
- package/dist/testing/test_app.d.ts.map +1 -1
- package/dist/testing/test_app.js +2 -3
- package/dist/testing/ws_round_trip.d.ts.map +1 -1
- package/dist/testing/ws_round_trip.js +3 -0
- package/dist/ui/ui_format.d.ts.map +1 -1
- package/dist/ui/ui_format.js +1 -2
- package/package.json +1 -1
package/dist/auth/CLAUDE.md
CHANGED
|
@@ -10,530 +10,272 @@ runner contract + operator recipes: ../../../docs/migrations.md. For
|
|
|
10
10
|
workspace-wide DI vocabulary: Skill(fuz-stack) `dependency-injection.md`.
|
|
11
11
|
|
|
12
12
|
**CLAUDE.md is a map; TSDoc is the detail.** Per-symbol semantics
|
|
13
|
-
(parameters, error shapes, invariants,
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
(parameters, error shapes, invariants, timing contracts) live on TSDoc next
|
|
14
|
+
to the code. This file orients you across the modules and documents the
|
|
15
|
+
cross-cutting invariants that don't fit on any single symbol.
|
|
16
16
|
|
|
17
17
|
## AppDeps split
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
- **Route caps** — `RouteFactoryDeps` — `Omit<AppDeps, 'db'>`; handlers get `db` via `RouteContext`.
|
|
21
|
-
- **Action caps** — `ActionFactoryDeps` (`auth/deps.ts`) — the `{log, audit}` shape action factories take; `RouteFactoryDeps`/`AppDeps` satisfy it structurally. The factories whose handlers end credentials or revoke roles (`create_account_actions`, `create_admin_actions`, `create_role_grant_offer_actions`, `create_self_service_role_actions`, and the standard bundle) take `RevokingActionFactoryDeps` — `ActionFactoryDeps` plus a required `connection_closer`. The two pure-read actor factories (`actor_lookup`/`actor_search`) take only `{log: Logger}`.
|
|
22
|
-
- **Parameters** — `*Options` — static startup values, per-factory.
|
|
23
|
-
- **Runtime state** — inline ref — mutable values: `bootstrap_status`, `DaemonTokenState`. NOT in deps or options.
|
|
19
|
+
The categories are root ../../../CLAUDE.md §AppDeps Vocabulary. What auth adds:
|
|
24
20
|
|
|
25
|
-
`audit
|
|
26
|
-
|
|
27
|
-
over the pool so rows persist when request transactions roll back. See root
|
|
28
|
-
../../../CLAUDE.md §AppDeps Vocabulary for the workspace-wide split.
|
|
21
|
+
- **Action caps** — `ActionFactoryDeps` (`auth/deps.ts`), the `{log, audit}` shape action factories take; `RouteFactoryDeps` / `AppDeps` satisfy it structurally. The factories whose handlers end credentials or revoke roles (`create_account_actions`, `create_admin_actions`, `create_role_grant_offer_actions`, `create_self_service_role_actions`, and the standard bundle) take `RevokingActionFactoryDeps` — `ActionFactoryDeps` plus a required `connection_closer` (`actions/CLAUDE.md` §Connection closer). The pure-read actor factories (`actor_lookup` / `actor_search`) take only `{log}`.
|
|
22
|
+
- **Runtime state** — `bootstrap_status` and `DaemonTokenState` are inline refs, never deps or options. `DaemonTokenState.keeper_account_id` is resolved by `create_app_server` at assembly when null, and set by the bootstrap route after its commit (and by `_testing_reset`); the daemon leg never re-reads it.
|
|
29
23
|
|
|
30
24
|
## Module map
|
|
31
25
|
|
|
32
26
|
### Crypto primitives (pure, I/O-free)
|
|
33
27
|
|
|
34
|
-
- `auth/keyring.ts` — `Keyring
|
|
35
|
-
- `auth/session_cookie.ts` —
|
|
36
|
-
- `auth/credential_headers.ts` — the grammar of the three credential headers as pure readers over header lines (`
|
|
37
|
-
- `auth/password.ts` — `Password
|
|
38
|
-
- `auth/password_argon2.ts` — `
|
|
39
|
-
- `auth/api_token.ts` —
|
|
40
|
-
- `auth/daemon_token.ts` — `DaemonToken`,
|
|
41
|
-
- `auth/bootstrap_account.ts` — `bootstrap_account
|
|
42
|
-
- `auth/bootstrap_token_store.ts` — `BootstrapTokenStore
|
|
43
|
-
|
|
44
|
-
Cross-cutting notes that don't live on any single symbol:
|
|
45
|
-
|
|
46
|
-
- **Password schemas are split deliberately.** `Password` (length min 12)
|
|
47
|
-
gates creation + change; `PasswordProvided` (length min 1) gates
|
|
48
|
-
login/verify so tightening creation rules doesn't lock out existing
|
|
49
|
-
accounts. Both carry `sensitivity: 'secret'` meta.
|
|
50
|
-
- **Argon2id parameters** track OWASP guidance (`memoryCost: 19456`,
|
|
51
|
-
`timeCost: 2`, `parallelism: 1`); `verify_dummy` equalizes timing on
|
|
52
|
-
account-lookup miss.
|
|
53
|
-
- **API token format** `secret_fuz_token_<base64url>` — prefix enables
|
|
54
|
-
secret scanning (GitHub, TruffleHog); public `id` is `tok_<12 chars>`;
|
|
55
|
-
storage key is the blake3 hash. Raw token returned once.
|
|
28
|
+
- `auth/keyring.ts` — `Keyring`; `create_keyring` validates and is the one way to build one.
|
|
29
|
+
- `auth/session_cookie.ts` — the signed session cookie, named by a bare `session_cookie_name` (`SESSION_COOKIE_NAME` the default), as on the Rust spine: `parse_session_from_cookies` reads it off the `Cookie` lines as `SessionCookieRead` — `absent`, `invalid` (a present cookie that fails to verify, the empty value included) or `valid` with its token and signed expiry; no refresh, the lifetime is an absolute cap signed at mint — and the `Set-Cookie` writers for a route's result (`sign_session_cookie` over a minted token, `format_session_clear_cookie`). The attributes are fixed (`SESSION_COOKIE_ATTRIBUTES`, no option weakens them), and the writers throw on a name or value outside RFC 6265's grammar. The strings and names match the Rust spine's byte for byte.
|
|
30
|
+
- `auth/credential_headers.ts` — the grammar of the three credential headers as pure readers over header lines (`read_session_cookie_value`, `read_bearer_token`, `read_daemon_token`; a credential header sent twice is discarded). Hono-free; twin of the Rust `fuz_auth::credential_headers`, both held to `testing/cross_backend/credential_conformance.json`.
|
|
31
|
+
- `auth/password.ts` — `Password` (creation and change) vs `PasswordProvided` (login and verify): split so tightening creation rules never locks out an existing account. Both carry `sensitivity: 'secret'`.
|
|
32
|
+
- `auth/password_argon2.ts` — `argon2_password_deps` (Argon2id at OWASP parameters); `verify_dummy` equalizes timing on an account-lookup miss.
|
|
33
|
+
- `auth/api_token.ts` — the API token format: the `secret_fuz_token_` prefix exists for secret scanning; the public id (`ApiTokenId`, `to_api_token_id`) is derived from the token; storage is the blake3 hash; the raw token is returned once. `is_api_token_shaped` is the precheck that keeps a malformed bearer from costing a query.
|
|
34
|
+
- `auth/daemon_token.ts` — `DaemonToken`, its shape precheck, `DAEMON_TOKEN_HEADER`, `DaemonTokenState`.
|
|
35
|
+
- `auth/bootstrap_account.ts` — `bootstrap_account`, the one-shot keeper bootstrap: one transaction holds the `bootstrap_lock`, the account and grants, the session and its cookie line, the success row, and last the token burn — a failed burn (`BootstrapTokenBurnError`) rolls everything back and answers `token_file_undeletable`. `read_bootstrap_token` is the read it and `is_bootstrap_available` share. Its success (`BootstrapAccountSuccess`, internal) is the Rust `BootstrapSuccess`: the session token, the account and actor, and the signed `Set-Cookie` line.
|
|
36
|
+
- `auth/bootstrap_token_store.ts` — `BootstrapTokenStore`, the one seam to the token file. Its read refuses a file with a second hard link, which would outlive the burn under the other name. Twin of the Rust trait.
|
|
56
37
|
|
|
57
38
|
### Schemas, types, DDL
|
|
58
39
|
|
|
59
|
-
Convention — `*_schema.ts` holds a domain's `@fuzdev/fuz_schema` schemas;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
- `auth/
|
|
65
|
-
- `auth/
|
|
66
|
-
- `auth/
|
|
67
|
-
- `auth/
|
|
68
|
-
- `auth/
|
|
69
|
-
- `auth/
|
|
70
|
-
- `auth/
|
|
71
|
-
- `auth/
|
|
72
|
-
- `auth/
|
|
73
|
-
- `auth/
|
|
74
|
-
- `auth/role_grant_offer_schema.ts` — `RoleGrantOffer`, `RoleGrantOfferJson
|
|
75
|
-
- `auth/role_grant_offer_ddl.ts` —
|
|
76
|
-
- `auth/role_grant_sql.ts` — the liveness predicates every grant and offer read shares
|
|
77
|
-
- `auth/role_grant_offer_notifications.ts` — the WS notification specs
|
|
40
|
+
Convention — `*_schema.ts` holds a domain's `@fuzdev/fuz_schema` schemas;
|
|
41
|
+
`*_ddl.ts` holds DDL strings. Every wire shape in auth is a fuz_schema
|
|
42
|
+
schema, its timestamps `Timestamp`s, its usernames `UsernameCanonical` on
|
|
43
|
+
output.
|
|
44
|
+
|
|
45
|
+
- `auth/account_schema.ts` — `Account`, `Actor`, `RoleGrant`, `AuthSession`, `ApiToken` and their client-safe `*Json` shapes; `SessionId` (the blake3 session hash, branded); the grant predicates `is_role_grant_active` and `has_global_role` (the client-side bare-list form of `has_scoped_role(_, role, null)`).
|
|
46
|
+
- `auth/name_schema.ts` — the one name grammar the auth vocabularies share (runs of lowercase letters joined by single underscores, capped at `NAME_LENGTH_MAX`) and `create_name_schema(label)`, so each domain keeps its own label in messages. Twin of the Rust `fuz_auth::is_valid_name`.
|
|
47
|
+
- `auth/role_schema.ts` — `RoleName`, `RoleSpec`, the builtins (`ROLE_KEEPER`, `ROLE_ADMIN`, `is_builtin_role`), `create_role_registry`, and the grant-path lookups (`role_has_grant_path`, `list_roles_with_grant_path`).
|
|
48
|
+
- `auth/scope_kind_schema.ts`, `auth/credential_type_schema.ts`, `auth/grant_path_schema.ts` — the other open name registries (§Open-registry composition) and their builtins. A `scope_kind` is copied into the row and its audit metadata; the grammar keeps out the partial unique indexes' `'GLOBAL'` token.
|
|
49
|
+
- `auth/token_lifetime.ts` — `TokenLifetimeInput` (`eternal` or `ttl`), required on `account_token_create`: no default, so `expires_at IS NULL` always means deliberately eternal.
|
|
50
|
+
- `auth/token_scope.ts` — per-credential authority narrowing on `api_token.scope`: `TokenScope` / `TokenScopeInput`, `parse_stored_token_scope` (fail-closed, byte-capped at mint and read as on the Rust spine), and the `<section>:<id>` capability vocabulary (`rpc:<method>`, `surface:<name>`). Not an open registry — ../../../docs/security.md §Token scoping says why.
|
|
51
|
+
- `auth/auth_ddl.ts` — DDL and seeds for the core identity tables.
|
|
52
|
+
- `auth/audit_log_schema.ts` — `AUDIT_EVENT_TYPES` (the builtins; read the frozen constant, not a copy), `AuditEventTypeName`, `audit_metadata_schemas`, `AuditLogEvent` (its `actor_id` / `target_actor_id` TSDoc is the per-event table), `create_audit_log_config`, the revocation scopes (§Audit emitter), and the wire rows.
|
|
53
|
+
- `auth/audit_log_ddl.ts` — the `audit_log` table; `seq BIGSERIAL` for cursor-based gap fill (`create_db` parses int8 so `seq` reads as a number).
|
|
54
|
+
- `auth/invite_schema.ts`, `auth/app_settings_schema.ts` (a single-row table, `CHECK (id = 1)`) — rows and wire shapes.
|
|
55
|
+
- `auth/role_grant_offer_schema.ts` — `RoleGrantOffer`, `RoleGrantOfferJson`, `to_role_grant_offer_json`.
|
|
56
|
+
- `auth/role_grant_offer_ddl.ts` — the offer table and indexes, `ROLE_GRANT_OFFER_SCOPE_SENTINEL_UUID` / `ROLE_GRANT_OFFER_SCOPE_KIND_GLOBAL_TOKEN`, `ROLE_GRANT_OFFER_COLUMNS` and the shared `ROLE_GRANT_OFFER_WITH_GRANTOR_SELECT` CTE tail (homed here, not with the queries, because `role_grant_queries.ts` writes offer rows too and must not import the offer queries).
|
|
57
|
+
- `auth/role_grant_sql.ts` — the liveness predicates every grant and offer read shares (`role_grant_active_sql`, `offer_pending_predicate`), twins of the Rust spine's.
|
|
58
|
+
- `auth/role_grant_offer_notifications.ts` — the offer-lifecycle WS notification specs and their post-commit send helpers (§WS notifications).
|
|
78
59
|
|
|
79
60
|
### Queries
|
|
80
61
|
|
|
81
|
-
All take `deps: QueryDeps = {db}` first
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
- `auth/
|
|
90
|
-
- `auth/
|
|
91
|
-
- `auth/
|
|
92
|
-
- `auth/
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
`
|
|
99
|
-
`
|
|
100
|
-
|
|
101
|
-
`
|
|
102
|
-
without a `satisfies keyof Row` clause, since `AppSettings` deliberately
|
|
103
|
-
doesn't carry `id`). Module direction: `role_grant_offer_queries.ts` imports
|
|
104
|
-
`role_grant_queries.ts` (the accept path writes through
|
|
105
|
-
`query_create_role_grant` / `query_role_grant_by_id`) and not the reverse —
|
|
106
|
-
the scope teardown in `role_grant_queries.ts` still _writes_ offer rows, which
|
|
107
|
-
is exactly why the offer const and the shared CTE tail live in the DDL module.
|
|
108
|
-
`query_revoke_role_grant` lives with the offer queries (as in the Rust twin):
|
|
109
|
-
it takes the offer locks and both supersedes.
|
|
110
|
-
Every const is pinned to the live schema by
|
|
111
|
-
`src/test/db/column_projections.db.test.ts`. The Rust `fuz_auth` twin carries
|
|
112
|
-
the same nine consts (names and order), projected through
|
|
113
|
-
`fuz_db::qualify_columns` / `omit_columns`, decoded with compile-time
|
|
114
|
-
name→index (`fuz_db::col!`), and pinned by its own
|
|
115
|
-
`tests/columns.rs` registry; its narrow purpose rows (`AccountRow` is an
|
|
116
|
-
`id, username` pair) keep private per-shape lists checked as subsets of the
|
|
117
|
-
table const.
|
|
118
|
-
|
|
119
|
-
`_unscoped` suffix on `query_session_revoke_by_hash_unscoped` and
|
|
120
|
-
`query_invite_claim_unscoped` is the safety signal: SQL only checks row state,
|
|
121
|
-
caller is responsible for scoping. Production scoping for invites is enforced
|
|
122
|
-
upstream in `auth/signup_routes.ts` via `query_invite_find_unclaimed_match_for_update`
|
|
123
|
-
(SELECT … FOR UPDATE inside the signup tx — find + claim atomic on the row lock).
|
|
62
|
+
All take `deps: QueryDeps = {db}` first. Module direction:
|
|
63
|
+
`role_grant_offer_queries.ts` imports `role_grant_queries.ts`, never the
|
|
64
|
+
reverse; `query_revoke_role_grant` lives with the offer queries (as in the
|
|
65
|
+
Rust twin) because it takes the offer locks and both supersedes. Every
|
|
66
|
+
read projects through its table's `*_COLUMNS` const (../../../docs/architecture.md
|
|
67
|
+
§Query Modules), pinned to the live schema by
|
|
68
|
+
`src/test/db/column_projections.db.test.ts`.
|
|
69
|
+
|
|
70
|
+
- `auth/account_queries.ts` — account CRUD, actor resolution, password update with a verify-write race guard, the admin listing, and the account-row locks every writer of an account takes first (§Row locks).
|
|
71
|
+
- `auth/actor_lookup_queries.ts` / `auth/actor_search_queries.ts` — batched id → label and prefix search; active actors of active accounts only.
|
|
72
|
+
- `auth/role_grant_queries.ts` — idempotent create, scope-aware lookups, `query_role_grant_revoke_for_scope` (parent-scope cascade).
|
|
73
|
+
- `auth/role_grant_offer_queries.ts` — the offer lifecycle, atomic `query_accept_offer` with sibling supersede, `query_revoke_role_grant` (every revoke's one supersede rule: the revokee's incoming siblings and every offer its actor made), the offer-row lockers, and the `RoleGrantOffer*Error` classes.
|
|
74
|
+
- `auth/session_queries.ts` — server-side sessions (blake3-hashed). `create_capped_session` is the one session minter (row, cap evictions, the raw token the caller signs with `sign_session_cookie`; Hono-free; twin of the Rust one). No touch or renewal: `expires_at` is an absolute cap on the database clock. The cap and the expiry sweep return the ids they remove, so the caller closes their connections after the commit.
|
|
75
|
+
- `auth/api_token_queries.ts` — `query_validate_api_token` (records nothing, refuses an unreadable stored scope) and the separate usage touch the bearer leg runs only once the account is found live; `query_api_token_live_account`, the no-touch re-check sharing the validate query's liveness predicate; the token cap.
|
|
76
|
+
- `auth/invite_queries.ts`, `auth/app_settings_queries.ts`.
|
|
77
|
+
- `auth/audit_log_queries.ts` — `query_audit_log` (the insert), the list and history reads, the retention cleanup, and the drift counters.
|
|
78
|
+
|
|
79
|
+
`_unscoped` (`query_session_revoke_by_hash_unscoped`, logout only;
|
|
80
|
+
`query_invite_claim_unscoped`) is the safety signal: the SQL checks row
|
|
81
|
+
state, the caller owns scoping. Signup scopes the invite claim with
|
|
82
|
+
`query_invite_find_unclaimed_match_for_update` in the same transaction.
|
|
124
83
|
|
|
125
84
|
### Audit emitter
|
|
126
85
|
|
|
127
|
-
`auth/audit_emitter.ts` defines
|
|
128
|
-
`AppDeps.audit
|
|
129
|
-
`
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
- `emit
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
`
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
(`
|
|
154
|
-
`
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
`
|
|
159
|
-
a success row (throws, writes nothing, and counts it for the drift guard). A
|
|
160
|
-
**failure** row is a fire-and-forget pool write, fail-open (logged, counted by
|
|
161
|
-
`get_audit_write_failures`). The Rust spine's split is the same
|
|
162
|
-
(`route_audit_emit`: `emit_in_tx` for every success row, REST and db-admin
|
|
163
|
-
included, `emit` on the pool for failure rows). `seq` follows INSERT order,
|
|
164
|
-
not commit order.
|
|
165
|
-
|
|
166
|
-
**When listeners hear about a row.** The fan-out depends on the outcome: a
|
|
167
|
-
**failure** row is announced as soon as it is written — the attempt happened
|
|
168
|
-
whatever the transaction does; a **success** row is announced only after the
|
|
169
|
-
request's transaction commits (a thunk on `ctx.post_commit_effects`) and never
|
|
170
|
-
when it rolls back — a handler-owned transaction truncates the queue itself
|
|
171
|
-
(`transaction_with_post_commit_rollback`), so a handler that catches its
|
|
172
|
-
failure and answers a refusal announces nothing. The listeners act on a success row — the revocation guards
|
|
173
|
-
close connections, the audit stream broadcasts it — so announcing one before
|
|
174
|
-
its commit would close while the revoked credential still reads as valid, and
|
|
175
|
-
a rolled-back revocation would close anyway. A route that runs without a
|
|
176
|
-
request transaction and opens its own around just its writes (`/login`,
|
|
177
|
-
`/password`) emits its success row after that transaction commits. The Rust
|
|
178
|
-
spine follows the same order: a success row emitted inside a request
|
|
179
|
-
transaction is written in it with its fan-out queued behind the commit, and
|
|
180
|
-
`/login` and `/password` emit after their own commit. `AuditEmitterContext` is therefore
|
|
181
|
-
`{db, pending_effects, post_commit_effects}` — `RouteContext` and `ActionContext`
|
|
182
|
-
both satisfy it.
|
|
183
|
-
|
|
184
|
-
**Revocation scopes** (`auth/audit_log_schema.ts`) —
|
|
185
|
-
`audit_event_revocation_scopes` declares, per builtin event type, what a
|
|
186
|
-
successful row invalidates (`RevocationScope`: `none` / `session` / `token` /
|
|
187
|
-
`account` / `role`), and `to_revocation_scope(event_type)` reads it (an unknown
|
|
188
|
-
event type is `none`). Typed over `AuditEventType`, so a new event type does not
|
|
189
|
-
compile until it declares its scope. The revocation listener
|
|
190
|
-
(`create_revocation_guard`, `actions/connection_closer.ts`) dispatches on it. Twin of the
|
|
191
|
-
`revocation_scope` column on the Rust `AUDIT_EVENT_SPECS`.
|
|
192
|
-
|
|
193
|
-
**Drift counters** (`auth/audit_log_queries.ts`) — `audit_metadata_validation_failures`
|
|
194
|
-
and `audit_unknown_event_type_failures` are process-wide, fail-open
|
|
195
|
-
(write the row anyway). Independent in implementation; under the factory
|
|
196
|
-
they track the same config. Sample via `get_*`; `reset_*` are test-only.
|
|
197
|
-
Another counter lives with the emitter (`auth/audit_emitter.ts`,
|
|
198
|
-
`get_audit_write_failures` / `reset_audit_write_failures`): rows the bound
|
|
199
|
-
emitter's pool write failed to insert. That write is fail-open too — logged,
|
|
200
|
-
counted, the request proceeds — so the count is how a lost row is seen. A
|
|
201
|
-
third, `get_audit_success_outside_transaction` /
|
|
202
|
-
`reset_audit_success_outside_transaction`, counts success rows written
|
|
203
|
-
outside a transaction (`emit` with none open, which also throws, and a success
|
|
204
|
-
`emit_pool`); `install_audit_drift_guard` holds all of them at zero per test.
|
|
86
|
+
`auth/audit_emitter.ts` defines `AuditEmitter`, the capability on
|
|
87
|
+
`AppDeps.audit`, built once by the consumer's `audit_factory` over
|
|
88
|
+
`create_audit_emitter`. Its module doc owns the rules ("Where a row is
|
|
89
|
+
written", "When listeners hear about a row"); in short:
|
|
90
|
+
|
|
91
|
+
- `emit` is async — always `await` it. A **success** row is written in the
|
|
92
|
+
transaction of the change it reports, last before the commit; a refused
|
|
93
|
+
INSERT fails the change, and with no transaction open `emit` throws. A
|
|
94
|
+
**failure** row is a fail-open pool write that survives the rollback.
|
|
95
|
+
- Listeners hear a failure row when it is written and a success row only
|
|
96
|
+
after its transaction commits — never on a rollback. The revocation guard
|
|
97
|
+
and the audit stream act on success rows, so announcing one early would
|
|
98
|
+
close connections for a revocation that might not land.
|
|
99
|
+
- `emit_role_grant_target` lifts the actor/account/ip boilerplate for
|
|
100
|
+
role-grant-shape rows; `emit_pool` is for code with no request (never a
|
|
101
|
+
success row paired with a mutation); `notify` announces rows a query wrote
|
|
102
|
+
in its own transaction; `drain_inflight` is what `AppServer.close` and
|
|
103
|
+
`_testing_drain_effects` wait on.
|
|
104
|
+
- `create_app_server` registers the audit-stream listener and the one
|
|
105
|
+
revocation guard through `add_listener`, and removes them on a failed
|
|
106
|
+
assembly or `AppServer.close` — the emitter outlives any one server.
|
|
107
|
+
- The drift counters (metadata validation, unknown event type, failed pool
|
|
108
|
+
write, success row outside a transaction) are process-wide and fail-open;
|
|
109
|
+
`install_audit_drift_guard` holds them at zero per test.
|
|
110
|
+
|
|
111
|
+
**Revocation scopes** — `audit_event_revocation_scopes`
|
|
112
|
+
(`auth/audit_log_schema.ts`) declares, per builtin event type, what a
|
|
113
|
+
successful row invalidates (`none` / `session` / `token` / `account` /
|
|
114
|
+
`role`), read by `to_revocation_scope` and dispatched on by
|
|
115
|
+
`create_revocation_guard`. Typed over `AuditEventType`, so a new builtin
|
|
116
|
+
does not compile until it declares its scope. Twin of the Rust
|
|
117
|
+
`AUDIT_EVENT_SPECS` `revocation_scope` column.
|
|
205
118
|
|
|
206
119
|
### Routes
|
|
207
120
|
|
|
208
|
-
- `auth/account_routes.ts` — `POST /login
|
|
209
|
-
- `auth/bootstrap_routes.ts` — `POST /bootstrap
|
|
210
|
-
- `auth/signup_routes.ts` — `POST /signup
|
|
211
|
-
- `auth/audit_log_routes.ts` — optional `GET /audit/stream` (SSE); list
|
|
212
|
-
- `auth/auth_guard_resolver.ts` — `fuz_auth_guard_resolver
|
|
213
|
-
|
|
214
|
-
**Hono-free route shapes.** Each cookie
|
|
215
|
-
`*_route_schema.ts`
|
|
216
|
-
`
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
`@fuzdev/fuz_schema` schemas: `/login` takes `UsernameProvided` and
|
|
230
|
-
`PasswordProvided`; the output `username` of `/logout`, `/signup` and
|
|
231
|
-
`/bootstrap` is a `UsernameCanonical`; `/password`'s revoke counts are
|
|
232
|
-
non-negative integers; `/bootstrap`'s `token` is 1 to
|
|
233
|
-
`BOOTSTRAP_TOKEN_LENGTH_MAX` (4096, the token file's cap) UTF-16 code units,
|
|
234
|
-
a longer one a `400 invalid_request_body` before the window check, with no
|
|
235
|
-
audit row — twin of the Rust `BOOTSTRAP_TOKEN_LENGTH_MAX`.
|
|
236
|
-
|
|
237
|
-
**`POST /login` timing floor.** Login 401s are floored to
|
|
238
|
-
`DEFAULT_LOGIN_FAIL_FLOOR_MS` (250ms) + uniform jitter (±25ms) via
|
|
239
|
-
`start_fail_floor` (`auth/fail_floor.ts`, shared with signup's 403 / 409
|
|
240
|
-
floor; twin of the Rust `fuz_auth::timing`), started before the work and
|
|
241
|
-
awaited after, so observed time is `max(work, delay)` and
|
|
242
|
-
found-wrong-password and not-found paths converge. 429 stays fast by design;
|
|
243
|
-
`verify_dummy` equalizes Argon2id timing on not-found. A floor of `0` or less
|
|
244
|
-
is announced once per process on stderr by `warn_if_fail_floor_disabled`
|
|
245
|
-
(`LOGIN_` / `SIGNUP_FAIL_FLOOR_DISABLED_BANNER`, the Rust spine's text).
|
|
246
|
-
|
|
247
|
-
**`POST /password` revokes everything.** Revokes all sessions + all API
|
|
248
|
-
tokens (force re-auth everywhere), then clears the session cookie. Declares
|
|
249
|
-
`credential_types: ['session']` (see ../../../docs/security.md
|
|
121
|
+
- `auth/account_routes.ts` — `POST /login`, `/logout`, `/password`, `GET /status`. Rules on `create_account_route_specs`; the close-after-commit split is below.
|
|
122
|
+
- `auth/bootstrap_routes.ts` — `POST /bootstrap`, `is_bootstrap_available` (the boot report), the `BootstrapStatus` ref. A success sets the daemon-token keeper (`daemon_token_state`) after its commit. The lock-race loser answers with no audit row.
|
|
123
|
+
- `auth/signup_routes.ts` — `POST /signup`, open or invite-gated; the success row last in its transaction, an unclassified failure a `signup` failure row with reason `internal_error`.
|
|
124
|
+
- `auth/audit_log_routes.ts` — the optional `GET /audit/stream` (SSE); list and history are RPC. Admission is two-phase (register pending, re-read the credential and role grants, admit), so a revocation racing the request opens no stream.
|
|
125
|
+
- `auth/auth_guard_resolver.ts` — `fuz_auth_guard_resolver`, injected into `apply_route_specs` so `http/` stays auth-agnostic; the one site every route spec passes, so it also refuses a malformed `required_scope` and one on an unrestricted route.
|
|
126
|
+
|
|
127
|
+
**Hono-free route shapes.** Each cookie- or SSE-coupled route module has a
|
|
128
|
+
sibling `*_route_schema.ts` holding its I/O schemas and route _shapes_
|
|
129
|
+
(`Omit<RouteSpec, 'handler'>`). The live factory attaches the handler; a
|
|
130
|
+
cross-process surface builder attaches a stub. One source for the live route
|
|
131
|
+
and the attack surface, and a Rust-only cross-process suite imports the
|
|
132
|
+
shapes without the `hono` peer. Shared limits (`DEFAULT_MAX_SESSIONS` /
|
|
133
|
+
`_TOKENS`) live in `account_route_schema.ts` for the same reason.
|
|
134
|
+
|
|
135
|
+
**`POST /login` timing floor.** Login 401s are floored (`start_fail_floor`,
|
|
136
|
+
`auth/fail_floor.ts`, shared with signup's 403 / 409; twin of the Rust
|
|
137
|
+
`fuz_auth::timing`) so found-wrong-password and not-found converge; 429
|
|
138
|
+
stays fast by design. A disabled floor is announced once per process.
|
|
139
|
+
|
|
140
|
+
**`POST /password` revokes everything** — every session and API token —
|
|
141
|
+
then clears the cookie. Session-only (../../../docs/security.md
|
|
250
142
|
§Credential-channel gating).
|
|
251
143
|
|
|
252
|
-
**The routes close what they revoke, after the commit.** `/
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
`
|
|
257
|
-
|
|
144
|
+
**The routes close what they revoke, after the commit.** `/logout` closes
|
|
145
|
+
the one session it ends (the account's other sessions and tokens stay
|
|
146
|
+
valid) through the route transaction's post-commit queue
|
|
147
|
+
(`queue_connection_close`). `/login` (the sessions its cap evicts) and
|
|
148
|
+
`/password` (the account) open their own transaction around just their
|
|
149
|
+
writes and close straight after it commits
|
|
150
|
+
(`close_connections_after_commit`), so a later throw can't drop a close a
|
|
151
|
+
committed revocation owes.
|
|
258
152
|
|
|
259
|
-
|
|
260
|
-
`/
|
|
261
|
-
|
|
153
|
+
Of the auth surface only `/login`, `/logout`, `/password`, `/status`,
|
|
154
|
+
`/signup`, `/bootstrap` and the optional `/audit/stream` are REST;
|
|
155
|
+
everything else is under §RPC action surfaces.
|
|
262
156
|
|
|
263
157
|
### Middleware
|
|
264
158
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
159
|
+
The credential legs, their order, the chain reset, the origin carve-outs and
|
|
160
|
+
the assembly asserts (`assert_endpoint_in_auth_scope`,
|
|
161
|
+
`assert_credential_routes_in_auth_scope`, `assert_middleware_stack_mounted`)
|
|
162
|
+
are root ../../../CLAUDE.md §Middleware Ordering — its origin-verification
|
|
163
|
+
and credential-leg layers (daemon token, session parsing, request context,
|
|
164
|
+
bearer auth) and the `transform_middleware` paragraph. The modules:
|
|
165
|
+
|
|
166
|
+
- `auth/middleware.ts` — `create_auth_middleware_specs(deps, options)`, the stack `create_app_server` mounts at `AUTH_MIDDLEWARE_PATH` (the origin check at `'*'`), and the assembly asserts above.
|
|
167
|
+
- `auth/session_middleware.ts` — the session leg's cookie read and the clear of a present, invalid cookie. A route sets or clears the cookie only on its success result (`route_ok(value, {set_cookie})` with `sign_session_cookie` / `format_session_clear_cookie`); nothing writes a cookie on the context but the middleware's clear.
|
|
168
|
+
- `auth/request_context.ts` — `RequestContext`, `resolve_acting_actor`, `apply_authorization_phase`, the predicates (`has_scoped_role`, `has_any_scoped_role` — builtin-role checks read the **global** grant, `scope_id` null), the guards (`require_auth`, `require_role`, `require_credential_types`, `require_token_scope`), `get_public_account` (what a `reads_credential` route may read — `null` for a scope-narrowed token), and `RequestContextForAuth`, the narrowing a route's `route.auth` and an action's `ctx.auth` share.
|
|
169
|
+
- `auth/resolved_auth.ts` — `ResolvedAuth`, the resolved credential as one union on `credential_type`, carrying the account row the leg read (the authorization phase builds on it rather than reading again). `revalidate_resolved_auth` is the no-touch re-read a long-lived connection (WS, the audit stream) runs between registering pending and admitting.
|
|
170
|
+
- `auth/credential_deadline.ts` — when a resolved credential stops authenticating, which an open WebSocket or audit stream closes at (expiry writes nothing a revocation listener hears). Survives host suspend and `setTimeout`'s ceiling; rules in its TSDoc.
|
|
171
|
+
- `auth/bearer_auth.ts` — the soft-fail bearer leg; a request with any `Origin` or `Referer` line discards the token.
|
|
172
|
+
- `auth/daemon_token_middleware.ts` — the daemon token **consumer** only, soft-fail on every non-success path. The producer (`write_daemon_token`, `start_daemon_token_rotation`) is in `testing/daemon_token_rotation.ts` behind the dev-env guard — no production assembly mints daemon tokens, as on the Rust spine.
|
|
276
173
|
|
|
277
174
|
### Cell layer
|
|
278
175
|
|
|
279
176
|
The cell content primitive's wire schemas, RPC handlers, and authz live
|
|
280
|
-
here; the schema
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
- `
|
|
292
|
-
|
|
293
|
-
`
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
- `auth/
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
defense-in-depth). "Admin" on every tier is the **global** grant — a
|
|
319
|
-
scoped `admin` row bypasses nothing. `can_view_cell`'s public and grant
|
|
320
|
-
arms also require the cell's `moderation` to admit an audience
|
|
321
|
-
(`db/cell_moderation.ts`: unmoderated or `approved`), so a pending or
|
|
322
|
-
rejected contribution is its author's and admins' alone; `can_edit_cell`'s
|
|
323
|
-
editor-grant arm requires the same admission. `can_steward_cell`
|
|
324
|
-
is the separate moderator read: the governing root's manager on a pending or
|
|
325
|
-
rejected contribution under a live root.
|
|
326
|
-
- `auth/cell_steward.ts` — `cell_steward_admits(deps, auth, cell)`: the steward
|
|
327
|
-
arm with its governing-root load, run only after `can_view_cell` denies. Its
|
|
328
|
-
module doc names where the arm applies (`cell_get`, `cell_moderate`, the
|
|
329
|
-
cell-scoped fact route, `cell_list`'s SQL) and where it must not (clone,
|
|
330
|
-
contribution parents, relation reads, `shared_with`, grant recompute).
|
|
331
|
-
- Strict relation-read visibility — every relation read drops edges whose far
|
|
332
|
-
endpoint the caller can't view, so a viewer of a parent can't enumerate
|
|
333
|
-
private linked cells by id. **Forward and reverse** `cell_field_list` /
|
|
334
|
-
`cell_item_list` filter in SQL before their limit
|
|
335
|
-
(`db/cell_view_sql.ts`'s `can_view_cell_sql`, bound through
|
|
336
|
-
`cell_viewer_from_auth`), so a page is up to `limit` edges whose other end
|
|
337
|
-
the caller can view — `CELL_RELATION_LIST_LIMIT_DEFAULT` (50) when omitted,
|
|
338
|
-
`CELL_RELATION_LIST_LIMIT_MAX` (500) at most
|
|
339
|
-
(`auth/cell_relation_list_limit.ts`), and hidden ends never crowd out
|
|
340
|
-
visible ones. The `cell_get` bundle (whose `*_truncated` counts raw edges)
|
|
341
|
-
and the clone walk read with `CELL_VIEWER_UNRESTRICTED` and filter after,
|
|
342
|
-
through `auth/cell_relation_visibility.ts`'s `filter_visible_target_ids(deps,
|
|
343
|
-
auth, ids)`: two queries for the whole id-set, the cells read through the
|
|
344
|
-
narrow `CELL_VIEW_COLUMNS` projection (`query_cell_view_load_many`), never
|
|
345
|
-
the payload.
|
|
346
|
-
- `auth/cell_actions.ts` — `create_cell_actions(deps)`, the generic
|
|
347
|
-
handlers. `cell_create` stamps `created_by` + writes the top-level `kind`
|
|
348
|
-
column; `path` writes are admin-only. It also resolves the directory tree:
|
|
349
|
-
`parent_id` (wire) → the governing `root_id` (`parent.root_id ?? parent.id`),
|
|
350
|
-
**404**-masking a hidden parent. `cell_update` gates `visibility`
|
|
351
|
-
writes on `can_manage_cell` (`ERROR_CELL_VISIBILITY_MANAGE_ONLY`) and
|
|
352
|
-
**never touches `kind`** / `parent_id` / `root_id` / `moderation` (all
|
|
353
|
-
write-once-at-birth or control-gated, never on the update patch).
|
|
354
|
-
`cell_get` bundles visibility-filtered `fields` + `items` (one over the cap
|
|
355
|
-
for truncation detection). `cell_clone` deep-walks viewable children only
|
|
356
|
-
(each clone inherits its source's `kind`; clones are unparented), and runs
|
|
357
|
-
the mounted `authorize_create` for **every** cell it writes as a parentless
|
|
358
|
-
create — a denial is the same 403 `cell_create_forbidden`, so clone is no
|
|
359
|
-
side door past the creation policy. The optional
|
|
360
|
-
parent-aware `authorize_create` dep (`CellCreateAuthorize` — an object whose
|
|
361
|
-
`authorize` takes a wider input `{kind, data, parent_id, parent_kind,
|
|
362
|
-
root_id, root_policy}` than `validate_data` and returns a `CellCreateVerdict`
|
|
363
|
-
`{allow:false} | {allow:true, moderation_required}`, and whose optional
|
|
364
|
-
`validate_policy` accepts or refuses a root policy write)
|
|
365
|
-
gates create by capability; it runs after the validator + the parent-resolve
|
|
366
|
-
(which **404**s `cell_not_found` on a missing or soft-deleted root as on a
|
|
367
|
-
missing parent, authorizer or not).
|
|
368
|
-
A **viewable**-parent / root-creation deny is **403** `cell_create_forbidden`
|
|
369
|
-
(the create-path **404** mask is reserved for a hidden parent); `Allow` folds
|
|
370
|
-
the moderation outcome (born `pending`+private vs `approved`+author-visibility).
|
|
371
|
-
The handler reads the governing root in-tx and hands its `policy` column
|
|
372
|
-
over as `root_policy`, so a directory policy resolves `root_policy[kind]`
|
|
373
|
-
without a DB read; an authorizer needing more reads through its third
|
|
374
|
-
argument, the request `ctx` (`ctx.db` is the request transaction), and only
|
|
375
|
-
reads. `None`/omitted = open
|
|
376
|
-
create (the default; mirrors `validate_data`), and every non-null policy write
|
|
377
|
-
refused `cell_policy_unsupported`. A root's `policy` (a JSON object, roots only
|
|
378
|
-
by table CHECK, opaque to the spine, readable on `CellJson`) is set at a
|
|
379
|
-
root's `cell_create` and changed only by `cell_policy_set` — the 404 mask, then
|
|
380
|
-
manage-tier (403 `cell_policy_manage_only`, an editor grant included), then
|
|
381
|
-
root-only (400 `cell_policy_root_only`), then the authorizer's
|
|
382
|
-
`validate_policy`; audited with the new and the previous policy. `cell_update`
|
|
383
|
-
never writes it (an editor can rewrite `data`, so a policy read from `data`
|
|
384
|
-
would be the editor's to loosen), and a clone is born without one.
|
|
385
|
-
`cell_moderate` is the `pending → approved | rejected` transition (plus
|
|
386
|
-
`approved → rejected`, the takedown), gated on
|
|
387
|
-
`can_manage_cell` of the **governing root** (admin / root owner — the author
|
|
388
|
-
lands a 403, the anti-self-approval guard; a root owner reaches the check
|
|
389
|
-
through the steward arm); approve flips
|
|
390
|
-
`moderation → approved` + `visibility → public`, reject sets `rejected`
|
|
391
|
-
(visibility untouched). The write carries the from-state: approve moves only
|
|
392
|
-
a pending contribution, reject a pending or approved one (the takedown); any
|
|
393
|
-
other transition answers 400 `cell_not_pending`.
|
|
394
|
-
- `auth/cell_action_helpers.ts` — the cell verbs' shared gates, each loading
|
|
395
|
-
the cell and 404-masking a miss and a denial alike
|
|
396
|
-
(`require_viewable_cell` — `auth` may be `null`, which reads no grants —
|
|
397
|
-
`require_editable_cell`, `require_manageable_cell`), plus
|
|
398
|
-
`cell_not_found_error` and `emit_cell_audit(ctx, audit, auth, event_type,
|
|
399
|
-
metadata)`. Twins of the Rust `fuz_cell_actions` helpers.
|
|
400
|
-
- `auth/cell_ref_admission.ts` — `require_new_refs_held`, the
|
|
401
|
-
possession gate `cell_create` / `cell_update` / `cell_clone` run last before
|
|
402
|
-
their write: a non-admin write introducing a fact ref (beyond the cell's
|
|
403
|
-
current refs on update, each cloned row's source refs on clone) that its
|
|
404
|
-
acting actor holds no `fact_holder` row for is 403 `cell_ref_not_held`,
|
|
405
|
-
answered from holder rows alone (stored and unstored hashes read the same);
|
|
406
|
-
a schema without the fact chain refuses every new ref. Global admin bypasses;
|
|
407
|
-
a scoped admin doesn't. Twin of the Rust `cell_ref_admission`.
|
|
408
|
-
- `auth/cell_grant_actions.ts` / `cell_field_actions.ts` /
|
|
409
|
-
`cell_item_actions.ts` / `cell_audit_actions.ts` — the relation + ACL +
|
|
410
|
-
audit handlers, exporting `to_grant_json` / `to_field_json` /
|
|
411
|
-
`to_item_json` reused by the `cell_get` bundle. `cell_grant_create` takes an
|
|
412
|
-
optional `authorize_grant_create` (`CellGrantCreateAuthorize`), asked of a
|
|
413
|
-
manager after the spine's gates and before the write (a level change too);
|
|
414
|
-
`false` is 403 `cell_grant_not_authorized`, nothing written.
|
|
415
|
-
`cell_grant_revoke` takes an optional `authorize_grant_revoke`
|
|
416
|
-
(`CellGrantRevokeAuthorize`), the steward widening: asked only of a caller
|
|
417
|
-
who neither manages the cell nor holds the grant, for a role-shaped grant
|
|
418
|
-
with a `scope_id`; `true` revokes (audit `steward: true`) with a delete
|
|
419
|
-
matching the level it saw, `false` keeps the 404 mask. Both callbacks, like
|
|
420
|
-
the create authorizer and the offer callback, take the request `ctx` last —
|
|
421
|
-
`ctx.db` is the request transaction — and must only read through it.
|
|
422
|
-
- `auth/all_cell_actions.ts` — `create_all_cell_actions(deps, {roles})`, the
|
|
423
|
-
handler-side twin of `all_cell_action_specs` and sibling of
|
|
424
|
-
`create_standard_rpc_actions`: bundles all five cell factories (CRUD + grant
|
|
425
|
-
- field + item + audit) into one list. Reach for this whenever a backend
|
|
426
|
-
mounts the full cell layer — a single source so an HTTP-RPC mount and a WS
|
|
427
|
-
mount (or two backends) can't diverge on which cell verbs they expose.
|
|
428
|
-
Distinct from `create_cell_actions` (the CRUD-only factory it bundles).
|
|
429
|
-
- `auth/cell_audit_metadata.ts` + `cell_grant_audit_metadata.ts` +
|
|
430
|
-
`cell_field_audit_metadata.ts` + `cell_item_audit_metadata.ts` — audit
|
|
431
|
-
metadata envelopes (IDs only), `@fuzdev/fuz_schema` `loose_object`s,
|
|
432
|
-
registered via `extra_events:` on the consumer's `create_audit_log_config`.
|
|
433
|
-
- `auth/cell_audit_events.ts` — `cell_audit_events`, the canonical
|
|
434
|
-
`event_type → metadata` map bundling every cell-domain audit event.
|
|
435
|
-
Consumers spread it into `extra_events` to register the whole layer in
|
|
436
|
-
one call (aggregator by design, not a compat shim).
|
|
437
|
-
|
|
438
|
-
The cell event types (`cell_create` / `_update` / `_delete` / `_clone` /
|
|
439
|
-
`_moderate`, `cell_policy_set`, `cell_grant_create` / `_revoke`, `cell_field_set` / `_delete`,
|
|
440
|
-
`cell_item_insert` / `_move` / `_delete`) are consumer-registered extras,
|
|
441
|
-
not built-ins — see §Audit event extensibility.
|
|
177
|
+
here; the schema and queries live in `db/` (`db/CLAUDE.md` §Cell layer). The
|
|
178
|
+
handler semantics are `auth/cell_actions.ts`'s module TSDoc; the model for
|
|
179
|
+
consumers is ../../../docs/usage.md §Cell data layer, and the security
|
|
180
|
+
properties (moderation gates reads, root policy is manage-tier) are
|
|
181
|
+
../../../docs/security.md §Authorization.
|
|
182
|
+
|
|
183
|
+
Rules a caller would miss:
|
|
184
|
+
|
|
185
|
+
- **Generic only** — no app vocabulary on the wire. Per-kind shape is the
|
|
186
|
+
consumer's `validate_data(kind, data)` on `CellActionDeps`, which accepts or
|
|
187
|
+
throws and never rewrites.
|
|
188
|
+
- **`kind` is not in `data`** — it is the top-level, write-once `cell.kind`
|
|
189
|
+
column; a stray `kind` in `data` is refused (`ERROR_CELL_KIND_IN_DATA`).
|
|
190
|
+
`parent_id` / `root_id` / `moderation` are likewise top-level and never on
|
|
191
|
+
the update patch.
|
|
192
|
+
- **Consumer callbacks only read.** `authorize_create`,
|
|
193
|
+
`authorize_grant_create`, `authorize_grant_revoke` (and the offer
|
|
194
|
+
`authorize`) take the request `ctx` last; `ctx.db` is the request
|
|
195
|
+
transaction.
|
|
196
|
+
- **A clone is no side door**: it runs `authorize_create` for every cell it
|
|
197
|
+
writes as a parentless create.
|
|
198
|
+
- **Every miss and denial on a gated read is a 404**, so a private cell's
|
|
199
|
+
existence doesn't leak.
|
|
200
|
+
|
|
201
|
+
Modules:
|
|
202
|
+
|
|
203
|
+
- `auth/cell_data_schema.ts` — `CellData`, the base a per-kind schema spreads.
|
|
204
|
+
- `auth/cell_action_specs.ts` — `CellJson`, `CellPolicy`, the generic verb specs (`cell_create / get / update / delete / list / clone / moderate / policy_set`), and `all_cell_action_specs`, which spreads the grant, field, item and audit registries in so codegen sees one cell namespace.
|
|
205
|
+
- `auth/cell_grant_action_specs.ts` / `cell_field_action_specs.ts` / `cell_item_action_specs.ts` / `cell_audit_action_specs.ts` — the relation, ACL and audit-timeline specs.
|
|
206
|
+
- `auth/cell_authorize.ts` — the pure view / edit / manage predicates. Manage (global admin or the `created_by` actor) is not a grant level and not delegable; "admin" is always the **global** grant. `can_steward_cell` is the separate moderator read.
|
|
207
|
+
- `auth/cell_steward.ts` — `cell_steward_admits`; its module doc names where the steward arm applies and where it must not.
|
|
208
|
+
- `auth/cell_relation_visibility.ts`, `auth/cell_relation_list_limit.ts` — strict relation-read visibility: every relation read drops edges whose far endpoint the caller can't view.
|
|
209
|
+
- `auth/cell_actions.ts` — `create_cell_actions(deps)`, the CRUD, clone, moderate and policy handlers.
|
|
210
|
+
- `auth/cell_action_helpers.ts` — the shared load-and-404-mask gates and `emit_cell_audit`; twins of the Rust `fuz_cell_actions` helpers.
|
|
211
|
+
- `auth/cell_ref_admission.ts` — `require_new_refs_held`: a non-admin write introducing a fact ref its actor holds no `fact_holder` row for is 403 `cell_ref_not_held`. Twin of the Rust `cell_ref_admission`.
|
|
212
|
+
- `auth/cell_grant_actions.ts` / `cell_field_actions.ts` / `cell_item_actions.ts` / `cell_audit_actions.ts` — the relation, ACL and audit handlers.
|
|
213
|
+
- `auth/all_cell_actions.ts` — `create_all_cell_actions(deps, {roles})`, the handler twin of `all_cell_action_specs`. Reach for it whenever a backend mounts the full cell layer, so an HTTP-RPC mount and a WS mount can't diverge.
|
|
214
|
+
- `auth/cell_audit_events.ts` (and the `cell_*_audit_metadata.ts` modules) — `cell_audit_events`, the cell event types' metadata map; consumers spread it into `extra_events`. The cell events are consumer-registered extras, not builtins (§Audit event extensibility).
|
|
442
215
|
|
|
443
216
|
## Cross-cutting invariants
|
|
444
217
|
|
|
445
|
-
The things that span multiple files and don't fit on any one symbol's TSDoc.
|
|
446
|
-
|
|
447
218
|
### Two-phase identity
|
|
448
219
|
|
|
449
|
-
**Authentication runs in middleware** (
|
|
450
|
-
|
|
451
|
-
Account-only — never loads actor or role_grants
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
`
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
path populates `REQUEST_CONTEXT_KEY`; it sets only `RESOLVED_AUTH_KEY`. Test
|
|
480
|
-
harnesses pre-populate `REQUEST_CONTEXT_KEY` and set `TEST_CONTEXT_PRESET_KEY`
|
|
481
|
-
to the `TEST_CONTEXT_PRESET` symbol to bypass DB-backed actor resolution
|
|
482
|
-
(`has_test_context_preset` reads it; any other value, `true` or a string
|
|
483
|
-
included, is unset); production code that reads `REQUEST_CONTEXT_KEY` is
|
|
484
|
-
reading test escape-hatch state.
|
|
485
|
-
|
|
486
|
-
**The session token key is fixed.** The session middleware writes the token
|
|
487
|
-
under `SESSION_TOKEN_KEY` (`hono_context.ts`, `'auth_session_id'`) and the
|
|
488
|
-
session leg reads it there; `SessionOptions` carries no context key, so the two
|
|
489
|
-
cannot disagree and the token cannot land on another spine key.
|
|
220
|
+
**Authentication runs in middleware** (daemon token / session / bearer) and
|
|
221
|
+
sets one `ResolvedAuth` (`RESOLVED_AUTH_KEY`) on a valid credential.
|
|
222
|
+
Account-only — never loads actor or role_grants.
|
|
223
|
+
|
|
224
|
+
**Authorization runs before input validation**, per route and per action
|
|
225
|
+
(`http/CLAUDE.md` §Validation pipeline for the full phase order). When the
|
|
226
|
+
input declares `acting?: ActingActor` or the auth requires role_grants,
|
|
227
|
+
the authorization phase resolves the acting actor and builds an actor-bound
|
|
228
|
+
`RequestContext`; a malformed selector reads as omitted (it only ever picks
|
|
229
|
+
among actors the account owns, and validation rejects it a step later).
|
|
230
|
+
Account-grain routes run with `RequestContext.actor: null`, so a multi-actor
|
|
231
|
+
account can log out without picking a persona.
|
|
232
|
+
|
|
233
|
+
`apply_authorization_phase` is pure data — it returns `AuthorizationResult`
|
|
234
|
+
and each transport binds a failure to its wire shape. The one 500 is
|
|
235
|
+
`no_actors_on_account` (a live account with no active actor). It builds on
|
|
236
|
+
the credential leg's account row: an account removed after that read runs an
|
|
237
|
+
account-grain handler on the row, while an actor-grain one finds no actor and
|
|
238
|
+
answers the dead credential's 401, as on the Rust spine.
|
|
239
|
+
|
|
240
|
+
**Production-middleware invariant.** The credential legs set only
|
|
241
|
+
`RESOLVED_AUTH_KEY`. The route pipeline's authorization phase writes the
|
|
242
|
+
actor-bound context to `REQUEST_CONTEXT_KEY`, which `require_role` reads.
|
|
243
|
+
Test harnesses pre-set `REQUEST_CONTEXT_KEY` and set
|
|
244
|
+
`TEST_CONTEXT_PRESET_KEY` to the `TEST_CONTEXT_PRESET` symbol to skip
|
|
245
|
+
DB-backed resolution (`has_test_context_preset`; any other value is unset).
|
|
246
|
+
|
|
247
|
+
**The session token key is fixed** (`SESSION_TOKEN_KEY`): the session
|
|
248
|
+
middleware takes only the cookie's name, no context key, so it and the leg
|
|
249
|
+
cannot disagree.
|
|
490
250
|
|
|
491
251
|
### Row locks
|
|
492
252
|
|
|
493
253
|
One class order in every transaction — accounts, then offers, then grants and
|
|
494
254
|
actors — and id order within a class. Every writer of an account takes its row
|
|
495
255
|
first (two mints of one account take turns there, so the caps hold); a
|
|
496
|
-
multi-row write
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
256
|
+
multi-row write locks its rows in one id-ordered statement before writing.
|
|
257
|
+
Each lock's strength and reason is on its query's TSDoc; the rule set:
|
|
258
|
+
../../../docs/security.md §Row Locks. `lock_pins.cross.test.ts` pins them on
|
|
259
|
+
both spines (../../test/CLAUDE.md).
|
|
500
260
|
|
|
501
261
|
### Wire timestamp shape
|
|
502
262
|
|
|
503
|
-
Every timestamp
|
|
504
|
-
`
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
`
|
|
510
|
-
|
|
511
|
-
`to_iso8601_seconds` (`timestamp.ts`), so the mint and the later
|
|
512
|
-
`account_token_list` read agree byte for byte.
|
|
513
|
-
|
|
514
|
-
The `*Json` schemas type these as `Timestamp` (`iso_datetime`), which a
|
|
515
|
-
millisecond stamp also satisfies — so the shape is pinned behaviorally by
|
|
516
|
-
`assert_iso8601_seconds` (`testing/cross_backend/wire_shapes.ts`) inside the
|
|
517
|
-
shared suites, on both backends.
|
|
518
|
-
|
|
519
|
-
The alias costs one rule: **an `ORDER BY` in a projected SELECT qualifies
|
|
520
|
-
its column** (`ORDER BY auth_session.created_at`) — bare, Postgres resolves
|
|
521
|
-
it to the formatted output column, which ties rows written inside the same
|
|
522
|
-
second and drops the btree index. Qualified, ordering stays exact and
|
|
523
|
-
index-backed, matching the Rust twin (which aliases nothing, so its bare
|
|
524
|
-
`ORDER BY created_at` already means the column).
|
|
263
|
+
Every wire timestamp is second-precision UTC, the shape both spines emit
|
|
264
|
+
(`timestamp.ts`). Rows get it from the SQL projection
|
|
265
|
+
(`iso8601_timestamp_column`, `db/sql_columns.ts`, which also owns the one
|
|
266
|
+
rule it costs: an `ORDER BY` in a projected SELECT qualifies its column);
|
|
267
|
+
stamps minted in TS use `to_iso8601_seconds`. `Timestamp` also admits
|
|
268
|
+
milliseconds, so the shape is pinned behaviorally by
|
|
269
|
+
`assert_iso8601_seconds` (`testing/cross_backend/wire_shapes.ts`) on both
|
|
270
|
+
backends.
|
|
525
271
|
|
|
526
272
|
### Open-registry composition
|
|
527
273
|
|
|
528
|
-
Four open
|
|
529
|
-
`CredentialTypeName`, `GrantPathName` — share
|
|
530
|
-
construction-time
|
|
531
|
-
|
|
532
|
-
cross-axis validation
|
|
533
|
-
letters joined by single underscores, at most `NAME_LENGTH_MAX` characters
|
|
534
|
-
(`auth/name_schema.ts`), which a token's `surface:` name holds too.
|
|
535
|
-
|
|
536
|
-
Dependency flow:
|
|
274
|
+
Four open name registries — `RoleName`, `ScopeKindName`,
|
|
275
|
+
`CredentialTypeName`, `GrantPathName` — share one factory shape
|
|
276
|
+
(construction-time grammar, duplicate and builtin-collision checks,
|
|
277
|
+
`ReadonlyMap` output) and one name grammar (`auth/name_schema.ts`), and
|
|
278
|
+
compose into `create_role_registry` for cross-axis validation:
|
|
537
279
|
|
|
538
280
|
```
|
|
539
281
|
create_credential_type_registry()
|
|
@@ -541,387 +283,188 @@ create_scope_kind_registry() → create_role_registry({roles, options})
|
|
|
541
283
|
create_grant_path_registry()
|
|
542
284
|
```
|
|
543
285
|
|
|
544
|
-
`role_specs` drives
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
`AuditEventTypeName` is the fifth open registry but composes differently —
|
|
550
|
-
via `create_audit_log_config({extra_events})` into the bound emitter.
|
|
286
|
+
`role_specs` drives the defaults: `admin_actions.grantable_roles` (roles with
|
|
287
|
+
the `admin` grant path) and the self-service eligible set (roles with
|
|
288
|
+
`self_service`). `AuditEventTypeName` is a fifth open registry that composes
|
|
289
|
+
differently — `create_audit_log_config({extra_events})` into the emitter.
|
|
551
290
|
|
|
552
291
|
### Audit `target_*_id` rules
|
|
553
292
|
|
|
554
|
-
The two target columns on `AuditLogEvent` populate by a single rule:
|
|
555
293
|
**`target_actor_id` is set when the event subject is bound to a specific
|
|
556
|
-
actor**; `target_account_id` is
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
-
|
|
564
|
-
the subject regardless of initiator). Use `audit.emit_role_grant_target`
|
|
565
|
-
to lift the `actor_id` / `account_id` / `ip` boilerplate.
|
|
566
|
-
- **Offer-shape events** (`role_grant_offer_create` / `_expire` / `_retract` /
|
|
567
|
-
`_supersede`) populate `target_actor_id` only when the offer was
|
|
568
|
-
actor-targeted at create time (`role_grant_offer.to_actor_id` set).
|
|
294
|
+
actor**; `target_account_id` whenever there is an account subject. The full
|
|
295
|
+
per-event table is the `AuditLogEvent.actor_id` / `target_actor_id` TSDoc.
|
|
296
|
+
The pattern across emit sites:
|
|
297
|
+
|
|
298
|
+
- **Role-grant-shape events** populate both targets — the grantee actor is
|
|
299
|
+
the subject whoever initiated. Use `audit.emit_role_grant_target`.
|
|
300
|
+
- **Offer-shape events** populate `target_actor_id` only when the offer was
|
|
301
|
+
actor-targeted (`role_grant_offer.to_actor_id` set).
|
|
569
302
|
- **Account-shape events** (login, logout, signup, bootstrap, password
|
|
570
|
-
change, session/token revoke,
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
303
|
+
change, session/token revoke, app_settings_update) stay account-grain on
|
|
304
|
+
`target_actor_id`. `actor_id` is null except on signup and bootstrap, which
|
|
305
|
+
name the actor they create.
|
|
306
|
+
- **Invite events** carry the acting `actor_id` and no target. A delete of a
|
|
307
|
+
missing invite writes a failure row with `attempted_invite_id`.
|
|
308
|
+
- **Account removal** writes the account event plus one per actor
|
|
309
|
+
(`actor_delete` / `_purge` / `_undelete`, `target_actor_id` the actor), each
|
|
310
|
+
snapshotting the identity into metadata, since `audit_log` ids carry no FK.
|
|
311
|
+
|
|
312
|
+
A connection close keyed on an audit row follows the row's revocation scope:
|
|
313
|
+
`account` and `role` key on `target_account_id ?? account_id`, `session` and
|
|
314
|
+
`token` on the row's metadata (`actions/CLAUDE.md` §Revocation guard).
|
|
580
315
|
|
|
581
316
|
### Audit event extensibility
|
|
582
317
|
|
|
583
|
-
Consumers extend the closed `AUDIT_EVENT_TYPES`
|
|
584
|
-
`create_audit_log_config({extra_events})` — a
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
`
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
`
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
### `AUDIT_EVENT_TYPES` builtins
|
|
601
|
-
|
|
602
|
-
For quick reference; the source-of-truth list is the `Object.freeze`d
|
|
603
|
-
constant in `auth/audit_log_schema.ts`.
|
|
604
|
-
|
|
605
|
-
```
|
|
606
|
-
login role_grant_create
|
|
607
|
-
logout role_grant_revoke
|
|
608
|
-
bootstrap role_grant_offer_create
|
|
609
|
-
signup role_grant_offer_accept
|
|
610
|
-
password_change role_grant_offer_decline
|
|
611
|
-
session_revoke role_grant_offer_retract
|
|
612
|
-
session_revoke_all role_grant_offer_expire
|
|
613
|
-
token_create role_grant_offer_supersede
|
|
614
|
-
token_revoke invite_create
|
|
615
|
-
token_revoke_all invite_delete
|
|
616
|
-
account_delete app_settings_update
|
|
617
|
-
account_purge db_admin_row_delete
|
|
618
|
-
account_undelete
|
|
619
|
-
actor_delete
|
|
620
|
-
actor_purge
|
|
621
|
-
actor_undelete
|
|
622
|
-
```
|
|
623
|
-
|
|
624
|
-
`account_delete` / `account_purge` / `account_undelete` snapshot
|
|
625
|
-
`{username, email}` into metadata; `actor_delete` / `actor_purge` /
|
|
626
|
-
`actor_undelete` snapshot `{name}`. The account-level handlers emit the
|
|
627
|
-
account event plus one per-actor event (`delete` = soft, `purge` = hard,
|
|
628
|
-
`undelete` = reactivation — see ../../../docs/security.md §Authorization,
|
|
629
|
-
"Account-removal target guards").
|
|
630
|
-
|
|
631
|
-
`role_grant_offer_supersede` carries
|
|
632
|
-
`reason: 'sibling_accepted' | 'role_grant_revoked' | 'scope_destroyed' | 'account_deleted'` (for `account_deleted`, `cause_id` is the removed account, the grantor's or the recipient's)
|
|
633
|
-
plus `cause_id` pointing to the row that triggered the supersede. The WS
|
|
634
|
-
`role_grant_offer_supersede` notification's reason set is narrower —
|
|
635
|
-
`sibling_accepted | role_grant_revoked | account_deleted`; `scope_destroyed`
|
|
636
|
-
is an audit reason only (a consumer's own cascade writes it).
|
|
318
|
+
Consumers extend the closed `AUDIT_EVENT_TYPES` via
|
|
319
|
+
`create_audit_log_config({extra_events})` — a fuz_schema metadata schema or
|
|
320
|
+
`null` per type; a collision with a builtin, an off-grammar name, or a value
|
|
321
|
+
that is no schema throws at construction. The DB column is `TEXT NOT NULL`,
|
|
322
|
+
and the read side (`AuditLogEvent.event_type`, the wire row, the
|
|
323
|
+
`audit_log_list` filter) is the widened `AuditEventTypeName`, so consumer
|
|
324
|
+
rows round-trip like builtins. The write side (`AuditLogInput<T>`,
|
|
325
|
+
`AuditMetadataMap`) stays closed so a builtin's metadata narrows by its
|
|
326
|
+
type. An unknown type or a metadata mismatch is logged and counted, and the
|
|
327
|
+
row written anyway; a failed pool write logs the lost row with its secret
|
|
328
|
+
fields masked.
|
|
329
|
+
|
|
330
|
+
`role_grant_offer_supersede` carries `reason` (`sibling_accepted` /
|
|
331
|
+
`role_grant_revoked` / `scope_destroyed` / `account_deleted`) and `cause_id`,
|
|
332
|
+
the row that triggered it. `scope_destroyed` is an audit reason only (a
|
|
333
|
+
consumer's own cascade writes it); the WS notification's set omits it.
|
|
637
334
|
|
|
638
335
|
### Keeper auth shape
|
|
639
336
|
|
|
640
337
|
Keeper is not a dedicated guard — it's a composable `RouteAuth` shape:
|
|
641
338
|
`{account: 'required', actor: 'required', roles: ['keeper'], credential_types: ['daemon_token']}`.
|
|
642
|
-
The
|
|
643
|
-
`ERROR_CREDENTIAL_TYPE_REQUIRED`)
|
|
644
|
-
(403 `ERROR_INSUFFICIENT_PERMISSIONS`)
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
in the HTTP RPC dispatcher (`actions/action_rpc.ts`), the WS dispatcher
|
|
649
|
-
(`actions/register_action_ws.ts`), and the admin bypasses inside
|
|
339
|
+
The channel gate (`require_credential_types`, 403
|
|
340
|
+
`ERROR_CREDENTIAL_TYPE_REQUIRED`) runs pre-authorization and the role gate
|
|
341
|
+
(`require_role`, 403 `ERROR_INSUFFICIENT_PERMISSIONS`) post-authorization,
|
|
342
|
+
so a non-daemon credential is refused before the route resolves an actor
|
|
343
|
+
for it. The same scope-aware check runs for actions in
|
|
344
|
+
`actions/perform_action.ts` (both dispatchers) and in the admin bypasses of
|
|
650
345
|
`auth/role_grant_offer_actions.ts`.
|
|
651
346
|
|
|
652
347
|
### Migrations
|
|
653
348
|
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
the
|
|
657
|
-
|
|
658
|
-
`
|
|
659
|
-
|
|
660
|
-
unless it is the spine's own object (`AUTH_MIGRATION_NS`, `CELL_MIGRATION_NS`,
|
|
661
|
-
`FACT_MIGRATION_NS`, each built by `create_spine_migration_namespace`). Runner contract, error vocabulary, and
|
|
662
|
-
operator recipes (rename, mark applied, reset, baseline) are in
|
|
663
|
-
../../../docs/migrations.md.
|
|
349
|
+
`auth/migrations.ts` — a frozen, append-only chain (`full_auth_schema`
|
|
350
|
+
first) under the reserved namespace `'fuz_auth'` (`AUTH_MIGRATION_NS`), each
|
|
351
|
+
entry twinned by name on the Rust spine. The reserved names are
|
|
352
|
+
`reserved_migration_namespaces` in `db/migrate.ts`, a consumer namespace under
|
|
353
|
+
one refused as `namespace-reserved`; that refusal, the runner contract, and the
|
|
354
|
+
operator recipes are ../../../docs/migrations.md.
|
|
664
355
|
|
|
665
356
|
## RPC action surfaces
|
|
666
357
|
|
|
667
|
-
Each registry splits across `*_action_specs.ts` (schemas
|
|
668
|
-
codegen
|
|
669
|
-
|
|
670
|
-
|
|
358
|
+
Each registry splits across `*_action_specs.ts` (schemas, specs, registry —
|
|
359
|
+
what client codegen imports, without the handlers' query-layer deps) and
|
|
360
|
+
`*_actions.ts` (`create_*_actions(deps, options)`). The spec modules carry
|
|
361
|
+
each action's input, output, auth and `error_reasons`; the handler modules'
|
|
362
|
+
TSDoc carries their semantics. This section keeps what spans actions.
|
|
671
363
|
|
|
672
|
-
- `create_admin_actions` —
|
|
673
|
-
- `create_role_grant_offer_actions` —
|
|
674
|
-
- `create_account_actions` —
|
|
675
|
-
- `create_self_service_role_actions` —
|
|
676
|
-
- `create_actor_lookup_actions` —
|
|
677
|
-
- `create_actor_search_actions` —
|
|
364
|
+
- `create_admin_actions` — `all_admin_action_specs` — bundled in `create_standard_rpc_actions`.
|
|
365
|
+
- `create_role_grant_offer_actions` — `all_role_grant_offer_action_specs` — bundled.
|
|
366
|
+
- `create_account_actions` — `all_account_action_specs` — bundled.
|
|
367
|
+
- `create_self_service_role_actions` — `all_self_service_role_action_specs` — not bundled (`eligible_roles` is app-specific).
|
|
368
|
+
- `create_actor_lookup_actions` — `all_actor_lookup_action_specs` — not bundled (opt-in id → label resolver).
|
|
369
|
+
- `create_actor_search_actions` — `all_actor_search_action_specs` — not bundled (opt-in prefix-search picker).
|
|
678
370
|
|
|
679
|
-
`auth/all_action_spec_registries.ts` exposes
|
|
680
|
-
for registry-wide invariant tests
|
|
681
|
-
|
|
371
|
+
`auth/all_action_spec_registries.ts` exposes
|
|
372
|
+
`all_fuz_auth_action_spec_registries` for registry-wide invariant tests — not
|
|
373
|
+
a mounting surface; protocol specs excluded.
|
|
682
374
|
|
|
683
375
|
### Authorization patterns
|
|
684
376
|
|
|
685
|
-
- **Spec-level enforcement.**
|
|
686
|
-
`auth: {account: 'required', actor: 'required', roles: ['admin']}
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
refers to another account. The spec can't express this because auth runs
|
|
695
|
-
before input parsing.
|
|
377
|
+
- **Spec-level enforcement.** Admin specs declare
|
|
378
|
+
`auth: {account: 'required', actor: 'required', roles: ['admin']}`, checked
|
|
379
|
+
per spec, so mixed-auth factories compose (`role_grant_revoke` is
|
|
380
|
+
admin-gated beside non-admin offer siblings).
|
|
381
|
+
- **Input-dependent elevation.** `role_grant_offer_list` / `_history` (GET
|
|
382
|
+
addressable, `side_effects: false`) admit any caller to its own inbox; the
|
|
383
|
+
handler requires admin when `account_id` names another account — auth runs
|
|
384
|
+
before input parsing, so the spec can't say it. `account_delete`'s self
|
|
385
|
+
path is session-only the same way.
|
|
696
386
|
- **Account-grain self-service.** `account_*` specs declare
|
|
697
|
-
`
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
`
|
|
706
|
-
|
|
707
|
-
resulting grant outlives revoking the token. `role_grant_offer_create`
|
|
708
|
-
carries it for the same pivot onto another account (a leaked bearer offers a
|
|
709
|
-
role to an account its holder controls); scripted conferral is
|
|
710
|
-
`role_grant_assign`'s.
|
|
711
|
-
REST `POST /logout` also declares `credential_types: ['session']`, but
|
|
712
|
-
for forensic fidelity rather than a threat — a bearer / daemon token
|
|
713
|
-
holds no session to end, so the gate refuses it instead of returning a
|
|
714
|
-
misleading 200 + a phantom `logout` audit row. `GET /audit/stream` carries
|
|
715
|
-
it so the stream stays closable (SSE subscribers are keyed by session
|
|
716
|
-
hash, so `token_revoke` could not close a bearer's stream).
|
|
717
|
-
Admin token/session revoke specs deliberately stay unrestricted (admin
|
|
718
|
-
scripting from CLI/bearer is legitimate operator workflow). See
|
|
719
|
-
../../../docs/security.md §Credential-channel gating.
|
|
720
|
-
- **Rate-limit posture.** Admin specs and authed-spam-prone surfaces
|
|
721
|
-
(`role_grant_offer_create`, `role_grant_revoke`, `account_token_create`,
|
|
722
|
-
`self_service_role_set`, `actor_lookup`, `actor_search`) declare
|
|
723
|
-
`rate_limit: 'account'`. Throttle-requests semantics — every invocation
|
|
724
|
-
records, regardless of outcome. Default
|
|
725
|
-
`default_action_account_rate_limit` is 1200/15min per actor.
|
|
387
|
+
`actor: 'none'`, with no `acting` on the input; IDOR is the account-scoped
|
|
388
|
+
queries.
|
|
389
|
+
- **Credential-channel gating** — which specs and routes are session-only,
|
|
390
|
+
and why (`/logout` for forensic fidelity; `/audit/stream` because a
|
|
391
|
+
long-lived admin feed is not handed to a channel the deployment never
|
|
392
|
+
chose): ../../../docs/security.md §Credential-channel gating. Every gated
|
|
393
|
+
event records `credential_type` in its metadata.
|
|
394
|
+
- **Rate limits.** Admin specs and the spam-prone surfaces declare
|
|
395
|
+
`rate_limit: 'account'`, which records every invocation whatever its
|
|
396
|
+
outcome (`default_action_account_rate_limit`, keyed per account).
|
|
726
397
|
|
|
727
398
|
### Admin actions
|
|
728
399
|
|
|
729
400
|
`create_admin_actions(deps, options?)` in `auth/admin_actions.ts`.
|
|
730
401
|
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
- `
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
Constants: `AUDIT_LOG_LIST_LIMIT_MAX = 200`, `ADMIN_ACCOUNT_LIST_DEFAULT_LIMIT = 50`,
|
|
756
|
-
`ADMIN_ACCOUNT_LIST_LIMIT_MAX = 200`.
|
|
757
|
-
|
|
758
|
-
Error reasons via `error.data.reason`: `ERROR_ACCOUNT_NOT_FOUND` (404 via
|
|
759
|
-
`jsonrpc_errors.not_found`) on admin revoke-all + account delete/purge,
|
|
760
|
-
`ERROR_INSUFFICIENT_PERMISSIONS` (403) when `account_delete` targets another
|
|
761
|
-
account without admin, `ERROR_PURGE_NOT_CONFIRMED` (400) when `account_purge`
|
|
762
|
-
omits `confirm: true`, `ERROR_CANNOT_DELETE_KEEPER` (403) when `account_delete` /
|
|
763
|
-
`account_purge` targets a keeper-role account (the keeper account is never
|
|
764
|
-
API-removable — auth + daemon-token both pivot on it),
|
|
765
|
-
`ERROR_CANNOT_DELETE_LAST_ADMIN` (403) when `account_delete` / `account_purge`
|
|
766
|
-
targets the sole remaining active admin (soft-deleted admins don't count; the
|
|
767
|
-
count locks its rows, so concurrent removals can't both pass), `ERROR_INVITE_ACCOUNT_EXISTS_USERNAME` /
|
|
768
|
-
`_EMAIL` / `ERROR_INVITE_DUPLICATE` on invite create, `ERROR_INVITE_NOT_FOUND`
|
|
769
|
-
on invite delete (with a failure-audit row). `invite_create` empty input is rejected at the schema by a
|
|
770
|
-
`check` and surfaces as `invalid_params` with `error.data.issues` (DEV-only `WireIssue`s).
|
|
771
|
-
|
|
772
|
-
Closure state:
|
|
773
|
-
|
|
774
|
-
- `grantable_roles` derived once from `options.roles?.role_specs ?? builtin_role_specs`
|
|
775
|
-
via `list_roles_with_grant_path(_, GRANT_PATH_ADMIN)`.
|
|
776
|
-
- `app_settings_get`/`_update` handlers are always wired and read/write the DB
|
|
777
|
-
directly (`query_app_settings_load`/`query_app_settings_update`);
|
|
778
|
-
`auth/signup_routes.ts` reads the current value fresh per request. There is no
|
|
779
|
-
`options.app_settings` ref.
|
|
780
|
-
- `deps.connection_closer` (required, `RevokingActionFactoryDeps`) —
|
|
781
|
-
`admin_session_revoke_all` / `admin_token_revoke_all` / `account_delete` /
|
|
782
|
-
`account_purge` queue a close of the target account's live connections
|
|
783
|
-
(`queue_connection_close`), run after the handler's transaction commits and
|
|
784
|
-
dropped if it throws. The close does not depend on the audit write; the
|
|
785
|
-
audit listeners repeat it on the announced row. Failure outcomes queue no
|
|
786
|
-
close.
|
|
787
|
-
|
|
788
|
-
Failure-outcome audit rows: `admin_session_revoke_all` and `_token_revoke_all`
|
|
789
|
-
emit an `outcome: 'failure'` row on `ERROR_ACCOUNT_NOT_FOUND` for forensic
|
|
790
|
-
visibility — `target_account_id` is null (FK rejects missing ids), and the
|
|
791
|
-
probed id is preserved under `metadata.attempted_account_id`. Every gated
|
|
792
|
-
event additionally records `credential_type` in metadata (defense in depth).
|
|
402
|
+
- **`account_delete` / `account_purge` / `account_undelete` carry `account_*`
|
|
403
|
+
names**, not `admin_`: privilege lives in the auth spec, so soft delete
|
|
404
|
+
stays self-service. They sit in the admin factory because their actor-grain
|
|
405
|
+
shape and queries match it. `account_undelete` is admin-only — a tombstoned
|
|
406
|
+
account can't authenticate, so there is no self path.
|
|
407
|
+
- **Account removal** (delete and purge alike) supersedes every pending offer
|
|
408
|
+
the account made and every one made to it (`account_deleted`, audited and
|
|
409
|
+
notified), deletes the unclaimed invites it issued, revokes its sessions and
|
|
410
|
+
tokens, and writes the account row plus one per actor. Delete keeps the role
|
|
411
|
+
grants, inert; purge is the keeper-only cascading delete with
|
|
412
|
+
`confirm: true`. The target guards (never a keeper account, never the last
|
|
413
|
+
active admin) and what an undelete restores are ../../../docs/security.md
|
|
414
|
+
§Authorization.
|
|
415
|
+
- **Failure rows on a miss.** `admin_session_revoke_all`,
|
|
416
|
+
`admin_token_revoke_all`, account delete / purge and `invite_delete` write
|
|
417
|
+
a failure row on a missing target, the probed id in metadata
|
|
418
|
+
(`attempted_account_id`, `attempted_invite_id`) since the FK rejects it.
|
|
419
|
+
- **Closure state.** `grantable_roles` is derived once from
|
|
420
|
+
`options.roles?.role_specs ?? builtin_role_specs`. App settings are read and
|
|
421
|
+
written in the database; signup reads `open_signup` fresh per request, so
|
|
422
|
+
there is no shared in-memory ref.
|
|
423
|
+
- **Closes.** The revoke-alls and the account removals queue a close of the
|
|
424
|
+
target account's connections (`queue_connection_close`), after the commit
|
|
425
|
+
and independent of the audit row; failure outcomes queue none.
|
|
793
426
|
|
|
794
427
|
### Role-grant-offer actions
|
|
795
428
|
|
|
796
429
|
`create_role_grant_offer_actions(deps, options?)` in
|
|
797
|
-
`auth/role_grant_offer_actions.ts
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
> `
|
|
802
|
-
>
|
|
803
|
-
> `testing/admin_integration.ts`
|
|
804
|
-
>
|
|
805
|
-
> `testing/db_entities.ts`
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
-
|
|
822
|
-
|
|
823
|
-
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
the locked count `query_count_active_accounts_with_global_role_locked`,
|
|
830
|
-
which leaves the revoked grant out — §Row locks). Besides the revokee's
|
|
831
|
-
incoming sibling offers, the revoke supersedes every pending offer the
|
|
832
|
-
revokee's actor made — the rule `query_revoke_role_grant` holds for every
|
|
833
|
-
revoke, the self-service disable's too;
|
|
834
|
-
`account_delete` / `account_purge` supersede both directions for the whole
|
|
835
|
-
account (`reason: 'account_deleted'`). Every supersede cascade returns —
|
|
836
|
-
and audits and notifies — its rows ordered by `created_at, id`; a revoke's
|
|
837
|
-
is two such runs, its incoming siblings then its outgoing offers. Who is
|
|
838
|
-
told: §WS notifications.
|
|
839
|
-
- `role_grant_assign_action_spec` — global-admin-only immediate conferral (no
|
|
840
|
-
consent step); input `{to_account_id, to_actor_id?, role, scope_kind?, scope_id?}`
|
|
841
|
-
(the pair as on offer-create); output `{ok, role_grant_id}`. The handler:
|
|
842
|
-
- re-checks the global admin grant behind the dispatcher gate;
|
|
843
|
-
- runs the same admin-grant-path gate as offer-create, and refuses a scoped
|
|
844
|
-
`admin` (`invalid_params` + `ERROR_ROLE_GRANT_BUILTIN_SCOPED`; a scoped
|
|
845
|
-
`keeper` stays 403 `ERROR_ROLE_NOT_WEB_GRANTABLE`, since `keeper` is never
|
|
846
|
-
web-grantable);
|
|
847
|
-
- runs the consumer's `RoleGrantOfferCreateAuthorize` callback (the offer
|
|
848
|
-
policy binds the consent-free path too — a refusal is 403
|
|
849
|
-
`ERROR_ROLE_GRANT_OFFER_NOT_AUTHORIZED`, failure-audited);
|
|
850
|
-
- resolves the target actor (named `to_actor_id` ∈ the account's active
|
|
851
|
-
actors, else its sole active actor — multi-actor → `invalid_params`,
|
|
852
|
-
actorless → 404);
|
|
853
|
-
- writes the `(scope_kind, scope_id)` pair via the idempotent
|
|
854
|
-
`query_create_role_grant`, and emits a `role_grant_create` audit row
|
|
855
|
-
(metadata carries `scope_kind`, success and failure).
|
|
856
|
-
|
|
857
|
-
No WS notification in v1. Reasons: `ERROR_INSUFFICIENT_PERMISSIONS`,
|
|
858
|
-
`ERROR_ROLE_NOT_WEB_GRANTABLE`, `ERROR_ROLE_GRANT_BUILTIN_SCOPED`,
|
|
859
|
-
`ERROR_ROLE_GRANT_OFFER_NOT_AUTHORIZED`,
|
|
860
|
-
`ERROR_ROLE_GRANT_OFFER_ACTOR_ACCOUNT_MISMATCH`.
|
|
861
|
-
|
|
862
|
-
Every input carries `acting?: ActingActor` (registry-time invariant 2).
|
|
863
|
-
`role_grant_revoke` keys on **`actor_id`**, not `account_id` — role_grants
|
|
864
|
-
are actor-scoped and deriving actor from account collapses under multi-actor
|
|
865
|
-
accounts.
|
|
866
|
-
|
|
867
|
-
`role_grant_offer_create` runs the **admin-grantability gate first** (offered
|
|
868
|
-
role's `RoleSpec.grant_paths` must include `'admin'`), then the
|
|
869
|
-
`RoleGrantOfferCreateAuthorize` callback. **Default `default_authorize`: the
|
|
870
|
-
caller must hold a _global_ `admin` grant** — conferral is admin-only, with no
|
|
871
|
-
holder-propagation (holding the offered role confers no power to offer it). The
|
|
872
|
-
global check (`has_scoped_role(_, ROLE_ADMIN, null)`) stops a scoped `admin`
|
|
873
|
-
escalating into global authority over the offer surface. A consumer needing
|
|
874
|
-
scope-aware delegation (e.g. a classroom teacher offering within their own
|
|
875
|
-
scope) supplies a custom `authorize` callback. The callback receives
|
|
876
|
-
`{to_account_id, role, scope_kind, scope_id}` and the request `ctx` (`ctx.db` is
|
|
877
|
-
the request transaction, for read-only checks), and also gates
|
|
878
|
-
`role_grant_assign`.
|
|
879
|
-
|
|
880
|
-
**Builtin roles (`admin` / `keeper`) are global-only**, decided by the spine
|
|
881
|
-
ahead of any callback. Offer-create refuses a scoped `admin`
|
|
882
|
-
(`invalid_params` + `ERROR_ROLE_GRANT_BUILTIN_SCOPED`; `keeper`, scoped or not,
|
|
883
|
-
stays 403 `ERROR_ROLE_GRANT_OFFER_ROLE_NOT_GRANTABLE` at the grant-path gate)
|
|
884
|
-
and a builtin offered by
|
|
885
|
-
a caller without a global `admin` (`ERROR_ROLE_GRANT_OFFER_NOT_AUTHORIZED`), so
|
|
886
|
-
a consumer callback only ever sees app roles or a global admin's unscoped
|
|
887
|
-
builtin offer. Accept (`query_accept_offer`, the chokepoint where an offer
|
|
888
|
-
becomes a grant) refuses a `keeper` offer (`ERROR_ROLE_GRANT_OFFER_ROLE_NOT_GRANTABLE`),
|
|
889
|
-
a scoped `admin` offer (`invalid_request` + `ERROR_ROLE_GRANT_BUILTIN_SCOPED`), and a
|
|
890
|
-
builtin offer whose grantor no longer holds an active global `admin` on an
|
|
891
|
-
active actor of an active account (`ERROR_ROLE_GRANT_OFFER_NOT_AUTHORIZED`,
|
|
892
|
-
read under the grantor's account lock) — via
|
|
893
|
-
`RoleGrantOfferBuiltinRefusedError` (`refusal: BuiltinOfferRefusal`). Each
|
|
894
|
-
refusal leaves the offer pending and is audited as a failed
|
|
895
|
-
`role_grant_offer_accept` with `{offer_id, role, scope_id, reason}`. The
|
|
896
|
-
grantor re-check is builtin-only: an app-role offer's authority was the
|
|
897
|
-
callback's call at create time.
|
|
898
|
-
|
|
899
|
-
Error reasons (`as const` literals):
|
|
900
|
-
|
|
901
|
-
- `ERROR_ROLE_GRANT_OFFER_SELF_TARGET`
|
|
902
|
-
- `ERROR_ROLE_GRANT_OFFER_TERMINAL`
|
|
903
|
-
- `ERROR_ROLE_GRANT_OFFER_EXPIRED`
|
|
904
|
-
- `ERROR_ROLE_GRANT_OFFER_NOT_FOUND` (404-over-403 IDOR mask)
|
|
905
|
-
- `ERROR_ROLE_GRANT_OFFER_ROLE_NOT_GRANTABLE`
|
|
906
|
-
- `ERROR_ROLE_GRANT_OFFER_NOT_AUTHORIZED`
|
|
907
|
-
- `ERROR_ROLE_GRANT_OFFER_ACTOR_ACCOUNT_MISMATCH` (supplied `to_actor_id` doesn't belong to `to_account_id`)
|
|
908
|
-
- `ERROR_ROLE_GRANT_OFFER_ACTOR_MISMATCH` (actor-targeted offer accepted by wrong actor)
|
|
909
|
-
- `ERROR_ROLE_GRANT_BUILTIN_SCOPED` (a grant path asked to bind `admin` / `keeper` to a scope)
|
|
910
|
-
|
|
911
|
-
Plus re-uses from `http/error_schemas.ts`: `ERROR_ROLE_GRANT_NOT_FOUND`,
|
|
912
|
-
`ERROR_ROLE_NOT_WEB_GRANTABLE`, `ERROR_INSUFFICIENT_PERMISSIONS`,
|
|
913
|
-
`ERROR_ACCOUNT_NOT_FOUND`. Each spec declares the reason codes its handler
|
|
914
|
-
may surface via `spec.error_reasons`; drift is caught per-module by
|
|
915
|
-
../../test/auth/role_grant_offer_actions.error_reasons.test.ts.
|
|
916
|
-
|
|
917
|
-
Failure-outcome audits use `emit_create_failure_audit` /
|
|
918
|
-
`emit_revoke_failure_audit` so all denial paths land uniform rows; the
|
|
919
|
-
admin-role-denied path (pre-IDOR) on `role_grant_revoke` emits no audit,
|
|
920
|
-
matching the middleware auth-guard precedent.
|
|
430
|
+
`auth/role_grant_offer_actions.ts`; the authorization rules per action are
|
|
431
|
+
its module TSDoc, the threat model ../../../docs/security.md §Authorization
|
|
432
|
+
(builtin roles global-only, consentful grants, the supersede cascade).
|
|
433
|
+
|
|
434
|
+
> **Hazard — `role_grant_offer_create` does not auto-accept**, admin or not.
|
|
435
|
+
> It returns `{offer}`; acceptance is a separate `role_grant_offer_accept`.
|
|
436
|
+
> A test that needs a grant drives offer + accept (`testing/admin_integration.ts`
|
|
437
|
+
> `offer_and_accept`) or skips the consent path with
|
|
438
|
+
> `create_test_role_grant_direct` (`testing/db_entities.ts`).
|
|
439
|
+
|
|
440
|
+
- **Default `authorize`: the caller holds a _global_ `admin` grant.** No
|
|
441
|
+
holder-propagation — holding a role confers no power to offer it. A
|
|
442
|
+
consumer needing scope-aware delegation (a teacher offering within their
|
|
443
|
+
classroom) supplies `authorize` (`RoleGrantOfferCreateAuthorize`), which
|
|
444
|
+
also gates `role_grant_assign`. It runs after the admin-grant-path gate and
|
|
445
|
+
only ever sees app roles or a global admin's unscoped builtin offer — the
|
|
446
|
+
builtin rules are decided first.
|
|
447
|
+
- **`role_grant_revoke` keys on `actor_id`**, not `account_id`: grants are
|
|
448
|
+
actor-scoped, and deriving the actor collapses under multi-actor accounts.
|
|
449
|
+
A revoke supersedes the revokee's incoming siblings and every offer its
|
|
450
|
+
actor made (`query_revoke_role_grant`, shared with the self-service
|
|
451
|
+
disable). A global grant's revoke queues a role-gated close of the
|
|
452
|
+
revokee's connections; a scoped revoke closes nothing, since a role gate
|
|
453
|
+
admits on global grants alone.
|
|
454
|
+
- **`role_grant_assign`** is the global-admin-only consent-free path; it
|
|
455
|
+
sends no WS notification.
|
|
456
|
+
- **Denials are audited uniformly** as failure rows; the admin-role denial on
|
|
457
|
+
`role_grant_revoke` (pre-IDOR) writes none, as an auth-guard refusal
|
|
458
|
+
doesn't. Supersede rows go through `emit_supersede_audits`, shared by both
|
|
459
|
+
revokes and account removal, as in the Rust twin.
|
|
460
|
+
- `error_reasons` drift is pinned per module by
|
|
461
|
+
../../test/auth/role_grant_offer_actions.error_reasons.test.ts.
|
|
921
462
|
|
|
922
463
|
#### WS notifications
|
|
923
464
|
|
|
924
|
-
Post-commit via `emit_after_commit` (
|
|
465
|
+
Post-commit via `emit_after_commit` (`http/CLAUDE.md` §Pending Effects),
|
|
466
|
+
through `ctx.notification_sender` (`actions/CLAUDE.md` §Notification
|
|
467
|
+
sender):
|
|
925
468
|
|
|
926
469
|
- Create — `role_grant_offer_received` → recipient.
|
|
927
470
|
- Retract — `role_grant_offer_retracted` → recipient.
|
|
@@ -929,247 +472,86 @@ Post-commit via `emit_after_commit` (see `http/CLAUDE.md` §Pending Effects):
|
|
|
929
472
|
- Decline — `role_grant_offer_declined` → grantor.
|
|
930
473
|
- Revoke (`role_grant_revoke`) — `role_grant_revoked` → revokee + `_supersede` per superseded offer (the revokee's incoming siblings and its own outgoing offers), each to its grantor and its recipient.
|
|
931
474
|
- Delete / purge (`account_delete`, `account_purge`) — `_supersede` (`account_deleted`) per superseded offer to the live party: the recipient of each offer the account made, the grantor of each offer made to it. The removed account hears nothing (its sockets close).
|
|
932
|
-
- Self-service disable — `_supersede` per superseded offer
|
|
933
|
-
|
|
934
|
-
`queue_supersede_notifications
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
`
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
notification builders. `role_grant_revoked`'s params are
|
|
945
|
-
`{role_grant_id, role, scope_kind, scope_id, reason}`, the grant's
|
|
946
|
-
`scope_kind` and `scope_id` both `null` for a global grant, every key
|
|
947
|
-
required. `role_grant_offer_notification_actions` holds the specs as
|
|
948
|
-
handler-less actions: `to_ws_actions` (`actions/protocol.ts`) appends them to
|
|
949
|
-
the WS action set of an RPC list holding `role_grant_offer_create`, so the WS
|
|
950
|
-
endpoint's surface lists them as `remote_notification` methods. The revokee's
|
|
951
|
-
notification is `role_grant_revoked`, not the admin action's
|
|
952
|
-
`role_grant_revoke`, so the two methods never collide. They never reach
|
|
953
|
-
dispatch: an inbound frame for one is answered `method_not_found`. They are not SSE events; `role_grant_offer_notification_specs`
|
|
954
|
-
holds them as `EventSpec`s for a consumer that broadcasts them on an SSE
|
|
955
|
-
channel of its own. Handlers send through `ctx.notification_sender`
|
|
956
|
-
(`actions/CLAUDE.md` §Notification sender) with `queue_offer_notification` /
|
|
957
|
-
`queue_supersede_notifications`, each queued post-commit.
|
|
958
|
-
|
|
959
|
-
Deps: `RevokingActionFactoryDeps`.
|
|
960
|
-
`role_grant_revoke` of a global grant queues a close of the revokee account's
|
|
961
|
-
connections gated on the role (`queue_connection_close`, `{kind: 'role'}`),
|
|
962
|
-
run after the transaction commits and independent of the audit row's
|
|
963
|
-
announcement; the revocation guard repeats it on the announced row. A scoped revoke
|
|
964
|
-
closes nothing — a role gate admits on global grants alone.
|
|
965
|
-
Target account travels via the send argument, not the payload — `revoked_by`
|
|
966
|
-
is deliberately not in the `role_grant_revoked` payload (the revokee doesn't
|
|
967
|
-
need to learn the admin's identity).
|
|
968
|
-
|
|
969
|
-
Options: `roles?: RoleRegistry` (drives admin-grant-path lookup),
|
|
970
|
-
`default_ttl_ms?` (defaults to `ROLE_GRANT_OFFER_DEFAULT_TTL_MS` = 30 days),
|
|
971
|
-
`authorize?: RoleGrantOfferCreateAuthorize`.
|
|
475
|
+
- Self-service disable — `_supersede` per superseded offer, each to its grantor and its recipient, as the admin revoke (the caller's answer lists no offers).
|
|
476
|
+
|
|
477
|
+
`queue_supersede_notifications` sends in the cascade's row order
|
|
478
|
+
(`created_at, id`), grantor before recipient; `SupersedeAudience` names the
|
|
479
|
+
`cause_id` each party gets (a recipient never learns its grantor's account
|
|
480
|
+
id). The specs are `auth/role_grant_offer_notifications.ts`'s handler-less
|
|
481
|
+
`remote_notification` actions: `to_ws_actions` lists them on a WS endpoint
|
|
482
|
+
holding `role_grant_offer_create`, and an inbound frame for one is
|
|
483
|
+
`method_not_found`. The revokee's method is `role_grant_revoked`, never the
|
|
484
|
+
admin action's `role_grant_revoke`, and its payload omits who revoked.
|
|
485
|
+
`role_grant_offer_notification_specs` holds them as `EventSpec`s for a
|
|
486
|
+
consumer broadcasting them over SSE of its own.
|
|
972
487
|
|
|
973
488
|
### Account actions — self-service
|
|
974
489
|
|
|
975
|
-
`create_account_actions(deps, options?)` in `auth/account_actions.ts
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
- `
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
- `
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
`session_id` validates as `SessionId`, the fuz_schema `Blake3Hash` branded;
|
|
992
|
-
`token_id` as `ApiTokenId` (`tok_[A-Za-z0-9_-]{12}`). Every input and output
|
|
993
|
-
is a `@fuzdev/fuz_schema` schema.
|
|
994
|
-
|
|
995
|
-
Audit events via `deps.audit.emit` with `ip: ctx.client_ip`:
|
|
996
|
-
`session_revoke`, `session_revoke_all`, `token_create`, `token_revoke`. Every
|
|
997
|
-
gated event also records `credential_type` in metadata (mirrors REST
|
|
998
|
-
`password_change`).
|
|
999
|
-
|
|
1000
|
-
Options: `max_tokens?: number | null` (defaults to `DEFAULT_MAX_TOKENS`;
|
|
1001
|
-
`null` disables). Deps: `RevokingActionFactoryDeps` — `log`, `audit`, and the
|
|
1002
|
-
required `connection_closer`. Each revoking handler queues its close
|
|
1003
|
-
(`queue_connection_close`: one session, one token, or the account), which runs
|
|
1004
|
-
after the handler's transaction commits and is dropped if it throws;
|
|
1005
|
-
`account_token_create` queues a token close for each token its cap evicts.
|
|
1006
|
-
Failure outcomes (`revoked: false`) queue no close — mirrors the listeners'
|
|
1007
|
-
`outcome === 'success'` guard so attacker-guessable ids can't target
|
|
1008
|
-
arbitrary connections.
|
|
490
|
+
`create_account_actions(deps, options?)` in `auth/account_actions.ts`
|
|
491
|
+
(session and token list / revoke / revoke-all, token create, and
|
|
492
|
+
`account_verify`).
|
|
493
|
+
|
|
494
|
+
- **`account_verify` is the credential probe** on both spines, callable by a
|
|
495
|
+
narrowed token that names it. There is no REST probe; `GET /status`
|
|
496
|
+
answers a narrowed token as anonymous.
|
|
497
|
+
- **`account_token_create` is default-deny at mint**: `scope` and `lifetime`
|
|
498
|
+
are both required. The token cap (`max_tokens`, `DEFAULT_MAX_TOKENS`,
|
|
499
|
+
`null` disables) evicts the oldest, closing each evicted token's
|
|
500
|
+
connections.
|
|
501
|
+
- **Each revoking handler queues its close** (one session, one token, or the
|
|
502
|
+
account) after the commit. A failure outcome (`revoked: false`) queues
|
|
503
|
+
none, so an attacker-guessable id can't target another user's
|
|
504
|
+
connections — the same reason the revocation guard acts only on success
|
|
505
|
+
rows.
|
|
1009
506
|
|
|
1010
507
|
### Standard RPC bundle
|
|
1011
508
|
|
|
1012
|
-
`create_standard_rpc_actions(deps, options)`
|
|
1013
|
-
spreads
|
|
1014
|
-
`
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
`
|
|
1018
|
-
|
|
1019
|
-
Option routing — `roles` is shared between admin + role-grant-offer;
|
|
1020
|
-
`default_ttl_ms` + `authorize` → role-grant-offer
|
|
1021
|
-
only; `max_tokens` → account only. On `deps`: `connection_closer`
|
|
1022
|
-
(required) → admin, account, and role-grant-offer (the role revoke's close).
|
|
1023
|
-
The notification sender is on every action's context, not on `deps`.
|
|
1024
|
-
|
|
1025
|
-
Pair with `create_app_server`'s `rpc_endpoints` factory form
|
|
1026
|
-
`(ctx) => Array<RpcEndpointSpec>` so the combined action list gets
|
|
1027
|
-
`ctx.deps`. `create_app_server` auto-mounts the
|
|
1028
|
-
endpoint via `create_rpc_endpoint`. To expose the standard surface over
|
|
1029
|
-
WebSocket as well, spread `protocol_actions` and the same factory into
|
|
1030
|
-
`ws_endpoints` — per-message authorization and rate limiting fire
|
|
1031
|
-
identically across HTTP RPC and WS.
|
|
1032
|
-
|
|
1033
|
-
Bundling account actions into the "standard" surface is deliberate: the
|
|
1034
|
-
admin integration suite exercises `account_token_create` / `_revoke` for
|
|
1035
|
-
cross-account isolation, so a consumer wiring the admin surface without
|
|
1036
|
-
account actions hits `method_not_found` on first admin-suite run.
|
|
509
|
+
`create_standard_rpc_actions(deps, options)` (`auth/standard_rpc_actions.ts`)
|
|
510
|
+
spreads the admin, role-grant-offer and account factories into one
|
|
511
|
+
`Array<RpcAction>`; the frontend mirror is `all_standard_action_specs`
|
|
512
|
+
(`auth/standard_action_specs.ts`). Option routing: `roles` → admin and
|
|
513
|
+
role-grant-offer; `default_ttl_ms` and `authorize` → role-grant-offer;
|
|
514
|
+
`max_tokens` → account. `deps.connection_closer` reaches all three.
|
|
1037
515
|
|
|
1038
|
-
|
|
516
|
+
Mount it through `create_app_server`'s `rpc_endpoints` factory form
|
|
517
|
+
`(ctx) => Array<RpcEndpointSpec>` so the handlers get `ctx.deps`; over
|
|
518
|
+
WebSocket, the same list goes in `ws_endpoints` (root ../../../CLAUDE.md
|
|
519
|
+
§Action Registries).
|
|
1039
520
|
|
|
1040
|
-
|
|
1041
|
-
`
|
|
1042
|
-
|
|
1043
|
-
caller. Idempotent in both directions (`changed: false` when the post-call
|
|
1044
|
-
state already matched). Its input and output are `@fuzdev/fuz_schema`
|
|
1045
|
-
`object`s, refusing unknown keys, `role` a `RoleName`.
|
|
1046
|
-
|
|
1047
|
-
Audit metadata carries `self_service: true` so admin reviewers can
|
|
1048
|
-
distinguish self-toggled role_grants. Eligibility derives from
|
|
1049
|
-
`roles.role_specs` by selecting roles with `'self_service' ∈ grant_paths`;
|
|
1050
|
-
override via `eligible_roles`. Roles outside the eligible set are rejected
|
|
1051
|
-
with `ERROR_ROLE_NOT_SELF_SERVICE_ELIGIBLE`. The eligible set is validated at
|
|
1052
|
-
construction (`validate_self_service_eligible_roles`): a builtin role or a name
|
|
1053
|
-
outside the `RoleName` grammar throws — self-toggling a builtin would hand any
|
|
1054
|
-
account instance-wide authority.
|
|
1055
|
-
|
|
1056
|
-
A disable revokes through `query_revoke_role_grant`, so it ends what the admin
|
|
1057
|
-
revoke ends: the caller's pending offers of the role and every offer its actor
|
|
1058
|
-
made, whatever the role, each audited (`role_grant_offer_supersede`,
|
|
1059
|
-
`role_grant_revoked`, the grant as cause) and told to both parties.
|
|
1060
|
-
|
|
1061
|
-
Deps are `RevokingActionFactoryDeps`: a disable that revokes queues a close of
|
|
1062
|
-
the caller's connections gated on the role (`queue_connection_close`,
|
|
1063
|
-
`{kind: 'role'}`), after the commit and independent of the audit write.
|
|
1064
|
-
|
|
1065
|
-
Method name is static (`role` lives in input, not method) — per-role
|
|
1066
|
-
parameterized methods would break the `satisfies RequestResponseActionSpec`
|
|
1067
|
-
codegen invariant.
|
|
1068
|
-
|
|
1069
|
-
Bundle **not** included in `create_standard_rpc_actions` — `eligible_roles`
|
|
1070
|
-
is app-specific.
|
|
521
|
+
Bundling the account actions is deliberate: the admin integration suite
|
|
522
|
+
exercises `account_token_create` / `_revoke` for cross-account isolation, so
|
|
523
|
+
a consumer mounting admin without account hits `method_not_found`.
|
|
1071
524
|
|
|
1072
|
-
###
|
|
1073
|
-
|
|
1074
|
-
Two opt-in helpers for surfaces that stamp actor ids (bylines, owner
|
|
1075
|
-
columns, grantor labels, picker UIs):
|
|
1076
|
-
|
|
1077
|
-
- `create_actor_lookup_actions(deps)` — `actor_lookup({ids}) → {actors}`,
|
|
1078
|
-
batched id → label resolver. `ACTOR_LOOKUP_IDS_MAX = 50`.
|
|
1079
|
-
- `create_actor_search_actions(deps)` — `actor_search({query, scope_ids?, limit?}) → {actors}`,
|
|
1080
|
-
prefix search. Default limit `ACTOR_SEARCH_LIMIT_DEFAULT = 20`, cap
|
|
1081
|
-
`_MAX = 50`. Non-admin callers must pass `scope_ids` (filtered to actors
|
|
1082
|
-
holding active role_grants on those scopes); admin-only when `scope_ids`
|
|
1083
|
-
is empty. `ERROR_ACTOR_SEARCH_SCOPE_REQUIRED` on non-admin + empty
|
|
1084
|
-
`scope_ids`.
|
|
1085
|
-
|
|
1086
|
-
Both: `auth: {account: 'required', actor: 'none'}` + `rate_limit: 'account'`,
|
|
1087
|
-
pure reads (no audit, no side effects), their input and output schemas
|
|
1088
|
-
`@fuzdev/fuz_schema` `object`s, which refuse unknown keys. `ActorLookupEntryJson` deliberately
|
|
1089
|
-
omits `account_id`, `email`, credentials, timestamps, and role state —
|
|
1090
|
-
control-plane details, timing-oracle avoidance, separation of concern. LIKE
|
|
1091
|
-
wildcards in the user-supplied query are escaped at the JS layer so
|
|
1092
|
-
`%xyz`-style inputs can't widen the per-call cap.
|
|
525
|
+
### Self-service role toggle
|
|
1093
526
|
|
|
1094
|
-
|
|
527
|
+
`create_self_service_role_actions(deps, {eligible_roles?, roles?})` —
|
|
528
|
+
`self_service_role_set({role, enabled})`, idempotent both ways, audited with
|
|
529
|
+
`self_service: true`. Eligibility defaults to roles with the `self_service`
|
|
530
|
+
grant path; a builtin or off-grammar name in `eligible_roles` throws at
|
|
531
|
+
construction, since self-toggling a builtin would hand any account
|
|
532
|
+
instance-wide authority. A disable revokes through `query_revoke_role_grant`,
|
|
533
|
+
so it supersedes what the admin revoke does and queues the same role-gated
|
|
534
|
+
close. The method is static (`role` is input) so codegen's
|
|
535
|
+
`satisfies RequestResponseActionSpec` holds.
|
|
1095
536
|
|
|
1096
|
-
###
|
|
537
|
+
### Actor lookup / actor search
|
|
1097
538
|
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
`
|
|
1101
|
-
|
|
1102
|
-
`
|
|
539
|
+
Two opt-in, account-grain, rate-limited pure reads for surfaces that stamp
|
|
540
|
+
actor ids (bylines, grantor labels, pickers): `create_actor_lookup_actions`
|
|
541
|
+
(`actor_lookup({ids})`, batched) and `create_actor_search_actions`
|
|
542
|
+
(`actor_search({query, scope_ids?, limit?})`, prefix). A non-admin search
|
|
543
|
+
must pass `scope_ids` (`ERROR_ACTOR_SEARCH_SCOPE_REQUIRED`). The entry shape
|
|
544
|
+
deliberately omits `account_id`, email, credentials, timestamps and role
|
|
545
|
+
state, and LIKE wildcards in the query are escaped so `%`-inputs can't widen
|
|
546
|
+
a call. Caps and limits are on the spec modules.
|
|
1103
547
|
|
|
1104
548
|
## Cleanup
|
|
1105
549
|
|
|
1106
|
-
`auth/cleanup.ts
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
pass at once, then one per interval (`DEFAULT_AUTH_CLEANUP_INTERVAL_MS`, ten
|
|
1115
|
-
minutes), until `stop()`. `create_app_server`'s `auth_cleanup` option calls
|
|
1116
|
-
it with `backend.deps` and chains `stop` into `AppServer.close`. **Off by
|
|
1117
|
-
default** there; a production server turns it on, and an in-process test
|
|
1118
|
-
harness leaves it off (a background pass deletes rows and writes audit rows
|
|
1119
|
-
mid-test).
|
|
1120
|
-
|
|
1121
|
-
`AuthCleanupDeps` is `{db, log, audit, connection_closer}`, all required —
|
|
1122
|
-
`AppDeps` satisfies it. Tests pass `create_test_audit_emitter()` from
|
|
1123
|
-
`testing/stubs.ts` and, where nothing is to be closed, `noop_connection_closer`.
|
|
1124
|
-
The sweep belongs in the process that holds the connections: run from another
|
|
1125
|
-
process against a live server's database, it deletes the rows that server's
|
|
1126
|
-
own pass would have closed for.
|
|
1127
|
-
|
|
1128
|
-
**The session sweep closes what it sweeps.** A connection never re-reads its
|
|
1129
|
-
session, and once the sweep deletes an expired row `account_session_revoke`
|
|
1130
|
-
has nothing left to close for — so `cleanup_expired_sessions` closes each
|
|
1131
|
-
swept session's connections itself, by session id
|
|
1132
|
-
(`connection_closer.close_connections` with a `session` target), which is why
|
|
1133
|
-
`query_session_cleanup_expired` returns the deleted ids. The close repeats
|
|
1134
|
-
one already made: every connection closes at its credential's deadline
|
|
1135
|
-
(`auth/credential_deadline.ts`) in the process holding it, sweep or no sweep.
|
|
1136
|
-
An expired API token's row is not swept; its connections close at their
|
|
1137
|
-
deadline (`account_token_revoke` still finds the row).
|
|
1138
|
-
|
|
1139
|
-
- The delete runs in its own transaction and the closes follow the commit —
|
|
1140
|
-
the rule every revocation close keeps (`actions/CLAUDE.md` §Connection
|
|
1141
|
-
closer).
|
|
1142
|
-
- A transaction that fails once the delete has answered still closes: the
|
|
1143
|
-
sessions are expired either way, the error is thrown after the closes, and
|
|
1144
|
-
the next pass deletes the rows the commit left.
|
|
1145
|
-
- A closer that throws for one session is logged and the rest still close.
|
|
1146
|
-
- No audit row is written for a swept session, so no listener repeats the
|
|
1147
|
-
close.
|
|
1148
|
-
|
|
1149
|
-
**The schedule** is `start_periodic` (`periodic.ts`, twin of the Rust
|
|
1150
|
-
`fuz_sys::periodic::spawn_periodic`), which owns the loop's rules:
|
|
1151
|
-
`start_auth_cleanup` is that loop over `run_auth_cleanup` plus the swept-counts
|
|
1152
|
-
log. Passes never overlap, and a pass that overruns its interval delays the
|
|
1153
|
-
next one (which starts when the slow one ends, the one after a full interval
|
|
1154
|
-
later) rather than bursting. A failed pass is logged at `warn` with its error
|
|
1155
|
-
and retried on the next interval. The timer is unref'd. An interval that is
|
|
1156
|
-
not a positive finite number, or exceeds `PERIODIC_INTERVAL_MS_MAX` (the
|
|
1157
|
-
largest delay a timer honors), throws at the call. **Shutdown differs from
|
|
1158
|
-
Rust**: its task drops a pass in progress; `stop()` ends the schedule at once
|
|
1159
|
-
and its promise resolves when the pass in progress has finished — await it
|
|
1160
|
-
before closing the database. The wait has no bound of its own.
|
|
1161
|
-
|
|
1162
|
-
**Offer idempotency.** `cleanup_expired_role_grant_offers` runs one transaction:
|
|
1163
|
-
`query_role_grant_offer_sweep_expired` claims the expired pending offers not
|
|
1164
|
-
yet audited by stamping `role_grant_offer.expire_audited_at` (`UPDATE …
|
|
1165
|
-
RETURNING`), and each claimed row's `role_grant_offer_expire` audit row is
|
|
1166
|
-
inserted in the same transaction; events fan out via `audit.notify` after the
|
|
1167
|
-
commit. Each expiry is audited exactly once — a second run finds nothing, and
|
|
1168
|
-
concurrent runs claim disjoint rows (a row another sweep holds blocks on its
|
|
1169
|
-
row lock, then fails the re-checked stamp). A failed audit insert rolls the
|
|
1170
|
-
stamps back and the next run retries, so the offer sweep can throw. The stamp
|
|
1171
|
-
is not a lifecycle state (outside the terminal CHECK and the pending
|
|
1172
|
-
predicate) and stays off the wire (`to_role_grant_offer_json` drops it); the
|
|
1173
|
-
create upsert's re-offer clears it, so a refreshed offer's own expiry is
|
|
1174
|
-
audited too. The `role_grant_offer_expire_sweep` partial index serves the
|
|
1175
|
-
claim.
|
|
550
|
+
`auth/cleanup.ts`, twin of the Rust `fuz_auth::auth_cleanup`:
|
|
551
|
+
`run_auth_cleanup` (one pass — the session sweep, which closes the swept
|
|
552
|
+
sessions' connections, then the offer-expiry sweep, audited exactly once)
|
|
553
|
+
and `start_auth_cleanup` (scheduled on `start_periodic`, `periodic.ts`). Off
|
|
554
|
+
by default in `create_app_server`; run it in the process that holds the
|
|
555
|
+
connections, never from another process against a live server's database.
|
|
556
|
+
The rules are the module TSDoc and `periodic.ts`'s (whose `stop()` differs
|
|
557
|
+
from the Rust shutdown); wiring is ../../../docs/usage.md §Auth cleanup.
|