@bymax-one/nest-auth 1.5.5 → 1.5.6
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 +29 -1
- package/dist/server/index.cjs +65 -37
- package/dist/server/index.d.cts +31 -15
- package/dist/server/index.d.ts +31 -15
- package/dist/server/index.mjs +65 -37
- package/dist/shared/index.d.cts +8 -0
- package/dist/shared/index.d.ts +8 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -18,6 +18,32 @@ what moves, and that note is the compatibility contract until strict SemVer begi
|
|
|
18
18
|
|
|
19
19
|
## [Unreleased]
|
|
20
20
|
|
|
21
|
+
## [1.5.6] - 2026-11-15
|
|
22
|
+
|
|
23
|
+
A patch release. The wire contract is at version 8.
|
|
24
|
+
|
|
25
|
+
### Changed
|
|
26
|
+
|
|
27
|
+
- **The recent-authentication mark belongs to the session that earned it.** Access tokens carry a new signed `sid`
|
|
28
|
+
claim, the refresh family id of the session, kept through every rotation. The mark is written under
|
|
29
|
+
`ra:{hmac_sha256(hmacKey, userSubject)}:{sid}` and read under the caller's own `sid`, so another session of the same
|
|
30
|
+
account cannot use a mark it did not earn. The TTL is unchanged (300 seconds). The mark expires with its TTL and is not deleted by
|
|
31
|
+
logout or session revocation; only a token carrying its `sid` can read it.
|
|
32
|
+
- **Mixed deployments fail closed.** A mark written under the older account-wide key is not honoured after the upgrade, a
|
|
33
|
+
token minted before `sid` existed earns no proof, and a reader that does not know `sid` never finds the new key.
|
|
34
|
+
In each case the answer is a request to re-authenticate, never a pass.
|
|
35
|
+
|
|
36
|
+
### Apply to a derived backend
|
|
37
|
+
|
|
38
|
+
- **A user with no local password is asked to re-authenticate once** for a sensitive action (enrolling MFA, and the
|
|
39
|
+
workspace actions that re-prove the account) after the upgrade: until the session's next sign-in or refresh the token
|
|
40
|
+
has no `sid`, and a mark written before the upgrade is not read.
|
|
41
|
+
- **A host that signs its own access tokens must add `sid`.** Set it to the refresh family id of the session, 36
|
|
42
|
+
characters of lowercase hexadecimal and hyphens in 8-4-4-4-12; a token without a usable `sid` never satisfies the
|
|
43
|
+
re-authentication gate. A host that calls `TokenManagerService.issueTokens` needs no change, because the token it
|
|
44
|
+
returns carries `sid`. `MfaService.setup` takes the caller's `sid` as a trailing argument, and
|
|
45
|
+
`ReauthenticationService.assertReauthenticated` takes it after `tenantId`.
|
|
46
|
+
|
|
21
47
|
## [1.5.5] - 2026-11-14
|
|
22
48
|
|
|
23
49
|
A patch release.
|
|
@@ -25,6 +51,7 @@ A patch release.
|
|
|
25
51
|
### Fixed
|
|
26
52
|
|
|
27
53
|
- `issueTokensForUserId` no longer marks the session it issues as recently authenticated, matching rust-auth.
|
|
54
|
+
- Security advisory: [GHSA-93hv-9p5j-f5xp](https://github.com/bymaxone/nest-auth/security/advisories/GHSA-93hv-9p5j-f5xp).
|
|
28
55
|
|
|
29
56
|
### Apply to a derived backend
|
|
30
57
|
|
|
@@ -5149,7 +5176,8 @@ ever installable.
|
|
|
5149
5176
|
- 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
|
|
5150
5177
|
- 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
|
|
5151
5178
|
|
|
5152
|
-
[Unreleased]: https://github.com/bymaxone/nest-auth/compare/v1.5.
|
|
5179
|
+
[Unreleased]: https://github.com/bymaxone/nest-auth/compare/v1.5.6...HEAD
|
|
5180
|
+
[1.5.6]: https://github.com/bymaxone/nest-auth/compare/v1.5.5...v1.5.6
|
|
5153
5181
|
[1.5.5]: https://github.com/bymaxone/nest-auth/compare/v1.5.4...v1.5.5
|
|
5154
5182
|
[1.5.4]: https://github.com/bymaxone/nest-auth/compare/v1.5.3...v1.5.4
|
|
5155
5183
|
[1.5.3]: https://github.com/bymaxone/nest-auth/compare/v1.5.2...v1.5.3
|
package/dist/server/index.cjs
CHANGED
|
@@ -6394,11 +6394,18 @@ exports.SessionService = _ts_decorate18([
|
|
|
6394
6394
|
|
|
6395
6395
|
// src/server/constants/recent-auth.ts
|
|
6396
6396
|
var RECENT_AUTH_TTL_SECONDS = 300;
|
|
6397
|
-
function recentAuthKey(plane, userId, hmacKey, tenantId) {
|
|
6398
|
-
return `ra:${hmacSha256(userSubject(plane, userId, tenantId), hmacKey)}`;
|
|
6397
|
+
function recentAuthKey(plane, userId, hmacKey, tenantId, sid) {
|
|
6398
|
+
return `ra:${hmacSha256(userSubject(plane, userId, tenantId), hmacKey)}:${sid}`;
|
|
6399
6399
|
}
|
|
6400
6400
|
__name(recentAuthKey, "recentAuthKey");
|
|
6401
6401
|
|
|
6402
|
+
// src/server/constants/session-id.ts
|
|
6403
|
+
var SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
6404
|
+
function sessionId(value) {
|
|
6405
|
+
return typeof value === "string" && SESSION_ID.test(value) ? value : void 0;
|
|
6406
|
+
}
|
|
6407
|
+
__name(sessionId, "sessionId");
|
|
6408
|
+
|
|
6402
6409
|
// src/server/utils/owner-fragment.ts
|
|
6403
6410
|
var UNKNOWN_OWNER = "userId=<unknown: no live session remains in this family to name it>";
|
|
6404
6411
|
function ownerFragment(ownerId) {
|
|
@@ -6406,6 +6413,14 @@ function ownerFragment(ownerId) {
|
|
|
6406
6413
|
}
|
|
6407
6414
|
__name(ownerFragment, "ownerFragment");
|
|
6408
6415
|
|
|
6416
|
+
// src/server/utils/serialize-session.ts
|
|
6417
|
+
function serializeSession(session) {
|
|
6418
|
+
if (session.familyId !== "") return JSON.stringify(session);
|
|
6419
|
+
const { familyId: _omitted, familyCreatedAt: _alsoOmitted, ...rest } = session;
|
|
6420
|
+
return JSON.stringify(rest);
|
|
6421
|
+
}
|
|
6422
|
+
__name(serializeSession, "serializeSession");
|
|
6423
|
+
|
|
6409
6424
|
// src/server/services/token-manager.service.ts
|
|
6410
6425
|
function _ts_decorate19(decorators, target, key, desc) {
|
|
6411
6426
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
@@ -6541,7 +6556,9 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6541
6556
|
*/
|
|
6542
6557
|
async issueTokens(user, ip, userAgent, overrides) {
|
|
6543
6558
|
const epoch = await this.redis.getUserTokenEpoch(user.id, user.tenantId, "dashboard");
|
|
6559
|
+
const familyId = crypto.randomUUID();
|
|
6544
6560
|
const accessToken = this.issueAccess({
|
|
6561
|
+
sid: familyId,
|
|
6545
6562
|
sub: user.id,
|
|
6546
6563
|
tenantId: user.tenantId,
|
|
6547
6564
|
role: user.role,
|
|
@@ -6553,27 +6570,34 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6553
6570
|
});
|
|
6554
6571
|
const rawRefreshToken = generateSecureToken();
|
|
6555
6572
|
const tokenHash = sha256(rawRefreshToken);
|
|
6556
|
-
const familyId = crypto.randomUUID();
|
|
6557
6573
|
const session = this.buildSession(user.id, user.tenantId, user.role, ip, userAgent, user.mfaEnabled, familyId, void 0, overrides?.mfaVerified === true);
|
|
6558
6574
|
const ttl = this.options.jwt.refreshExpiresInDays * 86400;
|
|
6559
6575
|
await this.redis.writeNewSession({
|
|
6560
6576
|
kind: "dashboard",
|
|
6561
6577
|
tenantId: user.tenantId,
|
|
6562
6578
|
tokenHash,
|
|
6563
|
-
sessionJson:
|
|
6579
|
+
sessionJson: serializeSession(session),
|
|
6564
6580
|
familyId,
|
|
6565
6581
|
userId: user.id,
|
|
6566
6582
|
refreshTtl: ttl
|
|
6567
6583
|
});
|
|
6568
|
-
if (overrides?.credentialProved === true)
|
|
6569
|
-
await this.redis.set(recentAuthKey("dashboard", user.id, this.options.hmacKey, user.tenantId), "1", RECENT_AUTH_TTL_SECONDS);
|
|
6570
|
-
}
|
|
6584
|
+
if (overrides?.credentialProved === true) await this.markRecentAuth(user, familyId);
|
|
6571
6585
|
return {
|
|
6572
6586
|
user,
|
|
6573
6587
|
accessToken,
|
|
6574
6588
|
rawRefreshToken
|
|
6575
6589
|
};
|
|
6576
6590
|
}
|
|
6591
|
+
/**
|
|
6592
|
+
* Records that the session just born presented a credential.
|
|
6593
|
+
*
|
|
6594
|
+
* Only a credential presented in this request proves a recent authentication; a refresh proves
|
|
6595
|
+
* possession of a token, so `reissueTokens` never writes this mark. The key names the session, so
|
|
6596
|
+
* another session of the same account cannot use it.
|
|
6597
|
+
*/
|
|
6598
|
+
async markRecentAuth(user, sid) {
|
|
6599
|
+
await this.redis.set(recentAuthKey("dashboard", user.id, this.options.hmacKey, user.tenantId, sid), "1", RECENT_AUTH_TTL_SECONDS);
|
|
6600
|
+
}
|
|
6577
6601
|
// ---------------------------------------------------------------------------
|
|
6578
6602
|
// Platform tokens
|
|
6579
6603
|
// ---------------------------------------------------------------------------
|
|
@@ -6591,7 +6615,9 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6591
6615
|
*/
|
|
6592
6616
|
async issuePlatformTokens(admin, ip, userAgent, overrides) {
|
|
6593
6617
|
const epoch = await this.redis.getUserTokenEpoch(admin.id, void 0, "platform");
|
|
6618
|
+
const familyId = crypto.randomUUID();
|
|
6594
6619
|
const accessToken = this.issuePlatformAccess({
|
|
6620
|
+
sid: familyId,
|
|
6595
6621
|
sub: admin.id,
|
|
6596
6622
|
role: admin.role,
|
|
6597
6623
|
type: "platform",
|
|
@@ -6601,14 +6627,13 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6601
6627
|
});
|
|
6602
6628
|
const rawRefreshToken = generateSecureToken();
|
|
6603
6629
|
const tokenHash = sha256(rawRefreshToken);
|
|
6604
|
-
const familyId = crypto.randomUUID();
|
|
6605
6630
|
const session = this.buildSession(admin.id, "", admin.role, ip, userAgent, admin.mfaEnabled, familyId, void 0, overrides?.mfaVerified === true);
|
|
6606
6631
|
const ttl = this.options.jwt.refreshExpiresInDays * 86400;
|
|
6607
6632
|
await this.redis.writeNewSession({
|
|
6608
6633
|
kind: "platform",
|
|
6609
6634
|
tenantId: void 0,
|
|
6610
6635
|
tokenHash,
|
|
6611
|
-
sessionJson:
|
|
6636
|
+
sessionJson: serializeSession(session),
|
|
6612
6637
|
familyId,
|
|
6613
6638
|
userId: admin.id,
|
|
6614
6639
|
refreshTtl: ttl
|
|
@@ -6692,7 +6717,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6692
6717
|
tenantId: seed.tenantId,
|
|
6693
6718
|
oldHash,
|
|
6694
6719
|
newHash,
|
|
6695
|
-
newSessionJson:
|
|
6720
|
+
newSessionJson: serializeSession(newSession),
|
|
6696
6721
|
familyId: seed.familyId,
|
|
6697
6722
|
userId: seed.userId,
|
|
6698
6723
|
refreshTtl,
|
|
@@ -6870,7 +6895,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6870
6895
|
kind: "dashboard",
|
|
6871
6896
|
tenantId: graceSession.tenantId,
|
|
6872
6897
|
newHash: anotherNewHash,
|
|
6873
|
-
newSessionJson:
|
|
6898
|
+
newSessionJson: serializeSession(anotherSession),
|
|
6874
6899
|
familyId: graceSession.familyId,
|
|
6875
6900
|
userId: graceSession.userId,
|
|
6876
6901
|
refreshTtl
|
|
@@ -6932,21 +6957,6 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6932
6957
|
};
|
|
6933
6958
|
}
|
|
6934
6959
|
/**
|
|
6935
|
-
* Serializes a session record for storage, omitting an empty `familyId`.
|
|
6936
|
-
*
|
|
6937
|
-
* rust-auth skips the field when empty (`skip_serializing_if`), so emitting `"familyId":""`
|
|
6938
|
-
* here would make the same session serialize to different bytes on each side and break the
|
|
6939
|
-
* shared-Redis contract. A record with no family simply has no key.
|
|
6940
|
-
*
|
|
6941
|
-
* @param session - The record to store.
|
|
6942
|
-
* @returns The JSON string written under an `rt:`/`prt:` key.
|
|
6943
|
-
*/
|
|
6944
|
-
serializeSession(session) {
|
|
6945
|
-
if (session.familyId !== "") return JSON.stringify(session);
|
|
6946
|
-
const { familyId: _omitted, familyCreatedAt: _alsoOmitted, ...rest } = session;
|
|
6947
|
-
return JSON.stringify(rest);
|
|
6948
|
-
}
|
|
6949
|
-
/**
|
|
6950
6960
|
* Constructs a session record from identity fields and request metadata.
|
|
6951
6961
|
*
|
|
6952
6962
|
* @param userId - Internal user ID for whom the session is being created.
|
|
@@ -7007,6 +7017,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7007
7017
|
async buildRotatedResult(session, rawRefreshToken) {
|
|
7008
7018
|
const epoch = await this.redis.getUserTokenEpoch(session.userId, session.tenantId, "dashboard");
|
|
7009
7019
|
const accessToken = this.issueAccess({
|
|
7020
|
+
sid: sessionId(session.familyId),
|
|
7010
7021
|
sub: session.userId,
|
|
7011
7022
|
tenantId: session.tenantId,
|
|
7012
7023
|
role: session.role,
|
|
@@ -7054,7 +7065,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7054
7065
|
tenantId: void 0,
|
|
7055
7066
|
oldHash,
|
|
7056
7067
|
newHash,
|
|
7057
|
-
newSessionJson:
|
|
7068
|
+
newSessionJson: serializeSession(newSession),
|
|
7058
7069
|
familyId: seed.familyId,
|
|
7059
7070
|
userId: seed.userId,
|
|
7060
7071
|
refreshTtl,
|
|
@@ -7107,7 +7118,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7107
7118
|
kind: "platform",
|
|
7108
7119
|
tenantId: void 0,
|
|
7109
7120
|
newHash: anotherNewHash,
|
|
7110
|
-
newSessionJson:
|
|
7121
|
+
newSessionJson: serializeSession(anotherSession),
|
|
7111
7122
|
familyId: graceSession.familyId,
|
|
7112
7123
|
userId: graceSession.userId,
|
|
7113
7124
|
refreshTtl
|
|
@@ -7129,6 +7140,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7129
7140
|
async buildPlatformRotatedResult(session, rawRefreshToken) {
|
|
7130
7141
|
const epoch = await this.redis.getUserTokenEpoch(session.userId, void 0, "platform");
|
|
7131
7142
|
const accessToken = this.issuePlatformAccess({
|
|
7143
|
+
sid: sessionId(session.familyId),
|
|
7132
7144
|
sub: session.userId,
|
|
7133
7145
|
role: session.role,
|
|
7134
7146
|
type: "platform",
|
|
@@ -7169,6 +7181,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7169
7181
|
*/
|
|
7170
7182
|
async reissuePlatformAccessWithAuthority(claims, role, mfaEnabled) {
|
|
7171
7183
|
return this.issuePlatformAccess({
|
|
7184
|
+
sid: claims.sid,
|
|
7172
7185
|
sub: claims.sub,
|
|
7173
7186
|
role,
|
|
7174
7187
|
type: "platform",
|
|
@@ -8029,6 +8042,7 @@ exports.AuthService = class _AuthService {
|
|
|
8029
8042
|
let authoritative = result.accessToken;
|
|
8030
8043
|
if (rotated.role !== user.role || rotated.tenantId !== user.tenantId || rotated.mfaEnabled !== user.mfaEnabled) {
|
|
8031
8044
|
authoritative = this.tokenManager.issueAccess({
|
|
8045
|
+
sid: rotated.sid,
|
|
8032
8046
|
sub: user.id,
|
|
8033
8047
|
tenantId: user.tenantId,
|
|
8034
8048
|
role: user.role,
|
|
@@ -8036,8 +8050,7 @@ exports.AuthService = class _AuthService {
|
|
|
8036
8050
|
status: user.status,
|
|
8037
8051
|
mfaEnabled: user.mfaEnabled,
|
|
8038
8052
|
// Carried across from the token the rotation just produced: a second factor already
|
|
8039
|
-
// cleared on this session stays cleared, and re-stamping
|
|
8040
|
-
// silently demand it again.
|
|
8053
|
+
// cleared on this session stays cleared, and re-stamping must not demand it again.
|
|
8041
8054
|
mfaVerified: rotated.mfaVerified,
|
|
8042
8055
|
epoch: await this.redis.getUserTokenEpoch(user.id, user.tenantId, "dashboard")
|
|
8043
8056
|
});
|
|
@@ -11951,14 +11964,16 @@ exports.ReauthenticationService = class ReauthenticationService {
|
|
|
11951
11964
|
* @param userId - The account being changed, part of the counter's key.
|
|
11952
11965
|
* @param passwordHash - The account's stored hash, or `null` for an OAuth-only account.
|
|
11953
11966
|
* @param password - The password the caller submitted, if any.
|
|
11967
|
+
* @param tenantId - The caller's tenant on the dashboard plane, part of the key.
|
|
11968
|
+
* @param sid - The caller's session, from its token; only an account with no password reads it.
|
|
11954
11969
|
* @throws {@link AuthException} `ACCOUNT_LOCKED` once the failure budget for this flow is
|
|
11955
11970
|
* spent, or `INVALID_CREDENTIALS` when the account has a password and the submitted one is
|
|
11956
11971
|
* absent or wrong. The latter is deliberately the same code a failed login returns: an
|
|
11957
11972
|
* attacker holding a stolen token learns nothing from it that a failed login does not tell them.
|
|
11958
11973
|
*/
|
|
11959
|
-
async assertReauthenticated(context, userId, passwordHash, password, tenantId) {
|
|
11974
|
+
async assertReauthenticated(context, userId, passwordHash, password, tenantId, sid) {
|
|
11960
11975
|
if (passwordHash === null) {
|
|
11961
|
-
const recent = await this.
|
|
11976
|
+
const recent = await this.recentMark(context, userId, tenantId, sid);
|
|
11962
11977
|
if (recent === null) {
|
|
11963
11978
|
this.logger.warn(`reauthenticate: no recent authentication userId=${logSafe(userId)} context=${context}`);
|
|
11964
11979
|
throw new AuthException(AUTH_ERROR_CODES.REAUTHENTICATION_REQUIRED);
|
|
@@ -11977,6 +11992,18 @@ exports.ReauthenticationService = class ReauthenticationService {
|
|
|
11977
11992
|
}
|
|
11978
11993
|
await this.lockout.reset("reauth", context, userId, tenantId);
|
|
11979
11994
|
}
|
|
11995
|
+
/**
|
|
11996
|
+
* The recent-authentication mark of the caller's own session, or `null` when there is none.
|
|
11997
|
+
*
|
|
11998
|
+
* The proof is the caller's own: the mark is keyed by the session that earned it, so another
|
|
11999
|
+
* session of the same account cannot borrow it. A token with no usable `sid`, one minted before
|
|
12000
|
+
* the claim existed, names no session and earns nothing, so no key is read for it.
|
|
12001
|
+
*/
|
|
12002
|
+
async recentMark(context, userId, tenantId, sid) {
|
|
12003
|
+
const bound = sessionId(sid);
|
|
12004
|
+
if (bound === void 0) return null;
|
|
12005
|
+
return await this.redis.get(recentAuthKey(context, userId, this.options.hmacKey, tenantId, bound));
|
|
12006
|
+
}
|
|
11980
12007
|
};
|
|
11981
12008
|
exports.ReauthenticationService = _ts_decorate41([
|
|
11982
12009
|
common.Injectable(),
|
|
@@ -12123,6 +12150,7 @@ exports.MfaService = class _MfaService {
|
|
|
12123
12150
|
*
|
|
12124
12151
|
* @param userId - Internal ID of the user enabling MFA.
|
|
12125
12152
|
* @param context - Which repository to use: `'dashboard'` (default) or `'platform'`.
|
|
12153
|
+
* @param sid - The caller's session (its token's `sid`), for an account with no local password.
|
|
12126
12154
|
* @returns Setup result containing the secret, QR URI, and plain recovery codes.
|
|
12127
12155
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
12128
12156
|
* @throws `MFA_NOT_ENABLED` if `context === 'platform'` and the platform user
|
|
@@ -12130,14 +12158,14 @@ exports.MfaService = class _MfaService {
|
|
|
12130
12158
|
* reused (rather than introducing a new one) because the consumer-facing
|
|
12131
12159
|
* meaning is the same: "platform MFA cannot operate".
|
|
12132
12160
|
*/
|
|
12133
|
-
async setup(userId, context = "dashboard", password, tenantId) {
|
|
12161
|
+
async setup(userId, context = "dashboard", password, tenantId, sid) {
|
|
12134
12162
|
assertPlaneTenant(context, tenantId);
|
|
12135
12163
|
if (context === "platform" && !this.platformUserRepo) {
|
|
12136
12164
|
throw new AuthException(AUTH_ERROR_CODES.MFA_NOT_ENABLED);
|
|
12137
12165
|
}
|
|
12138
12166
|
const user = await this.records.loadAccount(context, userId, tenantId);
|
|
12139
12167
|
if (user.mfaEnabled) throw new AuthException(AUTH_ERROR_CODES.MFA_ALREADY_ENABLED);
|
|
12140
|
-
await this.reauthentication.assertReauthenticated(context, userId, user.passwordHash, password, tenantId);
|
|
12168
|
+
await this.reauthentication.assertReauthenticated(context, userId, user.passwordHash, password, tenantId, sid);
|
|
12141
12169
|
const setupKey = `mfa_setup:${hmacSha256(userSubject(context, userId, tenantId), this.options.hmacKey)}`;
|
|
12142
12170
|
const existingFast = await this.redis.get(setupKey);
|
|
12143
12171
|
if (existingFast !== null) {
|
|
@@ -12493,7 +12521,7 @@ var MfaController = class {
|
|
|
12493
12521
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
12494
12522
|
*/
|
|
12495
12523
|
async setup(user, dto) {
|
|
12496
|
-
return this.mfaService.setup(user.sub, "dashboard", dto.password, user.tenantId);
|
|
12524
|
+
return this.mfaService.setup(user.sub, "dashboard", dto.password, user.tenantId, user.sid);
|
|
12497
12525
|
}
|
|
12498
12526
|
/**
|
|
12499
12527
|
* Verifies the first TOTP code and permanently enables MFA on the account.
|
|
@@ -14821,7 +14849,7 @@ var PlatformMfaController = class {
|
|
|
14821
14849
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
14822
14850
|
*/
|
|
14823
14851
|
async setup(user, dto) {
|
|
14824
|
-
return this.mfaService.setup(user.sub, "platform", dto.password);
|
|
14852
|
+
return this.mfaService.setup(user.sub, "platform", dto.password, void 0, user.sid);
|
|
14825
14853
|
}
|
|
14826
14854
|
/**
|
|
14827
14855
|
* Verifies the first TOTP code and permanently enables MFA on the platform
|
|
@@ -18494,7 +18522,7 @@ exports.WorkspaceCallerService = class WorkspaceCallerService {
|
|
|
18494
18522
|
*/
|
|
18495
18523
|
async reauthenticate(caller, password) {
|
|
18496
18524
|
const user = await this.find(caller);
|
|
18497
|
-
await this.reauthentication.assertReauthenticated("dashboard", user.id, user.passwordHash, password, caller.tenantId);
|
|
18525
|
+
await this.reauthentication.assertReauthenticated("dashboard", user.id, user.passwordHash, password, caller.tenantId, caller.sid);
|
|
18498
18526
|
}
|
|
18499
18527
|
async find(caller) {
|
|
18500
18528
|
const user = await this.users.findById({
|
package/dist/server/index.d.cts
CHANGED
|
@@ -4063,6 +4063,12 @@ declare class NoOpAuthHooks implements IAuthHooks {
|
|
|
4063
4063
|
interface DashboardJwtPayload {
|
|
4064
4064
|
/** Unique JWT ID (UUID v4) generated by TokenManagerService for revocation. */
|
|
4065
4065
|
jti: string;
|
|
4066
|
+
/**
|
|
4067
|
+
* The session this token belongs to: the refresh family id, unchanged through every rotation.
|
|
4068
|
+
* Absent on a token minted before the claim existed, which the re-authentication gate treats as
|
|
4069
|
+
* a session that earned no recent authentication.
|
|
4070
|
+
*/
|
|
4071
|
+
sid?: string | undefined;
|
|
4066
4072
|
/** Subject — the authenticated user's internal identifier. */
|
|
4067
4073
|
sub: string;
|
|
4068
4074
|
/** Tenant the user belongs to; used for data isolation. */
|
|
@@ -4119,6 +4125,8 @@ interface DashboardJwtPayload {
|
|
|
4119
4125
|
interface PlatformJwtPayload {
|
|
4120
4126
|
/** Unique JWT ID (UUID v4) generated by TokenManagerService for revocation. */
|
|
4121
4127
|
jti: string;
|
|
4128
|
+
/** The session this token belongs to, as on {@link DashboardJwtPayload.sid}. */
|
|
4129
|
+
sid?: string | undefined;
|
|
4122
4130
|
/** Subject — the platform administrator's internal identifier. */
|
|
4123
4131
|
sub: string;
|
|
4124
4132
|
/** Administrator's platform-level role (e.g. `'super_admin'`, `'support'`). */
|
|
@@ -9777,6 +9785,14 @@ declare class TokenManagerService {
|
|
|
9777
9785
|
mfaVerified?: boolean;
|
|
9778
9786
|
credentialProved?: boolean;
|
|
9779
9787
|
}): Promise<AuthResult>;
|
|
9788
|
+
/**
|
|
9789
|
+
* Records that the session just born presented a credential.
|
|
9790
|
+
*
|
|
9791
|
+
* Only a credential presented in this request proves a recent authentication; a refresh proves
|
|
9792
|
+
* possession of a token, so `reissueTokens` never writes this mark. The key names the session, so
|
|
9793
|
+
* another session of the same account cannot use it.
|
|
9794
|
+
*/
|
|
9795
|
+
private markRecentAuth;
|
|
9780
9796
|
/**
|
|
9781
9797
|
* Issues access + refresh tokens for a successfully authenticated platform admin.
|
|
9782
9798
|
*
|
|
@@ -9964,17 +9980,6 @@ declare class TokenManagerService {
|
|
|
9964
9980
|
* @returns The record with `familyId`, `familyCreatedAt` and `mfaVerified` normalized.
|
|
9965
9981
|
*/
|
|
9966
9982
|
private withOptionalFields;
|
|
9967
|
-
/**
|
|
9968
|
-
* Serializes a session record for storage, omitting an empty `familyId`.
|
|
9969
|
-
*
|
|
9970
|
-
* rust-auth skips the field when empty (`skip_serializing_if`), so emitting `"familyId":""`
|
|
9971
|
-
* here would make the same session serialize to different bytes on each side and break the
|
|
9972
|
-
* shared-Redis contract. A record with no family simply has no key.
|
|
9973
|
-
*
|
|
9974
|
-
* @param session - The record to store.
|
|
9975
|
-
* @returns The JSON string written under an `rt:`/`prt:` key.
|
|
9976
|
-
*/
|
|
9977
|
-
private serializeSession;
|
|
9978
9983
|
/**
|
|
9979
9984
|
* Constructs a session record from identity fields and request metadata.
|
|
9980
9985
|
*
|
|
@@ -11405,12 +11410,22 @@ declare class ReauthenticationService {
|
|
|
11405
11410
|
* @param userId - The account being changed, part of the counter's key.
|
|
11406
11411
|
* @param passwordHash - The account's stored hash, or `null` for an OAuth-only account.
|
|
11407
11412
|
* @param password - The password the caller submitted, if any.
|
|
11413
|
+
* @param tenantId - The caller's tenant on the dashboard plane, part of the key.
|
|
11414
|
+
* @param sid - The caller's session, from its token; only an account with no password reads it.
|
|
11408
11415
|
* @throws {@link AuthException} `ACCOUNT_LOCKED` once the failure budget for this flow is
|
|
11409
11416
|
* spent, or `INVALID_CREDENTIALS` when the account has a password and the submitted one is
|
|
11410
11417
|
* absent or wrong. The latter is deliberately the same code a failed login returns: an
|
|
11411
11418
|
* attacker holding a stolen token learns nothing from it that a failed login does not tell them.
|
|
11412
11419
|
*/
|
|
11413
|
-
assertReauthenticated(context: MfaFlowPlane, userId: string, passwordHash: string | null, password: string | undefined, tenantId: string | undefined): Promise<void>;
|
|
11420
|
+
assertReauthenticated(context: MfaFlowPlane, userId: string, passwordHash: string | null, password: string | undefined, tenantId: string | undefined, sid: string | undefined): Promise<void>;
|
|
11421
|
+
/**
|
|
11422
|
+
* The recent-authentication mark of the caller's own session, or `null` when there is none.
|
|
11423
|
+
*
|
|
11424
|
+
* The proof is the caller's own: the mark is keyed by the session that earned it, so another
|
|
11425
|
+
* session of the same account cannot borrow it. A token with no usable `sid`, one minted before
|
|
11426
|
+
* the claim existed, names no session and earns nothing, so no key is read for it.
|
|
11427
|
+
*/
|
|
11428
|
+
private recentMark;
|
|
11414
11429
|
}
|
|
11415
11430
|
|
|
11416
11431
|
/**
|
|
@@ -11482,6 +11497,7 @@ declare class MfaService {
|
|
|
11482
11497
|
*
|
|
11483
11498
|
* @param userId - Internal ID of the user enabling MFA.
|
|
11484
11499
|
* @param context - Which repository to use: `'dashboard'` (default) or `'platform'`.
|
|
11500
|
+
* @param sid - The caller's session (its token's `sid`), for an account with no local password.
|
|
11485
11501
|
* @returns Setup result containing the secret, QR URI, and plain recovery codes.
|
|
11486
11502
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
11487
11503
|
* @throws `MFA_NOT_ENABLED` if `context === 'platform'` and the platform user
|
|
@@ -11489,7 +11505,7 @@ declare class MfaService {
|
|
|
11489
11505
|
* reused (rather than introducing a new one) because the consumer-facing
|
|
11490
11506
|
* meaning is the same: "platform MFA cannot operate".
|
|
11491
11507
|
*/
|
|
11492
|
-
setup(userId: string, context?: 'dashboard' | 'platform', password?: string, tenantId?: string): Promise<MfaSetupResult>;
|
|
11508
|
+
setup(userId: string, context?: 'dashboard' | 'platform', password?: string, tenantId?: string, sid?: string): Promise<MfaSetupResult>;
|
|
11493
11509
|
/**
|
|
11494
11510
|
* Verifies the first TOTP code from the user's authenticator app and permanently
|
|
11495
11511
|
* enables MFA on the account.
|
|
@@ -14159,7 +14175,7 @@ declare class WorkspaceCallerService {
|
|
|
14159
14175
|
* lockout ceiling, `auth.reauthentication_required` for a passwordless account with no recent
|
|
14160
14176
|
* sign-in.
|
|
14161
14177
|
*/
|
|
14162
|
-
reauthenticate(caller: Pick<DashboardJwtPayload, 'sub' | 'tenantId'>, password: string | undefined): Promise<void>;
|
|
14178
|
+
reauthenticate(caller: Pick<DashboardJwtPayload, 'sub' | 'tenantId' | 'sid'>, password: string | undefined): Promise<void>;
|
|
14163
14179
|
private find;
|
|
14164
14180
|
}
|
|
14165
14181
|
|
|
@@ -14308,7 +14324,7 @@ declare class WorkspaceService {
|
|
|
14308
14324
|
*/
|
|
14309
14325
|
|
|
14310
14326
|
/** The verified token of the caller: who they are and the tenant they belong to. */
|
|
14311
|
-
type Caller = Pick<DashboardJwtPayload, 'sub' | 'tenantId'>;
|
|
14327
|
+
type Caller = Pick<DashboardJwtPayload, 'sub' | 'tenantId' | 'sid'>;
|
|
14312
14328
|
/** What a hand-over of ownership names: the receiver, the role the giver keeps, and the giver's proof. */
|
|
14313
14329
|
interface HandOver {
|
|
14314
14330
|
/** The member who becomes an owner. */
|
package/dist/server/index.d.ts
CHANGED
|
@@ -4063,6 +4063,12 @@ declare class NoOpAuthHooks implements IAuthHooks {
|
|
|
4063
4063
|
interface DashboardJwtPayload {
|
|
4064
4064
|
/** Unique JWT ID (UUID v4) generated by TokenManagerService for revocation. */
|
|
4065
4065
|
jti: string;
|
|
4066
|
+
/**
|
|
4067
|
+
* The session this token belongs to: the refresh family id, unchanged through every rotation.
|
|
4068
|
+
* Absent on a token minted before the claim existed, which the re-authentication gate treats as
|
|
4069
|
+
* a session that earned no recent authentication.
|
|
4070
|
+
*/
|
|
4071
|
+
sid?: string | undefined;
|
|
4066
4072
|
/** Subject — the authenticated user's internal identifier. */
|
|
4067
4073
|
sub: string;
|
|
4068
4074
|
/** Tenant the user belongs to; used for data isolation. */
|
|
@@ -4119,6 +4125,8 @@ interface DashboardJwtPayload {
|
|
|
4119
4125
|
interface PlatformJwtPayload {
|
|
4120
4126
|
/** Unique JWT ID (UUID v4) generated by TokenManagerService for revocation. */
|
|
4121
4127
|
jti: string;
|
|
4128
|
+
/** The session this token belongs to, as on {@link DashboardJwtPayload.sid}. */
|
|
4129
|
+
sid?: string | undefined;
|
|
4122
4130
|
/** Subject — the platform administrator's internal identifier. */
|
|
4123
4131
|
sub: string;
|
|
4124
4132
|
/** Administrator's platform-level role (e.g. `'super_admin'`, `'support'`). */
|
|
@@ -9777,6 +9785,14 @@ declare class TokenManagerService {
|
|
|
9777
9785
|
mfaVerified?: boolean;
|
|
9778
9786
|
credentialProved?: boolean;
|
|
9779
9787
|
}): Promise<AuthResult>;
|
|
9788
|
+
/**
|
|
9789
|
+
* Records that the session just born presented a credential.
|
|
9790
|
+
*
|
|
9791
|
+
* Only a credential presented in this request proves a recent authentication; a refresh proves
|
|
9792
|
+
* possession of a token, so `reissueTokens` never writes this mark. The key names the session, so
|
|
9793
|
+
* another session of the same account cannot use it.
|
|
9794
|
+
*/
|
|
9795
|
+
private markRecentAuth;
|
|
9780
9796
|
/**
|
|
9781
9797
|
* Issues access + refresh tokens for a successfully authenticated platform admin.
|
|
9782
9798
|
*
|
|
@@ -9964,17 +9980,6 @@ declare class TokenManagerService {
|
|
|
9964
9980
|
* @returns The record with `familyId`, `familyCreatedAt` and `mfaVerified` normalized.
|
|
9965
9981
|
*/
|
|
9966
9982
|
private withOptionalFields;
|
|
9967
|
-
/**
|
|
9968
|
-
* Serializes a session record for storage, omitting an empty `familyId`.
|
|
9969
|
-
*
|
|
9970
|
-
* rust-auth skips the field when empty (`skip_serializing_if`), so emitting `"familyId":""`
|
|
9971
|
-
* here would make the same session serialize to different bytes on each side and break the
|
|
9972
|
-
* shared-Redis contract. A record with no family simply has no key.
|
|
9973
|
-
*
|
|
9974
|
-
* @param session - The record to store.
|
|
9975
|
-
* @returns The JSON string written under an `rt:`/`prt:` key.
|
|
9976
|
-
*/
|
|
9977
|
-
private serializeSession;
|
|
9978
9983
|
/**
|
|
9979
9984
|
* Constructs a session record from identity fields and request metadata.
|
|
9980
9985
|
*
|
|
@@ -11405,12 +11410,22 @@ declare class ReauthenticationService {
|
|
|
11405
11410
|
* @param userId - The account being changed, part of the counter's key.
|
|
11406
11411
|
* @param passwordHash - The account's stored hash, or `null` for an OAuth-only account.
|
|
11407
11412
|
* @param password - The password the caller submitted, if any.
|
|
11413
|
+
* @param tenantId - The caller's tenant on the dashboard plane, part of the key.
|
|
11414
|
+
* @param sid - The caller's session, from its token; only an account with no password reads it.
|
|
11408
11415
|
* @throws {@link AuthException} `ACCOUNT_LOCKED` once the failure budget for this flow is
|
|
11409
11416
|
* spent, or `INVALID_CREDENTIALS` when the account has a password and the submitted one is
|
|
11410
11417
|
* absent or wrong. The latter is deliberately the same code a failed login returns: an
|
|
11411
11418
|
* attacker holding a stolen token learns nothing from it that a failed login does not tell them.
|
|
11412
11419
|
*/
|
|
11413
|
-
assertReauthenticated(context: MfaFlowPlane, userId: string, passwordHash: string | null, password: string | undefined, tenantId: string | undefined): Promise<void>;
|
|
11420
|
+
assertReauthenticated(context: MfaFlowPlane, userId: string, passwordHash: string | null, password: string | undefined, tenantId: string | undefined, sid: string | undefined): Promise<void>;
|
|
11421
|
+
/**
|
|
11422
|
+
* The recent-authentication mark of the caller's own session, or `null` when there is none.
|
|
11423
|
+
*
|
|
11424
|
+
* The proof is the caller's own: the mark is keyed by the session that earned it, so another
|
|
11425
|
+
* session of the same account cannot borrow it. A token with no usable `sid`, one minted before
|
|
11426
|
+
* the claim existed, names no session and earns nothing, so no key is read for it.
|
|
11427
|
+
*/
|
|
11428
|
+
private recentMark;
|
|
11414
11429
|
}
|
|
11415
11430
|
|
|
11416
11431
|
/**
|
|
@@ -11482,6 +11497,7 @@ declare class MfaService {
|
|
|
11482
11497
|
*
|
|
11483
11498
|
* @param userId - Internal ID of the user enabling MFA.
|
|
11484
11499
|
* @param context - Which repository to use: `'dashboard'` (default) or `'platform'`.
|
|
11500
|
+
* @param sid - The caller's session (its token's `sid`), for an account with no local password.
|
|
11485
11501
|
* @returns Setup result containing the secret, QR URI, and plain recovery codes.
|
|
11486
11502
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
11487
11503
|
* @throws `MFA_NOT_ENABLED` if `context === 'platform'` and the platform user
|
|
@@ -11489,7 +11505,7 @@ declare class MfaService {
|
|
|
11489
11505
|
* reused (rather than introducing a new one) because the consumer-facing
|
|
11490
11506
|
* meaning is the same: "platform MFA cannot operate".
|
|
11491
11507
|
*/
|
|
11492
|
-
setup(userId: string, context?: 'dashboard' | 'platform', password?: string, tenantId?: string): Promise<MfaSetupResult>;
|
|
11508
|
+
setup(userId: string, context?: 'dashboard' | 'platform', password?: string, tenantId?: string, sid?: string): Promise<MfaSetupResult>;
|
|
11493
11509
|
/**
|
|
11494
11510
|
* Verifies the first TOTP code from the user's authenticator app and permanently
|
|
11495
11511
|
* enables MFA on the account.
|
|
@@ -14159,7 +14175,7 @@ declare class WorkspaceCallerService {
|
|
|
14159
14175
|
* lockout ceiling, `auth.reauthentication_required` for a passwordless account with no recent
|
|
14160
14176
|
* sign-in.
|
|
14161
14177
|
*/
|
|
14162
|
-
reauthenticate(caller: Pick<DashboardJwtPayload, 'sub' | 'tenantId'>, password: string | undefined): Promise<void>;
|
|
14178
|
+
reauthenticate(caller: Pick<DashboardJwtPayload, 'sub' | 'tenantId' | 'sid'>, password: string | undefined): Promise<void>;
|
|
14163
14179
|
private find;
|
|
14164
14180
|
}
|
|
14165
14181
|
|
|
@@ -14308,7 +14324,7 @@ declare class WorkspaceService {
|
|
|
14308
14324
|
*/
|
|
14309
14325
|
|
|
14310
14326
|
/** The verified token of the caller: who they are and the tenant they belong to. */
|
|
14311
|
-
type Caller = Pick<DashboardJwtPayload, 'sub' | 'tenantId'>;
|
|
14327
|
+
type Caller = Pick<DashboardJwtPayload, 'sub' | 'tenantId' | 'sid'>;
|
|
14312
14328
|
/** What a hand-over of ownership names: the receiver, the role the giver keeps, and the giver's proof. */
|
|
14313
14329
|
interface HandOver {
|
|
14314
14330
|
/** The member who becomes an owner. */
|
package/dist/server/index.mjs
CHANGED
|
@@ -6392,11 +6392,18 @@ SessionService = _ts_decorate18([
|
|
|
6392
6392
|
|
|
6393
6393
|
// src/server/constants/recent-auth.ts
|
|
6394
6394
|
var RECENT_AUTH_TTL_SECONDS = 300;
|
|
6395
|
-
function recentAuthKey(plane, userId, hmacKey, tenantId) {
|
|
6396
|
-
return `ra:${hmacSha256(userSubject(plane, userId, tenantId), hmacKey)}`;
|
|
6395
|
+
function recentAuthKey(plane, userId, hmacKey, tenantId, sid) {
|
|
6396
|
+
return `ra:${hmacSha256(userSubject(plane, userId, tenantId), hmacKey)}:${sid}`;
|
|
6397
6397
|
}
|
|
6398
6398
|
__name(recentAuthKey, "recentAuthKey");
|
|
6399
6399
|
|
|
6400
|
+
// src/server/constants/session-id.ts
|
|
6401
|
+
var SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
6402
|
+
function sessionId(value) {
|
|
6403
|
+
return typeof value === "string" && SESSION_ID.test(value) ? value : void 0;
|
|
6404
|
+
}
|
|
6405
|
+
__name(sessionId, "sessionId");
|
|
6406
|
+
|
|
6400
6407
|
// src/server/utils/owner-fragment.ts
|
|
6401
6408
|
var UNKNOWN_OWNER = "userId=<unknown: no live session remains in this family to name it>";
|
|
6402
6409
|
function ownerFragment(ownerId) {
|
|
@@ -6404,6 +6411,14 @@ function ownerFragment(ownerId) {
|
|
|
6404
6411
|
}
|
|
6405
6412
|
__name(ownerFragment, "ownerFragment");
|
|
6406
6413
|
|
|
6414
|
+
// src/server/utils/serialize-session.ts
|
|
6415
|
+
function serializeSession(session) {
|
|
6416
|
+
if (session.familyId !== "") return JSON.stringify(session);
|
|
6417
|
+
const { familyId: _omitted, familyCreatedAt: _alsoOmitted, ...rest } = session;
|
|
6418
|
+
return JSON.stringify(rest);
|
|
6419
|
+
}
|
|
6420
|
+
__name(serializeSession, "serializeSession");
|
|
6421
|
+
|
|
6407
6422
|
// src/server/services/token-manager.service.ts
|
|
6408
6423
|
function _ts_decorate19(decorators, target, key, desc) {
|
|
6409
6424
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
@@ -6539,7 +6554,9 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6539
6554
|
*/
|
|
6540
6555
|
async issueTokens(user, ip, userAgent, overrides) {
|
|
6541
6556
|
const epoch = await this.redis.getUserTokenEpoch(user.id, user.tenantId, "dashboard");
|
|
6557
|
+
const familyId = randomUUID();
|
|
6542
6558
|
const accessToken = this.issueAccess({
|
|
6559
|
+
sid: familyId,
|
|
6543
6560
|
sub: user.id,
|
|
6544
6561
|
tenantId: user.tenantId,
|
|
6545
6562
|
role: user.role,
|
|
@@ -6551,27 +6568,34 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6551
6568
|
});
|
|
6552
6569
|
const rawRefreshToken = generateSecureToken();
|
|
6553
6570
|
const tokenHash = sha256(rawRefreshToken);
|
|
6554
|
-
const familyId = randomUUID();
|
|
6555
6571
|
const session = this.buildSession(user.id, user.tenantId, user.role, ip, userAgent, user.mfaEnabled, familyId, void 0, overrides?.mfaVerified === true);
|
|
6556
6572
|
const ttl = this.options.jwt.refreshExpiresInDays * 86400;
|
|
6557
6573
|
await this.redis.writeNewSession({
|
|
6558
6574
|
kind: "dashboard",
|
|
6559
6575
|
tenantId: user.tenantId,
|
|
6560
6576
|
tokenHash,
|
|
6561
|
-
sessionJson:
|
|
6577
|
+
sessionJson: serializeSession(session),
|
|
6562
6578
|
familyId,
|
|
6563
6579
|
userId: user.id,
|
|
6564
6580
|
refreshTtl: ttl
|
|
6565
6581
|
});
|
|
6566
|
-
if (overrides?.credentialProved === true)
|
|
6567
|
-
await this.redis.set(recentAuthKey("dashboard", user.id, this.options.hmacKey, user.tenantId), "1", RECENT_AUTH_TTL_SECONDS);
|
|
6568
|
-
}
|
|
6582
|
+
if (overrides?.credentialProved === true) await this.markRecentAuth(user, familyId);
|
|
6569
6583
|
return {
|
|
6570
6584
|
user,
|
|
6571
6585
|
accessToken,
|
|
6572
6586
|
rawRefreshToken
|
|
6573
6587
|
};
|
|
6574
6588
|
}
|
|
6589
|
+
/**
|
|
6590
|
+
* Records that the session just born presented a credential.
|
|
6591
|
+
*
|
|
6592
|
+
* Only a credential presented in this request proves a recent authentication; a refresh proves
|
|
6593
|
+
* possession of a token, so `reissueTokens` never writes this mark. The key names the session, so
|
|
6594
|
+
* another session of the same account cannot use it.
|
|
6595
|
+
*/
|
|
6596
|
+
async markRecentAuth(user, sid) {
|
|
6597
|
+
await this.redis.set(recentAuthKey("dashboard", user.id, this.options.hmacKey, user.tenantId, sid), "1", RECENT_AUTH_TTL_SECONDS);
|
|
6598
|
+
}
|
|
6575
6599
|
// ---------------------------------------------------------------------------
|
|
6576
6600
|
// Platform tokens
|
|
6577
6601
|
// ---------------------------------------------------------------------------
|
|
@@ -6589,7 +6613,9 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6589
6613
|
*/
|
|
6590
6614
|
async issuePlatformTokens(admin, ip, userAgent, overrides) {
|
|
6591
6615
|
const epoch = await this.redis.getUserTokenEpoch(admin.id, void 0, "platform");
|
|
6616
|
+
const familyId = randomUUID();
|
|
6592
6617
|
const accessToken = this.issuePlatformAccess({
|
|
6618
|
+
sid: familyId,
|
|
6593
6619
|
sub: admin.id,
|
|
6594
6620
|
role: admin.role,
|
|
6595
6621
|
type: "platform",
|
|
@@ -6599,14 +6625,13 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6599
6625
|
});
|
|
6600
6626
|
const rawRefreshToken = generateSecureToken();
|
|
6601
6627
|
const tokenHash = sha256(rawRefreshToken);
|
|
6602
|
-
const familyId = randomUUID();
|
|
6603
6628
|
const session = this.buildSession(admin.id, "", admin.role, ip, userAgent, admin.mfaEnabled, familyId, void 0, overrides?.mfaVerified === true);
|
|
6604
6629
|
const ttl = this.options.jwt.refreshExpiresInDays * 86400;
|
|
6605
6630
|
await this.redis.writeNewSession({
|
|
6606
6631
|
kind: "platform",
|
|
6607
6632
|
tenantId: void 0,
|
|
6608
6633
|
tokenHash,
|
|
6609
|
-
sessionJson:
|
|
6634
|
+
sessionJson: serializeSession(session),
|
|
6610
6635
|
familyId,
|
|
6611
6636
|
userId: admin.id,
|
|
6612
6637
|
refreshTtl: ttl
|
|
@@ -6690,7 +6715,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6690
6715
|
tenantId: seed.tenantId,
|
|
6691
6716
|
oldHash,
|
|
6692
6717
|
newHash,
|
|
6693
|
-
newSessionJson:
|
|
6718
|
+
newSessionJson: serializeSession(newSession),
|
|
6694
6719
|
familyId: seed.familyId,
|
|
6695
6720
|
userId: seed.userId,
|
|
6696
6721
|
refreshTtl,
|
|
@@ -6868,7 +6893,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6868
6893
|
kind: "dashboard",
|
|
6869
6894
|
tenantId: graceSession.tenantId,
|
|
6870
6895
|
newHash: anotherNewHash,
|
|
6871
|
-
newSessionJson:
|
|
6896
|
+
newSessionJson: serializeSession(anotherSession),
|
|
6872
6897
|
familyId: graceSession.familyId,
|
|
6873
6898
|
userId: graceSession.userId,
|
|
6874
6899
|
refreshTtl
|
|
@@ -6930,21 +6955,6 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6930
6955
|
};
|
|
6931
6956
|
}
|
|
6932
6957
|
/**
|
|
6933
|
-
* Serializes a session record for storage, omitting an empty `familyId`.
|
|
6934
|
-
*
|
|
6935
|
-
* rust-auth skips the field when empty (`skip_serializing_if`), so emitting `"familyId":""`
|
|
6936
|
-
* here would make the same session serialize to different bytes on each side and break the
|
|
6937
|
-
* shared-Redis contract. A record with no family simply has no key.
|
|
6938
|
-
*
|
|
6939
|
-
* @param session - The record to store.
|
|
6940
|
-
* @returns The JSON string written under an `rt:`/`prt:` key.
|
|
6941
|
-
*/
|
|
6942
|
-
serializeSession(session) {
|
|
6943
|
-
if (session.familyId !== "") return JSON.stringify(session);
|
|
6944
|
-
const { familyId: _omitted, familyCreatedAt: _alsoOmitted, ...rest } = session;
|
|
6945
|
-
return JSON.stringify(rest);
|
|
6946
|
-
}
|
|
6947
|
-
/**
|
|
6948
6958
|
* Constructs a session record from identity fields and request metadata.
|
|
6949
6959
|
*
|
|
6950
6960
|
* @param userId - Internal user ID for whom the session is being created.
|
|
@@ -7005,6 +7015,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7005
7015
|
async buildRotatedResult(session, rawRefreshToken) {
|
|
7006
7016
|
const epoch = await this.redis.getUserTokenEpoch(session.userId, session.tenantId, "dashboard");
|
|
7007
7017
|
const accessToken = this.issueAccess({
|
|
7018
|
+
sid: sessionId(session.familyId),
|
|
7008
7019
|
sub: session.userId,
|
|
7009
7020
|
tenantId: session.tenantId,
|
|
7010
7021
|
role: session.role,
|
|
@@ -7052,7 +7063,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7052
7063
|
tenantId: void 0,
|
|
7053
7064
|
oldHash,
|
|
7054
7065
|
newHash,
|
|
7055
|
-
newSessionJson:
|
|
7066
|
+
newSessionJson: serializeSession(newSession),
|
|
7056
7067
|
familyId: seed.familyId,
|
|
7057
7068
|
userId: seed.userId,
|
|
7058
7069
|
refreshTtl,
|
|
@@ -7105,7 +7116,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7105
7116
|
kind: "platform",
|
|
7106
7117
|
tenantId: void 0,
|
|
7107
7118
|
newHash: anotherNewHash,
|
|
7108
|
-
newSessionJson:
|
|
7119
|
+
newSessionJson: serializeSession(anotherSession),
|
|
7109
7120
|
familyId: graceSession.familyId,
|
|
7110
7121
|
userId: graceSession.userId,
|
|
7111
7122
|
refreshTtl
|
|
@@ -7127,6 +7138,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7127
7138
|
async buildPlatformRotatedResult(session, rawRefreshToken) {
|
|
7128
7139
|
const epoch = await this.redis.getUserTokenEpoch(session.userId, void 0, "platform");
|
|
7129
7140
|
const accessToken = this.issuePlatformAccess({
|
|
7141
|
+
sid: sessionId(session.familyId),
|
|
7130
7142
|
sub: session.userId,
|
|
7131
7143
|
role: session.role,
|
|
7132
7144
|
type: "platform",
|
|
@@ -7167,6 +7179,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7167
7179
|
*/
|
|
7168
7180
|
async reissuePlatformAccessWithAuthority(claims, role, mfaEnabled) {
|
|
7169
7181
|
return this.issuePlatformAccess({
|
|
7182
|
+
sid: claims.sid,
|
|
7170
7183
|
sub: claims.sub,
|
|
7171
7184
|
role,
|
|
7172
7185
|
type: "platform",
|
|
@@ -8027,6 +8040,7 @@ var AuthService = class _AuthService {
|
|
|
8027
8040
|
let authoritative = result.accessToken;
|
|
8028
8041
|
if (rotated.role !== user.role || rotated.tenantId !== user.tenantId || rotated.mfaEnabled !== user.mfaEnabled) {
|
|
8029
8042
|
authoritative = this.tokenManager.issueAccess({
|
|
8043
|
+
sid: rotated.sid,
|
|
8030
8044
|
sub: user.id,
|
|
8031
8045
|
tenantId: user.tenantId,
|
|
8032
8046
|
role: user.role,
|
|
@@ -8034,8 +8048,7 @@ var AuthService = class _AuthService {
|
|
|
8034
8048
|
status: user.status,
|
|
8035
8049
|
mfaEnabled: user.mfaEnabled,
|
|
8036
8050
|
// Carried across from the token the rotation just produced: a second factor already
|
|
8037
|
-
// cleared on this session stays cleared, and re-stamping
|
|
8038
|
-
// silently demand it again.
|
|
8051
|
+
// cleared on this session stays cleared, and re-stamping must not demand it again.
|
|
8039
8052
|
mfaVerified: rotated.mfaVerified,
|
|
8040
8053
|
epoch: await this.redis.getUserTokenEpoch(user.id, user.tenantId, "dashboard")
|
|
8041
8054
|
});
|
|
@@ -11949,14 +11962,16 @@ var ReauthenticationService = class {
|
|
|
11949
11962
|
* @param userId - The account being changed, part of the counter's key.
|
|
11950
11963
|
* @param passwordHash - The account's stored hash, or `null` for an OAuth-only account.
|
|
11951
11964
|
* @param password - The password the caller submitted, if any.
|
|
11965
|
+
* @param tenantId - The caller's tenant on the dashboard plane, part of the key.
|
|
11966
|
+
* @param sid - The caller's session, from its token; only an account with no password reads it.
|
|
11952
11967
|
* @throws {@link AuthException} `ACCOUNT_LOCKED` once the failure budget for this flow is
|
|
11953
11968
|
* spent, or `INVALID_CREDENTIALS` when the account has a password and the submitted one is
|
|
11954
11969
|
* absent or wrong. The latter is deliberately the same code a failed login returns: an
|
|
11955
11970
|
* attacker holding a stolen token learns nothing from it that a failed login does not tell them.
|
|
11956
11971
|
*/
|
|
11957
|
-
async assertReauthenticated(context, userId, passwordHash, password, tenantId) {
|
|
11972
|
+
async assertReauthenticated(context, userId, passwordHash, password, tenantId, sid) {
|
|
11958
11973
|
if (passwordHash === null) {
|
|
11959
|
-
const recent = await this.
|
|
11974
|
+
const recent = await this.recentMark(context, userId, tenantId, sid);
|
|
11960
11975
|
if (recent === null) {
|
|
11961
11976
|
this.logger.warn(`reauthenticate: no recent authentication userId=${logSafe(userId)} context=${context}`);
|
|
11962
11977
|
throw new AuthException(AUTH_ERROR_CODES.REAUTHENTICATION_REQUIRED);
|
|
@@ -11975,6 +11990,18 @@ var ReauthenticationService = class {
|
|
|
11975
11990
|
}
|
|
11976
11991
|
await this.lockout.reset("reauth", context, userId, tenantId);
|
|
11977
11992
|
}
|
|
11993
|
+
/**
|
|
11994
|
+
* The recent-authentication mark of the caller's own session, or `null` when there is none.
|
|
11995
|
+
*
|
|
11996
|
+
* The proof is the caller's own: the mark is keyed by the session that earned it, so another
|
|
11997
|
+
* session of the same account cannot borrow it. A token with no usable `sid`, one minted before
|
|
11998
|
+
* the claim existed, names no session and earns nothing, so no key is read for it.
|
|
11999
|
+
*/
|
|
12000
|
+
async recentMark(context, userId, tenantId, sid) {
|
|
12001
|
+
const bound = sessionId(sid);
|
|
12002
|
+
if (bound === void 0) return null;
|
|
12003
|
+
return await this.redis.get(recentAuthKey(context, userId, this.options.hmacKey, tenantId, bound));
|
|
12004
|
+
}
|
|
11978
12005
|
};
|
|
11979
12006
|
ReauthenticationService = _ts_decorate41([
|
|
11980
12007
|
Injectable(),
|
|
@@ -12121,6 +12148,7 @@ var MfaService = class _MfaService {
|
|
|
12121
12148
|
*
|
|
12122
12149
|
* @param userId - Internal ID of the user enabling MFA.
|
|
12123
12150
|
* @param context - Which repository to use: `'dashboard'` (default) or `'platform'`.
|
|
12151
|
+
* @param sid - The caller's session (its token's `sid`), for an account with no local password.
|
|
12124
12152
|
* @returns Setup result containing the secret, QR URI, and plain recovery codes.
|
|
12125
12153
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
12126
12154
|
* @throws `MFA_NOT_ENABLED` if `context === 'platform'` and the platform user
|
|
@@ -12128,14 +12156,14 @@ var MfaService = class _MfaService {
|
|
|
12128
12156
|
* reused (rather than introducing a new one) because the consumer-facing
|
|
12129
12157
|
* meaning is the same: "platform MFA cannot operate".
|
|
12130
12158
|
*/
|
|
12131
|
-
async setup(userId, context = "dashboard", password, tenantId) {
|
|
12159
|
+
async setup(userId, context = "dashboard", password, tenantId, sid) {
|
|
12132
12160
|
assertPlaneTenant(context, tenantId);
|
|
12133
12161
|
if (context === "platform" && !this.platformUserRepo) {
|
|
12134
12162
|
throw new AuthException(AUTH_ERROR_CODES.MFA_NOT_ENABLED);
|
|
12135
12163
|
}
|
|
12136
12164
|
const user = await this.records.loadAccount(context, userId, tenantId);
|
|
12137
12165
|
if (user.mfaEnabled) throw new AuthException(AUTH_ERROR_CODES.MFA_ALREADY_ENABLED);
|
|
12138
|
-
await this.reauthentication.assertReauthenticated(context, userId, user.passwordHash, password, tenantId);
|
|
12166
|
+
await this.reauthentication.assertReauthenticated(context, userId, user.passwordHash, password, tenantId, sid);
|
|
12139
12167
|
const setupKey = `mfa_setup:${hmacSha256(userSubject(context, userId, tenantId), this.options.hmacKey)}`;
|
|
12140
12168
|
const existingFast = await this.redis.get(setupKey);
|
|
12141
12169
|
if (existingFast !== null) {
|
|
@@ -12491,7 +12519,7 @@ var MfaController = class {
|
|
|
12491
12519
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
12492
12520
|
*/
|
|
12493
12521
|
async setup(user, dto) {
|
|
12494
|
-
return this.mfaService.setup(user.sub, "dashboard", dto.password, user.tenantId);
|
|
12522
|
+
return this.mfaService.setup(user.sub, "dashboard", dto.password, user.tenantId, user.sid);
|
|
12495
12523
|
}
|
|
12496
12524
|
/**
|
|
12497
12525
|
* Verifies the first TOTP code and permanently enables MFA on the account.
|
|
@@ -14819,7 +14847,7 @@ var PlatformMfaController = class {
|
|
|
14819
14847
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
14820
14848
|
*/
|
|
14821
14849
|
async setup(user, dto) {
|
|
14822
|
-
return this.mfaService.setup(user.sub, "platform", dto.password);
|
|
14850
|
+
return this.mfaService.setup(user.sub, "platform", dto.password, void 0, user.sid);
|
|
14823
14851
|
}
|
|
14824
14852
|
/**
|
|
14825
14853
|
* Verifies the first TOTP code and permanently enables MFA on the platform
|
|
@@ -18492,7 +18520,7 @@ var WorkspaceCallerService = class {
|
|
|
18492
18520
|
*/
|
|
18493
18521
|
async reauthenticate(caller, password) {
|
|
18494
18522
|
const user = await this.find(caller);
|
|
18495
|
-
await this.reauthentication.assertReauthenticated("dashboard", user.id, user.passwordHash, password, caller.tenantId);
|
|
18523
|
+
await this.reauthentication.assertReauthenticated("dashboard", user.id, user.passwordHash, password, caller.tenantId, caller.sid);
|
|
18496
18524
|
}
|
|
18497
18525
|
async find(caller) {
|
|
18498
18526
|
const user = await this.users.findById({
|
package/dist/shared/index.d.cts
CHANGED
|
@@ -1059,6 +1059,12 @@ declare class AuthClientError extends Error {
|
|
|
1059
1059
|
interface DashboardJwtPayload {
|
|
1060
1060
|
/** Unique JWT ID (UUID v4) generated by the server for revocation tracking. */
|
|
1061
1061
|
jti: string;
|
|
1062
|
+
/**
|
|
1063
|
+
* The session this token belongs to: the refresh family id, unchanged through every rotation.
|
|
1064
|
+
* Absent on a token minted before the claim existed, which the re-authentication gate treats as
|
|
1065
|
+
* a session that earned no recent authentication.
|
|
1066
|
+
*/
|
|
1067
|
+
sid?: string | undefined;
|
|
1062
1068
|
/** Subject — the authenticated user's internal identifier. */
|
|
1063
1069
|
sub: string;
|
|
1064
1070
|
/** Tenant the user belongs to; used for data isolation. */
|
|
@@ -1098,6 +1104,8 @@ interface DashboardJwtPayload {
|
|
|
1098
1104
|
interface PlatformJwtPayload {
|
|
1099
1105
|
/** Unique JWT ID (UUID v4) generated by the server for revocation tracking. */
|
|
1100
1106
|
jti: string;
|
|
1107
|
+
/** The session this token belongs to, as on {@link DashboardJwtPayload.sid}. */
|
|
1108
|
+
sid?: string | undefined;
|
|
1101
1109
|
/** Subject — the platform administrator's internal identifier. */
|
|
1102
1110
|
sub: string;
|
|
1103
1111
|
/** Administrator's platform-level role (e.g. `'super_admin'`, `'support'`). */
|
package/dist/shared/index.d.ts
CHANGED
|
@@ -1059,6 +1059,12 @@ declare class AuthClientError extends Error {
|
|
|
1059
1059
|
interface DashboardJwtPayload {
|
|
1060
1060
|
/** Unique JWT ID (UUID v4) generated by the server for revocation tracking. */
|
|
1061
1061
|
jti: string;
|
|
1062
|
+
/**
|
|
1063
|
+
* The session this token belongs to: the refresh family id, unchanged through every rotation.
|
|
1064
|
+
* Absent on a token minted before the claim existed, which the re-authentication gate treats as
|
|
1065
|
+
* a session that earned no recent authentication.
|
|
1066
|
+
*/
|
|
1067
|
+
sid?: string | undefined;
|
|
1062
1068
|
/** Subject — the authenticated user's internal identifier. */
|
|
1063
1069
|
sub: string;
|
|
1064
1070
|
/** Tenant the user belongs to; used for data isolation. */
|
|
@@ -1098,6 +1104,8 @@ interface DashboardJwtPayload {
|
|
|
1098
1104
|
interface PlatformJwtPayload {
|
|
1099
1105
|
/** Unique JWT ID (UUID v4) generated by the server for revocation tracking. */
|
|
1100
1106
|
jti: string;
|
|
1107
|
+
/** The session this token belongs to, as on {@link DashboardJwtPayload.sid}. */
|
|
1108
|
+
sid?: string | undefined;
|
|
1101
1109
|
/** Subject — the platform administrator's internal identifier. */
|
|
1102
1110
|
sub: string;
|
|
1103
1111
|
/** Administrator's platform-level role (e.g. `'super_admin'`, `'support'`). */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bymax-one/nest-auth",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.6",
|
|
4
4
|
"description": "Full-stack authentication and authorization package for NestJS, React and Next.js — JWT, MFA, OAuth, sessions, multi-tenant SaaS ready",
|
|
5
5
|
"author": "Bymax One <support@bymax.one>",
|
|
6
6
|
"license": "MIT",
|