This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
440 lines (398 loc) · 13.8 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
var trees = require('./lib/trees.mod.js');
var words = require('./lib/norvig-10000.mod.js');
var names = require('./lib/all-names.mod.js');
var passwords = require('./lib/passwords-10000.mod.js');
var PasswordChecker = function() {
var self = this;
// Holds errors from the last check
this.errors = [];
this.password = null;
// Defaults
this.allowed_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
this.allowed_numbers = "0123456789";
this.allowed_symbols = "_-!\"?$%^&*()+={}[]:;@'~#|<>,.?\\/ ";
this.words = words;
this.names = names;
this.passwords = passwords;
this.words_tree = trees.arrayToTree(this.words, true, 3);
this.names_tree = trees.arrayToTree(this.names, true, 3);
this.passwords_tree = trees.arrayToTree(this.passwords, true, 3);
this.minimum_length = 0;
this.maximum_length = 0;
// Update word lists
Object.defineProperties(this, {
'min_length': {
set: function(value) {
this.minimum_length = value;
if (value) {
this.addRule('min_length', this.checkMinLength.bind(this));
} else {
delete this.rules.min_length;
}
}
},
'max_length': {
set: function(value) {
this.maximum_length = value;
if (value) {
this.addRule('max_length', this.checkMaxLength.bind(this));
} else {
delete this.rules.max_length;
}
}
},
'disallowed_words': {
set: self.updateList.bind(self, 'words')
},
'disallowed_names': {
set: self.updateList.bind(self, 'names')
},
'disallowed_passwords': {
set: self.updateList.bind(self, 'passwords')
}
});
// Current rules and results of the last check if any
this.rules = {};
};
module.exports = PasswordChecker;
/**
* Update the words in a list and update the _tree version of the list
* @param {string} list_name Name of the list to update
* @param {array} list List of the names that should be in the list
*/
PasswordChecker.prototype.updateList = function(list_name, list) {
this[list_name] = list;
for (var i in this[list_name]) {
this[list_name][i] = this[list_name][i].toLowerCase()
}
this[list_name + '_tree'] = trees.arrayToTree(this[list_name], true);
};
/**
* Check a password by running any rules set
* @param {string} password The password to test
* @param {Function} cb Optional callback function
* @return {boolean} Returns true when all rules pass and false if any fails
*/
PasswordChecker.prototype.check = function(password, cb) {
this.errors = [];
this.password = password;
// if(this.minimum_length) {
// this.addRule('min_length', this.checkMinLength.bind(this));
// } else delete this.rules.min_length;
// if(this.maximum_length) {
// this.addRule('max_length', this.checkMaxLength.bind(this));
// } else delete this.rules.max_length;
for (var i in this.rules) {
var err = this.rules[i].method();
if (err) {
this.errors.push(err);
this.rules[i].failed = true;
this.rules[i].error_message = err.message;
} else {
this.rules[i].failed = false;
}
}
if (cb) {
cb(this.errors.length ? this.errors : null);
}
return this.errors.length ? false : true;
};
PasswordChecker.prototype.addRule = function(name, method) {
if (!name) {
throw new Error('name is needed');
}
if (!method) {
throw new Error('method is needed');
}
this.rules[name] = {
name: name,
method: method,
error_message: null,
failed: null
};
};
/*************************************
* Start of settings for the checker *
*************************************/
/**
* Should check that the password only has letters from allowed_letters in it
* @param {boolean} active true|false
*/
PasswordChecker.prototype.checkLetters = function(active) {
if (active) {
this.addRule('check_letters', this.checkAllowedLetters.bind(this));
} else {
delete this.rules.check_letters;
}
};
/**
* Should check that the password only has numbers from allowed_letters in it
* @param {boolean} active true|false
*/
PasswordChecker.prototype.checkNumbers = function(active) {
if (active) {
this.addRule('check_numbers', this.checkAllowedNumbers.bind(this));
} else {
delete this.rules.check_numbers;
}
};
/**
* Should check that the password only has symbols from allowed_letters in it
* @param {boolean} active true|false
*/
PasswordChecker.prototype.checkSymbols = function(active) {
if (active) {
this.addRule('check_symbols', this.checkAllowedSymbols.bind(this));
} else {
delete this.rules.check_symbols;
}
};
/**
* Should check that the password has letters in it
* @param {boolean} active true|false
*/
PasswordChecker.prototype.requireLetters = function(active) {
if (active) {
this.addRule('require_letters', this.checkRequireLetters.bind(this));
} else {
delete this.rules.require_letters;
}
};
/**
* Should check that the password has numbers in it
* @param {boolean} active true|false
*/
PasswordChecker.prototype.requireNumbers = function(active) {
if (active) {
this.addRule('require_numbers', this.checkRequireNumbers.bind(this));
} else {
delete this.rules.require_numbers;
}
};
/**
* Should check that the password has symbols in it
* @param {boolean} active true|false
*/
PasswordChecker.prototype.requireSymbols = function(active) {
if (active) {
this.addRule('require_symbols', this.checkRequireSymbols.bind(this));
} else {
delete this.rules.require_symbols;
}
};
/**
* Should check that the password has numbers OR symbols in it
* @param {boolean} active true|false
*/
PasswordChecker.prototype.requireNumbersOrSymbols = function(active) {
if (active) {
this.addRule('require_numbers_or_symbols', this.checkRequireNumbersOrSymbols.bind(this));
} else {
delete this.rules.require_numbers_or_symbols;
}
};
/**
* Check the password against the names list
* @param {boolean} active true|false
* @param {boolean} in_password Also check if the words are IN the password
* @param {number} len Minimum length of words to check for if in_password == true
*/
PasswordChecker.prototype.disallowNames = function(active, in_password, len) {
if (active) {
this.addRule('disallow_names', this.checkNames.bind(this, !!in_password, len));
} else {
delete this.rules.disallow_names;
}
};
/**
* Check the password against the words list
* @param {boolean} active true|false
* @param {boolean} in_password Also check if the words are IN the password
* @param {number} len Minimum length of words to check for if in_password == true
*/
PasswordChecker.prototype.disallowWords = function(active, in_password, len) {
if (active) {
this.addRule('disallow_words', this.checkWords.bind(this, !!in_password, len));
} else {
delete this.rules.disallow_words;
}
};
/**
* Check the password against the passwords list
* @param {boolean} active true|false
* @param {boolean} in_password Also check if the words are IN the password
* @param {number} len Minimum length of words to check for if in_password == true
*/
PasswordChecker.prototype.disallowPasswords = function(active, in_password, len) {
len = (typeof len === 'undefined') ? len : 4;
if (active) {
this.addRule('disallow_passwords', this.checkPasswords.bind(this, !!in_password, len));
} else {
delete this.rules.disallow_passwords;
}
};
/***********************************
* End of settings for the checker *
***********************************/
/**************************
* Start of check methods *
**************************/
/**
* Check if the password length is >= minimum_length
* @return {Error} if the length is too short
*/
PasswordChecker.prototype.checkMinLength = function() {
if (this.minimum_length && this.password.length < this.minimum_length) {
return new Error('The password is too short');
}
};
/**
* Check if the password length is <= maximum_length
* @return {Error} if the length is too long
*/
PasswordChecker.prototype.checkMaxLength = function() {
if (this.maximum_length && this.password.length > this.maximum_length) {
return new Error('The password is too long');
}
};
/**
* Check that the password only has allowed letters
* @return {Error} if disallowed letters were found
*/
PasswordChecker.prototype.checkAllowedLetters = function() {
var regex = new RegExp('^[' + this.allowed_letters.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") + ']*$');
if (!regex.test(this.password.replace(/[^a-zA-Z]/g, ""))) {
return new Error('Letters found that are not allowed');
}
};
/**
* Check that the password only has allowed numbers
* @return {Error} if disallowed numbers were found
*/
PasswordChecker.prototype.checkAllowedNumbers = function() {
var regex = new RegExp('^[' + this.allowed_numbers.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") + ']*$');
if (!regex.test(this.password.replace(/[^0-9]/g, ""))) {
return new Error('Numbers found that are not allowed');
}
};
/**
* Check that the password only has allowed symbols
* @return {Error} if disallowed symbols were found
*/
PasswordChecker.prototype.checkAllowedSymbols = function() {
var regex = new RegExp('^[' + this.allowed_symbols.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") + ']*$');
if (!regex.test(this.password.replace(/[a-zA-Z0-9]/g, ""))) {
return new Error('Symbols found that are now allowed');
}
};
/**
* Check that the password has letters
* @return {Error} if no letters were found
*/
PasswordChecker.prototype.checkRequireLetters = function() {
var regex = new RegExp('[' + this.allowed_letters.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") + ']');
if (!regex.test(this.password)) {
return new Error('No letters found');
}
};
/**
* Check that the password has numbers
* @return {Error} if no numbers were found
*/
PasswordChecker.prototype.checkRequireNumbers = function() {
var regex = new RegExp('[' + this.allowed_numbers.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") + ']');
if (!regex.test(this.password)) {
return new Error('No numbers found');
}
};
/**
* Check that the password has symbols
* @return {Error} if no symbols were found
*/
PasswordChecker.prototype.checkRequireSymbols = function() {
var regex = new RegExp('[' + this.allowed_symbols.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") + ']');
if (!regex.test(this.password)) {
return new Error('No symbols found');
}
};
/**
* Check that the password has numbers and/or symbols
* @return {Error} if no numbers and/or symbols were found
*/
PasswordChecker.prototype.checkRequireNumbersOrSymbols = function() {
var regexNumbers = new RegExp('[' + this.allowed_numbers.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") + ']');
var regexSymbols = new RegExp('[' + this.allowed_symbols.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&") + ']');
if (!regexNumbers.test(this.password) && !regexSymbols.test(this.password)) {
return new Error('No numbers or symbols found');
}
};
/**
* Check if password is a name from the disallowed names list
* @param {boolean} in_password Also check if names are in the password
* @param {number} min_word_length Minimum length of words to check for if in_password == true
* @return {Error} If any matches were found
*/
PasswordChecker.prototype.checkNames = function(in_password, min_word_length) {
if (in_password && this.hasWordInList(this.names, min_word_length)) {
return new Error('Password includes name from disallowed list');
} else if (this.wordInList(this.names_tree)) {
return new Error('Password is in disallowed names list');
}
};
/**
* Check if password is a word from the disallowed words list
* @param {boolean} in_password Also check if words are in the password
* @param {number} min_word_length Minimum length of words to check for if in_password == true
* @return {Error} If any matches were found
*/
PasswordChecker.prototype.checkWords = function(in_password, min_word_length) {
if (in_password && this.hasWordInList(this.words, min_word_length)) {
return new Error('Password includes word from disallowed list');
} else if (this.wordInList(this.words_tree)) {
return new Error('Password is in disallowed words list');
}
};
/**
* Check if password is a password from the disallowed passwords list
* @param {boolean} in_password Also check if words are in the password
* @param {number} min_word_length Minimum length of words to check for if in_password == true
* @return {Error} If any matches were found
*/
PasswordChecker.prototype.checkPasswords = function(in_password, min_word_length) {
if (in_password && this.hasWordInList(this.passwords, min_word_length)) {
return new Error('Password includes password from disallowed list');
} else if (this.wordInList(this.passwords_tree)) {
return new Error('Password is in disallowed passwords list');
}
};
/************************
* End of check methods *
************************/
/**
* Check if password is in a list
* @param {array} list The list to check the password against
* @param {number} min_word_length Default is 4 - Only check words of this length
* @return {Boolean} true if in list, false if not in list
*/
PasswordChecker.prototype.hasWordInList = function(list, min_word_length) {
min_word_length = (typeof min_word_length !== 'undefined') ? min_word_length : 4;
var str = this.password.toLowerCase();
for (var i in list) {
if (list[i].length >= min_word_length && str.indexOf(list[i]) !== -1) {
return true;
}
}
return false;
};
/**
* Check if password is in a list
* @param {words list} list_tree The list tree to check the password against
* @return {Boolean} true if in list, false if not in list
*/
PasswordChecker.prototype.wordInList = function(list_tree) {
var str = this.password.toLowerCase();
if (trees.inTree(str, list_tree)) {
return true;
}
return false;
};