File size: 1,647 Bytes
5569e4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const SEQUENTIAL_PATTERNS = [
  "abcdefghijklmnopqrstuvwxyz",
  "zyxwvutsrqponmlkjihgfedcba",
  "0123456789",
  "9876543210",
  "qwertyuiop",
  "poiuytrewq",
  "asdfghjkl",
  "lkjhgfdsa",
  "zxcvbnm",
  "mnbvcxz",
];

export function hasSequentialCharacters(password, span = 3) {
  const normalized = password.toLowerCase();
  if (normalized.length < span) return false;

  return SEQUENTIAL_PATTERNS.some((pattern) => {
    for (let index = 0; index <= pattern.length - span; index += 1) {
      if (normalized.includes(pattern.slice(index, index + span))) {
        return true;
      }
    }
    return false;
  });
}

export function getPasswordValidation(password, options = {}) {
  const {
    minLength = 8,
    requireUppercase = false,
    requireNumber = false,
    requireSpecial = false,
  } = options;

  return {
    hasValue: password.length > 0,
    minLength: password.length >= minLength,
    uppercase: !requireUppercase || /[A-Z]/.test(password),
    number: !requireNumber || /\d/.test(password),
    special: !requireSpecial || /[^A-Za-z0-9]/.test(password),
    noSequence: !hasSequentialCharacters(password),
  };
}

export function getPasswordValidationMessage(checks, options = {}) {
  const { minLength = 8 } = options;
  if (!checks.hasValue) return "";
  if (!checks.minLength) return `Use at least ${minLength} characters.`;
  if (!checks.uppercase) return "Add at least one uppercase letter.";
  if (!checks.number) return "Add at least one number.";
  if (!checks.special) return "Add at least one special character.";
  if (!checks.noSequence) return "Avoid sequential characters like abc or 123.";
  return "";
}