-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_strong_password.js
152 lines (127 loc) · 3.66 KB
/
is_strong_password.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const Typo = typeof require != 'undefined' ? require('typo-js') : Typo;
/**
* Strong password validation.
*/
class StrongPassword {
/**
* Contructor.
*
* @param {object} params
*/
constructor(params) {
let results;
this.strong = false;
if (!params) {
this.reasonText = 'no params';
return;
}
this.locale = params.locale || 'en_US';
this.password = params.password;
this.minimumLength = params.minimumLength || 10;
this.minimumWords = params.minimumWords || 4;
this.mustHaveNumbers = true;
if (typeof params.numbers == 'boolean') {
this.mustHaveNumbers = params.numbers;
}
this.mustHaveLowerCase = true;
if (typeof params.lowercase == 'boolean') {
this.mustHaveLowerCase = params.lowercase;
}
this.mustHaveUpperCase = true;
if (typeof params.uppercase == 'boolean') {
this.mustHaveUpperCase = params.uppercase;
}
this.dictionaries = params.dictionaries || false;
this.version = '1.1.2';
results = this.check();
this.strong = results.strong;
this.reasonText = results.reason;
}
/**
* Check if the password is strong.
*
* @return {boolean}
*/
check() {
let strong = true;
let reasonText;
let notRealWords = [];
let dictionary;
if (this.dictionaries) {
dictionary = new Typo(this.locale, false, false, {
dictionaryPath: this.dictionaries
});
} else {
dictionary = new Typo(this.locale);
}
if (!this.password) {
return {
strong: false,
reason: 'empty'
};
}
if (this.password.length < this.minimumLength) {
return {
strong: false,
reason: 'to short'
};
}
if (this.password.split(' ').length < this.minimumWords) {
return {
strong: false,
reason: 'too few words'
};
}
if (this.mustHaveNumbers && !this.password.match(/\d+/g)) {
return {
strong: false,
reason: 'has no numbers'
};
}
if (this.mustHaveLowerCase && !this.password.match(/[a-z]/g)) {
return {
strong: false,
reason: 'has no alphabet chars'
};
}
if (this.mustHaveUpperCase && !this.password.match(/[A-Z]/g)) {
return {
strong: false,
reason: 'has no uppercase alphabet chars'
};
}
this.password.split(' ').forEach(function(value) {
var cleanValue = value.replace(/[,.?!]/g, '');
if (!dictionary.check(cleanValue) && !Number.isInteger(cleanValue * 1)) {
notRealWords.push(value);
}
});
if (notRealWords.length > 0) {
reasonText = 'doesnt has a real word(s) [' + (notRealWords.join(', ')) + ']';
strong = false;
}
return {
strong: strong,
reason: reasonText
};
}
/**
* Return if the password is strong.
*
* @return {boolean}
*/
get isStrong() {
return this.strong;
}
/**
* Return the reason if the password is not strong.
*
* @return {boolean}
*/
get reason() {
return this.reasonText;
}
}
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = StrongPassword;
}