@fuzdev/fuz_app 0.183.0 → 0.185.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.
Files changed (223) hide show
  1. package/dist/actions/CLAUDE.md +538 -1223
  2. package/dist/actions/action_codegen.d.ts +1 -1
  3. package/dist/actions/action_codegen.d.ts.map +1 -1
  4. package/dist/actions/action_codegen.js +3 -2
  5. package/dist/actions/connection_closer.d.ts +11 -9
  6. package/dist/actions/connection_closer.d.ts.map +1 -1
  7. package/dist/actions/connection_closer.js +11 -9
  8. package/dist/actions/frontend_rpc_client.d.ts +1 -1
  9. package/dist/actions/register_action_ws.d.ts +21 -1
  10. package/dist/actions/register_action_ws.d.ts.map +1 -1
  11. package/dist/actions/register_action_ws.js +2 -1
  12. package/dist/actions/ws_socket_session.d.ts +25 -4
  13. package/dist/actions/ws_socket_session.d.ts.map +1 -1
  14. package/dist/actions/ws_socket_session.js +81 -25
  15. package/dist/auth/CLAUDE.md +433 -1048
  16. package/dist/auth/account_queries.d.ts +1 -1
  17. package/dist/auth/account_queries.js +1 -1
  18. package/dist/auth/account_routes.d.ts +4 -5
  19. package/dist/auth/account_routes.d.ts.map +1 -1
  20. package/dist/auth/account_routes.js +8 -10
  21. package/dist/auth/admin_actions.d.ts.map +1 -1
  22. package/dist/auth/admin_actions.js +10 -11
  23. package/dist/auth/api_token.d.ts +20 -3
  24. package/dist/auth/api_token.d.ts.map +1 -1
  25. package/dist/auth/api_token.js +28 -9
  26. package/dist/auth/api_token_queries.d.ts +2 -2
  27. package/dist/auth/api_token_queries.js +2 -2
  28. package/dist/auth/audit_log_routes.d.ts +1 -1
  29. package/dist/auth/audit_log_routes.d.ts.map +1 -1
  30. package/dist/auth/audit_log_routes.js +3 -2
  31. package/dist/auth/audit_log_schema.d.ts +15 -9
  32. package/dist/auth/audit_log_schema.d.ts.map +1 -1
  33. package/dist/auth/audit_log_schema.js +1 -2
  34. package/dist/auth/auth_guard_resolver.d.ts +1 -1
  35. package/dist/auth/auth_guard_resolver.js +1 -1
  36. package/dist/auth/bearer_auth.d.ts +2 -5
  37. package/dist/auth/bearer_auth.d.ts.map +1 -1
  38. package/dist/auth/bearer_auth.js +4 -13
  39. package/dist/auth/bootstrap_account.d.ts +13 -15
  40. package/dist/auth/bootstrap_account.d.ts.map +1 -1
  41. package/dist/auth/bootstrap_account.js +9 -11
  42. package/dist/auth/bootstrap_routes.d.ts +20 -21
  43. package/dist/auth/bootstrap_routes.d.ts.map +1 -1
  44. package/dist/auth/bootstrap_routes.js +21 -19
  45. package/dist/auth/bootstrap_token_store.d.ts +3 -4
  46. package/dist/auth/bootstrap_token_store.d.ts.map +1 -1
  47. package/dist/auth/bootstrap_token_store.js +1 -1
  48. package/dist/auth/cleanup.d.ts +34 -3
  49. package/dist/auth/cleanup.d.ts.map +1 -1
  50. package/dist/auth/cleanup.js +11 -3
  51. package/dist/auth/daemon_token.d.ts +5 -1
  52. package/dist/auth/daemon_token.d.ts.map +1 -1
  53. package/dist/auth/daemon_token_middleware.d.ts +12 -8
  54. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  55. package/dist/auth/daemon_token_middleware.js +15 -21
  56. package/dist/auth/deps.d.ts +11 -0
  57. package/dist/auth/deps.d.ts.map +1 -1
  58. package/dist/auth/invite_queries.d.ts +51 -3
  59. package/dist/auth/invite_queries.d.ts.map +1 -1
  60. package/dist/auth/invite_queries.js +81 -6
  61. package/dist/auth/invite_schema.d.ts +2 -1
  62. package/dist/auth/invite_schema.d.ts.map +1 -1
  63. package/dist/auth/middleware.d.ts +2 -3
  64. package/dist/auth/middleware.d.ts.map +1 -1
  65. package/dist/auth/middleware.js +3 -3
  66. package/dist/auth/request_context.d.ts +3 -3
  67. package/dist/auth/request_context.js +3 -3
  68. package/dist/auth/role_grant_offer_actions.d.ts +2 -2
  69. package/dist/auth/role_grant_offer_actions.js +2 -2
  70. package/dist/auth/session_cookie.d.ts +82 -174
  71. package/dist/auth/session_cookie.d.ts.map +1 -1
  72. package/dist/auth/session_cookie.js +80 -130
  73. package/dist/auth/session_middleware.d.ts +8 -17
  74. package/dist/auth/session_middleware.d.ts.map +1 -1
  75. package/dist/auth/session_middleware.js +14 -23
  76. package/dist/auth/session_queries.d.ts +9 -14
  77. package/dist/auth/session_queries.d.ts.map +1 -1
  78. package/dist/auth/session_queries.js +8 -9
  79. package/dist/auth/signup_routes.d.ts.map +1 -1
  80. package/dist/auth/signup_routes.js +4 -6
  81. package/dist/db/CLAUDE.md +5 -3
  82. package/dist/dev/setup.d.ts +3 -1
  83. package/dist/dev/setup.d.ts.map +1 -1
  84. package/dist/dev/setup.js +3 -1
  85. package/dist/http/CLAUDE.md +315 -768
  86. package/dist/http/db_routes.d.ts +1 -1
  87. package/dist/http/db_routes.js +1 -1
  88. package/dist/http/origin.d.ts +7 -8
  89. package/dist/http/origin.d.ts.map +1 -1
  90. package/dist/http/origin.js +7 -8
  91. package/dist/http/pending_effects.d.ts +82 -4
  92. package/dist/http/pending_effects.d.ts.map +1 -1
  93. package/dist/http/pending_effects.js +82 -4
  94. package/dist/http/route_result.d.ts +1 -1
  95. package/dist/http/route_spec.d.ts +17 -14
  96. package/dist/http/route_spec.d.ts.map +1 -1
  97. package/dist/realtime/audit_log_sse.d.ts +1 -1
  98. package/dist/realtime/sse.d.ts +6 -25
  99. package/dist/realtime/sse.d.ts.map +1 -1
  100. package/dist/realtime/sse.js +4 -25
  101. package/dist/realtime/sse_constants.d.ts +1 -1
  102. package/dist/realtime/sse_constants.js +1 -1
  103. package/dist/server/app_backend.d.ts +4 -5
  104. package/dist/server/app_backend.d.ts.map +1 -1
  105. package/dist/server/app_backend.js +3 -1
  106. package/dist/server/app_server.d.ts +87 -76
  107. package/dist/server/app_server.d.ts.map +1 -1
  108. package/dist/server/app_server.js +111 -91
  109. package/dist/server/app_server_context.d.ts +1 -2
  110. package/dist/server/app_server_context.d.ts.map +1 -1
  111. package/dist/server/env.js +2 -2
  112. package/dist/server/shutdown_drain.d.ts +69 -0
  113. package/dist/server/shutdown_drain.d.ts.map +1 -0
  114. package/dist/server/shutdown_drain.js +77 -0
  115. package/dist/testing/CLAUDE.md +89 -57
  116. package/dist/testing/admin_integration.d.ts +4 -5
  117. package/dist/testing/admin_integration.d.ts.map +1 -1
  118. package/dist/testing/admin_integration.js +39 -23
  119. package/dist/testing/app_server.d.ts +3 -8
  120. package/dist/testing/app_server.d.ts.map +1 -1
  121. package/dist/testing/app_server.js +10 -9
  122. package/dist/testing/attack_surface.d.ts +1 -1
  123. package/dist/testing/attack_surface.d.ts.map +1 -1
  124. package/dist/testing/audit_completeness.d.ts +2 -3
  125. package/dist/testing/audit_completeness.d.ts.map +1 -1
  126. package/dist/testing/audit_completeness.js +1 -1
  127. package/dist/testing/cross_backend/bootstrap_backend.d.ts +3 -1
  128. package/dist/testing/cross_backend/bootstrap_backend.d.ts.map +1 -1
  129. package/dist/testing/cross_backend/bootstrap_backend.js +5 -1
  130. package/dist/testing/cross_backend/capability_mounts.d.ts.map +1 -1
  131. package/dist/testing/cross_backend/capability_mounts.js +16 -11
  132. package/dist/testing/cross_backend/conformance_table.d.ts +5 -6
  133. package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
  134. package/dist/testing/cross_backend/conformance_table.js +4 -4
  135. package/dist/testing/cross_backend/cookie_attributes.js +1 -1
  136. package/dist/testing/cross_backend/create_dual_spawn_global_setup.d.ts +10 -1
  137. package/dist/testing/cross_backend/create_dual_spawn_global_setup.d.ts.map +1 -1
  138. package/dist/testing/cross_backend/create_dual_spawn_global_setup.js +33 -7
  139. package/dist/testing/cross_backend/default_backend_configs.d.ts +3 -3
  140. package/dist/testing/cross_backend/default_spine_surface.d.ts +0 -6
  141. package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
  142. package/dist/testing/cross_backend/default_spine_surface.js +4 -9
  143. package/dist/testing/cross_backend/differential.d.ts +2 -3
  144. package/dist/testing/cross_backend/differential.d.ts.map +1 -1
  145. package/dist/testing/cross_backend/differential.js +1 -1
  146. package/dist/testing/cross_backend/full_spine_mount.d.ts +1 -42
  147. package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
  148. package/dist/testing/cross_backend/full_spine_mount.js +45 -4
  149. package/dist/testing/cross_backend/in_process_setup.d.ts +7 -8
  150. package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
  151. package/dist/testing/cross_backend/in_process_setup.js +10 -13
  152. package/dist/testing/cross_backend/rpc_success_fixtures.d.ts.map +1 -1
  153. package/dist/testing/cross_backend/rpc_success_fixtures.js +2 -1
  154. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts +3 -4
  155. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
  156. package/dist/testing/cross_backend/rust_spine_consumer_surface.js +2 -2
  157. package/dist/testing/cross_backend/setup.d.ts +1 -1
  158. package/dist/testing/cross_backend/spawn_backend.d.ts +14 -0
  159. package/dist/testing/cross_backend/spawn_backend.d.ts.map +1 -1
  160. package/dist/testing/cross_backend/spawn_backend.js +56 -8
  161. package/dist/testing/cross_backend/spine_suites.d.ts +18 -16
  162. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  163. package/dist/testing/cross_backend/spine_suites.js +38 -18
  164. package/dist/testing/cross_backend/standard.d.ts +19 -84
  165. package/dist/testing/cross_backend/standard.d.ts.map +1 -1
  166. package/dist/testing/cross_backend/standard.js +8 -23
  167. package/dist/testing/cross_backend/standard_auth.d.ts +74 -0
  168. package/dist/testing/cross_backend/standard_auth.d.ts.map +1 -0
  169. package/dist/testing/cross_backend/standard_auth.js +28 -0
  170. package/dist/testing/cross_backend/standard_bundle.d.ts +48 -0
  171. package/dist/testing/cross_backend/standard_bundle.d.ts.map +1 -0
  172. package/dist/testing/cross_backend/standard_bundle.js +43 -0
  173. package/dist/testing/cross_backend/testing_credential_read.d.ts +10 -11
  174. package/dist/testing/cross_backend/testing_credential_read.d.ts.map +1 -1
  175. package/dist/testing/cross_backend/testing_credential_read.js +12 -12
  176. package/dist/testing/cross_backend/testing_reset_actions.d.ts +46 -30
  177. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  178. package/dist/testing/cross_backend/testing_reset_actions.js +52 -31
  179. package/dist/testing/cross_backend/testing_server_bun.d.ts.map +1 -1
  180. package/dist/testing/cross_backend/testing_server_bun.js +5 -4
  181. package/dist/testing/cross_backend/testing_server_core.d.ts +16 -3
  182. package/dist/testing/cross_backend/testing_server_core.d.ts.map +1 -1
  183. package/dist/testing/cross_backend/testing_server_core.js +22 -7
  184. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts +6 -2
  185. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts.map +1 -1
  186. package/dist/testing/cross_backend/ts_spine_backend_config.js +7 -3
  187. package/dist/testing/daemon_token_rotation.d.ts.map +1 -1
  188. package/dist/testing/daemon_token_rotation.js +2 -3
  189. package/dist/testing/error_coverage.d.ts +5 -0
  190. package/dist/testing/error_coverage.d.ts.map +1 -1
  191. package/dist/testing/error_coverage.js +2 -1
  192. package/dist/testing/integration.d.ts +6 -23
  193. package/dist/testing/integration.d.ts.map +1 -1
  194. package/dist/testing/integration.js +53 -83
  195. package/dist/testing/integration_helpers.d.ts +16 -0
  196. package/dist/testing/integration_helpers.d.ts.map +1 -1
  197. package/dist/testing/integration_helpers.js +29 -1
  198. package/dist/testing/middleware_gate_probes.d.ts +55 -1
  199. package/dist/testing/middleware_gate_probes.d.ts.map +1 -1
  200. package/dist/testing/middleware_gate_probes.js +69 -3
  201. package/dist/testing/rate_limiting.d.ts +2 -3
  202. package/dist/testing/rate_limiting.d.ts.map +1 -1
  203. package/dist/testing/rate_limiting.js +3 -3
  204. package/dist/testing/rpc_helpers.d.ts +1 -2
  205. package/dist/testing/rpc_helpers.d.ts.map +1 -1
  206. package/dist/testing/rpc_helpers.js +3 -3
  207. package/dist/testing/rpc_round_trip.d.ts +2 -3
  208. package/dist/testing/rpc_round_trip.d.ts.map +1 -1
  209. package/dist/testing/rpc_round_trip.js +1 -1
  210. package/dist/testing/sse_round_trip.d.ts +2 -3
  211. package/dist/testing/sse_round_trip.d.ts.map +1 -1
  212. package/dist/testing/sse_round_trip.js +2 -2
  213. package/dist/testing/stubs.d.ts +5 -6
  214. package/dist/testing/stubs.d.ts.map +1 -1
  215. package/dist/testing/stubs.js +10 -8
  216. package/dist/testing/test_app.d.ts +8 -5
  217. package/dist/testing/test_app.d.ts.map +1 -1
  218. package/dist/testing/test_app.js +7 -7
  219. package/dist/testing/ws_round_trip.d.ts.map +1 -1
  220. package/dist/testing/ws_round_trip.js +3 -0
  221. package/dist/ui/ui_format.d.ts.map +1 -1
  222. package/dist/ui/ui_format.js +1 -2
  223. package/package.json +1 -1
