Skip to content

Commit

Permalink
Update password validator to adhere to OWASP ASVS v4.0.3 recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Jan 8, 2025
1 parent a7312ab commit 2929544
Showing 1 changed file with 0 additions and 48 deletions.
48 changes: 0 additions & 48 deletions src/lib/inputvalidation/passwordValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,6 @@ import type { TwColor } from '$lib/types/Tailwind';
import type { ValidationResult } from '$lib/types/ValidationResult';
import { calculateStringEntropy } from '$lib/utils/entropy';

function countCharacters(value: string): {
lower: number;
upper: number;
digit: number;
special: number;
} {
const uniqueChars = new Set(value);
let lower = 0;
let upper = 0;
let digit = 0;
let special = 0;

for (const char of uniqueChars) {
if (char.match(/[a-z]/)) {
lower++;
} else if (char.match(/[A-Z]/)) {
upper++;
} else if (char.match(/[0-9]/)) {
digit++;
} else if ('!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'.includes(char)) {
special++;
}
}

return { lower, upper, digit, special };
}

export function validatePassword(value: string): ValidationResult | null {
if (value.length == 0) {
return null;
Expand Down Expand Up @@ -60,27 +33,6 @@ export function validatePassword(value: string): ValidationResult | null {
return result;
}

const { lower, upper, digit, special } = countCharacters(value);
if (lower < 1) {
result.message = 'Password must contain a lowercase character';
return result;
}

if (upper < 1) {
result.message = 'Password must contain a uppercase character';
return result;
}

if (digit < 1) {
result.message = 'Password must contain a digit';
return result;
}

if (special < 1) {
result.message = 'Password must contain a special character';
return result;
}

return { valid: true };
}

Expand Down

0 comments on commit 2929544

Please sign in to comment.