@astram/core 1.4.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/audit/index.d.ts +1 -1
- package/dist/audit/index.d.ts.map +1 -1
- package/dist/audit/index.js.map +1 -1
- package/dist/authentication/credential-resolver.d.ts +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/session/ports.d.ts +34 -0
- package/dist/session/ports.d.ts.map +1 -1
- package/dist/session/session-authority.d.ts +21 -1
- package/dist/session/session-authority.d.ts.map +1 -1
- package/dist/session/session-authority.js +152 -6
- package/dist/session/session-authority.js.map +1 -1
- package/dist/storage/file-ingest.d.ts +90 -0
- package/dist/storage/file-ingest.d.ts.map +1 -0
- package/dist/storage/file-ingest.js +508 -0
- package/dist/storage/file-ingest.js.map +1 -0
- package/dist/storage/index.d.ts +13 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +11 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/ports.d.ts +66 -0
- package/dist/storage/ports.d.ts.map +1 -0
- package/dist/storage/ports.js +30 -0
- package/dist/storage/ports.js.map +1 -0
- package/dist-cjs/index.js +1 -0
- package/dist-cjs/session/session-authority.js +153 -7
- package/dist-cjs/storage/file-ingest.js +512 -0
- package/dist-cjs/storage/index.js +19 -0
- package/dist-cjs/storage/ports.js +34 -0
- package/package.json +1 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object storage PORTS (SPEC docs/features/file-storage/SPEC.md §5 A-C) — the framework-agnostic
|
|
3
|
+
* contract for keeping file bytes OUTSIDE the database (a private, server-side-encrypted bucket),
|
|
4
|
+
* plus the byte-digest capability the pure ingest check needs.
|
|
5
|
+
*
|
|
6
|
+
* ADR-0003 (zero-dep core): the backends are real dependencies (an S3 SDK, `node:crypto`), so
|
|
7
|
+
* core defines only these interfaces; `@astram/guard` ships the concrete adapters
|
|
8
|
+
* (`S3ObjectStore`, `NodeSha256Digest`) and INJECTS them.
|
|
9
|
+
*
|
|
10
|
+
* SECURITY: keys are server-built (`t/<tenantId>/<id>`), never client input. Errors carry FIXED
|
|
11
|
+
* messages: no key, credential, endpoint userinfo, file name or bytes ever reaches a message.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* The object store cannot be reached or refused the operation. Fixed message, and deliberately no
|
|
15
|
+
* `cause`: backend (SDK) errors can carry the endpoint, request ids or userinfo, so adapters drop them.
|
|
16
|
+
*/
|
|
17
|
+
export class ObjectStoreUnavailableError extends Error {
|
|
18
|
+
constructor() {
|
|
19
|
+
super('object store unavailable');
|
|
20
|
+
this.name = 'ObjectStoreUnavailableError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** The requested object does not exist. Fixed message (the key is never echoed). */
|
|
24
|
+
export class ObjectNotFoundError extends Error {
|
|
25
|
+
constructor() {
|
|
26
|
+
super('object not found');
|
|
27
|
+
this.name = 'ObjectNotFoundError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=ports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ports.js","sourceRoot":"","sources":["../../src/storage/ports.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAiDH;;;GAGG;AACH,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IACpD;QACE,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;IAC5C,CAAC;CACF;AAED,oFAAoF;AACpF,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C;QACE,KAAK,CAAC,kBAAkB,CAAC,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF"}
|
package/dist-cjs/index.js
CHANGED
|
@@ -63,3 +63,4 @@ __exportStar(require("./metadata/index.js"), exports);
|
|
|
63
63
|
// prior-layer seam changed): admin ports + query/command types + §11.3 safeguard
|
|
64
64
|
// signatures + the query/command service factories + command-pipeline order.
|
|
65
65
|
__exportStar(require("./admin/index.js"), exports);
|
|
66
|
+
__exportStar(require("./storage/index.js"), exports);
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
* ADR-0003: framework-agnostic — no framework / HTTP / DB / node imports.
|
|
42
42
|
*/
|
|
43
43
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.SessionRejectedError = exports.SESSION_REJECTED_REASON = void 0;
|
|
44
|
+
exports.DEFAULT_REFRESH_REPLAY_WINDOW_MS = exports.SessionRejectedError = exports.SESSION_REJECTED_REASON = void 0;
|
|
45
45
|
exports.readHotSession = readHotSession;
|
|
46
46
|
exports.createSessionAuthority = createSessionAuthority;
|
|
47
47
|
const lifetimes_js_1 = require("./lifetimes.js");
|
|
@@ -65,6 +65,33 @@ exports.SessionRejectedError = SessionRejectedError;
|
|
|
65
65
|
const INITIAL_PERMISSIONS_VERSION = 0;
|
|
66
66
|
/** The initial cached-facts slot value — not yet projected (Authorization fills it). */
|
|
67
67
|
const INITIAL_CACHED_FACTS = null;
|
|
68
|
+
/** KI #51 (ADR-0036): the default recent-rotation window and the longest one accepted. */
|
|
69
|
+
exports.DEFAULT_REFRESH_REPLAY_WINDOW_MS = 20_000;
|
|
70
|
+
const MAX_REFRESH_REPLAY_WINDOW_MS = 120_000;
|
|
71
|
+
/** How long a reuse waits for a concurrent winner to remember its successor. */
|
|
72
|
+
const DEFAULT_REPLAY_WAIT_MS = 2_000;
|
|
73
|
+
const MAX_REPLAY_WAIT_MS = 10_000;
|
|
74
|
+
/** How far in the future a winner's `rotatedAtMs` may be (another node's clock). */
|
|
75
|
+
const MAX_CLOCK_SKEW_MS = 2_000;
|
|
76
|
+
function boundedInteger(value, max, name) {
|
|
77
|
+
if (!Number.isInteger(value) || value < 0 || value > max) {
|
|
78
|
+
throw new RangeError(`${name} must be an integer between 0 and ${max}`);
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
function isRecentRotation(value) {
|
|
83
|
+
if (value === null || typeof value !== 'object')
|
|
84
|
+
return false;
|
|
85
|
+
const v = value;
|
|
86
|
+
return (typeof v.accessToken === 'string' &&
|
|
87
|
+
v.accessToken.length > 0 &&
|
|
88
|
+
typeof v.refreshToken === 'string' &&
|
|
89
|
+
v.refreshToken.length > 0 &&
|
|
90
|
+
typeof v.accessExpiresAtMs === 'number' &&
|
|
91
|
+
Number.isFinite(v.accessExpiresAtMs) &&
|
|
92
|
+
typeof v.rotatedAtMs === 'number' &&
|
|
93
|
+
Number.isFinite(v.rotatedAtMs));
|
|
94
|
+
}
|
|
68
95
|
/** The Redis hot-session key for a session id (tenant-scoped, opaque, no PII). */
|
|
69
96
|
function hotKey(sessionId) {
|
|
70
97
|
return `shield:sess:${sessionId}`;
|
|
@@ -81,7 +108,8 @@ class SessionAuthorityImpl {
|
|
|
81
108
|
clock;
|
|
82
109
|
lifetimes;
|
|
83
110
|
deviceTrust;
|
|
84
|
-
|
|
111
|
+
replay;
|
|
112
|
+
constructor(recordStore, hotStore, tokenGenerator, refreshHasher, clock, lifetimes, deviceTrust, replay) {
|
|
85
113
|
this.recordStore = recordStore;
|
|
86
114
|
this.hotStore = hotStore;
|
|
87
115
|
this.tokenGenerator = tokenGenerator;
|
|
@@ -89,6 +117,7 @@ class SessionAuthorityImpl {
|
|
|
89
117
|
this.clock = clock;
|
|
90
118
|
this.lifetimes = lifetimes;
|
|
91
119
|
this.deviceTrust = deviceTrust;
|
|
120
|
+
this.replay = replay;
|
|
92
121
|
}
|
|
93
122
|
// -- establish (login) ----------------------------------------------------
|
|
94
123
|
async establish(principal) {
|
|
@@ -189,7 +218,16 @@ class SessionAuthorityImpl {
|
|
|
189
218
|
// `active` (already `rotated`, or `revoked`, or `expired`) is a REPLAY of a
|
|
190
219
|
// spent token — the signature of theft. Kill the ENTIRE lineage (every refresh
|
|
191
220
|
// record + the session), then reject. Both thief and victim are cut off.
|
|
221
|
+
//
|
|
222
|
+
// KI #51 / #54 (ADR-0036): a `rotated` record may be a REPEAT of a refresh that just
|
|
223
|
+
// succeeded (a concurrent request, or a client retry after a timeout). Inside the window, and
|
|
224
|
+
// only while that rotation's successor is unused, the SAME successor is returned instead.
|
|
192
225
|
if (record.status !== 'active') {
|
|
226
|
+
if (record.status === 'rotated') {
|
|
227
|
+
const repeat = await this.tolerateRepeat(refreshToken, record);
|
|
228
|
+
if (repeat !== null)
|
|
229
|
+
return repeat;
|
|
230
|
+
}
|
|
193
231
|
await this.recordStore.revokeLineage(record.lineageId);
|
|
194
232
|
throw new SessionRejectedError();
|
|
195
233
|
}
|
|
@@ -236,9 +274,13 @@ class SessionAuthorityImpl {
|
|
|
236
274
|
// truthy non-boolean must NOT be treated as a win (never fail open).
|
|
237
275
|
const won = (await this.recordStore.markRotatedIfActive(record.refreshId)) === true;
|
|
238
276
|
if (!won) {
|
|
239
|
-
// Lost the CAS ⇒ this token was already claimed/spent by another caller
|
|
240
|
-
//
|
|
241
|
-
//
|
|
277
|
+
// Lost the CAS ⇒ this token was already claimed/spent by another caller. KI #51 (ADR-0036):
|
|
278
|
+
// a concurrent repeat of the same refresh receives the winner's successor (waiting briefly
|
|
279
|
+
// for the winner to remember it). Otherwise: reuse of a spent token ⇒ kill the ENTIRE
|
|
280
|
+
// lineage, then reject. Both the winner's live successor and this caller are cut off.
|
|
281
|
+
const repeat = await this.tolerateRepeat(refreshToken, record);
|
|
282
|
+
if (repeat !== null)
|
|
283
|
+
return repeat;
|
|
242
284
|
await this.recordStore.revokeLineage(record.lineageId);
|
|
243
285
|
throw new SessionRejectedError();
|
|
244
286
|
}
|
|
@@ -272,6 +314,13 @@ class SessionAuthorityImpl {
|
|
|
272
314
|
throw new SessionRejectedError();
|
|
273
315
|
}
|
|
274
316
|
await this.putHotRaw(record.sessionId, principalId, tenantId);
|
|
317
|
+
const accessExpiresAtMs = nowMs + this.lifetimes.accessTtlMs;
|
|
318
|
+
await this.rememberRotation(refreshToken, {
|
|
319
|
+
accessToken: newAccessToken,
|
|
320
|
+
refreshToken: newRefreshToken,
|
|
321
|
+
accessExpiresAtMs,
|
|
322
|
+
rotatedAtMs: nowMs,
|
|
323
|
+
});
|
|
275
324
|
const rotatedSession = this.freezeSession({
|
|
276
325
|
sessionId: record.sessionId,
|
|
277
326
|
principalId,
|
|
@@ -281,13 +330,101 @@ class SessionAuthorityImpl {
|
|
|
281
330
|
// session's identity + absolute-cap anchor stay stable across rotations. The
|
|
282
331
|
// freshly-minted access token's validity is the `expiresAtMs` bound below.
|
|
283
332
|
issuedAtMs: session.issuedAtMs,
|
|
284
|
-
expiresAtMs:
|
|
333
|
+
expiresAtMs: accessExpiresAtMs,
|
|
285
334
|
});
|
|
286
335
|
return Object.freeze({
|
|
287
336
|
session: rotatedSession,
|
|
288
337
|
tokens: Object.freeze({ accessToken: newAccessToken, refreshToken: newRefreshToken }),
|
|
289
338
|
});
|
|
290
339
|
}
|
|
340
|
+
// -- recent-rotation window (KI #51 / #54, ADR-0036) ---------------------
|
|
341
|
+
/** Winner only: remember the successor under the spent token. Best-effort (never fails rotate). */
|
|
342
|
+
async rememberRotation(previousRefreshToken, rotation) {
|
|
343
|
+
const replay = this.replay;
|
|
344
|
+
if (replay === undefined)
|
|
345
|
+
return;
|
|
346
|
+
try {
|
|
347
|
+
await replay.store.remember(previousRefreshToken, Object.freeze({ ...rotation }), replay.windowMs);
|
|
348
|
+
}
|
|
349
|
+
catch {
|
|
350
|
+
// Not remembered ⇒ a repeat of this token is treated as a replay (today's behaviour).
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* A repeat of `record`'s refresh: the remembered successor, RE-PROVEN against the durable store,
|
|
355
|
+
* or `null` (the caller then kills the lineage). Accepted only when ALL hold: the entry is
|
|
356
|
+
* well-formed and inside the window by its own timestamp; its refresh token is the ACTIVE
|
|
357
|
+
* direct successor of `record` (same lineage, same session, `prevRefreshId` = `record`) — so
|
|
358
|
+
* once the successor itself rotated, or the lineage/session was revoked, nothing is returned;
|
|
359
|
+
* the session is active and inside its idle and absolute windows. Any error ⇒ `null`.
|
|
360
|
+
*/
|
|
361
|
+
async tolerateRepeat(refreshToken, record) {
|
|
362
|
+
const replay = this.replay;
|
|
363
|
+
if (replay === undefined)
|
|
364
|
+
return null;
|
|
365
|
+
try {
|
|
366
|
+
const rotation = await replay.store.recall(refreshToken, replay.waitMs);
|
|
367
|
+
if (!isRecentRotation(rotation))
|
|
368
|
+
return null;
|
|
369
|
+
const nowMs = this.clock.nowMs();
|
|
370
|
+
// A small forward skew is tolerated (the winner may be another node with a clock slightly
|
|
371
|
+
// ahead); the age bound is inclusive: a repeat at exactly `windowMs` is still inside.
|
|
372
|
+
if (rotation.rotatedAtMs > nowMs + Math.min(MAX_CLOCK_SKEW_MS, replay.windowMs) ||
|
|
373
|
+
nowMs - rotation.rotatedAtMs > replay.windowMs) {
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
const successor = await this.recordStore.findRefreshByHash(this.refreshHasher.hash(rotation.refreshToken));
|
|
377
|
+
if (successor === null ||
|
|
378
|
+
typeof successor !== 'object' ||
|
|
379
|
+
successor.status !== 'active' ||
|
|
380
|
+
successor.prevRefreshId !== record.refreshId ||
|
|
381
|
+
successor.lineageId !== record.lineageId ||
|
|
382
|
+
successor.sessionId !== record.sessionId ||
|
|
383
|
+
typeof successor.idleExpiresAtMs !== 'number' ||
|
|
384
|
+
nowMs >= successor.idleExpiresAtMs) {
|
|
385
|
+
return null;
|
|
386
|
+
}
|
|
387
|
+
const session = await this.recordStore.findSessionById(record.sessionId);
|
|
388
|
+
if (session === null ||
|
|
389
|
+
typeof session !== 'object' ||
|
|
390
|
+
session.status !== 'active' ||
|
|
391
|
+
typeof session.absoluteExpiresAtMs !== 'number' ||
|
|
392
|
+
nowMs >= session.absoluteExpiresAtMs ||
|
|
393
|
+
typeof session.principalId !== 'string' ||
|
|
394
|
+
session.principalId.length === 0 ||
|
|
395
|
+
typeof session.tenantId !== 'string' ||
|
|
396
|
+
session.tenantId.length === 0) {
|
|
397
|
+
return null;
|
|
398
|
+
}
|
|
399
|
+
try {
|
|
400
|
+
replay.onTolerated?.(Object.freeze({
|
|
401
|
+
sessionId: record.sessionId,
|
|
402
|
+
principalId: session.principalId,
|
|
403
|
+
tenantId: session.tenantId,
|
|
404
|
+
}));
|
|
405
|
+
}
|
|
406
|
+
catch {
|
|
407
|
+
// An observer failure never changes the answer.
|
|
408
|
+
}
|
|
409
|
+
return Object.freeze({
|
|
410
|
+
session: this.freezeSession({
|
|
411
|
+
sessionId: record.sessionId,
|
|
412
|
+
principalId: session.principalId,
|
|
413
|
+
tenantId: session.tenantId,
|
|
414
|
+
status: 'active',
|
|
415
|
+
issuedAtMs: session.issuedAtMs,
|
|
416
|
+
expiresAtMs: rotation.accessExpiresAtMs,
|
|
417
|
+
}),
|
|
418
|
+
tokens: Object.freeze({
|
|
419
|
+
accessToken: rotation.accessToken,
|
|
420
|
+
refreshToken: rotation.refreshToken,
|
|
421
|
+
}),
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
catch {
|
|
425
|
+
return null;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
291
428
|
// -- revoke (logout / admin) ---------------------------------------------
|
|
292
429
|
async revoke(sessionId) {
|
|
293
430
|
try {
|
|
@@ -394,5 +531,14 @@ async function readHotSession(hotStore, sessionId) {
|
|
|
394
531
|
*/
|
|
395
532
|
function createSessionAuthority(options) {
|
|
396
533
|
const lifetimes = (0, lifetimes_js_1.resolveLifetimes)(options.lifetimes);
|
|
397
|
-
return new SessionAuthorityImpl(options.recordStore, options.hotStore, options.tokenGenerator, options.refreshHasher, options.clock, lifetimes, options.deviceTrust);
|
|
534
|
+
return new SessionAuthorityImpl(options.recordStore, options.hotStore, options.tokenGenerator, options.refreshHasher, options.clock, lifetimes, options.deviceTrust, resolveReplayWindow(options));
|
|
535
|
+
}
|
|
536
|
+
/** Validate the replay settings eagerly (construction time); `undefined` = window off. */
|
|
537
|
+
function resolveReplayWindow(options) {
|
|
538
|
+
const windowMs = boundedInteger(options.replayWindowMs ?? exports.DEFAULT_REFRESH_REPLAY_WINDOW_MS, MAX_REFRESH_REPLAY_WINDOW_MS, 'replayWindowMs');
|
|
539
|
+
const waitMs = boundedInteger(options.replayWaitMs ?? DEFAULT_REPLAY_WAIT_MS, MAX_REPLAY_WAIT_MS, 'replayWaitMs');
|
|
540
|
+
const store = options.recentRotations;
|
|
541
|
+
if (store === undefined || store === null || windowMs === 0)
|
|
542
|
+
return undefined;
|
|
543
|
+
return Object.freeze({ store, windowMs, waitMs, onTolerated: options.onReplayTolerated });
|
|
398
544
|
}
|