@astram/core 0.1.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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/admin/admin-service.d.ts +221 -0
- package/dist/admin/admin-service.d.ts.map +1 -0
- package/dist/admin/admin-service.js +155 -0
- package/dist/admin/admin-service.js.map +1 -0
- package/dist/admin/command/grants.d.ts +40 -0
- package/dist/admin/command/grants.d.ts.map +1 -0
- package/dist/admin/command/grants.js +196 -0
- package/dist/admin/command/grants.js.map +1 -0
- package/dist/admin/command/index.d.ts +23 -0
- package/dist/admin/command/index.d.ts.map +1 -0
- package/dist/admin/command/index.js +23 -0
- package/dist/admin/command/index.js.map +1 -0
- package/dist/admin/command/invitations.d.ts +72 -0
- package/dist/admin/command/invitations.d.ts.map +1 -0
- package/dist/admin/command/invitations.js +241 -0
- package/dist/admin/command/invitations.js.map +1 -0
- package/dist/admin/command/permissions.d.ts +33 -0
- package/dist/admin/command/permissions.d.ts.map +1 -0
- package/dist/admin/command/permissions.js +67 -0
- package/dist/admin/command/permissions.js.map +1 -0
- package/dist/admin/command/pipeline.d.ts +79 -0
- package/dist/admin/command/pipeline.d.ts.map +1 -0
- package/dist/admin/command/pipeline.js +106 -0
- package/dist/admin/command/pipeline.js.map +1 -0
- package/dist/admin/command/policies.d.ts +50 -0
- package/dist/admin/command/policies.d.ts.map +1 -0
- package/dist/admin/command/policies.js +219 -0
- package/dist/admin/command/policies.js.map +1 -0
- package/dist/admin/command/service-accounts.d.ts +58 -0
- package/dist/admin/command/service-accounts.d.ts.map +1 -0
- package/dist/admin/command/service-accounts.js +272 -0
- package/dist/admin/command/service-accounts.js.map +1 -0
- package/dist/admin/command/service.d.ts +65 -0
- package/dist/admin/command/service.d.ts.map +1 -0
- package/dist/admin/command/service.js +128 -0
- package/dist/admin/command/service.js.map +1 -0
- package/dist/admin/command/tiers.d.ts +40 -0
- package/dist/admin/command/tiers.d.ts.map +1 -0
- package/dist/admin/command/tiers.js +79 -0
- package/dist/admin/command/tiers.js.map +1 -0
- package/dist/admin/index.d.ts +36 -0
- package/dist/admin/index.d.ts.map +1 -0
- package/dist/admin/index.js +63 -0
- package/dist/admin/index.js.map +1 -0
- package/dist/admin/ports.d.ts +288 -0
- package/dist/admin/ports.d.ts.map +1 -0
- package/dist/admin/ports.js +39 -0
- package/dist/admin/ports.js.map +1 -0
- package/dist/admin/query/audit-view.d.ts +52 -0
- package/dist/admin/query/audit-view.d.ts.map +1 -0
- package/dist/admin/query/audit-view.js +84 -0
- package/dist/admin/query/audit-view.js.map +1 -0
- package/dist/admin/query/catalog.d.ts +70 -0
- package/dist/admin/query/catalog.d.ts.map +1 -0
- package/dist/admin/query/catalog.js +169 -0
- package/dist/admin/query/catalog.js.map +1 -0
- package/dist/admin/query/explain.d.ts +52 -0
- package/dist/admin/query/explain.d.ts.map +1 -0
- package/dist/admin/query/explain.js +145 -0
- package/dist/admin/query/explain.js.map +1 -0
- package/dist/admin/query/index.d.ts +45 -0
- package/dist/admin/query/index.d.ts.map +1 -0
- package/dist/admin/query/index.js +58 -0
- package/dist/admin/query/index.js.map +1 -0
- package/dist/admin/query/simulate.d.ts +42 -0
- package/dist/admin/query/simulate.d.ts.map +1 -0
- package/dist/admin/query/simulate.js +91 -0
- package/dist/admin/query/simulate.js.map +1 -0
- package/dist/admin/safeguards-impl.d.ts +67 -0
- package/dist/admin/safeguards-impl.d.ts.map +1 -0
- package/dist/admin/safeguards-impl.js +318 -0
- package/dist/admin/safeguards-impl.js.map +1 -0
- package/dist/admin/safeguards.d.ts +89 -0
- package/dist/admin/safeguards.d.ts.map +1 -0
- package/dist/admin/safeguards.js +91 -0
- package/dist/admin/safeguards.js.map +1 -0
- package/dist/admin/types.d.ts +485 -0
- package/dist/admin/types.d.ts.map +1 -0
- package/dist/admin/types.js +38 -0
- package/dist/admin/types.js.map +1 -0
- package/dist/audit/audit-recorder.d.ts +129 -0
- package/dist/audit/audit-recorder.d.ts.map +1 -0
- package/dist/audit/audit-recorder.js +265 -0
- package/dist/audit/audit-recorder.js.map +1 -0
- package/dist/audit/canonicalize.d.ts +38 -0
- package/dist/audit/canonicalize.d.ts.map +1 -0
- package/dist/audit/canonicalize.js +174 -0
- package/dist/audit/canonicalize.js.map +1 -0
- package/dist/audit/errors.d.ts +56 -0
- package/dist/audit/errors.d.ts.map +1 -0
- package/dist/audit/errors.js +41 -0
- package/dist/audit/errors.js.map +1 -0
- package/dist/audit/index.d.ts +45 -0
- package/dist/audit/index.d.ts.map +1 -0
- package/dist/audit/index.js +24 -0
- package/dist/audit/index.js.map +1 -0
- package/dist/audit/ports.d.ts +131 -0
- package/dist/audit/ports.d.ts.map +1 -0
- package/dist/audit/ports.js +27 -0
- package/dist/audit/ports.js.map +1 -0
- package/dist/audit/sensitive.d.ts +68 -0
- package/dist/audit/sensitive.d.ts.map +1 -0
- package/dist/audit/sensitive.js +159 -0
- package/dist/audit/sensitive.js.map +1 -0
- package/dist/audit/walk.d.ts +109 -0
- package/dist/audit/walk.d.ts.map +1 -0
- package/dist/audit/walk.js +117 -0
- package/dist/audit/walk.js.map +1 -0
- package/dist/authentication/authenticator.d.ts +74 -0
- package/dist/authentication/authenticator.d.ts.map +1 -0
- package/dist/authentication/authenticator.js +208 -0
- package/dist/authentication/authenticator.js.map +1 -0
- package/dist/authentication/credential-resolver.d.ts +76 -0
- package/dist/authentication/credential-resolver.d.ts.map +1 -0
- package/dist/authentication/credential-resolver.js +21 -0
- package/dist/authentication/credential-resolver.js.map +1 -0
- package/dist/authentication/index.d.ts +52 -0
- package/dist/authentication/index.d.ts.map +1 -0
- package/dist/authentication/index.js +27 -0
- package/dist/authentication/index.js.map +1 -0
- package/dist/authentication/password-hasher.d.ts +41 -0
- package/dist/authentication/password-hasher.d.ts.map +1 -0
- package/dist/authentication/password-hasher.js +21 -0
- package/dist/authentication/password-hasher.js.map +1 -0
- package/dist/authentication/totp.d.ts +101 -0
- package/dist/authentication/totp.d.ts.map +1 -0
- package/dist/authentication/totp.js +151 -0
- package/dist/authentication/totp.js.map +1 -0
- package/dist/authorization/cross-tenant/grant-evaluator.d.ts +90 -0
- package/dist/authorization/cross-tenant/grant-evaluator.d.ts.map +1 -0
- package/dist/authorization/cross-tenant/grant-evaluator.js +165 -0
- package/dist/authorization/cross-tenant/grant-evaluator.js.map +1 -0
- package/dist/authorization/decision-authority.d.ts +212 -0
- package/dist/authorization/decision-authority.d.ts.map +1 -0
- package/dist/authorization/decision-authority.js +200 -0
- package/dist/authorization/decision-authority.js.map +1 -0
- package/dist/authorization/default-gates.d.ts +41 -0
- package/dist/authorization/default-gates.d.ts.map +1 -0
- package/dist/authorization/default-gates.js +47 -0
- package/dist/authorization/default-gates.js.map +1 -0
- package/dist/authorization/delegation/operation-identity.d.ts +132 -0
- package/dist/authorization/delegation/operation-identity.d.ts.map +1 -0
- package/dist/authorization/delegation/operation-identity.js +220 -0
- package/dist/authorization/delegation/operation-identity.js.map +1 -0
- package/dist/authorization/errors.d.ts +54 -0
- package/dist/authorization/errors.d.ts.map +1 -0
- package/dist/authorization/errors.js +63 -0
- package/dist/authorization/errors.js.map +1 -0
- package/dist/authorization/gates/object-type.d.ts +63 -0
- package/dist/authorization/gates/object-type.d.ts.map +1 -0
- package/dist/authorization/gates/object-type.js +177 -0
- package/dist/authorization/gates/object-type.js.map +1 -0
- package/dist/authorization/gates/tenant.d.ts +50 -0
- package/dist/authorization/gates/tenant.d.ts.map +1 -0
- package/dist/authorization/gates/tenant.js +87 -0
- package/dist/authorization/gates/tenant.js.map +1 -0
- package/dist/authorization/gates/wiring.d.ts +57 -0
- package/dist/authorization/gates/wiring.d.ts.map +1 -0
- package/dist/authorization/gates/wiring.js +56 -0
- package/dist/authorization/gates/wiring.js.map +1 -0
- package/dist/authorization/index.d.ts +136 -0
- package/dist/authorization/index.d.ts.map +1 -0
- package/dist/authorization/index.js +82 -0
- package/dist/authorization/index.js.map +1 -0
- package/dist/authorization/ports.d.ts +271 -0
- package/dist/authorization/ports.d.ts.map +1 -0
- package/dist/authorization/ports.js +36 -0
- package/dist/authorization/ports.js.map +1 -0
- package/dist/authorization/reason-codes.d.ts +75 -0
- package/dist/authorization/reason-codes.d.ts.map +1 -0
- package/dist/authorization/reason-codes.js +98 -0
- package/dist/authorization/reason-codes.js.map +1 -0
- package/dist/authorization/record/permit-tier.d.ts +57 -0
- package/dist/authorization/record/permit-tier.d.ts.map +1 -0
- package/dist/authorization/record/permit-tier.js +79 -0
- package/dist/authorization/record/permit-tier.js.map +1 -0
- package/dist/authorization/record/rule-evaluator.d.ts +116 -0
- package/dist/authorization/record/rule-evaluator.d.ts.map +1 -0
- package/dist/authorization/record/rule-evaluator.js +125 -0
- package/dist/authorization/record/rule-evaluator.js.map +1 -0
- package/dist/authorization/record/shares.d.ts +70 -0
- package/dist/authorization/record/shares.d.ts.map +1 -0
- package/dist/authorization/record/shares.js +107 -0
- package/dist/authorization/record/shares.js.map +1 -0
- package/dist/identity/broker.d.ts +59 -0
- package/dist/identity/broker.d.ts.map +1 -0
- package/dist/identity/broker.js +135 -0
- package/dist/identity/broker.js.map +1 -0
- package/dist/identity/claim-mapper.d.ts +84 -0
- package/dist/identity/claim-mapper.d.ts.map +1 -0
- package/dist/identity/claim-mapper.js +48 -0
- package/dist/identity/claim-mapper.js.map +1 -0
- package/dist/identity/index.d.ts +60 -0
- package/dist/identity/index.d.ts.map +1 -0
- package/dist/identity/index.js +24 -0
- package/dist/identity/index.js.map +1 -0
- package/dist/identity/internal-adapter.d.ts +54 -0
- package/dist/identity/internal-adapter.d.ts.map +1 -0
- package/dist/identity/internal-adapter.js +88 -0
- package/dist/identity/internal-adapter.js.map +1 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/dist/metadata/completeness.d.ts +136 -0
- package/dist/metadata/completeness.d.ts.map +1 -0
- package/dist/metadata/completeness.js +106 -0
- package/dist/metadata/completeness.js.map +1 -0
- package/dist/metadata/dependencies.d.ts +114 -0
- package/dist/metadata/dependencies.d.ts.map +1 -0
- package/dist/metadata/dependencies.js +105 -0
- package/dist/metadata/dependencies.js.map +1 -0
- package/dist/metadata/index.d.ts +153 -0
- package/dist/metadata/index.d.ts.map +1 -0
- package/dist/metadata/index.js +51 -0
- package/dist/metadata/index.js.map +1 -0
- package/dist/metadata/registry.d.ts +23 -0
- package/dist/metadata/registry.d.ts.map +1 -0
- package/dist/metadata/registry.js +204 -0
- package/dist/metadata/registry.js.map +1 -0
- package/dist/risk/evaluator.d.ts +76 -0
- package/dist/risk/evaluator.d.ts.map +1 -0
- package/dist/risk/evaluator.js +59 -0
- package/dist/risk/evaluator.js.map +1 -0
- package/dist/risk/index.d.ts +40 -0
- package/dist/risk/index.d.ts.map +1 -0
- package/dist/risk/index.js +20 -0
- package/dist/risk/index.js.map +1 -0
- package/dist/risk/risk-engine.d.ts +76 -0
- package/dist/risk/risk-engine.d.ts.map +1 -0
- package/dist/risk/risk-engine.js +287 -0
- package/dist/risk/risk-engine.js.map +1 -0
- package/dist/risk/signals.d.ts +60 -0
- package/dist/risk/signals.d.ts.map +1 -0
- package/dist/risk/signals.js +104 -0
- package/dist/risk/signals.js.map +1 -0
- package/dist/risk/thresholds.d.ts +54 -0
- package/dist/risk/thresholds.d.ts.map +1 -0
- package/dist/risk/thresholds.js +60 -0
- package/dist/risk/thresholds.js.map +1 -0
- package/dist/session/index.d.ts +47 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +20 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/lifetimes.d.ts +50 -0
- package/dist/session/lifetimes.d.ts.map +1 -0
- package/dist/session/lifetimes.js +67 -0
- package/dist/session/lifetimes.js.map +1 -0
- package/dist/session/ports.d.ts +266 -0
- package/dist/session/ports.d.ts.map +1 -0
- package/dist/session/ports.js +23 -0
- package/dist/session/ports.js.map +1 -0
- package/dist/session/session-authority.d.ts +97 -0
- package/dist/session/session-authority.d.ts.map +1 -0
- package/dist/session/session-authority.js +393 -0
- package/dist/session/session-authority.js.map +1 -0
- package/package.json +32 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Engine seam: **Session** — establishes/rotates/revokes the session.
|
|
3
|
+
*
|
|
4
|
+
* Two-tier storage (ADR-0013): durable session + refresh-token records live in
|
|
5
|
+
* MariaDB (lifecycle events only); the hot per-request session state + cached
|
|
6
|
+
* authorization facts live in Redis. This seam names the lifecycle contract only;
|
|
7
|
+
* the storage tiers, rotation, and fail-closed lookup are a later layer.
|
|
8
|
+
*
|
|
9
|
+
* TYPES / INTERFACES ONLY. ADR-0003: framework-agnostic.
|
|
10
|
+
*/
|
|
11
|
+
// --- Engine implementation behind the FROZEN seam above (this layer) ----------
|
|
12
|
+
// The storage/crypto/clock PORTs (concrete MariaDB/Redis/node:crypto adapters
|
|
13
|
+
// ship in @astram/guard — Core stays zero-dep):
|
|
14
|
+
export * from './ports.js';
|
|
15
|
+
// The lifetime config + ADR-0015 defaults + bounds validation:
|
|
16
|
+
export * from './lifetimes.js';
|
|
17
|
+
// The Session engine (establish/rotate-with-reuse-detection/revoke) + the
|
|
18
|
+
// per-request hot-read path + the `createSessionAuthority` factory:
|
|
19
|
+
export * from './session-authority.js';
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/session/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAyCH,iFAAiF;AACjF,8EAA8E;AAC9E,gDAAgD;AAChD,cAAc,YAAY,CAAC;AAC3B,+DAA+D;AAC/D,cAAc,gBAAgB,CAAC;AAC/B,0EAA0E;AAC1E,oEAAoE;AACpE,cAAc,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* **Session & token lifetime configuration** (ADR-0015). All three values are
|
|
3
|
+
* runtime-configurable per deployment; the DEFAULTS below match the ADR:
|
|
4
|
+
*
|
|
5
|
+
* - access token: **15 minutes**
|
|
6
|
+
* - refresh token: sliding **14-day idle** window
|
|
7
|
+
* - refresh token: **60-day absolute cap** (max session age regardless of activity)
|
|
8
|
+
*
|
|
9
|
+
* The idle window and the absolute cap are DISTINCT and independently enforced
|
|
10
|
+
* (ADR-0015): a session dies at the earlier of (last-refresh + idle) and
|
|
11
|
+
* (login + absolute-cap). Rotation-on-use + reuse-detection are the engine's job
|
|
12
|
+
* (`./session-authority.ts`); this file owns only the numeric config + its bounds
|
|
13
|
+
* validation.
|
|
14
|
+
*
|
|
15
|
+
* ⛔ SCOPE: this ships the DEPLOYMENT-DEFAULT config. Per-tenant override
|
|
16
|
+
* RESOLUTION (tenant policy → deployment default precedence, ADR-0015) is a later
|
|
17
|
+
* concern; a caller may construct a different {@link SessionLifetimeConfig} per
|
|
18
|
+
* tenant and hand it to a per-tenant authority, but this layer does not resolve
|
|
19
|
+
* tenant policy.
|
|
20
|
+
*
|
|
21
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* The three lifetime values the Session engine enforces (ADR-0015). All in
|
|
25
|
+
* UTC-epoch milliseconds of duration. Validated at construction by
|
|
26
|
+
* {@link resolveLifetimes}: `accessTtlMs ≤ refreshIdleMs ≤ refreshAbsoluteCapMs`,
|
|
27
|
+
* and every value strictly positive.
|
|
28
|
+
*/
|
|
29
|
+
export interface SessionLifetimeConfig {
|
|
30
|
+
/** Access-token TTL (ADR-0015 default 15 min). Bounds hot-record TTL + revocation latency. */
|
|
31
|
+
readonly accessTtlMs: number;
|
|
32
|
+
/** Sliding refresh idle window (ADR-0015 default 14 days). Reset on each rotation. */
|
|
33
|
+
readonly refreshIdleMs: number;
|
|
34
|
+
/** Absolute session cap (ADR-0015 default 60 days). Measured from login; never reset. */
|
|
35
|
+
readonly refreshAbsoluteCapMs: number;
|
|
36
|
+
}
|
|
37
|
+
/** The ADR-0015 documented defaults (15 min / 14 d idle / 60 d cap). */
|
|
38
|
+
export declare const DEFAULT_SESSION_LIFETIMES: SessionLifetimeConfig;
|
|
39
|
+
/**
|
|
40
|
+
* Validate + freeze a lifetime config, applying ADR-0015 defaults for any omitted
|
|
41
|
+
* field. Enforces the sane-bounds invariant from ADR-0015 ("access TTL not longer
|
|
42
|
+
* than the refresh idle window") extended to the full chain:
|
|
43
|
+
*
|
|
44
|
+
* 0 < accessTtlMs ≤ refreshIdleMs ≤ refreshAbsoluteCapMs
|
|
45
|
+
*
|
|
46
|
+
* Throws LOUD on a nonsensical config (this is construction-time, not a hot path —
|
|
47
|
+
* a misconfiguration must fail the boot, never silently ship a broken policy).
|
|
48
|
+
*/
|
|
49
|
+
export declare function resolveLifetimes(partial?: Partial<SessionLifetimeConfig>): SessionLifetimeConfig;
|
|
50
|
+
//# sourceMappingURL=lifetimes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifetimes.d.ts","sourceRoot":"","sources":["../../src/session/lifetimes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAMH;;;;;GAKG;AACH,MAAM,WAAW,qBAAqB;IACpC,8FAA8F;IAC9F,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,sFAAsF;IACtF,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,yFAAyF;IACzF,QAAQ,CAAC,oBAAoB,EAAE,MAAM,CAAC;CACvC;AAED,wEAAwE;AACxE,eAAO,MAAM,yBAAyB,EAAE,qBAItC,CAAC;AAOH;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,GACvC,qBAAqB,CA6BvB"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* **Session & token lifetime configuration** (ADR-0015). All three values are
|
|
3
|
+
* runtime-configurable per deployment; the DEFAULTS below match the ADR:
|
|
4
|
+
*
|
|
5
|
+
* - access token: **15 minutes**
|
|
6
|
+
* - refresh token: sliding **14-day idle** window
|
|
7
|
+
* - refresh token: **60-day absolute cap** (max session age regardless of activity)
|
|
8
|
+
*
|
|
9
|
+
* The idle window and the absolute cap are DISTINCT and independently enforced
|
|
10
|
+
* (ADR-0015): a session dies at the earlier of (last-refresh + idle) and
|
|
11
|
+
* (login + absolute-cap). Rotation-on-use + reuse-detection are the engine's job
|
|
12
|
+
* (`./session-authority.ts`); this file owns only the numeric config + its bounds
|
|
13
|
+
* validation.
|
|
14
|
+
*
|
|
15
|
+
* ⛔ SCOPE: this ships the DEPLOYMENT-DEFAULT config. Per-tenant override
|
|
16
|
+
* RESOLUTION (tenant policy → deployment default precedence, ADR-0015) is a later
|
|
17
|
+
* concern; a caller may construct a different {@link SessionLifetimeConfig} per
|
|
18
|
+
* tenant and hand it to a per-tenant authority, but this layer does not resolve
|
|
19
|
+
* tenant policy.
|
|
20
|
+
*
|
|
21
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
|
|
22
|
+
*/
|
|
23
|
+
/** Milliseconds in one minute / day (local, node-free constants). */
|
|
24
|
+
const MINUTE_MS = 60_000;
|
|
25
|
+
const DAY_MS = 24 * 60 * MINUTE_MS;
|
|
26
|
+
/** The ADR-0015 documented defaults (15 min / 14 d idle / 60 d cap). */
|
|
27
|
+
export const DEFAULT_SESSION_LIFETIMES = Object.freeze({
|
|
28
|
+
accessTtlMs: 15 * MINUTE_MS,
|
|
29
|
+
refreshIdleMs: 14 * DAY_MS,
|
|
30
|
+
refreshAbsoluteCapMs: 60 * DAY_MS,
|
|
31
|
+
});
|
|
32
|
+
/** A finite, strictly-positive integer millisecond duration. */
|
|
33
|
+
function isPositiveDuration(v) {
|
|
34
|
+
return typeof v === 'number' && Number.isFinite(v) && v > 0;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Validate + freeze a lifetime config, applying ADR-0015 defaults for any omitted
|
|
38
|
+
* field. Enforces the sane-bounds invariant from ADR-0015 ("access TTL not longer
|
|
39
|
+
* than the refresh idle window") extended to the full chain:
|
|
40
|
+
*
|
|
41
|
+
* 0 < accessTtlMs ≤ refreshIdleMs ≤ refreshAbsoluteCapMs
|
|
42
|
+
*
|
|
43
|
+
* Throws LOUD on a nonsensical config (this is construction-time, not a hot path —
|
|
44
|
+
* a misconfiguration must fail the boot, never silently ship a broken policy).
|
|
45
|
+
*/
|
|
46
|
+
export function resolveLifetimes(partial) {
|
|
47
|
+
const accessTtlMs = partial?.accessTtlMs ?? DEFAULT_SESSION_LIFETIMES.accessTtlMs;
|
|
48
|
+
const refreshIdleMs = partial?.refreshIdleMs ?? DEFAULT_SESSION_LIFETIMES.refreshIdleMs;
|
|
49
|
+
const refreshAbsoluteCapMs = partial?.refreshAbsoluteCapMs ?? DEFAULT_SESSION_LIFETIMES.refreshAbsoluteCapMs;
|
|
50
|
+
if (!isPositiveDuration(accessTtlMs)) {
|
|
51
|
+
throw new Error('SessionLifetimeConfig: accessTtlMs must be a positive, finite number of ms');
|
|
52
|
+
}
|
|
53
|
+
if (!isPositiveDuration(refreshIdleMs)) {
|
|
54
|
+
throw new Error('SessionLifetimeConfig: refreshIdleMs must be a positive, finite number of ms');
|
|
55
|
+
}
|
|
56
|
+
if (!isPositiveDuration(refreshAbsoluteCapMs)) {
|
|
57
|
+
throw new Error('SessionLifetimeConfig: refreshAbsoluteCapMs must be a positive, finite number of ms');
|
|
58
|
+
}
|
|
59
|
+
if (accessTtlMs > refreshIdleMs) {
|
|
60
|
+
throw new Error('SessionLifetimeConfig: accessTtlMs must be ≤ refreshIdleMs (ADR-0015 sane-bounds)');
|
|
61
|
+
}
|
|
62
|
+
if (refreshIdleMs > refreshAbsoluteCapMs) {
|
|
63
|
+
throw new Error('SessionLifetimeConfig: refreshIdleMs must be ≤ refreshAbsoluteCapMs (ADR-0015 sane-bounds)');
|
|
64
|
+
}
|
|
65
|
+
return Object.freeze({ accessTtlMs, refreshIdleMs, refreshAbsoluteCapMs });
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=lifetimes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lifetimes.js","sourceRoot":"","sources":["../../src/session/lifetimes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,qEAAqE;AACrE,MAAM,SAAS,GAAG,MAAM,CAAC;AACzB,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,SAAS,CAAC;AAiBnC,wEAAwE;AACxE,MAAM,CAAC,MAAM,yBAAyB,GAA0B,MAAM,CAAC,MAAM,CAAC;IAC5E,WAAW,EAAE,EAAE,GAAG,SAAS;IAC3B,aAAa,EAAE,EAAE,GAAG,MAAM;IAC1B,oBAAoB,EAAE,EAAE,GAAG,MAAM;CAClC,CAAC,CAAC;AAEH,gEAAgE;AAChE,SAAS,kBAAkB,CAAC,CAAU;IACpC,OAAO,OAAO,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,gBAAgB,CAC9B,OAAwC;IAExC,MAAM,WAAW,GAAG,OAAO,EAAE,WAAW,IAAI,yBAAyB,CAAC,WAAW,CAAC;IAClF,MAAM,aAAa,GAAG,OAAO,EAAE,aAAa,IAAI,yBAAyB,CAAC,aAAa,CAAC;IACxF,MAAM,oBAAoB,GACxB,OAAO,EAAE,oBAAoB,IAAI,yBAAyB,CAAC,oBAAoB,CAAC;IAElF,IAAI,CAAC,kBAAkB,CAAC,WAAW,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,4EAA4E,CAAC,CAAC;IAChG,CAAC;IACD,IAAI,CAAC,kBAAkB,CAAC,aAAa,CAAC,EAAE,CAAC;QACvC,MAAM,IAAI,KAAK,CAAC,8EAA8E,CAAC,CAAC;IAClG,CAAC;IACD,IAAI,CAAC,kBAAkB,CAAC,oBAAoB,CAAC,EAAE,CAAC;QAC9C,MAAM,IAAI,KAAK,CACb,qFAAqF,CACtF,CAAC;IACJ,CAAC;IACD,IAAI,WAAW,GAAG,aAAa,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CACb,mFAAmF,CACpF,CAAC;IACJ,CAAC;IACD,IAAI,aAAa,GAAG,oBAAoB,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,4FAA4F,CAC7F,CAAC;IACJ,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,aAAa,EAAE,oBAAoB,EAAE,CAAC,CAAC;AAC7E,CAAC"}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Session engine **PORTS** — the framework-agnostic contracts the
|
|
3
|
+
* SessionAuthority engine (`./session-authority.ts`) uses to reach the two
|
|
4
|
+
* storage tiers (ADR-0013), mint/hash tokens, and read the clock, WITHOUT the
|
|
5
|
+
* engine ever knowing which DB / cache / crypto backs them.
|
|
6
|
+
*
|
|
7
|
+
* ADR-0003 (zero-dep Core): every concrete adapter (MariaDB, Redis, node:crypto)
|
|
8
|
+
* is a real runtime dependency and CANNOT live here. The Core defines only these
|
|
9
|
+
* interfaces; `@astram/guard` ships the concretes and INJECTS them into
|
|
10
|
+
* `createSessionAuthority(...)`. This keeps `@astram/core` at ZERO runtime
|
|
11
|
+
* dependencies (the load-bearing invariant) while the substrate stays swappable.
|
|
12
|
+
*
|
|
13
|
+
* ⛔ SECURITY — the engine NEVER logs a token and NEVER stores a RAW refresh
|
|
14
|
+
* token. A refresh token is stored ONLY as its keyed hash (via
|
|
15
|
+
* {@link RefreshTokenHasher}); the durable {@link SessionRecordStore} therefore
|
|
16
|
+
* only ever receives a HASH. No port method returns a raw token except the one
|
|
17
|
+
* mint operation ({@link TokenGenerator.mint}); the raw refresh token leaves the
|
|
18
|
+
* engine EXACTLY ONCE, in the establishment result, never persisted.
|
|
19
|
+
*
|
|
20
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
|
|
21
|
+
*/
|
|
22
|
+
import type { SessionStatus } from './index.js';
|
|
23
|
+
/**
|
|
24
|
+
* The durable session record persisted at login (ADR-0013). Principal + tenant +
|
|
25
|
+
* issuance/expiry + lifecycle status. `issuedAtMs` anchors the ADR-0015 absolute
|
|
26
|
+
* cap (the cap is measured from session issuance, independent of refresh activity).
|
|
27
|
+
*/
|
|
28
|
+
export interface SessionRecord {
|
|
29
|
+
readonly sessionId: string;
|
|
30
|
+
readonly principalId: string;
|
|
31
|
+
readonly tenantId: string;
|
|
32
|
+
readonly status: SessionStatus;
|
|
33
|
+
/** UTC epoch ms — the platform sets UTC times explicitly. */
|
|
34
|
+
readonly issuedAtMs: number;
|
|
35
|
+
/** Absolute-cap expiry (ADR-0015): issuedAtMs + refreshAbsoluteCapMs. */
|
|
36
|
+
readonly absoluteExpiresAtMs: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A durable refresh-token record with rotation lineage (ADR-0013/0015). The token
|
|
40
|
+
* itself is NEVER stored — only its keyed hash. `lineageId` chains every rotation
|
|
41
|
+
* of one login together; `prevRefreshId` points at the record this one rotated
|
|
42
|
+
* OUT of (null for the first token of a lineage). Reuse detection walks the
|
|
43
|
+
* lineage: replaying a token whose record is already `rotated`/`revoked` kills the
|
|
44
|
+
* WHOLE lineage.
|
|
45
|
+
*/
|
|
46
|
+
export interface RefreshTokenRecord {
|
|
47
|
+
readonly refreshId: string;
|
|
48
|
+
readonly sessionId: string;
|
|
49
|
+
readonly lineageId: string;
|
|
50
|
+
/** The record this token superseded (null = first in the lineage). */
|
|
51
|
+
readonly prevRefreshId: string | null;
|
|
52
|
+
/**
|
|
53
|
+
* The KEYED HASH of the raw refresh token (never the raw token). Produced by
|
|
54
|
+
* {@link RefreshTokenHasher}; the store matches on this in `findRefreshByHash`.
|
|
55
|
+
*/
|
|
56
|
+
readonly tokenHash: string;
|
|
57
|
+
readonly status: SessionStatus;
|
|
58
|
+
/** UTC epoch ms this refresh record was issued (drives the idle window). */
|
|
59
|
+
readonly issuedAtMs: number;
|
|
60
|
+
/** Sliding idle expiry (ADR-0015): issuedAtMs + refreshIdleMs. */
|
|
61
|
+
readonly idleExpiresAtMs: number;
|
|
62
|
+
}
|
|
63
|
+
/** Input to {@link SessionRecordStore.createSession} (all fields caller-supplied). */
|
|
64
|
+
export interface CreateSessionInput {
|
|
65
|
+
readonly sessionId: string;
|
|
66
|
+
readonly principalId: string;
|
|
67
|
+
readonly tenantId: string;
|
|
68
|
+
readonly issuedAtMs: number;
|
|
69
|
+
readonly absoluteExpiresAtMs: number;
|
|
70
|
+
}
|
|
71
|
+
/** Input to {@link SessionRecordStore.insertRefreshRecord} — a HASH, never a raw token. */
|
|
72
|
+
export interface InsertRefreshInput {
|
|
73
|
+
readonly refreshId: string;
|
|
74
|
+
readonly sessionId: string;
|
|
75
|
+
readonly lineageId: string;
|
|
76
|
+
readonly prevRefreshId: string | null;
|
|
77
|
+
/** ⛔ HASH ONLY — the caller (engine) hashes the raw token before it ever reaches here. */
|
|
78
|
+
readonly tokenHash: string;
|
|
79
|
+
readonly issuedAtMs: number;
|
|
80
|
+
readonly idleExpiresAtMs: number;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The **durable session/refresh-token store** PORT (MariaDB adapter). Written and
|
|
84
|
+
* read ONLY at lifecycle events (ADR-0013) — the per-request hot path never calls
|
|
85
|
+
* this. Concrete: `MariaDbSessionRecordStore` in `@astram/guard`.
|
|
86
|
+
*
|
|
87
|
+
* ⛔ It only ever receives a token HASH (never a raw token). Idempotency / natural
|
|
88
|
+
* keys / SQL are the adapter's concern; the engine treats every method as an
|
|
89
|
+
* opaque durable operation.
|
|
90
|
+
*/
|
|
91
|
+
export interface SessionRecordStore {
|
|
92
|
+
/** Create the authoritative session record at login. */
|
|
93
|
+
createSession(input: CreateSessionInput): Promise<SessionRecord>;
|
|
94
|
+
/**
|
|
95
|
+
* Insert a refresh-token record (login: first of a lineage; refresh: same
|
|
96
|
+
* lineage, `prevRefreshId` set). Stores the token HASH, never the raw token.
|
|
97
|
+
*/
|
|
98
|
+
insertRefreshRecord(input: InsertRefreshInput): Promise<RefreshTokenRecord>;
|
|
99
|
+
/**
|
|
100
|
+
* Look up a refresh record by its token HASH. Returns `null` when NO record
|
|
101
|
+
* matches (an unknown/forged token) — the engine treats `null` as fail-closed
|
|
102
|
+
* reject. (Contrast a spent-but-known token: it is FOUND with a non-`active`
|
|
103
|
+
* status → reuse detection.)
|
|
104
|
+
*/
|
|
105
|
+
findRefreshByHash(tokenHash: string): Promise<RefreshTokenRecord | null>;
|
|
106
|
+
/**
|
|
107
|
+
* Look up the durable session record by id. Read at REFRESH (a lifecycle event)
|
|
108
|
+
* to enforce the ADR-0015 absolute cap (measured from `issuedAtMs`) and to
|
|
109
|
+
* rebuild the hot record from the authoritative identity — this is the
|
|
110
|
+
* silent-recovery source of truth. Returns `null` if the session is gone
|
|
111
|
+
* (engine fails closed).
|
|
112
|
+
*/
|
|
113
|
+
findSessionById(sessionId: string): Promise<SessionRecord | null>;
|
|
114
|
+
/**
|
|
115
|
+
* ⛔ ATOMIC ROTATION CLAIM (compare-and-set) — the single-winner primitive that
|
|
116
|
+
* makes reuse detection correct under CONCURRENCY. Atomically transition the
|
|
117
|
+
* record `active → rotated` and report whether THIS caller performed the
|
|
118
|
+
* transition:
|
|
119
|
+
* - returns `true` iff the record was `active` and THIS call flipped it to
|
|
120
|
+
* `rotated` (this caller WON the race and may mint the successor);
|
|
121
|
+
* - returns `false` iff the record was NOT `active` at claim time (already
|
|
122
|
+
* rotated/revoked/expired by a racing caller or a prior use) — this caller
|
|
123
|
+
* LOST; the engine treats a lost claim as a spent-token REPLAY and kills the
|
|
124
|
+
* lineage.
|
|
125
|
+
*
|
|
126
|
+
* The transition MUST be atomic w.r.t. concurrent callers (the DB adapter does a
|
|
127
|
+
* conditional `UPDATE ... WHERE status='active'` and returns `affectedRows === 1`
|
|
128
|
+
* — the database is the true arbiter). Two near-simultaneous `rotate` calls on
|
|
129
|
+
* ONE token therefore yield exactly ONE `true`; every other caller gets `false`.
|
|
130
|
+
*
|
|
131
|
+
* The engine gates on STRICT `=== true` (a truthy non-boolean is NOT a win —
|
|
132
|
+
* never fail open). Idempotent-safe: a second claim on an already-rotated record
|
|
133
|
+
* simply returns `false`.
|
|
134
|
+
*/
|
|
135
|
+
markRotatedIfActive(refreshId: string): Promise<boolean>;
|
|
136
|
+
/** Mark a specific refresh record `revoked`. */
|
|
137
|
+
markRevoked(refreshId: string): Promise<void>;
|
|
138
|
+
/**
|
|
139
|
+
* ⛔ THE REUSE KILL-SWITCH (ADR-0015): revoke EVERY refresh record AND the
|
|
140
|
+
* session in a lineage. Called on reuse detection (a replayed spent token) so
|
|
141
|
+
* the whole chain — including the legitimate holder's live token — is dead and
|
|
142
|
+
* cannot refresh again. Idempotent.
|
|
143
|
+
*/
|
|
144
|
+
revokeLineage(lineageId: string): Promise<void>;
|
|
145
|
+
/**
|
|
146
|
+
* Revoke a session (logout / admin). Marks the durable record `revoked` and
|
|
147
|
+
* refuses all future refreshes of its lineage. Idempotent.
|
|
148
|
+
*/
|
|
149
|
+
revokeSession(sessionId: string): Promise<void>;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The hot access-session state cached in Redis for O(1) per-request lookup
|
|
153
|
+
* (ADR-0013). Carries the principal identity the request needs PLUS the ADR-0011
|
|
154
|
+
* permission-version + an OPAQUE cached-facts SLOT.
|
|
155
|
+
*
|
|
156
|
+
* ⛔ SCOPE: this engine only STORES / RE-POPULATES / DELETES this record. It does
|
|
157
|
+
* NOT read `cachedFacts` for a decision, version-compare, or reload — that is
|
|
158
|
+
* Authorization's job (ADR-0011, step 8). `permissionsVersion` starts at 0 and is
|
|
159
|
+
* an opaque counter to this layer; `cachedFacts` is an opaque blob this layer
|
|
160
|
+
* never interprets.
|
|
161
|
+
*/
|
|
162
|
+
export interface HotSessionState {
|
|
163
|
+
readonly sessionId: string;
|
|
164
|
+
readonly principalId: string;
|
|
165
|
+
readonly tenantId: string;
|
|
166
|
+
/** ADR-0011 per-principal/tenant version counter. This layer stores it; Authorization compares it. */
|
|
167
|
+
readonly permissionsVersion: number;
|
|
168
|
+
/**
|
|
169
|
+
* ADR-0011 cached coarse-grant facts — an OPAQUE slot. Populated at
|
|
170
|
+
* establish/rotate; CONSUMED by Authorization later. `null` = facts not yet
|
|
171
|
+
* projected (Authorization will project + reload). This layer never reads inside it.
|
|
172
|
+
*/
|
|
173
|
+
readonly cachedFacts: unknown;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The outcome kinds a hot-store read can yield. CRITICAL (ADR-0013/0016): the
|
|
177
|
+
* store MUST distinguish "cannot answer" (Redis down / timeout — the request must
|
|
178
|
+
* fail closed, never fall back to the durable tier) from "absent" (Redis is up and
|
|
179
|
+
* simply has no such key — a normal miss). Conflating them either fails open or
|
|
180
|
+
* logs everyone out on a blip.
|
|
181
|
+
*/
|
|
182
|
+
export type HotSessionOutcome = {
|
|
183
|
+
readonly kind: 'found';
|
|
184
|
+
readonly state: HotSessionState;
|
|
185
|
+
} | {
|
|
186
|
+
readonly kind: 'absent';
|
|
187
|
+
} | {
|
|
188
|
+
readonly kind: 'unavailable';
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* The **hot session store** PORT (Redis adapter). Serves the per-request path and
|
|
192
|
+
* is (re)populated at lifecycle events. Concrete: `RedisHotSessionStore` in
|
|
193
|
+
* `@astram/guard`.
|
|
194
|
+
*
|
|
195
|
+
* ⛔ `getHotSession` MUST return `{ kind: 'unavailable' }` (not throw silently, not
|
|
196
|
+
* `absent`) when it genuinely cannot answer, so the engine fails CLOSED and never
|
|
197
|
+
* serves the hot path from the durable tier (ADR-0013).
|
|
198
|
+
*/
|
|
199
|
+
export interface HotSessionStore {
|
|
200
|
+
/** Cache the hot session state under `key` with a TTL (ms). Lifecycle-event only. */
|
|
201
|
+
putHotSession(key: string, state: HotSessionState, ttlMs: number): Promise<void>;
|
|
202
|
+
/**
|
|
203
|
+
* Read the hot session for `key`. Returns a discriminated outcome that
|
|
204
|
+
* distinguishes found / absent / unavailable (see {@link HotSessionOutcome}).
|
|
205
|
+
*/
|
|
206
|
+
getHotSession(key: string): Promise<HotSessionOutcome>;
|
|
207
|
+
/** Delete the hot session for `key` (logout / revoke). */
|
|
208
|
+
deleteHotSession(key: string): Promise<void>;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Mints an opaque, unguessable random token (access or refresh). Concrete:
|
|
212
|
+
* `CryptoTokenGenerator` (node:crypto `randomBytes`) in `@astram/guard`. The
|
|
213
|
+
* engine treats the result as opaque bytes-as-string; it never parses it.
|
|
214
|
+
*
|
|
215
|
+
* ⛔ The token is a secret. The generator MUST NOT log it; the engine returns the
|
|
216
|
+
* raw refresh token to the caller EXACTLY ONCE and otherwise only ever handles its
|
|
217
|
+
* hash.
|
|
218
|
+
*/
|
|
219
|
+
export interface TokenGenerator {
|
|
220
|
+
/** Return a fresh, high-entropy opaque token string. */
|
|
221
|
+
mint(): string;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Produces a stable keyed hash of a raw refresh token for storage/lookup — a
|
|
225
|
+
* keyed hash / HMAC so a leaked DB cannot be brute-forced offline as easily as a
|
|
226
|
+
* bare digest. Concrete: `Sha256RefreshTokenHasher` (HMAC-SHA-256) in
|
|
227
|
+
* `@astram/guard`.
|
|
228
|
+
*
|
|
229
|
+
* ⛔ The engine stores ONLY this hash and looks up ONLY by this hash — the raw
|
|
230
|
+
* token is never persisted. `hash` MUST be deterministic (same input → same
|
|
231
|
+
* output) so `findRefreshByHash` can match, and MUST NOT log its input.
|
|
232
|
+
*/
|
|
233
|
+
export interface RefreshTokenHasher {
|
|
234
|
+
/** Deterministic keyed hash of the raw token. Never logs the raw token. */
|
|
235
|
+
hash(rawToken: string): string;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The clock PORT (UTC epoch ms). Injected so expiry/rotation is deterministic in
|
|
239
|
+
* tests. Concrete: `SystemClock` (`Date.now`) in `@astram/guard`.
|
|
240
|
+
*/
|
|
241
|
+
export interface Clock {
|
|
242
|
+
/** Current time as UTC epoch milliseconds. */
|
|
243
|
+
nowMs(): number;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* OPTIONAL device-trust HOOK (ADR-0015 mentions device/client metadata). This
|
|
247
|
+
* layer ships only the SLOT — a no-op default; it does NOT implement device-trust
|
|
248
|
+
* policy (fingerprint binding, trusted-device lists, step-up-on-new-device). A
|
|
249
|
+
* later layer may supply a real hook. The engine calls it, if present, but treats
|
|
250
|
+
* a `false`/throw as "not additionally trusted" and NEVER lets the hook grant a
|
|
251
|
+
* session it otherwise wouldn't (it is advisory, never a bypass).
|
|
252
|
+
*/
|
|
253
|
+
export interface DeviceTrustHook {
|
|
254
|
+
/**
|
|
255
|
+
* Advisory device-trust check for a lifecycle event. Returns whether the device
|
|
256
|
+
* is additionally trusted. Default impl (none supplied) is treated as a no-op.
|
|
257
|
+
*/
|
|
258
|
+
isTrusted?(context: DeviceTrustContext): Promise<boolean> | boolean;
|
|
259
|
+
}
|
|
260
|
+
/** Minimal context handed to the device-trust hook (kept intentionally thin). */
|
|
261
|
+
export interface DeviceTrustContext {
|
|
262
|
+
readonly principalId: string;
|
|
263
|
+
readonly tenantId: string;
|
|
264
|
+
readonly sessionId: string;
|
|
265
|
+
}
|
|
266
|
+
//# sourceMappingURL=ports.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ports.d.ts","sourceRoot":"","sources":["../../src/session/ports.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAQhD;;;;GAIG;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,kBAAkB;IACjC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Session engine **PORTS** — the framework-agnostic contracts the
|
|
3
|
+
* SessionAuthority engine (`./session-authority.ts`) uses to reach the two
|
|
4
|
+
* storage tiers (ADR-0013), mint/hash tokens, and read the clock, WITHOUT the
|
|
5
|
+
* engine ever knowing which DB / cache / crypto backs them.
|
|
6
|
+
*
|
|
7
|
+
* ADR-0003 (zero-dep Core): every concrete adapter (MariaDB, Redis, node:crypto)
|
|
8
|
+
* is a real runtime dependency and CANNOT live here. The Core defines only these
|
|
9
|
+
* interfaces; `@astram/guard` ships the concretes and INJECTS them into
|
|
10
|
+
* `createSessionAuthority(...)`. This keeps `@astram/core` at ZERO runtime
|
|
11
|
+
* dependencies (the load-bearing invariant) while the substrate stays swappable.
|
|
12
|
+
*
|
|
13
|
+
* ⛔ SECURITY — the engine NEVER logs a token and NEVER stores a RAW refresh
|
|
14
|
+
* token. A refresh token is stored ONLY as its keyed hash (via
|
|
15
|
+
* {@link RefreshTokenHasher}); the durable {@link SessionRecordStore} therefore
|
|
16
|
+
* only ever receives a HASH. No port method returns a raw token except the one
|
|
17
|
+
* mint operation ({@link TokenGenerator.mint}); the raw refresh token leaves the
|
|
18
|
+
* engine EXACTLY ONCE, in the establishment result, never persisted.
|
|
19
|
+
*
|
|
20
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
|
|
21
|
+
*/
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=ports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ports.js","sourceRoot":"","sources":["../../src/session/ports.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Session **engine** — the body behind the FROZEN {@link SessionAuthority}
|
|
3
|
+
* seam (`./index.ts`). Establishes a session on login, ROTATES the refresh token
|
|
4
|
+
* on refresh (with THEFT DETECTION), and revokes on logout. Orchestrates the
|
|
5
|
+
* two-tier hybrid storage (ADR-0013): durable MariaDB records written ONLY at
|
|
6
|
+
* lifecycle events, hot Redis state (re)populated at those same events and served
|
|
7
|
+
* per-request WITHOUT ever touching the DB.
|
|
8
|
+
*
|
|
9
|
+
* Mirrors the Authentication engine (`../authentication/authenticator.ts`)
|
|
10
|
+
* EXACTLY: a pure `createSessionAuthority(...)` factory over INJECTED ports, a
|
|
11
|
+
* stateless impl, STRICT `=== true` gates over verdict-returning ports, and a
|
|
12
|
+
* top-level try/catch on every public method → a GENERIC fail-closed reject that
|
|
13
|
+
* NEVER leaks a token, a hash, or resource existence.
|
|
14
|
+
*
|
|
15
|
+
* ── THE SECURITY CORE — reuse detection → lineage kill (ADR-0013/0015):
|
|
16
|
+
* Every refresh rotates the token (mint a new pair, mark the old `rotated`, same
|
|
17
|
+
* lineage). A refresh token presented AFTER it was rotated out (or after the
|
|
18
|
+
* lineage was revoked) is a REPLAY — the sign of a stolen token. On that signal
|
|
19
|
+
* the engine calls `recordStore.revokeLineage(lineageId)`, killing EVERY refresh
|
|
20
|
+
* record AND the session in the chain (including the legitimate holder's live
|
|
21
|
+
* token), then rejects. Both the thief and the victim are logged out; the
|
|
22
|
+
* attacker cannot ride a stolen token forward, and the reuse is durably recorded.
|
|
23
|
+
*
|
|
24
|
+
* ── SILENT RECOVERY (ADR-0013): rotate re-populates Redis from the DURABLE record
|
|
25
|
+
* every time. A Redis loss (restart/eviction) is recovered at the user's next
|
|
26
|
+
* refresh with no forced re-login — the durable tier is the source of truth for
|
|
27
|
+
* rotation, and the hot tier is rebuilt from it.
|
|
28
|
+
*
|
|
29
|
+
* ── HOT PATH NEVER TOUCHES THE DB (ADR-0013): the per-request read path
|
|
30
|
+
* ({@link readHotSession}) calls ONLY the hot store; it makes ZERO durable-store
|
|
31
|
+
* calls, and fails CLOSED if the hot store cannot answer (never falls back to
|
|
32
|
+
* MariaDB, ADR-0013/0016).
|
|
33
|
+
*
|
|
34
|
+
* ── PERM-VERSION / FACTS SLOT (ADR-0011): establish seeds a hot record with
|
|
35
|
+
* `permissionsVersion: 0` and an OPAQUE `cachedFacts: null` slot; rotate carries
|
|
36
|
+
* the version forward. This layer only STORES/RE-POPULATES/INVALIDATES the slot —
|
|
37
|
+
* it does NOT project fact content, version-compare, or reload (that is
|
|
38
|
+
* Authorization, step 8).
|
|
39
|
+
*
|
|
40
|
+
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
|
|
41
|
+
*/
|
|
42
|
+
import type { SessionAuthority } from './index.js';
|
|
43
|
+
import type { SessionLifetimeConfig } from './lifetimes.js';
|
|
44
|
+
import type { Clock, DeviceTrustHook, HotSessionState, HotSessionStore, RefreshTokenHasher, SessionRecordStore, TokenGenerator } from './ports.js';
|
|
45
|
+
/**
|
|
46
|
+
* A single GENERIC failure for every rejected session-lifecycle path (unknown
|
|
47
|
+
* token, replay, expiry, port outage). It NEVER distinguishes them and NEVER
|
|
48
|
+
* carries a token/hash — mirrors the Authentication engine's anti-enumeration
|
|
49
|
+
* reason discipline. Thrown as a typed error so a caller gets a uniform rejection
|
|
50
|
+
* rather than a raw 500 that could leak store internals.
|
|
51
|
+
*/
|
|
52
|
+
export declare const SESSION_REJECTED_REASON = "session_rejected";
|
|
53
|
+
/** The one error type the engine rejects with — always the generic reason. */
|
|
54
|
+
export declare class SessionRejectedError extends Error {
|
|
55
|
+
constructor();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The per-request HOT READ path (ADR-0013). Deliberately a FREE function, NOT a
|
|
59
|
+
* SessionAuthority method — it is not a lifecycle event. It reads the hot store
|
|
60
|
+
* ALONE and makes ZERO durable-store calls. Fails CLOSED (returns `null`) when the
|
|
61
|
+
* hot store cannot answer (`unavailable`) — NEVER falls back to MariaDB
|
|
62
|
+
* (ADR-0013/0016). `absent` (a genuine miss) also returns `null` but is a distinct
|
|
63
|
+
* outcome the caller/enforcement can classify (ADR-0016 transient-vs-deny is
|
|
64
|
+
* Authorization's job; this only surfaces the fail-closed result).
|
|
65
|
+
*
|
|
66
|
+
* Returns the strict-`true` gate result: the {@link HotSessionState} ONLY on a
|
|
67
|
+
* genuine `found`; `null` on both `absent` and `unavailable` (fail-closed).
|
|
68
|
+
*/
|
|
69
|
+
export declare function readHotSession(hotStore: HotSessionStore, sessionId: string): Promise<HotSessionState | null>;
|
|
70
|
+
/** Options for {@link createSessionAuthority} (mirrors CreateAuthenticatorOptions). */
|
|
71
|
+
export interface CreateSessionAuthorityOptions {
|
|
72
|
+
/** Durable session/refresh-token store PORT (MariaDB adapter in @astram/guard). */
|
|
73
|
+
readonly recordStore: SessionRecordStore;
|
|
74
|
+
/** Hot session store PORT (Redis adapter in @astram/guard). */
|
|
75
|
+
readonly hotStore: HotSessionStore;
|
|
76
|
+
/** Opaque-token minter PORT (node:crypto adapter). */
|
|
77
|
+
readonly tokenGenerator: TokenGenerator;
|
|
78
|
+
/** Refresh-token keyed hasher PORT (HMAC adapter). */
|
|
79
|
+
readonly refreshHasher: RefreshTokenHasher;
|
|
80
|
+
/** Clock PORT (UTC epoch ms) — injected for deterministic expiry tests. */
|
|
81
|
+
readonly clock: Clock;
|
|
82
|
+
/**
|
|
83
|
+
* Lifetime config (ADR-0015). Omitted fields default to
|
|
84
|
+
* {@link DEFAULT_SESSION_LIFETIMES}; validated (bounds) at construction.
|
|
85
|
+
*/
|
|
86
|
+
readonly lifetimes?: Partial<SessionLifetimeConfig>;
|
|
87
|
+
/** OPTIONAL device-trust HOOK (no-op slot — not implemented this layer). */
|
|
88
|
+
readonly deviceTrust?: DeviceTrustHook;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Wire the injected ports and return an object implementing the FROZEN
|
|
92
|
+
* {@link SessionAuthority}. The engine's public entry point (mirrors
|
|
93
|
+
* `createAuthenticator`). Validates the lifetime config eagerly at construction
|
|
94
|
+
* (throws LOUD on a nonsensical config — construction-time, not a hot path).
|
|
95
|
+
*/
|
|
96
|
+
export declare function createSessionAuthority(options: CreateSessionAuthorityOptions): SessionAuthority;
|
|
97
|
+
//# sourceMappingURL=session-authority.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-authority.d.ts","sourceRoot":"","sources":["../../src/session/session-authority.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAGH,OAAO,KAAK,EAEV,gBAAgB,EAGjB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAE5D,OAAO,KAAK,EACV,KAAK,EACL,eAAe,EACf,eAAe,EACf,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,qBAAqB,CAAC;AAE1D,8EAA8E;AAC9E,qBAAa,oBAAqB,SAAQ,KAAK;;CAK9C;AAoVD;;;;;;;;;;;GAWG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,eAAe,EACzB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAqBjC;AAED,uFAAuF;AACvF,MAAM,WAAW,6BAA6B;IAC5C,mFAAmF;IACnF,QAAQ,CAAC,WAAW,EAAE,kBAAkB,CAAC;IACzC,+DAA+D;IAC/D,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAC;IACnC,sDAAsD;IACtD,QAAQ,CAAC,cAAc,EAAE,cAAc,CAAC;IACxC,sDAAsD;IACtD,QAAQ,CAAC,aAAa,EAAE,kBAAkB,CAAC;IAC3C,2EAA2E;IAC3E,QAAQ,CA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