Skip to content

Commit

Permalink
Fixed errors testing password with length less than the allowed lengt…
Browse files Browse the repository at this point in the history
…h and allErrors flag to true. Updated and fixed docs and test examples.
  • Loading branch information
Raúl Polanco Montiel committed Jun 18, 2021
1 parent 2717b1b commit 26a84b0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ This is the only case where the `message` property will be an array of strings.
Success result, with weak password:

```js
console.log(passwordStrength('testtest', { minUpperChars: 0, minSpecialChars: 0 }));
console.log(passwordStrength('testtest', { minUpperChars: 0, minSpecialChars: 0, minNumberChars: 0 }));
// {"success":true,"key":"weak","message":"Weak","color":"#c43d4b"}
```

Success result, with average password:

```js
console.log(passwordStrength('testest1', { minUpperChars: 0 }));
console.log(passwordStrength('testest1', { minUpperChars: 0, minSpecialChars: 0 }));
// {"success":true,"key":"average","message":"Average","color":"#cc9900"}
```

Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function test(title, password, options = {}) {
process.stdout.write('\n');
}

// Error examples
test('Empty password', '');
test('Min password length', 'test');
test('Min upper chars', 'testtest', { minUpperChars: 1 });
Expand All @@ -19,7 +20,9 @@ test('Min numbers', 'TestTest', { minNumberChars: 1 });
test('Min special chars', 'TestTest1', { minSpecialChars: 1 });
test('Max consecutive repeating chars', 'TestTTTTest1!', { maxConsecutiveRepeatingChars: 2 });
test('Empty password with all possible errors', '', { allErrors: true });
test('Weak password', 'testtest', { minUpperChars: 0, minNumberChars: 0, minSpecialChars: 0 });
test('Average password', 'testest1', { minUpperChars: 0, minNumberChars: 0, minSpecialChars: 0 });

