@fuzdev/fuz_app 0.182.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.
Files changed (295) hide show
  1. package/README.md +2 -0
  2. package/dist/actions/CLAUDE.md +535 -1223
  3. package/dist/actions/action_codegen.d.ts +1 -1
  4. package/dist/actions/action_codegen.d.ts.map +1 -1
  5. package/dist/actions/action_codegen.js +3 -2
  6. package/dist/actions/connection_closer.d.ts +11 -9
  7. package/dist/actions/connection_closer.d.ts.map +1 -1
  8. package/dist/actions/connection_closer.js +11 -9
  9. package/dist/actions/frontend_rpc_client.d.ts +1 -1
  10. package/dist/actions/register_action_ws.d.ts +14 -1
  11. package/dist/actions/register_action_ws.d.ts.map +1 -1
  12. package/dist/actions/register_action_ws.js +2 -1
  13. package/dist/actions/ws_socket_session.d.ts +9 -1
  14. package/dist/actions/ws_socket_session.d.ts.map +1 -1
  15. package/dist/actions/ws_socket_session.js +43 -19
  16. package/dist/auth/CLAUDE.md +430 -1024
  17. package/dist/auth/account_queries.d.ts +1 -1
  18. package/dist/auth/account_queries.js +1 -1
  19. package/dist/auth/account_routes.d.ts +9 -8
  20. package/dist/auth/account_routes.d.ts.map +1 -1
  21. package/dist/auth/account_routes.js +78 -75
  22. package/dist/auth/admin_actions.d.ts.map +1 -1
  23. package/dist/auth/admin_actions.js +32 -28
  24. package/dist/auth/api_token.d.ts +20 -3
  25. package/dist/auth/api_token.d.ts.map +1 -1
  26. package/dist/auth/api_token.js +28 -9
  27. package/dist/auth/api_token_queries.d.ts +2 -2
  28. package/dist/auth/api_token_queries.js +2 -2
  29. package/dist/auth/audit_emitter.d.ts +75 -84
  30. package/dist/auth/audit_emitter.d.ts.map +1 -1
  31. package/dist/auth/audit_emitter.js +83 -71
  32. package/dist/auth/audit_log_schema.d.ts +25 -14
  33. package/dist/auth/audit_log_schema.d.ts.map +1 -1
  34. package/dist/auth/audit_log_schema.js +23 -8
  35. package/dist/auth/auth_guard_resolver.d.ts +1 -1
  36. package/dist/auth/auth_guard_resolver.js +1 -1
  37. package/dist/auth/bearer_auth.d.ts +2 -5
  38. package/dist/auth/bearer_auth.d.ts.map +1 -1
  39. package/dist/auth/bearer_auth.js +4 -13
  40. package/dist/auth/bootstrap_account.d.ts +42 -26
  41. package/dist/auth/bootstrap_account.d.ts.map +1 -1
  42. package/dist/auth/bootstrap_account.js +36 -19
  43. package/dist/auth/bootstrap_routes.d.ts +20 -21
  44. package/dist/auth/bootstrap_routes.d.ts.map +1 -1
  45. package/dist/auth/bootstrap_routes.js +26 -31
  46. package/dist/auth/bootstrap_token_store.d.ts +3 -4
  47. package/dist/auth/bootstrap_token_store.d.ts.map +1 -1
  48. package/dist/auth/bootstrap_token_store.js +1 -1
  49. package/dist/auth/daemon_token.d.ts +5 -1
  50. package/dist/auth/daemon_token.d.ts.map +1 -1
  51. package/dist/auth/daemon_token_middleware.d.ts +12 -8
  52. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  53. package/dist/auth/daemon_token_middleware.js +15 -21
  54. package/dist/auth/deps.d.ts +19 -5
  55. package/dist/auth/deps.d.ts.map +1 -1
  56. package/dist/auth/deps.js +3 -2
  57. package/dist/auth/invite_queries.d.ts +51 -3
  58. package/dist/auth/invite_queries.d.ts.map +1 -1
  59. package/dist/auth/invite_queries.js +81 -6
  60. package/dist/auth/invite_schema.d.ts +2 -1
  61. package/dist/auth/invite_schema.d.ts.map +1 -1
  62. package/dist/auth/middleware.d.ts +2 -3
  63. package/dist/auth/middleware.d.ts.map +1 -1
  64. package/dist/auth/middleware.js +3 -3
  65. package/dist/auth/request_context.d.ts +3 -3
  66. package/dist/auth/request_context.js +3 -3
  67. package/dist/auth/resolved_auth.d.ts +7 -7
  68. package/dist/auth/resolved_auth.d.ts.map +1 -1
  69. package/dist/auth/resolved_auth.js +4 -4
  70. package/dist/auth/role_grant_offer_actions.d.ts +27 -3
  71. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  72. package/dist/auth/role_grant_offer_actions.js +42 -35
  73. package/dist/auth/role_grant_offer_queries.d.ts +60 -4
  74. package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
  75. package/dist/auth/role_grant_offer_queries.js +79 -5
  76. package/dist/auth/role_grant_queries.d.ts +3 -52
  77. package/dist/auth/role_grant_queries.d.ts.map +1 -1
  78. package/dist/auth/role_grant_queries.js +5 -80
  79. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  80. package/dist/auth/self_service_role_actions.js +11 -22
  81. package/dist/auth/session_cookie.d.ts +82 -174
  82. package/dist/auth/session_cookie.d.ts.map +1 -1
  83. package/dist/auth/session_cookie.js +80 -130
  84. package/dist/auth/session_middleware.d.ts +8 -17
  85. package/dist/auth/session_middleware.d.ts.map +1 -1
  86. package/dist/auth/session_middleware.js +14 -23
  87. package/dist/auth/session_queries.d.ts +15 -17
  88. package/dist/auth/session_queries.d.ts.map +1 -1
  89. package/dist/auth/session_queries.js +28 -20
  90. package/dist/auth/signup_routes.d.ts.map +1 -1
  91. package/dist/auth/signup_routes.js +93 -43
  92. package/dist/db/CLAUDE.md +5 -3
  93. package/dist/db/sql_columns.d.ts +1 -1
  94. package/dist/db/sql_columns.js +1 -1
  95. package/dist/dev/setup.d.ts +3 -1
  96. package/dist/dev/setup.d.ts.map +1 -1
  97. package/dist/dev/setup.js +3 -1
  98. package/dist/http/CLAUDE.md +315 -763
  99. package/dist/http/db_routes.d.ts +8 -23
  100. package/dist/http/db_routes.d.ts.map +1 -1
  101. package/dist/http/db_routes.js +73 -73
  102. package/dist/http/origin.d.ts +7 -8
  103. package/dist/http/origin.d.ts.map +1 -1
  104. package/dist/http/origin.js +7 -8
  105. package/dist/http/pending_effects.d.ts +118 -6
  106. package/dist/http/pending_effects.d.ts.map +1 -1
  107. package/dist/http/pending_effects.js +114 -6
  108. package/dist/http/route_result.d.ts +1 -1
  109. package/dist/http/route_spec.d.ts +14 -11
  110. package/dist/http/route_spec.d.ts.map +1 -1
  111. package/dist/realtime/sse.d.ts +4 -23
  112. package/dist/realtime/sse.d.ts.map +1 -1
  113. package/dist/realtime/sse.js +4 -25
  114. package/dist/server/app_backend.d.ts +4 -5
  115. package/dist/server/app_backend.d.ts.map +1 -1
  116. package/dist/server/app_backend.js +3 -1
  117. package/dist/server/app_server.d.ts +80 -16
  118. package/dist/server/app_server.d.ts.map +1 -1
  119. package/dist/server/app_server.js +113 -31
  120. package/dist/server/app_server_context.d.ts +1 -2
  121. package/dist/server/app_server_context.d.ts.map +1 -1
  122. package/dist/server/env.js +2 -2
  123. package/dist/testing/CLAUDE.md +265 -796
  124. package/dist/testing/admin_integration.d.ts +2 -3
  125. package/dist/testing/admin_integration.d.ts.map +1 -1
  126. package/dist/testing/admin_integration.js +3 -3
  127. package/dist/testing/adversarial_404.d.ts +6 -1
  128. package/dist/testing/adversarial_404.d.ts.map +1 -1
  129. package/dist/testing/adversarial_404.js +38 -17
  130. package/dist/testing/app_server.d.ts +23 -148
  131. package/dist/testing/app_server.d.ts.map +1 -1
  132. package/dist/testing/app_server.js +53 -131
  133. package/dist/testing/attack_surface.d.ts +1 -1
  134. package/dist/testing/attack_surface.d.ts.map +1 -1
  135. package/dist/testing/audit_completeness.d.ts +2 -3
  136. package/dist/testing/audit_completeness.d.ts.map +1 -1
  137. package/dist/testing/audit_completeness.js +1 -1
  138. package/dist/testing/audit_drift_guard.d.ts +11 -6
  139. package/dist/testing/audit_drift_guard.d.ts.map +1 -1
  140. package/dist/testing/audit_drift_guard.js +15 -8
  141. package/dist/testing/cross_backend/audit_cross_helpers.d.ts +12 -9
  142. package/dist/testing/cross_backend/audit_cross_helpers.d.ts.map +1 -1
  143. package/dist/testing/cross_backend/audit_cross_helpers.js +13 -14
  144. package/dist/testing/cross_backend/capabilities.d.ts +9 -6
  145. package/dist/testing/cross_backend/capabilities.d.ts.map +1 -1
  146. package/dist/testing/cross_backend/capabilities.js +11 -7
  147. package/dist/testing/cross_backend/capability_mounts.d.ts +103 -0
  148. package/dist/testing/cross_backend/capability_mounts.d.ts.map +1 -0
  149. package/dist/testing/cross_backend/capability_mounts.js +428 -0
  150. package/dist/testing/cross_backend/cell_moderate.js +2 -2
  151. package/dist/testing/cross_backend/cell_relations.d.ts.map +1 -1
  152. package/dist/testing/cross_backend/cell_relations.js +12 -11
  153. package/dist/testing/cross_backend/conformance_principal.d.ts +54 -0
  154. package/dist/testing/cross_backend/conformance_principal.d.ts.map +1 -0
  155. package/dist/testing/cross_backend/conformance_principal.js +283 -0
  156. package/dist/testing/cross_backend/conformance_table.d.ts +7 -56
  157. package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
  158. package/dist/testing/cross_backend/conformance_table.js +6 -274
  159. package/dist/testing/cross_backend/cookie_attributes.js +1 -1
  160. package/dist/testing/cross_backend/credential_deadline.d.ts.map +1 -1
  161. package/dist/testing/cross_backend/credential_deadline.js +2 -0
  162. package/dist/testing/cross_backend/credential_wire.d.ts +1 -1
  163. package/dist/testing/cross_backend/credential_wire.d.ts.map +1 -1
  164. package/dist/testing/cross_backend/db_state_diff.d.ts +72 -0
  165. package/dist/testing/cross_backend/db_state_diff.d.ts.map +1 -0
  166. package/dist/testing/cross_backend/db_state_diff.js +444 -0
  167. package/dist/testing/cross_backend/declared_skips.d.ts +68 -0
  168. package/dist/testing/cross_backend/declared_skips.d.ts.map +1 -0
  169. package/dist/testing/cross_backend/declared_skips.js +132 -0
  170. package/dist/testing/cross_backend/default_backend_configs.d.ts +3 -3
  171. package/dist/testing/cross_backend/default_spine_surface.d.ts +0 -6
  172. package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
  173. package/dist/testing/cross_backend/default_spine_surface.js +4 -9
  174. package/dist/testing/cross_backend/differential.d.ts +13 -5
  175. package/dist/testing/cross_backend/differential.d.ts.map +1 -1
  176. package/dist/testing/cross_backend/differential.js +35 -11
  177. package/dist/testing/cross_backend/differential_normalize.d.ts +12 -0
  178. package/dist/testing/cross_backend/differential_normalize.d.ts.map +1 -1
  179. package/dist/testing/cross_backend/differential_normalize.js +16 -0
  180. package/dist/testing/cross_backend/full_spine_mount.d.ts +1 -42
  181. package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
  182. package/dist/testing/cross_backend/full_spine_mount.js +45 -4
  183. package/dist/testing/cross_backend/in_process_setup.d.ts +28 -19
  184. package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
  185. package/dist/testing/cross_backend/in_process_setup.js +156 -142
  186. package/dist/testing/cross_backend/login_security.d.ts +12 -0
  187. package/dist/testing/cross_backend/login_security.d.ts.map +1 -1
  188. package/dist/testing/cross_backend/login_security.js +21 -4
  189. package/dist/testing/cross_backend/request_target_conformance_wire.d.ts +0 -2
  190. package/dist/testing/cross_backend/request_target_conformance_wire.d.ts.map +1 -1
  191. package/dist/testing/cross_backend/request_target_conformance_wire.js +3 -1
  192. package/dist/testing/cross_backend/revocation_drivers.d.ts +18 -5
  193. package/dist/testing/cross_backend/revocation_drivers.d.ts.map +1 -1
  194. package/dist/testing/cross_backend/revocation_drivers.js +18 -10
  195. package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts +1 -1
  196. package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts.map +1 -1
  197. package/dist/testing/cross_backend/role_grant_offer_lifecycle.d.ts.map +1 -1
  198. package/dist/testing/cross_backend/role_grant_offer_lifecycle.js +33 -1
  199. package/dist/testing/cross_backend/role_grant_participation.js +4 -4
  200. package/dist/testing/cross_backend/rpc_adversarial.js +1 -1
  201. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts +3 -4
  202. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
  203. package/dist/testing/cross_backend/rust_spine_consumer_surface.js +2 -2
  204. package/dist/testing/cross_backend/session_cap.d.ts +2 -2
  205. package/dist/testing/cross_backend/session_cap.d.ts.map +1 -1
  206. package/dist/testing/cross_backend/session_cap.js +22 -11
  207. package/dist/testing/cross_backend/setup.d.ts +32 -2
  208. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  209. package/dist/testing/cross_backend/setup.js +32 -1
  210. package/dist/testing/cross_backend/spine_suites.d.ts +125 -15
  211. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  212. package/dist/testing/cross_backend/spine_suites.js +459 -62
  213. package/dist/testing/cross_backend/spine_surface_constants.d.ts +1 -1
  214. package/dist/testing/cross_backend/spine_surface_constants.d.ts.map +1 -1
  215. package/dist/testing/cross_backend/spine_surface_constants.js +1 -0
  216. package/dist/testing/cross_backend/sse_round_trip.d.ts +9 -1
  217. package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
  218. package/dist/testing/cross_backend/sse_round_trip.js +123 -27
  219. package/dist/testing/cross_backend/standard.d.ts +2 -3
  220. package/dist/testing/cross_backend/standard.d.ts.map +1 -1
  221. package/dist/testing/cross_backend/standard.js +3 -3
  222. package/dist/testing/cross_backend/testing_backdoor.d.ts.map +1 -1
  223. package/dist/testing/cross_backend/testing_backdoor.js +4 -2
  224. package/dist/testing/cross_backend/testing_credential_read.d.ts +10 -11
  225. package/dist/testing/cross_backend/testing_credential_read.d.ts.map +1 -1
  226. package/dist/testing/cross_backend/testing_credential_read.js +12 -12
  227. package/dist/testing/cross_backend/testing_reset_actions.d.ts +84 -47
  228. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  229. package/dist/testing/cross_backend/testing_reset_actions.js +95 -63
  230. package/dist/testing/cross_backend/testing_server_core.d.ts.map +1 -1
  231. package/dist/testing/cross_backend/testing_server_core.js +2 -3
  232. package/dist/testing/cross_backend/transport_composition.d.ts +1 -1
  233. package/dist/testing/cross_backend/transport_composition.d.ts.map +1 -1
  234. package/dist/testing/cross_backend/transport_composition.js +7 -3
  235. package/dist/testing/cross_backend/ws_round_trip.d.ts +5 -0
  236. package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
  237. package/dist/testing/cross_backend/ws_round_trip.js +54 -8
  238. package/dist/testing/daemon_token_rotation.d.ts.map +1 -1
  239. package/dist/testing/daemon_token_rotation.js +2 -3
  240. package/dist/testing/data_exposure.d.ts.map +1 -1
  241. package/dist/testing/data_exposure.js +3 -0
  242. package/dist/testing/db.d.ts +0 -32
  243. package/dist/testing/db.d.ts.map +1 -1
  244. package/dist/testing/db.js +0 -86
  245. package/dist/testing/db_entities.d.ts +14 -0
  246. package/dist/testing/db_entities.d.ts.map +1 -1
  247. package/dist/testing/db_entities.js +17 -0
  248. package/dist/testing/db_invariant_queries.d.ts +259 -0
  249. package/dist/testing/db_invariant_queries.d.ts.map +1 -0
  250. package/dist/testing/db_invariant_queries.js +400 -0
  251. package/dist/testing/db_invariants.d.ts +106 -0
  252. package/dist/testing/db_invariants.d.ts.map +1 -0
  253. package/dist/testing/db_invariants.gen.sql.d.ts +11 -0
  254. package/dist/testing/db_invariants.gen.sql.d.ts.map +1 -0
  255. package/dist/testing/db_invariants.gen.sql.js +13 -0
  256. package/dist/testing/db_invariants.js +141 -0
  257. package/dist/testing/db_invariants.sql +152 -0
  258. package/dist/testing/describe_db.d.ts +36 -0
  259. package/dist/testing/describe_db.d.ts.map +1 -0
  260. package/dist/testing/describe_db.js +71 -0
  261. package/dist/testing/integration.d.ts +8 -12
  262. package/dist/testing/integration.d.ts.map +1 -1
  263. package/dist/testing/integration.js +13 -17
  264. package/dist/testing/integration_helpers.d.ts +1 -1
  265. package/dist/testing/integration_helpers.d.ts.map +1 -1
  266. package/dist/testing/rate_limiting.d.ts +3 -4
  267. package/dist/testing/rate_limiting.d.ts.map +1 -1
  268. package/dist/testing/rate_limiting.js +6 -5
  269. package/dist/testing/role_grant_helpers.d.ts +1 -1
  270. package/dist/testing/role_grant_helpers.d.ts.map +1 -1
  271. package/dist/testing/round_trip.d.ts.map +1 -1
  272. package/dist/testing/round_trip.js +3 -0
  273. package/dist/testing/rpc_attack_surface.d.ts +8 -6
  274. package/dist/testing/rpc_attack_surface.d.ts.map +1 -1
  275. package/dist/testing/rpc_attack_surface.js +69 -6
  276. package/dist/testing/rpc_helpers.d.ts +1 -2
  277. package/dist/testing/rpc_helpers.d.ts.map +1 -1
  278. package/dist/testing/rpc_helpers.js +3 -3
  279. package/dist/testing/rpc_round_trip.d.ts +3 -4
  280. package/dist/testing/rpc_round_trip.d.ts.map +1 -1
  281. package/dist/testing/rpc_round_trip.js +4 -1
  282. package/dist/testing/sse_round_trip.d.ts +3 -4
  283. package/dist/testing/sse_round_trip.d.ts.map +1 -1
  284. package/dist/testing/sse_round_trip.js +15 -6
  285. package/dist/testing/stubs.d.ts +9 -8
  286. package/dist/testing/stubs.d.ts.map +1 -1
  287. package/dist/testing/stubs.js +20 -13
  288. package/dist/testing/test_app.d.ts +168 -0
  289. package/dist/testing/test_app.d.ts.map +1 -0
  290. package/dist/testing/test_app.js +123 -0
  291. package/dist/testing/ws_round_trip.d.ts.map +1 -1
  292. package/dist/testing/ws_round_trip.js +3 -0
  293. package/dist/ui/ui_format.d.ts.map +1 -1
  294. package/dist/ui/ui_format.js +1 -2
  295. package/package.json +1 -1