@@ -20,12 +20,60 @@ import type { Invite, CreateInviteInput, InviteWithUsernamesJson } from './invit
20
20
  */
21
21
  export declare const INVITE_COLUMNS: readonly ["id", "email", "username", "claimed_by", "claimed_at", "created_at", "created_by"];
22
22
  /**
23
- * Create a new invite.
23
+ * Error thrown by `query_create_invite` when an unclaimed invite already
24
+ * reserves the email or the username: the partial unique index
25
+ * `idx_invite_email_unclaimed` or `idx_invite_username_unclaimed` refused the
26
+ * insert (SQLSTATE `23505`), which aborts the caller's transaction. Twin of
27
+ * the Rust `InviteCreateError::Duplicate`.
28
+ */
29
+ export declare class InviteDuplicateError extends Error {
30
+ constructor();
31
+ }
32
+ /**
33
+ * Error thrown by `query_create_invite` when the issuer is not an active
34
+ * account: its actor is missing or tombstoned, or its account soft-deleted — a
35
+ * delete that committed while the invite was in flight. Twin of the Rust
36
+ * `InviteCreateError::IssuerNotActive`.
37
+ */
38
+ export declare class InviteIssuerNotActiveError extends Error {
39
+ constructor();
40
+ }
41
+ /**
42
+ * Create an invite issued by the actor `input.created_by`.
24
43
  *
25
- * @param deps - query dependencies
26
- * @param input - the invite fields
44
+ * The issuer must be an active actor of a live account, checked here so every
45
+ * caller — the spine's `invite_create` handler and a consumer minting invites
46
+ * on its own path — gets the rule: an invite records no authority, so one
47
+ * minted for an issuer deleted mid-flight would outlive the delete that should
48
+ * have voided it. The issuer's account row is read under `FOR SHARE`
49
+ * (`query_account_lock_live_many`) before the insert: an account delete writes
50
+ * its tombstone first, so a delete that wrote its tombstone before the read is
51
+ * waited for if uncommitted and seen (the invite is refused), and one that
52
+ * reaches the row after it waits for this transaction to
53
+ * commit and then deletes the invite with the account's other unclaimed ones
54
+ * (`query_invite_delete_unclaimed_by_creators`). The actor's tombstone is read
55
+ * after the lock, so a delete waited on is seen there too.
56
+ *
57
+ * Run it inside the caller's transaction, which the lock spans — on the pool
58
+ * the lock ends with its statement and orders nothing. Lock order: account
59
+ * rows come before actor and `role_grant` rows, as the last-admin guard
60
+ * (`query_count_active_accounts_with_global_role_locked`) takes them (account,
61
+ * then actor, then grant). So take it before the transaction locks any actor
62
+ * or `role_grant` row — or lock the issuer's account row first yourself
63
+ * (`query_account_lock_live_many`), which this query then re-takes without
64
+ * waiting: a transaction holding a lock on the issuer's admin grant while it
65
+ * waits here on an account row a removal holds, while the removal's guard
66
+ * waits on that grant, deadlocks.
67
+ *
68
+ * Callers that pre-check for account collisions on `email` / `username` do so
69
+ * upstream. Twin of the Rust `query_create_invite`.
70
+ *
71
+ * @param deps - query dependencies (must be the caller's transaction)
72
+ * @param input - the invite fields and its issuing actor
27
73
  * @returns the created invite
28
74
  * @mutates `invite` table - inserts the new row
75
+ * @throws InviteIssuerNotActiveError if `created_by` names no actor, a tombstoned one, or one whose account is soft-deleted; nothing is inserted
76
+ * @throws InviteDuplicateError if an unclaimed invite with the same email or username already exists
29
77
  */
