@bymax-one/nest-auth 1.4.21 → 1.4.22

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.
@@ -9150,6 +9150,340 @@ declare class MfaRecordService {
9150
9150
  loadAccount(context: 'dashboard' | 'platform', userId: string, tenantId?: string): Promise<AuthUser | AuthPlatformUser>;
9151
9151
  }
9152
9152
 
9153
+ /**
9154
+ * @fileoverview The MFA challenge: exchanging an MFA temp token and a TOTP or recovery code for
9155
+ * full session tokens, on either identity plane.
9156
+ *
9157
+ * @layer Service
9158
+ */
9159
+
9160
+ /** The resolved MFA configuration group, present whenever the MFA services are registered. */
9161
+ type MfaConfig = Required<NonNullable<ResolvedOptions['mfa']>>;
9162
+ /** Completes an MFA challenge: verifies the code, spends the temp token and issues the session. */
9163
+ declare class MfaChallengeService {
9164
+ private readonly options;
9165
+ private readonly userRepo;
9166
+ private readonly platformUserRepo;
9167
+ private readonly tokenManager;
9168
+ private readonly flowLockout;
9169
+ private readonly records;
9170
+ private readonly sessionService;
9171
+ private readonly hooks;
9172
+ private readonly logger;
9173
+ constructor(options: ResolvedOptions, userRepo: IUserRepository, platformUserRepo: IPlatformUserRepository | null, tokenManager: TokenManagerService, flowLockout: MfaFlowLockoutService, records: MfaRecordService, sessionService: SessionService, hooks: IAuthHooks);
9174
+ /**
9175
+ * Validates a TOTP or recovery code and exchanges the MFA temp token for full
9176
+ * access tokens.
9177
+ *
9178
+ * Handles both `'dashboard'` and `'platform'` contexts. Applies brute-force
9179
+ * protection per-user. TOTP codes include anti-replay protection via a short-lived
9180
+ * Redis key keyed on an HMAC of the code and user ID.
9181
+ *
9182
+ * The order is part of the contract: the temp token is verified but NOT consumed until the code
9183
+ * passes, so a mistyped digit is retryable (`MFA_INVALID_CODE`) rather than a dead end
9184
+ * (`MFA_TEMP_TOKEN_INVALID`); the lockout counter caps how many wrong codes one token can try;
9185
+ * and the consume must win before a session is issued.
9186
+ *
9187
+ * @param mfaTempToken - Short-lived MFA challenge JWT issued at login.
9188
+ * @param code - 6-digit TOTP code or `dddd-dddd-dddd` recovery code.
9189
+ * @param ip - Client IP address for session audit.
9190
+ * @param userAgent - User-Agent header for session description.
9191
+ * @param mfa - The resolved MFA configuration (cipher keys and TOTP window).
9192
+ * @returns `AuthResult` for dashboard context, `PlatformAuthResult` for platform context.
9193
+ * @throws `MFA_TEMP_TOKEN_INVALID` if the token is invalid or already consumed.
9194
+ * @throws `ACCOUNT_LOCKED` if the brute-force threshold has been reached.
9195
+ * @throws `MFA_INVALID_CODE` if the submitted code is incorrect.
9196
+ */
9197
+ challenge(mfaTempToken: string, code: string, ip: string, userAgent: string, mfa: MfaConfig): Promise<AuthResult | PlatformAuthResult>;
9198
+ /**
9199
+ * Refuses a challenge for an account at its failure ceiling.
9200
+ *
9201
+ * The 'challenge' counter is namespaced away from 'disable', so a pre-auth attacker holding only
9202
+ * a temp token cannot lock the user out of `disable()`, and away from the other identity plane,
9203
+ * whose id space may collide.
9204
+ *
9205
+ * @param scope - The plane, account and tenant the challenge belongs to.
9206
+ * @throws {@link AuthException} `ACCOUNT_LOCKED` at the ceiling.
9207
+ */
9208
+ private assertNotLocked;
9209
+ /**
9210
+ * Counts a wrong code and refuses it. The temp token stays alive: the user can retry under it
9211
+ * until the lockout surfaces `ACCOUNT_LOCKED`.
9212
+ *
9213
+ * @param scope - The plane, account and tenant the challenge belongs to.
9214
+ * @throws {@link AuthException} `MFA_INVALID_CODE`, always.
9215
+ */
9216
+ private refuseWrongCode;
9217
+ /**
9218
+ * Validates the submitted code against the account: a six-digit TOTP code with anti-replay, or a
9219
+ * recovery code compared in constant time and then claimed for exactly one challenge.
9220
+ *
9221
+ * @param user - The account, holding the stored recovery-code digests.
9222
+ * @param scope - The plane, account and tenant the challenge belongs to.
9223
+ * @param secretBase32 - The decrypted TOTP secret.
9224
+ * @param code - The submitted code.
9225
+ * @param mfa - The resolved MFA configuration.
9226
+ * @returns Whether the code is valid, and the index of the matched recovery digest (`-1` for
9227
+ * TOTP and for a recovery code that matched nothing).
9228
+ */
9229
+ private checkCode;
9230
+ /**
9231
+ * Spends the recovery code a challenge used, or migrates the secret to the current key.
9232
+ *
9233
+ * A recovery challenge removes the matched digest in a serialized transition, spliced against
9234
+ * the record as it stands INSIDE the lock rather than the copy read earlier: splicing the stale
9235
+ * copy would roll a concurrent `regenerateRecoveryCodes` back wholesale and undo a completed
9236
+ * `disable` — see `MfaRecordService.transition`. A TOTP challenge writes nothing on its own, so
9237
+ * a secret that opened under a retired key is re-encrypted by a detached write: the challenge
9238
+ * has already succeeded and the retired key still opens the secret, so a failure costs the
9239
+ * migration and nothing else.
9240
+ *
9241
+ * @param user - The account as read at the start of the challenge.
9242
+ * @param scope - The plane, account and tenant the challenge belongs to.
9243
+ * @param opened - The decrypted secret, and whether it opened under a retired key.
9244
+ * @param recoveryIndex - Index of the spent recovery digest, or `-1` after a TOTP challenge.
9245
+ * @param mfa - The resolved MFA configuration.
9246
+ */
9247
+ private settleRecovery;
9248
+ /**
9249
+ * Issues the dashboard session for a challenge that passed.
9250
+ *
9251
+ * @param user - The dashboard account.
9252
+ * @param userId - The account id from the temp token, for the hook context.
9253
+ * @param ip - Client IP address for session audit.
9254
+ * @param userAgent - User-Agent header for session description.
9255
+ * @returns The dashboard auth result.
9256
+ */
9257
+ private issueDashboardSession;
9258
+ /**
9259
+ * Issues the platform session for a challenge that passed, reusing the account already fetched
9260
+ * to avoid a TOCTOU double-read. The platform repository's presence was validated by
9261
+ * `MfaRecordService.loadAccount`, so the cast below is safe.
9262
+ *
9263
+ * @param platformUser - The platform administrator.
9264
+ * @param userId - The account id from the temp token, for the hook context.
9265
+ * @param ip - Client IP address for session audit.
9266
+ * @param userAgent - User-Agent header for session description.
9267
+ * @returns The platform auth result.
9268
+ */
9269
+ private issuePlatformSession;
9270
+ /**
9271
+ * The challenge for an entry point that serves the platform plane only.
9272
+ *
9273
+ * Reads the plane from the temp token first and refuses a dashboard token with
9274
+ * `PLATFORM_AUTH_REQUIRED` before anything is spent. {@link MfaChallengeService.challenge} consumes the
9275
+ * token, burns the TOTP code, stores a session, stamps the last login and fires the hooks, so
9276
+ * refusing after it leaves an orphan session nobody holds a token for. The first verification only
9277
+ * reads: it touches neither the brute-force counter nor the TOTP anti-replay key. The challenge
9278
+ * verifies the token again, and the plane is a signed claim, so both reads see the same one.
9279
+ *
9280
+ * @param mfaTempToken - Short-lived MFA challenge JWT issued at platform login.
9281
+ * @param code - 6-digit TOTP code or `dddd-dddd-dddd` recovery code.
9282
+ * @param ip - Client IP address for session audit.
9283
+ * @param userAgent - User-Agent header for session description.
9284
+ * @param mfa - The resolved MFA configuration (cipher keys and TOTP window).
9285
+ * @returns The platform auth result.
9286
+ * @throws `PLATFORM_AUTH_REQUIRED` if the token is for the dashboard plane.
9287
+ * @throws `MFA_TEMP_TOKEN_INVALID` if the token is invalid or already consumed.
9288
+ * @throws `ACCOUNT_LOCKED` if the brute-force threshold has been reached.
9289
+ * @throws `MFA_INVALID_CODE` if the submitted code is incorrect.
9290
+ */
9291
+ challengePlatformOnly(mfaTempToken: string, code: string, ip: string, userAgent: string, mfa: MfaConfig): Promise<PlatformAuthResult>;
9292
+ }
9293
+
9294
+ /**
9295
+ * @fileoverview Changing an MFA enrolment that already exists: turning it off with a TOTP code,
9296
+ * removing it administratively, and replacing its recovery codes.
9297
+ *
9298
+ * @layer Service
9299
+ */
9300
+
9301
+ /** Turns an enrolment off, removes it administratively, or replaces its recovery codes. */
9302
+ declare class MfaManagementService {
9303
+ private readonly options;
9304
+ private readonly platformUserRepo;
9305
+ private readonly redis;
9306
+ private readonly flowLockout;
9307
+ private readonly records;
9308
+ private readonly emailProvider;
9309
+ private readonly hooks;
9310
+ private readonly logger;
9311
+ constructor(options: ResolvedOptions, platformUserRepo: IPlatformUserRepository | null, redis: AuthRedisService, flowLockout: MfaFlowLockoutService, records: MfaRecordService, emailProvider: IEmailProvider, hooks: IAuthHooks);
9312
+ /**
9313
+ * Disables MFA on a user's account after verifying a current TOTP code.
9314
+ *
9315
+ * Only TOTP codes are accepted — recovery codes cannot disable MFA by design.
9316
+ * See {@link MfaDisableDto} for the rationale.
9317
+ *
9318
+ * Supports both dashboard users and platform administrators. The caller must
9319
+ * pass the correct `context` value derived from the authenticated JWT `type`
9320
+ * claim so the right repository is used.
9321
+ *
9322
+ * After disabling, all refresh sessions are atomically invalidated so that the
9323
+ * next rotation produces tokens with `mfaEnabled: false` and `mfaVerified: false`,
9324
+ * clearing any stale `mfaVerified: true` claims from previously issued access tokens.
9325
+ *
9326
+ * @param userId - Internal ID of the user disabling MFA.
9327
+ * @param code - 6-digit TOTP code from the authenticator app.
9328
+ * @param ip - Client IP address (forwarded to hooks).
9329
+ * @param userAgent - User-Agent header (forwarded to hooks).
9330
+ * @param context - Which repository to use: `'dashboard'` or `'platform'`.
9331
+ * @param tenantId - The tenant on the dashboard plane; absent on the platform plane.
9332
+ * @param mfa - The resolved MFA configuration (cipher keys and TOTP window).
9333
+ * @throws `TOKEN_INVALID` if the user is not found.
9334
+ * @throws `MFA_NOT_ENABLED` if MFA is not currently active.
9335
+ * @throws `ACCOUNT_LOCKED` if the brute-force threshold has been reached.
9336
+ * @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
9337
+ */
9338
+ disable(userId: string, code: string, ip: string, userAgent: string, context: 'dashboard' | 'platform', tenantId: string | undefined, mfa: MfaConfig): Promise<void>;
9339
+ /**
9340
+ * Removes a user's second factor without their TOTP code, for a support desk facing a user
9341
+ * who has lost their authenticator AND their recovery codes.
9342
+ *
9343
+ * Every self-service exit from MFA needs the factor itself: {@link disable} wants a valid
9344
+ * TOTP code, and the recovery codes want the codes. A user who has lost both is locked out
9345
+ * permanently, by the control that exists to protect them — ASVS v5 §6.1.1 asks for an
9346
+ * administrative path out for exactly that reason.
9347
+ *
9348
+ * **Authorising the caller is the consumer's job.** The library deliberately ships no route
9349
+ * for this, the same decision and for the same reason as {@link AuthService.unlockAccount}:
9350
+ * who may reset whom is a question only the host application can answer, and every route
9351
+ * this library ships is scoped to the caller's own account.
9352
+ *
9353
+ * Idempotent: resetting an account that has no second factor is a no-op, so a support desk
9354
+ * retrying does not get an error for a job already done.
9355
+ *
9356
+ * Three things happen beyond clearing the record, and none of them are optional:
9357
+ *
9358
+ * - **Sessions are invalidated and the token epoch is bumped**, so access tokens carrying
9359
+ * `mfaVerified: true` die with the factor rather than outliving it.
9360
+ * - **The user is notified**, through the same channel {@link disable} uses. An
9361
+ * administrative reset that the account holder cannot see is an account-takeover path:
9362
+ * an attacker who reaches the support desk removes the second factor silently. The
9363
+ * notification is what makes it an event the owner can detect and dispute.
9364
+ * - **It is logged** under its own prefix, so an administrative removal is distinguishable
9365
+ * from a user-initiated one in the library's own log.
9366
+ *
9367
+ * The `afterMfaDisabled` hook fires too, so consumer-side alerting keeps working. It is not
9368
+ * given a separate hook: the consumer is the one calling this method, so they already know
9369
+ * an administrative reset happened — the hook exists to tell them about the paths they do
9370
+ * not initiate.
9371
+ *
9372
+ * @param userId - Internal ID of the user whose second factor is being removed.
9373
+ * @param context - Which identity plane the user belongs to: `'dashboard'` or `'platform'`.
9374
+ * Must match the plane the account lives in, or the lookup misses.
9375
+ * @param tenantId - The tenant on the dashboard plane; absent on the platform plane.
9376
+ * @throws `TOKEN_INVALID` if no user with that ID exists in the given plane.
9377
+ */
9378
+ resetMfa(userId: string, context: 'dashboard' | 'platform', tenantId: string | undefined): Promise<void>;
9379
+ /**
9380
+ * Regenerates the user's MFA recovery codes after verifying a current TOTP code.
9381
+ *
9382
+ * Mirrors {@link disable} in its security posture: only TOTP codes are accepted
9383
+ * (recovery codes cannot rotate themselves by design), the brute-force counter
9384
+ * is checked, and the regeneration is fired against the correct repository for
9385
+ * the supplied `context`. The TOTP secret on the user record is unchanged — only
9386
+ * the recovery code list is replaced.
9387
+ *
9388
+ * The order is deliberate and differs from the challenge: the account is read and the enrolment
9389
+ * checked BEFORE the lockout. The challenge runs its brute-force check first because it is a
9390
+ * PUBLIC endpoint, where an attacker who knows a userId could otherwise force a database read
9391
+ * per request. This and `disable` sit behind a JWT guard that binds the caller to a userId, so
9392
+ * the counter exists to throttle repeated TOTP guesses by the legitimate user, or by someone
9393
+ * holding their access token, and a read per request from an authenticated session is
9394
+ * acceptable for that threat model. Do not reorder it to match the challenge.
9395
+ *
9396
+ * Returns the plain-text codes once. They are NOT persisted in plain form; only a
9397
+ * keyed HMAC-SHA-256 of each goes into the database. The caller is responsible for showing the
9398
+ * codes to the user exactly once and warning them to save them safely.
9399
+ *
9400
+ * @remarks
9401
+ * **Sessions are intentionally NOT invalidated after this call.** Unlike
9402
+ * {@link disable}, which flips `mfaEnabled` back to `false`, this method does not change
9403
+ * the verification credential — the TOTP secret stays the same and existing
9404
+ * `mfaVerified: true` access tokens remain valid. Invalidating sessions here
9405
+ * would force a redundant re-auth on every recovery-code rotation, which is a
9406
+ * routine hygiene action a user might perform several times over the lifetime
9407
+ * of an account.
9408
+ *
9409
+ * @param userId - Internal ID of the user rotating recovery codes.
9410
+ * @param totpCode - 6-digit TOTP code from the authenticator app.
9411
+ * @param ip - Client IP address (forwarded to hooks).
9412
+ * @param userAgent - User-Agent header (forwarded to hooks).
9413
+ * @param context - Which repository to use: `'dashboard'` or `'platform'`.
9414
+ * @param tenantId - The tenant on the dashboard plane; absent on the platform plane.
9415
+ * @param mfa - The resolved MFA configuration (cipher keys, TOTP window, code count).
9416
+ * @throws `TOKEN_INVALID` if the user is not found or `mfaSecret` is missing
9417
+ * when `mfaEnabled` is `true` (database inconsistency).
9418
+ * @throws `MFA_NOT_ENABLED` if MFA is not currently active, or if
9419
+ * `context === 'platform'` and the platform user repository was not
9420
+ * configured at module registration.
9421
+ * @throws `ACCOUNT_LOCKED` if the brute-force threshold has been reached.
9422
+ * @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
9423
+ * @returns The freshly generated plain-text recovery codes, one-time display.
9424
+ */
9425
+ regenerateRecoveryCodes(userId: string, totpCode: string, ip: string, userAgent: string, context: 'dashboard' | 'platform', tenantId: string | undefined, mfa: MfaConfig): Promise<{
9426
+ recoveryCodes: string[];
9427
+ }>;
9428
+ /**
9429
+ * Checks the lockout, then verifies a TOTP code for an authenticated MFA change, counting a
9430
+ * wrong code and clearing the counter on a right one.
9431
+ *
9432
+ * The 'disable' counter is namespaced away from 'challenge', so a pre-auth attacker holding only
9433
+ * a temp token cannot exhaust the threshold and block the authenticated user's own changes.
9434
+ *
9435
+ * @param origin - The calling flow, for the log lines.
9436
+ * @param scope - The plane, account and tenant the change applies to.
9437
+ * @param user - The account, holding the encrypted secret.
9438
+ * @param code - The submitted TOTP code.
9439
+ * @param mfa - The resolved MFA configuration.
9440
+ * @throws {@link AuthException} `ACCOUNT_LOCKED` at the ceiling, `TOKEN_INVALID` when the secret
9441
+ * is absent on an enabled account (a database inconsistency), `MFA_INVALID_CODE` on a wrong
9442
+ * code.
9443
+ */
9444
+ private spendTotpAttempt;
9445
+ /**
9446
+ * Clears the MFA record and revokes everything issued under it.
9447
+ *
9448
+ * Serialized against every other MFA transition, so a challenge that read the record a moment
9449
+ * earlier cannot splice `mfaEnabled: true` and the old secret back on top of this. Sessions are
9450
+ * invalidated and the token epoch advances so outstanding access tokens die with the factor: an
9451
+ * auth-state change revokes everything issued under the previous state, in both directions, the
9452
+ * same rule the password-reset flow applies. Scoped to the caller's own identity plane.
9453
+ *
9454
+ * @param scope - The plane, account and tenant to clear.
9455
+ */
9456
+ private clearFactor;
9457
+ /**
9458
+ * Tells the account holder their second factor was turned off.
9459
+ *
9460
+ * @param origin - The calling flow, for the log line.
9461
+ * @param scope - The plane, account and tenant that changed.
9462
+ * @param user - The account, read for its address.
9463
+ */
9464
+ private notifyDisabled;
9465
+ /**
9466
+ * Fires the `afterMfaDisabled` hook, fire-and-forget: its failure must not undo a completed
9467
+ * removal.
9468
+ *
9469
+ * @param scope - The plane, account and tenant that changed.
9470
+ * @param user - The account as read before the change.
9471
+ * @param ip - Client IP address, or empty for an administrative call.
9472
+ * @param userAgent - User-Agent header, or empty for an administrative call.
9473
+ */
9474
+ private fireDisabledHook;
9475
+ /**
9476
+ * Fires the `afterMfaRecoveryCodesRegenerated` hook, fire-and-forget: its failure must not undo
9477
+ * a completed regeneration.
9478
+ *
9479
+ * @param scope - The plane, account and tenant that changed.
9480
+ * @param user - The account as read before the change.
9481
+ * @param ip - Client IP address.
9482
+ * @param userAgent - User-Agent header.
9483
+ */
9484
+ private fireRegeneratedHook;
9485
+ }
9486
+
9153
9487
  /**
9154
9488
  * @fileoverview Re-proving who is at the keyboard before a sensitive change: the account password,
9155
9489
  * counted like a login, or for an account with no password a recent real authentication.
@@ -9231,13 +9565,15 @@ declare class MfaService {
9231
9565
  private readonly redis;
9232
9566
  private readonly tokenManager;
9233
9567
  private readonly records;
9568
+ private readonly challengeFlow;
9569
+ private readonly management;
9234
9570
  private readonly flowLockout;
9235
9571
  private readonly reauthentication;
9236
9572
  private readonly sessionService;
9237
9573
  private readonly emailProvider;
9238
9574
  private readonly hooks;
9239
9575
  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);
9576
+ constructor(options: ResolvedOptions, userRepo: IUserRepository, platformUserRepo: IPlatformUserRepository | null, redis: AuthRedisService, tokenManager: TokenManagerService, records: MfaRecordService, challengeFlow: MfaChallengeService, management: MfaManagementService, flowLockout: MfaFlowLockoutService, reauthentication: ReauthenticationService, sessionService: SessionService, emailProvider: IEmailProvider, hooks: IAuthHooks);
9241
9577
  /**
9242
9578
  * Returns the resolved MFA configuration. `options.mfa` is always present when `MfaService` is registered.
9243
9579
  *
@@ -9292,9 +9628,8 @@ declare class MfaService {
9292
9628
  * Validates a TOTP or recovery code and exchanges the MFA temp token for full
9293
9629
  * access tokens.
9294
9630
  *
9295
- * Handles both `'dashboard'` and `'platform'` contexts. Applies brute-force
9296
- * protection per-user. TOTP codes include anti-replay protection via a short-lived
9297
- * Redis key keyed on an HMAC of the code and user ID.
9631
+ * Handles both `'dashboard'` and `'platform'` contexts; the flow lives in
9632
+ * {@link MfaChallengeService.challenge}.
9298
9633
  *
9299
9634
  * @param mfaTempToken - Short-lived MFA challenge JWT issued at login.
9300
9635
  * @param code - 6-digit TOTP code or `dddd-dddd-dddd` recovery code.
@@ -9307,14 +9642,9 @@ declare class MfaService {
9307
9642
  */
