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

Commit

Permalink
Repeating Characters condition
Browse files Browse the repository at this point in the history
Added a regular expression test to the while condition, in order to
ensure no repeat characters are present in the generated password.
  • Loading branch information
mleanos committed Sep 22, 2015
1 parent 1c7d742 commit 3d37e20
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions modules/users/server/models/user.server.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ UserSchema.statics.findUniqueUsername = function (username, suffix, callback) {
UserSchema.statics.generateRandomPassphrase = function () {
return new Promise(function (resolve, reject) {
var password = '';
var repeatingCharacters = new RegExp('(.)\\1{2,}', 'g');

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

// check if we need to remove any repeating characters.
password = password.replace(/(.)\1{2,}/g, '');
// check if we need to remove any repeating characters.
password = password.replace(repeatingCharacters, '');
}

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

0 comments on commit 3d37e20

Please sign in to comment.