@bymax-one/nest-auth 1.4.20 → 1.4.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -1
- package/README.md +9 -8
- package/dist/server/index.cjs +1171 -1013
- package/dist/server/index.d.cts +250 -173
- package/dist/server/index.d.ts +250 -173
- package/dist/server/index.mjs +1172 -1014
- package/package.json +1 -1
package/dist/server/index.d.cts
CHANGED
|
@@ -8930,58 +8930,95 @@ declare class EmailChangeService implements OnModuleInit {
|
|
|
8930
8930
|
}
|
|
8931
8931
|
|
|
8932
8932
|
/**
|
|
8933
|
-
*
|
|
8934
|
-
*
|
|
8933
|
+
* @fileoverview The brute-force counters of the MFA-adjacent flows (`challenge`, `disable`,
|
|
8934
|
+
* `reauth`): which identifier each flow counts under, and the lockout read, failure record and
|
|
8935
|
+
* reset over it.
|
|
8935
8936
|
*
|
|
8936
|
-
* @
|
|
8937
|
-
* `recoveryCodes` must be displayed to the user **once** at setup time and never
|
|
8938
|
-
* stored in plain text — the service stores a keyed HMAC-SHA-256 of each one.
|
|
8939
|
-
* `secret` is provided for manual entry in authenticator apps that cannot scan a QR code.
|
|
8937
|
+
* @layer Service
|
|
8940
8938
|
*/
|
|
8941
|
-
|
|
8942
|
-
|
|
8943
|
-
|
|
8944
|
-
|
|
8945
|
-
|
|
8946
|
-
|
|
8947
|
-
|
|
8939
|
+
|
|
8940
|
+
/** The flows that keep a failure counter of their own. */
|
|
8941
|
+
type MfaFlow = 'challenge' | 'disable' | 'reauth';
|
|
8942
|
+
/** The authentication plane a counter belongs to. */
|
|
8943
|
+
type MfaFlowPlane = 'dashboard' | 'platform';
|
|
8944
|
+
/** Failure counters for the MFA flows, keyed by flow, plane, account and tenant. */
|
|
8945
|
+
declare class MfaFlowLockoutService {
|
|
8946
|
+
private readonly options;
|
|
8947
|
+
private readonly bruteForce;
|
|
8948
|
+
constructor(options: ResolvedOptions, bruteForce: BruteForceService);
|
|
8949
|
+
/**
|
|
8950
|
+
* The tenant-scoped brute-force identifier for one MFA flow, and the plane-only one that pods on
|
|
8951
|
+
* an older version count under. The two are equal on the platform plane, where the subject
|
|
8952
|
+
* carries no tenant, and distinct on the dashboard plane, where it does.
|
|
8953
|
+
*
|
|
8954
|
+
* A rolling upgrade runs old and new code against one Redis at once: on the dashboard plane an
|
|
8955
|
+
* old pod counts a failure under `{flow}:{plane}:{userId}` while a new pod counts it under
|
|
8956
|
+
* `{flow}:{userSubject}`, and either alone leaves a hole: a lockout an old pod filled would not
|
|
8957
|
+
* stop a new pod, and a success on one would not clear the other. So every read consults both
|
|
8958
|
+
* and every write touches both, and the two move as one; the plane-only arm can be dropped once
|
|
8959
|
+
* no pod writes it. On the platform plane the two ids are identical, so the plane-only arm is
|
|
8960
|
+
* skipped: operating it would be the same key a second time, double-counting a failure and
|
|
8961
|
+
* locking a platform admin out at half the threshold. `{flow}` (`challenge` / `disable` /
|
|
8962
|
+
* `reauth`) keeps the three counters isolated from each other.
|
|
8963
|
+
*/
|
|
8964
|
+
private counterIds;
|
|
8965
|
+
/**
|
|
8966
|
+
* Locked out when EITHER the legacy or the tenant-scoped counter has reached the threshold, so
|
|
8967
|
+
* neither a lockout an old pod recorded nor one a new pod recorded can be bypassed mid-migration.
|
|
8968
|
+
*/
|
|
8969
|
+
isLockedOut(flow: MfaFlow, context: MfaFlowPlane, userId: string, tenantId: string | undefined): Promise<boolean>;
|
|
8970
|
+
/**
|
|
8971
|
+
* Records the failure under both counters so the budget an attacker spends is seen by old and
|
|
8972
|
+
* new code alike — but under the scoped id ONCE when the two coincide (platform plane).
|
|
8973
|
+
*/
|
|
8974
|
+
recordFailure(flow: MfaFlow, context: MfaFlowPlane, userId: string, tenantId: string | undefined): Promise<void>;
|
|
8975
|
+
/**
|
|
8976
|
+
* Clears both counters on success — resetting only one would leave the other to lock a
|
|
8977
|
+
* legitimately authenticated user out on the next attempt — collapsing to one when they coincide.
|
|
8978
|
+
*/
|
|
8979
|
+
reset(flow: MfaFlow, context: MfaFlowPlane, userId: string, tenantId: string | undefined): Promise<void>;
|
|
8948
8980
|
}
|
|
8981
|
+
|
|
8949
8982
|
/**
|
|
8950
|
-
*
|
|
8951
|
-
*
|
|
8952
|
-
* Handles the complete MFA flow for both dashboard users and platform admins:
|
|
8953
|
-
* - **Setup**: generate TOTP secret + recovery codes, store temporarily in Redis
|
|
8954
|
-
* - **Verify & Enable**: confirm first TOTP code, persist encrypted secret to DB
|
|
8955
|
-
* - **Challenge**: exchange a valid MFA temp token + code for full access tokens
|
|
8956
|
-
* - **Disable**: require a current TOTP code to disable MFA
|
|
8983
|
+
* @fileoverview The TOTP secret's at-rest cipher: encrypt under the current key, decrypt under the
|
|
8984
|
+
* current key or any key a rotation retired, and compare a stored ciphertext to a known secret.
|
|
8957
8985
|
*
|
|
8958
|
-
* @
|
|
8959
|
-
|
|
8960
|
-
|
|
8986
|
+
* @layer Utility
|
|
8987
|
+
*/
|
|
8988
|
+
/** The keys the secret cipher works with: the one in force and those a rotation retired. */
|
|
8989
|
+
interface MfaSecretKeys {
|
|
8990
|
+
readonly encryptionKey: string;
|
|
8991
|
+
readonly previousEncryptionKeys?: readonly string[];
|
|
8992
|
+
}
|
|
8993
|
+
|
|
8994
|
+
/**
|
|
8995
|
+
* @fileoverview The MFA record's moving parts: the serialized read-modify-write every transition
|
|
8996
|
+
* goes through, the single-use claim on a recovery code, TOTP anti-replay, the re-encryption of a
|
|
8997
|
+
* secret under the current key, and the account read behind the MFA flows.
|
|
8961
8998
|
*
|
|
8962
8999
|
* @layer Service
|
|
8963
9000
|
*/
|
|
8964
|
-
|
|
9001
|
+
|
|
9002
|
+
/**
|
|
9003
|
+
* The three fields every MFA transition rewrites together.
|
|
9004
|
+
*
|
|
9005
|
+
* `mfaSecret` and `mfaRecoveryCodes` widen to `undefined` as well as `null` because the two
|
|
9006
|
+
* planes' record types differ there — the dashboard user nulls them, the platform admin leaves
|
|
9007
|
+
* them absent — and one transition point has to satisfy both.
|
|
9008
|
+
*/
|
|
9009
|
+
interface MfaRecordUpdate {
|
|
9010
|
+
mfaEnabled: boolean;
|
|
9011
|
+
mfaSecret: string | null | undefined;
|
|
9012
|
+
mfaRecoveryCodes: string[] | null | undefined;
|
|
9013
|
+
}
|
|
9014
|
+
/** Reads and writes the MFA state of one account, serializing every write per account and plane. */
|
|
9015
|
+
declare class MfaRecordService {
|
|
8965
9016
|
private readonly options;
|
|
8966
9017
|
private readonly userRepo;
|
|
8967
9018
|
private readonly platformUserRepo;
|
|
8968
9019
|
private readonly redis;
|
|
8969
|
-
private readonly tokenManager;
|
|
8970
|
-
private readonly bruteForce;
|
|
8971
|
-
private readonly passwordService;
|
|
8972
|
-
private readonly sessionService;
|
|
8973
|
-
private readonly emailProvider;
|
|
8974
|
-
private readonly hooks;
|
|
8975
9020
|
private readonly logger;
|
|
8976
|
-
constructor(options: ResolvedOptions, userRepo: IUserRepository, platformUserRepo: IPlatformUserRepository | null, redis: AuthRedisService
|
|
8977
|
-
/**
|
|
8978
|
-
* Returns the resolved MFA configuration. `options.mfa` is always present when `MfaService` is registered.
|
|
8979
|
-
*
|
|
8980
|
-
* `options.mfa` is always present when `MfaService` is registered — the module
|
|
8981
|
-
* only registers the service when `mfa` is configured. The single suppression
|
|
8982
|
-
* here eliminates repetitive `!` assertions throughout the class.
|
|
8983
|
-
*/
|
|
8984
|
-
private get mfaOptions();
|
|
9021
|
+
constructor(options: ResolvedOptions, userRepo: IUserRepository, platformUserRepo: IPlatformUserRepository | null, redis: AuthRedisService);
|
|
8985
9022
|
/**
|
|
8986
9023
|
* Store the TOTP secret re-encrypted under the current key.
|
|
8987
9024
|
*
|
|
@@ -8999,8 +9036,9 @@ declare class MfaService {
|
|
|
8999
9036
|
* @param context - Which identity plane the account belongs to.
|
|
9000
9037
|
* @param secretBase32 - The decrypted secret.
|
|
9001
9038
|
* @param tenantId - The tenant the account belongs to, so the write is scoped like the read.
|
|
9039
|
+
* @param keys - The secret-cipher keys; the secret is written under the current one.
|
|
9002
9040
|
*/
|
|
9003
|
-
|
|
9041
|
+
reencryptSecret(userId: string, context: 'dashboard' | 'platform', secretBase32: string, tenantId: string | undefined, keys: MfaSecretKeys): Promise<void>;
|
|
9004
9042
|
/**
|
|
9005
9043
|
* Performs one MFA state transition as a serialized read-modify-write.
|
|
9006
9044
|
*
|
|
@@ -9038,7 +9076,7 @@ declare class MfaService {
|
|
|
9038
9076
|
* @returns `true` when a write happened, `false` when `mutate` abandoned the transition.
|
|
9039
9077
|
* @throws {@link AuthException} `MFA_STATE_CONFLICT` when another transition holds the lock.
|
|
9040
9078
|
*/
|
|
9041
|
-
|
|
9079
|
+
transition(context: 'dashboard' | 'platform', userId: string, tenantId: string | undefined, mutate: (current: AuthUser | AuthPlatformUser) => MfaRecordUpdate | null): Promise<boolean>;
|
|
9042
9080
|
/**
|
|
9043
9081
|
* Claims a matched recovery code for exactly one challenge.
|
|
9044
9082
|
*
|
|
@@ -9065,7 +9103,149 @@ declare class MfaService {
|
|
|
9065
9103
|
* @param code - The submitted code, never stored — only its keyed MAC becomes a key.
|
|
9066
9104
|
* @returns `true` when this challenge is the one that claimed it.
|
|
9067
9105
|
*/
|
|
9068
|
-
|
|
9106
|
+
claimRecoveryCode(context: 'dashboard' | 'platform', userId: string, code: string, tenantId: string | undefined): Promise<boolean>;
|
|
9107
|
+
/**
|
|
9108
|
+
* Verifies a TOTP code and enforces anti-replay within the validation window.
|
|
9109
|
+
*
|
|
9110
|
+
* Stores a Redis key `tu:{hmac}` where `hmac = hmacSha256("{userSubject}:{code}", hmacKey)` —
|
|
9111
|
+
* so the marker is scoped to the plane and, on the dashboard plane, to the tenant. A second
|
|
9112
|
+
* submission of the same code within the TTL is rejected as a replay. Binding the marker to
|
|
9113
|
+
* both the subject and the code value prevents code disclosure in Redis and cross-user,
|
|
9114
|
+
* cross-tenant and cross-plane replay alike.
|
|
9115
|
+
*
|
|
9116
|
+
* A second marker under the plane-only preimage `{context}:{userId}:{code}` is written and
|
|
9117
|
+
* required alongside it; see the call site for why both must be fresh while pods of two
|
|
9118
|
+
* versions are live.
|
|
9119
|
+
*
|
|
9120
|
+
* @returns `true` if the code is valid and has not been replayed, `false` otherwise.
|
|
9121
|
+
*/
|
|
9122
|
+
verifyTotpWithAntiReplay(context: 'dashboard' | 'platform', userId: string, secretBase32: string, code: string, window: number, tenantId: string | undefined): Promise<boolean>;
|
|
9123
|
+
/**
|
|
9124
|
+
* Fetches a user from the correct repository based on the MFA context, and refuses one whose
|
|
9125
|
+
* account is blocked.
|
|
9126
|
+
*
|
|
9127
|
+
* The status gate lives here rather than in each caller because every entry point that
|
|
9128
|
+
* reaches this method changes or spends an authentication factor, and every one of them
|
|
9129
|
+
* must refuse a suspended or banned account. A gate in `challenge` alone would leave `setup`,
|
|
9130
|
+
* `verifyAndEnable`, `disable` and `regenerateRecoveryCodes` open: an operator who suspended a
|
|
9131
|
+
* compromised account would buy nothing against an attacker still holding an unexpired access
|
|
9132
|
+
* token, who could turn the second factor off — or enrol their own authenticator over it — for
|
|
9133
|
+
* the token's remaining lifetime. Nothing else covers that window: no status change bumps the
|
|
9134
|
+
* token epoch, so the per-request check is the only defence, and neither MFA controller
|
|
9135
|
+
* composes `UserStatusGuard` (the platform plane has no status guard at all). Every other
|
|
9136
|
+
* authority-bearing route in the library does gate on status; changing an authentication
|
|
9137
|
+
* factor is at least as privileged as minting an invitation.
|
|
9138
|
+
*
|
|
9139
|
+
* Gating the fetch rather than the callers is deliberate: a method added later inherits the
|
|
9140
|
+
* check instead of having to remember it.
|
|
9141
|
+
*
|
|
9142
|
+
* @param context - Which identity plane the caller is acting on.
|
|
9143
|
+
* @param userId - The subject taken from the verified token.
|
|
9144
|
+
* @param tenantId - The tenant the caller was authenticated in, on the dashboard plane;
|
|
9145
|
+
* absent on the platform plane, whose admins belong to none.
|
|
9146
|
+
* @returns The account record, guaranteed to be in good standing.
|
|
9147
|
+
* @throws `TOKEN_INVALID` if the user is not found.
|
|
9148
|
+
* @throws {@link AuthException} the status error when the account is blocked.
|
|
9149
|
+
*/
|
|
9150
|
+
loadAccount(context: 'dashboard' | 'platform', userId: string, tenantId?: string): Promise<AuthUser | AuthPlatformUser>;
|
|
9151
|
+
}
|
|
9152
|
+
|
|
9153
|
+
/**
|
|
9154
|
+
* @fileoverview Re-proving who is at the keyboard before a sensitive change: the account password,
|
|
9155
|
+
* counted like a login, or for an account with no password a recent real authentication.
|
|
9156
|
+
*
|
|
9157
|
+
* @layer Service
|
|
9158
|
+
*/
|
|
9159
|
+
|
|
9160
|
+
/** Re-authentication for the callers that change a credential or a membership. */
|
|
9161
|
+
declare class ReauthenticationService {
|
|
9162
|
+
private readonly options;
|
|
9163
|
+
private readonly redis;
|
|
9164
|
+
private readonly passwordService;
|
|
9165
|
+
private readonly lockout;
|
|
9166
|
+
private readonly logger;
|
|
9167
|
+
constructor(options: ResolvedOptions, redis: AuthRedisService, passwordService: PasswordService, lockout: MfaFlowLockoutService);
|
|
9168
|
+
/**
|
|
9169
|
+
* Requires the caller to re-prove the account password before a factor is changed.
|
|
9170
|
+
*
|
|
9171
|
+
* Counted like a login, and for the same reason. `login` refuses an account after N wrong
|
|
9172
|
+
* passwords, and this door asks for the same secret, so it carries the same lockout: without
|
|
9173
|
+
* one, a caller holding a stolen access token but not the password could guess it here
|
|
9174
|
+
* indefinitely, bounded only by the per-route IP limit, which a distributed caller sidesteps.
|
|
9175
|
+
* Winning the guess buys the whole account: enrol a factor, change the password, move the
|
|
9176
|
+
* address.
|
|
9177
|
+
*
|
|
9178
|
+
* The identifier is namespaced by flow and by plane. Sharing one counter with `login` would
|
|
9179
|
+
* let an authenticated caller lock the owner out of their own sign-in, and sharing it across
|
|
9180
|
+
* planes would let a dashboard user and a platform admin holding the same id exhaust each
|
|
9181
|
+
* other's budget — the same two reasons the challenge and disable counters are split.
|
|
9182
|
+
*
|
|
9183
|
+
* @param context - The authentication plane, part of the counter's key.
|
|
9184
|
+
* @param userId - The account being changed, part of the counter's key.
|
|
9185
|
+
* @param passwordHash - The account's stored hash, or `null` for an OAuth-only account.
|
|
9186
|
+
* @param password - The password the caller submitted, if any.
|
|
9187
|
+
* @throws {@link AuthException} `ACCOUNT_LOCKED` once the failure budget for this flow is
|
|
9188
|
+
* spent, or `INVALID_CREDENTIALS` when the account has a password and the submitted one is
|
|
9189
|
+
* absent or wrong. The latter is deliberately the same code a failed login returns: an
|
|
9190
|
+
* attacker holding a stolen token learns nothing from it that a failed login does not tell them.
|
|
9191
|
+
*/
|
|
9192
|
+
assertReauthenticated(context: MfaFlowPlane, userId: string, passwordHash: string | null, password: string | undefined, tenantId: string | undefined): Promise<void>;
|
|
9193
|
+
}
|
|
9194
|
+
|
|
9195
|
+
/**
|
|
9196
|
+
* Result returned by {@link MfaService.setup} containing the TOTP secret,
|
|
9197
|
+
* a QR code URI for authenticator apps, and the one-time plain-text recovery codes.
|
|
9198
|
+
*
|
|
9199
|
+
* @remarks
|
|
9200
|
+
* `recoveryCodes` must be displayed to the user **once** at setup time and never
|
|
9201
|
+
* stored in plain text — the service stores a keyed HMAC-SHA-256 of each one.
|
|
9202
|
+
* `secret` is provided for manual entry in authenticator apps that cannot scan a QR code.
|
|
9203
|
+
*/
|
|
9204
|
+
interface MfaSetupResult {
|
|
9205
|
+
/** Base32-encoded TOTP secret for manual entry in authenticator apps. */
|
|
9206
|
+
secret: string;
|
|
9207
|
+
/** `otpauth://totp/` URI for QR code generation. */
|
|
9208
|
+
qrCodeUri: string;
|
|
9209
|
+
/** Plain-text recovery codes. Display once and never persist in this form. */
|
|
9210
|
+
recoveryCodes: string[];
|
|
9211
|
+
}
|
|
9212
|
+
/**
|
|
9213
|
+
* Manages TOTP-based multi-factor authentication lifecycle.
|
|
9214
|
+
*
|
|
9215
|
+
* Handles the complete MFA flow for both dashboard users and platform admins:
|
|
9216
|
+
* - **Setup**: generate TOTP secret + recovery codes, store temporarily in Redis
|
|
9217
|
+
* - **Verify & Enable**: confirm first TOTP code, persist encrypted secret to DB
|
|
9218
|
+
* - **Challenge**: exchange a valid MFA temp token + code for full access tokens
|
|
9219
|
+
* - **Disable**: require a current TOTP code to disable MFA
|
|
9220
|
+
*
|
|
9221
|
+
* @remarks
|
|
9222
|
+
* This service is only registered when `options.mfa` is configured in
|
|
9223
|
+
* `BymaxAuthModule.registerAsync()`. All crypto operations use `node:crypto` only.
|
|
9224
|
+
*
|
|
9225
|
+
* @layer Service
|
|
9226
|
+
*/
|
|
9227
|
+
declare class MfaService {
|
|
9228
|
+
private readonly options;
|
|
9229
|
+
private readonly userRepo;
|
|
9230
|
+
private readonly platformUserRepo;
|
|
9231
|
+
private readonly redis;
|
|
9232
|
+
private readonly tokenManager;
|
|
9233
|
+
private readonly records;
|
|
9234
|
+
private readonly flowLockout;
|
|
9235
|
+
private readonly reauthentication;
|
|
9236
|
+
private readonly sessionService;
|
|
9237
|
+
private readonly emailProvider;
|
|
9238
|
+
private readonly hooks;
|
|
9239
|
+
private readonly logger;
|
|
9240
|
+
constructor(options: ResolvedOptions, userRepo: IUserRepository, platformUserRepo: IPlatformUserRepository | null, redis: AuthRedisService, tokenManager: TokenManagerService, records: MfaRecordService, flowLockout: MfaFlowLockoutService, reauthentication: ReauthenticationService, sessionService: SessionService, emailProvider: IEmailProvider, hooks: IAuthHooks);
|
|
9241
|
+
/**
|
|
9242
|
+
* Returns the resolved MFA configuration. `options.mfa` is always present when `MfaService` is registered.
|
|
9243
|
+
*
|
|
9244
|
+
* `options.mfa` is always present when `MfaService` is registered — the module
|
|
9245
|
+
* only registers the service when `mfa` is configured. The single suppression
|
|
9246
|
+
* here eliminates repetitive `!` assertions throughout the class.
|
|
9247
|
+
*/
|
|
9248
|
+
private get mfaOptions();
|
|
9069
9249
|
/**
|
|
9070
9250
|
* Initiates the MFA setup flow for a dashboard user or platform administrator.
|
|
9071
9251
|
*
|
|
@@ -9251,107 +9431,6 @@ declare class MfaService {
|
|
|
9251
9431
|
regenerateRecoveryCodes(userId: string, totpCode: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<{
|
|
9252
9432
|
recoveryCodes: string[];
|
|
9253
9433
|
}>;
|
|
9254
|
-
/**
|
|
9255
|
-
* Verifies a TOTP code and enforces anti-replay within the validation window.
|
|
9256
|
-
*
|
|
9257
|
-
* Stores a Redis key `tu:{hmac}` where `hmac = hmacSha256("{userSubject}:{code}", hmacKey)` —
|
|
9258
|
-
* so the marker is scoped to the plane and, on the dashboard plane, to the tenant. A second
|
|
9259
|
-
* submission of the same code within the TTL is rejected as a replay. Binding the marker to
|
|
9260
|
-
* both the subject and the code value prevents code disclosure in Redis and cross-user,
|
|
9261
|
-
* cross-tenant and cross-plane replay alike.
|
|
9262
|
-
*
|
|
9263
|
-
* A second marker under the pre-scoping preimage `{context}:{userId}:{code}` is written and
|
|
9264
|
-
* required alongside it; see the call site for why both must be fresh during a rolling upgrade.
|
|
9265
|
-
*
|
|
9266
|
-
* @returns `true` if the code is valid and has not been replayed, `false` otherwise.
|
|
9267
|
-
*/
|
|
9268
|
-
private verifyTotpWithAntiReplay;
|
|
9269
|
-
/**
|
|
9270
|
-
* Requires the caller to re-prove the account password before a factor is changed.
|
|
9271
|
-
*
|
|
9272
|
-
* Counted like a login, and for the same reason. `login` refuses an account after N wrong
|
|
9273
|
-
* passwords; this door asks for the same secret and used to refuse nothing, so a caller
|
|
9274
|
-
* holding a stolen access token but not the password could guess it here indefinitely. The
|
|
9275
|
-
* only control left was the per-route IP limit, which a distributed caller sidesteps — and
|
|
9276
|
-
* winning the guess buys the whole account: enrol a factor, change the password, move the
|
|
9277
|
-
* address. A door that takes the password has to carry the password's lockout.
|
|
9278
|
-
*
|
|
9279
|
-
* The identifier is namespaced by flow and by plane. Sharing one counter with `login` would
|
|
9280
|
-
* let an authenticated caller lock the owner out of their own sign-in, and sharing it across
|
|
9281
|
-
* planes would let a dashboard user and a platform admin holding the same id exhaust each
|
|
9282
|
-
* other's budget — the same two reasons the challenge and disable counters are split.
|
|
9283
|
-
*
|
|
9284
|
-
* @param context - The authentication plane, part of the counter's key.
|
|
9285
|
-
* @param userId - The account being changed, part of the counter's key.
|
|
9286
|
-
* @param passwordHash - The account's stored hash, or `null` for an OAuth-only account.
|
|
9287
|
-
* @param password - The password the caller submitted, if any.
|
|
9288
|
-
* @throws {@link AuthException} `ACCOUNT_LOCKED` once the failure budget for this flow is
|
|
9289
|
-
* spent, or `INVALID_CREDENTIALS` when the account has a password and the submitted one is
|
|
9290
|
-
* absent or wrong. The latter is deliberately the same code a failed login returns: an
|
|
9291
|
-
* attacker holding a stolen token learns nothing from it beyond what they already knew.
|
|
9292
|
-
*/
|
|
9293
|
-
private assertReauthenticated;
|
|
9294
|
-
/**
|
|
9295
|
-
* Asserts a dashboard-plane call carries a tenant, narrowing it to `string` for the caller.
|
|
9296
|
-
*
|
|
9297
|
-
* This is the dashboard half of {@link assertPlaneTenant}, split out for one reason: an
|
|
9298
|
-
* assertion signature is the only way to state "past this point the tenant is present" to the
|
|
9299
|
-
* type system. `IUserRepository.findById` requires the tenant, and the account read behind the
|
|
9300
|
-
* MFA flows sits under a `context === 'dashboard'` branch where a `void` guard run earlier in
|
|
9301
|
-
* the caller has narrowed nothing. Without this the branch had to pass `string | undefined`
|
|
9302
|
-
* into a `string` parameter, which is precisely the tenant-blind read the guard exists to stop.
|
|
9303
|
-
*
|
|
9304
|
-
* @param tenantId - The tenant supplied by the caller, if any.
|
|
9305
|
-
* @throws {@link AuthException} `VALIDATION` (400) when it is missing or blank.
|
|
9306
|
-
*/
|
|
9307
|
-
private assertDashboardTenant;
|
|
9308
|
-
/**
|
|
9309
|
-
* A dashboard MFA flow MUST carry the tenant it was authenticated in; a platform flow MUST NOT.
|
|
9310
|
-
*
|
|
9311
|
-
* The controller sources the tenant from the verified JWT, which always carries a validated
|
|
9312
|
-
* `tenantId`, so this never trips through the HTTP surface. It guards the OTHER caller:
|
|
9313
|
-
* `MfaService` is a public API, and a host wiring it directly — or a future SSO path — could hand
|
|
9314
|
-
* a dashboard `userId` with no tenant, which would silently reach the tenant-blind account read
|
|
9315
|
-
* and key derivation. Refuse it here, naming the field, rather than let it degrade to the
|
|
9316
|
-
* pre-tenant behaviour, which is exactly the fallback an attacker would aim for.
|
|
9317
|
-
*
|
|
9318
|
-
* @param context - The identity plane the call is on.
|
|
9319
|
-
* @param tenantId - The tenant supplied by the caller, if any.
|
|
9320
|
-
* @throws {@link AuthException} `VALIDATION` (400) when a dashboard call omits the tenant, or a
|
|
9321
|
-
* platform call supplies one.
|
|
9322
|
-
*/
|
|
9323
|
-
private assertPlaneTenant;
|
|
9324
|
-
/**
|
|
9325
|
-
* The tenant-scoped brute-force identifier for one MFA flow, and the legacy plane-only one it
|
|
9326
|
-
* replaces — equal on the platform plane, where the subject never carried a tenant, and distinct
|
|
9327
|
-
* on the dashboard plane, where it now does.
|
|
9328
|
-
*
|
|
9329
|
-
* A rolling upgrade runs old and new code against one Redis at once: on the dashboard plane an
|
|
9330
|
-
* old pod counts a failure under `{flow}:{plane}:{userId}` while a new pod counts it under
|
|
9331
|
-
* `{flow}:{userSubject}`, and either alone leaves a hole — a lockout an old pod filled would not
|
|
9332
|
-
* stop a new pod, and a success on one would not clear the other. For one release every read
|
|
9333
|
-
* consults both and every write touches both, so the two move as one; a later release drops the
|
|
9334
|
-
* legacy arm. On the platform plane the two ids are identical (no tenant ever entered the
|
|
9335
|
-
* subject), so the legacy arm is skipped — operating it would just be the same key a second time,
|
|
9336
|
-
* double-counting a failure and locking a platform admin out at half the threshold. `{flow}`
|
|
9337
|
-
* (`challenge` / `disable` / `reauth`) keeps the three counters isolated, exactly as before.
|
|
9338
|
-
*/
|
|
9339
|
-
private mfaFlowCounterIds;
|
|
9340
|
-
/**
|
|
9341
|
-
* Locked out when EITHER the legacy or the tenant-scoped counter has reached the threshold, so
|
|
9342
|
-
* neither a lockout an old pod recorded nor one a new pod recorded can be bypassed mid-migration.
|
|
9343
|
-
*/
|
|
9344
|
-
private isMfaFlowLockedOut;
|
|
9345
|
-
/**
|
|
9346
|
-
* Records the failure under both counters so the budget an attacker spends is seen by old and
|
|
9347
|
-
* new code alike — but under the scoped id ONCE when the two coincide (platform plane).
|
|
9348
|
-
*/
|
|
9349
|
-
private recordMfaFlowFailure;
|
|
9350
|
-
/**
|
|
9351
|
-
* Clears both counters on success — resetting only one would leave the other to lock a
|
|
9352
|
-
* legitimately authenticated user out on the next attempt — collapsing to one when they coincide.
|
|
9353
|
-
*/
|
|
9354
|
-
private resetMfaFlowFailures;
|
|
9355
9434
|
/**
|
|
9356
9435
|
* Sends an MFA state-change notice, fire-and-forget, with the recipient kept out of the log.
|
|
9357
9436
|
*
|
|
@@ -9376,35 +9455,6 @@ declare class MfaService {
|
|
|
9376
9455
|
* @param send - Which notice to dispatch.
|
|
9377
9456
|
*/
|
|
9378
9457
|
private notify;
|
|
9379
|
-
/**
|
|
9380
|
-
* Fetches a user from the correct repository based on the MFA context, and refuses one whose
|
|
9381
|
-
* account is blocked.
|
|
9382
|
-
*
|
|
9383
|
-
* The status gate lives here rather than in each caller because every entry point that
|
|
9384
|
-
* reaches this method changes or spends an authentication factor, and every one of them
|
|
9385
|
-
* must refuse a suspended or banned account. It used to live in `challenge` alone, so
|
|
9386
|
-
* `setup`, `verifyAndEnable`, `disable` and `regenerateRecoveryCodes` had no gate at all:
|
|
9387
|
-
* an operator who suspended a compromised account bought nothing against an attacker still
|
|
9388
|
-
* holding an unexpired access token, who could turn the second factor off — or enrol their
|
|
9389
|
-
* own authenticator over it — for the token's remaining lifetime. Nothing else covers that
|
|
9390
|
-
* window: no status change bumps the token epoch, so the per-request check is the only
|
|
9391
|
-
* defence, and neither MFA controller composes `UserStatusGuard` (the platform plane has no
|
|
9392
|
-
* status guard at all). Every other authority-bearing route in the library does gate on
|
|
9393
|
-
* status; changing an authentication factor is at least as privileged as minting an
|
|
9394
|
-
* invitation.
|
|
9395
|
-
*
|
|
9396
|
-
* Gating the fetch rather than the callers is deliberate: a method added later inherits the
|
|
9397
|
-
* check instead of having to remember it.
|
|
9398
|
-
*
|
|
9399
|
-
* @param context - Which identity plane the caller is acting on.
|
|
9400
|
-
* @param userId - The subject taken from the verified token.
|
|
9401
|
-
* @param tenantId - The tenant the caller was authenticated in, on the dashboard plane;
|
|
9402
|
-
* absent on the platform plane, whose admins belong to none.
|
|
9403
|
-
* @returns The account record, guaranteed to be in good standing.
|
|
9404
|
-
* @throws `TOKEN_INVALID` if the user is not found.
|
|
9405
|
-
* @throws {@link AuthException} the status error when the account is blocked.
|
|
9406
|
-
*/
|
|
9407
|
-
private fetchUserForContext;
|
|
9408
9458
|
}
|
|
9409
9459
|
|
|
9410
9460
|
/**
|
|
@@ -10787,6 +10837,28 @@ interface IApiKeyRepository {
|
|
|
10787
10837
|
* @returns `false` when no such key exists.
|
|
10788
10838
|
*/
|
|
10789
10839
|
setExpiry(id: string, at: Date): Promise<boolean>;
|
|
10840
|
+
/**
|
|
10841
|
+
* Lowers when a key stops verifying to `at`, never raising it: an atomic minimum.
|
|
10842
|
+
*
|
|
10843
|
+
* OPTIONAL, and the right way for rotation to retire a key. Rotation reads the key, decides a
|
|
10844
|
+
* deadline and writes it, and two rotations of one key interleave: with only {@link setExpiry}
|
|
10845
|
+
* a delayed rotation with a longer overlap overwrites the shorter deadline another one set, and
|
|
10846
|
+
* the old key keeps verifying past the shortest overlap anyone asked for. Implement this as one
|
|
10847
|
+
* statement so the comparison and the write cannot be separated:
|
|
10848
|
+
* `UPDATE ... SET expires_at = LEAST(COALESCE(expires_at, :at), :at) WHERE id = :id`, or an
|
|
10849
|
+
* update guarded by `WHERE expires_at IS NULL OR expires_at > :at`.
|
|
10850
|
+
*
|
|
10851
|
+
* When the repository implements it, rotation calls it with the end of the overlap and does not
|
|
10852
|
+
* compute a minimum itself. Without it, rotation falls back to reading the current expiry and
|
|
10853
|
+
* calling {@link setExpiry}, which is not safe against concurrent rotations of the same key: a
|
|
10854
|
+
* host that cannot implement this method must serialize rotation per key.
|
|
10855
|
+
*
|
|
10856
|
+
* @param id - The key to change.
|
|
10857
|
+
* @param at - The latest instant the key may keep verifying until.
|
|
10858
|
+
* @returns `false` when no such key exists. A key that already expires at or before `at` is left
|
|
10859
|
+
* unchanged and is not an error.
|
|
10860
|
+
*/
|
|
10861
|
+
limitExpiry?(id: string, at: Date): Promise<boolean>;
|
|
10790
10862
|
/**
|
|
10791
10863
|
* Records when keys were last used, in one batch.
|
|
10792
10864
|
*
|
|
@@ -11108,6 +11180,11 @@ declare class ApiKeyService {
|
|
|
11108
11180
|
* The new key is issued first and the old one retired second, so a failure between the two
|
|
11109
11181
|
* leaves both valid — the safe direction — rather than neither.
|
|
11110
11182
|
*
|
|
11183
|
+
* With an overlap the old key's deadline is lowered through the repository's optional
|
|
11184
|
+
* `limitExpiry`, an atomic minimum, so concurrent rotations of one key cannot push a shorter
|
|
11185
|
+
* deadline back. A repository without it gets a read followed by `setExpiry`, which two
|
|
11186
|
+
* rotations of the same key can interleave; such a host must serialize rotation per key.
|
|
11187
|
+
*
|
|
11111
11188
|
* @param id - The active key to replace.
|
|
11112
11189
|
* @param input - The overlap and an optional expiry for the new key.
|
|
11113
11190
|
* @returns The new key, plaintext once.
|
|
@@ -11121,5 +11198,5 @@ declare class ApiKeyService {
|
|
|
11121
11198
|
private validatedExpiry;
|
|
11122
11199
|
}
|
|
11123
11200
|
|
|
11124
|
-
export { API_KEY_MAX_LABEL_LENGTH, API_KEY_MAX_OVERLAP_SECONDS, AUTH_EMAIL_KINDS, AUTH_ERROR_CODES, AUTH_ERROR_MESSAGES, AUTH_ERROR_STATUS, AUTH_SECURITY_SCHEMES, AUTH_THROTTLE_CONFIGS, AcceptInvitationDto, AccountStatusService, AllowAllBreachChecker, ApiKeyGuard, ApiKeyScopes, ApiKeyService, AuthException, AuthExceptionFilter, AuthRateLimit, AuthRateLimitGuard, AuthRevocationService, AuthService, AuthTokenVerifierService, Authenticated, BYMAX_AUTH_API_KEY_CLOCK, BYMAX_AUTH_API_KEY_REPOSITORY, BYMAX_AUTH_BREACH_CHECKER, BYMAX_AUTH_EMAIL_PROVIDER, BYMAX_AUTH_HOOKS, BYMAX_AUTH_OPTIONS, BYMAX_AUTH_PLATFORM_USER_REPOSITORY, BYMAX_AUTH_REDIS_CLIENT, BYMAX_AUTH_USER_REPOSITORY, BruteForceService, BymaxAuthModule, ChangePasswordDto, CommonPasswordChecker, CreateInvitationDto, CurrentApiKey, CurrentUser, DefaultAuthEmailProvider, EmailChangeService, ForgotPasswordDto, HibpBreachChecker, IS_PUBLIC_KEY, InvitationService, JwtAuthGuard, JwtPlatformGuard, LoginDto, MfaChallengeDto, MfaDisableDto, MfaRegenerateRecoveryCodesDto, MfaRequiredGuard, MfaService, MfaVerifyDto, NoOpAuthHooks, NoOpEmailProvider, OAuthService, OptionalAuthGuard, OtpService, PLATFORM_ROLES_KEY, PasswordResetService, PlatformAuthService, PlatformLoginDto, PlatformRoles, PlatformRolesGuard, Public, ROLES_KEY, RegisterDto, RequestEncoding, RequestEncodingGuard, ResendOtpDto, ResendVerificationDto, ResetPasswordDto, Roles, RolesGuard, SKIP_MFA_KEY, SelfOrAdminGuard, ServiceKey, SessionService, SkipMfa, TokenDeliveryService, TrustedOriginGuard, UserStatusGuard, VerifyEmailDto, VerifyOtpDto, WS_TICKET_TTL_SECONDS, WsAuthExceptionFilter, WsJwtGuard, WsTicketService, authDocumentSecurity, createAuthValidationPipe, decrypt, describeChannelStatus, describeError, encrypt, generateSecureToken, hasRole, hmacSha256, redactSecrets, reduceToBaseWord, sanitizeHeaders, sha256, sleep, timingSafeCompare };
|
|
11201
|
+
export { API_KEY_MAX_LABEL_LENGTH, API_KEY_MAX_OVERLAP_SECONDS, AUTH_EMAIL_KINDS, AUTH_ERROR_CODES, AUTH_ERROR_MESSAGES, AUTH_ERROR_STATUS, AUTH_SECURITY_SCHEMES, AUTH_THROTTLE_CONFIGS, AcceptInvitationDto, AccountStatusService, AllowAllBreachChecker, ApiKeyGuard, ApiKeyScopes, ApiKeyService, AuthException, AuthExceptionFilter, AuthRateLimit, AuthRateLimitGuard, AuthRevocationService, AuthService, AuthTokenVerifierService, Authenticated, BYMAX_AUTH_API_KEY_CLOCK, BYMAX_AUTH_API_KEY_REPOSITORY, BYMAX_AUTH_BREACH_CHECKER, BYMAX_AUTH_EMAIL_PROVIDER, BYMAX_AUTH_HOOKS, BYMAX_AUTH_OPTIONS, BYMAX_AUTH_PLATFORM_USER_REPOSITORY, BYMAX_AUTH_REDIS_CLIENT, BYMAX_AUTH_USER_REPOSITORY, BruteForceService, BymaxAuthModule, ChangePasswordDto, CommonPasswordChecker, CreateInvitationDto, CurrentApiKey, CurrentUser, DefaultAuthEmailProvider, EmailChangeService, ForgotPasswordDto, HibpBreachChecker, IS_PUBLIC_KEY, InvitationService, JwtAuthGuard, JwtPlatformGuard, LoginDto, MfaChallengeDto, MfaDisableDto, MfaRegenerateRecoveryCodesDto, MfaRequiredGuard, MfaService, MfaVerifyDto, NoOpAuthHooks, NoOpEmailProvider, OAuthService, OptionalAuthGuard, OtpService, PLATFORM_ROLES_KEY, PasswordResetService, PlatformAuthService, PlatformLoginDto, PlatformRoles, PlatformRolesGuard, Public, ROLES_KEY, ReauthenticationService, RegisterDto, RequestEncoding, RequestEncodingGuard, ResendOtpDto, ResendVerificationDto, ResetPasswordDto, Roles, RolesGuard, SKIP_MFA_KEY, SelfOrAdminGuard, ServiceKey, SessionService, SkipMfa, TokenDeliveryService, TrustedOriginGuard, UserStatusGuard, VerifyEmailDto, VerifyOtpDto, WS_TICKET_TTL_SECONDS, WsAuthExceptionFilter, WsJwtGuard, WsTicketService, authDocumentSecurity, createAuthValidationPipe, decrypt, describeChannelStatus, describeError, encrypt, generateSecureToken, hasRole, hmacSha256, redactSecrets, reduceToBaseWord, sanitizeHeaders, sha256, sleep, timingSafeCompare };
|
|
11125
11202
|
export type { ApiKeyAccessEvent, ApiKeyClock, ApiKeyDenyReason, ApiKeyPrincipal, AuthApiKey, AuthApiKeySummary, AuthDocumentSecurityParams, AuthEmailCatalogue, AuthEmailContext, AuthEmailKind, AuthEmailMessage, AuthEmailSink, AuthErrorCode, AuthFieldError, AuthGuardFamily, AuthModuleAsyncOptions, AuthPlane, AuthPlatformUser, AuthRateLimitWindow, AuthResult, AuthUser, AuthenticatedRequest, BearerAuthResponse, BeforeRegisterResult, BothAuthResponse, BymaxAuthModuleOptions, BymaxAuthRateLimitOptions, ClientIpSource, CookieAuthResponse, CreateSessionParams, CreateUserData, CreateWithOAuthData, DashboardJwtPayload, DefaultAuthEmailProviderOptions, DeliveryErrorPolicy, DeliveryErrorPolicyMap, FindUserByEmailParams, FindUserByOAuthIdParams, HookContext, IApiKeyRepository, IAuthHooks, IEmailProvider, IPasswordBreachChecker, IPlatformUserRepository, IUserRepository, InviteData, IssueApiKeyInput, IssuedApiKey, LinkOAuthParams, ListSessionsParams, LiveSessionParams, MfaChallengeResult, MfaSetupResult, MfaTempPayload, OAuthLoginResult, OAuthMfaChallengeResult, OAuthProfile, OAuthProviderPlugin, OpenApiSecurityRequirement, PlatformAuthResult, PlatformAuthenticatedRequest, PlatformBearerAuthResponse, PlatformJwtPayload, RequestEncodingPolicy, ResolvedOptions, RevocableTokenPayload, RevokeAllExceptCurrentParams, RevokeAllSessionsParams, RevokeSessionParams, RotateApiKeyInput, RotatedTokenResult, SafeAuthPlatformUser, SafeAuthUser, SessionAlertInfo, SessionInfo, TenantResolverRequest, TenantScopedUserRef, UpdateEmailParams, UpdateEmailVerifiedParams, UpdateMfaData, UpdateMfaParams, UpdatePasswordParams, UpdatePlatformMfaData, UpdateStatusParams, VerifiedAccessToken, VerifyAccessTokenOptions, WsTicketSnapshot };
|