-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
79 lines (72 loc) · 1.91 KB
/
mod.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
export interface IParams {
password: string;
minLen?: number;
maxLen?: number;
containsNum?: boolean;
containsSpecialChar?: boolean;
containsAlphabet?: boolean;
checkWithCommonPasswords?: boolean;
}
export interface VerificationResult {
isValid: boolean;
reason?: string;
}
const url =
"https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10k-most-common.txt";
const response = await fetch(
url,
);
const passwordList = await response.text();
export function checkPasswordWithResult({
password,
minLen = 0,
maxLen = 0,
containsNum = true,
containsSpecialChar = true,
containsAlphabet = true,
checkWithCommonPasswords = false,
}: IParams): VerificationResult {
if (minLen != 0 && password.length < minLen) {
return {
isValid: false,
reason: `The password should contain at least ${minLen} characters`,
};
}
if (maxLen != 0 && password.length > maxLen) {
return {
isValid: false,
reason: `The password should contain at most ${maxLen} characters`,
};
}
if (containsNum && password.search(/\d/) == -1) {
return {
isValid: false,
reason: "The password should contain at least one digit",
};
}
if (containsSpecialChar && password.search(/[^\w\s]/) == -1) {
return {
isValid: false,
reason: "The password should contain at least one special character",
};
}
if (containsAlphabet && password.search(/[A-Za-z]/) == -1) {
return {
isValid: false,
reason: "The password should contain at least one letter",
};
}
if (
checkWithCommonPasswords && passwordList != undefined &&
passwordList.includes(password)
) {
return {
isValid: false,
reason: "The password should not be too common",
};
}
return { isValid: true };
}
export function checkPassword(params: IParams): boolean {
return checkPasswordWithResult(params).isValid;
}