@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.
- package/README.md +2 -0
- package/dist/actions/CLAUDE.md +535 -1223
- package/dist/actions/action_codegen.d.ts +1 -1
- package/dist/actions/action_codegen.d.ts.map +1 -1
- package/dist/actions/action_codegen.js +3 -2
- package/dist/actions/connection_closer.d.ts +11 -9
- package/dist/actions/connection_closer.d.ts.map +1 -1
- package/dist/actions/connection_closer.js +11 -9
- package/dist/actions/frontend_rpc_client.d.ts +1 -1
- package/dist/actions/register_action_ws.d.ts +14 -1
- package/dist/actions/register_action_ws.d.ts.map +1 -1
- package/dist/actions/register_action_ws.js +2 -1
- package/dist/actions/ws_socket_session.d.ts +9 -1
- package/dist/actions/ws_socket_session.d.ts.map +1 -1
- package/dist/actions/ws_socket_session.js +43 -19
- package/dist/auth/CLAUDE.md +430 -1024
- package/dist/auth/account_queries.d.ts +1 -1
- package/dist/auth/account_queries.js +1 -1
- package/dist/auth/account_routes.d.ts +9 -8
- package/dist/auth/account_routes.d.ts.map +1 -1
- package/dist/auth/account_routes.js +78 -75
- package/dist/auth/admin_actions.d.ts.map +1 -1
- package/dist/auth/admin_actions.js +32 -28
- package/dist/auth/api_token.d.ts +20 -3
- package/dist/auth/api_token.d.ts.map +1 -1
- package/dist/auth/api_token.js +28 -9
- package/dist/auth/api_token_queries.d.ts +2 -2
- package/dist/auth/api_token_queries.js +2 -2
- package/dist/auth/audit_emitter.d.ts +75 -84
- package/dist/auth/audit_emitter.d.ts.map +1 -1
- package/dist/auth/audit_emitter.js +83 -71
- package/dist/auth/audit_log_schema.d.ts +25 -14
- package/dist/auth/audit_log_schema.d.ts.map +1 -1
- package/dist/auth/audit_log_schema.js +23 -8
- package/dist/auth/auth_guard_resolver.d.ts +1 -1
- package/dist/auth/auth_guard_resolver.js +1 -1
- package/dist/auth/bearer_auth.d.ts +2 -5
- package/dist/auth/bearer_auth.d.ts.map +1 -1
- package/dist/auth/bearer_auth.js +4 -13
- package/dist/auth/bootstrap_account.d.ts +42 -26
- package/dist/auth/bootstrap_account.d.ts.map +1 -1
- package/dist/auth/bootstrap_account.js +36 -19
- package/dist/auth/bootstrap_routes.d.ts +20 -21
- package/dist/auth/bootstrap_routes.d.ts.map +1 -1
- package/dist/auth/bootstrap_routes.js +26 -31
- package/dist/auth/bootstrap_token_store.d.ts +3 -4
- package/dist/auth/bootstrap_token_store.d.ts.map +1 -1
- package/dist/auth/bootstrap_token_store.js +1 -1
- package/dist/auth/daemon_token.d.ts +5 -1
- package/dist/auth/daemon_token.d.ts.map +1 -1
- package/dist/auth/daemon_token_middleware.d.ts +12 -8
- package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
- package/dist/auth/daemon_token_middleware.js +15 -21
- package/dist/auth/deps.d.ts +19 -5
- package/dist/auth/deps.d.ts.map +1 -1
- package/dist/auth/deps.js +3 -2
- package/dist/auth/invite_queries.d.ts +51 -3
- package/dist/auth/invite_queries.d.ts.map +1 -1
- package/dist/auth/invite_queries.js +81 -6
- package/dist/auth/invite_schema.d.ts +2 -1
- package/dist/auth/invite_schema.d.ts.map +1 -1
- package/dist/auth/middleware.d.ts +2 -3
- package/dist/auth/middleware.d.ts.map +1 -1
- package/dist/auth/middleware.js +3 -3
- package/dist/auth/request_context.d.ts +3 -3
- package/dist/auth/request_context.js +3 -3
- package/dist/auth/resolved_auth.d.ts +7 -7
- package/dist/auth/resolved_auth.d.ts.map +1 -1
- package/dist/auth/resolved_auth.js +4 -4
- package/dist/auth/role_grant_offer_actions.d.ts +27 -3
- package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_actions.js +42 -35
- package/dist/auth/role_grant_offer_queries.d.ts +60 -4
- package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_queries.js +79 -5
- package/dist/auth/role_grant_queries.d.ts +3 -52
- package/dist/auth/role_grant_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_queries.js +5 -80
- package/dist/auth/self_service_role_actions.d.ts.map +1 -1
- package/dist/auth/self_service_role_actions.js +11 -22
- package/dist/auth/session_cookie.d.ts +82 -174
- package/dist/auth/session_cookie.d.ts.map +1 -1
- package/dist/auth/session_cookie.js +80 -130
- package/dist/auth/session_middleware.d.ts +8 -17
- package/dist/auth/session_middleware.d.ts.map +1 -1
- package/dist/auth/session_middleware.js +14 -23
- package/dist/auth/session_queries.d.ts +15 -17
- package/dist/auth/session_queries.d.ts.map +1 -1
- package/dist/auth/session_queries.js +28 -20
- package/dist/auth/signup_routes.d.ts.map +1 -1
- package/dist/auth/signup_routes.js +93 -43
- package/dist/db/CLAUDE.md +5 -3
- package/dist/db/sql_columns.d.ts +1 -1
- package/dist/db/sql_columns.js +1 -1
- package/dist/dev/setup.d.ts +3 -1
- package/dist/dev/setup.d.ts.map +1 -1
- package/dist/dev/setup.js +3 -1
- package/dist/http/CLAUDE.md +315 -763
- package/dist/http/db_routes.d.ts +8 -23
- package/dist/http/db_routes.d.ts.map +1 -1
- package/dist/http/db_routes.js +73 -73
- package/dist/http/origin.d.ts +7 -8
- package/dist/http/origin.d.ts.map +1 -1
- package/dist/http/origin.js +7 -8
- package/dist/http/pending_effects.d.ts +118 -6
- package/dist/http/pending_effects.d.ts.map +1 -1
- package/dist/http/pending_effects.js +114 -6
- package/dist/http/route_result.d.ts +1 -1
- package/dist/http/route_spec.d.ts +14 -11
- package/dist/http/route_spec.d.ts.map +1 -1
- package/dist/realtime/sse.d.ts +4 -23
- package/dist/realtime/sse.d.ts.map +1 -1
- package/dist/realtime/sse.js +4 -25
- package/dist/server/app_backend.d.ts +4 -5
- package/dist/server/app_backend.d.ts.map +1 -1
- package/dist/server/app_backend.js +3 -1
- package/dist/server/app_server.d.ts +80 -16
- package/dist/server/app_server.d.ts.map +1 -1
- package/dist/server/app_server.js +113 -31
- package/dist/server/app_server_context.d.ts +1 -2
- package/dist/server/app_server_context.d.ts.map +1 -1
- package/dist/server/env.js +2 -2
- package/dist/testing/CLAUDE.md +265 -796
- package/dist/testing/admin_integration.d.ts +2 -3
- package/dist/testing/admin_integration.d.ts.map +1 -1
- package/dist/testing/admin_integration.js +3 -3
- package/dist/testing/adversarial_404.d.ts +6 -1
- package/dist/testing/adversarial_404.d.ts.map +1 -1
- package/dist/testing/adversarial_404.js +38 -17
- package/dist/testing/app_server.d.ts +23 -148
- package/dist/testing/app_server.d.ts.map +1 -1
- package/dist/testing/app_server.js +53 -131
- package/dist/testing/attack_surface.d.ts +1 -1
- package/dist/testing/attack_surface.d.ts.map +1 -1
- package/dist/testing/audit_completeness.d.ts +2 -3
- package/dist/testing/audit_completeness.d.ts.map +1 -1
- package/dist/testing/audit_completeness.js +1 -1
- package/dist/testing/audit_drift_guard.d.ts +11 -6
- package/dist/testing/audit_drift_guard.d.ts.map +1 -1
- package/dist/testing/audit_drift_guard.js +15 -8
- package/dist/testing/cross_backend/audit_cross_helpers.d.ts +12 -9
- package/dist/testing/cross_backend/audit_cross_helpers.d.ts.map +1 -1
- package/dist/testing/cross_backend/audit_cross_helpers.js +13 -14
- package/dist/testing/cross_backend/capabilities.d.ts +9 -6
- package/dist/testing/cross_backend/capabilities.d.ts.map +1 -1
- package/dist/testing/cross_backend/capabilities.js +11 -7
- package/dist/testing/cross_backend/capability_mounts.d.ts +103 -0
- package/dist/testing/cross_backend/capability_mounts.d.ts.map +1 -0
- package/dist/testing/cross_backend/capability_mounts.js +428 -0
- package/dist/testing/cross_backend/cell_moderate.js +2 -2
- package/dist/testing/cross_backend/cell_relations.d.ts.map +1 -1
- package/dist/testing/cross_backend/cell_relations.js +12 -11
- package/dist/testing/cross_backend/conformance_principal.d.ts +54 -0
- package/dist/testing/cross_backend/conformance_principal.d.ts.map +1 -0
- package/dist/testing/cross_backend/conformance_principal.js +283 -0
- package/dist/testing/cross_backend/conformance_table.d.ts +7 -56
- package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
- package/dist/testing/cross_backend/conformance_table.js +6 -274
- package/dist/testing/cross_backend/cookie_attributes.js +1 -1
- package/dist/testing/cross_backend/credential_deadline.d.ts.map +1 -1
- package/dist/testing/cross_backend/credential_deadline.js +2 -0
- package/dist/testing/cross_backend/credential_wire.d.ts +1 -1
- package/dist/testing/cross_backend/credential_wire.d.ts.map +1 -1
- package/dist/testing/cross_backend/db_state_diff.d.ts +72 -0
- package/dist/testing/cross_backend/db_state_diff.d.ts.map +1 -0
- package/dist/testing/cross_backend/db_state_diff.js +444 -0
- package/dist/testing/cross_backend/declared_skips.d.ts +68 -0
- package/dist/testing/cross_backend/declared_skips.d.ts.map +1 -0
- package/dist/testing/cross_backend/declared_skips.js +132 -0
- package/dist/testing/cross_backend/default_backend_configs.d.ts +3 -3
- package/dist/testing/cross_backend/default_spine_surface.d.ts +0 -6
- package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
- package/dist/testing/cross_backend/default_spine_surface.js +4 -9
- package/dist/testing/cross_backend/differential.d.ts +13 -5
- package/dist/testing/cross_backend/differential.d.ts.map +1 -1
- package/dist/testing/cross_backend/differential.js +35 -11
- package/dist/testing/cross_backend/differential_normalize.d.ts +12 -0
- package/dist/testing/cross_backend/differential_normalize.d.ts.map +1 -1
- package/dist/testing/cross_backend/differential_normalize.js +16 -0
- package/dist/testing/cross_backend/full_spine_mount.d.ts +1 -42
- package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
- package/dist/testing/cross_backend/full_spine_mount.js +45 -4
- package/dist/testing/cross_backend/in_process_setup.d.ts +28 -19
- package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/in_process_setup.js +156 -142
- package/dist/testing/cross_backend/login_security.d.ts +12 -0
- package/dist/testing/cross_backend/login_security.d.ts.map +1 -1
- package/dist/testing/cross_backend/login_security.js +21 -4
- package/dist/testing/cross_backend/request_target_conformance_wire.d.ts +0 -2
- package/dist/testing/cross_backend/request_target_conformance_wire.d.ts.map +1 -1
- package/dist/testing/cross_backend/request_target_conformance_wire.js +3 -1
- package/dist/testing/cross_backend/revocation_drivers.d.ts +18 -5
- package/dist/testing/cross_backend/revocation_drivers.d.ts.map +1 -1
- package/dist/testing/cross_backend/revocation_drivers.js +18 -10
- package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts +1 -1
- package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts.map +1 -1
- package/dist/testing/cross_backend/role_grant_offer_lifecycle.d.ts.map +1 -1
- package/dist/testing/cross_backend/role_grant_offer_lifecycle.js +33 -1
- package/dist/testing/cross_backend/role_grant_participation.js +4 -4
- package/dist/testing/cross_backend/rpc_adversarial.js +1 -1
- package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts +3 -4
- package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
- package/dist/testing/cross_backend/rust_spine_consumer_surface.js +2 -2
- package/dist/testing/cross_backend/session_cap.d.ts +2 -2
- package/dist/testing/cross_backend/session_cap.d.ts.map +1 -1
- package/dist/testing/cross_backend/session_cap.js +22 -11
- package/dist/testing/cross_backend/setup.d.ts +32 -2
- package/dist/testing/cross_backend/setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/setup.js +32 -1
- package/dist/testing/cross_backend/spine_suites.d.ts +125 -15
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +459 -62
- package/dist/testing/cross_backend/spine_surface_constants.d.ts +1 -1
- package/dist/testing/cross_backend/spine_surface_constants.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_surface_constants.js +1 -0
- package/dist/testing/cross_backend/sse_round_trip.d.ts +9 -1
- package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
- package/dist/testing/cross_backend/sse_round_trip.js +123 -27
- package/dist/testing/cross_backend/standard.d.ts +2 -3
- package/dist/testing/cross_backend/standard.d.ts.map +1 -1
- package/dist/testing/cross_backend/standard.js +3 -3
- package/dist/testing/cross_backend/testing_backdoor.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_backdoor.js +4 -2
- package/dist/testing/cross_backend/testing_credential_read.d.ts +10 -11
- package/dist/testing/cross_backend/testing_credential_read.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_credential_read.js +12 -12
- package/dist/testing/cross_backend/testing_reset_actions.d.ts +84 -47
- package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_reset_actions.js +95 -63
- package/dist/testing/cross_backend/testing_server_core.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_server_core.js +2 -3
- package/dist/testing/cross_backend/transport_composition.d.ts +1 -1
- package/dist/testing/cross_backend/transport_composition.d.ts.map +1 -1
- package/dist/testing/cross_backend/transport_composition.js +7 -3
- package/dist/testing/cross_backend/ws_round_trip.d.ts +5 -0
- package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
- package/dist/testing/cross_backend/ws_round_trip.js +54 -8
- package/dist/testing/daemon_token_rotation.d.ts.map +1 -1
- package/dist/testing/daemon_token_rotation.js +2 -3
- package/dist/testing/data_exposure.d.ts.map +1 -1
- package/dist/testing/data_exposure.js +3 -0
- package/dist/testing/db.d.ts +0 -32
- package/dist/testing/db.d.ts.map +1 -1
- package/dist/testing/db.js +0 -86
- package/dist/testing/db_entities.d.ts +14 -0
- package/dist/testing/db_entities.d.ts.map +1 -1
- package/dist/testing/db_entities.js +17 -0
- package/dist/testing/db_invariant_queries.d.ts +259 -0
- package/dist/testing/db_invariant_queries.d.ts.map +1 -0
- package/dist/testing/db_invariant_queries.js +400 -0
- package/dist/testing/db_invariants.d.ts +106 -0
- package/dist/testing/db_invariants.d.ts.map +1 -0
- package/dist/testing/db_invariants.gen.sql.d.ts +11 -0
- package/dist/testing/db_invariants.gen.sql.d.ts.map +1 -0
- package/dist/testing/db_invariants.gen.sql.js +13 -0
- package/dist/testing/db_invariants.js +141 -0
- package/dist/testing/db_invariants.sql +152 -0
- package/dist/testing/describe_db.d.ts +36 -0
- package/dist/testing/describe_db.d.ts.map +1 -0
- package/dist/testing/describe_db.js +71 -0
- package/dist/testing/integration.d.ts +8 -12
- package/dist/testing/integration.d.ts.map +1 -1
- package/dist/testing/integration.js +13 -17
- package/dist/testing/integration_helpers.d.ts +1 -1
- package/dist/testing/integration_helpers.d.ts.map +1 -1
- package/dist/testing/rate_limiting.d.ts +3 -4
- package/dist/testing/rate_limiting.d.ts.map +1 -1
- package/dist/testing/rate_limiting.js +6 -5
- package/dist/testing/role_grant_helpers.d.ts +1 -1
- package/dist/testing/role_grant_helpers.d.ts.map +1 -1
- package/dist/testing/round_trip.d.ts.map +1 -1
- package/dist/testing/round_trip.js +3 -0
- package/dist/testing/rpc_attack_surface.d.ts +8 -6
- package/dist/testing/rpc_attack_surface.d.ts.map +1 -1
- package/dist/testing/rpc_attack_surface.js +69 -6
- package/dist/testing/rpc_helpers.d.ts +1 -2
- package/dist/testing/rpc_helpers.d.ts.map +1 -1
- package/dist/testing/rpc_helpers.js +3 -3
- package/dist/testing/rpc_round_trip.d.ts +3 -4
- package/dist/testing/rpc_round_trip.d.ts.map +1 -1
- package/dist/testing/rpc_round_trip.js +4 -1
- package/dist/testing/sse_round_trip.d.ts +3 -4
- package/dist/testing/sse_round_trip.d.ts.map +1 -1
- package/dist/testing/sse_round_trip.js +15 -6
- package/dist/testing/stubs.d.ts +9 -8
- package/dist/testing/stubs.d.ts.map +1 -1
- package/dist/testing/stubs.js +20 -13
- package/dist/testing/test_app.d.ts +168 -0
- package/dist/testing/test_app.d.ts.map +1 -0
- package/dist/testing/test_app.js +123 -0
- package/dist/testing/ws_round_trip.d.ts.map +1 -1
- package/dist/testing/ws_round_trip.js +3 -0
- package/dist/ui/ui_format.d.ts.map +1 -1
- package/dist/ui/ui_format.js +1 -2
- package/package.json +1 -1
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
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
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
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 +
|
|
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
|
|
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:
|
|
62
|
-
*
|
|
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 (
|
|
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 —
|
|
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
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* `
|
|
121
|
-
*
|
|
122
|
-
*
|
|
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
|
|
126
|
-
* @param
|
|
127
|
-
* @
|
|
128
|
-
* @
|
|
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
|
|
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
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* `
|
|
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
|
|
137
|
-
* @
|
|
138
|
-
* @
|
|
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
|
|
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
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
*
|
|
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
|
|
205
|
+
* @returns the cookie's reading
|
|
250
206
|
*/
|
|
251
|
-
export const
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
|
3
|
-
*
|
|
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
|
-
* `
|
|
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 `
|
|
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 {
|
|
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.
|
|
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
|
|
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,
|
|
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,
|
|
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
|
|
3
|
-
*
|
|
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
|
-
* `
|
|
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 `
|
|
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 {
|
|
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.
|
|
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
|
|
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,
|
|
98
|
+
export const create_session_middleware = (keyring, session_cookie_name) => {
|
|
108
99
|
return async (c, next) => {
|
|
109
|
-
const
|
|
110
|
-
const
|
|
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,
|
|
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,
|
|
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 (
|
|
118
|
-
const clear = format_session_clear_cookie(
|
|
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
|
|
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
|
|
259
|
-
|
|
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
|
|
266
|
-
*
|
|
267
|
-
*
|
|
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
|
|
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;
|
|
1
|
+
{"version":3,"file":"session_queries.d.ts","sourceRoot":"","sources":["../../src/lib/auth/session_queries.ts"],"names":[],"mappings":"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"}
|
|
@@ -8,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 =
|
|
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
|
-
*
|
|
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
|
|
53
|
-
await deps.db.query(`INSERT INTO auth_session (id, account_id, expires_at)
|
|
54
|
-
|
|
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
|
|
293
|
-
*
|
|
294
|
-
*
|
|
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
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
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;
|
|
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;AAI7E,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,kBAAkB,KACzB,KAAK,CAAC,SAAS,CA6QjB,CAAC"}
|