diff --git a/apps/meteor/app/lib/server/lib/passwordPolicy.ts b/apps/meteor/app/lib/server/lib/passwordPolicy.ts index 6e06b3b787ce..f949ede6acdc 100644 --- a/apps/meteor/app/lib/server/lib/passwordPolicy.ts +++ b/apps/meteor/app/lib/server/lib/passwordPolicy.ts @@ -2,15 +2,15 @@ import { PasswordPolicy } from '@rocket.chat/password-policies'; import { settings } from '../../../settings/server'; -let enabled = false; -let minLength = -1; -let maxLength = -1; -let forbidRepeatingCharacters = false; -let forbidRepeatingCharactersCount = 3; -let mustContainAtLeastOneLowercase = false; -let mustContainAtLeastOneUppercase = false; -let mustContainAtLeastOneNumber = false; -let mustContainAtLeastOneSpecialCharacter = false; +const enabled = false; +const minLength = -1; +const maxLength = -1; +const forbidRepeatingCharacters = false; +const forbidRepeatingCharactersCount = 3; +const mustContainAtLeastOneLowercase = false; +const mustContainAtLeastOneUppercase = false; +const mustContainAtLeastOneNumber = false; +const mustContainAtLeastOneSpecialCharacter = false; export let passwordPolicy = new PasswordPolicy({ enabled, @@ -25,9 +25,19 @@ export let passwordPolicy = new PasswordPolicy({ throwError: true, }); -settings.watch('Accounts_Password_Policy_Enabled', (value) => { - enabled = Boolean(value); - passwordPolicy = new PasswordPolicy({ +settings.watchMultiple( + [ + 'Accounts_Password_Policy_Enabled', + 'Accounts_Password_Policy_MinLength', + 'Accounts_Password_Policy_MaxLength', + 'Accounts_Password_Policy_ForbidRepeatingCharacters', + 'Accounts_Password_Policy_ForbidRepeatingCharactersCount', + 'Accounts_Password_Policy_AtLeastOneLowercase', + 'Accounts_Password_Policy_AtLeastOneUppercase', + 'Accounts_Password_Policy_AtLeastOneNumber', + 'Accounts_Password_Policy_AtLeastOneSpecialCharacter', + ], + ([ enabled, minLength, maxLength, @@ -37,126 +47,61 @@ settings.watch('Accounts_Password_Policy_Enabled', (value) => { mustContainAtLeastOneUppercase, mustContainAtLeastOneNumber, mustContainAtLeastOneSpecialCharacter, - throwError: true, - }); -}); -settings.watch('Accounts_Password_Policy_MinLength', (value) => { - minLength = Number(value); - passwordPolicy = new PasswordPolicy({ - enabled, - minLength, - maxLength, - forbidRepeatingCharacters, - forbidRepeatingCharactersCount, - mustContainAtLeastOneLowercase, - mustContainAtLeastOneUppercase, - mustContainAtLeastOneNumber, - mustContainAtLeastOneSpecialCharacter, - throwError: true, - }); -}); -settings.watch('Accounts_Password_Policy_MaxLength', (value) => { - maxLength = Number(value); - passwordPolicy = new PasswordPolicy({ - enabled, - minLength, - maxLength, - forbidRepeatingCharacters, - forbidRepeatingCharactersCount, - mustContainAtLeastOneLowercase, - mustContainAtLeastOneUppercase, - mustContainAtLeastOneNumber, - mustContainAtLeastOneSpecialCharacter, - throwError: true, - }); -}); -settings.watch('Accounts_Password_Policy_ForbidRepeatingCharacters', (value) => { - forbidRepeatingCharacters = Boolean(value); - passwordPolicy = new PasswordPolicy({ - enabled, - minLength, - maxLength, - forbidRepeatingCharacters, - forbidRepeatingCharactersCount, - mustContainAtLeastOneLowercase, - mustContainAtLeastOneUppercase, - mustContainAtLeastOneNumber, - mustContainAtLeastOneSpecialCharacter, - throwError: true, - }); -}); -settings.watch('Accounts_Password_Policy_ForbidRepeatingCharactersCount', (value) => { - forbidRepeatingCharactersCount = Number(value); - passwordPolicy = new PasswordPolicy({ - enabled, - minLength, - maxLength, - forbidRepeatingCharacters, - forbidRepeatingCharactersCount, - mustContainAtLeastOneLowercase, - mustContainAtLeastOneUppercase, - mustContainAtLeastOneNumber, - mustContainAtLeastOneSpecialCharacter, - throwError: true, - }); -}); -settings.watch('Accounts_Password_Policy_AtLeastOneLowercase', (value) => { - mustContainAtLeastOneLowercase = Boolean(value); - passwordPolicy = new PasswordPolicy({ - enabled, - minLength, - maxLength, - forbidRepeatingCharacters, - forbidRepeatingCharactersCount, - mustContainAtLeastOneLowercase, - mustContainAtLeastOneUppercase, - mustContainAtLeastOneNumber, - mustContainAtLeastOneSpecialCharacter, - throwError: true, - }); -}); -settings.watch('Accounts_Password_Policy_AtLeastOneUppercase', (value) => { - mustContainAtLeastOneUppercase = Boolean(value); - passwordPolicy = new PasswordPolicy({ - enabled, - minLength, - maxLength, - forbidRepeatingCharacters, - forbidRepeatingCharactersCount, - mustContainAtLeastOneLowercase, - mustContainAtLeastOneUppercase, - mustContainAtLeastOneNumber, - mustContainAtLeastOneSpecialCharacter, - throwError: true, - }); -}); -settings.watch('Accounts_Password_Policy_AtLeastOneNumber', (value) => { - mustContainAtLeastOneNumber = Boolean(value); - passwordPolicy = new PasswordPolicy({ - enabled, - minLength, - maxLength, - forbidRepeatingCharacters, - forbidRepeatingCharactersCount, - mustContainAtLeastOneLowercase, - mustContainAtLeastOneUppercase, - mustContainAtLeastOneNumber, - mustContainAtLeastOneSpecialCharacter, - throwError: true, - }); -}); -settings.watch('Accounts_Password_Policy_AtLeastOneSpecialCharacter', (value) => { - mustContainAtLeastOneSpecialCharacter = Boolean(value); - passwordPolicy = new PasswordPolicy({ - enabled, - minLength, - maxLength, - forbidRepeatingCharacters, - forbidRepeatingCharactersCount, - mustContainAtLeastOneLowercase, - mustContainAtLeastOneUppercase, - mustContainAtLeastOneNumber, - mustContainAtLeastOneSpecialCharacter, - throwError: true, - }); -}); + ]) => { + passwordPolicy = new PasswordPolicy({ + enabled: Boolean(enabled), + minLength: Number(minLength), + maxLength: Number(maxLength), + forbidRepeatingCharacters: Boolean(forbidRepeatingCharacters), + forbidRepeatingCharactersCount: Number(forbidRepeatingCharactersCount), + mustContainAtLeastOneLowercase: Boolean(mustContainAtLeastOneLowercase), + mustContainAtLeastOneUppercase: Boolean(mustContainAtLeastOneUppercase), + mustContainAtLeastOneNumber: Boolean(mustContainAtLeastOneNumber), + mustContainAtLeastOneSpecialCharacter: Boolean(mustContainAtLeastOneSpecialCharacter), + throwError: true, + }); + }, +); +// enabled, +// minLength, +// maxLength, +// forbidRepeatingCharacters, +// forbidRepeatingCharactersCount, +// mustContainAtLeastOneLowercase, +// mustContainAtLeastOneUppercase, +// mustContainAtLeastOneNumber, +// mustContainAtLeastOneSpecialCharacter, +// throwError: true, +// }); +// }); +// settings.watch('Accounts_Password_Policy_AtLeastOneNumber', (value) => { +// mustContainAtLeastOneNumber = Boolean(value); +// passwordPolicy = new PasswordPolicy({ +// enabled, +// minLength, +// maxLength, +// forbidRepeatingCharacters, +// forbidRepeatingCharactersCount, +// mustContainAtLeastOneLowercase, +// mustContainAtLeastOneUppercase, +// mustContainAtLeastOneNumber, +// mustContainAtLeastOneSpecialCharacter, +// throwError: true, +// }); +// }); +// settings.watch('Accounts_Password_Policy_AtLeastOneSpecialCharacter', (value) => { +// console.log('Accounts_Password_Policy_AtLeastOneSpecialCharacter'); +// mustContainAtLeastOneSpecialCharacter = Boolean(value); +// passwordPolicy = new PasswordPolicy({ +// enabled, +// minLength, +// maxLength, +// forbidRepeatingCharacters, +// forbidRepeatingCharactersCount, +// mustContainAtLeastOneLowercase, +// mustContainAtLeastOneUppercase, +// mustContainAtLeastOneNumber, +// mustContainAtLeastOneSpecialCharacter, +// throwError: true, +// }); +// }); diff --git a/packages/password-policies/src/PasswordPolicyClass.ts b/packages/password-policies/src/PasswordPolicyClass.ts index 5afc1b2a9445..8212df18002c 100644 --- a/packages/password-policies/src/PasswordPolicyClass.ts +++ b/packages/password-policies/src/PasswordPolicyClass.ts @@ -4,8 +4,15 @@ type PasswordPolicyType = { enabled: boolean; policy: [name: string, options?: Record][]; }; + +type ValidationMessageType = { + name: string; + isValid: boolean; + limit?: number; +}; + export class PasswordPolicy { - regex: { + private regex: { forbiddingRepeatingCharacters: RegExp; mustContainAtLeastOneLowercase: RegExp; mustContainAtLeastOneUppercase: RegExp; @@ -65,10 +72,6 @@ export class PasswordPolicy { }; } - get passwordForbidRepeatingCharactersCount() { - return this.forbidRepeatingCharactersCount; - } - error( error: string, message: string, @@ -84,19 +87,12 @@ export class PasswordPolicy { return false; } - // TODO: implement isLoading state? sendValidationMessage(password: string): { name: string; isValid: boolean; limit?: number; }[] { - type validationMessage = { - name: string; - isValid: boolean; - limit?: number; - }; - - const validationReturn: validationMessage[] = []; + const validationReturn: ValidationMessageType[] = []; if (!this.enabled) { return [];