This repository has been archived by the owner on Mar 23, 2022. It is now read-only.
forked from nowsecure/owasp-password-strength-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owasp-password-strength-test.js
183 lines (152 loc) · 5.44 KB
/
owasp-password-strength-test.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
/* globals define */
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.owaspPasswordStrengthTest = factory();
}
}(this, function () {
class OwaspPasswordStrengthTest {
constructor(options = {}) {
this.config = {
allowPassphrases : true,
maxLength : 128,
minLength : 10,
minPhraseLength : 20,
minOptionalTestsToPass : 4,
};
this.configure(options);
this.tests = {
// An array of required tests. A password *must* pass these tests in order
// to be considered strong.
required: [
// enforce a minimum length
(password) => {
if (password.length < this.config.minLength) {
return 'The password must be at least ' + this.config.minLength + ' characters long.';
}
},
// enforce a maximum length
(password) => {
if (password.length > this.config.maxLength) {
return 'The password must be fewer than ' + this.config.maxLength + ' characters.';
}
},
// forbid repeating characters
(password) => {
if (/(.)\1{2,}/.test(password)) {
return 'The password may not contain sequences of three or more repeated characters.';
}
},
],
// An array of optional tests. These tests are "optional" in two senses:
//
// 1. Passphrases (passwords whose length exceeds
// this.configs.minPhraseLength) are not obligated to pass these tests
// provided that this.configs.allowPassphrases is set to Boolean true
// (which it is by default).
//
// 2. A password need only to pass this.configs.minOptionalTestsToPass
// number of these optional tests in order to be considered strong.
optional: [
// require at least one lowercase letter
(password) => {
if (!/[a-z]/.test(password)) {
return 'The password must contain at least one lowercase letter.';
}
},
// require at least one uppercase letter
(password) => {
if (!/[A-Z]/.test(password)) {
return 'The password must contain at least one uppercase letter.';
}
},
// require at least one number
(password) => {
if (!/[0-9]/.test(password)) {
return 'The password must contain at least one number.';
}
},
// require at least one special character
(password) => {
if (!/[^A-Za-z0-9]/.test(password)) {
return 'The password must contain at least one special character.';
}
},
],
};
}
configure(options) {
for (let prop in options) {
if (options.hasOwnProperty(prop) && this.config.hasOwnProperty(prop)) {
this.config[prop] = options[prop];
}
}
}
test(password) {
// create an object to store the test results
let result = {
errors : [],
failedTests : [],
passedTests : [],
requiredTestErrors : [],
optionalTestErrors : [],
isPassphrase : false,
strong : true,
optionalTestsPassed : 0,
};
// Always submit the password/passphrase to the required tests
let i = 0;
this.tests.required.forEach(function(test) {
let err = test(password);
if (typeof err === 'string') {
result.strong = false;
result.errors.push(err);
result.requiredTestErrors.push(err);
result.failedTests.push(i);
} else {
result.passedTests.push(i);
}
i++;
});
// If configured to allow passphrases, and if the password is of a
// sufficient length to consider it a passphrase, exempt it from the
// optional tests.
if (
this.config.allowPassphrases === true &&
password.length >= this.config.minPhraseLength
) {
result.isPassphrase = true;
}
if (!result.isPassphrase) {
let j = this.tests.required.length;
this.tests.optional.forEach(function(test) {
let err = test(password);
if (typeof err === 'string') {
result.errors.push(err);
result.optionalTestErrors.push(err);
result.failedTests.push(j);
} else {
result.optionalTestsPassed++;
result.passedTests.push(j);
}
j++;
});
}
// If the password is not a passphrase, assert that it has passed a
// sufficient number of the optional tests, per the configuration
if (
!result.isPassphrase &&
result.optionalTestsPassed < this.config.minOptionalTestsToPass
) {
result.strong = false;
}
// return the result
return result;
};
}
return OwaspPasswordStrengthTest;
}
));