-
Notifications
You must be signed in to change notification settings - Fork 946
Define implementation of internal password policy class #7447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e482874
* Define PasswordPolicyImpl
ch5zzy 7fe89f1
Update API reports
ch5zzy 9353dbe
Update docs
ch5zzy dc1eb5e
Remove PasswordValidationStatusImpl since the internal typing extends…
ch5zzy 0135226
Add PasswordPolicyCustomStrengthOptions internal typing
ch5zzy 8bcbb93
Fix undefined check on custom strength options
ch5zzy a9e4fdd
Revert change to public policy type and policy response
ch5zzy f0acf72
Update constant naming and check undefined fields in policy
ch5zzy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/auth/src/core/auth/password_policy_impl.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect, use } from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import sinonChai from 'sinon-chai'; | ||
import { PasswordPolicy } from '../../model/public_types'; | ||
import { PasswordPolicyImpl } from './password_policy_impl'; | ||
import { GetPasswordPolicyResponse } from '../../api/password_policy/get_password_policy'; | ||
|
||
use(sinonChai); | ||
use(chaiAsPromised); | ||
|
||
describe('core/auth/password_policy_impl', () => { | ||
const TEST_MIN_PASSWORD_LENGTH = 6; | ||
const TEST_MAX_PASSWORD_LENGTH = 30; | ||
const TEST_CONTAINS_LOWERCASE = true; | ||
const TEST_CONTAINS_UPPERCASE = true; | ||
const TEST_CONTAINS_NUMERIC = true; | ||
const TEST_CONTAINS_NON_ALPHANUMERIC = true; | ||
const TEST_ALLOWED_NON_ALPHANUMERIC_CHARS = ['!', '(', ')']; | ||
const TEST_SCHEMA_VERSION = 1; | ||
const PASSWORD_POLICY_RESPONSE_REQUIRE_ALL: GetPasswordPolicyResponse = { | ||
customStrengthOptions: { | ||
minPasswordLength: TEST_MIN_PASSWORD_LENGTH, | ||
maxPasswordLength: TEST_MAX_PASSWORD_LENGTH, | ||
containsLowercaseCharacter: TEST_CONTAINS_LOWERCASE, | ||
containsUppercaseCharacter: TEST_CONTAINS_UPPERCASE, | ||
containsNumericCharacter: TEST_CONTAINS_NUMERIC, | ||
containsNonAlphanumericCharacter: TEST_CONTAINS_NON_ALPHANUMERIC | ||
}, | ||
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS, | ||
schemaVersion: TEST_SCHEMA_VERSION | ||
}; | ||
const PASSWORD_POLICY_RESPONSE_REQUIRE_LENGTH: GetPasswordPolicyResponse = { | ||
customStrengthOptions: { | ||
minPasswordLength: TEST_MIN_PASSWORD_LENGTH, | ||
maxPasswordLength: TEST_MAX_PASSWORD_LENGTH | ||
}, | ||
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS, | ||
schemaVersion: TEST_SCHEMA_VERSION | ||
}; | ||
const PASSWORD_POLICY_REQUIRE_ALL: PasswordPolicy = { | ||
customStrengthOptions: { | ||
minPasswordLength: TEST_MIN_PASSWORD_LENGTH, | ||
maxPasswordLength: TEST_MAX_PASSWORD_LENGTH, | ||
containsLowercaseLetter: TEST_CONTAINS_LOWERCASE, | ||
containsUppercaseLetter: TEST_CONTAINS_UPPERCASE, | ||
containsNumericCharacter: TEST_CONTAINS_NUMERIC, | ||
containsNonAlphanumericCharacter: TEST_CONTAINS_UPPERCASE | ||
}, | ||
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS | ||
}; | ||
const PASSWORD_POLICY_REQUIRE_LENGTH: PasswordPolicy = { | ||
customStrengthOptions: { | ||
minPasswordLength: TEST_MIN_PASSWORD_LENGTH, | ||
maxPasswordLength: TEST_MAX_PASSWORD_LENGTH | ||
}, | ||
allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS | ||
}; | ||
|
||
context('#PasswordPolicyImpl', () => { | ||
it('can construct the password policy from the backend response', () => { | ||
const policy: PasswordPolicy = new PasswordPolicyImpl( | ||
PASSWORD_POLICY_RESPONSE_REQUIRE_ALL | ||
); | ||
// The password policy contains the schema version internally, but the public typing does not. | ||
ch5zzy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Only check the fields that are publicly exposed. | ||
expect(policy.customStrengthOptions).to.eql( | ||
PASSWORD_POLICY_REQUIRE_ALL.customStrengthOptions | ||
); | ||
expect(policy.allowedNonAlphanumericCharacters).to.eql( | ||
PASSWORD_POLICY_REQUIRE_ALL.allowedNonAlphanumericCharacters | ||
); | ||
}); | ||
|
||
it('only includes requirements defined in the response', () => { | ||
const policy: PasswordPolicy = new PasswordPolicyImpl( | ||
ch5zzy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PASSWORD_POLICY_RESPONSE_REQUIRE_LENGTH | ||
); | ||
expect(policy.customStrengthOptions).to.eql( | ||
PASSWORD_POLICY_REQUIRE_LENGTH.customStrengthOptions | ||
); | ||
expect(policy.allowedNonAlphanumericCharacters).to.eql( | ||
PASSWORD_POLICY_REQUIRE_LENGTH.allowedNonAlphanumericCharacters | ||
); | ||
// Requirements that are not in the response should be undefined. | ||
expect(policy.customStrengthOptions.containsLowercaseLetter).to.be | ||
.undefined; | ||
expect(policy.customStrengthOptions.containsUppercaseLetter).to.be | ||
.undefined; | ||
expect(policy.customStrengthOptions.containsNumericCharacter).to.be | ||
.undefined; | ||
expect(policy.customStrengthOptions.containsNonAlphanumericCharacter).to | ||
.be.undefined; | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { GetPasswordPolicyResponse } from '../../api/password_policy/get_password_policy'; | ||
import { | ||
PasswordPolicyCustomStrengthOptions, | ||
PasswordPolicyInternal, | ||
PasswordValidationStatusInternal | ||
} from '../../model/password_policy'; | ||
import { PasswordValidationStatus } from '../../model/public_types'; | ||
|
||
/** | ||
* Stores password policy requirements and provides password validation against the policy. | ||
* | ||
* @internal | ||
*/ | ||
export class PasswordPolicyImpl implements PasswordPolicyInternal { | ||
readonly customStrengthOptions: PasswordPolicyCustomStrengthOptions; | ||
readonly allowedNonAlphanumericCharacters: string[]; | ||
readonly schemaVersion: number; | ||
|
||
constructor(response: GetPasswordPolicyResponse) { | ||
// Only include custom strength options defined in the response. | ||
const responseOptions = response.customStrengthOptions; | ||
this.customStrengthOptions = {}; | ||
if (responseOptions.minPasswordLength) { | ||
this.customStrengthOptions.minPasswordLength = | ||
responseOptions.minPasswordLength; | ||
} | ||
if (responseOptions.maxPasswordLength) { | ||
this.customStrengthOptions.maxPasswordLength = | ||
responseOptions.maxPasswordLength; | ||
} | ||
if (responseOptions.containsLowercaseCharacter !== undefined) { | ||
this.customStrengthOptions.containsLowercaseLetter = | ||
responseOptions.containsLowercaseCharacter; | ||
} | ||
if (responseOptions.containsUppercaseCharacter !== undefined) { | ||
this.customStrengthOptions.containsUppercaseLetter = | ||
responseOptions.containsUppercaseCharacter; | ||
} | ||
if (responseOptions.containsNumericCharacter !== undefined) { | ||
this.customStrengthOptions.containsNumericCharacter = | ||
responseOptions.containsNumericCharacter; | ||
} | ||
if (responseOptions.containsNonAlphanumericCharacter !== undefined) { | ||
this.customStrengthOptions.containsNonAlphanumericCharacter = | ||
responseOptions.containsNonAlphanumericCharacter; | ||
} | ||
|
||
this.allowedNonAlphanumericCharacters = | ||
response.allowedNonAlphanumericCharacters; | ||
this.schemaVersion = response.schemaVersion; | ||
} | ||
|
||
validatePassword(password: string): PasswordValidationStatus { | ||
const status: PasswordValidationStatusInternal = { | ||
isValid: false, | ||
passwordPolicy: this | ||
}; | ||
|
||
// TODO: Implement private helper methods for checking length and character options. | ||
// Call these here to populate the status object. | ||
if (password) { | ||
status.isValid = true; | ||
} | ||
|
||
return status; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.