@@ -1,24 +1,32 @@
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
  */
13
+ import { read_session_cookie_value } from './credential_headers.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 const SESSION_COOKIE_NAME = 'fuz_session';
12
20
  /**
13
21
  * Cookie max age in seconds (30 days — aligned with AUTH_SESSION_LIFETIME_MS).
14
22
  *
15
23
  * This is an **absolute** session lifetime: the cookie is signed once at
16
- * 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
17
25
  * row's fixed `expires_at`. There is deliberately no sliding renewal on
18
26
  * either leg — see `docs/security.md` §Session Security.
19
27
  */
20
28
  export const SESSION_AGE_MAX = 60 * 60 * 24 * 30;
21
- /** Separator between identity payload and expires_at in signed value. */
29
+ /** Separator between the session token and `expires_at` in the signed value. */
22
30
  const VALUE_SEPARATOR = ':';
23
31
  /** Strict integer matcher for the embedded `expires_at` field. */
24
32
  const EXPIRES_AT_INTEGER_RE = /^\d+$/;
@@ -58,9 +66,8 @@ export const is_cookie_name = (name) => typeof name === 'string' && COOKIE_NAME_
58
66
  * what a reader that decodes nothing needs. Twin of the Rust spine's
59
67
  * `is_cookie_value`.
