Skip to content

Commit

Permalink
Fixes to password policy validation (#2227)
Browse files Browse the repository at this point in the history
* 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 <110490169+pragatimodi@users.noreply.github.com>
  • Loading branch information
kevinthecheung and pragatimodi authored Jun 27, 2023
1 parent 626814a commit 64f0336
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/auth/auth-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2146,49 +2146,42 @@ export class PasswordPolicyAuthConfig implements PasswordPolicyConfig {
);
}
}
if (typeof options.constraints.requireUppercase !== undefined &&
if (typeof options.constraints.requireUppercase !== 'undefined' &&
!validator.isBoolean(options.constraints.requireUppercase)) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_CONFIG,
'"PasswordPolicyConfig.constraints.requireUppercase" must be a boolean.',
);
}
if (typeof options.constraints.requireLowercase !== undefined &&
if (typeof options.constraints.requireLowercase !== 'undefined' &&
!validator.isBoolean(options.constraints.requireLowercase)) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_CONFIG,
'"PasswordPolicyConfig.constraints.requireLowercase" must be a boolean.',
);
}
if (typeof options.constraints.requireNonAlphanumeric !== undefined &&
if (typeof options.constraints.requireNonAlphanumeric !== 'undefined' &&
!validator.isBoolean(options.constraints.requireNonAlphanumeric)) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_CONFIG,
'"PasswordPolicyConfig.constraints.requireNonAlphanumeric"' +
' must be a boolean.',
);
}
if (typeof options.constraints.requireNumeric !== undefined &&
if (typeof options.constraints.requireNumeric !== 'undefined' &&
!validator.isBoolean(options.constraints.requireNumeric)) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_CONFIG,
'"PasswordPolicyConfig.constraints.requireNumeric" must be a boolean.',
);
}
if (!validator.isNumber(options.constraints.minLength)) {
if (typeof options.constraints.minLength === 'undefined') {
options.constraints.minLength = 6;
} else if (!validator.isNumber(options.constraints.minLength)) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_CONFIG,
'"PasswordPolicyConfig.constraints.minLength" must be a number.',
);
}
if (!validator.isNumber(options.constraints.maxLength)) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_CONFIG,
'"PasswordPolicyConfig.constraints.maxLength" must be a number.',
);
}
if (options.constraints.minLength === undefined) {
options.constraints.minLength = 6;
} else {
if (!(options.constraints.minLength >= 6
&& options.constraints.minLength <= 30)) {
Expand All @@ -2199,8 +2192,13 @@ export class PasswordPolicyAuthConfig implements PasswordPolicyConfig {
);
}
}
if (options.constraints.maxLength === undefined) {
if (typeof options.constraints.maxLength === 'undefined') {
options.constraints.maxLength = 4096;
} else if (!validator.isNumber(options.constraints.maxLength)) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_CONFIG,
'"PasswordPolicyConfig.constraints.maxLength" must be a number.',
);
} else {
if (!(options.constraints.maxLength >= options.constraints.minLength &&
options.constraints.maxLength <= 4096)) {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/auth/auth-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1297,4 +1297,28 @@ describe('PasswordPolicyAuthConfig',() => {
expect(validConfig.forceUpgradeOnSignin).to.deep.equal(true);
});
});

describe('buildServerRequest()', () => {
it('should return server request with default constraints', () => {
expect(PasswordPolicyAuthConfig.buildServerRequest({
enforcementState: 'ENFORCE',
constraints: {},
})).to.deep.equal({
passwordPolicyEnforcementState: 'ENFORCE',
forceUpgradeOnSignin: false,
passwordPolicyVersions: [
{
customStrengthOptions: {
containsLowercaseCharacter: false,
containsUppercaseCharacter: false,
containsNumericCharacter: false,
containsNonAlphanumericCharacter: false,
minPasswordLength: 6,
maxPasswordLength: 4096,
}
}
]
});
});
});
});

0 comments on commit 64f0336

Please sign in to comment.