9308
9643
  challenge(mfaTempToken: string, code: string, ip: string, userAgent: string): Promise<AuthResult | PlatformAuthResult>;
9309
9644
  /**
9310
- * The challenge for an entry point that serves the platform plane only.
9311
- *
9312
- * Reads the plane from the temp token first and refuses a dashboard token with
9313
- * `PLATFORM_AUTH_REQUIRED` before anything is spent. {@link MfaService.challenge} consumes the
9314
- * token, burns the TOTP code, stores a session, stamps the last login and fires the hooks, so
9315
- * refusing after it leaves an orphan session nobody holds a token for. The first verification only
9316
- * reads: it touches neither the brute-force counter nor the TOTP anti-replay key. The challenge
9317
- * verifies the token again, and the plane is a signed claim, so both reads see the same one.
9645
+ * The challenge for an entry point that serves the platform plane only: a dashboard token is
9646
+ * refused with `PLATFORM_AUTH_REQUIRED` before anything is spent. See
9647
+ * {@link MfaChallengeService.challengePlatformOnly}.
9318
9648
  *
9319
9649
  * @param mfaTempToken - Short-lived MFA challenge JWT issued at platform login.
9320
9650
  * @param code - 6-digit TOTP code or `dddd-dddd-dddd` recovery code.
@@ -9330,22 +9660,15 @@ declare class MfaService {
9330
9660
  /**
9331
9661
  * Disables MFA on a user's account after verifying a current TOTP code.
9332
9662
  *
9333
- * Only TOTP codes are accepted — recovery codes cannot disable MFA by design.
9334
- * See {@link MfaDisableDto} for the rationale.
9335
- *
9336
- * Supports both dashboard users and platform administrators. The caller must
9337
- * pass the correct `context` value derived from the authenticated JWT `type`
9338
- * claim so the right repository is used.
9339
- *
9340
- * After disabling, all refresh sessions are atomically invalidated so that the
9341
- * next rotation produces tokens with `mfaEnabled: false` and `mfaVerified: false`,
9342
- * clearing any stale `mfaVerified: true` claims from previously issued access tokens.
9663
+ * Only TOTP codes are accepted — recovery codes cannot disable MFA by design. The flow lives in
9664
+ * {@link MfaManagementService.disable}.
9343
9665
  *
9344
9666
  * @param userId - Internal ID of the user disabling MFA.
9345
9667
  * @param code - 6-digit TOTP code from the authenticator app.
9346
9668
  * @param ip - Client IP address (forwarded to hooks).
9347
9669
  * @param userAgent - User-Agent header (forwarded to hooks).
9348
9670
  * @param context - Which repository to use: `'dashboard'` (default) or `'platform'`.
9671
+ * @param tenantId - The tenant on the dashboard plane; absent on the platform plane.
9349
9672
  * @throws `TOKEN_INVALID` if the user is not found.
9350
9673
  * @throws `MFA_NOT_ENABLED` if MFA is not currently active.
9351
9674
  * @throws `ACCOUNT_LOCKED` if the brute-force threshold has been reached.
@@ -9353,72 +9676,30 @@ declare class MfaService {
9353
9676
  */