60
68
  *
61
- * Anything but a string is refused, whatever its type says: the value comes
62
- * out of a consumer's `encode_identity` and keyring, and a regex alone would
63
- * read `undefined` as the text `undefined`.
69
+ * Anything but a string is refused, whatever its type says: a regex alone
70
+ * would read `undefined` as the text `undefined`.
64
71
  *
65
72
  * @param value - a cookie value
66
73
  */
@@ -87,14 +94,14 @@ const assert_cookie_name = (cookie_name) => {
87
94
  * @param value - the signed cookie value (`create_session_cookie_value`)
88
95
  * @param max_age - the cookie's lifetime in seconds
89
96
  * @returns the `Set-Cookie` header value
90
- * @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
97
+ * @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
91
98
  */
92
99
  export const format_session_set_cookie = (cookie_name, value, max_age) => {
93
100
  assert_cookie_name(cookie_name);
94
101
  if (!is_cookie_value(value)) {
95
102
  // the value is a credential: say what is wrong with it, never what it is
96
103
  throw new Error(`session cookie '${cookie_name}': the signed value holds a character outside RFC 6265's ` +
97
- 'cookie-octets — `encode_identity` must emit only visible ASCII without `"`, `,`, `;` or `\\`');
104
+ 'cookie-octets — only visible ASCII without `"`, `,`, `;` or `\\` can be written');
98
105
  }
99
106
  if (!Number.isSafeInteger(max_age) || max_age < 0) {
100
107
  throw new Error(`session cookie '${cookie_name}': max_age ${max_age} is not a whole number of seconds`);
@@ -115,60 +122,51 @@ export const format_session_clear_cookie = (cookie_name) => {
115
122
  return `${cookie_name}=; ${SESSION_COOKIE_ATTRIBUTES}; Max-Age=0`;
116
123
  };
117
124
  /**
118
- * The `Set-Cookie` value that sets the session cookie to `value`, a signed
119
- * cookie value (`create_session_cookie_value`, or the `cookie_value` of
120
- * `create_capped_session`) — what a success carries
121
- * (`route_ok(value, {set_cookie: [...]})`). `options.max_age` sets `Max-Age`,
122
- * as it set the value's embedded expiry; `SESSION_AGE_MAX` when unset. Twin of
123
- * the Rust `session_cookie_header`, which signs the token itself.
125
+ * Sign a session token into a session cookie value: the keyring's signature
126
+ * of `${session_token}:${expires_at}`, the expiry `SESSION_AGE_MAX` from now —
127
+ * what a `Cookie` header carries after `<name>=`. Signed once, at mint: the
128
+ * lifetime is absolute, never renewed (`docs/security.md` §Session Security).
129
+ * Twin of the Rust `create_session_cookie_value`.
124
130
  *
125
- * @param value - the signed cookie value
126
- * @param options - the cookie name and lifetime
127
- * @returns the `Set-Cookie` header value (`format_session_set_cookie`)
128
- * @throws Error when the cookie name or the value is outside the RFC 6265 grammar
131
+ * @param keyring - key ring for signing
132
+ * @param session_token - the raw session token
133
+ * @param now_seconds - current time in seconds (for testing)
134
+ * @returns the signed cookie value
129
135
  */
130
- export const session_cookie_header = (value, options) => format_session_set_cookie(options.cookie_name, value, options.max_age ?? SESSION_AGE_MAX);
136
+ export const create_session_cookie_value = async (keyring, session_token, now_seconds) => {
137
+ const now = now_seconds ?? Math.floor(Date.now() / 1000);
138
+ return keyring.sign(`${session_token}${VALUE_SEPARATOR}${now + SESSION_AGE_MAX}`);
139
+ };
131
140
  /**
132
- * The `Set-Cookie` value that clears the session cookie — what a logout or a
133
- * password change carries on its success. Twin of the Rust
134
- * `clear_session_cookie_header`.
141
+ * Sign a session token and build the `Set-Cookie` header value that sets it
142
+ * under `cookie_name` — `format_session_set_cookie` over
143
+ * `create_session_cookie_value`, with `SESSION_AGE_MAX` as its lifetime. What
144
+ * a success that mints a session carries (`route_ok(value, {set_cookie})`).
145
+ * Twin of the Rust `sign_session_cookie`.
135
146
  *
136
- * @param options - the cookie name
137
- * @returns the `Set-Cookie` header value (`format_session_clear_cookie`)
138
- * @throws Error when the cookie name is outside the RFC 6265 grammar
147
+ * @param keyring - key ring for signing
148
+ * @param session_token - the raw session token (`create_capped_session`'s `token`)
149
+ * @param cookie_name - the session cookie's name
150
+ * @returns the `Set-Cookie` header value
151
+ * @throws Error when the cookie name, or the signed value, is outside the RFC 6265 grammar
139
152
  */
140
- export const clear_session_cookie_header = (options) => format_session_clear_cookie(options.cookie_name);
153
+ export const sign_session_cookie = async (keyring, session_token, cookie_name) => format_session_set_cookie(cookie_name, await create_session_cookie_value(keyring, session_token), SESSION_AGE_MAX);
141
154
  /**
142
- * Parse a signed session cookie value.
143
- *
144
- * The signed value format is `${encode(identity)}:${expires_at}`.
145
- * Tries all keys in order to support key rotation.
146
- *
147
- * An empty string is a cookie that was sent with no value (`name=`): present
148
- * and invalid, like any other value that fails verification. Only a nullish
149
- * `signed_value` is "no cookie".
150
- *
151
- * @param signed_value - the raw cookie value (signed), nullish when the request carries no session cookie
152
- * @param keyring - key ring for verification
153
- * @param options - session configuration with decode logic
154
- * @param now_seconds - current time in seconds (for testing)
155
- * @returns `ParsedSession` if valid, null if invalid/expired (the empty string included), undefined if missing
155
+ * Verify one signed cookie value: the MAC under any of the keyring's keys (a
156
+ * retired key keeps verifying until it leaves the keyring — safe to drop after
157
+ * `SESSION_AGE_MAX`, `docs/security.md` §Cookie Key Rotation), the
158
+ * `expires_at` grammar and freshness, and a non-empty token.
156
159
  */
157
- export const parse_session = async (signed_value, keyring, options, now_seconds) => {
158
- if (signed_value == null)
159
- return undefined;
160
+ const verify_session_cookie_value = async (signed_value, keyring, now_seconds) => {
160
161
  const result = await keyring.verify(signed_value);
161
162
  if (!result)
162
163
  return null;
163
- // Split on the last VALUE_SEPARATOR to get identity_payload and expires_at
164
+ // split on the last separator: the token, then the signed expiry
164
165
  const last_sep = result.value.lastIndexOf(VALUE_SEPARATOR);
165
166
  if (last_sep === -1)
166
167
  return null;
167
- const identity_payload = result.value.slice(0, last_sep);
168
+ const token = result.value.slice(0, last_sep);
168
169
  const expires_at_str = result.value.slice(last_sep + 1);
169
- const identity = options.decode_identity(identity_payload);
170
- if (identity === null)
171
- return null;
172
170
  // Strict integer match — `parseInt` would accept `'123abc'` as `123` and
173
171
  // `' 123'` as `123`. HMAC integrity makes such a tampered value
174
172
  // theoretical, but stricter parsing closes the gap as defense-in-depth.
@@ -178,86 +176,38 @@ export const parse_session = async (signed_value, keyring, options, now_seconds)
178
176
  if (BigInt(expires_at_str) > EXPIRES_AT_MAX)
179
177
  return null;
180
178
  const expires_at = parseInt(expires_at_str, 10);
181
- // Check expiration
182
- const now = now_seconds ?? Math.floor(Date.now() / 1000);
183
- if (expires_at <= now)
179
+ if (expires_at <= now_seconds)
184
180
  return null;
185
- return {
186
- identity,
187
- key_index: result.key_index,
188
- expires_at
189
- };
190
- };
191
- /**
192
- * Create a signed session cookie value.
193
- *
194
- * Format: `${encode(identity)}:${expires_at}` signed with HMAC-SHA256.
195
- * Embeds expiration in the signed value itself for defense-in-depth.
196
- *
197
- * @param keyring - key ring for signing
198
- * @param identity - the identity to encode
199
- * @param options - session configuration with encode logic
200
- * @param now_seconds - current time in seconds (for testing)
201
- * @returns signed cookie value string
202
- */
203
- export const create_session_cookie_value = async (keyring, identity, options, now_seconds) => {
204
- const max_age = options.max_age ?? SESSION_AGE_MAX;
205
- const now = now_seconds ?? Math.floor(Date.now() / 1000);
206
- const expires_at = now + max_age;
207
- const value = `${options.encode_identity(identity)}${VALUE_SEPARATOR}${expires_at}`;
208
- return keyring.sign(value);
181
+ if (token === '')
182
+ return null;
183
+ return { token, expires_at };
209
184
  };
210
185
  /**
211
- * Create a session config for raw session token identity.
212
- *
213
- * The standard pattern: cookie stores the raw session token,
214
- * server hashes it (blake3) to look up the `auth_session` row.
215
- * Only the `cookie_name` varies per app.
216
- *
217
- * @param cookie_name - cookie name (e.g. `'zap_session'`, `'visiones_session'`)
218
- * @returns a `SessionOptions<string>` ready for use with session middleware
219
- */
220
- export const create_session_config = (cookie_name) => ({
221
- cookie_name,
222
- encode_identity: (session_id) => session_id,
223
- decode_identity: (payload) => payload || null
224
- });
225
- /** Canonical session config for fuz_app auth. */
226
- export const fuz_session_config = create_session_config('fuz_session');
227
- /**
228
- * Process a session cookie and determine what action to take.
229
- *
230
- * There is deliberately no refresh action: any re-sign computes a fresh
231
- * `now + max_age` expiry, extending the cookie past the DB row's fixed
232
- * `expires_at` — the holder would go silently anonymous while carrying a
233
- * self-perpetuating cookie. The absolute lifetime is one signing at mint,
234
- * on both legs, matching the Rust spine. A cookie signed by a retired
235
- * (non-primary) key keeps verifying until the key leaves the keyring;
236
- * retired keys are safe to drop after `SESSION_AGE_MAX` (see
237
- * `docs/security.md` §Cookie Key Rotation).
238
- *
239
- * The three outcomes are what the cookie alone shows: no cookie (`action:
240
- * 'none'`), a cookie that fails verification — unreadable, a bad MAC, a signed
241
- * expiry that has passed, an empty identity, an empty value — (`action:
242
- * 'clear'`), and a valid one. A valid cookie whose `auth_session` row is gone
243
- * is still valid here; nothing clears it.
244
- *
245
- * @param signed_value - the raw cookie value, nullish when the request carries no session cookie
246
- * @param keyring - key ring for verification and signing
247
- * @param options - session configuration
186
+ * Read the session cookie out of a request's `Cookie` lines: the
187
+ * `cookie_name` pair (`read_session_cookie_value` — the first, verbatim),
188
+ * verified under the keyring, its signed expiry checked, its session token
189
+ * answered with that expiry.
190
+ *
191
+ * The three answers are what the cookie alone shows (`SessionCookieRead`). An
192
+ * empty value (`name=`) is a cookie sent with none: present and invalid. A
193
+ * valid cookie whose `auth_session` row is gone is still valid here; nothing
194
+ * clears it. There is deliberately no refresh: a re-sign would compute a
195
+ * fresh expiry past the row's fixed `expires_at`, so the lifetime is the one
196
+ * signing at mint, on both spines.
197
+ *
198
+ * Twin of the Rust `parse_session_from_cookies`, which takes the request's
199
+ * headers.
200
+ *
201
+ * @param cookie_lines - the request's `Cookie` header lines
202
+ * @param keyring - key ring for verification
203
+ * @param cookie_name - the session cookie's name
248
204
  * @param now_seconds - current time in seconds (for testing)
249
- * @returns result with validity and action to take
205
+ * @returns the cookie's reading
250
206
  */
251
- export const process_session_cookie = async (signed_value, keyring, options, now_seconds) => {
252
- const now = now_seconds ?? Math.floor(Date.now() / 1000);
253
- const parsed = await parse_session(signed_value, keyring, options, now);
254
- if (parsed === undefined) {
255
- // No cookie present
256
- return { valid: false, action: 'none' };
257
- }
258
- if (parsed === null) {
259
- // Invalid cookie - should be cleared
260
- return { valid: false, action: 'clear' };
261
- }
262
- return { valid: true, action: 'none', identity: parsed.identity, expires_at: parsed.expires_at };
207
+ export const parse_session_from_cookies = async (cookie_lines, keyring, cookie_name, now_seconds) => {
208
+ const signed_value = read_session_cookie_value(cookie_lines, cookie_name);
209
+ if (signed_value === null)
210
+ return { kind: 'absent' };
211
+ const cookie = await verify_session_cookie_value(signed_value, keyring, now_seconds ?? Math.floor(Date.now() / 1000));
212
+ return cookie ? { kind: 'valid', cookie } : { kind: 'invalid' };
263
213
  };
@@ -1,36 +1,27 @@
1
1
  /**
2
- * Hono session boundary — the session cookie read off a request, and the
3
- * request-time middleware that resolves it and clears a stale one.
2
+ * Hono session boundary — the request-time middleware that reads the session
3
+ * cookie off a request, resolves it, and clears a stale one.
4
4
  *
5
5
  * The cookie is read from the request's `Cookie` lines by
6
- * `read_session_cookie_value` and written as the exact `Set-Cookie` strings of
6
+ * `parse_session_from_cookies` and written as the exact `Set-Cookie` strings of
7
7
  * `auth/session_cookie.ts` — no framework cookie parser or serializer sits on
8
8
  * the credential path, so nothing is percent-decoded on the way in or
9
9
  * percent-encoded on the way out. A route sets or clears the cookie by
10
10
  * returning the line on its success (`route_ok(value, {set_cookie})`, the
11
- * lines from `session_cookie_header` and `clear_session_cookie_header`), never
11
+ * lines from `sign_session_cookie` and `format_session_clear_cookie`), never
12
12
  * by writing on the context.
13
13
  *
14
14
  * @module
15
15
  */
16
- import type { Context, MiddlewareHandler } from 'hono';
16
+ import type { MiddlewareHandler } from 'hono';
17
17
  import type { Keyring } from './keyring.js';
18
- import { type SessionOptions } from './session_cookie.js';
19
- /**
20
- * Read the session cookie's value from a request, as sent
21
- * (`read_session_cookie_value` over the request's `Cookie` lines).
22
- *
23
- * @returns the value — `''` for a cookie sent with none — or `null` when the request carries no session cookie
24
- */
25
- export declare const get_session_cookie: <T>(c: Context, options: SessionOptions<T>) => string | null;
26
18
  /**
27
19
  * Create session middleware that parses cookies and sets the session token on
28
20
  * context.
29
21
  *
30
22
  * Always sets `SESSION_TOKEN_KEY` (null when invalid/missing) for type-safe
31
23
  * reads — the key the session leg (`create_request_context_middleware`)
32
- * reads. The identity is the session token, so the options are a
33
- * `SessionOptions<string>`.
24
+ * reads.
34
25
  *
35
26
  * A cookie that is present and fails verification is cleared — an empty value
36
27
  * (`name=`) included; a request with no session cookie clears nothing. The
@@ -100,8 +91,8 @@ export declare const get_session_cookie: <T>(c: Context, options: SessionOptions
100
91
  * where its own router puts the auth scope.
101
92
  *
102
93
  * @param keyring - key ring for cookie verification
103
- * @param options - session configuration
94
+ * @param session_cookie_name - the session cookie's name
104
95
  * @mutates Hono context - sets `SESSION_TOKEN_KEY` and `SESSION_COOKIE_EXPIRES_AT_KEY`, and may put the clearing `Set-Cookie` on the context's headers, recording it under `EXPECTED_FIRST_SET_COOKIE_KEY`
105
96
  */
106
- export declare const create_session_middleware: (keyring: Keyring, options: SessionOptions<string>) => MiddlewareHandler;
97
+ export declare const create_session_middleware: (keyring: Keyring, session_cookie_name: string) => MiddlewareHandler;
107
98
  //# sourceMappingURL=session_middleware.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"session_middleware.d.ts","sourceRoot":"","sources":["../../src/lib/auth/session_middleware.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAEvD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EACN,KAAK,cAAc,EAGnB,MAAM,qBAAqB,CAAC;AAQ7B;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,EAAE,GAAG,OAAO,EAAE,SAAS,cAAc,CAAC,CAAC,CAAC,KAAG,MAAM,GAAG,IACV,CAAC;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+EG;AACH,eAAO,MAAM,yBAAyB,GACrC,SAAS,OAAO,EAChB,SAAS,cAAc,CAAC,MAAM,CAAC,KAC7B,iBAoBF,CAAC"}
1
+ {"version":3,"file":"session_middleware.d.ts","sourceRoot":"","sources":["../../src/lib/auth/session_middleware.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAE9C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAS5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8EG;AACH,eAAO,MAAM,yBAAyB,GACrC,SAAS,OAAO,EAChB,qBAAqB,MAAM,KACzB,iBAwBF,CAAC"}
@@ -1,37 +1,28 @@
1
1
  /**
2
- * Hono session boundary — the session cookie read off a request, and the
3
- * request-time middleware that resolves it and clears a stale one.
2
+ * Hono session boundary — the request-time middleware that reads the session
3
+ * cookie off a request, resolves it, and clears a stale one.
4
4
  *
5
5
  * The cookie is read from the request's `Cookie` lines by
6
- * `read_session_cookie_value` and written as the exact `Set-Cookie` strings of
6
+ * `parse_session_from_cookies` and written as the exact `Set-Cookie` strings of
7
7
  * `auth/session_cookie.ts` — no framework cookie parser or serializer sits on
8
8
  * the credential path, so nothing is percent-decoded on the way in or
9
9
  * percent-encoded on the way out. A route sets or clears the cookie by
10
10
  * returning the line on its success (`route_ok(value, {set_cookie})`, the
11
- * lines from `session_cookie_header` and `clear_session_cookie_header`), never
11
+ * lines from `sign_session_cookie` and `format_session_clear_cookie`), never
12
12
  * by writing on the context.
13
13
  *
14
14
  * @module
15
15
  */
16
- import { read_session_cookie_value } from './credential_headers.js';
17
- import { format_session_clear_cookie, process_session_cookie } from './session_cookie.js';
16
+ import { format_session_clear_cookie, parse_session_from_cookies } from './session_cookie.js';
18
17
  import { EXPECTED_FIRST_SET_COOKIE_KEY, SESSION_COOKIE_EXPIRES_AT_KEY, SESSION_TOKEN_KEY } from '../hono_context.js';
19
18
  import { get_header_lines } from '../http/header_lines.js';
20
- /**
21
- * Read the session cookie's value from a request, as sent
22
- * (`read_session_cookie_value` over the request's `Cookie` lines).
23
- *
24
- * @returns the value — `''` for a cookie sent with none — or `null` when the request carries no session cookie
25
- */
26
- export const get_session_cookie = (c, options) => read_session_cookie_value(get_header_lines(c, 'Cookie'), options.cookie_name);
27
19
  /**
28
20
  * Create session middleware that parses cookies and sets the session token on
29
21
  * context.
30
22
  *
31
23
  * Always sets `SESSION_TOKEN_KEY` (null when invalid/missing) for type-safe
32
24
  * reads — the key the session leg (`create_request_context_middleware`)
33
- * reads. The identity is the session token, so the options are a
34
- * `SessionOptions<string>`.
25
+ * reads.
35
26
  *
36
27
  * A cookie that is present and fails verification is cleared — an empty value
37
28
  * (`name=`) included; a request with no session cookie clears nothing. The
@@ -101,21 +92,21 @@ export const get_session_cookie = (c, options) => read_session_cookie_value(get_
101
92
  * where its own router puts the auth scope.
102
93
  *
103
94
  * @param keyring - key ring for cookie verification
104
- * @param options - session configuration
95
+ * @param session_cookie_name - the session cookie's name
105
96
  * @mutates Hono context - sets `SESSION_TOKEN_KEY` and `SESSION_COOKIE_EXPIRES_AT_KEY`, and may put the clearing `Set-Cookie` on the context's headers, recording it under `EXPECTED_FIRST_SET_COOKIE_KEY`
106
97
  */
107
- export const create_session_middleware = (keyring, options) => {
98
+ export const create_session_middleware = (keyring, session_cookie_name) => {
108
99
  return async (c, next) => {
109
- const signed_value = get_session_cookie(c, options);
110
- const result = await process_session_cookie(signed_value, keyring, options);
100
+ const read = await parse_session_from_cookies(get_header_lines(c, 'Cookie'), keyring, session_cookie_name);
101
+ const cookie = read.kind === 'valid' ? read.cookie : null;
111
102
  // Always set the token (null when invalid/missing) for type-safe reads
112
- c.set(SESSION_TOKEN_KEY, result.identity ?? null);
103
+ c.set(SESSION_TOKEN_KEY, cookie?.token ?? null);
113
104
  // the signed expiry rides beside it, for the credential deadline
114
- c.set(SESSION_COOKIE_EXPIRES_AT_KEY, result.expires_at ?? null);
105
+ c.set(SESSION_COOKIE_EXPIRES_AT_KEY, cookie?.expires_at ?? null);
115
106
  // ahead of the handler, so it precedes any `Set-Cookie` a success carries;
116
107
  // recorded, so the DEV `Set-Cookie` check can see a response that lost it
117
- if (result.action === 'clear') {
118
- const clear = format_session_clear_cookie(options.cookie_name);
108
+ if (read.kind === 'invalid') {
109
+ const clear = format_session_clear_cookie(session_cookie_name);
119
110
  c.header('Set-Cookie', clear, { append: true });
120
111
  c.set(EXPECTED_FIRST_SET_COOKIE_KEY, clear);
121
112
  }
@@ -7,8 +7,6 @@
7
7
  * @module
8
8
  */
9
9
  import type { QueryDeps } from '../db/query_deps.js';
10
- import type { Keyring } from './keyring.js';
11
- import { type SessionOptions } from './session_cookie.js';
12
10
  import type { AuthSession, SessionId } from './account_schema.js';
13
11
  /**
14
12
  * Session lifetime in milliseconds (30 days).
@@ -36,15 +34,18 @@ export declare const hash_session_token: (token: string) => SessionId;
36
34
  */
37
35
  export declare const generate_session_token: () => string;
38
36
  /**
39
- * Create a new auth session.
37
+ * Create a new auth session, expiring `AUTH_SESSION_LIFETIME_MS` after the
38
+ * database's `NOW()` — the clock the validity check (`expires_at > NOW()`)
39
+ * reads, and the one `created_at` takes, so the expiry sits a whole lifetime
40
+ * past the row's creation and every session one transaction creates shares
41
+ * it. Twin of the Rust `query_create_session`, same statement.
40
42
  *
41
43
  * @param deps - query dependencies
42
44
  * @param token_hash - blake3 hash of the session token (use `hash_session_token`)
43
45
  * @param account_id - the account this session belongs to
44
- * @param expires_at - when the session expires
45
46
  * @mutates `auth_session` table - inserts a row keyed by `token_hash`
46
47
  */
47
- export declare const query_create_session: (deps: QueryDeps, token_hash: string, account_id: string, expires_at: Date) => Promise<void>;
48
+ export declare const query_create_session: (deps: QueryDeps, token_hash: string, account_id: string) => Promise<void>;
48
49
  /**
49
50
  * The full `auth_session` column set, named explicitly so a row read fails
50
51
  * loud on schema drift — `SELECT *` would silently carry a dropped or
@@ -242,29 +243,26 @@ export declare const query_session_cleanup_expired: (deps: QueryDeps) => Promise
242
243
  * Options for `create_capped_session`.
243
244
  */
244
245
  export interface CreateCappedSessionOptions {
245
- /** Keyring for cookie signing. */
246
- keyring: Keyring;
247
246
  /** Query deps (needs db for session creation). */
248
247
  deps: QueryDeps;
249
248
  /** The account to create a session for. */
250
249
  account_id: string;
251
- /** Session cookie configuration. */
252
- session_options: SessionOptions<string>;
253
250
  /** Per-account session cap (`null` to skip enforcement). */
254
251
  max_sessions?: number | null;
255
252
  }
256
- /** What `create_capped_session` minted. */
253
+ /** What `create_capped_session` minted. Twin of the Rust `CappedSession`. */
257
254
  export interface CappedSession {
258
- /** The signed session cookie value, for `session_cookie_header`. */
259
- cookie_value: string;
255
+ /** The raw session token, for `sign_session_cookie` — a live credential. */
256
+ token: string;
260
257
  /** The token hashes of the sessions the cap evicted, empty without a cap. */
261
258
  evicted: Array<string>;
262
259
  }
263
260
  /**
264
- * Mint an auth session: generate a token, persist its hashed row, enforce
265
- * the per-account cap when given one, and sign the cookie value — without
266
- * touching a response, so a caller minting inside a transaction sets the
267
- * cookie only once that transaction has succeeded. Twin of the Rust
261
+ * Mint an auth session: generate a token, persist its hashed row, and enforce
262
+ * the per-account cap when given one — without touching a response. The
263
+ * caller signs the token into its `Set-Cookie` line (`sign_session_cookie`)
264
+ * and sets it on its success, so a session minted inside a transaction gets
265
+ * its cookie only once that transaction has succeeded. Twin of the Rust
268
266
  * `fuz_auth::create_capped_session`.
269
267
  *
270
268
  * The cap deletes the sessions it evicts, but not the connections they
@@ -279,7 +277,7 @@ export interface CappedSession {
279
277
  * account's mints take turns, which the cap counts on
280
278
  * (`query_session_enforce_limit`).
281
279
  *
282
- * @returns the signed cookie value and the evicted sessions' token hashes
280
+ * @returns the raw session token and the evicted sessions' token hashes
283
281
  * @mutates `auth_session` table - inserts the new session row (and evicts older rows when `max_sessions` is set)
284
282
  */
285
283
  export declare const create_capped_session: (options: CreateCappedSessionOptions) => Promise<CappedSession>;
@@ -1 +1 @@
1
- {"version":3,"file":"session_queries.d.ts","sourceRoot":"","sources":["../../src/lib/auth/session_queries.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAA+B,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAEvF,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAGlE;;;;;;;GAOG;AACH,eAAO,MAAM,wBAA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,9 +8,10 @@
8
8
  */
9
9
  import { hash_blake3 } from '@fuzdev/fuz_util/hash_blake3.ts';
10
10
  import { generate_random_base64url } from '../crypto.js';
11
- import { create_session_cookie_value } from './session_cookie.js';
12
11
  import { columns_sql, iso8601_timestamp_expr, qualify_columns } from '../db/sql_columns.js';
13
12
  import { EXPIRES_IN_MS_SQL } from './credential_deadline.js';
13
+ /** The session lifetime in whole seconds, which the insert's interval spells. */
14
+ const AUTH_SESSION_LIFETIME_SECONDS = 30 * 24 * 60 * 60;
14
15
  /**
15
16
  * Session lifetime in milliseconds (30 days).
16
17
  *
@@ -19,7 +20,7 @@ import { EXPIRES_IN_MS_SQL } from './credential_deadline.js';
19
20
  * window renews a leaked cookie forever; see `docs/security.md` §Session
20
21
  * Security). The cookie's `SESSION_AGE_MAX` mirrors this value.
21
22
  */
22
- export const AUTH_SESSION_LIFETIME_MS = 30 * 24 * 60 * 60 * 1000;
23
+ export const AUTH_SESSION_LIFETIME_MS = AUTH_SESSION_LIFETIME_SECONDS * 1000;
23
24
  /**
24
25
  * Hash a session token to its storage key using blake3.
25
26
  *
@@ -41,20 +42,28 @@ export const generate_session_token = () => {
41
42
  return generate_random_base64url();
42
43
  };
43
44
  /**
44
- * Create a new auth session.
45
+ * `AUTH_SESSION_LIFETIME_MS` as the SQL interval the session insert adds to
46
+ * `NOW()` — `INTERVAL '2592000 seconds'`, the Rust spine's literal. Elapsed
47
+ * seconds, not `'30 days'`: a day interval is calendar time in the session's
48
+ * time zone, so a session crossing a DST change would live an hour more or
49
+ * less.
50
+ */
51
+ const AUTH_SESSION_LIFETIME_SQL = `INTERVAL '${AUTH_SESSION_LIFETIME_SECONDS} seconds'`;
52
+ /**
53
+ * Create a new auth session, expiring `AUTH_SESSION_LIFETIME_MS` after the
54
+ * database's `NOW()` — the clock the validity check (`expires_at > NOW()`)
55
+ * reads, and the one `created_at` takes, so the expiry sits a whole lifetime
56
+ * past the row's creation and every session one transaction creates shares
57
+ * it. Twin of the Rust `query_create_session`, same statement.
45
58
  *
46
59
  * @param deps - query dependencies
47
60
  * @param token_hash - blake3 hash of the session token (use `hash_session_token`)
48
61
  * @param account_id - the account this session belongs to
49
- * @param expires_at - when the session expires
50
62
  * @mutates `auth_session` table - inserts a row keyed by `token_hash`
51
63
  */
52
- export const query_create_session = async (deps, token_hash, account_id, expires_at) => {
53
- await deps.db.query(`INSERT INTO auth_session (id, account_id, expires_at) VALUES ($1, $2, $3)`, [
54
- token_hash,
55
- account_id,
56
- expires_at.toISOString()
57
- ]);
64
+ export const query_create_session = async (deps, token_hash, account_id) => {
65
+ await deps.db.query(`INSERT INTO auth_session (id, account_id, expires_at)
66
+ VALUES ($1, $2, NOW() + ${AUTH_SESSION_LIFETIME_SQL})`, [token_hash, account_id]);
58
67
  };
59
68
  /**
60
69
  * The full `auth_session` column set, named explicitly so a row read fails
@@ -288,10 +297,11 @@ export const query_session_cleanup_expired = async (deps) => {
288
297
  return rows.map((row) => row.id);
289
298
  };
290
299
  /**
291
- * Mint an auth session: generate a token, persist its hashed row, enforce
292
- * the per-account cap when given one, and sign the cookie value — without
293
- * touching a response, so a caller minting inside a transaction sets the
294
- * cookie only once that transaction has succeeded. Twin of the Rust
300
+ * Mint an auth session: generate a token, persist its hashed row, and enforce
301
+ * the per-account cap when given one — without touching a response. The
302
+ * caller signs the token into its `Set-Cookie` line (`sign_session_cookie`)
303
+ * and sets it on its success, so a session minted inside a transaction gets
304
+ * its cookie only once that transaction has succeeded. Twin of the Rust
295
305
  * `fuz_auth::create_capped_session`.
296
306
  *
297
307
  * The cap deletes the sessions it evicts, but not the connections they
@@ -306,16 +316,14 @@ export const query_session_cleanup_expired = async (deps) => {
306
316
  * account's mints take turns, which the cap counts on
307
317
  * (`query_session_enforce_limit`).
308
318
  *
309
- * @returns the signed cookie value and the evicted sessions' token hashes
319
+ * @returns the raw session token and the evicted sessions' token hashes
310
320
  * @mutates `auth_session` table - inserts the new session row (and evicts older rows when `max_sessions` is set)
311
321
  */
312
322
  export const create_capped_session = async (options) => {
313
- const { keyring, deps, account_id, session_options, max_sessions } = options;
323
+ const { deps, account_id, max_sessions } = options;
314
324
  const session_token = generate_session_token();
315
325
  const token_hash = hash_session_token(session_token);
316
- const expires_at = new Date(Date.now() + AUTH_SESSION_LIFETIME_MS);
317
- await query_create_session(deps, token_hash, account_id, expires_at);
326
+ await query_create_session(deps, token_hash, account_id);
318
327
  const evicted = max_sessions != null ? await query_session_enforce_limit(deps, account_id, max_sessions) : [];
319
- const cookie_value = await create_session_cookie_value(keyring, session_token, session_options);
320
- return { cookie_value, evicted };
328
+ return { token: session_token, evicted };
321
329
  };
@@ -1 +1 @@
1
- {"version":3,"file":"signup_routes.d.ts","sourceRoot":"","sources":["../../src/lib/auth/signup_routes.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAcH,OAAO,EAAwB,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAG7E,OAAO,EAAoB,KAAK,WAAW,EAA6B,MAAM,oBAAoB,CAAC;AACnG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAOlD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAGnE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,4BAA4B,MAAM,CAAC;AAEhD;;;;;;;GAOG;AACH,eAAO,MAAM,6BAA6B,KAAK,CAAC;AAEhD;;GAEG;AACH,MAAM,WAAW,kBAAmB,SAAQ,uBAAuB;IAClE;;;;;;;;;OASG;IACH,sBAAsB,EAAE,WAAW,GAAG,IAAI,CAAC;IAC3C,6FAA6F;IAC7F,2BAA2B,EAAE,WAAW,GAAG,IAAI,CAAC;IAChD;;;;;OAKG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;CAC/B;AAID;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,GACrC,MAAM,gBAAgB,EACtB,SAAS,kBA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