Skip to content

Commit 64f0336

Browse files
Fixes to password policy validation (#2227)
* Fixes to password policy validation * add test * feat(auth): Add `TotpInfo` field to `UserRecord` (#2197) * Adding TotpInfo to userRecord * Changing type from `any` to `unknown` for type safety. * Addressing feedback --------- Co-authored-by: pragatimodi <[email protected]>
1 parent 626814a commit 64f0336

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/auth/auth-config.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -2146,49 +2146,42 @@ export class PasswordPolicyAuthConfig implements PasswordPolicyConfig {
21462146
);
21472147
}
21482148
}
2149-
if (typeof options.constraints.requireUppercase !== undefined &&
2149+
if (typeof options.constraints.requireUppercase !== 'undefined' &&
21502150
!validator.isBoolean(options.constraints.requireUppercase)) {
21512151
throw new FirebaseAuthError(
21522152
AuthClientErrorCode.INVALID_CONFIG,
21532153
'"PasswordPolicyConfig.constraints.requireUppercase" must be a boolean.',
21542154
);
21552155
}
2156-
if (typeof options.constraints.requireLowercase !== undefined &&
2156+
if (typeof options.constraints.requireLowercase !== 'undefined' &&
21572157
!validator.isBoolean(options.constraints.requireLowercase)) {
21582158
throw new FirebaseAuthError(
21592159
AuthClientErrorCode.INVALID_CONFIG,
21602160
'"PasswordPolicyConfig.constraints.requireLowercase" must be a boolean.',
21612161
);
21622162
}
2163-
if (typeof options.constraints.requireNonAlphanumeric !== undefined &&
2163+
if (typeof options.constraints.requireNonAlphanumeric !== 'undefined' &&
21642164
!validator.isBoolean(options.constraints.requireNonAlphanumeric)) {
21652165
throw new FirebaseAuthError(
21662166
AuthClientErrorCode.INVALID_CONFIG,
21672167
'"PasswordPolicyConfig.constraints.requireNonAlphanumeric"' +
21682168
' must be a boolean.',
21692169
);
21702170
}
2171-
if (typeof options.constraints.requireNumeric !== undefined &&
2171+
if (typeof options.constraints.requireNumeric !== 'undefined' &&
21722172
!validator.isBoolean(options.constraints.requireNumeric)) {
21732173
throw new FirebaseAuthError(
21742174
AuthClientErrorCode.INVALID_CONFIG,
21752175
'"PasswordPolicyConfig.constraints.requireNumeric" must be a boolean.',
21762176
);
21772177
}
2178-
if (!validator.isNumber(options.constraints.minLength)) {
2178+
if (typeof options.constraints.minLength === 'undefined') {
2179+
options.constraints.minLength = 6;
2180+
} else if (!validator.isNumber(options.constraints.minLength)) {
21792181
throw new FirebaseAuthError(
21802182
AuthClientErrorCode.INVALID_CONFIG,
21812183
'"PasswordPolicyConfig.constraints.minLength" must be a number.',
21822184
);
2183-
}
2184-
if (!validator.isNumber(options.constraints.maxLength)) {
2185-
throw new FirebaseAuthError(
2186-
AuthClientErrorCode.INVALID_CONFIG,
2187-
'"PasswordPolicyConfig.constraints.maxLength" must be a number.',
2188-
);
2189-
}
2190-
if (options.constraints.minLength === undefined) {
2191-
options.constraints.minLength = 6;
21922185
} else {
21932186
if (!(options.constraints.minLength >= 6
21942187
&& options.constraints.minLength <= 30)) {
@@ -2199,8 +2192,13 @@ export class PasswordPolicyAuthConfig implements PasswordPolicyConfig {
21992192
);
22002193
}
22012194
}
2202-
if (options.constraints.maxLength === undefined) {
2195+
if (typeof options.constraints.maxLength === 'undefined') {
22032196
options.constraints.maxLength = 4096;
2197+
} else if (!validator.isNumber(options.constraints.maxLength)) {
2198+
throw new FirebaseAuthError(
2199+
AuthClientErrorCode.INVALID_CONFIG,
2200+
'"PasswordPolicyConfig.constraints.maxLength" must be a number.',
2201+
);
22042202
} else {
22052203
if (!(options.constraints.maxLength >= options.constraints.minLength &&
22062204
options.constraints.maxLength <= 4096)) {

test/unit/auth/auth-config.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -1297,4 +1297,28 @@ describe('PasswordPolicyAuthConfig',() => {
12971297
expect(validConfig.forceUpgradeOnSignin).to.deep.equal(true);
12981298
});
12991299
});
1300+
1301+
describe('buildServerRequest()', () => {
1302+
it('should return server request with default constraints', () => {
1303+
expect(PasswordPolicyAuthConfig.buildServerRequest({
1304+
enforcementState: 'ENFORCE',
1305+
constraints: {},
1306+
})).to.deep.equal({
1307+
passwordPolicyEnforcementState: 'ENFORCE',
1308+
forceUpgradeOnSignin: false,
1309+
passwordPolicyVersions: [
1310+
{
1311+
customStrengthOptions: {
1312+
containsLowercaseCharacter: false,
1313+
containsUppercaseCharacter: false,
1314+
containsNumericCharacter: false,
1315+
containsNonAlphanumericCharacter: false,
1316+
minPasswordLength: 6,
1317+
maxPasswordLength: 4096,
1318+
}
1319+
}
1320+
]
1321+
});
1322+
});
1323+
});
13001324
});

0 commit comments

Comments
 (0)