30
78
  export declare const query_create_invite: (deps: QueryDeps, input: CreateInviteInput) => Promise<Invite>;
31
79
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"invite_queries.d.ts","sourceRoot":"","sources":["../../src/lib/auth/invite_queries.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,KA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10
+ import { is_pg_unique_violation } from '../db/pg_error.js';
10
11
  import { columns_sql, iso8601_timestamp_expr, qualify_columns } from '../db/sql_columns.js';
12
+ import { query_account_lock_live_many } from './account_queries.js';
11
13
  /**
12
14
  * The full `invite` column set, named explicitly so a row read fails loud on
13
15
  * schema drift — invite rows ride the `invite_create` / `invite_list` RPC
@@ -29,17 +31,90 @@ export const INVITE_COLUMNS = [
29
31
  /** The `invite` timestamp override, by row qualifier (`''` bare, `i` for the username JOIN). */
30
32
  const invite_expr = iso8601_timestamp_expr(INVITE_COLUMNS, ['claimed_at', 'created_at']);
31
33
  /**
32
- * Create a new invite.
34
+ * Error thrown by `query_create_invite` when an unclaimed invite already
35
+ * reserves the email or the username: the partial unique index
36
+ * `idx_invite_email_unclaimed` or `idx_invite_username_unclaimed` refused the
37
+ * insert (SQLSTATE `23505`), which aborts the caller's transaction. Twin of
38
+ * the Rust `InviteCreateError::Duplicate`.
39
+ */
40
+ export class InviteDuplicateError extends Error {
41
+ constructor() {
42
+ super('an unclaimed invite already exists');
43
+ this.name = 'InviteDuplicateError';
44
+ }
45
+ }
46
+ /**
47
+ * Error thrown by `query_create_invite` when the issuer is not an active
48
+ * account: its actor is missing or tombstoned, or its account soft-deleted — a
49
+ * delete that committed while the invite was in flight. Twin of the Rust
50
+ * `InviteCreateError::IssuerNotActive`.
51
+ */
52
+ export class InviteIssuerNotActiveError extends Error {
53
+ constructor() {
54
+ super('the issuer is not an active account');
55
+ this.name = 'InviteIssuerNotActiveError';
56
+ }
57
+ }
58
+ /**
59
+ * Create an invite issued by the actor `input.created_by`.
33
60
  *
34
- * @param deps - query dependencies
35
- * @param input - the invite fields
61
+ * The issuer must be an active actor of a live account, checked here so every
62
+ * caller — the spine's `invite_create` handler and a consumer minting invites
63
+ * on its own path — gets the rule: an invite records no authority, so one
64
+ * minted for an issuer deleted mid-flight would outlive the delete that should
65
+ * have voided it. The issuer's account row is read under `FOR SHARE`
66
+ * (`query_account_lock_live_many`) before the insert: an account delete writes
67
+ * its tombstone first, so a delete that wrote its tombstone before the read is
68
+ * waited for if uncommitted and seen (the invite is refused), and one that
69
+ * reaches the row after it waits for this transaction to
70
+ * commit and then deletes the invite with the account's other unclaimed ones
71
+ * (`query_invite_delete_unclaimed_by_creators`). The actor's tombstone is read
72
+ * after the lock, so a delete waited on is seen there too.
73
+ *
74
+ * Run it inside the caller's transaction, which the lock spans — on the pool
75
+ * the lock ends with its statement and orders nothing. Lock order: account
76
+ * rows come before actor and `role_grant` rows, as the last-admin guard
77
+ * (`query_count_active_accounts_with_global_role_locked`) takes them (account,
78
+ * then actor, then grant). So take it before the transaction locks any actor
79
+ * or `role_grant` row — or lock the issuer's account row first yourself
80
+ * (`query_account_lock_live_many`), which this query then re-takes without
81
+ * waiting: a transaction holding a lock on the issuer's admin grant while it
82
+ * waits here on an account row a removal holds, while the removal's guard
83
+ * waits on that grant, deadlocks.
84
+ *
85
+ * Callers that pre-check for account collisions on `email` / `username` do so
86
+ * upstream. Twin of the Rust `query_create_invite`.
87
+ *
88
+ * @param deps - query dependencies (must be the caller's transaction)
89
+ * @param input - the invite fields and its issuing actor
36
90
  * @returns the created invite
37
91
  * @mutates `invite` table - inserts the new row
92
+ * @throws InviteIssuerNotActiveError if `created_by` names no actor, a tombstoned one, or one whose account is soft-deleted; nothing is inserted
93
+ * @throws InviteDuplicateError if an unclaimed invite with the same email or username already exists
38
94
  */
39
95
  export const query_create_invite = async (deps, input) => {
40
- const row = await deps.db.query_one(`INSERT INTO invite (email, username, created_by)
41
- VALUES ($1, $2, $3)
42
- RETURNING ${columns_sql(INVITE_COLUMNS, invite_expr(''))}`, [input.email ?? null, input.username ?? null, input.created_by]);
96
+ // An actor's account never changes, so it is read unlocked.
97
+ const issuer = await deps.db.query_one(`SELECT account_id FROM actor WHERE id = $1`, [input.created_by]);
98
+ if (!issuer ||
99
+ !(await query_account_lock_live_many(deps, [issuer.account_id])).has(issuer.account_id)) {
100
+ throw new InviteIssuerNotActiveError();
101
+ }
102
+ // After the lock, so a delete it waited on is seen.
103
+ const issuer_actor = await deps.db.query_one(`SELECT id FROM actor WHERE id = $1 AND deleted_at IS NULL`, [input.created_by]);
104
+ if (!issuer_actor) {
105
+ throw new InviteIssuerNotActiveError();
106
+ }
107
+ let row;
108
+ try {
109
+ row = await deps.db.query_one(`INSERT INTO invite (email, username, created_by)
110
+ VALUES ($1, $2, $3)
111
+ RETURNING ${columns_sql(INVITE_COLUMNS, invite_expr(''))}`, [input.email ?? null, input.username ?? null, input.created_by]);
112
+ }
113
+ catch (err) {
114
+ if (is_pg_unique_violation(err))
115
+ throw new InviteDuplicateError();
116
+ throw err;
117
+ }
43
118
  return assert_row(row, 'INSERT INTO invite');
44
119
  };
45
120
  /**
@@ -52,6 +52,7 @@ export type InviteWithUsernamesJson = Infer<typeof InviteWithUsernamesJson>;
52
52
  export interface CreateInviteInput {
53
53
  email?: Email | null;
54
54
  username?: Username | null;
55
- created_by: Uuid | null;
55
+ /** The issuing actor, which must be active on a live account (`query_create_invite`). */
56
+ created_by: Uuid;
56
57
  }
