@bymax-one/nest-auth 1.5.5 → 1.5.7
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 +77 -1
- package/README.md +7 -1
- package/dist/react/index.cjs +3 -1
- package/dist/react/index.d.cts +3 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.mjs +3 -1
- package/dist/server/index.cjs +236 -112
- package/dist/server/index.d.cts +97 -23
- package/dist/server/index.d.ts +97 -23
- package/dist/server/index.mjs +236 -113
- package/dist/shared/index.d.cts +8 -0
- package/dist/shared/index.d.ts +8 -0
- package/package.json +1 -1
package/dist/server/index.cjs
CHANGED
|
@@ -2430,6 +2430,42 @@ var AuthException = class extends common.HttpException {
|
|
|
2430
2430
|
}
|
|
2431
2431
|
};
|
|
2432
2432
|
|
|
2433
|
+
// src/server/redis/create-session-script.ts
|
|
2434
|
+
var CREATE_SESSION_LUA = `
|
|
2435
|
+
-- The commands that can fail on a key of the wrong type come FIRST: Redis does not undo the
|
|
2436
|
+
-- writes a script made before one of its commands fails, so an error here must leave no refresh
|
|
2437
|
+
-- record behind. The index and the family are the only keys this script adds to a set.
|
|
2438
|
+
redis.call('SADD', KEYS[2], ARGV[4] .. ':' .. ARGV[5])
|
|
2439
|
+
redis.call('EXPIRE', KEYS[2], ARGV[2])
|
|
2440
|
+
-- The family index holds bare hashes: it only ever tracks live sessions, so the prefix is
|
|
2441
|
+
-- implied. A session with no lineage skips it rather than writing an unkeyed member.
|
|
2442
|
+
if ARGV[3] ~= '' then
|
|
2443
|
+
redis.call('SADD', KEYS[3], ARGV[5])
|
|
2444
|
+
redis.call('EXPIRE', KEYS[3], ARGV[2])
|
|
2445
|
+
end
|
|
2446
|
+
redis.call('SET', KEYS[1], ARGV[1], 'EX', ARGV[2])
|
|
2447
|
+
-- The records of the session itself, each with its own lifetime.
|
|
2448
|
+
for i = 4, #KEYS do
|
|
2449
|
+
redis.call('SET', KEYS[i], ARGV[2 * i - 2], 'EX', ARGV[2 * i - 1])
|
|
2450
|
+
end
|
|
2451
|
+
return 1
|
|
2452
|
+
`;
|
|
2453
|
+
function sessionExtraArgs(extras) {
|
|
2454
|
+
for (const { key, value, ttl } of extras) {
|
|
2455
|
+
if (key === "" || value === "" || !Number.isInteger(ttl) || ttl <= 0) {
|
|
2456
|
+
throw new TypeError("A session record needs a key, a value and a positive whole-second TTL.");
|
|
2457
|
+
}
|
|
2458
|
+
}
|
|
2459
|
+
return {
|
|
2460
|
+
keys: extras.map(({ key }) => key),
|
|
2461
|
+
argv: extras.flatMap(({ value, ttl }) => [
|
|
2462
|
+
value,
|
|
2463
|
+
String(ttl)
|
|
2464
|
+
])
|
|
2465
|
+
};
|
|
2466
|
+
}
|
|
2467
|
+
__name(sessionExtraArgs, "sessionExtraArgs");
|
|
2468
|
+
|
|
2433
2469
|
// src/server/redis/redis-outage.ts
|
|
2434
2470
|
var MESSAGE_KINDS = /* @__PURE__ */ new Map([
|
|
2435
2471
|
[
|
|
@@ -2590,18 +2626,6 @@ if ARGV[3] ~= '' then
|
|
|
2590
2626
|
end
|
|
2591
2627
|
return 1
|
|
2592
2628
|
`;
|
|
2593
|
-
var CREATE_SESSION_LUA = `
|
|
2594
|
-
redis.call('SET', KEYS[1], ARGV[1], 'EX', ARGV[2])
|
|
2595
|
-
redis.call('SADD', KEYS[2], ARGV[4] .. ':' .. ARGV[5])
|
|
2596
|
-
redis.call('EXPIRE', KEYS[2], ARGV[2])
|
|
2597
|
-
-- The family index holds bare hashes: it only ever tracks live sessions, so the prefix is
|
|
2598
|
-
-- implied. A session with no lineage skips it rather than writing an unkeyed member.
|
|
2599
|
-
if ARGV[3] ~= '' then
|
|
2600
|
-
redis.call('SADD', KEYS[3], ARGV[5])
|
|
2601
|
-
redis.call('EXPIRE', KEYS[3], ARGV[2])
|
|
2602
|
-
end
|
|
2603
|
-
return 1
|
|
2604
|
-
`;
|
|
2605
2629
|
var ROTATE_LUA = `
|
|
2606
2630
|
local old = redis.call('GET', KEYS[1])
|
|
2607
2631
|
if old then
|
|
@@ -2955,18 +2979,21 @@ var AuthRedisService = class {
|
|
|
2955
2979
|
*/
|
|
2956
2980
|
async writeNewSession(params) {
|
|
2957
2981
|
const p = prefixesFor(params.kind);
|
|
2982
|
+
const extra = sessionExtraArgs(params.extras ?? []);
|
|
2958
2983
|
await this.eval(CREATE_SESSION_LUA, [
|
|
2959
2984
|
`${p.live}:${params.tokenHash}`,
|
|
2960
2985
|
sessionIndexKey(params.kind, params.userId, this.#hmacKey, params.tenantId),
|
|
2961
2986
|
// Always passed, even with no family: a Lua script's key list is fixed at the call
|
|
2962
2987
|
// site, and the script skips the key rather than the caller skipping the argument.
|
|
2963
|
-
`${p.family}:${params.familyId}
|
|
2988
|
+
`${p.family}:${params.familyId}`,
|
|
2989
|
+
...extra.keys
|
|
2964
2990
|
], [
|
|
2965
2991
|
params.sessionJson,
|
|
2966
2992
|
String(params.refreshTtl),
|
|
2967
2993
|
params.familyId,
|
|
2968
2994
|
p.live,
|
|
2969
|
-
params.tokenHash
|
|
2995
|
+
params.tokenHash,
|
|
2996
|
+
...extra.argv
|
|
2970
2997
|
]);
|
|
2971
2998
|
}
|
|
2972
2999
|
/**
|
|
@@ -4645,7 +4672,7 @@ function cacheKeysFor(ref, hmacKey) {
|
|
|
4645
4672
|
};
|
|
4646
4673
|
}
|
|
4647
4674
|
__name(cacheKeysFor, "cacheKeysFor");
|
|
4648
|
-
exports.AccountStatusService = class
|
|
4675
|
+
exports.AccountStatusService = class _AccountStatusService {
|
|
4649
4676
|
static {
|
|
4650
4677
|
__name(this, "AccountStatusService");
|
|
4651
4678
|
}
|
|
@@ -4653,6 +4680,7 @@ exports.AccountStatusService = class AccountStatusService {
|
|
|
4653
4680
|
userRepo;
|
|
4654
4681
|
options;
|
|
4655
4682
|
platformUserRepo;
|
|
4683
|
+
logger = new common.Logger(_AccountStatusService.name);
|
|
4656
4684
|
/**
|
|
4657
4685
|
* @param redis - The auth module's Redis service, holding the status and verified caches.
|
|
4658
4686
|
* @param userRepo - The consumer's dashboard user repository, read on a cache miss.
|
|
@@ -4740,10 +4768,10 @@ exports.AccountStatusService = class AccountStatusService {
|
|
|
4740
4768
|
throw new AuthException(AUTH_ERROR_CODES.TOKEN_INVALID);
|
|
4741
4769
|
}
|
|
4742
4770
|
status = userRecord.status;
|
|
4743
|
-
await this.
|
|
4771
|
+
await this.fillCache(statusKey, status, cacheTtl);
|
|
4744
4772
|
if (requireVerified) {
|
|
4745
4773
|
verified = userRecord.emailVerified ? "1" : "0";
|
|
4746
|
-
await this.
|
|
4774
|
+
await this.fillCache(verifiedKey, verified, cacheTtl);
|
|
4747
4775
|
}
|
|
4748
4776
|
}
|
|
4749
4777
|
assertNotBlocked(status, this.options.blockedStatuses);
|
|
@@ -4752,6 +4780,25 @@ exports.AccountStatusService = class AccountStatusService {
|
|
|
4752
4780
|
}
|
|
4753
4781
|
}
|
|
4754
4782
|
/**
|
|
4783
|
+
* Stores one cached fact, best effort.
|
|
4784
|
+
*
|
|
4785
|
+
* The repository has already answered when this runs, so the decision does not depend on the
|
|
4786
|
+
* write: a store that cannot take it must not turn that decision into an error, and above all
|
|
4787
|
+
* must not answer a blocked account with an outage a client retries. A failed write is logged
|
|
4788
|
+
* without the store's text and the next request reads the repository again.
|
|
4789
|
+
*
|
|
4790
|
+
* @param key - The cache key.
|
|
4791
|
+
* @param value - The fact just read from the repository.
|
|
4792
|
+
* @param ttl - The cache lifetime in seconds.
|
|
4793
|
+
*/
|
|
4794
|
+
async fillCache(key, value, ttl) {
|
|
4795
|
+
try {
|
|
4796
|
+
await this.redis.set(key, value, ttl);
|
|
4797
|
+
} catch (err) {
|
|
4798
|
+
this.logger.warn(`account status cache write failed, the next request reads the repository again: ${describeChannelStatus(err)}`);
|
|
4799
|
+
}
|
|
4800
|
+
}
|
|
4801
|
+
/**
|
|
4755
4802
|
* The same decision as {@link assertDashboardAccountUsable}, read from the repository now.
|
|
4756
4803
|
*
|
|
4757
4804
|
* Neither reads nor writes the status cache, so the answer is as current as the repository:
|
|
@@ -5636,6 +5683,63 @@ function runDetached(logger, run, failure) {
|
|
|
5636
5683
|
}
|
|
5637
5684
|
__name(runDetached, "runDetached");
|
|
5638
5685
|
|
|
5686
|
+
// src/server/utils/session-detail.ts
|
|
5687
|
+
var MAX_SESSION_FIELD_LENGTH = 45;
|
|
5688
|
+
function parseUserAgent(ua) {
|
|
5689
|
+
let browser = "Unknown Browser";
|
|
5690
|
+
if (/Edg\//.test(ua)) {
|
|
5691
|
+
browser = "Edge";
|
|
5692
|
+
} else if (/OPR\/|Opera/.test(ua)) {
|
|
5693
|
+
browser = "Opera";
|
|
5694
|
+
} else if (/Chrome\//.test(ua)) {
|
|
5695
|
+
browser = "Chrome";
|
|
5696
|
+
} else if (/Firefox\//.test(ua)) {
|
|
5697
|
+
browser = "Firefox";
|
|
5698
|
+
} else if (/Safari\//.test(ua) && /Version\//.test(ua)) {
|
|
5699
|
+
browser = "Safari";
|
|
5700
|
+
}
|
|
5701
|
+
let os = "Unknown OS";
|
|
5702
|
+
if (/Android/.test(ua)) {
|
|
5703
|
+
os = "Android";
|
|
5704
|
+
} else if (/iPhone|iPad|iPod/.test(ua)) {
|
|
5705
|
+
os = "iOS";
|
|
5706
|
+
} else if (/Windows/.test(ua)) {
|
|
5707
|
+
os = "Windows";
|
|
5708
|
+
} else if (/Macintosh|Mac OS X/.test(ua)) {
|
|
5709
|
+
os = "macOS";
|
|
5710
|
+
} else if (/Linux/.test(ua)) {
|
|
5711
|
+
os = "Linux";
|
|
5712
|
+
}
|
|
5713
|
+
return `${browser} on ${os}`;
|
|
5714
|
+
}
|
|
5715
|
+
__name(parseUserAgent, "parseUserAgent");
|
|
5716
|
+
function dashboardSessionDetail(ip, userAgent, now3) {
|
|
5717
|
+
return JSON.stringify({
|
|
5718
|
+
device: parseUserAgent(userAgent),
|
|
5719
|
+
ip: ip.slice(0, MAX_SESSION_FIELD_LENGTH),
|
|
5720
|
+
createdAt: now3,
|
|
5721
|
+
lastActivityAt: now3
|
|
5722
|
+
});
|
|
5723
|
+
}
|
|
5724
|
+
__name(dashboardSessionDetail, "dashboardSessionDetail");
|
|
5725
|
+
function platformSessionDetail(ip, userAgent, now3) {
|
|
5726
|
+
return JSON.stringify({
|
|
5727
|
+
device: userAgent.slice(0, MAX_SESSION_FIELD_LENGTH),
|
|
5728
|
+
ip: ip.slice(0, MAX_SESSION_FIELD_LENGTH),
|
|
5729
|
+
createdAt: now3,
|
|
5730
|
+
lastActivityAt: now3
|
|
5731
|
+
});
|
|
5732
|
+
}
|
|
5733
|
+
__name(platformSessionDetail, "platformSessionDetail");
|
|
5734
|
+
function platformSessionExtra(tokenHash, ip, userAgent, ttl) {
|
|
5735
|
+
return {
|
|
5736
|
+
key: `psd:${tokenHash}`,
|
|
5737
|
+
value: platformSessionDetail(ip, userAgent, Date.now()),
|
|
5738
|
+
ttl
|
|
5739
|
+
};
|
|
5740
|
+
}
|
|
5741
|
+
__name(platformSessionExtra, "platformSessionExtra");
|
|
5742
|
+
|
|
5639
5743
|
// src/server/utils/settle-writes.ts
|
|
5640
5744
|
async function settleWrites(writes) {
|
|
5641
5745
|
const failures = [];
|
|
@@ -5723,34 +5827,6 @@ function parseStoredDetail(raw) {
|
|
|
5723
5827
|
}
|
|
5724
5828
|
}
|
|
5725
5829
|
__name(parseStoredDetail, "parseStoredDetail");
|
|
5726
|
-
function parseUserAgent(ua) {
|
|
5727
|
-
let browser = "Unknown Browser";
|
|
5728
|
-
if (/Edg\//.test(ua)) {
|
|
5729
|
-
browser = "Edge";
|
|
5730
|
-
} else if (/OPR\/|Opera/.test(ua)) {
|
|
5731
|
-
browser = "Opera";
|
|
5732
|
-
} else if (/Chrome\//.test(ua)) {
|
|
5733
|
-
browser = "Chrome";
|
|
5734
|
-
} else if (/Firefox\//.test(ua)) {
|
|
5735
|
-
browser = "Firefox";
|
|
5736
|
-
} else if (/Safari\//.test(ua) && /Version\//.test(ua)) {
|
|
5737
|
-
browser = "Safari";
|
|
5738
|
-
}
|
|
5739
|
-
let os = "Unknown OS";
|
|
5740
|
-
if (/Android/.test(ua)) {
|
|
5741
|
-
os = "Android";
|
|
5742
|
-
} else if (/iPhone|iPad|iPod/.test(ua)) {
|
|
5743
|
-
os = "iOS";
|
|
5744
|
-
} else if (/Windows/.test(ua)) {
|
|
5745
|
-
os = "Windows";
|
|
5746
|
-
} else if (/Macintosh|Mac OS X/.test(ua)) {
|
|
5747
|
-
os = "macOS";
|
|
5748
|
-
} else if (/Linux/.test(ua)) {
|
|
5749
|
-
os = "Linux";
|
|
5750
|
-
}
|
|
5751
|
-
return `${browser} on ${os}`;
|
|
5752
|
-
}
|
|
5753
|
-
__name(parseUserAgent, "parseUserAgent");
|
|
5754
5830
|
function toSafeUser(user) {
|
|
5755
5831
|
const { passwordHash: _p, mfaSecret: _s, mfaRecoveryCodes: _r, ...safe } = user;
|
|
5756
5832
|
return safe;
|
|
@@ -5807,10 +5883,10 @@ exports.SessionService = class _SessionService {
|
|
|
5807
5883
|
/**
|
|
5808
5884
|
* Records a new session in Redis and enforces the concurrent session limit.
|
|
5809
5885
|
*
|
|
5810
|
-
*
|
|
5811
|
-
*
|
|
5812
|
-
* session when the user has exceeded
|
|
5813
|
-
* hook (fire-and-forget)
|
|
5886
|
+
* The device and activity record under `sd:{hash}` is written with the session itself, by
|
|
5887
|
+
* `TokenManagerService.issueTokens`, so it cannot be missing for a session that exists. This
|
|
5888
|
+
* method calls `enforceSessionLimit` to evict the oldest session when the user has exceeded
|
|
5889
|
+
* their limit, and fires the `onNewSession` hook (fire-and-forget).
|
|
5814
5890
|
*
|
|
5815
5891
|
* @remarks
|
|
5816
5892
|
* **Caller ordering contract:** This method must be called **after**
|
|
@@ -5828,17 +5904,6 @@ exports.SessionService = class _SessionService {
|
|
|
5828
5904
|
this.assertTenant(tenantId);
|
|
5829
5905
|
const hash = sha256(rawRefreshToken);
|
|
5830
5906
|
const device = parseUserAgent(userAgent);
|
|
5831
|
-
const now3 = Date.now();
|
|
5832
|
-
const ttl = this.options.jwt.refreshExpiresInDays * 86400;
|
|
5833
|
-
const detail = {
|
|
5834
|
-
device,
|
|
5835
|
-
// Truncate to MAX_IP_LENGTH before storage to guard against oversized
|
|
5836
|
-
// attacker-controlled values arriving via X-Forwarded-For.
|
|
5837
|
-
ip: ip.slice(0, MAX_IP_LENGTH),
|
|
5838
|
-
createdAt: now3,
|
|
5839
|
-
lastActivityAt: now3
|
|
5840
|
-
};
|
|
5841
|
-
await this.redis.set(`sd:${hash}`, JSON.stringify(detail), ttl);
|
|
5842
5907
|
await this.enforceSessionLimit(userId, tenantId, hash, ip, userAgent);
|
|
5843
5908
|
if (this.hooks?.onNewSession) {
|
|
5844
5909
|
const { onNewSession } = this.hooks;
|
|
@@ -6394,11 +6459,18 @@ exports.SessionService = _ts_decorate18([
|
|
|
6394
6459
|
|
|
6395
6460
|
// src/server/constants/recent-auth.ts
|
|
6396
6461
|
var RECENT_AUTH_TTL_SECONDS = 300;
|
|
6397
|
-
function recentAuthKey(plane, userId, hmacKey, tenantId) {
|
|
6398
|
-
return `ra:${hmacSha256(userSubject(plane, userId, tenantId), hmacKey)}`;
|
|
6462
|
+
function recentAuthKey(plane, userId, hmacKey, tenantId, sid) {
|
|
6463
|
+
return `ra:${hmacSha256(userSubject(plane, userId, tenantId), hmacKey)}:${sid}`;
|
|
6399
6464
|
}
|
|
6400
6465
|
__name(recentAuthKey, "recentAuthKey");
|
|
6401
6466
|
|
|
6467
|
+
// src/server/constants/session-id.ts
|
|
6468
|
+
var SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
6469
|
+
function sessionId(value) {
|
|
6470
|
+
return typeof value === "string" && SESSION_ID.test(value) ? value : void 0;
|
|
6471
|
+
}
|
|
6472
|
+
__name(sessionId, "sessionId");
|
|
6473
|
+
|
|
6402
6474
|
// src/server/utils/owner-fragment.ts
|
|
6403
6475
|
var UNKNOWN_OWNER = "userId=<unknown: no live session remains in this family to name it>";
|
|
6404
6476
|
function ownerFragment(ownerId) {
|
|
@@ -6406,6 +6478,14 @@ function ownerFragment(ownerId) {
|
|
|
6406
6478
|
}
|
|
6407
6479
|
__name(ownerFragment, "ownerFragment");
|
|
6408
6480
|
|
|
6481
|
+
// src/server/utils/serialize-session.ts
|
|
6482
|
+
function serializeSession(session) {
|
|
6483
|
+
if (session.familyId !== "") return JSON.stringify(session);
|
|
6484
|
+
const { familyId: _omitted, familyCreatedAt: _alsoOmitted, ...rest } = session;
|
|
6485
|
+
return JSON.stringify(rest);
|
|
6486
|
+
}
|
|
6487
|
+
__name(serializeSession, "serializeSession");
|
|
6488
|
+
|
|
6409
6489
|
// src/server/services/token-manager.service.ts
|
|
6410
6490
|
function _ts_decorate19(decorators, target, key, desc) {
|
|
6411
6491
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
@@ -6434,7 +6514,6 @@ function _ts_param13(paramIndex, decorator) {
|
|
|
6434
6514
|
}
|
|
6435
6515
|
__name(_ts_param13, "_ts_param");
|
|
6436
6516
|
var MFA_TEMP_TOKEN_TTL_SECONDS = 300;
|
|
6437
|
-
var MAX_SESSION_FIELD_LENGTH = 45;
|
|
6438
6517
|
var CREDENTIAL_PROVED = {
|
|
6439
6518
|
credentialProved: true
|
|
6440
6519
|
};
|
|
@@ -6541,7 +6620,9 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6541
6620
|
*/
|
|
6542
6621
|
async issueTokens(user, ip, userAgent, overrides) {
|
|
6543
6622
|
const epoch = await this.redis.getUserTokenEpoch(user.id, user.tenantId, "dashboard");
|
|
6623
|
+
const familyId = crypto.randomUUID();
|
|
6544
6624
|
const accessToken = this.issueAccess({
|
|
6625
|
+
sid: familyId,
|
|
6545
6626
|
sub: user.id,
|
|
6546
6627
|
tenantId: user.tenantId,
|
|
6547
6628
|
role: user.role,
|
|
@@ -6553,27 +6634,53 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6553
6634
|
});
|
|
6554
6635
|
const rawRefreshToken = generateSecureToken();
|
|
6555
6636
|
const tokenHash = sha256(rawRefreshToken);
|
|
6556
|
-
const familyId = crypto.randomUUID();
|
|
6557
6637
|
const session = this.buildSession(user.id, user.tenantId, user.role, ip, userAgent, user.mfaEnabled, familyId, void 0, overrides?.mfaVerified === true);
|
|
6558
6638
|
const ttl = this.options.jwt.refreshExpiresInDays * 86400;
|
|
6639
|
+
const proved = overrides?.credentialProved === true;
|
|
6559
6640
|
await this.redis.writeNewSession({
|
|
6560
6641
|
kind: "dashboard",
|
|
6561
6642
|
tenantId: user.tenantId,
|
|
6562
6643
|
tokenHash,
|
|
6563
|
-
sessionJson:
|
|
6644
|
+
sessionJson: serializeSession(session),
|
|
6564
6645
|
familyId,
|
|
6565
6646
|
userId: user.id,
|
|
6566
|
-
refreshTtl: ttl
|
|
6647
|
+
refreshTtl: ttl,
|
|
6648
|
+
extras: this.dashboardExtras(user, familyId, tokenHash, ttl, ip, userAgent, proved)
|
|
6567
6649
|
});
|
|
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
|
-
}
|
|
6571
6650
|
return {
|
|
6572
6651
|
user,
|
|
6573
6652
|
accessToken,
|
|
6574
6653
|
rawRefreshToken
|
|
6575
6654
|
};
|
|
6576
6655
|
}
|
|
6656
|
+
/**
|
|
6657
|
+
* The records a new dashboard session is written with, beyond the session itself.
|
|
6658
|
+
*
|
|
6659
|
+
* The recent-authentication mark is there only when a credential was presented in this request;
|
|
6660
|
+
* a refresh proves possession of a token, so `reissueTokens` never writes it. It keeps its own
|
|
6661
|
+
* short lifetime and its key names the session, so another session of the same account cannot
|
|
6662
|
+
* use it. The session detail is there only when sessions are enabled.
|
|
6663
|
+
*/
|
|
6664
|
+
dashboardExtras(user, sid, tokenHash, ttl, ip, userAgent, proved) {
|
|
6665
|
+
const markKey = recentAuthKey("dashboard", user.id, this.options.hmacKey, user.tenantId, sid);
|
|
6666
|
+
const detail = dashboardSessionDetail(ip, userAgent, Date.now());
|
|
6667
|
+
return [
|
|
6668
|
+
...proved ? [
|
|
6669
|
+
{
|
|
6670
|
+
key: markKey,
|
|
6671
|
+
value: "1",
|
|
6672
|
+
ttl: RECENT_AUTH_TTL_SECONDS
|
|
6673
|
+
}
|
|
6674
|
+
] : [],
|
|
6675
|
+
...this.options.sessions.enabled ? [
|
|
6676
|
+
{
|
|
6677
|
+
key: `sd:${tokenHash}`,
|
|
6678
|
+
value: detail,
|
|
6679
|
+
ttl
|
|
6680
|
+
}
|
|
6681
|
+
] : []
|
|
6682
|
+
];
|
|
6683
|
+
}
|
|
6577
6684
|
// ---------------------------------------------------------------------------
|
|
6578
6685
|
// Platform tokens
|
|
6579
6686
|
// ---------------------------------------------------------------------------
|
|
@@ -6591,7 +6698,9 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6591
6698
|
*/
|
|
6592
6699
|
async issuePlatformTokens(admin, ip, userAgent, overrides) {
|
|
6593
6700
|
const epoch = await this.redis.getUserTokenEpoch(admin.id, void 0, "platform");
|
|
6701
|
+
const familyId = crypto.randomUUID();
|
|
6594
6702
|
const accessToken = this.issuePlatformAccess({
|
|
6703
|
+
sid: familyId,
|
|
6595
6704
|
sub: admin.id,
|
|
6596
6705
|
role: admin.role,
|
|
6597
6706
|
type: "platform",
|
|
@@ -6601,19 +6710,20 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6601
6710
|
});
|
|
6602
6711
|
const rawRefreshToken = generateSecureToken();
|
|
6603
6712
|
const tokenHash = sha256(rawRefreshToken);
|
|
6604
|
-
const familyId = crypto.randomUUID();
|
|
6605
6713
|
const session = this.buildSession(admin.id, "", admin.role, ip, userAgent, admin.mfaEnabled, familyId, void 0, overrides?.mfaVerified === true);
|
|
6606
6714
|
const ttl = this.options.jwt.refreshExpiresInDays * 86400;
|
|
6607
6715
|
await this.redis.writeNewSession({
|
|
6608
6716
|
kind: "platform",
|
|
6609
6717
|
tenantId: void 0,
|
|
6610
6718
|
tokenHash,
|
|
6611
|
-
sessionJson:
|
|
6719
|
+
sessionJson: serializeSession(session),
|
|
6612
6720
|
familyId,
|
|
6613
6721
|
userId: admin.id,
|
|
6614
|
-
refreshTtl: ttl
|
|
6722
|
+
refreshTtl: ttl,
|
|
6723
|
+
extras: [
|
|
6724
|
+
platformSessionExtra(tokenHash, ip, userAgent, ttl)
|
|
6725
|
+
]
|
|
6615
6726
|
});
|
|
6616
|
-
await this.writePlatformSessionDetail(tokenHash, ip, userAgent, ttl);
|
|
6617
6727
|
return {
|
|
6618
6728
|
admin,
|
|
6619
6729
|
accessToken,
|
|
@@ -6623,9 +6733,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6623
6733
|
/**
|
|
6624
6734
|
* Writes the per-session detail record for a platform session under `psd:{hash}`.
|
|
6625
6735
|
*
|
|
6626
|
-
* The
|
|
6627
|
-
* but platform sessions never had one, so a session listing had nothing to describe them
|
|
6628
|
-
* with. The field set and the Unix-millisecond timestamps mirror the dashboard record
|
|
6736
|
+
* The field set and the Unix-millisecond timestamps mirror the dashboard record
|
|
6629
6737
|
* exactly, so either backend sharing this Redis reads the same shape.
|
|
6630
6738
|
*
|
|
6631
6739
|
* @param tokenHash - SHA-256 of the raw refresh token; the key's `{hash}` segment.
|
|
@@ -6634,14 +6742,8 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6634
6742
|
* @param ttl - Record lifetime in seconds; matches the refresh session it describes.
|
|
6635
6743
|
*/
|
|
6636
6744
|
async writePlatformSessionDetail(tokenHash, ip, userAgent, ttl) {
|
|
6637
|
-
const
|
|
6638
|
-
|
|
6639
|
-
device: userAgent.slice(0, MAX_SESSION_FIELD_LENGTH),
|
|
6640
|
-
ip: ip.slice(0, MAX_SESSION_FIELD_LENGTH),
|
|
6641
|
-
createdAt: now3,
|
|
6642
|
-
lastActivityAt: now3
|
|
6643
|
-
};
|
|
6644
|
-
await this.redis.set(`psd:${tokenHash}`, JSON.stringify(detail), ttl);
|
|
6745
|
+
const extra = platformSessionExtra(tokenHash, ip, userAgent, ttl);
|
|
6746
|
+
await this.redis.set(extra.key, extra.value, extra.ttl);
|
|
6645
6747
|
}
|
|
6646
6748
|
// ---------------------------------------------------------------------------
|
|
6647
6749
|
// Token rotation
|
|
@@ -6692,7 +6794,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6692
6794
|
tenantId: seed.tenantId,
|
|
6693
6795
|
oldHash,
|
|
6694
6796
|
newHash,
|
|
6695
|
-
newSessionJson:
|
|
6797
|
+
newSessionJson: serializeSession(newSession),
|
|
6696
6798
|
familyId: seed.familyId,
|
|
6697
6799
|
userId: seed.userId,
|
|
6698
6800
|
refreshTtl,
|
|
@@ -6870,7 +6972,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6870
6972
|
kind: "dashboard",
|
|
6871
6973
|
tenantId: graceSession.tenantId,
|
|
6872
6974
|
newHash: anotherNewHash,
|
|
6873
|
-
newSessionJson:
|
|
6975
|
+
newSessionJson: serializeSession(anotherSession),
|
|
6874
6976
|
familyId: graceSession.familyId,
|
|
6875
6977
|
userId: graceSession.userId,
|
|
6876
6978
|
refreshTtl
|
|
@@ -6932,21 +7034,6 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
6932
7034
|
};
|
|
6933
7035
|
}
|
|
6934
7036
|
/**
|
|
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
7037
|
* Constructs a session record from identity fields and request metadata.
|
|
6951
7038
|
*
|
|
6952
7039
|
* @param userId - Internal user ID for whom the session is being created.
|
|
@@ -7007,6 +7094,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7007
7094
|
async buildRotatedResult(session, rawRefreshToken) {
|
|
7008
7095
|
const epoch = await this.redis.getUserTokenEpoch(session.userId, session.tenantId, "dashboard");
|
|
7009
7096
|
const accessToken = this.issueAccess({
|
|
7097
|
+
sid: sessionId(session.familyId),
|
|
7010
7098
|
sub: session.userId,
|
|
7011
7099
|
tenantId: session.tenantId,
|
|
7012
7100
|
role: session.role,
|
|
@@ -7054,7 +7142,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7054
7142
|
tenantId: void 0,
|
|
7055
7143
|
oldHash,
|
|
7056
7144
|
newHash,
|
|
7057
|
-
newSessionJson:
|
|
7145
|
+
newSessionJson: serializeSession(newSession),
|
|
7058
7146
|
familyId: seed.familyId,
|
|
7059
7147
|
userId: seed.userId,
|
|
7060
7148
|
refreshTtl,
|
|
@@ -7107,7 +7195,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7107
7195
|
kind: "platform",
|
|
7108
7196
|
tenantId: void 0,
|
|
7109
7197
|
newHash: anotherNewHash,
|
|
7110
|
-
newSessionJson:
|
|
7198
|
+
newSessionJson: serializeSession(anotherSession),
|
|
7111
7199
|
familyId: graceSession.familyId,
|
|
7112
7200
|
userId: graceSession.userId,
|
|
7113
7201
|
refreshTtl
|
|
@@ -7129,6 +7217,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7129
7217
|
async buildPlatformRotatedResult(session, rawRefreshToken) {
|
|
7130
7218
|
const epoch = await this.redis.getUserTokenEpoch(session.userId, void 0, "platform");
|
|
7131
7219
|
const accessToken = this.issuePlatformAccess({
|
|
7220
|
+
sid: sessionId(session.familyId),
|
|
7132
7221
|
sub: session.userId,
|
|
7133
7222
|
role: session.role,
|
|
7134
7223
|
type: "platform",
|
|
@@ -7169,6 +7258,7 @@ var TokenManagerService = class _TokenManagerService {
|
|
|
7169
7258
|
*/
|
|
7170
7259
|
async reissuePlatformAccessWithAuthority(claims, role, mfaEnabled) {
|
|
7171
7260
|
return this.issuePlatformAccess({
|
|
7261
|
+
sid: claims.sid,
|
|
7172
7262
|
sub: claims.sub,
|
|
7173
7263
|
role,
|
|
7174
7264
|
type: "platform",
|
|
@@ -8029,6 +8119,7 @@ exports.AuthService = class _AuthService {
|
|
|
8029
8119
|
let authoritative = result.accessToken;
|
|
8030
8120
|
if (rotated.role !== user.role || rotated.tenantId !== user.tenantId || rotated.mfaEnabled !== user.mfaEnabled) {
|
|
8031
8121
|
authoritative = this.tokenManager.issueAccess({
|
|
8122
|
+
sid: rotated.sid,
|
|
8032
8123
|
sub: user.id,
|
|
8033
8124
|
tenantId: user.tenantId,
|
|
8034
8125
|
role: user.role,
|
|
@@ -8036,8 +8127,7 @@ exports.AuthService = class _AuthService {
|
|
|
8036
8127
|
status: user.status,
|
|
8037
8128
|
mfaEnabled: user.mfaEnabled,
|
|
8038
8129
|
// 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.
|
|
8130
|
+
// cleared on this session stays cleared, and re-stamping must not demand it again.
|
|
8041
8131
|
mfaVerified: rotated.mfaVerified,
|
|
8042
8132
|
epoch: await this.redis.getUserTokenEpoch(user.id, user.tenantId, "dashboard")
|
|
8043
8133
|
});
|
|
@@ -11006,11 +11096,29 @@ var MfaRecordService = class {
|
|
|
11006
11096
|
}
|
|
11007
11097
|
return true;
|
|
11008
11098
|
} finally {
|
|
11099
|
+
await this.releaseLock(lockKey, lockToken);
|
|
11100
|
+
}
|
|
11101
|
+
}
|
|
11102
|
+
/**
|
|
11103
|
+
* Releases the transition lock this call took, whatever the store answers.
|
|
11104
|
+
*
|
|
11105
|
+
* A compare-and-delete: only a lock still holding this call's nonce is removed. A release that
|
|
11106
|
+
* fails is logged and swallowed, because the transition has already been decided by then: an
|
|
11107
|
+
* error here would replace the transition's own result, answering failure for a write that
|
|
11108
|
+
* committed or hiding the error that did occur. The lock's short TTL ends it on its own.
|
|
11109
|
+
*
|
|
11110
|
+
* @param lockKey - The account's lock key.
|
|
11111
|
+
* @param lockToken - The nonce this call wrote when it took the lock.
|
|
11112
|
+
*/
|
|
11113
|
+
async releaseLock(lockKey, lockToken) {
|
|
11114
|
+
try {
|
|
11009
11115
|
await this.redis.eval(RELEASE_LOCK_LUA, [
|
|
11010
11116
|
lockKey
|
|
11011
11117
|
], [
|
|
11012
11118
|
lockToken
|
|
11013
11119
|
]);
|
|
11120
|
+
} catch (err) {
|
|
11121
|
+
this.logger.warn(`MFA transition lock release failed, the lock expires on its own: ${describeChannelStatus(err)}`);
|
|
11014
11122
|
}
|
|
11015
11123
|
}
|
|
11016
11124
|
/**
|
|
@@ -11951,14 +12059,16 @@ exports.ReauthenticationService = class ReauthenticationService {
|
|
|
11951
12059
|
* @param userId - The account being changed, part of the counter's key.
|
|
11952
12060
|
* @param passwordHash - The account's stored hash, or `null` for an OAuth-only account.
|
|
11953
12061
|
* @param password - The password the caller submitted, if any.
|
|
12062
|
+
* @param tenantId - The caller's tenant on the dashboard plane, part of the key.
|
|
12063
|
+
* @param sid - The caller's session, from its token; only an account with no password reads it.
|
|
11954
12064
|
* @throws {@link AuthException} `ACCOUNT_LOCKED` once the failure budget for this flow is
|
|
11955
12065
|
* spent, or `INVALID_CREDENTIALS` when the account has a password and the submitted one is
|
|
11956
12066
|
* absent or wrong. The latter is deliberately the same code a failed login returns: an
|
|
11957
12067
|
* attacker holding a stolen token learns nothing from it that a failed login does not tell them.
|
|
11958
12068
|
*/
|
|
11959
|
-
async assertReauthenticated(context, userId, passwordHash, password, tenantId) {
|
|
12069
|
+
async assertReauthenticated(context, userId, passwordHash, password, tenantId, sid) {
|
|
11960
12070
|
if (passwordHash === null) {
|
|
11961
|
-
const recent = await this.
|
|
12071
|
+
const recent = await this.recentMark(context, userId, tenantId, sid);
|
|
11962
12072
|
if (recent === null) {
|
|
11963
12073
|
this.logger.warn(`reauthenticate: no recent authentication userId=${logSafe(userId)} context=${context}`);
|
|
11964
12074
|
throw new AuthException(AUTH_ERROR_CODES.REAUTHENTICATION_REQUIRED);
|
|
@@ -11977,6 +12087,18 @@ exports.ReauthenticationService = class ReauthenticationService {
|
|
|
11977
12087
|
}
|
|
11978
12088
|
await this.lockout.reset("reauth", context, userId, tenantId);
|
|
11979
12089
|
}
|
|
12090
|
+
/**
|
|
12091
|
+
* The recent-authentication mark of the caller's own session, or `null` when there is none.
|
|
12092
|
+
*
|
|
12093
|
+
* The proof is the caller's own: the mark is keyed by the session that earned it, so another
|
|
12094
|
+
* session of the same account cannot borrow it. A token with no usable `sid`, one minted before
|
|
12095
|
+
* the claim existed, names no session and earns nothing, so no key is read for it.
|
|
12096
|
+
*/
|
|
12097
|
+
async recentMark(context, userId, tenantId, sid) {
|
|
12098
|
+
const bound = sessionId(sid);
|
|
12099
|
+
if (bound === void 0) return null;
|
|
12100
|
+
return await this.redis.get(recentAuthKey(context, userId, this.options.hmacKey, tenantId, bound));
|
|
12101
|
+
}
|
|
11980
12102
|
};
|
|
11981
12103
|
exports.ReauthenticationService = _ts_decorate41([
|
|
11982
12104
|
common.Injectable(),
|
|
@@ -12123,6 +12245,7 @@ exports.MfaService = class _MfaService {
|
|
|
12123
12245
|
*
|
|
12124
12246
|
* @param userId - Internal ID of the user enabling MFA.
|
|
12125
12247
|
* @param context - Which repository to use: `'dashboard'` (default) or `'platform'`.
|
|
12248
|
+
* @param sid - The caller's session (its token's `sid`), for an account with no local password.
|
|
12126
12249
|
* @returns Setup result containing the secret, QR URI, and plain recovery codes.
|
|
12127
12250
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
12128
12251
|
* @throws `MFA_NOT_ENABLED` if `context === 'platform'` and the platform user
|
|
@@ -12130,14 +12253,14 @@ exports.MfaService = class _MfaService {
|
|
|
12130
12253
|
* reused (rather than introducing a new one) because the consumer-facing
|
|
12131
12254
|
* meaning is the same: "platform MFA cannot operate".
|
|
12132
12255
|
*/
|
|
12133
|
-
async setup(userId, context = "dashboard", password, tenantId) {
|
|
12256
|
+
async setup(userId, context = "dashboard", password, tenantId, sid) {
|
|
12134
12257
|
assertPlaneTenant(context, tenantId);
|
|
12135
12258
|
if (context === "platform" && !this.platformUserRepo) {
|
|
12136
12259
|
throw new AuthException(AUTH_ERROR_CODES.MFA_NOT_ENABLED);
|
|
12137
12260
|
}
|
|
12138
12261
|
const user = await this.records.loadAccount(context, userId, tenantId);
|
|
12139
12262
|
if (user.mfaEnabled) throw new AuthException(AUTH_ERROR_CODES.MFA_ALREADY_ENABLED);
|
|
12140
|
-
await this.reauthentication.assertReauthenticated(context, userId, user.passwordHash, password, tenantId);
|
|
12263
|
+
await this.reauthentication.assertReauthenticated(context, userId, user.passwordHash, password, tenantId, sid);
|
|
12141
12264
|
const setupKey = `mfa_setup:${hmacSha256(userSubject(context, userId, tenantId), this.options.hmacKey)}`;
|
|
12142
12265
|
const existingFast = await this.redis.get(setupKey);
|
|
12143
12266
|
if (existingFast !== null) {
|
|
@@ -12493,7 +12616,7 @@ var MfaController = class {
|
|
|
12493
12616
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
12494
12617
|
*/
|
|
12495
12618
|
async setup(user, dto) {
|
|
12496
|
-
return this.mfaService.setup(user.sub, "dashboard", dto.password, user.tenantId);
|
|
12619
|
+
return this.mfaService.setup(user.sub, "dashboard", dto.password, user.tenantId, user.sid);
|
|
12497
12620
|
}
|
|
12498
12621
|
/**
|
|
12499
12622
|
* Verifies the first TOTP code and permanently enables MFA on the account.
|
|
@@ -14821,7 +14944,7 @@ var PlatformMfaController = class {
|
|
|
14821
14944
|
* @throws `MFA_ALREADY_ENABLED` if MFA is already active on the account.
|
|
14822
14945
|
*/
|
|
14823
14946
|
async setup(user, dto) {
|
|
14824
|
-
return this.mfaService.setup(user.sub, "platform", dto.password);
|
|
14947
|
+
return this.mfaService.setup(user.sub, "platform", dto.password, void 0, user.sid);
|
|
14825
14948
|
}
|
|
14826
14949
|
/**
|
|
14827
14950
|
* Verifies the first TOTP code and permanently enables MFA on the platform
|
|
@@ -18494,7 +18617,7 @@ exports.WorkspaceCallerService = class WorkspaceCallerService {
|
|
|
18494
18617
|
*/
|
|
18495
18618
|
async reauthenticate(caller, password) {
|
|
18496
18619
|
const user = await this.find(caller);
|
|
18497
|
-
await this.reauthentication.assertReauthenticated("dashboard", user.id, user.passwordHash, password, caller.tenantId);
|
|
18620
|
+
await this.reauthentication.assertReauthenticated("dashboard", user.id, user.passwordHash, password, caller.tenantId, caller.sid);
|
|
18498
18621
|
}
|
|
18499
18622
|
async find(caller) {
|
|
18500
18623
|
const user = await this.users.findById({
|
|
@@ -25629,6 +25752,7 @@ exports.encrypt = encrypt;
|
|
|
25629
25752
|
exports.generateSecureToken = generateSecureToken;
|
|
25630
25753
|
exports.hasRole = hasRole;
|
|
25631
25754
|
exports.hmacSha256 = hmacSha256;
|
|
25755
|
+
exports.isServiceUnavailable = isServiceUnavailable;
|
|
25632
25756
|
exports.isWorkspaceOwnerRole = isWorkspaceOwnerRole;
|
|
25633
25757
|
exports.readWorkspacePosition = readWorkspacePosition;
|
|
25634
25758
|
exports.redactSecrets = redactSecrets;
|