@bymax-one/nest-auth 1.4.19 → 1.4.21

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/CHANGELOG.md CHANGED
@@ -18,6 +18,83 @@ what moves, and that note is the compatibility contract until strict SemVer begi
18
18
 
19
19
  ## [Unreleased]
20
20
 
21
+ ## [1.4.21] - 2026-11-08
22
+
23
+ ### Apply to a derived backend
24
+
25
+ - **Add `limitExpiry` to your `IApiKeyRepository` (optional, recommended).** Rotating a key with an
26
+ overlap read its expiry and then wrote the earlier of that and the end of the overlap with
27
+ `setExpiry`. Two rotations of one key could interleave, so that a delayed one with a longer
28
+ overlap overwrote the shorter deadline another had just set, and the old key kept verifying past
29
+ the shortest overlap anyone asked for. Implement
30
+ `limitExpiry(id, at): Promise<boolean>` as one atomic minimum, for example
31
+ `UPDATE api_keys SET expires_at = LEAST(COALESCE(expires_at, :at), :at) WHERE id = :id`, and
32
+ return `false` only when the key does not exist. Rotation then calls it and no longer computes the
33
+ minimum itself. A repository without it keeps the old read-then-write behaviour, which has the gap:
34
+ serialize rotation per key there.
35
+
36
+ ### Fixed
37
+
38
+ - **Concurrent rotations of one API key no longer lengthen the old key's life.** With a repository
39
+ that implements the new optional `limitExpiry`, rotation lowers the old key's expiry atomically
40
+ instead of overwriting it with a value computed from an earlier read. See the note above.
41
+
42
+ ### Added
43
+
44
+ - **`ReauthenticationService` is exported.** It re-proves the account password before a sensitive
45
+ change, counted against the same `reauth` lockout `MfaService` uses, or for an account with no
46
+ password requires a recent real sign-in. A host can guard its own destructive routes with it. It
47
+ is provided by `BymaxAuthModule` whether or not MFA is configured.
48
+
49
+ ### Changed
50
+
51
+ - **`MfaService` moves its re-authentication, flow lockout, record-transition and plane-tenant
52
+ code to internal modules.** Behaviour, error codes, Redis keys and log text are unchanged, and
53
+ none of the moved members was part of the public API.
54
+
55
+ ## [1.4.20] - 2026-11-07
56
+
57
+ ### Apply to a derived backend
58
+
59
+ - **`SelfOrAdminGuard` no longer admits a request addressed to another tenant.** It decided by the
60
+ caller's id and role alone, so a token for tenant `t1` and user `u1` was admitted on
61
+ `/tenants/t2/users/u1`, and the `admin` role, which is held inside one tenant, was admitted on any
62
+ tenant's users; the guard's documentation left the tenant check to the controller. When the route
63
+ captures the tenant as `tenantId`, `tenant_id` or `tenant`, every capture present must now equal
64
+ the token's `tenantId`, otherwise the guard answers `INSUFFICIENT_ROLE` (`403`) before it looks at
65
+ the id or the role. Captures that disagree with one another, are empty, or are arrays are refused,
66
+ and so is a token with no tenant. A route that captures no tenant is not tenant-checked, and
67
+ the controller or service must still enforce the tenant there. A host that relied on the guard
68
+ to admit an admin across tenants on such a route now gets a `403`. The `userId` or `id` capture
69
+ must now be one non-empty path segment: an empty value or one carrying a slash (a decoded `%2F`)
70
+ is refused with `INSUFFICIENT_ROLE` for an admin as well, and so is an owner whose own id contains a
71
+ slash, who could reach their own resource through a `%2F` before. A route that names the tenant any
72
+ other way, or sits on a router mounted without `mergeParams`, is not tenant-checked either.
73
+
74
+ ### Fixed
75
+
76
+ - **A cross-tenant request is no longer admitted by `SelfOrAdminGuard`.** See the note above.
77
+
78
+ ### Changed
79
+
80
+ - **`MfaService` moves private methods to internal modules.** They moved out of the class as plain
81
+ functions; behaviour, error codes and log text are unchanged, and none of them was part of the
82
+ public API.
83
+
84
+ ### Documentation
85
+
86
+ - **The shared contract states when the password-reset cooldown is released.**
87
+ `responseBodies.passwordResetRequest.failures.cooldown` said "released on any failure"; it now says
88
+ the cooldown is released when the request itself observes the failure (the account lookup, the
89
+ code or token store, or a send awaited inside the request), and that a send which fails after the
90
+ response has left never releases it. No behaviour changes. The contract's sha256 changes with it, so a copy kept elsewhere is refreshed
91
+ from `conformance/wire-contract.json`.
92
+ - **Correction to the 1.4.19 notes on the resend cooldown.** The 1.4.19 entry for the password-reset
93
+ `500` split says the cooldown a request took "is released on any failure". That is wrong for a
94
+ send that fails after the response has left (a detached send): it keeps the cooldown, as the code
95
+ and its documentation state. The cooldown is released only for a failure the request
96
+ itself observes. The 1.4.19 text stands as published; this line supersedes it.
97
+
21
98
  ## [1.4.19] - 2026-11-07
22
99
 
23
100
  ### Apply to a derived backend
@@ -4557,7 +4634,9 @@ ever installable.
4557
4634
  - Phase 4 password-reset tests cover: both `token` and `otp` flows, mutual exclusivity validation, `verifiedToken` exchange, resend cooldown, anti-enumeration (no error on unknown email), and session invalidation on reset