9354
9677
  disable(userId: string, code: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<void>;
9355
9678
  /**
9356
- * Removes a user's second factor without their TOTP code, for a support desk facing a user
9357
- * who has lost their authenticator AND their recovery codes.
9679
+ * Removes a user's second factor without their TOTP code, for a support desk facing a user who
9680
+ * has lost their authenticator AND their recovery codes.
9358
9681
  *
9359
- * Every self-service exit from MFA needs the factor itself: {@link disable} wants a valid
9360
- * TOTP code, and the recovery codes want the codes. A user who has lost both is locked out
9361
- * permanently, by the control that exists to protect them — ASVS v5 §6.1.1 asks for an
9362
- * administrative path out for exactly that reason.
9363
- *
9364
- * **Authorising the caller is the consumer's job.** The library deliberately ships no route
9365
- * for this, the same decision and for the same reason as {@link AuthService.unlockAccount}:
9366
- * who may reset whom is a question only the host application can answer, and all 39 routes
9367
- * this library does ship are scoped to the caller's own account.
9368
- *
9369
- * Idempotent: resetting an account that has no second factor is a no-op, so a support desk
9370
- * retrying does not get an error for a job already done.
9371
- *
9372
- * Three things happen beyond clearing the record, and none of them are optional:
9373
- *
9374
- * - **Sessions are invalidated and the token epoch is bumped**, so access tokens carrying
9375
- * `mfaVerified: true` die with the factor rather than outliving it.
9376
- * - **The user is notified**, through the same channel {@link disable} uses. An
9377
- * administrative reset that the account holder cannot see is an account-takeover path:
9378
- * an attacker who reaches the support desk removes the second factor silently. The
9379
- * notification is what makes it an event the owner can detect and dispute.
9380
- * - **It is logged** under its own prefix, so an administrative removal is distinguishable
9381
- * from a user-initiated one in the library's own log.
9382
- *
9383
- * The `afterMfaDisabled` hook fires too, so consumer-side alerting keeps working. It is not
9384
- * given a separate hook: the consumer is the one calling this method, so they already know
9385
- * an administrative reset happened — the hook exists to tell them about the paths they do
9386
- * not initiate.
9682
+ * Authorising the caller is the consumer's job: the library ships no route for this. The flow
9683
+ * lives in {@link MfaManagementService.resetMfa}. Idempotent.
9387
9684
  *
9388
9685
  * @param userId - Internal ID of the user whose second factor is being removed.
9389
9686
  * @param context - Which identity plane the user belongs to: `'dashboard'` (default) or
9390
9687
  * `'platform'`. Must match the plane the account lives in, or the lookup misses.
9688
+ * @param tenantId - The tenant on the dashboard plane; absent on the platform plane.
9391
9689
  * @throws `TOKEN_INVALID` if no user with that ID exists in the given plane.
9392
9690
  */
9393
9691
  resetMfa(userId: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<void>;
9394
9692
  /**
9395
- * Regenerates the user's MFA recovery codes after verifying a current TOTP code.
9396
- *
9397
- * Mirrors {@link disable} in its security posture: only TOTP codes are accepted
9398
- * (recovery codes cannot rotate themselves by design), the brute-force counter
9399
- * is checked, and the regeneration is fired against the correct repository for
9400
- * the supplied `context`. The TOTP secret on the user record is unchanged — only
9401
- * the recovery code list is replaced.
9402
- *
9403
- * Returns the plain-text codes once. They are NOT persisted in plain form; only a
9404
- * keyed HMAC-SHA-256 of each goes into the database. The caller is responsible for showing the
9405
- * codes to the user exactly once and warning them to save them safely.
9406
- *
9407
- * @remarks
9408
- * **Sessions are intentionally NOT invalidated after this call.** Unlike
9409
- * {@link verifyAndEnable} (which flips `mfaEnabled` from `false` to `true`) and
9410
- * {@link disable} (which flips it back to `false`), this method does not change
9411
- * the verification credential — the TOTP secret stays the same and existing
9412
- * `mfaVerified: true` access tokens remain valid. Invalidating sessions here
9413
- * would force a redundant re-auth on every recovery-code rotation, which is a
9414
- * routine hygiene action a user might perform several times over the lifetime
9415
- * of an account.
9693
+ * Regenerates the user's MFA recovery codes after verifying a current TOTP code. Sessions are
9694
+ * intentionally NOT invalidated: the TOTP secret is unchanged. The flow lives in
9695
+ * {@link MfaManagementService.regenerateRecoveryCodes}.
9416
9696
  *
9417
9697
  * @param userId - Internal ID of the user rotating recovery codes.
9418
9698
  * @param totpCode - 6-digit TOTP code from the authenticator app.
9419
9699
  * @param ip - Client IP address (forwarded to hooks).
9420
9700
  * @param userAgent - User-Agent header (forwarded to hooks).
9421
9701
  * @param context - Which repository to use: `'dashboard'` (default) or `'platform'`.
9702
+ * @param tenantId - The tenant on the dashboard plane; absent on the platform plane.
9422
9703
  * @throws `TOKEN_INVALID` if the user is not found or `mfaSecret` is missing
9423
9704
  * when `mfaEnabled` is `true` (database inconsistency).
9424
9705
  * @throws `MFA_NOT_ENABLED` if MFA is not currently active, or if
@@ -9431,30 +9712,6 @@ declare class MfaService {
9431
9712
  regenerateRecoveryCodes(userId: string, totpCode: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<{
9432
9713
  recoveryCodes: string[];
9433
9714
  }>;
9434
- /**
9435
- * Sends an MFA state-change notice, fire-and-forget, with the recipient kept out of the log.
9436
- *
9437
- * **Not awaited, and that is the fix, not a shortcut.** These three sends used to be awaited
9438
- * with no `catch`, so a rejected delivery travelled out of the service to `AuthExceptionFilter`
9439
- * — which answered the caller with an error for an operation that had ALREADY completed. By
9440
- * that point the secret is written, the sessions are invalidated and the token epoch is bumped;
9441
- * the user's second factor is on, and telling them it failed is how they end up locked out of
9442
- * an account they just secured. `PasswordResetService.notifyPasswordChanged` had reached the
9443
- * same conclusion for the same reason.
9444
- *
9445
- * **And the address does not reach the log.** The filter logged that error raw, and an SMTP
9446
- * rejection routinely NAMES the recipient it refused (`550 user@example.com: recipient
9447
- * rejected`) — no quoted body required, which makes it the likeliest exposure of the set.
9448
- * `describeChannelStatus` publishes nothing the channel wrote, and nothing parsed off it either,
9449
- * so no part of that name comes through. `withheld` covers the fields this line composes rather than the error, and
9450
- * `safeLogLine` checks the seam the template opens between them.
9451
- *
9452
- * @param origin - The calling flow, for the log line.
9453
- * @param userId - Whose account changed.
9454
- * @param user - The account, read for its tenant and address.
9455
- * @param send - Which notice to dispatch.
9456
- */
9457
- private notify;
9458
9715
  }
9459
9716
 
9460
9717
  /**