-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-4264-password-policy
- Loading branch information
Showing
19 changed files
with
193 additions
and
18 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
webapp/packages/core-authentication/src/PasswordPolicyService.ts
This file contains 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,73 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { computed, makeObservable } from 'mobx'; | ||
|
||
import { injectable } from '@cloudbeaver/core-di'; | ||
import { LocalizationService } from '@cloudbeaver/core-localization'; | ||
import { ServerConfigResource } from '@cloudbeaver/core-root'; | ||
import type { PasswordPolicyConfig } from '@cloudbeaver/core-sdk'; | ||
|
||
const DEFAULT_PASSWORD_POLICY: PasswordPolicyConfig = { | ||
minLength: 8, | ||
requiresUpperLowerCase: false, | ||
minDigits: 0, | ||
minSpecialCharacters: 0, | ||
}; | ||
|
||
type ValidationResult = { isValid: true; errorMessage: null } | { isValid: false; errorMessage: string }; | ||
|
||
@injectable() | ||
export class PasswordPolicyService { | ||
get config(): PasswordPolicyConfig { | ||
return { | ||
minLength: this.serverConfigResource.data?.passwordPolicyConfiguration?.minLength || DEFAULT_PASSWORD_POLICY.minLength, | ||
requiresUpperLowerCase: | ||
this.serverConfigResource.data?.passwordPolicyConfiguration?.requiresUpperLowerCase || DEFAULT_PASSWORD_POLICY.requiresUpperLowerCase, | ||
minDigits: this.serverConfigResource.data?.passwordPolicyConfiguration?.minDigits || DEFAULT_PASSWORD_POLICY.minDigits, | ||
minSpecialCharacters: | ||
this.serverConfigResource.data?.passwordPolicyConfiguration?.minSpecialCharacters || DEFAULT_PASSWORD_POLICY.minSpecialCharacters, | ||
}; | ||
} | ||
|
||
constructor(private readonly serverConfigResource: ServerConfigResource, private readonly localizationService: LocalizationService) { | ||
makeObservable(this, { | ||
config: computed, | ||
}); | ||
} | ||
|
||
validatePassword(password: string): ValidationResult { | ||
if (password.length < this.config.minLength) { | ||
return { | ||
isValid: false, | ||
errorMessage: this.localizationService.translate('core_authentication_password_policy_min_length', undefined, { min: this.config.minLength }), | ||
}; | ||
} | ||
|
||
if (this.config.requiresUpperLowerCase && !(/[a-z]/.test(password) && /[A-Z]/.test(password))) { | ||
return { isValid: false, errorMessage: this.localizationService.translate('core_authentication_password_policy_upper_lower_case') }; | ||
} | ||
|
||
if ((password.match(/\d/g) || []).length < this.config.minDigits) { | ||
return { | ||
isValid: false, | ||
errorMessage: this.localizationService.translate('core_authentication_password_policy_min_digits', undefined, { min: this.config.minDigits }), | ||
}; | ||
} | ||
|
||
if ((password.match(/[!@#$%^&*(),.?":{}|<>]/g) || []).length < this.config.minSpecialCharacters) { | ||
return { | ||
isValid: false, | ||
errorMessage: this.localizationService.translate('core_authentication_password_policy_min_special_characters', undefined, { | ||
min: this.config.minSpecialCharacters, | ||
}), | ||
}; | ||
} | ||
|
||
return { isValid: true, errorMessage: null }; | ||
} | ||
} |
This file contains 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 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 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 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 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 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
22 changes: 22 additions & 0 deletions
22
webapp/packages/core-authentication/src/usePasswordPolicy.ts
This file contains 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,22 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { useCustomInputValidation } from '@cloudbeaver/core-blocks'; | ||
import { useService } from '@cloudbeaver/core-di'; | ||
|
||
import { PasswordPolicyService } from './PasswordPolicyService'; | ||
|
||
export function usePasswordPolicy() { | ||
const passwordPolicyService = useService(PasswordPolicyService); | ||
|
||
const ref = useCustomInputValidation<string>(value => { | ||
const validation = passwordPolicyService.validatePassword(value); | ||
return validation.isValid ? null : validation.errorMessage; | ||
}); | ||
|
||
return ref; | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.