-
Notifications
You must be signed in to change notification settings - Fork 56
/
owasp-password-strength-test.js
178 lines (153 loc) · 5.29 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
/* 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 () {
var owasp = {};
// These are configuration settings that will be used when testing password
// strength
owasp.configs = {
allowPassphrases : true,
maxLength : 128,
minLength : 10,
minPhraseLength : 20,
minOptionalTestsToPass : 4,
};
// This method makes it more convenient to set config parameters
owasp.config = function(params) {
for (var prop in params) {
if (params.hasOwnProperty(prop) && this.configs.hasOwnProperty(prop)) {
this.configs[prop] = params[prop];
}
}
};
// This is an object containing the tests to run against all passwords.
owasp.tests = {
// An array of required tests. A password *must* pass these tests in order
// to be considered strong.
required: [
// enforce a minimum length
function(password) {
if (password.length < owasp.configs.minLength) {
return 'The password must be at least ' + owasp.configs.minLength + ' characters long.';
}
},
// enforce a maximum length
function(password) {
if (password.length > owasp.configs.maxLength) {
return 'The password must be fewer than ' + owasp.configs.maxLength + ' characters.';
}
},
// forbid repeating characters
function(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
function(password) {
if (!/[a-z]/.test(password)) {
return 'The password must contain at least one lowercase letter.';
}
},
// require at least one uppercase letter
function(password) {
if (!/[A-Z]/.test(password)) {
return 'The password must contain at least one uppercase letter.';
}
},
// require at least one number
function(password) {
if (!/[0-9]/.test(password)) {
return 'The password must contain at least one number.';
}
},
// require at least one special character
function(password) {
if (!/[^A-Za-z0-9]/.test(password)) {
return 'The password must contain at least one special character.';
}
},
],
};
// This method tests password strength
owasp.test = function(password) {
// create an object to store the test results
var result = {
errors : [],
failedTests : [],
passedTests : [],
requiredTestErrors : [],
optionalTestErrors : [],
isPassphrase : false,
strong : true,
optionalTestsPassed : 0,
};
// Always submit the password/passphrase to the required tests
var i = 0;
this.tests.required.forEach(function(test) {
var 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.configs.allowPassphrases === true &&
password.length >= this.configs.minPhraseLength
) {
result.isPassphrase = true;
}
if (!result.isPassphrase) {
var j = this.tests.required.length;
this.tests.optional.forEach(function(test) {
var 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.configs.minOptionalTestsToPass
) {
result.strong = false;
}
// return the result
return result;
};
return owasp;
}
));