Skip to content

Commit

Permalink
chore: update password scoring system for mt5
Browse files Browse the repository at this point in the history
  • Loading branch information
aizad-deriv committed May 6, 2024
1 parent 77f9864 commit 872d1be
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { dictionary } from '@zxcvbn-ts/language-common';
import { passwordErrorMessage, warningMessages } from '../../../constants/password';
import {
calculateScore,
calculateScoreMT5,
cfdSchema,
mt5Schema,
passwordKeys,
Expand All @@ -19,7 +20,7 @@ import PasswordViewerIcon from './PasswordViewerIcon';
import './WalletPasswordField.scss';

export const validatePassword = (password: string, mt5Policy: boolean) => {
const score = calculateScore(password);
const score = mt5Policy ? calculateScoreMT5(password) : calculateScore(password);
let errorMessage = '';

const options = { dictionary: { ...dictionary } };
Expand Down
49 changes: 42 additions & 7 deletions packages/wallets/src/utils/password-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ export type passwordKeys =
| 'wordByItself';

// Calculate Scores based on password strength
// CFD Password Validation
export const validPassword = (value: string) => {
return passwordRegex.isPasswordValid.test(value);
};

export const validPasswordMT5 = (value: string) => {
return passwordRegex.isMT5PasswordValid.test(value);
};

export const isPasswordValid = (password: string) => {
const isPasswordValid = (password: string) => {
return passwordRegex.isPasswordValid.test(password) && passwordRegex.isLengthValid.test(password);
};

export const isPasswordModerate = (password: string) => {
const isPasswordModerate = (password: string) => {
const hasMoreThanOneSymbol = (password.match(/\W/g) ?? []).length > 1;
return (
isPasswordValid(password) &&
Expand All @@ -44,7 +41,7 @@ export const isPasswordModerate = (password: string) => {
);
};

export const isPasswordStrong = (password: string) => {
const isPasswordStrong = (password: string) => {
const hasMoreThanOneSymbol = (password.match(/\W/g) ?? []).length > 1;
return (
isPasswordValid(password) &&
Expand All @@ -62,6 +59,44 @@ export const calculateScore = (password: string) => {
if (isPasswordStrong(password)) return 4;
};

// MT5 Password Validation
export const validPasswordMT5 = (value: string) => {
return passwordRegex.isMT5PasswordValid.test(value);
};

const isPasswordValidMT5 = (password: string) => {
return passwordRegex.isMT5PasswordValid.test(password) && passwordRegex.isMT5LengthValid.test(password);
};

const isPasswordModerateMT5 = (password: string) => {
const hasMoreThanOneSymbol = (password.match(/\W/g) ?? []).length > 1;
return (
isPasswordValidMT5(password) &&
hasMoreThanOneSymbol &&
password.length >= passwordValues.minLength &&
password.length < passwordValues.maxLengthMT5 &&
passwordRegex.isMT5LengthValid
);
};

const isPasswordStrongMT5 = (password: string) => {
const hasMoreThanOneSymbol = (password.match(/\W/g) ?? []).length > 1;
return (
isPasswordValidMT5(password) &&
hasMoreThanOneSymbol &&
password.length >= passwordValues.maxLengthMT5 &&
passwordRegex.isMT5LengthValid
);
};

export const calculateScoreMT5 = (password: string) => {
if (password.length === 0) return 0;
if (!isPasswordValidMT5(password)) return 1;
if (!isPasswordStrongMT5(password) && isPasswordValidMT5(password) && !isPasswordModerateMT5(password)) return 2;
if (!isPasswordStrongMT5(password) && isPasswordValidMT5(password) && isPasswordModerateMT5(password)) return 3;
if (isPasswordStrongMT5(password)) return 4;
};

// Password Schemas
export const mt5Schema = Yup.string()
.required(passwordErrorMessage.invalidLengthMT5)
Expand Down

0 comments on commit 872d1be

Please sign in to comment.