@bymax-one/nest-auth 1.5.7 → 1.5.9
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 +115 -1
- package/README.md +9 -1
- package/dist/react/index.cjs +12 -10
- package/dist/react/index.d.cts +5 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.mjs +12 -10
- package/dist/server/index.cjs +455 -171
- package/dist/server/index.d.cts +4 -4
- package/dist/server/index.d.ts +4 -4
- package/dist/server/index.mjs +455 -171
- package/package.json +1 -1
package/dist/server/index.cjs
CHANGED
|
@@ -320,6 +320,16 @@ function validateApiKeyTuning(group) {
|
|
|
320
320
|
}
|
|
321
321
|
__name(validateApiKeyTuning, "validateApiKeyTuning");
|
|
322
322
|
|
|
323
|
+
// src/server/config/describe-received.ts
|
|
324
|
+
function describeReceived(value) {
|
|
325
|
+
if (typeof value === "number") return String(value);
|
|
326
|
+
if (value === null || value === void 0) return String(value);
|
|
327
|
+
if (Array.isArray(value)) return "an array";
|
|
328
|
+
const kind = typeof value;
|
|
329
|
+
return `${kind === "object" ? "an" : "a"} ${kind}`;
|
|
330
|
+
}
|
|
331
|
+
__name(describeReceived, "describeReceived");
|
|
332
|
+
|
|
323
333
|
// src/server/config/duration.ts
|
|
324
334
|
var DURATION_UNIT_SECONDS = {
|
|
325
335
|
ms: 1e-3,
|
|
@@ -364,6 +374,426 @@ function durationToSeconds(value) {
|
|
|
364
374
|
return amount * seconds;
|
|
365
375
|
}
|
|
366
376
|
__name(durationToSeconds, "durationToSeconds");
|
|
377
|
+
var TOTP_STEP_SECONDS = 30;
|
|
378
|
+
var TOTP_DIGITS = 6;
|
|
379
|
+
var BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
380
|
+
var MIN_SECRET_BYTES = 20;
|
|
381
|
+
function toBase32(input) {
|
|
382
|
+
let bits = 0;
|
|
383
|
+
let value = 0;
|
|
384
|
+
let output = "";
|
|
385
|
+
for (let i = 0; i < input.length; i++) {
|
|
386
|
+
value = value << 8 | input.readUInt8(i);
|
|
387
|
+
bits += 8;
|
|
388
|
+
while (bits >= 5) {
|
|
389
|
+
bits -= 5;
|
|
390
|
+
output += BASE32_ALPHABET[value >>> bits & 31];
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
if (bits > 0) {
|
|
394
|
+
output += BASE32_ALPHABET[value << 5 - bits & 31];
|
|
395
|
+
}
|
|
396
|
+
return output;
|
|
397
|
+
}
|
|
398
|
+
__name(toBase32, "toBase32");
|
|
399
|
+
function fromBase32(input) {
|
|
400
|
+
const normalized = input.toUpperCase().replace(/[\s=-]/g, "");
|
|
401
|
+
let bits = 0;
|
|
402
|
+
let value = 0;
|
|
403
|
+
const output = [];
|
|
404
|
+
for (const char of normalized) {
|
|
405
|
+
const idx = BASE32_ALPHABET.indexOf(char);
|
|
406
|
+
if (idx === -1) {
|
|
407
|
+
throw new Error(`[totp] Invalid Base32 character: ${JSON.stringify(char)}`);
|
|
408
|
+
}
|
|
409
|
+
value = value << 5 | idx;
|
|
410
|
+
bits += 5;
|
|
411
|
+
if (bits >= 8) {
|
|
412
|
+
bits -= 8;
|
|
413
|
+
output.push(value >>> bits & 255);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return Buffer.from(output);
|
|
417
|
+
}
|
|
418
|
+
__name(fromBase32, "fromBase32");
|
|
419
|
+
function generateTotpSecret() {
|
|
420
|
+
const raw = crypto.randomBytes(MIN_SECRET_BYTES);
|
|
421
|
+
return {
|
|
422
|
+
raw,
|
|
423
|
+
base32: toBase32(raw)
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
__name(generateTotpSecret, "generateTotpSecret");
|
|
427
|
+
function buildTotpUri(secretBase32, accountEmail, issuer) {
|
|
428
|
+
if (!secretBase32 || secretBase32.length < 32) {
|
|
429
|
+
throw new Error("[totp] secretBase32 must encode at least 20 bytes (minimum 32 base32 characters)");
|
|
430
|
+
}
|
|
431
|
+
if (!issuer) {
|
|
432
|
+
throw new Error("[totp] issuer must not be empty");
|
|
433
|
+
}
|
|
434
|
+
if (!accountEmail) {
|
|
435
|
+
throw new Error("[totp] accountEmail must not be empty");
|
|
436
|
+
}
|
|
437
|
+
const label = `${encodeURIComponent(issuer)}:${encodeURIComponent(accountEmail)}`;
|
|
438
|
+
const qs = [
|
|
439
|
+
`secret=${encodeURIComponent(secretBase32)}`,
|
|
440
|
+
`issuer=${encodeURIComponent(issuer)}`,
|
|
441
|
+
`algorithm=SHA1`,
|
|
442
|
+
`digits=${String(TOTP_DIGITS)}`,
|
|
443
|
+
`period=${String(TOTP_STEP_SECONDS)}`
|
|
444
|
+
].join("&");
|
|
445
|
+
return `otpauth://totp/${label}?${qs}`;
|
|
446
|
+
}
|
|
447
|
+
__name(buildTotpUri, "buildTotpUri");
|
|
448
|
+
function generateHotp(secretBase32, counter) {
|
|
449
|
+
const key = fromBase32(secretBase32);
|
|
450
|
+
if (key.length < MIN_SECRET_BYTES) {
|
|
451
|
+
throw new Error(`[totp] Secret too short: ${key.length} bytes (minimum ${MIN_SECRET_BYTES}). Generate secrets with generateTotpSecret().`);
|
|
452
|
+
}
|
|
453
|
+
if (!Number.isInteger(counter) || counter < 0 || counter > Number.MAX_SAFE_INTEGER) {
|
|
454
|
+
throw new Error(`[totp] Counter must be a non-negative safe integer (got ${counter}).`);
|
|
455
|
+
}
|
|
456
|
+
const counterBuffer = Buffer.allocUnsafe(8);
|
|
457
|
+
const hi = Math.floor(counter / 4294967296);
|
|
458
|
+
const lo = counter >>> 0;
|
|
459
|
+
counterBuffer.writeUInt32BE(hi, 0);
|
|
460
|
+
counterBuffer.writeUInt32BE(lo, 4);
|
|
461
|
+
const hmac = crypto.createHmac("sha1", key).update(counterBuffer).digest();
|
|
462
|
+
const offset = hmac.readUInt8(hmac.length - 1) & 15;
|
|
463
|
+
const truncated = (hmac.readUInt8(offset) & 127) << 24 | (hmac.readUInt8(offset + 1) & 255) << 16 | (hmac.readUInt8(offset + 2) & 255) << 8 | hmac.readUInt8(offset + 3);
|
|
464
|
+
const mod = 10 ** TOTP_DIGITS;
|
|
465
|
+
return String(truncated % mod).padStart(TOTP_DIGITS, "0");
|
|
466
|
+
}
|
|
467
|
+
__name(generateHotp, "generateHotp");
|
|
468
|
+
var MAX_VERIFY_WINDOW = 2;
|
|
469
|
+
function verifyTotp(secretBase32, code, window = 1) {
|
|
470
|
+
if (!/^\d{6}$/.test(code)) return false;
|
|
471
|
+
const currentStep = Math.floor(Date.now() / 1e3 / TOTP_STEP_SECONDS);
|
|
472
|
+
const effective = Math.min(Math.max(window, 0), MAX_VERIFY_WINDOW);
|
|
473
|
+
for (let delta = -effective; delta <= effective; delta++) {
|
|
474
|
+
const expected = generateHotp(secretBase32, currentStep + delta);
|
|
475
|
+
const expectedBuf = Buffer.from(expected, "utf8");
|
|
476
|
+
const codeBuf = Buffer.from(code, "utf8");
|
|
477
|
+
if (expectedBuf.length === codeBuf.length && crypto.timingSafeEqual(expectedBuf, codeBuf)) {
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
__name(verifyTotp, "verifyTotp");
|
|
484
|
+
|
|
485
|
+
// src/server/config/numeric-options.ts
|
|
486
|
+
var MAX_USER_STATUS_CACHE_TTL_SECONDS = 3600;
|
|
487
|
+
var MAX_REDIS_TTL_SECONDS = 31536e4;
|
|
488
|
+
var MAX_COOKIE_MAX_AGE_MS = MAX_REDIS_TTL_SECONDS * 1e3;
|
|
489
|
+
var MAX_SESSIONS_PER_USER = 1e3;
|
|
490
|
+
var MAX_LIFETIME_DAYS = 3650;
|
|
491
|
+
var MIN_ABSOLUTE_LIFETIME_DAYS = 1 / 24;
|
|
492
|
+
var MAX_SCRYPT_PARALLELIZATION = 16;
|
|
493
|
+
var MAX_BRUTE_FORCE_WINDOW_SECONDS = 604800;
|
|
494
|
+
function assertWholeNumber(path, value, min, max, why) {
|
|
495
|
+
if (value === void 0) return;
|
|
496
|
+
if (Number.isSafeInteger(value) && value >= min && value <= max) return;
|
|
497
|
+
throw new Error(`[BymaxAuthModule] ${path} must be a whole number between ${min} and ${max} inclusive (received ${describeReceived(value)}). ${why}`);
|
|
498
|
+
}
|
|
499
|
+
__name(assertWholeNumber, "assertWholeNumber");
|
|
500
|
+
function validateWholeNumberOptions(userOptions) {
|
|
501
|
+
assertWholeNumber("jwt.accessCookieMaxAgeMs", userOptions.jwt.accessCookieMaxAgeMs, 1e3, MAX_COOKIE_MAX_AGE_MS, "It is the cookie Max-Age in milliseconds, sent in whole seconds: under 1000 it becomes Max-Age=0, which deletes the cookie, and NaN is ignored by the browser, so the session would end with the tab.");
|
|
502
|
+
assertWholeNumber("sessions.defaultMaxSessions", userOptions.sessions?.defaultMaxSessions, 1, MAX_SESSIONS_PER_USER, "It caps concurrent sessions per user, counted in whole sessions: NaN evicts no session, zero or a negative value evicts them all, a fraction cannot be counted, and a value above 1000 is no cap.");
|
|
503
|
+
assertWholeNumber("bruteForce.windowSeconds", userOptions.bruteForce?.windowSeconds, 1, MAX_BRUTE_FORCE_WINDOW_SECONDS, "It is the Redis expiry of each failure counter: Redis deletes a key on `EXPIRE key 0`, so a zero or negative window destroys each counter as it is created and the lockout never engages, silently; a window past a week locks an account for a month over a few typos.");
|
|
504
|
+
assertWholeNumber("userStatusCacheTtlSeconds", userOptions.userStatusCacheTtlSeconds, 1, MAX_USER_STATUS_CACHE_TTL_SECONDS, "It is how long a suspended account keeps passing the dashboard guards, so it has a ceiling.");
|
|
505
|
+
}
|
|
506
|
+
__name(validateWholeNumberOptions, "validateWholeNumberOptions");
|
|
507
|
+
function validateRedisTtls(userOptions) {
|
|
508
|
+
const ttls = [
|
|
509
|
+
[
|
|
510
|
+
"passwordReset.tokenTtlSeconds",
|
|
511
|
+
userOptions.passwordReset?.tokenTtlSeconds
|
|
512
|
+
],
|
|
513
|
+
[
|
|
514
|
+
"passwordReset.otpTtlSeconds",
|
|
515
|
+
userOptions.passwordReset?.otpTtlSeconds
|
|
516
|
+
],
|
|
517
|
+
[
|
|
518
|
+
"emailVerification.otpTtlSeconds",
|
|
519
|
+
userOptions.emailVerification?.otpTtlSeconds
|
|
520
|
+
],
|
|
521
|
+
[
|
|
522
|
+
"emailChange.tokenTtlSeconds",
|
|
523
|
+
userOptions.emailChange?.tokenTtlSeconds
|
|
524
|
+
],
|
|
525
|
+
[
|
|
526
|
+
"invitations.tokenTtlSeconds",
|
|
527
|
+
userOptions.invitations?.tokenTtlSeconds
|
|
528
|
+
]
|
|
529
|
+
];
|
|
530
|
+
for (const [path, value] of ttls) {
|
|
531
|
+
assertWholeNumber(path, value, 1, MAX_REDIS_TTL_SECONDS, "It is a Redis expiry in seconds: zero or a negative deletes the record as it is written, and NaN, a fraction, Infinity or a value past what Redis can hold is refused by Redis on every request.");
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
__name(validateRedisTtls, "validateRedisTtls");
|
|
535
|
+
function validateAbsoluteSessionLifetime(jwt) {
|
|
536
|
+
const days = jwt.absoluteSessionLifetimeDays;
|
|
537
|
+
if (days === void 0 || days === 0 || Number.isFinite(days) && days >= MIN_ABSOLUTE_LIFETIME_DAYS && days <= MAX_LIFETIME_DAYS) {
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
throw new Error(`[BymaxAuthModule] jwt.absoluteSessionLifetimeDays must be 0 or between ${MIN_ABSOLUTE_LIFETIME_DAYS.toFixed(4)} and ${MAX_LIFETIME_DAYS} days (received ${describeReceived(days)}). Only 0 switches the cap off; NaN, a negative or a value so large that its length in milliseconds overflows would skip it without being asked to, and a value under an hour ends every session at its first rotation after that age, minutes after login. Set 0 to accept unbounded sessions deliberately.`);
|
|
541
|
+
}
|
|
542
|
+
__name(validateAbsoluteSessionLifetime, "validateAbsoluteSessionLifetime");
|
|
543
|
+
function validatePasswordResetOtpLength(passwordReset) {
|
|
544
|
+
const otpLength = passwordReset?.otpLength;
|
|
545
|
+
if (otpLength === void 0) return;
|
|
546
|
+
if (!Number.isInteger(otpLength) || otpLength < 4 || otpLength > 8) {
|
|
547
|
+
throw new Error(`[BymaxAuthModule] passwordReset.otpLength must be between 4 and 8 inclusive (received ${describeReceived(otpLength)}). Values below 4 are too easily guessable; values above 8 are not required for security and degrade user experience. The value must also be a whole number: a comparison with NaN is false both ways, so a bound written as a range alone lets it through.`);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
__name(validatePasswordResetOtpLength, "validatePasswordResetOtpLength");
|
|
551
|
+
function validateMfaVerificationParameters(mfa) {
|
|
552
|
+
const totpWindow = mfa?.totpWindow;
|
|
553
|
+
if (totpWindow !== void 0 && (!Number.isInteger(totpWindow) || totpWindow < 0 || totpWindow > MAX_VERIFY_WINDOW)) {
|
|
554
|
+
throw new Error(`[BymaxAuthModule] mfa.totpWindow must be between 0 and ${MAX_VERIFY_WINDOW} inclusive (received ${describeReceived(totpWindow)}). The window is counted in 30-second steps on either side of the current time, so ${typeof totpWindow === "number" ? 2 * totpWindow + 1 : "2N + 1"} codes would be valid at once. RFC 6238 \xA75.2 recommends at most one step of tolerance; the default of 1 accepts three codes. The bound matches the drift window the verifier actually applies, so a configured value means what it says and is never silently clamped. It must be a whole number.`);
|
|
555
|
+
}
|
|
556
|
+
const recoveryCodeCount = mfa?.recoveryCodeCount;
|
|
557
|
+
if (recoveryCodeCount !== void 0 && (!Number.isInteger(recoveryCodeCount) || recoveryCodeCount < 1 || recoveryCodeCount > 50)) {
|
|
558
|
+
throw new Error(`[BymaxAuthModule] mfa.recoveryCodeCount must be between 1 and 50 inclusive (received ${describeReceived(recoveryCodeCount)}). Zero enrols an account with no way back if the authenticator is lost. The count must be a whole number.`);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
__name(validateMfaVerificationParameters, "validateMfaVerificationParameters");
|
|
562
|
+
var MAX_REFRESH_GRACE_WINDOW_SECONDS = 300;
|
|
563
|
+
function validateRefreshGraceWindow(jwt) {
|
|
564
|
+
const graceSeconds = jwt.refreshGraceWindowSeconds ?? DEFAULT_OPTIONS.jwt.refreshGraceWindowSeconds;
|
|
565
|
+
const refreshExpiresInDays = jwt.refreshExpiresInDays ?? DEFAULT_OPTIONS.jwt.refreshExpiresInDays;
|
|
566
|
+
const refreshLifetimeSeconds = typeof refreshExpiresInDays === "number" ? refreshExpiresInDays * 86400 : Number.NaN;
|
|
567
|
+
const isGraceNumber = typeof graceSeconds === "number";
|
|
568
|
+
if (!Number.isFinite(refreshExpiresInDays) || refreshExpiresInDays <= 0 || refreshExpiresInDays > MAX_LIFETIME_DAYS || !Number.isInteger(refreshLifetimeSeconds)) {
|
|
569
|
+
throw new Error(`[BymaxAuthModule] jwt.refreshExpiresInDays must be a positive finite number. It received ${describeReceived(refreshExpiresInDays)}. Zero, negative, NaN, and Infinity are all rejected \u2014 any of these would produce an invalid Redis TTL and cause all token rotations to fail at runtime. It is at most ${MAX_LIFETIME_DAYS} days, and days \xD7 86400 must be a whole number of seconds, because Redis takes an integer expiry: 1.5 and 0.25 are, 1.1 is not.`);
|
|
570
|
+
}
|
|
571
|
+
if (isGraceNumber && graceSeconds >= refreshLifetimeSeconds) {
|
|
572
|
+
throw new Error(`[BymaxAuthModule] jwt.refreshGraceWindowSeconds (${graceSeconds} s) must be less than the refresh token lifetime jwt.refreshExpiresInDays * 86400 (${refreshLifetimeSeconds} s). A grace window equal to or longer than the token lifetime would allow grace pointers to outlive the refresh session they protect.`);
|
|
573
|
+
}
|
|
574
|
+
if (!isGraceNumber || !Number.isInteger(graceSeconds) || graceSeconds < 0 || graceSeconds > MAX_REFRESH_GRACE_WINDOW_SECONDS) {
|
|
575
|
+
throw new Error(`[BymaxAuthModule] jwt.refreshGraceWindowSeconds must be between 0 and ${MAX_REFRESH_GRACE_WINDOW_SECONDS} inclusive (received ${describeReceived(graceSeconds)}). The window is how long an already-consumed refresh token recovers a session, so it is exactly the replay window for a stolen one. It covers a client that rotated but never received the response \u2014 a retry, not a policy. 0 disables grace recovery entirely. The value must be a whole number: Redis takes an integer expiry, so a fraction or NaN would fail every rotation.`);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
__name(validateRefreshGraceWindow, "validateRefreshGraceWindow");
|
|
579
|
+
function validateScryptParallelizationCeiling(password) {
|
|
580
|
+
const parallelization = password?.parallelization;
|
|
581
|
+
if (parallelization === void 0 || !Number.isFinite(parallelization) || parallelization <= MAX_SCRYPT_PARALLELIZATION) {
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
throw new Error(`[BymaxAuthModule] password.parallelization must be at most ${MAX_SCRYPT_PARALLELIZATION} (received ${describeReceived(parallelization)}). scrypt's CPU cost grows linearly with it and an unauthenticated login derives a hash even for an address that does not exist.`);
|
|
585
|
+
}
|
|
586
|
+
__name(validateScryptParallelizationCeiling, "validateScryptParallelizationCeiling");
|
|
587
|
+
function validateNumericOptions(userOptions) {
|
|
588
|
+
validatePasswordResetOtpLength(userOptions.passwordReset);
|
|
589
|
+
validateMfaVerificationParameters(userOptions.mfa);
|
|
590
|
+
validateRefreshGraceWindow(userOptions.jwt);
|
|
591
|
+
validateAbsoluteSessionLifetime(userOptions.jwt);
|
|
592
|
+
validateScryptParallelizationCeiling(userOptions.password);
|
|
593
|
+
validateWholeNumberOptions(userOptions);
|
|
594
|
+
validateRedisTtls(userOptions);
|
|
595
|
+
}
|
|
596
|
+
__name(validateNumericOptions, "validateNumericOptions");
|
|
597
|
+
|
|
598
|
+
// src/server/config/shape-checks.ts
|
|
599
|
+
function assertObject(label, value) {
|
|
600
|
+
if (Object(value) === value) return;
|
|
601
|
+
throw new Error(`[BymaxAuthModule] ${label} must be an object (received ${describeReceived(value)}).`);
|
|
602
|
+
}
|
|
603
|
+
__name(assertObject, "assertObject");
|
|
604
|
+
function assertArray(label, value) {
|
|
605
|
+
if (Array.isArray(value)) return;
|
|
606
|
+
throw new Error(`[BymaxAuthModule] ${label} must be an array (received ${describeReceived(value)}).`);
|
|
607
|
+
}
|
|
608
|
+
__name(assertArray, "assertArray");
|
|
609
|
+
|
|
610
|
+
// src/server/config/unset-options.ts
|
|
611
|
+
var TOP_KEYS = {
|
|
612
|
+
identifierKey: true,
|
|
613
|
+
jwt: true,
|
|
614
|
+
password: true,
|
|
615
|
+
tokenDelivery: true,
|
|
616
|
+
environment: true,
|
|
617
|
+
secureCookies: true,
|
|
618
|
+
cookies: true,
|
|
619
|
+
rateLimit: true,
|
|
620
|
+
mfa: true,
|
|
621
|
+
sessions: true,
|
|
622
|
+
bruteForce: true,
|
|
623
|
+
passwordReset: true,
|
|
624
|
+
emailVerification: true,
|
|
625
|
+
platform: true,
|
|
626
|
+
emailChange: true,
|
|
627
|
+
invitations: true,
|
|
628
|
+
apiKeys: true,
|
|
629
|
+
roles: true,
|
|
630
|
+
blockedStatuses: true,
|
|
631
|
+
redisNamespace: true,
|
|
632
|
+
oauth: true,
|
|
633
|
+
workspaces: true,
|
|
634
|
+
registration: true,
|
|
635
|
+
routePrefix: true,
|
|
636
|
+
tenantIdResolver: true,
|
|
637
|
+
enforceTenantBinding: true,
|
|
638
|
+
isTrustedProxy: true,
|
|
639
|
+
controllers: true,
|
|
640
|
+
userStatusCacheTtlSeconds: true
|
|
641
|
+
};
|
|
642
|
+
var GROUP_KEYS = {
|
|
643
|
+
jwt: {
|
|
644
|
+
secret: true,
|
|
645
|
+
previousSecrets: true,
|
|
646
|
+
accessExpiresIn: true,
|
|
647
|
+
accessCookieMaxAgeMs: true,
|
|
648
|
+
refreshExpiresInDays: true,
|
|
649
|
+
absoluteSessionLifetimeDays: true,
|
|
650
|
+
algorithm: true,
|
|
651
|
+
issuer: true,
|
|
652
|
+
audience: true,
|
|
653
|
+
refreshGraceWindowSeconds: true
|
|
654
|
+
},
|
|
655
|
+
password: {
|
|
656
|
+
costFactor: true,
|
|
657
|
+
minLength: true,
|
|
658
|
+
blockSize: true,
|
|
659
|
+
parallelization: true,
|
|
660
|
+
blocklist: true
|
|
661
|
+
},
|
|
662
|
+
cookies: {
|
|
663
|
+
accessTokenName: true,
|
|
664
|
+
refreshTokenName: true,
|
|
665
|
+
sessionSignalName: true,
|
|
666
|
+
refreshCookiePath: true,
|
|
667
|
+
mfaTempCookiePath: true,
|
|
668
|
+
resolveDomains: true,
|
|
669
|
+
sameSite: true,
|
|
670
|
+
trustedOrigins: true
|
|
671
|
+
},
|
|
672
|
+
sessions: {
|
|
673
|
+
enabled: true,
|
|
674
|
+
defaultMaxSessions: true,
|
|
675
|
+
maxSessionsResolver: true
|
|
676
|
+
},
|
|
677
|
+
bruteForce: {
|
|
678
|
+
maxAttempts: true,
|
|
679
|
+
windowSeconds: true
|
|
680
|
+
},
|
|
681
|
+
passwordReset: {
|
|
682
|
+
method: true,
|
|
683
|
+
tokenTtlSeconds: true,
|
|
684
|
+
otpTtlSeconds: true,
|
|
685
|
+
otpLength: true
|
|
686
|
+
},
|
|
687
|
+
emailVerification: {
|
|
688
|
+
required: true,
|
|
689
|
+
otpTtlSeconds: true
|
|
690
|
+
},
|
|
691
|
+
platform: {
|
|
692
|
+
enabled: true
|
|
693
|
+
},
|
|
694
|
+
invitations: {
|
|
695
|
+
enabled: true,
|
|
696
|
+
tokenTtlSeconds: true
|
|
697
|
+
},
|
|
698
|
+
emailChange: {
|
|
699
|
+
tokenTtlSeconds: true
|
|
700
|
+
},
|
|
701
|
+
controllers: {
|
|
702
|
+
auth: true,
|
|
703
|
+
mfa: true,
|
|
704
|
+
passwordReset: true,
|
|
705
|
+
sessions: true,
|
|
706
|
+
platform: true,
|
|
707
|
+
oauth: true,
|
|
708
|
+
invitations: true,
|
|
709
|
+
emailChange: true,
|
|
710
|
+
workspaces: true,
|
|
711
|
+
workspaceInvitations: true
|
|
712
|
+
},
|
|
713
|
+
rateLimit: {
|
|
714
|
+
enabled: true,
|
|
715
|
+
clientIpSource: true
|
|
716
|
+
},
|
|
717
|
+
mfa: {
|
|
718
|
+
encryptionKey: true,
|
|
719
|
+
previousEncryptionKeys: true,
|
|
720
|
+
issuer: true,
|
|
721
|
+
recoveryCodeCount: true,
|
|
722
|
+
totpWindow: true
|
|
723
|
+
},
|
|
724
|
+
apiKeys: {
|
|
725
|
+
enabled: true,
|
|
726
|
+
pepper: true,
|
|
727
|
+
scopes: true,
|
|
728
|
+
header: true,
|
|
729
|
+
prefix: true,
|
|
730
|
+
lastUsedFlushSeconds: true,
|
|
731
|
+
auditMode: true
|
|
732
|
+
},
|
|
733
|
+
workspaces: {
|
|
734
|
+
enabled: true,
|
|
735
|
+
roles: true,
|
|
736
|
+
permissions: true,
|
|
737
|
+
allowCreate: true,
|
|
738
|
+
maxWorkspacesPerUser: true,
|
|
739
|
+
invitationTtlSeconds: true,
|
|
740
|
+
maxPendingInvitationsPerWorkspace: true,
|
|
741
|
+
membershipCacheTtlSeconds: true,
|
|
742
|
+
deletionGraceDays: true,
|
|
743
|
+
accessDeniedEvents: true,
|
|
744
|
+
resolveRequestWorkspace: true,
|
|
745
|
+
resolveWsWorkspace: true
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
var WORKSPACE_SUBGROUP_KEYS = {
|
|
749
|
+
accessDeniedEvents: [
|
|
750
|
+
"max",
|
|
751
|
+
"windowSeconds"
|
|
752
|
+
]
|
|
753
|
+
};
|
|
754
|
+
function isObjectGroup(value) {
|
|
755
|
+
return Object(value) === value && typeof value !== "function" && !Array.isArray(value);
|
|
756
|
+
}
|
|
757
|
+
__name(isObjectGroup, "isObjectGroup");
|
|
758
|
+
function readSetKeys(group, keys) {
|
|
759
|
+
const out = {};
|
|
760
|
+
for (const key of /* @__PURE__ */ new Set([
|
|
761
|
+
...keys,
|
|
762
|
+
...Object.keys(group)
|
|
763
|
+
])) {
|
|
764
|
+
const entry = Reflect.get(group, key);
|
|
765
|
+
if (entry !== void 0 && entry !== null) out[key] = entry;
|
|
766
|
+
}
|
|
767
|
+
return out;
|
|
768
|
+
}
|
|
769
|
+
__name(readSetKeys, "readSetKeys");
|
|
770
|
+
function dropUnsetKeys(value) {
|
|
771
|
+
const copy = {
|
|
772
|
+
...value
|
|
773
|
+
};
|
|
774
|
+
for (const [key, entry] of Object.entries(copy)) {
|
|
775
|
+
if (entry === void 0 || entry === null) Reflect.deleteProperty(copy, key);
|
|
776
|
+
}
|
|
777
|
+
return copy;
|
|
778
|
+
}
|
|
779
|
+
__name(dropUnsetKeys, "dropUnsetKeys");
|
|
780
|
+
function dropUnset(options) {
|
|
781
|
+
const copy = readSetKeys(options, Object.keys(TOP_KEYS));
|
|
782
|
+
for (const [name, keys] of Object.entries(GROUP_KEYS)) {
|
|
783
|
+
const entry = copy[name];
|
|
784
|
+
if (!isObjectGroup(entry)) continue;
|
|
785
|
+
const group = readSetKeys(entry, Object.keys(keys));
|
|
786
|
+
if (name === "workspaces") {
|
|
787
|
+
for (const [sub, subKeys] of Object.entries(WORKSPACE_SUBGROUP_KEYS)) {
|
|
788
|
+
const inner = group[sub];
|
|
789
|
+
if (isObjectGroup(inner)) group[sub] = readSetKeys(inner, subKeys);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
copy[name] = group;
|
|
793
|
+
}
|
|
794
|
+
return Object.assign({}, copy);
|
|
795
|
+
}
|
|
796
|
+
__name(dropUnset, "dropUnset");
|
|
367
797
|
|
|
368
798
|
// src/server/constants/workspace-roles.ts
|
|
369
799
|
var ANY_WORKSPACE_MEMBER = /* @__PURE__ */ Symbol("ANY_WORKSPACE_MEMBER");
|
|
@@ -518,6 +948,7 @@ __name(validateWorkspaceRoles, "validateWorkspaceRoles");
|
|
|
518
948
|
function validateWorkspacePermissions(permissions, roles) {
|
|
519
949
|
for (const [action, list] of Object.entries(permissions ?? {})) {
|
|
520
950
|
if (list === void 0) continue;
|
|
951
|
+
assertArray(`workspaces.permissions.${action}`, list);
|
|
521
952
|
for (const role of list) {
|
|
522
953
|
const known = roles.has(role);
|
|
523
954
|
if (!known && !(action === "viewMembers" && role === ANY_WORKSPACE_MEMBER)) {
|
|
@@ -668,113 +1099,6 @@ __name(assertWorkspaceProviders, "assertWorkspaceProviders");
|
|
|
668
1099
|
|
|
669
1100
|
// src/server/constants/token-epoch.ts
|
|
670
1101
|
var TOKEN_EPOCH_RETENTION_SECONDS = 30 * 24 * 60 * 60;
|
|
671
|
-
var TOTP_STEP_SECONDS = 30;
|
|
672
|
-
var TOTP_DIGITS = 6;
|
|
673
|
-
var BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
674
|
-
var MIN_SECRET_BYTES = 20;
|
|
675
|
-
function toBase32(input) {
|
|
676
|
-
let bits = 0;
|
|
677
|
-
let value = 0;
|
|
678
|
-
let output = "";
|
|
679
|
-
for (let i = 0; i < input.length; i++) {
|
|
680
|
-
value = value << 8 | input.readUInt8(i);
|
|
681
|
-
bits += 8;
|
|
682
|
-
while (bits >= 5) {
|
|
683
|
-
bits -= 5;
|
|
684
|
-
output += BASE32_ALPHABET[value >>> bits & 31];
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
if (bits > 0) {
|
|
688
|
-
output += BASE32_ALPHABET[value << 5 - bits & 31];
|
|
689
|
-
}
|
|
690
|
-
return output;
|
|
691
|
-
}
|
|
692
|
-
__name(toBase32, "toBase32");
|
|
693
|
-
function fromBase32(input) {
|
|
694
|
-
const normalized = input.toUpperCase().replace(/[\s=-]/g, "");
|
|
695
|
-
let bits = 0;
|
|
696
|
-
let value = 0;
|
|
697
|
-
const output = [];
|
|
698
|
-
for (const char of normalized) {
|
|
699
|
-
const idx = BASE32_ALPHABET.indexOf(char);
|
|
700
|
-
if (idx === -1) {
|
|
701
|
-
throw new Error(`[totp] Invalid Base32 character: ${JSON.stringify(char)}`);
|
|
702
|
-
}
|
|
703
|
-
value = value << 5 | idx;
|
|
704
|
-
bits += 5;
|
|
705
|
-
if (bits >= 8) {
|
|
706
|
-
bits -= 8;
|
|
707
|
-
output.push(value >>> bits & 255);
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
return Buffer.from(output);
|
|
711
|
-
}
|
|
712
|
-
__name(fromBase32, "fromBase32");
|
|
713
|
-
function generateTotpSecret() {
|
|
714
|
-
const raw = crypto.randomBytes(MIN_SECRET_BYTES);
|
|
715
|
-
return {
|
|
716
|
-
raw,
|
|
717
|
-
base32: toBase32(raw)
|
|
718
|
-
};
|
|
719
|
-
}
|
|
720
|
-
__name(generateTotpSecret, "generateTotpSecret");
|
|
721
|
-
function buildTotpUri(secretBase32, accountEmail, issuer) {
|
|
722
|
-
if (!secretBase32 || secretBase32.length < 32) {
|
|
723
|
-
throw new Error("[totp] secretBase32 must encode at least 20 bytes (minimum 32 base32 characters)");
|
|
724
|
-
}
|
|
725
|
-
if (!issuer) {
|
|
726
|
-
throw new Error("[totp] issuer must not be empty");
|
|
727
|
-
}
|
|
728
|
-
if (!accountEmail) {
|
|
729
|
-
throw new Error("[totp] accountEmail must not be empty");
|
|
730
|
-
}
|
|
731
|
-
const label = `${encodeURIComponent(issuer)}:${encodeURIComponent(accountEmail)}`;
|
|
732
|
-
const qs = [
|
|
733
|
-
`secret=${encodeURIComponent(secretBase32)}`,
|
|
734
|
-
`issuer=${encodeURIComponent(issuer)}`,
|
|
735
|
-
`algorithm=SHA1`,
|
|
736
|
-
`digits=${String(TOTP_DIGITS)}`,
|
|
737
|
-
`period=${String(TOTP_STEP_SECONDS)}`
|
|
738
|
-
].join("&");
|
|
739
|
-
return `otpauth://totp/${label}?${qs}`;
|
|
740
|
-
}
|
|
741
|
-
__name(buildTotpUri, "buildTotpUri");
|
|
742
|
-
function generateHotp(secretBase32, counter) {
|
|
743
|
-
const key = fromBase32(secretBase32);
|
|
744
|
-
if (key.length < MIN_SECRET_BYTES) {
|
|
745
|
-
throw new Error(`[totp] Secret too short: ${key.length} bytes (minimum ${MIN_SECRET_BYTES}). Generate secrets with generateTotpSecret().`);
|
|
746
|
-
}
|
|
747
|
-
if (!Number.isInteger(counter) || counter < 0 || counter > Number.MAX_SAFE_INTEGER) {
|
|
748
|
-
throw new Error(`[totp] Counter must be a non-negative safe integer (got ${counter}).`);
|
|
749
|
-
}
|
|
750
|
-
const counterBuffer = Buffer.allocUnsafe(8);
|
|
751
|
-
const hi = Math.floor(counter / 4294967296);
|
|
752
|
-
const lo = counter >>> 0;
|
|
753
|
-
counterBuffer.writeUInt32BE(hi, 0);
|
|
754
|
-
counterBuffer.writeUInt32BE(lo, 4);
|
|
755
|
-
const hmac = crypto.createHmac("sha1", key).update(counterBuffer).digest();
|
|
756
|
-
const offset = hmac.readUInt8(hmac.length - 1) & 15;
|
|
757
|
-
const truncated = (hmac.readUInt8(offset) & 127) << 24 | (hmac.readUInt8(offset + 1) & 255) << 16 | (hmac.readUInt8(offset + 2) & 255) << 8 | hmac.readUInt8(offset + 3);
|
|
758
|
-
const mod = 10 ** TOTP_DIGITS;
|
|
759
|
-
return String(truncated % mod).padStart(TOTP_DIGITS, "0");
|
|
760
|
-
}
|
|
761
|
-
__name(generateHotp, "generateHotp");
|
|
762
|
-
var MAX_VERIFY_WINDOW = 2;
|
|
763
|
-
function verifyTotp(secretBase32, code, window = 1) {
|
|
764
|
-
if (!/^\d{6}$/.test(code)) return false;
|
|
765
|
-
const currentStep = Math.floor(Date.now() / 1e3 / TOTP_STEP_SECONDS);
|
|
766
|
-
const effective = Math.min(Math.max(window, 0), MAX_VERIFY_WINDOW);
|
|
767
|
-
for (let delta = -effective; delta <= effective; delta++) {
|
|
768
|
-
const expected = generateHotp(secretBase32, currentStep + delta);
|
|
769
|
-
const expectedBuf = Buffer.from(expected, "utf8");
|
|
770
|
-
const codeBuf = Buffer.from(code, "utf8");
|
|
771
|
-
if (expectedBuf.length === codeBuf.length && crypto.timingSafeEqual(expectedBuf, codeBuf)) {
|
|
772
|
-
return true;
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
return false;
|
|
776
|
-
}
|
|
777
|
-
__name(verifyTotp, "verifyTotp");
|
|
778
1102
|
|
|
779
1103
|
// src/server/config/resolved-options.ts
|
|
780
1104
|
var HMAC_KEY_DERIVATION_LABEL = "bymax-auth:hmac-key:v1";
|
|
@@ -786,17 +1110,17 @@ function isProductionDeployment(userOptions) {
|
|
|
786
1110
|
return (userOptions.environment ?? "production") === "production";
|
|
787
1111
|
}
|
|
788
1112
|
__name(isProductionDeployment, "isProductionDeployment");
|
|
789
|
-
function resolveOptions(
|
|
1113
|
+
function resolveOptions(given) {
|
|
1114
|
+
const userOptions = dropUnset(given);
|
|
790
1115
|
validateJwt(userOptions.jwt);
|
|
791
1116
|
validateIdentifierKey(userOptions.identifierKey);
|
|
792
1117
|
validateMfaEncryptionKey(userOptions.mfa);
|
|
793
1118
|
validateRolesHierarchy(userOptions.roles);
|
|
794
1119
|
validatePlatformAdmin(userOptions.platform, userOptions.roles);
|
|
795
|
-
|
|
1120
|
+
validateNumericOptions(userOptions);
|
|
796
1121
|
validatePasswordMinLength(userOptions.password);
|
|
797
1122
|
validatePasswordCostFactor(userOptions.password);
|
|
798
1123
|
validatePasswordMemoryParameters(userOptions.password);
|
|
799
|
-
validateMfaVerificationParameters(userOptions.mfa);
|
|
800
1124
|
validateBruteForce(userOptions.bruteForce);
|
|
801
1125
|
validateOAuthProviders(userOptions.oauth, isProductionDeployment(userOptions));
|
|
802
1126
|
validateClientIpSource(userOptions.rateLimit);
|
|
@@ -812,7 +1136,6 @@ function resolveOptions(userOptions) {
|
|
|
812
1136
|
validateProductionCookiesAreSecure(userOptions);
|
|
813
1137
|
validateCookiePrefixes(userOptions);
|
|
814
1138
|
validateTrustedOrigins(userOptions);
|
|
815
|
-
validateRefreshGraceWindow(userOptions.jwt);
|
|
816
1139
|
validateAccessLifetimeAgainstEpochRetention(userOptions.jwt);
|
|
817
1140
|
const { mfa: _mfa, identifierKey: _identifierKey, workspaces: _workspaces, ...userOptionsWithoutMfa } = userOptions;
|
|
818
1141
|
const resolved = {
|
|
@@ -886,16 +1209,14 @@ function resolveOptions(userOptions) {
|
|
|
886
1209
|
userStatusCacheTtlSeconds: userOptions.userStatusCacheTtlSeconds ?? DEFAULT_OPTIONS.userStatusCacheTtlSeconds,
|
|
887
1210
|
// Evaluated once at startup — not re-evaluated per request.
|
|
888
1211
|
//
|
|
889
|
-
// `clientIpSource` has no default and `validateClientIpSource`
|
|
890
|
-
//
|
|
891
|
-
//
|
|
892
|
-
// consults no request header at all.
|
|
1212
|
+
// `clientIpSource` has no default and `validateClientIpSource` refused a deployment that left it
|
|
1213
|
+
// unset with limiting on. The fallback is reached only when limiting is OFF, where the value is
|
|
1214
|
+
// never read; `'peer'` is the inert choice because it consults no request header at all.
|
|
893
1215
|
rateLimit: {
|
|
894
1216
|
...DEFAULT_OPTIONS.rateLimit,
|
|
895
1217
|
...userOptions.rateLimit,
|
|
896
|
-
// Stryker disable next-line OptionalChaining: unreachable — `validateClientIpSource` has
|
|
897
|
-
//
|
|
898
|
-
// naming a source or disabling limiting), so `rateLimit` is never undefined here
|
|
1218
|
+
// Stryker disable next-line OptionalChaining: unreachable — `validateClientIpSource` has run, and
|
|
1219
|
+
// it returns only when `rateLimit` is an object (naming a source or disabling limiting)
|
|
899
1220
|
clientIpSource: userOptions.rateLimit?.clientIpSource ?? "peer"
|
|
900
1221
|
},
|
|
901
1222
|
environment: userOptions.environment ?? "production",
|
|
@@ -960,7 +1281,7 @@ function validateClientIpSource(rateLimit) {
|
|
|
960
1281
|
if (rateLimit?.clientIpSource === "peer" || rateLimit?.clientIpSource === "trusted-proxy") {
|
|
961
1282
|
return;
|
|
962
1283
|
}
|
|
963
|
-
throw new Error(`[BymaxAuthModule] rateLimit.clientIpSource must be 'peer' or 'trusted-proxy' when rate limiting is enabled (
|
|
1284
|
+
throw new Error(`[BymaxAuthModule] rateLimit.clientIpSource must be 'peer' or 'trusted-proxy' when rate limiting is enabled (received ${describeReceived(rateLimit?.clientIpSource)}). Set 'peer' when the application is directly exposed (the limit keys on the socket address), or 'trusted-proxy' when it runs behind a proxy and 'trust proxy' is configured for the real hop count (the limit keys on the forwarded client address). There is no default: 'peer' behind a proxy puts every client in ONE bucket, so one caller can rate-limit your whole user base, and 'trusted-proxy' without a proxy lets the caller choose their own key. Both look like a working limiter. Pass rateLimit.enabled: false if the limits are enforced at the edge instead.`);
|
|
964
1285
|
}
|
|
965
1286
|
__name(validateClientIpSource, "validateClientIpSource");
|
|
966
1287
|
function validateTenantBinding(userOptions) {
|
|
@@ -1067,6 +1388,7 @@ function validateRolesHierarchy(roles) {
|
|
|
1067
1388
|
}
|
|
1068
1389
|
const allRoles = new Set(Object.keys(roles.hierarchy));
|
|
1069
1390
|
for (const [role, children] of Object.entries(roles.hierarchy)) {
|
|
1391
|
+
assertArray(`roles.hierarchy['${role}']`, children);
|
|
1070
1392
|
for (const child of children) {
|
|
1071
1393
|
if (!allRoles.has(child)) {
|
|
1072
1394
|
throw new Error(`[BymaxAuthModule] roles.hierarchy['${role}'] references unknown role '${child}'. All roles referenced as children must be declared as keys in the hierarchy.`);
|
|
@@ -1081,24 +1403,16 @@ function validatePlatformAdmin(platform, roles) {
|
|
|
1081
1403
|
}
|
|
1082
1404
|
}
|
|
1083
1405
|
__name(validatePlatformAdmin, "validatePlatformAdmin");
|
|
1084
|
-
function validatePasswordResetOtpLength(passwordReset) {
|
|
1085
|
-
const otpLength = passwordReset?.otpLength;
|
|
1086
|
-
if (otpLength === void 0) return;
|
|
1087
|
-
if (otpLength < 4 || otpLength > 8) {
|
|
1088
|
-
throw new Error(`[BymaxAuthModule] passwordReset.otpLength must be between 4 and 8 inclusive (current: ${otpLength}). Values below 4 are too easily guessable; values above 8 are not required for security and degrade user experience.`);
|
|
1089
|
-
}
|
|
1090
|
-
}
|
|
1091
|
-
__name(validatePasswordResetOtpLength, "validatePasswordResetOtpLength");
|
|
1092
1406
|
function assertIntegralScryptParameter(name, value) {
|
|
1093
1407
|
if (value === void 0 || Number.isInteger(value)) return;
|
|
1094
|
-
throw new Error(`[BymaxAuthModule] password.${name} must be an integer (
|
|
1408
|
+
throw new Error(`[BymaxAuthModule] password.${name} must be an integer (received ${describeReceived(value)}). Node's crypto.scrypt rejects a non-integral cost parameter, and it does so on the first derivation \u2014 which is the first login after deploy, not at startup.`);
|
|
1095
1409
|
}
|
|
1096
1410
|
__name(assertIntegralScryptParameter, "assertIntegralScryptParameter");
|
|
1097
1411
|
function validatePasswordMinLength(password) {
|
|
1098
1412
|
const minLength = password?.minLength;
|
|
1099
1413
|
if (minLength === void 0) return;
|
|
1100
1414
|
if (!Number.isInteger(minLength) || minLength < 8 || minLength > 128) {
|
|
1101
|
-
throw new Error(`[BymaxAuthModule] password.minLength must be an integer between 8 and 128 (
|
|
1415
|
+
throw new Error(`[BymaxAuthModule] password.minLength must be an integer between 8 and 128 (received ${describeReceived(minLength)}). NIST SP 800-63B-4 \xA73.1.1.1 allows 8 only for a password used as part of multi-factor authentication and requires 15 for one used as a single factor; 128 is the maximum the password DTOs accept, so a higher policy would reject every password that reaches this service.`);
|
|
1102
1416
|
}
|
|
1103
1417
|
}
|
|
1104
1418
|
__name(validatePasswordMinLength, "validatePasswordMinLength");
|
|
@@ -1107,24 +1421,20 @@ function validatePasswordCostFactor(password) {
|
|
|
1107
1421
|
if (costFactor === void 0) return;
|
|
1108
1422
|
assertIntegralScryptParameter("costFactor", costFactor);
|
|
1109
1423
|
if (costFactor < 16384) {
|
|
1110
|
-
throw new Error(`[BymaxAuthModule] password.costFactor must be at least 16384 (2^14) (
|
|
1424
|
+
throw new Error(`[BymaxAuthModule] password.costFactor must be at least 16384 (2^14) (received ${describeReceived(costFactor)}). Lower values produce hashes vulnerable to brute-force attacks. The recommended minimum for production is 32768 (2^15).`);
|
|
1111
1425
|
}
|
|
1112
1426
|
if ((costFactor & costFactor - 1) !== 0) {
|
|
1113
|
-
throw new Error(`[BymaxAuthModule] password.costFactor must be a power of 2 (
|
|
1427
|
+
throw new Error(`[BymaxAuthModule] password.costFactor must be a power of 2 (received ${describeReceived(costFactor)}).`);
|
|
1114
1428
|
}
|
|
1115
1429
|
}
|
|
1116
1430
|
__name(validatePasswordCostFactor, "validatePasswordCostFactor");
|
|
1117
1431
|
function validateBruteForce(bruteForce) {
|
|
1118
|
-
const windowSeconds = bruteForce?.windowSeconds;
|
|
1119
|
-
if (windowSeconds !== void 0 && (!Number.isInteger(windowSeconds) || windowSeconds < 1)) {
|
|
1120
|
-
throw new Error(`[BymaxAuthModule] bruteForce.windowSeconds must be a whole number of at least 1 (current: ${windowSeconds}). Redis deletes a key on \`EXPIRE key 0\`, so a zero or negative window destroys each failure counter as it is created and the account lockout never engages \u2014 silently, with the configuration still reading as enabled.`);
|
|
1121
|
-
}
|
|
1122
1432
|
const maxAttempts = bruteForce?.maxAttempts;
|
|
1123
1433
|
if (maxAttempts !== void 0 && (!Number.isInteger(maxAttempts) || maxAttempts < 1)) {
|
|
1124
|
-
throw new Error(`[BymaxAuthModule] bruteForce.maxAttempts must be a whole number of at least 1 (
|
|
1434
|
+
throw new Error(`[BymaxAuthModule] bruteForce.maxAttempts must be a whole number of at least 1 (received ${describeReceived(maxAttempts)}). Zero locks out every account permanently, because a freshly created counter already satisfies "attempts >= 0".`);
|
|
1125
1435
|
}
|
|
1126
1436
|
if (maxAttempts !== void 0 && maxAttempts > MAX_BRUTE_FORCE_ATTEMPTS) {
|
|
1127
|
-
throw new Error(`[BymaxAuthModule] bruteForce.maxAttempts must not exceed ${MAX_BRUTE_FORCE_ATTEMPTS} (
|
|
1437
|
+
throw new Error(`[BymaxAuthModule] bruteForce.maxAttempts must not exceed ${MAX_BRUTE_FORCE_ATTEMPTS} (received ${describeReceived(maxAttempts)}). A threshold this high disables the lockout as effectively as switching it off, while the configuration still reads as enabled.`);
|
|
1128
1438
|
}
|
|
1129
1439
|
}
|
|
1130
1440
|
__name(validateBruteForce, "validateBruteForce");
|
|
@@ -1134,12 +1444,12 @@ function validatePasswordMemoryParameters(password) {
|
|
|
1134
1444
|
const blockSize = password?.blockSize;
|
|
1135
1445
|
assertIntegralScryptParameter("blockSize", blockSize);
|
|
1136
1446
|
if (blockSize !== void 0 && blockSize < 8) {
|
|
1137
|
-
throw new Error(`[BymaxAuthModule] password.blockSize must be at least 8 (
|
|
1447
|
+
throw new Error(`[BymaxAuthModule] password.blockSize must be at least 8 (received ${describeReceived(blockSize)}). scrypt's memory cost is 128 * N * r, so a smaller block size divides the memory hardness that password.costFactor's floor exists to guarantee.`);
|
|
1138
1448
|
}
|
|
1139
1449
|
const parallelization = password?.parallelization;
|
|
1140
1450
|
assertIntegralScryptParameter("parallelization", parallelization);
|
|
1141
1451
|
if (parallelization !== void 0 && parallelization < 1) {
|
|
1142
|
-
throw new Error(`[BymaxAuthModule] password.parallelization must be at least 1 (
|
|
1452
|
+
throw new Error(`[BymaxAuthModule] password.parallelization must be at least 1 (received ${describeReceived(parallelization)}).`);
|
|
1143
1453
|
}
|
|
1144
1454
|
const effectiveN = password?.costFactor ?? DEFAULT_OPTIONS.password.costFactor;
|
|
1145
1455
|
const effectiveR = password?.blockSize ?? DEFAULT_OPTIONS.password.blockSize;
|
|
@@ -1149,17 +1459,6 @@ function validatePasswordMemoryParameters(password) {
|
|
|
1149
1459
|
}
|
|
1150
1460
|
}
|
|
1151
1461
|
__name(validatePasswordMemoryParameters, "validatePasswordMemoryParameters");
|
|
1152
|
-
function validateMfaVerificationParameters(mfa) {
|
|
1153
|
-
const totpWindow = mfa?.totpWindow;
|
|
1154
|
-
if (totpWindow !== void 0 && (totpWindow < 0 || totpWindow > MAX_VERIFY_WINDOW)) {
|
|
1155
|
-
throw new Error(`[BymaxAuthModule] mfa.totpWindow must be between 0 and ${MAX_VERIFY_WINDOW} inclusive (current: ${totpWindow}). The window is counted in 30-second steps on either side of now, so ${2 * totpWindow + 1} codes would be valid at once. RFC 6238 \xA75.2 recommends at most one step of tolerance; the default of 1 accepts three codes. The bound matches the drift window the verifier actually applies, so a configured value always means what it says instead of being silently clamped.`);
|
|
1156
|
-
}
|
|
1157
|
-
const recoveryCodeCount = mfa?.recoveryCodeCount;
|
|
1158
|
-
if (recoveryCodeCount !== void 0 && (recoveryCodeCount < 1 || recoveryCodeCount > 50)) {
|
|
1159
|
-
throw new Error(`[BymaxAuthModule] mfa.recoveryCodeCount must be between 1 and 50 inclusive (current: ${recoveryCodeCount}). Zero enrols an account with no way back if the authenticator is lost.`);
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1162
|
-
__name(validateMfaVerificationParameters, "validateMfaVerificationParameters");
|
|
1163
1462
|
var REQUIRED_OAUTH_FIELDS = [
|
|
1164
1463
|
"clientId",
|
|
1165
1464
|
"clientSecret",
|
|
@@ -1170,6 +1469,7 @@ function validateOAuthProviders(oauth, isProduction) {
|
|
|
1170
1469
|
for (const [providerOrField, rawConfig] of Object.entries(oauth)) {
|
|
1171
1470
|
if (providerOrField === "successRedirectUrl" || providerOrField === "mfaRedirectUrl" || providerOrField === "errorRedirectUrl") continue;
|
|
1172
1471
|
const provider = providerOrField;
|
|
1472
|
+
assertObject(`oauth.${provider}`, rawConfig);
|
|
1173
1473
|
const config = rawConfig;
|
|
1174
1474
|
for (const field of REQUIRED_OAUTH_FIELDS) {
|
|
1175
1475
|
if (!config[field]) {
|
|
@@ -1178,7 +1478,7 @@ function validateOAuthProviders(oauth, isProduction) {
|
|
|
1178
1478
|
}
|
|
1179
1479
|
const callbackUrl = config["callbackUrl"];
|
|
1180
1480
|
if (typeof callbackUrl === "string" && !callbackUrl.startsWith("https://") && isProduction) {
|
|
1181
|
-
throw new Error(`[BymaxAuthModule] oauth.${provider}.callbackUrl must use HTTPS in production (
|
|
1481
|
+
throw new Error(`[BymaxAuthModule] oauth.${provider}.callbackUrl must use HTTPS in production (received a string). Use an HTTPS URL to prevent authorization code interception.`);
|
|
1182
1482
|
}
|
|
1183
1483
|
}
|
|
1184
1484
|
}
|
|
@@ -1192,7 +1492,7 @@ function validateOAuthSuccessRedirectUrl(userOptions) {
|
|
|
1192
1492
|
const isProduction = isProductionDeployment(userOptions);
|
|
1193
1493
|
const isSafe = url.startsWith("/") || url.startsWith("https://");
|
|
1194
1494
|
if (isProduction && !isSafe) {
|
|
1195
|
-
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (
|
|
1495
|
+
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (received a string).`);
|
|
1196
1496
|
}
|
|
1197
1497
|
if (userOptions.tokenDelivery === "bearer") {
|
|
1198
1498
|
throw new Error(`[BymaxAuthModule] oauth.successRedirectUrl is set but tokenDelivery is 'bearer'. A redirect discards the JSON response body, so the access token would never reach the client. Use tokenDelivery: 'cookie' or 'both' when configuring a successRedirectUrl.`);
|
|
@@ -1208,7 +1508,7 @@ function validateOAuthMfaRedirectUrl(userOptions) {
|
|
|
1208
1508
|
const isProduction = isProductionDeployment(userOptions);
|
|
1209
1509
|
const isSafe = url.startsWith("/") || url.startsWith("https://");
|
|
1210
1510
|
if (isProduction && !isSafe) {
|
|
1211
|
-
throw new Error(`[BymaxAuthModule] oauth.mfaRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (
|
|
1511
|
+
throw new Error(`[BymaxAuthModule] oauth.mfaRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (received a string).`);
|
|
1212
1512
|
}
|
|
1213
1513
|
}
|
|
1214
1514
|
__name(validateOAuthMfaRedirectUrl, "validateOAuthMfaRedirectUrl");
|
|
@@ -1221,7 +1521,7 @@ function validateOAuthErrorRedirectUrl(userOptions) {
|
|
|
1221
1521
|
const isProduction = isProductionDeployment(userOptions);
|
|
1222
1522
|
const isSafe = url.startsWith("/") || url.startsWith("https://");
|
|
1223
1523
|
if (isProduction && !isSafe) {
|
|
1224
|
-
throw new Error(`[BymaxAuthModule] oauth.errorRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (
|
|
1524
|
+
throw new Error(`[BymaxAuthModule] oauth.errorRedirectUrl must use HTTPS or be a same-origin path (starts with '/') in production (received a string).`);
|
|
1225
1525
|
}
|
|
1226
1526
|
}
|
|
1227
1527
|
__name(validateOAuthErrorRedirectUrl, "validateOAuthErrorRedirectUrl");
|
|
@@ -1303,22 +1603,6 @@ function isAbsoluteOrigin(value) {
|
|
|
1303
1603
|
}
|
|
1304
1604
|
}
|
|
1305
1605
|
__name(isAbsoluteOrigin, "isAbsoluteOrigin");
|
|
1306
|
-
var MAX_REFRESH_GRACE_WINDOW_SECONDS = 300;
|
|
1307
|
-
function validateRefreshGraceWindow(jwt) {
|
|
1308
|
-
const graceSeconds = jwt.refreshGraceWindowSeconds ?? DEFAULT_OPTIONS.jwt.refreshGraceWindowSeconds;
|
|
1309
|
-
const refreshExpiresInDays = jwt.refreshExpiresInDays ?? DEFAULT_OPTIONS.jwt.refreshExpiresInDays;
|
|
1310
|
-
const refreshLifetimeSeconds = refreshExpiresInDays * 86400;
|
|
1311
|
-
if (!Number.isFinite(refreshExpiresInDays) || refreshExpiresInDays <= 0) {
|
|
1312
|
-
throw new Error(`[BymaxAuthModule] jwt.refreshExpiresInDays must be a positive finite number. Got: ${refreshExpiresInDays}. Zero, negative, NaN, and Infinity are all rejected \u2014 any of these would produce an invalid Redis TTL and cause all token rotations to fail at runtime.`);
|
|
1313
|
-
}
|
|
1314
|
-
if (graceSeconds >= refreshLifetimeSeconds) {
|
|
1315
|
-
throw new Error(`[BymaxAuthModule] jwt.refreshGraceWindowSeconds (${graceSeconds} s) must be less than the refresh token lifetime jwt.refreshExpiresInDays * 86400 (${refreshLifetimeSeconds} s). A grace window equal to or longer than the token lifetime would allow grace pointers to outlive the refresh session they protect.`);
|
|
1316
|
-
}
|
|
1317
|
-
if (graceSeconds < 0 || graceSeconds > MAX_REFRESH_GRACE_WINDOW_SECONDS) {
|
|
1318
|
-
throw new Error(`[BymaxAuthModule] jwt.refreshGraceWindowSeconds must be between 0 and ${MAX_REFRESH_GRACE_WINDOW_SECONDS} inclusive (current: ${graceSeconds}). The window is how long an already-consumed refresh token still recovers a session, so it is exactly the replay window for a stolen one. It covers a client that rotated but never received the response \u2014 a retry, not a policy. 0 disables grace recovery entirely.`);
|
|
1319
|
-
}
|
|
1320
|
-
}
|
|
1321
|
-
__name(validateRefreshGraceWindow, "validateRefreshGraceWindow");
|
|
1322
1606
|
function validateAccessLifetimeAgainstEpochRetention(jwt) {
|
|
1323
1607
|
const configured = jwt.accessExpiresIn ?? DEFAULT_OPTIONS.jwt.accessExpiresIn;
|
|
1324
1608
|
const seconds = durationToSeconds(configured);
|
|
@@ -24884,7 +25168,7 @@ var DefaultAuthEmailProvider = class _DefaultAuthEmailProvider {
|
|
|
24884
25168
|
this.sink = sink;
|
|
24885
25169
|
this.messages = {
|
|
24886
25170
|
...DEFAULT_MESSAGES,
|
|
24887
|
-
...options?.messages
|
|
25171
|
+
...dropUnsetKeys(options?.messages ?? {})
|
|
24888
25172
|
};
|
|
24889
25173
|
const configured = options?.onDeliveryError;
|
|
24890
25174
|
this.deliveryErrorPolicy = new Map(typeof configured === "string" ? AUTH_EMAIL_KINDS.map((kind) => [
|