57
58
  //# sourceMappingURL=invite_schema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"invite_schema.d.ts","sourceRoot":"","sources":["../../src/lib/auth/invite_schema.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAA4B,KAAK,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAEpF,OAAO,EAAE,KAAK,EAAgC,IAAI,EAAE,KAAK,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AAEnG,oCAAoC;AACpC,MAAM,WAAW,MAAM;IACtB,EAAE,EAAE,IAAI,CAAC;IACT,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAC;IAC1B,UAAU,EAAE,IAAI,GAAG,IAAI,CAAC;IACxB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,E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C;AACH,MAAM,MAAM,uBAAuB,GAAG,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE5E,oCAAoC;AACpC,MAAM,WAAW,iBAAiB;IACjC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;IACrB,QAAQ,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAC;IAC3B,yFAAyF;IACzF,UAAU,EAAE,IAAI,CAAC;CACjB"}
@@ -6,7 +6,6 @@
6
6
  *
7
7
  * @module
8
8
  */
9
- import type { SessionOptions } from './session_cookie.js';
10
9
  import type { AppDeps } from './deps.js';
11
10
  import type { DaemonTokenState } from './daemon_token.js';
12
11
  import type { MiddlewareCarveOut, MiddlewareSpec } from '../http/middleware_spec.js';
