Fixed breached pwd error messages

This commit is contained in:
Joel Biddle
2023-08-22 20:20:54 +10:00
parent f47a119474
commit 1242d88acb
8 changed files with 81 additions and 70 deletions
@@ -1,3 +1,5 @@
import { checkIsPasswordBreached } from "./checkIsPasswordBreached";
/* eslint-disable no-param-reassign */
interface PasswordCheckProps {
password: string;
@@ -6,17 +8,19 @@ interface PasswordCheckProps {
setPasswordErrorTooLong: (value: boolean) => void;
setPasswordErrorNumber: (value: boolean) => void;
setPasswordErrorLowerCase: (value: boolean) => void;
setPasswordErrorIsBreached: (value: boolean) => void;
}
/**
* This function checks a user password with respect to some criteria.
*/
const passwordCheck = ({
const passwordCheck = async ({
password,
setPasswordErrorTooShort,
setPasswordErrorTooLong,
setPasswordErrorNumber,
setPasswordErrorLowerCase,
setPasswordErrorIsBreached,
errorCheck
}: PasswordCheckProps) => {
if (!password || password.length < 14) {
@@ -57,6 +61,13 @@ const passwordCheck = ({
setPasswordErrorLowerCase(false);
}
if (await checkIsPasswordBreached(password)) {
setPasswordErrorIsBreached(true);
errorCheck = true;
} else {
setPasswordErrorIsBreached(false);
}
// if (!/[A-Z]/.test(password)) {
// setPasswordErrorUpperCase(true);
// errorCheck = true;
@@ -1,21 +1,21 @@
import { checkIsPasswordBreached } from "./checkIsPasswordBreached";
type Errors = {
tooShort?: string,
tooLong?: string,
upperCase?: string,
lowerCase?: string,
number?: string,
specialChar?: string,
repeatedChar?: string,
commonPassword?: string,
breachedPassword?: string
};
tooShort?: string;
tooLong?: string;
upperCase?: string;
lowerCase?: string;
number?: string;
specialChar?: string;
repeatedChar?: string;
commonPassword?: string;
breachedPassword?: string;
};
interface CheckPasswordParams {
password: string;
commonPasswords: string[];
setErrors: (value: Errors) => void;
password: string;
commonPasswords: string[];
setErrors: (value: Errors) => void;
}
/**
@@ -26,62 +26,61 @@ interface CheckPasswordParams {
* - Contains at least 1 lowercase character (a-z)
* - Contains at least 1 number (0-9)
* - Does not contain 3 repeat, consecutive characters
*
*
* The function returns whether or not the password [password]
* passes the minimum requirements above. It sets errors on
* passes the minimum requirements above. It sets errors on
* an erorr object via [setErrors].
*
*
* @param {Object} obj
* @param {String} obj.password - the password to check
* @param {Function} obj.setErrors - set state function to set error object
*/
const checkPassword = async ({
password,
commonPasswords,
setErrors
password,
commonPasswords,
setErrors
}: CheckPasswordParams): Promise<boolean> => {
const errors: Errors = {};
const errors: Errors = {};
const isBreachedPassword = await checkIsPasswordBreached(password)
if (password.length < 14) {
errors.tooShort = "at least 14 characters";
}
if (password.length < 14) {
errors.tooShort = "at least 14 characters";
}
if (password.length > 100) {
errors.tooLong = "at most 100 characters";
}
if (password.length > 100) {
errors.tooLong = "at most 100 characters";
}
if (!/[A-Z]/.test(password)) {
errors.upperCase = "at least 1 uppercase character (A-Z)";
}
if (!/[A-Z]/.test(password)) {
errors.upperCase = "at least 1 uppercase character (A-Z)";
}
if (!/[a-z]/.test(password)) {
errors.lowerCase = "at least 1 lowercase character (a-z)";
}
if (!/[a-z]/.test(password)) {
errors.lowerCase = "at least 1 lowercase character (a-z)";
}
if (!/[0-9]/.test(password)) {
errors.number = "at least 1 number (0-9)";
}
if (!/[0-9]/.test(password)) {
errors.number = "at least 1 number (0-9)";
}
if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
errors.specialChar = "at least 1 special character (!@#$%^&*(),.?)";
}
if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
errors.specialChar = "at least 1 special character (!@#$%^&*(),.?)";
}
if (/([A-Za-z0-9])\1\1\1/.test(password)) {
errors.repeatedChar = "No 3 repeat, consecutive characters";
}
if (commonPasswords.includes(password)) {
errors.commonPassword = "No common passwords";
}
if (/([A-Za-z0-9])\1\1\1/.test(password)) {
errors.repeatedChar = "No 3 repeat, consecutive characters";
}
if (isBreachedPassword) {
errors.breachedPassword = "The password you provided is in a list of passwords commonly used on other websites. Please try again with a stronger password.";
}
setErrors(errors);
return Object.keys(errors).length > 0;
}
if (commonPasswords.includes(password)) {
errors.commonPassword = "No common passwords";
}
export default checkPassword;
if (await checkIsPasswordBreached(password)) {
errors.breachedPassword =
"The password you provided is in a list of passwords commonly used on other websites. Please try again with a stronger password.";
}
setErrors(errors);
return Object.keys(errors).length > 0;
};
export default checkPassword;