@bymax-one/nest-auth 1.3.1 → 1.3.2
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 +48 -1
- package/README.md +5 -3
- package/dist/server/index.cjs +1098 -336
- package/dist/server/index.d.cts +135 -11
- package/dist/server/index.d.ts +135 -11
- package/dist/server/index.mjs +1098 -336
- package/dist/shared/index.d.cts +8 -0
- package/dist/shared/index.d.ts +8 -0
- package/package.json +2 -2
package/dist/server/index.d.cts
CHANGED
|
@@ -213,10 +213,18 @@ interface IUserRepository {
|
|
|
213
213
|
/**
|
|
214
214
|
* Updates the user's TOTP MFA configuration.
|
|
215
215
|
*
|
|
216
|
+
* The tenant is required for the same reason {@link findById} takes one: the library may not
|
|
217
|
+
* assume ids are unique across tenants, so a write keyed by id alone can land on another tenant's
|
|
218
|
+
* row — or on a row other than the one the MFA flow read a moment earlier, which would leave a
|
|
219
|
+
* spent recovery code in the account's list. An implementation MUST scope the update by both the
|
|
220
|
+
* id and the tenant, exactly as its read does.
|
|
221
|
+
*
|
|
216
222
|
* @param id - The user's unique identifier.
|
|
223
|
+
* @param tenantId - The tenant the account belongs to; the update MUST be scoped to it, as
|
|
224
|
+
* {@link findById} scopes its read. Never omitted for a dashboard account.
|
|
217
225
|
* @param data - New MFA state. See {@link UpdateMfaData}.
|
|
218
226
|
*/
|
|
219
|
-
updateMfa(id: string, data: UpdateMfaData): Promise<void>;
|
|
227
|
+
updateMfa(id: string, tenantId: string | undefined, data: UpdateMfaData): Promise<void>;
|
|
220
228
|
/**
|
|
221
229
|
* Records the current timestamp as the user's last successful login time.
|
|
222
230
|
*
|
|
@@ -1100,6 +1108,25 @@ interface BymaxAuthModuleOptions {
|
|
|
1100
1108
|
* ```
|
|
1101
1109
|
*/
|
|
1102
1110
|
tenantIdResolver?: (req: Request) => string | Promise<string>;
|
|
1111
|
+
/**
|
|
1112
|
+
* Whether the dashboard guard re-resolves the request's tenant and rejects a token whose
|
|
1113
|
+
* `tenantId` claim does not match it.
|
|
1114
|
+
*
|
|
1115
|
+
* The resolver runs on the credential flows — register, login, OTP, OAuth — where it decides
|
|
1116
|
+
* which tenant an account is scoped to. It does **not** run on every guarded request: a valid
|
|
1117
|
+
* token is trusted for the tenant baked into it. That is safe on its own, because a token minted
|
|
1118
|
+
* for tenant A never carries tenant B, so there is no cross-tenant escalation to be had. What it
|
|
1119
|
+
* does leave open is a token being *presented* under the wrong host — accepted, and operating as
|
|
1120
|
+
* its own tenant rather than the host's. For a deployment where the host is the tenant boundary
|
|
1121
|
+
* (subdomain-per-tenant behind an edge that refuses a `Host` it does not serve), turning this on
|
|
1122
|
+
* closes that gap: the guard calls {@link tenantIdResolver} and refuses a mismatch.
|
|
1123
|
+
*
|
|
1124
|
+
* Off by default, and requires a resolver to have any effect: with no resolver there is nothing
|
|
1125
|
+
* to compare against. Leave it off where a token is legitimately used across hosts (a single API
|
|
1126
|
+
* origin serving many tenants by claim), because there the resolved host tenant and the token
|
|
1127
|
+
* tenant are *meant* to differ.
|
|
1128
|
+
*/
|
|
1129
|
+
enforceTenantBinding?: boolean;
|
|
1103
1130
|
/**
|
|
1104
1131
|
* Granular control over which controllers are registered.
|
|
1105
1132
|
* Allows disabling endpoints that are not needed for a specific application.
|
|
@@ -2985,6 +3012,24 @@ interface MfaTempPayload {
|
|
|
2985
3012
|
type: 'mfa_challenge';
|
|
2986
3013
|
/** Indicates which authentication context triggered the MFA challenge. */
|
|
2987
3014
|
context: 'dashboard' | 'platform';
|
|
3015
|
+
/**
|
|
3016
|
+
* The tenant the dashboard challenge belongs to. Present on the dashboard plane, ABSENT on the
|
|
3017
|
+
* platform plane — platform admins carry no tenant. It is the whole reason the challenge can
|
|
3018
|
+
* resolve the pending account tenant-scoped: without it the second-factor step, the
|
|
3019
|
+
* account-status gate and the finally-minted session all resolved the account by `sub` alone, so
|
|
3020
|
+
* under a host schema whose ids are per-tenant sequential a challenge could decide against, and
|
|
3021
|
+
* mint a session for, a homonym in another tenant.
|
|
3022
|
+
*
|
|
3023
|
+
* NOT optional in effect, despite the `?`: `verifyMfaTempToken` REJECTS a dashboard token that
|
|
3024
|
+
* lacks it and a platform token that carries it. The `?` only expresses that the two planes
|
|
3025
|
+
* populate it differently — it is never a value the caller may omit and have honoured. A missing
|
|
3026
|
+
* claim reads as an invalid token, not as a fall back to the pre-tenant lookup: a fallback would
|
|
3027
|
+
* leave the tenant-blind path reachable to anyone who simply drops the field (RFC 8725 §3.9/§3.12;
|
|
3028
|
+
* ASVS 6.6.2 — the out-of-band token must be bound to the request that originated it). A token
|
|
3029
|
+
* minted before this claim existed is therefore refused, and the user redoes login within the
|
|
3030
|
+
* five-minute TTL; there is no in-flight fallback to drain.
|
|
3031
|
+
*/
|
|
3032
|
+
tenantId?: string;
|
|
2988
3033
|
/**
|
|
2989
3034
|
* The subject's token **epoch** at issuance, in the plane named by `context`.
|
|
2990
3035
|
*
|
|
@@ -4910,9 +4955,10 @@ declare class TrustedOriginGuard implements CanActivate {
|
|
|
4910
4955
|
* Verifies that the authenticated user's account status is not blocked.
|
|
4911
4956
|
*
|
|
4912
4957
|
* On each request, the user's current status is resolved from a Redis cache
|
|
4913
|
-
* (`us:{userId}`) to avoid a database round-trip on every request. A cache
|
|
4914
|
-
* triggers a repository lookup, and the result is cached for
|
|
4915
|
-
* `userStatusCacheTtlSeconds` (default: 60 s).
|
|
4958
|
+
* (`us:{tenantId}:{userId}`) to avoid a database round-trip on every request. A cache
|
|
4959
|
+
* miss triggers a tenant-scoped repository lookup, and the result is cached for
|
|
4960
|
+
* `userStatusCacheTtlSeconds` (default: 60 s). The tenant is part of the key because a
|
|
4961
|
+
* repository id is unique only within a tenant.
|
|
4916
4962
|
*
|
|
4917
4963
|
* Status-specific errors allow the client to display the correct message:
|
|
4918
4964
|
* - `BANNED` → `ACCOUNT_BANNED` (403)
|
|
@@ -4920,6 +4966,15 @@ declare class TrustedOriginGuard implements CanActivate {
|
|
|
4920
4966
|
* - `SUSPENDED` → `ACCOUNT_SUSPENDED` (403)
|
|
4921
4967
|
* - `PENDING` → `PENDING_APPROVAL` (403)
|
|
4922
4968
|
*
|
|
4969
|
+
* When `emailVerification.required` is enabled (the default), this guard also
|
|
4970
|
+
* refuses an account whose email is not yet verified, with `EMAIL_NOT_VERIFIED`.
|
|
4971
|
+
* This is what makes verification a gate on API access and not only on `login`:
|
|
4972
|
+
* registration issues a session before verification, and without this check that
|
|
4973
|
+
* session would reach every route this guard protects for the access token's
|
|
4974
|
+
* lifetime. The verified flag is cached alongside the status under
|
|
4975
|
+
* `uev:{tenantId}:{userId}`, on the same miss and the same TTL, so the check costs no
|
|
4976
|
+
* extra round-trip.
|
|
4977
|
+
*
|
|
4923
4978
|
* Routes without an authenticated user (`request.user` absent) are passed
|
|
4924
4979
|
* through — this guard is designed to be composed after {@link JwtAuthGuard}.
|
|
4925
4980
|
*
|
|
@@ -5945,7 +6000,24 @@ declare class BruteForceService {
|
|
|
5945
6000
|
*/
|
|
5946
6001
|
declare class OtpService {
|
|
5947
6002
|
private readonly redis;
|
|
5948
|
-
|
|
6003
|
+
private readonly options;
|
|
6004
|
+
constructor(redis: AuthRedisService, options: ResolvedOptions);
|
|
6005
|
+
/**
|
|
6006
|
+
* Keyed one-way transform of an OTP under which it is stored and compared.
|
|
6007
|
+
*
|
|
6008
|
+
* The record is never the code itself: a six-digit code is a keyspace of a million, which a
|
|
6009
|
+
* plain digest lets anyone who reads Redis reverse instantly, so the transform is HMAC-SHA256
|
|
6010
|
+
* under the library's server-only `hmacKey` — the same key that already conceals the identifier.
|
|
6011
|
+
* It is bound to the identifier so the same code under two accounts does not collapse to one
|
|
6012
|
+
* value. `store` and `verify` compute it the same way; the byte-identical verify script keeps
|
|
6013
|
+
* comparing two opaque strings, so the rust-auth side stays in step by transforming the code the
|
|
6014
|
+
* same way before it reads or writes the shared record.
|
|
6015
|
+
*
|
|
6016
|
+
* @param identifier - The user-scoped identifier the record is keyed by.
|
|
6017
|
+
* @param code - The plaintext OTP.
|
|
6018
|
+
* @returns The hex HMAC written to, and compared against, the record.
|
|
6019
|
+
*/
|
|
6020
|
+
private fingerprint;
|
|
5949
6021
|
/**
|
|
5950
6022
|
* Generates a cryptographically secure numeric OTP string.
|
|
5951
6023
|
*
|
|
@@ -6739,9 +6811,12 @@ declare class TokenManagerService {
|
|
|
6739
6811
|
*
|
|
6740
6812
|
* @param userId - The user (or admin) pending MFA completion.
|
|
6741
6813
|
* @param context - Authentication context: `'dashboard'` or `'platform'`.
|
|
6814
|
+
* @param tenantId - The tenant the account belongs to, on the dashboard plane only. Stamped
|
|
6815
|
+
* into the token so the challenge resolves the account tenant-scoped rather than by `sub`
|
|
6816
|
+
* alone. Omitted on the platform plane, which has no tenant.
|
|
6742
6817
|
* @returns The signed MFA temp JWT.
|
|
6743
6818
|
*/
|
|
6744
|
-
issueMfaTempToken(userId: string, context: 'dashboard' | 'platform'): Promise<string>;
|
|
6819
|
+
issueMfaTempToken(userId: string, context: 'dashboard' | 'platform', tenantId?: string): Promise<string>;
|
|
6745
6820
|
/**
|
|
6746
6821
|
* Verifies a MFA temp token WITHOUT consuming it.
|
|
6747
6822
|
*
|
|
@@ -6779,6 +6854,7 @@ declare class TokenManagerService {
|
|
|
6779
6854
|
userId: string;
|
|
6780
6855
|
context: 'dashboard' | 'platform';
|
|
6781
6856
|
jti: string;
|
|
6857
|
+
tenantId?: string;
|
|
6782
6858
|
}>;
|
|
6783
6859
|
/**
|
|
6784
6860
|
* Atomically consumes a previously-verified MFA temp token.
|
|
@@ -7356,6 +7432,7 @@ declare class MfaService {
|
|
|
7356
7432
|
* @param context - Which identity plane the account belongs to.
|
|
7357
7433
|
* @param secretBase32 - The decrypted secret.
|
|
7358
7434
|
* @param recoveryCodes - The stored recovery digests, written back unchanged.
|
|
7435
|
+
* @param tenantId - The tenant the account belongs to, so the write is scoped like the read.
|
|
7359
7436
|
*/
|
|
7360
7437
|
private reencryptSecret;
|
|
7361
7438
|
/**
|
|
@@ -7546,7 +7623,7 @@ declare class MfaService {
|
|
|
7546
7623
|
* reused (rather than introducing a new one) because the consumer-facing
|
|
7547
7624
|
* meaning is the same: "platform MFA cannot operate".
|
|
7548
7625
|
*/
|
|
7549
|
-
setup(userId: string, context?: 'dashboard' | 'platform', password?: string): Promise<MfaSetupResult>;
|
|
7626
|
+
setup(userId: string, context?: 'dashboard' | 'platform', password?: string, tenantId?: string): Promise<MfaSetupResult>;
|
|
7550
7627
|
/**
|
|
7551
7628
|
* Verifies the first TOTP code from the user's authenticator app and permanently
|
|
7552
7629
|
* enables MFA on the account.
|
|
@@ -7567,7 +7644,7 @@ declare class MfaService {
|
|
|
7567
7644
|
* @throws `MFA_NOT_ENABLED` if `context === 'platform'` and the platform user
|
|
7568
7645
|
* repository was not configured at module registration.
|
|
7569
7646
|
*/
|
|
7570
|
-
verifyAndEnable(userId: string, code: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform'): Promise<void>;
|
|
7647
|
+
verifyAndEnable(userId: string, code: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<void>;
|
|
7571
7648
|
/**
|
|
7572
7649
|
* Validates a TOTP or recovery code and exchanges the MFA temp token for full
|
|
7573
7650
|
* access tokens.
|
|
@@ -7610,7 +7687,7 @@ declare class MfaService {
|
|
|
7610
7687
|
* @throws `ACCOUNT_LOCKED` if the brute-force threshold has been reached.
|
|
7611
7688
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
|
|
7612
7689
|
*/
|
|
7613
|
-
disable(userId: string, code: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform'): Promise<void>;
|
|
7690
|
+
disable(userId: string, code: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<void>;
|
|
7614
7691
|
/**
|
|
7615
7692
|
* Removes a user's second factor without their TOTP code, for a support desk facing a user
|
|
7616
7693
|
* who has lost their authenticator AND their recovery codes.
|
|
@@ -7649,7 +7726,7 @@ declare class MfaService {
|
|
|
7649
7726
|
* `'platform'`. Must match the plane the account lives in, or the lookup misses.
|
|
7650
7727
|
* @throws `TOKEN_INVALID` if no user with that ID exists in the given plane.
|
|
7651
7728
|
*/
|
|
7652
|
-
resetMfa(userId: string, context?: 'dashboard' | 'platform'): Promise<void>;
|
|
7729
|
+
resetMfa(userId: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<void>;
|
|
7653
7730
|
/**
|
|
7654
7731
|
* Regenerates the user's MFA recovery codes after verifying a current TOTP code.
|
|
7655
7732
|
*
|
|
@@ -7687,7 +7764,7 @@ declare class MfaService {
|
|
|
7687
7764
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
|
|
7688
7765
|
* @returns The freshly generated plain-text recovery codes, one-time display.
|
|
7689
7766
|
*/
|
|
7690
|
-
regenerateRecoveryCodes(userId: string, totpCode: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform'): Promise<{
|
|
7767
|
+
regenerateRecoveryCodes(userId: string, totpCode: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<{
|
|
7691
7768
|
recoveryCodes: string[];
|
|
7692
7769
|
}>;
|
|
7693
7770
|
/**
|
|
@@ -7752,6 +7829,53 @@ declare class MfaService {
|
|
|
7752
7829
|
* @throws `TOKEN_INVALID` if the user is not found.
|
|
7753
7830
|
* @throws {@link AuthException} the status error when the account is blocked.
|
|
7754
7831
|
*/
|
|
7832
|
+
/**
|
|
7833
|
+
* A dashboard MFA flow MUST carry the tenant it was authenticated in; a platform flow MUST NOT.
|
|
7834
|
+
*
|
|
7835
|
+
* The controller sources the tenant from the verified JWT, which always carries a validated
|
|
7836
|
+
* `tenantId`, so this never trips through the HTTP surface. It guards the OTHER caller:
|
|
7837
|
+
* `MfaService` is a public API, and a host wiring it directly — or a future SSO path — could hand
|
|
7838
|
+
* a dashboard `userId` with no tenant, which would silently reach the tenant-blind account read
|
|
7839
|
+
* and key derivation. Refuse it here, naming the field, rather than let it degrade to the
|
|
7840
|
+
* pre-tenant behaviour, which is exactly the fallback an attacker would aim for.
|
|
7841
|
+
*
|
|
7842
|
+
* @param context - The identity plane the call is on.
|
|
7843
|
+
* @param tenantId - The tenant supplied by the caller, if any.
|
|
7844
|
+
* @throws {@link AuthException} `VALIDATION` (400) when a dashboard call omits the tenant, or a
|
|
7845
|
+
* platform call supplies one.
|
|
7846
|
+
*/
|
|
7847
|
+
private assertPlaneTenant;
|
|
7848
|
+
/**
|
|
7849
|
+
* The tenant-scoped brute-force identifier for one MFA flow, and the legacy plane-only one it
|
|
7850
|
+
* replaces — equal on the platform plane, where the subject never carried a tenant, and distinct
|
|
7851
|
+
* on the dashboard plane, where it now does.
|
|
7852
|
+
*
|
|
7853
|
+
* A rolling upgrade runs old and new code against one Redis at once: on the dashboard plane an
|
|
7854
|
+
* old pod counts a failure under `{flow}:{plane}:{userId}` while a new pod counts it under
|
|
7855
|
+
* `{flow}:{mfaSubject}`, and either alone leaves a hole — a lockout an old pod filled would not
|
|
7856
|
+
* stop a new pod, and a success on one would not clear the other. For one release every read
|
|
7857
|
+
* consults both and every write touches both, so the two move as one; a later release drops the
|
|
7858
|
+
* legacy arm. On the platform plane the two ids are identical (no tenant ever entered the
|
|
7859
|
+
* subject), so the legacy arm is skipped — operating it would just be the same key a second time,
|
|
7860
|
+
* double-counting a failure and locking a platform admin out at half the threshold. `{flow}`
|
|
7861
|
+
* (`challenge` / `disable` / `reauth`) keeps the three counters isolated, exactly as before.
|
|
7862
|
+
*/
|
|
7863
|
+
private mfaFlowCounterIds;
|
|
7864
|
+
/**
|
|
7865
|
+
* Locked out when EITHER the legacy or the tenant-scoped counter has reached the threshold, so
|
|
7866
|
+
* neither a lockout an old pod recorded nor one a new pod recorded can be bypassed mid-migration.
|
|
7867
|
+
*/
|
|
7868
|
+
private isMfaFlowLockedOut;
|
|
7869
|
+
/**
|
|
7870
|
+
* Records the failure under both counters so the budget an attacker spends is seen by old and
|
|
7871
|
+
* new code alike — but under the scoped id ONCE when the two coincide (platform plane).
|
|
7872
|
+
*/
|
|
7873
|
+
private recordMfaFlowFailure;
|
|
7874
|
+
/**
|
|
7875
|
+
* Clears both counters on success — resetting only one would leave the other to lock a
|
|
7876
|
+
* legitimately authenticated user out on the next attempt — collapsing to one when they coincide.
|
|
7877
|
+
*/
|
|
7878
|
+
private resetMfaFlowFailures;
|
|
7755
7879
|
private fetchUserForContext;
|
|
7756
7880
|
}
|
|
7757
7881
|
|
package/dist/server/index.d.ts
CHANGED
|
@@ -213,10 +213,18 @@ interface IUserRepository {
|
|
|
213
213
|
/**
|
|
214
214
|
* Updates the user's TOTP MFA configuration.
|
|
215
215
|
*
|
|
216
|
+
* The tenant is required for the same reason {@link findById} takes one: the library may not
|
|
217
|
+
* assume ids are unique across tenants, so a write keyed by id alone can land on another tenant's
|
|
218
|
+
* row — or on a row other than the one the MFA flow read a moment earlier, which would leave a
|
|
219
|
+
* spent recovery code in the account's list. An implementation MUST scope the update by both the
|
|
220
|
+
* id and the tenant, exactly as its read does.
|
|
221
|
+
*
|
|
216
222
|
* @param id - The user's unique identifier.
|
|
223
|
+
* @param tenantId - The tenant the account belongs to; the update MUST be scoped to it, as
|
|
224
|
+
* {@link findById} scopes its read. Never omitted for a dashboard account.
|
|
217
225
|
* @param data - New MFA state. See {@link UpdateMfaData}.
|
|
218
226
|
*/
|
|
219
|
-
updateMfa(id: string, data: UpdateMfaData): Promise<void>;
|
|
227
|
+
updateMfa(id: string, tenantId: string | undefined, data: UpdateMfaData): Promise<void>;
|
|
220
228
|
/**
|
|
221
229
|
* Records the current timestamp as the user's last successful login time.
|
|
222
230
|
*
|
|
@@ -1100,6 +1108,25 @@ interface BymaxAuthModuleOptions {
|
|
|
1100
1108
|
* ```
|
|
1101
1109
|
*/
|
|
1102
1110
|
tenantIdResolver?: (req: Request) => string | Promise<string>;
|
|
1111
|
+
/**
|
|
1112
|
+
* Whether the dashboard guard re-resolves the request's tenant and rejects a token whose
|
|
1113
|
+
* `tenantId` claim does not match it.
|
|
1114
|
+
*
|
|
1115
|
+
* The resolver runs on the credential flows — register, login, OTP, OAuth — where it decides
|
|
1116
|
+
* which tenant an account is scoped to. It does **not** run on every guarded request: a valid
|
|
1117
|
+
* token is trusted for the tenant baked into it. That is safe on its own, because a token minted
|
|
1118
|
+
* for tenant A never carries tenant B, so there is no cross-tenant escalation to be had. What it
|
|
1119
|
+
* does leave open is a token being *presented* under the wrong host — accepted, and operating as
|
|
1120
|
+
* its own tenant rather than the host's. For a deployment where the host is the tenant boundary
|
|
1121
|
+
* (subdomain-per-tenant behind an edge that refuses a `Host` it does not serve), turning this on
|
|
1122
|
+
* closes that gap: the guard calls {@link tenantIdResolver} and refuses a mismatch.
|
|
1123
|
+
*
|
|
1124
|
+
* Off by default, and requires a resolver to have any effect: with no resolver there is nothing
|
|
1125
|
+
* to compare against. Leave it off where a token is legitimately used across hosts (a single API
|
|
1126
|
+
* origin serving many tenants by claim), because there the resolved host tenant and the token
|
|
1127
|
+
* tenant are *meant* to differ.
|
|
1128
|
+
*/
|
|
1129
|
+
enforceTenantBinding?: boolean;
|
|
1103
1130
|
/**
|
|
1104
1131
|
* Granular control over which controllers are registered.
|
|
1105
1132
|
* Allows disabling endpoints that are not needed for a specific application.
|
|
@@ -2985,6 +3012,24 @@ interface MfaTempPayload {
|
|
|
2985
3012
|
type: 'mfa_challenge';
|
|
2986
3013
|
/** Indicates which authentication context triggered the MFA challenge. */
|
|
2987
3014
|
context: 'dashboard' | 'platform';
|
|
3015
|
+
/**
|
|
3016
|
+
* The tenant the dashboard challenge belongs to. Present on the dashboard plane, ABSENT on the
|
|
3017
|
+
* platform plane — platform admins carry no tenant. It is the whole reason the challenge can
|
|
3018
|
+
* resolve the pending account tenant-scoped: without it the second-factor step, the
|
|
3019
|
+
* account-status gate and the finally-minted session all resolved the account by `sub` alone, so
|
|
3020
|
+
* under a host schema whose ids are per-tenant sequential a challenge could decide against, and
|
|
3021
|
+
* mint a session for, a homonym in another tenant.
|
|
3022
|
+
*
|
|
3023
|
+
* NOT optional in effect, despite the `?`: `verifyMfaTempToken` REJECTS a dashboard token that
|
|
3024
|
+
* lacks it and a platform token that carries it. The `?` only expresses that the two planes
|
|
3025
|
+
* populate it differently — it is never a value the caller may omit and have honoured. A missing
|
|
3026
|
+
* claim reads as an invalid token, not as a fall back to the pre-tenant lookup: a fallback would
|
|
3027
|
+
* leave the tenant-blind path reachable to anyone who simply drops the field (RFC 8725 §3.9/§3.12;
|
|
3028
|
+
* ASVS 6.6.2 — the out-of-band token must be bound to the request that originated it). A token
|
|
3029
|
+
* minted before this claim existed is therefore refused, and the user redoes login within the
|
|
3030
|
+
* five-minute TTL; there is no in-flight fallback to drain.
|
|
3031
|
+
*/
|
|
3032
|
+
tenantId?: string;
|
|
2988
3033
|
/**
|
|
2989
3034
|
* The subject's token **epoch** at issuance, in the plane named by `context`.
|
|
2990
3035
|
*
|
|
@@ -4910,9 +4955,10 @@ declare class TrustedOriginGuard implements CanActivate {
|
|
|
4910
4955
|
* Verifies that the authenticated user's account status is not blocked.
|
|
4911
4956
|
*
|
|
4912
4957
|
* On each request, the user's current status is resolved from a Redis cache
|
|
4913
|
-
* (`us:{userId}`) to avoid a database round-trip on every request. A cache
|
|
4914
|
-
* triggers a repository lookup, and the result is cached for
|
|
4915
|
-
* `userStatusCacheTtlSeconds` (default: 60 s).
|
|
4958
|
+
* (`us:{tenantId}:{userId}`) to avoid a database round-trip on every request. A cache
|
|
4959
|
+
* miss triggers a tenant-scoped repository lookup, and the result is cached for
|
|
4960
|
+
* `userStatusCacheTtlSeconds` (default: 60 s). The tenant is part of the key because a
|
|
4961
|
+
* repository id is unique only within a tenant.
|
|
4916
4962
|
*
|
|
4917
4963
|
* Status-specific errors allow the client to display the correct message:
|
|
4918
4964
|
* - `BANNED` → `ACCOUNT_BANNED` (403)
|
|
@@ -4920,6 +4966,15 @@ declare class TrustedOriginGuard implements CanActivate {
|
|
|
4920
4966
|
* - `SUSPENDED` → `ACCOUNT_SUSPENDED` (403)
|
|
4921
4967
|
* - `PENDING` → `PENDING_APPROVAL` (403)
|
|
4922
4968
|
*
|
|
4969
|
+
* When `emailVerification.required` is enabled (the default), this guard also
|
|
4970
|
+
* refuses an account whose email is not yet verified, with `EMAIL_NOT_VERIFIED`.
|
|
4971
|
+
* This is what makes verification a gate on API access and not only on `login`:
|
|
4972
|
+
* registration issues a session before verification, and without this check that
|
|
4973
|
+
* session would reach every route this guard protects for the access token's
|
|
4974
|
+
* lifetime. The verified flag is cached alongside the status under
|
|
4975
|
+
* `uev:{tenantId}:{userId}`, on the same miss and the same TTL, so the check costs no
|
|
4976
|
+
* extra round-trip.
|
|
4977
|
+
*
|
|
4923
4978
|
* Routes without an authenticated user (`request.user` absent) are passed
|
|
4924
4979
|
* through — this guard is designed to be composed after {@link JwtAuthGuard}.
|
|
4925
4980
|
*
|
|
@@ -5945,7 +6000,24 @@ declare class BruteForceService {
|
|
|
5945
6000
|
*/
|
|
5946
6001
|
declare class OtpService {
|
|
5947
6002
|
private readonly redis;
|
|
5948
|
-
|
|
6003
|
+
private readonly options;
|
|
6004
|
+
constructor(redis: AuthRedisService, options: ResolvedOptions);
|
|
6005
|
+
/**
|
|
6006
|
+
* Keyed one-way transform of an OTP under which it is stored and compared.
|
|
6007
|
+
*
|
|
6008
|
+
* The record is never the code itself: a six-digit code is a keyspace of a million, which a
|
|
6009
|
+
* plain digest lets anyone who reads Redis reverse instantly, so the transform is HMAC-SHA256
|
|
6010
|
+
* under the library's server-only `hmacKey` — the same key that already conceals the identifier.
|
|
6011
|
+
* It is bound to the identifier so the same code under two accounts does not collapse to one
|
|
6012
|
+
* value. `store` and `verify` compute it the same way; the byte-identical verify script keeps
|
|
6013
|
+
* comparing two opaque strings, so the rust-auth side stays in step by transforming the code the
|
|
6014
|
+
* same way before it reads or writes the shared record.
|
|
6015
|
+
*
|
|
6016
|
+
* @param identifier - The user-scoped identifier the record is keyed by.
|
|
6017
|
+
* @param code - The plaintext OTP.
|
|
6018
|
+
* @returns The hex HMAC written to, and compared against, the record.
|
|
6019
|
+
*/
|
|
6020
|
+
private fingerprint;
|
|
5949
6021
|
/**
|
|
5950
6022
|
* Generates a cryptographically secure numeric OTP string.
|
|
5951
6023
|
*
|
|
@@ -6739,9 +6811,12 @@ declare class TokenManagerService {
|
|
|
6739
6811
|
*
|
|
6740
6812
|
* @param userId - The user (or admin) pending MFA completion.
|
|
6741
6813
|
* @param context - Authentication context: `'dashboard'` or `'platform'`.
|
|
6814
|
+
* @param tenantId - The tenant the account belongs to, on the dashboard plane only. Stamped
|
|
6815
|
+
* into the token so the challenge resolves the account tenant-scoped rather than by `sub`
|
|
6816
|
+
* alone. Omitted on the platform plane, which has no tenant.
|
|
6742
6817
|
* @returns The signed MFA temp JWT.
|
|
6743
6818
|
*/
|
|
6744
|
-
issueMfaTempToken(userId: string, context: 'dashboard' | 'platform'): Promise<string>;
|
|
6819
|
+
issueMfaTempToken(userId: string, context: 'dashboard' | 'platform', tenantId?: string): Promise<string>;
|
|
6745
6820
|
/**
|
|
6746
6821
|
* Verifies a MFA temp token WITHOUT consuming it.
|
|
6747
6822
|
*
|
|
@@ -6779,6 +6854,7 @@ declare class TokenManagerService {
|
|
|
6779
6854
|
userId: string;
|
|
6780
6855
|
context: 'dashboard' | 'platform';
|
|
6781
6856
|
jti: string;
|
|
6857
|
+
tenantId?: string;
|
|
6782
6858
|
}>;
|
|
6783
6859
|
/**
|
|
6784
6860
|
* Atomically consumes a previously-verified MFA temp token.
|
|
@@ -7356,6 +7432,7 @@ declare class MfaService {
|
|
|
7356
7432
|
* @param context - Which identity plane the account belongs to.
|
|
7357
7433
|
* @param secretBase32 - The decrypted secret.
|
|
7358
7434
|
* @param recoveryCodes - The stored recovery digests, written back unchanged.
|
|
7435
|
+
* @param tenantId - The tenant the account belongs to, so the write is scoped like the read.
|
|
7359
7436
|
*/
|
|
7360
7437
|
private reencryptSecret;
|
|
7361
7438
|
/**
|
|
@@ -7546,7 +7623,7 @@ declare class MfaService {
|
|
|
7546
7623
|
* reused (rather than introducing a new one) because the consumer-facing
|
|
7547
7624
|
* meaning is the same: "platform MFA cannot operate".
|
|
7548
7625
|
*/
|
|
7549
|
-
setup(userId: string, context?: 'dashboard' | 'platform', password?: string): Promise<MfaSetupResult>;
|
|
7626
|
+
setup(userId: string, context?: 'dashboard' | 'platform', password?: string, tenantId?: string): Promise<MfaSetupResult>;
|
|
7550
7627
|
/**
|
|
7551
7628
|
* Verifies the first TOTP code from the user's authenticator app and permanently
|
|
7552
7629
|
* enables MFA on the account.
|
|
@@ -7567,7 +7644,7 @@ declare class MfaService {
|
|
|
7567
7644
|
* @throws `MFA_NOT_ENABLED` if `context === 'platform'` and the platform user
|
|
7568
7645
|
* repository was not configured at module registration.
|
|
7569
7646
|
*/
|
|
7570
|
-
verifyAndEnable(userId: string, code: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform'): Promise<void>;
|
|
7647
|
+
verifyAndEnable(userId: string, code: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<void>;
|
|
7571
7648
|
/**
|
|
7572
7649
|
* Validates a TOTP or recovery code and exchanges the MFA temp token for full
|
|
7573
7650
|
* access tokens.
|
|
@@ -7610,7 +7687,7 @@ declare class MfaService {
|
|
|
7610
7687
|
* @throws `ACCOUNT_LOCKED` if the brute-force threshold has been reached.
|
|
7611
7688
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
|
|
7612
7689
|
*/
|
|
7613
|
-
disable(userId: string, code: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform'): Promise<void>;
|
|
7690
|
+
disable(userId: string, code: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<void>;
|
|
7614
7691
|
/**
|
|
7615
7692
|
* Removes a user's second factor without their TOTP code, for a support desk facing a user
|
|
7616
7693
|
* who has lost their authenticator AND their recovery codes.
|
|
@@ -7649,7 +7726,7 @@ declare class MfaService {
|
|
|
7649
7726
|
* `'platform'`. Must match the plane the account lives in, or the lookup misses.
|
|
7650
7727
|
* @throws `TOKEN_INVALID` if no user with that ID exists in the given plane.
|
|
7651
7728
|
*/
|
|
7652
|
-
resetMfa(userId: string, context?: 'dashboard' | 'platform'): Promise<void>;
|
|
7729
|
+
resetMfa(userId: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<void>;
|
|
7653
7730
|
/**
|
|
7654
7731
|
* Regenerates the user's MFA recovery codes after verifying a current TOTP code.
|
|
7655
7732
|
*
|
|
@@ -7687,7 +7764,7 @@ declare class MfaService {
|
|
|
7687
7764
|
* @throws `MFA_INVALID_CODE` if the submitted TOTP code is incorrect.
|
|
7688
7765
|
* @returns The freshly generated plain-text recovery codes, one-time display.
|
|
7689
7766
|
*/
|
|
7690
|
-
regenerateRecoveryCodes(userId: string, totpCode: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform'): Promise<{
|
|
7767
|
+
regenerateRecoveryCodes(userId: string, totpCode: string, ip: string, userAgent: string, context?: 'dashboard' | 'platform', tenantId?: string): Promise<{
|
|
7691
7768
|
recoveryCodes: string[];
|
|
7692
7769
|
}>;
|
|
7693
7770
|
/**
|
|
@@ -7752,6 +7829,53 @@ declare class MfaService {
|
|
|
7752
7829
|
* @throws `TOKEN_INVALID` if the user is not found.
|
|
7753
7830
|
* @throws {@link AuthException} the status error when the account is blocked.
|
|
7754
7831
|
*/
|
|
7832
|
+
/**
|
|
7833
|
+
* A dashboard MFA flow MUST carry the tenant it was authenticated in; a platform flow MUST NOT.
|
|
7834
|
+
*
|
|
7835
|
+
* The controller sources the tenant from the verified JWT, which always carries a validated
|
|
7836
|
+
* `tenantId`, so this never trips through the HTTP surface. It guards the OTHER caller:
|
|
7837
|
+
* `MfaService` is a public API, and a host wiring it directly — or a future SSO path — could hand
|
|
7838
|
+
* a dashboard `userId` with no tenant, which would silently reach the tenant-blind account read
|
|
7839
|
+
* and key derivation. Refuse it here, naming the field, rather than let it degrade to the
|
|
7840
|
+
* pre-tenant behaviour, which is exactly the fallback an attacker would aim for.
|
|
7841
|
+
*
|
|
7842
|
+
* @param context - The identity plane the call is on.
|
|
7843
|
+
* @param tenantId - The tenant supplied by the caller, if any.
|
|
7844
|
+
* @throws {@link AuthException} `VALIDATION` (400) when a dashboard call omits the tenant, or a
|
|
7845
|
+
* platform call supplies one.
|
|
7846
|
+
*/
|
|
7847
|
+
private assertPlaneTenant;
|
|
7848
|
+
/**
|
|
7849
|
+
* The tenant-scoped brute-force identifier for one MFA flow, and the legacy plane-only one it
|
|
7850
|
+
* replaces — equal on the platform plane, where the subject never carried a tenant, and distinct
|
|
7851
|
+
* on the dashboard plane, where it now does.
|
|
7852
|
+
*
|
|
7853
|
+
* A rolling upgrade runs old and new code against one Redis at once: on the dashboard plane an
|
|
7854
|
+
* old pod counts a failure under `{flow}:{plane}:{userId}` while a new pod counts it under
|
|
7855
|
+
* `{flow}:{mfaSubject}`, and either alone leaves a hole — a lockout an old pod filled would not
|
|
7856
|
+
* stop a new pod, and a success on one would not clear the other. For one release every read
|
|
7857
|
+
* consults both and every write touches both, so the two move as one; a later release drops the
|
|
7858
|
+
* legacy arm. On the platform plane the two ids are identical (no tenant ever entered the
|
|
7859
|
+
* subject), so the legacy arm is skipped — operating it would just be the same key a second time,
|
|
7860
|
+
* double-counting a failure and locking a platform admin out at half the threshold. `{flow}`
|
|
7861
|
+
* (`challenge` / `disable` / `reauth`) keeps the three counters isolated, exactly as before.
|
|
7862
|
+
*/
|
|
7863
|
+
private mfaFlowCounterIds;
|
|
7864
|
+
/**
|
|
7865
|
+
* Locked out when EITHER the legacy or the tenant-scoped counter has reached the threshold, so
|
|
7866
|
+
* neither a lockout an old pod recorded nor one a new pod recorded can be bypassed mid-migration.
|
|
7867
|
+
*/
|
|
7868
|
+
private isMfaFlowLockedOut;
|
|
7869
|
+
/**
|
|
7870
|
+
* Records the failure under both counters so the budget an attacker spends is seen by old and
|
|
7871
|
+
* new code alike — but under the scoped id ONCE when the two coincide (platform plane).
|
|
7872
|
+
*/
|
|
7873
|
+
private recordMfaFlowFailure;
|
|
7874
|
+
/**
|
|
7875
|
+
* Clears both counters on success — resetting only one would leave the other to lock a
|
|
7876
|
+
* legitimately authenticated user out on the next attempt — collapsing to one when they coincide.
|
|
7877
|
+
*/
|
|
7878
|
+
private resetMfaFlowFailures;
|
|
7755
7879
|
private fetchUserForContext;
|
|
7756
7880
|
}
|
|
7757
7881
|
|