@@ -144,7 +143,7 @@ export declare const assert_middleware_stack_mounted: (stack_specs: ReadonlyArra
144
143
  */
145
144
  export interface AuthMiddlewareOptions {
146
145
  allowed_origins: Array<RegExp>;
147
- session_options: SessionOptions<string>;
146
+ session_cookie_name: string;
148
147
  /**
149
148
  * Path pattern for the session, request-context, bearer, and daemon-token
150
149
  * layers (default: `AUTH_MIDDLEWARE_PATH`, `'/api/*'`). The origin check
@@ -177,7 +176,7 @@ export interface AuthMiddlewareOptions {
177
176
  * non-standard paths (e.g., tx's `/tx` binary endpoint).
178
177
  *
179
178
  * @param deps - stateless capabilities (keyring, db)
180
- * @param options - middleware configuration (allowed_origins, session_options, path, daemon_token_state, origin_carve_outs)
179
+ * @param options - middleware configuration (allowed_origins, session_cookie_name, path, daemon_token_state, origin_carve_outs)
181
180
  * @throws Error when an origin carve-out has no path or no reason, or overlaps `path`
182
181
  * @returns the middleware spec array
183
182
  */
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/lib/auth/middleware.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,KAAK,EAAE,OAAO,EAAE,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,CACkD,CAAC;AAEtF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,eAAO,MAAM,+BAA+B,GAC3C,aAAa,aAAa,CAAC,cAAc,CAAC,EAC1C,kBAAkB,aAAa,CAAC,cAAc,CAAC,KAC7C,IA+BF,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACrC,eAAe,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC/B,mBAAmB,EAAE,MAAM,CAAC;IAC5B;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;;;OAMG;IACH,iBAAiB,CAAC,EAAE,aAAa,CAAC,kBAAkB,CAAC,CAAC;IACtD,mFAAmF;IACnF,kBAAkB,CAAC,EAAE,gBAAgB,CAAC;CACtC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,4BAA4B,GACxC,MAAM,OAAO,EACb,SAAS,qBAAqB,KAC5B,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAqF/B,CAAC"}
@@ -224,13 +224,13 @@ export const assert_middleware_stack_mounted = (stack_specs, middleware_specs) =
224
224
  * non-standard paths (e.g., tx's `/tx` binary endpoint).
225
225
  *
226
226
  * @param deps - stateless capabilities (keyring, db)
227
- * @param options - middleware configuration (allowed_origins, session_options, path, daemon_token_state, origin_carve_outs)
227
+ * @param options - middleware configuration (allowed_origins, session_cookie_name, path, daemon_token_state, origin_carve_outs)
228
228
  * @throws Error when an origin carve-out has no path or no reason, or overlaps `path`
229
229
  * @returns the middleware spec array
230
230
  */
231
231
  export const create_auth_middleware_specs = async (deps, options) => {
232
232
  const { keyring, db } = deps;
233
- const { allowed_origins, session_options, path = AUTH_MIDDLEWARE_PATH, daemon_token_state } = options;
233
+ const { allowed_origins, session_cookie_name, path = AUTH_MIDDLEWARE_PATH, daemon_token_state } = options;
234
234
  // copied before it is checked, so what is checked is what runs
235
235
  const origin_carve_outs = freeze_origin_carve_outs(options.origin_carve_outs ?? []);
236
236
  assert_origin_carve_outs_valid(origin_carve_outs, path);
@@ -246,7 +246,7 @@ export const create_auth_middleware_specs = async (deps, options) => {
246
246
  import('./request_context.js'),
247
247
  import('./bearer_auth.js')
248
248
  ]);
249
- const session_middleware = create_session_middleware(keyring, session_options);
249
+ const session_middleware = create_session_middleware(keyring, session_cookie_name);
250
250
  // the first credential leg clears the resolved credential: the daemon
251
251
  // token's when it is composed, else this one
252
252
  const request_context_middleware = create_request_context_middleware(query_deps, {
@@ -489,9 +489,9 @@ export type AuthorizationResult = {
489
489
  * Pure data — the function does not touch a Hono context. Each transport
490
490
  * passes the account its credential resolved (`ResolvedAuth.account`) and
491
491
  * binds the returned `AuthorizationResult` to its wire shape. The REST
492
- * pipeline additionally writes `REQUEST_CONTEXT_KEY` on `c` for downstream
493
- * `require_role` / `require_credential_types` middleware that still reads
494
- * the resolved context off the Hono context.
492
+ * pipeline additionally writes `REQUEST_CONTEXT_KEY` on `c` for the
493
+ * downstream `require_role` middleware, which reads the resolved context off
494
+ * the Hono context.
495
495
  *
496
496
  * The account is never read here: the context is built on the credential
497
497
  * leg's row, so a request reads its account once, as on the Rust spine,
@@ -499,9 +499,9 @@ export const build_request_context = async (deps, account, actor) => {
499
499
  * Pure data — the function does not touch a Hono context. Each transport
500
500
  * passes the account its credential resolved (`ResolvedAuth.account`) and
501
501
  * binds the returned `AuthorizationResult` to its wire shape. The REST
502
- * pipeline additionally writes `REQUEST_CONTEXT_KEY` on `c` for downstream
503
- * `require_role` / `require_credential_types` middleware that still reads
504
- * the resolved context off the Hono context.
502
+ * pipeline additionally writes `REQUEST_CONTEXT_KEY` on `c` for the
503
+ * downstream `require_role` middleware, which reads the resolved context off
504
+ * the Hono context.
505
505
  *
506
506
  * The account is never read here: the context is built on the credential
507
507
  * leg's row, so a request reads its account once, as on the Rust spine,
@@ -4,7 +4,7 @@
4
4
  * The offer-lifecycle methods (create / accept / decline / retract / list /
5
5
  * history) plus `role_grant_revoke` and `role_grant_assign` (both
6
6
  * admin-only). All mount
7
- * on a consumer's JSON-RPC endpoint via `create_rpc_endpoint`. The action
7
+ * on a consumer's JSON-RPC endpoint through `rpc_endpoints`. The action
8
8
  * specs themselves live in `auth/role_grant_offer_action_specs.ts`. Mutations
9
9
  * declare `side_effects: true` so the RPC dispatcher wraps the handler in
10
10
  * a DB transaction; `role_grant_offer_list` and `role_grant_offer_history` declare
@@ -139,7 +139,7 @@ export interface RoleGrantOfferActionOptions {
139
139
  *
140
140
  * @param deps - `RevokingActionFactoryDeps`: `log`, `audit`, and the `connection_closer` a revoked global grant closes the revokee's role-gated connections through. WS notifications go through the handler context's `notification_sender`, not a dep
141
141
  * @param options - role registry, default TTL, authorization override
142
- * @returns the `RpcAction` array to spread into a `create_rpc_endpoint` call
142
+ * @returns the `RpcAction` array to spread into an `RpcEndpointSpec`'s `actions`
143
143
  */
144
144
  export declare const create_role_grant_offer_actions: (deps: RevokingActionFactoryDeps, options?: RoleGrantOfferActionOptions) => Array<RpcAction>;
145
145
  //# sourceMappingURL=role_grant_offer_actions.d.ts.map
@@ -4,7 +4,7 @@
4
4
  * The offer-lifecycle methods (create / accept / decline / retract / list /
5
5
  * history) plus `role_grant_revoke` and `role_grant_assign` (both
6
6
  * admin-only). All mount
7
- * on a consumer's JSON-RPC endpoint via `create_rpc_endpoint`. The action
7
+ * on a consumer's JSON-RPC endpoint through `rpc_endpoints`. The action
8
8
  * specs themselves live in `auth/role_grant_offer_action_specs.ts`. Mutations
9
9
  * declare `side_effects: true` so the RPC dispatcher wraps the handler in
10
10
  * a DB transaction; `role_grant_offer_list` and `role_grant_offer_history` declare
@@ -141,7 +141,7 @@ const default_authorize = async (auth, _input, _deps, _ctx) => {
141
141
  *
142
142
  * @param deps - `RevokingActionFactoryDeps`: `log`, `audit`, and the `connection_closer` a revoked global grant closes the revokee's role-gated connections through. WS notifications go through the handler context's `notification_sender`, not a dep
143
143
  * @param options - role registry, default TTL, authorization override
144
- * @returns the `RpcAction` array to spread into a `create_rpc_endpoint` call
144
+ * @returns the `RpcAction` array to spread into an `RpcEndpointSpec`'s `actions`
145
145
  */
146
146
  export const create_role_grant_offer_actions = (deps, options = {}) => {
147
147
  const { log, audit, connection_closer } = deps;
@@ -1,20 +1,27 @@
1
1
  /**
2
- * Generic session management for cookie-based auth.
2
+ * The signed session cookie: its name, value format, verification, and the
3
+ * exact `Set-Cookie` strings that set and clear it — byte for byte the Rust
4
+ * spine's `fuz_auth::session_cookie`.
3
5
  *
4
- * Parameterized on identity type via `SessionOptions<TIdentity>`.
5
- * Handles signing, expiration, and key rotation. Apps provide
6
- * encode/decode for their specific identity format.
7
- *
8
- * Cookie value format: `${encode(identity)}:${expires_at}` (signed with HMAC-SHA256).
6
+ * Cookie value: `keyring.sign("${session_token}:${expires_at}")`. The signed
7
+ * payload carries the raw session token (whose blake3 hash is the
8
+ * `auth_session` row id) and its expiry, so one HMAC covers both the token
9
+ * bind and the freshness check.
9
10
  *
10
11
  * @module
11
12
  */
12
13
  import type { Keyring } from './keyring.js';
14
+ /**
15
+ * The default session cookie name — the Rust spine's `SESSION_COOKIE_NAME`,
16
+ * so a cookie either spine mints reads on both. An app with its own name
17
+ * passes it as `session_cookie_name` wherever the spine takes one.
18
+ */
19
+ export declare const SESSION_COOKIE_NAME = "fuz_session";
13
20
  /**
14
21
  * Cookie max age in seconds (30 days — aligned with AUTH_SESSION_LIFETIME_MS).
15
22
  *
16
23
  * This is an **absolute** session lifetime: the cookie is signed once at
17
- * mint (`expires_at = mint + max_age`) and never re-signed, matching the DB
24
+ * mint (`expires_at = mint + SESSION_AGE_MAX`) and never re-signed, matching the DB
18
25
  * row's fixed `expires_at`. There is deliberately no sliding renewal on
19
26
  * either leg — see `docs/security.md` §Session Security.
20
27
  */
@@ -49,9 +56,8 @@ export declare const is_cookie_name: (name: string) => boolean;
49
56
  * what a reader that decodes nothing needs. Twin of the Rust spine's
50
57
  * `is_cookie_value`.
51
58
  *
52
- * Anything but a string is refused, whatever its type says: the value comes
53
- * out of a consumer's `encode_identity` and keyring, and a regex alone would
54
- * read `undefined` as the text `undefined`.
59
+ * Anything but a string is refused, whatever its type says: a regex alone
60
+ * would read `undefined` as the text `undefined`.
55
61
  *
56
62
  * @param value - a cookie value
57
63
  */
@@ -67,7 +73,7 @@ export declare const is_cookie_value: (value: string) => boolean;
67
73
  * @param value - the signed cookie value (`create_session_cookie_value`)
68
74
  * @param max_age - the cookie's lifetime in seconds
69
75
  * @returns the `Set-Cookie` header value
70
- * @throws Error when the name is not an RFC 6265 `cookie-name`, the value holds anything but `cookie-octet`s (an `encode_identity` that emits a space, a `;`, or a non-ASCII character), or `max_age` is not a non-negative safe integer — a header that would read back differently is never written
76
+ * @throws Error when the name is not an RFC 6265 `cookie-name`, the value holds anything but `cookie-octet`s (a space, a `;`, a non-ASCII character), or `max_age` is not a non-negative safe integer — a header that would read back differently is never written
71
77
  */
72
78
  export declare const format_session_set_cookie: (cookie_name: string, value: string, max_age: number) => string;
73
79
  /**
@@ -81,181 +87,83 @@ export declare const format_session_set_cookie: (cookie_name: string, value: str
81
87
  */
82
88
  export declare const format_session_clear_cookie: (cookie_name: string) => string;
83
89
  /**
84
- * Configuration for a session cookie format.
85
- *
86
- * Apps provide encode/decode to control the identity portion
87
- * of the cookie payload.
88
- *
89
- * The `TIdentity` type parameter determines the trust model:
90
- * - `string` (e.g. a session_id) — the cookie references a server-side session record,
91
- * enabling per-session revocation and metadata. Use when you need admin controls
92
- * like "revoke all sessions" or per-session audit trails.
93
- * - `number` (e.g. an account_id) — the cookie directly encodes the user identity,
94
- * requiring no server-side session state. Simpler, but individual sessions
95
- * can only be invalidated by rotating the signing key (which invalidates all sessions).
96
- *
97
- * The cookie helpers here take either. The spine's session middleware and
98
- * auth chain take `SessionOptions<string>` — the identity is the session token
99
- * the session leg hashes to find its `auth_session` row (`create_session_config`).
100
- *
101
- * @example
102
- * ```ts
103
- * // zap: 3-part format (admin:session_id)
104
- * const zap_config: SessionOptions<string> = {
105
- * cookie_name: 'zap_session',
106
- * encode_identity: (session_id) => `admin:${session_id}`,
107
- * decode_identity: (payload) => {
108
- * const parts = payload.split(':');
109
- * if (parts.length !== 2 || parts[0] !== 'admin') return null;
110
- * return parts[1] || null;
111
- * },
112
- * };
113
- *
114
- * // visiones: 1-part format (account_id)
115
- * const visiones_config: SessionOptions<number> = {
116
- * cookie_name: 'session_id',
117
- * encode_identity: (id) => String(id),
118
- * decode_identity: (payload) => {
119
- * const n = parseInt(payload, 10);
120
- * return Number.isFinite(n) && n > 0 ? n : null;
121
- * },
122
- * };
123
- * ```
124
- */
125
- export interface SessionOptions<TIdentity> {
126
- cookie_name: string;
127
- /**
128
- * Cookie lifetime in seconds. Single source of truth for both the embedded
129
- * `expires_at` (via `create_session_cookie_value`) and the cookie's HTTP
130
- * `Max-Age` attribute (via `session_cookie_header`). Defaults to
131
- * `SESSION_AGE_MAX` (30 days). Every other attribute is fixed
132
- * (`SESSION_COOKIE_ATTRIBUTES`).
133
- */
134
- max_age?: number;
135
- /**
136
- * Encode identity into the cookie payload (before the `:expires_at` suffix).
137
- * The payload goes into the cookie as written, so it must hold only RFC
138
- * 6265 `cookie-octet`s (`is_cookie_value`) — `session_cookie_header` throws
139
- * on anything else.
140
- */
141
- encode_identity: (identity: TIdentity) => string;
142
- /** Decode identity from cookie payload. Return null if invalid. */
143
- decode_identity: (payload: string) => TIdentity | null;
144
- }
145
- /**
146
- * The `Set-Cookie` value that sets the session cookie to `value`, a signed
147
- * cookie value (`create_session_cookie_value`, or the `cookie_value` of
148
- * `create_capped_session`) — what a success carries
149
- * (`route_ok(value, {set_cookie: [...]})`). `options.max_age` sets `Max-Age`,
150
- * as it set the value's embedded expiry; `SESSION_AGE_MAX` when unset. Twin of
151
- * the Rust `session_cookie_header`, which signs the token itself.
90
+ * Sign a session token into a session cookie value: the keyring's signature
91
+ * of `${session_token}:${expires_at}`, the expiry `SESSION_AGE_MAX` from now —
92
+ * what a `Cookie` header carries after `<name>=`. Signed once, at mint: the
93
+ * lifetime is absolute, never renewed (`docs/security.md` §Session Security).
94
+ * Twin of the Rust `create_session_cookie_value`.
152
95
  *
153
- * @param value - the signed cookie value
154
- * @param options - the cookie name and lifetime
155
- * @returns the `Set-Cookie` header value (`format_session_set_cookie`)
156
- * @throws Error when the cookie name or the value is outside the RFC 6265 grammar
96
+ * @param keyring - key ring for signing
97
+ * @param session_token - the raw session token
98
+ * @param now_seconds - current time in seconds (for testing)
99
+ * @returns the signed cookie value
157
100
  */
158
- export declare const session_cookie_header: <T>(value: string, options: SessionOptions<T>) => string;
101
+ export declare const create_session_cookie_value: (keyring: Keyring, session_token: string, now_seconds?: number) => Promise<string>;
159
102
  /**
160
- * The `Set-Cookie` value that clears the session cookie — what a logout or a
161
- * password change carries on its success. Twin of the Rust
162
- * `clear_session_cookie_header`.
103
+ * Sign a session token and build the `Set-Cookie` header value that sets it
104
+ * under `cookie_name` — `format_session_set_cookie` over
105
+ * `create_session_cookie_value`, with `SESSION_AGE_MAX` as its lifetime. What
106
+ * a success that mints a session carries (`route_ok(value, {set_cookie})`).
107
+ * Twin of the Rust `sign_session_cookie`.
163
108
  *
164
- * @param options - the cookie name
165
- * @returns the `Set-Cookie` header value (`format_session_clear_cookie`)
166
- * @throws Error when the cookie name is outside the RFC 6265 grammar
109
+ * @param keyring - key ring for signing
110
+ * @param session_token - the raw session token (`create_capped_session`'s `token`)
111
+ * @param cookie_name - the session cookie's name
112
+ * @returns the `Set-Cookie` header value
113
+ * @throws Error when the cookie name, or the signed value, is outside the RFC 6265 grammar
167
114
  */
168
- export declare const clear_session_cookie_header: <T>(options: SessionOptions<T>) => string;
115
+ export declare const sign_session_cookie: (keyring: Keyring, session_token: string, cookie_name: string) => Promise<string>;
169
116
  /**
170
- * Result of parsing a signed session cookie.
117
+ * A verified, unexpired session cookie: the raw session token it carries and
118
+ * the expiry signed beside it. Twin of the Rust `SessionCookie`.
171
119
  */
172
- export interface ParsedSession<TIdentity> {
173
- /** The decoded identity. */
174
- identity: TIdentity;
175
- /** Index of the key that verified the signature (0 = primary). */
176
- key_index: number;
120
+ export interface SessionCookie {
121
+ /** The raw session token — its blake3 hash is the `auth_session` row id. */
122
+ token: string;
177
123
  /** The signed expiry, in seconds since the Unix epoch — later than the parse. */
178
124
  expires_at: number;
179
125
  }
180
126
  /**
181
- * Parse a signed session cookie value.
182
- *
183
- * The signed value format is `${encode(identity)}:${expires_at}`.
184
- * Tries all keys in order to support key rotation.
185
- *
186
- * An empty string is a cookie that was sent with no value (`name=`): present
187
- * and invalid, like any other value that fails verification. Only a nullish
188
- * `signed_value` is "no cookie".
189
- *
190
- * @param signed_value - the raw cookie value (signed), nullish when the request carries no session cookie
127
+ * How a request's `Cookie` lines read for the session cookie — what the
128
+ * cookie alone shows, before any database work. Twin of the Rust
129
+ * `SessionCookieRead`.
130
+ *
131
+ * - `absent` — no pair names the cookie
132
+ * - `invalid` — a pair names it and its value fails verification: unreadable,
133
+ * a bad MAC, a signed expiry that has passed, an empty token, an empty
134
+ * value. What the session middleware clears.
135
+ * - `valid` — it verifies under the keyring and its signed expiry is ahead.
136
+ * Says nothing about the `auth_session` row.
137
+ */
138
+ export type SessionCookieRead = {
139
+ kind: 'absent';
140
+ } | {
141
+ kind: 'invalid';
142
+ } | {
143
+ kind: 'valid';
144
+ cookie: SessionCookie;
145
+ };
146
+ /**
147
+ * Read the session cookie out of a request's `Cookie` lines: the
148
+ * `cookie_name` pair (`read_session_cookie_value` — the first, verbatim),
149
+ * verified under the keyring, its signed expiry checked, its session token
150
+ * answered with that expiry.
151
+ *
152
+ * The three answers are what the cookie alone shows (`SessionCookieRead`). An
153
+ * empty value (`name=`) is a cookie sent with none: present and invalid. A
154
+ * valid cookie whose `auth_session` row is gone is still valid here; nothing
155
+ * clears it. There is deliberately no refresh: a re-sign would compute a
156
+ * fresh expiry past the row's fixed `expires_at`, so the lifetime is the one
157
+ * signing at mint, on both spines.
158
+ *
159
+ * Twin of the Rust `parse_session_from_cookies`, which takes the request's
160
+ * headers.
161
+ *
162
+ * @param cookie_lines - the request's `Cookie` header lines
191
163
  * @param keyring - key ring for verification
192
- * @param options - session configuration with decode logic
193
- * @param now_seconds - current time in seconds (for testing)
194
- * @returns `ParsedSession` if valid, null if invalid/expired (the empty string included), undefined if missing
195
- */
196
- export declare const parse_session: <TIdentity>(signed_value: string | null | undefined, keyring: Keyring, options: SessionOptions<TIdentity>, now_seconds?: number) => Promise<ParsedSession<TIdentity> | null | undefined>;
197
- /**
198
- * Create a signed session cookie value.
199
- *
200
- * Format: `${encode(identity)}:${expires_at}` signed with HMAC-SHA256.
201
- * Embeds expiration in the signed value itself for defense-in-depth.
202
- *
203
- * @param keyring - key ring for signing
204
- * @param identity - the identity to encode
205
- * @param options - session configuration with encode logic
206
- * @param now_seconds - current time in seconds (for testing)
207
- * @returns signed cookie value string
208
- */
209
- export declare const create_session_cookie_value: <TIdentity>(keyring: Keyring, identity: TIdentity, options: SessionOptions<TIdentity>, now_seconds?: number) => Promise<string>;
210
- /**
211
- * Result of processing a session cookie.
212
- */
213
- export interface ProcessSessionResult<TIdentity> {
214
- /** Whether the session is valid. */
215
- valid: boolean;
216
- /** Action the adapter should take. */
217
- action: 'none' | 'clear';
218
- /** The decoded identity if the cookie was valid. */
219
- identity?: TIdentity;
220
- /** The cookie's signed expiry, epoch seconds, if the cookie was valid. */
221
- expires_at?: number;
222
- }
223
- /**
224
- * Create a session config for raw session token identity.
225
- *
226
- * The standard pattern: cookie stores the raw session token,
227
- * server hashes it (blake3) to look up the `auth_session` row.
228
- * Only the `cookie_name` varies per app.
229
- *
230
- * @param cookie_name - cookie name (e.g. `'zap_session'`, `'visiones_session'`)
231
- * @returns a `SessionOptions<string>` ready for use with session middleware
232
- */
233
- export declare const create_session_config: (cookie_name: string) => SessionOptions<string>;
234
- /** Canonical session config for fuz_app auth. */
235
- export declare const fuz_session_config: SessionOptions<string>;
236
- /**
237
- * Process a session cookie and determine what action to take.
238
- *
239
- * There is deliberately no refresh action: any re-sign computes a fresh
240
- * `now + max_age` expiry, extending the cookie past the DB row's fixed
241
- * `expires_at` — the holder would go silently anonymous while carrying a
242
- * self-perpetuating cookie. The absolute lifetime is one signing at mint,
243
- * on both legs, matching the Rust spine. A cookie signed by a retired
244
- * (non-primary) key keeps verifying until the key leaves the keyring;
245
- * retired keys are safe to drop after `SESSION_AGE_MAX` (see
246
- * `docs/security.md` §Cookie Key Rotation).
247
- *
248
- * The three outcomes are what the cookie alone shows: no cookie (`action:
249
- * 'none'`), a cookie that fails verification — unreadable, a bad MAC, a signed
250
- * expiry that has passed, an empty identity, an empty value — (`action:
251
- * 'clear'`), and a valid one. A valid cookie whose `auth_session` row is gone
252
- * is still valid here; nothing clears it.
253
- *
254
- * @param signed_value - the raw cookie value, nullish when the request carries no session cookie
255
- * @param keyring - key ring for verification and signing
256
- * @param options - session configuration
164
+ * @param cookie_name - the session cookie's name
257
165
  * @param now_seconds - current time in seconds (for testing)
258
- * @returns result with validity and action to take
166
+ * @returns the cookie's reading
259
167
  */
260
- export declare const process_session_cookie: <TIdentity>(signed_value: string | null | undefined, keyring: Keyring, options: SessionOptions<TIdentity>, now_seconds?: number) => Promise<ProcessSessionResult<TIdentity>>;
168
+ export declare const parse_session_from_cookies: (cookie_lines: ReadonlyArray<string>, keyring: Keyring, cookie_name: string, now_seconds?: number) => Promise<SessionCookieRead>;
261
169
  //# sourceMappingURL=session_cookie.d.ts.map