|
| 1 | +export type PasswordStrengthType = |
| 2 | + | "Very weak" |
| 3 | + | "Weak" |
| 4 | + | "Regular" |
| 5 | + | "Strong" |
| 6 | + | "Very strong" |
| 7 | + | "Not classified"; |
| 8 | + |
| 9 | +export type PasswordStrengthFunction = ( |
| 10 | + password: string, |
| 11 | + passwordLength: number, |
| 12 | +) => boolean; |
| 13 | + |
| 14 | +export interface PasswordStrengthTesterOptions { |
| 15 | + isVeryWeak: PasswordStrengthFunction; |
| 16 | + isWeak: PasswordStrengthFunction; |
| 17 | + isRegular: PasswordStrengthFunction; |
| 18 | + isStrong: PasswordStrengthFunction; |
| 19 | + isVeryStrong: PasswordStrengthFunction; |
| 20 | +} |
| 21 | + |
1 | 22 | /**
|
2 |
| - * @description Avalia a força de uma senha e retorna o tipo de força da senha. |
3 |
| - * |
4 |
| - * @returns O tipo de força da senha ('veryWeak', 'weak', 'regular', 'strong' ou 'veryStrong'). |
5 |
| - * |
6 |
| - * @example |
7 |
| - * passwordStrengthTester('12345'); // Output: 'veryWeak' |
8 |
| - * |
9 |
| - * @example |
10 |
| - * passwordStrengthTester('abcdef'); // Output: 'weak' |
| 23 | + * @param password - The password to test |
| 24 | + * @param Options - An object containing functions to test the password strength |
| 25 | + * @param Options.isVeryWeak - A function to test if the password is very weak |
| 26 | + * @param Options.isWeak - A function to test if the password is weak |
| 27 | + * @param Options.isRegular - A function to test if the password is regular |
| 28 | + * @param Options.isStrong - A function to test if the password is strong |
| 29 | + * @param Options.isVeryStrong - A function to test if the password is very strong |
| 30 | + * @throws {TypeError} - If the input is not a string |
11 | 31 | *
|
12 | 32 | * @example
|
13 |
| - * passwordStrengthTester('abc12345'); // Output: 'regular' |
| 33 | + * const options = { |
| 34 | + * isVeryWeak: (password, passwordLength) => passwordLength < 6, |
| 35 | + * isWeak: (password, passwordLength) => passwordLength < 8, |
| 36 | + * isRegular: (password, passwordLength) => passwordLength < 10, |
| 37 | + * isStrong: (password, passwordLength) => passwordLength < 12, |
| 38 | + * isVeryStrong: (password, passwordLength) => passwordLength >= 12, |
| 39 | + * } |
14 | 40 | *
|
15 |
| - * @example |
16 |
| - * passwordStrengthTester('Abc123awdasd'); // Output: 'strong' |
17 |
| - * |
18 |
| - * @example |
19 |
| - * passwordStrengthTester('SuperSecurePassword123!@'); // Output: 'veryStrong' |
| 41 | + * passwordStrengthTester("12345", options); // Very weak |
| 42 | + * passwordStrengthTester("1234567", options); // Weak |
| 43 | + * @returns {string} |
20 | 44 | */
|
21 |
| -function passwordStrengthTester(password: string): string { |
| 45 | +function passwordStrengthTester( |
| 46 | + password: string, |
| 47 | + { |
| 48 | + isVeryWeak, |
| 49 | + isWeak, |
| 50 | + isRegular, |
| 51 | + isStrong, |
| 52 | + isVeryStrong, |
| 53 | + }: PasswordStrengthTesterOptions, |
| 54 | +): string { |
22 | 55 | if (typeof password !== "string") {
|
23 | 56 | throw new TypeError("The input should be a string.");
|
24 | 57 | }
|
25 | 58 | const passwordLength: number = password.length;
|
26 |
| - let strengthType: string; |
| 59 | + let strengthType: PasswordStrengthType; |
27 | 60 |
|
28 | 61 | switch (true) {
|
29 |
| - case isVeryWeak(password, passwordLength) || commonPassword(password): |
30 |
| - strengthType = "veryWeak"; |
| 62 | + case isVeryWeak(password, passwordLength): |
| 63 | + strengthType = "Very weak"; |
31 | 64 | break;
|
32 | 65 |
|
33 | 66 | case isWeak(password, passwordLength):
|
34 |
| - strengthType = "weak"; |
| 67 | + strengthType = "Weak"; |
35 | 68 | break;
|
36 | 69 |
|
37 | 70 | case isRegular(password, passwordLength):
|
38 |
| - strengthType = "regular"; |
39 |
| - break; |
40 |
| - |
41 |
| - case isVeryStrong(password, passwordLength): |
42 |
| - strengthType = "veryStrong"; |
| 71 | + strengthType = "Regular"; |
43 | 72 | break;
|
44 | 73 |
|
45 | 74 | case isStrong(password, passwordLength):
|
46 |
| - strengthType = "strong"; |
| 75 | + strengthType = "Strong"; |
47 | 76 | break;
|
48 | 77 |
|
49 |
| - case isRegular2(password, passwordLength): |
50 |
| - strengthType = "regular"; |
| 78 | + case isVeryStrong(password, passwordLength): |
| 79 | + strengthType = "Very strong"; |
51 | 80 | break;
|
52 | 81 |
|
53 | 82 | default:
|
54 |
| - strengthType = "not classified"; |
| 83 | + strengthType = "Not classified"; |
55 | 84 | break;
|
56 | 85 | }
|
57 | 86 |
|
58 | 87 | return strengthType;
|
59 | 88 | }
|
60 | 89 |
|
61 |
| -function isVeryWeak(password: string, passwordLength: number): boolean { |
62 |
| - return passwordLength <= 5 && /^\d+$/.test(password); |
63 |
| -} |
64 |
| - |
65 |
| -function isWeak(password: string, passwordLength: number): boolean { |
66 |
| - return ( |
67 |
| - (passwordLength <= 5 && /^[a-zA-Z0-9]+$/.test(password)) || |
68 |
| - (passwordLength >= 6 && |
69 |
| - /^[a-zA-Z0-9]+$/.test(password) && |
70 |
| - passwordLength <= 7) || |
71 |
| - (passwordLength < 10 && /(.)\1{3,}/.test(password)) || |
72 |
| - (passwordLength >= 5 && passwordLength <= 8 && /^\d+$/.test(password)) |
73 |
| - ); |
74 |
| -} |
75 |
| - |
76 |
| -function isRegular(password: string, passwordLength: number): boolean { |
77 |
| - return /(.)\1{5,}/.test(password) && passwordLength > 10; |
78 |
| -} |
79 |
| - |
80 |
| -function isVeryStrong(password: string, passwordLength: number): boolean { |
81 |
| - return ( |
82 |
| - passwordLength > 16 || |
83 |
| - (password.length >= 8 && |
84 |
| - /[A-Z]/.test(password) && |
85 |
| - /[a-z]/.test(password) && |
86 |
| - /\d/.test(password) && |
87 |
| - /[\W_]/.test(password)) |
88 |
| - ); |
89 |
| -} |
90 |
| - |
91 |
| -function isRegular2(password: string, passwordLength: number): boolean { |
92 |
| - return ( |
93 |
| - (passwordLength >= 9 && passwordLength <= 12) || |
94 |
| - (password.length >= 6 && |
95 |
| - password.length <= 8 && |
96 |
| - /\d/.test(password) && |
97 |
| - /[a-zA-Z]/.test(password)) |
98 |
| - ); |
99 |
| -} |
100 |
| - |
101 |
| -function isStrong(password: string, passwordLength: number): boolean { |
102 |
| - return ( |
103 |
| - (passwordLength >= 13 && passwordLength <= 16) || |
104 |
| - (password.length >= 8 && |
105 |
| - /[A-Z]/.test(password) && |
106 |
| - /[a-z]/.test(password) && |
107 |
| - /\d/.test(password)) |
108 |
| - ); |
109 |
| -} |
110 |
| - |
111 |
| -function commonPassword(password: string): boolean { |
112 |
| - const commonPasswords: string[] = [ |
113 |
| - "123", |
114 |
| - "1234", |
115 |
| - "12345", |
116 |
| - "123456", |
117 |
| - "1234567", |
118 |
| - "12345678", |
119 |
| - "123456789", |
120 |
| - "password", |
121 |
| - "password", |
122 |
| - "password!", |
123 |
| - "password!1", |
124 |
| - "admin", |
125 |
| - "admin!", |
126 |
| - "Admin", |
127 |
| - "Admin!", |
128 |
| - "admin123", |
129 |
| - "P@ssw0rd", |
130 |
| - "Password", |
131 |
| - "password123", |
132 |
| - "password123!", |
133 |
| - "Qwerty", |
134 |
| - "Qwerty!", |
135 |
| - "Qwerty123", |
136 |
| - "Qwerty123!", |
137 |
| - ]; |
138 |
| - |
139 |
| - return commonPasswords.includes(password); |
140 |
| -} |
141 |
| - |
142 | 90 | export default passwordStrengthTester;
|
0 commit comments