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.
This commit is contained in:
mleanos
2015-09-22 04:02:31 -07:00
parent 1c7d74298b
commit 3d37e20128

View File

@@ -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
@@ -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