// Success examples
test('Weak password', 'testtest', { minUpperChars: 0, minSpecialChars: 0, minNumberChars: 0 });
test('Average password', 'testest1', { minUpperChars: 0, minSpecialChars: 0 });
test('Strong password', 'TestTest1!');
test('Secure password', 'Test123%$/)=!');
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/pwd-strength/-/pwd-strength-1.1.1.tgz",
"_resolved": "https://registry.npmjs.org/pwd-strength/-/pwd-strength-1.1.2.tgz",
"_shasum": "1ccc118c7a90b02d22c3ae1815af237321e32878",
"_spec": "pwd-strength",
"_where": "C:\\nodejs\\pwd-strength",
Expand Down Expand Up @@ -57,5 +57,5 @@
"demo": "node ./index.js",
"test": "tape ./test/*spec.js | tap-spec"
},
"version": "1.1.1"
"version": "1.1.2"
}
50 changes: 25 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = function passwordStrength(value, options) {

let defaults = {
debug: false,
debug: false, // Debug mode
allErrors: false, // Return all errors instead the first error found
minUpperChars: 1, // Upper characters ([A-Z])
minLowerChars: 1, // Lower characters ([a-z])
Expand Down Expand Up @@ -76,6 +76,7 @@ module.exports = function passwordStrength(value, options) {
for (let i=0; i < charPassword.length; i++) {
let currentChar = charPassword[i];

// Counting chars
if (currentChar.match(/[A-Z]/g)) {
num.upper++;
} else if (currentChar.match(/[a-z]/g)) {
Expand All @@ -86,15 +87,14 @@ module.exports = function passwordStrength(value, options) {
num.symbols++;
}

if(previousChar !== null && num.repeatingChars <= settings.maxConsecutiveRepeatingChars && charPassword[i] === previousChar) {
if(num.repeatingChars === 0) num.repeatingChars = 1;
num.repeatingChars++;
}
if(num.repeatingChars > settings.maxConsecutiveRepeatingChars) {
continue;
}
if(previousChar !== null && charPassword[i] !== previousChar) {
num.repeatingChars = 0;
// Repeating chars
if (previousChar !== null && num.repeatingChars <= settings.maxConsecutiveRepeatingChars) {
if (charPassword[i] === previousChar) {
if (num.repeatingChars === 0) num.repeatingChars = 1;
num.repeatingChars++;
} else {
num.repeatingChars = 0;
}
}
previousChar = charPassword[i];
}
Expand Down Expand Up @@ -123,7 +123,7 @@ module.exports = function passwordStrength(value, options) {
}

function format(msg, variable) {
if(typeof(msg) === 'string') {
if (typeof(msg) === 'string') {
return msg.replace('%s', variable);
} else {
return '';
Expand Down Expand Up @@ -153,43 +153,43 @@ module.exports = function passwordStrength(value, options) {
color: ''
};

if(strPassword === '') {
if (strPassword === '') {
let message = lang.enterPassword;
result.message.push(message);
result.color = colors.error;
}
if(charPassword.length < settings.minPasswordLength) {
if (charPassword.length < settings.minPasswordLength) {
let message = settings.minPasswordLength !== 1 ? lang.minPasswordChars : lang.minPasswordChar;
result.message.push(format(message, settings.minPasswordLength));
result.color = colors.error;
}
if(num.lower < settings.minLowerChars) {
if (num.lower < settings.minLowerChars) {
let message = settings.minLowerChars !== 1 ? lang.minLowerChars : lang.minLowerChar;
result.message.push(format(message, settings.minLowerChars));
result.color = colors.error;
}
if(num.upper < settings.minUpperChars) {
if (num.upper < settings.minUpperChars) {
let message = settings.minUpperChars !== 1 ? lang.minUpperChars : lang.minUpperChar;
result.message.push(format(message, settings.minUpperChars));
result.color = colors.error;
}
if(num.numbers < settings.minNumberChars) {
if (num.numbers < settings.minNumberChars) {
let message = settings.minNumberChars !== 1 ? lang.minNumberChars : lang.minNumberChar;
result.message.push(format(message, settings.minNumberChars));
result.color = colors.error;
}
if(num.symbols < settings.minSpecialChars) {
if (num.symbols < settings.minSpecialChars) {
let message = settings.minSpecialChars !== 1 ? lang.minSpecialChars : lang.minSpecialChar;
result.message.push(format(message, settings.minSpecialChars));
result.color = colors.error;
}
if(settings.maxConsecutiveRepeatingChars > 1 && num.repeatingChars > settings.maxConsecutiveRepeatingChars) {
if (settings.maxConsecutiveRepeatingChars > 1 && num.repeatingChars > settings.maxConsecutiveRepeatingChars) {
let message = lang.maxConsecutiveRepeatingChars;
result.message.push(format(message, settings.maxConsecutiveRepeatingChars));
result.color = colors.error;
}

if(result.message.length === 0) {
if (result.message.length === 0) {
if (score < 50) {
result.success = true;
result.key = 'weak';
Expand All @@ -213,9 +213,9 @@ module.exports = function passwordStrength(value, options) {
}
}

if(settings.debug) {
if (settings.debug) {

result.debug = 'Base score:' + baseScore + '\n'
result.debug = 'Base score: ' + baseScore + '\n'
+ 'Length bonus: ' + (num.excess*settings.excess) + ' ['+num.excess+'x'+settings.excess+']\n'
+ 'Upper case bonus: ' + (num.upper*settings.minUpperChars) + ' ['+num.upper+'x'+settings.minUpperChars+']\n'
+ 'Lower case bonus: ' + (num.lower*settings.minLowerChars) + ' ['+num.lower+'x'+settings.minLowerChars+']\n'
Expand All @@ -229,24 +229,24 @@ module.exports = function passwordStrength(value, options) {
console.log(result.debug);
}

if(!result.success && !settings.allErrors) {
if (!result.success && !settings.allErrors) {
result.message = result.message.length > 0 ? result.message[0] : '';
}

return result;

}

init();

if (charPassword.length >= settings.minPasswordLength) {
baseScore = 50;
analyzeString();
calcComplexity();
} else {
baseScore = 0;
}

analyzeString();
calcComplexity();

return outputResult();

};
50 changes: 50 additions & 0 deletions test/pwd-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ test('validate min password length', function(t) {
const result = passwordStrength('Test', {
minPasswordLength: 5
});
t.equal(result.key, 'error');
t.equal(result.success, false);
t.end();
});
Expand All @@ -13,6 +14,7 @@ test('validate min lower chars', function(t) {
const result = passwordStrength('TEST', {
minLowerChars: 1
});
t.equal(result.key, 'error');
t.equal(result.success, false);
t.end();
});
Expand All @@ -21,6 +23,7 @@ test('validate min upper chars', function(t) {
const result = passwordStrength('test', {
minUpperChars: 1
});
t.equal(result.key, 'error');
t.equal(result.success, false);
t.end();
});
Expand All @@ -29,6 +32,7 @@ test('validate min numbers', function(t) {
const result = passwordStrength('Test', {
minNumberChars: 1
});
t.equal(result.key, 'error');
t.equal(result.success, false);
t.end();
});
Expand All @@ -44,6 +48,52 @@ test('validate max consecutive repeating chars', function(t) {
const result = passwordStrength('Test', {
maxConsecutiveRepeatingChars: 2
});
t.equal(result.key, 'error');
t.equal(result.success, false);
t.end();
});

test('validate all errors with empty password', function(t) {
const result = passwordStrength('', {
allErrors: true
});
t.equal(result.key, 'error');
t.equal(result.success, false);
t.equal(result.message.length, 6);
t.end();
});

test('validate weak', function(t) {
const result = passwordStrength('testtest', {
minUpperChars: 0,
minSpecialChars: 0,
minNumberChars: 0
});
t.equal(result.key, 'weak');
t.equal(result.success, true);
t.end();
});

test('validate average', function(t) {
const result = passwordStrength('testest1', {
minUpperChars: 0,
minSpecialChars: 0
});
t.equal(result.key, 'average');
t.equal(result.success, true);
t.end();
});

test('validate strong', function(t) {
const result = passwordStrength('TestTest1!');
t.equal(result.key, 'strong');
t.equal(result.success, true);
t.end();
});

test('validate secure', function(t) {
const result = passwordStrength('Test123%$/)=!');
t.equal(result.key, 'secure');
t.equal(result.success, true);
t.end();
});

0 comments on commit 26a84b0

Please sign in to comment.