4558
4635
  - Phase 5 tests cover: platform login with MFA path and brute-force lockout, `JwtPlatformGuard` cross-context rejection, `PlatformRolesGuard` hierarchy enforcement, OAuth CSRF state lifecycle, `onOAuthLogin` hook resolution strategies, and invitation role-authorization + acceptance single-use enforcement
4559
4636
 
4560
- [Unreleased]: https://github.com/bymaxone/nest-auth/compare/v1.4.19...HEAD
4637
+ [Unreleased]: https://github.com/bymaxone/nest-auth/compare/v1.4.21...HEAD
4638
+ [1.4.21]: https://github.com/bymaxone/nest-auth/compare/v1.4.20...v1.4.21
4639
+ [1.4.20]: https://github.com/bymaxone/nest-auth/compare/v1.4.19...v1.4.20
4561
4640
  [1.4.19]: https://github.com/bymaxone/nest-auth/compare/v1.4.18...v1.4.19
4562
4641
  [1.4.18]: https://github.com/bymaxone/nest-auth/compare/v1.4.17...v1.4.18
4563
4642
  [1.4.17]: https://github.com/bymaxone/nest-auth/compare/v1.4.16...v1.4.17
package/README.md CHANGED
@@ -1316,14 +1316,15 @@ export class KeysAdminController {
1316
1316
  You implement `IApiKeyRepository`, as you do for users. The library supplies the `id` and the
1317
1317
  `digest`; the store only persists them.
1318
1318
 
1319
- | Method | Contract |
1320
- | ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1321
- | `findById(id)` | One primary-key read per request. Return revoked and expired rows too: the library decides validity. |
1322
- | `create(row)` | Insert `{ id, label, digest, scopes, createdAt, expiresAt }`. Do not make `label` unique. |
1323
- | `markRevoked(id, at)` | `false` when unknown. Revoking twice is not an error. A row with `revokedAt` set is revoked at once on every instance, whatever instant it holds: a future date is not a schedule, so schedule with `expiresAt`. |
1324
- | `setExpiry(id, at)` | `false` when unknown. The scheduling field: a key stops verifying at `expiresAt`. Used by rotation to retire the old key after the overlap. |
1325
- | `touchLastUsed(entries)` | One batch. **Must be monotonic**: `GREATEST(last_used_at, :at)`, never backwards — several instances flush independently. |
1326
- | `listByLabel(label)` | Every key of a label, for your admin screen. |
1319
+ | Method | Contract |
1320
+ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
1321
+ | `findById(id)` | One primary-key read per request. Return revoked and expired rows too: the library decides validity. |
1322
+ | `create(row)` | Insert `{ id, label, digest, scopes, createdAt, expiresAt }`. Do not make `label` unique. |
1323
+ | `markRevoked(id, at)` | `false` when unknown. Revoking twice is not an error. A row with `revokedAt` set is revoked at once on every instance, whatever instant it holds: a future date is not a schedule, so schedule with `expiresAt`. |
1324
+ | `setExpiry(id, at)` | `false` when unknown. The scheduling field: a key stops verifying at `expiresAt`. Used by rotation to retire the old key after the overlap. |
1325
+ | `limitExpiry(id, at)` | **Optional, recommended.** An atomic minimum: lower the expiry to `at`, never raise it (`SET expires_at = LEAST(COALESCE(expires_at, :at), :at)`). `false` when unknown. Rotation uses it when present; without it two rotations of one key can interleave and leave the old key valid past the shortest overlap, so serialize rotation per key. |
1326
+ | `touchLastUsed(entries)` | One batch. **Must be monotonic**: `GREATEST(last_used_at, :at)`, never backwards — several instances flush independently. |
1327
+ | `listByLabel(label)` | Every key of a label, for your admin screen. |
1327
1328
 
1328
1329
  ### How a key is stored and checked
1329
1330
 
@@ -2263,7 +2264,7 @@ conditional on nothing having invalidated in between, which this service does no
2263
2264
  | `ApiKeyGuard` | `@ServiceKey('read')` | Service API key for machine callers; fails closed without scopes |
2264
2265
 
2265
2266
  > [!NOTE]
2266
- > Three additional guards — `SelfOrAdminGuard` (ownership checks), `OptionalAuthGuard` (routes that behave differently for anonymous vs authenticated users), and `WsJwtGuard` (JWT authentication on WebSocket gateways) — are exported from the public `@bymax-one/nest-auth` barrel. Use them exactly like the core guards above.
2267
+ > Three additional guards — `SelfOrAdminGuard` (ownership checks), `OptionalAuthGuard` (routes that behave differently for anonymous vs authenticated users), and `WsJwtGuard` (JWT authentication on WebSocket gateways) — are exported from the public `@bymax-one/nest-auth` barrel. Use them exactly like the core guards above. `SelfOrAdminGuard` also holds a route that captures the tenant (`:tenantId`, `:tenant_id` or `:tenant`) to the token's tenant: a capture that differs from `req.user.tenantId`, disagrees with another capture, or is empty is refused with `403` before the self-or-admin decision, so neither a matching id nor the `admin` role reaches into another tenant. A wildcard capture arrives as an array and is refused, and the id (`:userId` or `:id`) must be one non-empty path segment, so an empty value or a decoded `%2F` is refused too. Only those three capture names are read: a route that names the tenant `{org}` or captures none is decided by identity and role alone, and your handler must bind the tenant itself. If a capture name is used at two nesting levels Express keeps the inner value, and a parent router mounted without `mergeParams` does not expose its captures to the inner route, so the guard does not see them.
2267
2268
 
2268
2269
  ### One error envelope
2269
2270