@astram/core 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/dist/admin/admin-service.d.ts +15 -7
  2. package/dist/admin/admin-service.d.ts.map +1 -1
  3. package/dist/admin/admin-service.js +18 -10
  4. package/dist/admin/admin-service.js.map +1 -1
  5. package/dist/admin/command/grants.d.ts.map +1 -1
  6. package/dist/admin/command/grants.js +2 -5
  7. package/dist/admin/command/grants.js.map +1 -1
  8. package/dist/admin/command/invitations.d.ts.map +1 -1
  9. package/dist/admin/command/invitations.js +3 -6
  10. package/dist/admin/command/invitations.js.map +1 -1
  11. package/dist/admin/command/permissions.d.ts.map +1 -1
  12. package/dist/admin/command/permissions.js +2 -6
  13. package/dist/admin/command/permissions.js.map +1 -1
  14. package/dist/admin/command/pipeline.d.ts +5 -2
  15. package/dist/admin/command/pipeline.d.ts.map +1 -1
  16. package/dist/admin/command/pipeline.js +6 -3
  17. package/dist/admin/command/pipeline.js.map +1 -1
  18. package/dist/admin/command/policies.d.ts.map +1 -1
  19. package/dist/admin/command/policies.js +7 -22
  20. package/dist/admin/command/policies.js.map +1 -1
  21. package/dist/admin/command/service-accounts.d.ts +17 -11
  22. package/dist/admin/command/service-accounts.d.ts.map +1 -1
  23. package/dist/admin/command/service-accounts.js +18 -24
  24. package/dist/admin/command/service-accounts.js.map +1 -1
  25. package/dist/admin/command/service.d.ts +5 -1
  26. package/dist/admin/command/service.d.ts.map +1 -1
  27. package/dist/admin/command/service.js +3 -5
  28. package/dist/admin/command/service.js.map +1 -1
  29. package/dist/admin/command/tiers.d.ts.map +1 -1
  30. package/dist/admin/command/tiers.js.map +1 -1
  31. package/dist/admin/index.d.ts.map +1 -1
  32. package/dist/admin/index.js +3 -2
  33. package/dist/admin/index.js.map +1 -1
  34. package/dist/admin/ports.d.ts.map +1 -1
  35. package/dist/admin/ports.js.map +1 -1
  36. package/dist/admin/query/catalog.d.ts.map +1 -1
  37. package/dist/admin/query/catalog.js +5 -13
  38. package/dist/admin/query/catalog.js.map +1 -1
  39. package/dist/admin/query/index.d.ts +2 -2
  40. package/dist/admin/query/index.d.ts.map +1 -1
  41. package/dist/admin/query/index.js +2 -2
  42. package/dist/admin/query/index.js.map +1 -1
  43. package/dist/admin/safeguards-impl.d.ts.map +1 -1
  44. package/dist/admin/safeguards-impl.js +3 -7
  45. package/dist/admin/safeguards-impl.js.map +1 -1
  46. package/dist/admin/safeguards.d.ts.map +1 -1
  47. package/dist/admin/safeguards.js.map +1 -1
  48. package/dist/admin/types.d.ts +12 -0
  49. package/dist/admin/types.d.ts.map +1 -1
  50. package/dist/admin/types.js.map +1 -1
  51. package/dist/audit/audit-recorder.d.ts.map +1 -1
  52. package/dist/audit/audit-recorder.js +1 -2
  53. package/dist/audit/audit-recorder.js.map +1 -1
  54. package/dist/audit/sensitive.d.ts.map +1 -1
  55. package/dist/audit/sensitive.js.map +1 -1
  56. package/dist/audit/walk.d.ts.map +1 -1
  57. package/dist/audit/walk.js +1 -4
  58. package/dist/audit/walk.js.map +1 -1
  59. package/dist/authentication/authenticator.d.ts.map +1 -1
  60. package/dist/authentication/authenticator.js.map +1 -1
  61. package/dist/authentication/credential-resolver.d.ts +1 -1
  62. package/dist/authentication/service-account-authenticator.d.ts.map +1 -1
  63. package/dist/authentication/service-account-authenticator.js +1 -3
  64. package/dist/authentication/service-account-authenticator.js.map +1 -1
  65. package/dist/authentication/totp.d.ts.map +1 -1
  66. package/dist/authentication/totp.js.map +1 -1
  67. package/dist/authorization/cross-tenant/grant-evaluator.d.ts.map +1 -1
  68. package/dist/authorization/cross-tenant/grant-evaluator.js.map +1 -1
  69. package/dist/authorization/decision-authority.d.ts +1 -1
  70. package/dist/authorization/decision-authority.d.ts.map +1 -1
  71. package/dist/authorization/decision-authority.js +5 -6
  72. package/dist/authorization/decision-authority.js.map +1 -1
  73. package/dist/authorization/default-gates.d.ts.map +1 -1
  74. package/dist/authorization/default-gates.js.map +1 -1
  75. package/dist/authorization/delegation/operation-identity.d.ts.map +1 -1
  76. package/dist/authorization/delegation/operation-identity.js.map +1 -1
  77. package/dist/authorization/errors.d.ts.map +1 -1
  78. package/dist/authorization/errors.js.map +1 -1
  79. package/dist/authorization/gates/object-type.d.ts +2 -2
  80. package/dist/authorization/gates/object-type.d.ts.map +1 -1
  81. package/dist/authorization/gates/object-type.js +5 -7
  82. package/dist/authorization/gates/object-type.js.map +1 -1
  83. package/dist/authorization/gates/wiring.d.ts.map +1 -1
  84. package/dist/authorization/gates/wiring.js.map +1 -1
  85. package/dist/authorization/ports.d.ts +17 -0
  86. package/dist/authorization/ports.d.ts.map +1 -1
  87. package/dist/authorization/reason-codes.js.map +1 -1
  88. package/dist/authorization/record/owner-baseline.d.ts.map +1 -1
  89. package/dist/authorization/record/owner-baseline.js +1 -3
  90. package/dist/authorization/record/owner-baseline.js.map +1 -1
  91. package/dist/authorization/record/permit-tier.d.ts.map +1 -1
  92. package/dist/authorization/record/permit-tier.js.map +1 -1
  93. package/dist/authorization/record/shares.d.ts.map +1 -1
  94. package/dist/authorization/record/shares.js.map +1 -1
  95. package/dist/encryption/encryption-service.d.ts.map +1 -1
  96. package/dist/encryption/encryption-service.js +56 -11
  97. package/dist/encryption/encryption-service.js.map +1 -1
  98. package/dist/encryption/index.d.ts +2 -2
  99. package/dist/encryption/index.d.ts.map +1 -1
  100. package/dist/encryption/index.js +1 -1
  101. package/dist/encryption/index.js.map +1 -1
  102. package/dist/encryption/ports.d.ts +4 -0
  103. package/dist/encryption/ports.d.ts.map +1 -1
  104. package/dist/identity/broker.d.ts.map +1 -1
  105. package/dist/identity/broker.js +2 -4
  106. package/dist/identity/broker.js.map +1 -1
  107. package/dist/identity/internal-adapter.d.ts.map +1 -1
  108. package/dist/identity/internal-adapter.js.map +1 -1
  109. package/dist/metadata/completeness.d.ts.map +1 -1
  110. package/dist/metadata/completeness.js +2 -2
  111. package/dist/metadata/completeness.js.map +1 -1
  112. package/dist/metadata/dependencies.d.ts.map +1 -1
  113. package/dist/metadata/dependencies.js.map +1 -1
  114. package/dist/metadata/index.d.ts +19 -0
  115. package/dist/metadata/index.d.ts.map +1 -1
  116. package/dist/metadata/index.js +24 -0
  117. package/dist/metadata/index.js.map +1 -1
  118. package/dist/metadata/registry.d.ts.map +1 -1
  119. package/dist/metadata/registry.js +24 -0
  120. package/dist/metadata/registry.js.map +1 -1
  121. package/dist/risk/risk-engine.d.ts.map +1 -1
  122. package/dist/risk/risk-engine.js +1 -3
  123. package/dist/risk/risk-engine.js.map +1 -1
  124. package/dist/risk/signals.d.ts.map +1 -1
  125. package/dist/risk/signals.js +1 -2
  126. package/dist/risk/signals.js.map +1 -1
  127. package/dist/risk/thresholds.d.ts.map +1 -1
  128. package/dist/risk/thresholds.js.map +1 -1
  129. package/dist/secrets/index.d.ts +1 -1
  130. package/dist/secrets/index.d.ts.map +1 -1
  131. package/dist/secrets/index.js +1 -1
  132. package/dist/secrets/index.js.map +1 -1
  133. package/dist/session/lifetimes.d.ts.map +1 -1
  134. package/dist/session/lifetimes.js.map +1 -1
  135. package/dist/session/ports.d.ts +34 -0
  136. package/dist/session/ports.d.ts.map +1 -1
  137. package/dist/session/session-authority.d.ts +21 -1
  138. package/dist/session/session-authority.d.ts.map +1 -1
  139. package/dist/session/session-authority.js +152 -6
  140. package/dist/session/session-authority.js.map +1 -1
  141. package/dist-cjs/admin/admin-service.js +18 -10
  142. package/dist-cjs/admin/command/grants.js +2 -5
  143. package/dist-cjs/admin/command/invitations.js +3 -6
  144. package/dist-cjs/admin/command/permissions.js +2 -6
  145. package/dist-cjs/admin/command/pipeline.js +6 -3
  146. package/dist-cjs/admin/command/policies.js +7 -22
  147. package/dist-cjs/admin/command/service-accounts.js +18 -24
  148. package/dist-cjs/admin/command/service.js +2 -4
  149. package/dist-cjs/admin/index.js +3 -2
  150. package/dist-cjs/admin/query/catalog.js +5 -13
  151. package/dist-cjs/admin/safeguards-impl.js +3 -7
  152. package/dist-cjs/audit/audit-recorder.js +1 -2
  153. package/dist-cjs/audit/walk.js +1 -4
  154. package/dist-cjs/authentication/service-account-authenticator.js +1 -3
  155. package/dist-cjs/authorization/decision-authority.js +5 -6
  156. package/dist-cjs/authorization/gates/object-type.js +4 -6
  157. package/dist-cjs/authorization/record/owner-baseline.js +1 -3
  158. package/dist-cjs/encryption/encryption-service.js +56 -11
  159. package/dist-cjs/identity/broker.js +1 -3
  160. package/dist-cjs/metadata/completeness.js +2 -2
  161. package/dist-cjs/metadata/index.js +25 -0
  162. package/dist-cjs/metadata/registry.js +24 -0
  163. package/dist-cjs/risk/risk-engine.js +1 -3
  164. package/dist-cjs/risk/signals.js +1 -2
  165. package/dist-cjs/session/session-authority.js +153 -7
  166. package/package.json +1 -1
@@ -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
- constructor(recordStore, hotStore, tokenGenerator, refreshHasher, clock, lifetimes, deviceTrust) {
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
- // treat as reuse of a spent token: kill the ENTIRE lineage, then reject.
241
- // Both the winner's live successor and this caller are cut off.
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: nowMs + this.lifetimes.accessTtlMs,
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astram/core",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "license": "MIT",
5
5
  "description": "Astram framework — sealed security core (Identity, Authentication, Session, Risk, Authorization, Audit). ZERO framework deps — the portable decision brain (ADR-0003). The frozen public contract consumers build against; installed, not edited (ADR-0023).",
6
6
  "type": "module",