Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 3d37e20

Browse files
committed
Repeating Characters condition
Added a regular expression test to the while condition, in order to ensure no repeat characters are present in the generated password.
1 parent 1c7d742 commit 3d37e20

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

modules/users/server/models/user.server.model.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,11 @@ UserSchema.statics.findUniqueUsername = function (username, suffix, callback) {
175175
UserSchema.statics.generateRandomPassphrase = function () {
176176
return new Promise(function (resolve, reject) {
177177
var password = '';
178+
var repeatingCharacters = new RegExp('(.)\\1{2,}', 'g');
178179

179180
// iterate until the we have a valid passphrase.
180181
// NOTE: Should rarely iterate more than once, but we need this to ensure no repeating characters are present.
181-
while (password.length < 20) {
182+
while (password.length < 20 || repeatingCharacters.test(password)) {
182183
// build the random password
183184
password = generatePassword.generate({
184185
length: Math.floor(Math.random() * (20)) + 20, // randomize length between 20 and 40 characters
@@ -188,8 +189,8 @@ UserSchema.statics.generateRandomPassphrase = function () {
188189
excludeSimilarCharacters: true,
189190
});
190191

191-
// check if we need to remove any repeating characters.
192-
password = password.replace(/(.)\1{2,}/g, '');
192+
// check if we need to remove any repeating characters.
193+
password = password.replace(repeatingCharacters, '');
193194
}
194195

195196
// Send the rejection back if the passphrase fails to pass the strength test

0 commit comments

Comments
 (0)