diff --git a/README.md b/README.md index f05a3cf..c2387b6 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Here is a list of the settings currently available: Setting | Default | Description ---------------------------- | ----------- | ------------------------------------------------------------- debug | `false` | Enable debug mode +allErrors | `false` | Show all errors found as an array instead the first one as string minUpperChars | `1` | Minimum uppercase characters required `[A-Z]`. minLowerChars | `1` | Minimum lowercase characters required `[a-z]`. minNumberChars | `1` | Minimum numbers required `[0-9]`. @@ -36,6 +37,7 @@ weak | *Weak* average | *Average* strong | *Strong* secure | *Secure* +enterPassword | *Enter a password* minPasswordChar | *At least %s character please* minPasswordChars | *At least %s characters please* minLowerChar | *At least %s lowercase character please* @@ -70,16 +72,47 @@ secure | #007000 The function returns an object as follows: +### Error response + +```js + { + "success": false, // any error occurs + "key": "error", // Indicates error + "message": "First error message found", // Multilanguage string with the error message + "color": "#rrggbb" // Hex color depending on the key + } +``` + +### Error response with `allErrors: true` setting + +```js + { + "success": false, // any error occurs + "key": "error", // Indicates error + "message": ["Error message 1", "Error message 2", "Error message 3", ...], // Multilanguage array of strings with the error messages + "color": "#rrggbb" // Hex color depending on the key + } +``` + +### Success response + ```js { - "success":false|true, // false if any error occurs, otherwise true - "key":"error|weak|average|strong|secure", // Indicates error or the strength - "message":"Error message or strength", // Multilanguage string with the error message or strength - "color":"#rrggbb" // Hex color depending on the key + "success": true, // no errors + "key": "weak|average|strong|secure", // Indicates the strength + "message": "Strength", // Multilanguage string with the strength + "color": "#rrggbb" // Hex color depending on the key } ``` -## Examples +## Error examples + +Default settings, the first error checked will be the empty password: + +```js + console.log(passwordStrength('')); + // {"success":false,"key":"error","message":"Enter a password","color":"#ee0000"} +``` Default settings, the first error checked will be the password length: @@ -123,6 +156,16 @@ Maximum consecutive repeating characters allowed: // {"success":false,"key":"error","message":"No more than 2 consecutive repeating characters or numbers please","color":"#ee0000"} ``` +With `allErrors: true` setting and the empty password, all possible errors will be shown, only in an error occurs (`success: false` in the response). +This is the only case where the `message` property will be an array of strings. + +```js + console.log(passwordStrength('', { allErrors: true })); + // {"success":false,"key":"error","message":["Enter a password","At least 8 characters please","At least 1 lowercase character please","At least 1 uppercase character please","At least 1 number please","At least 1 special character please"],"color":"#ee0000"} +``` + +## Success examples + Success result, with weak password: ```js diff --git a/index.js b/index.js index 54eea2b..aa567b5 100644 --- a/index.js +++ b/index.js @@ -11,12 +11,14 @@ function test(title, password, options = {}) { process.stdout.write('\n'); } +test('Empty password', ''); test('Min password length', 'test'); test('Min upper chars', 'testtest', { minUpperChars: 1 }); test('Min lower chars', 'TESTTEST', { minLowerChars: 1 }); 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 }); test('Strong password', 'TestTest1!'); diff --git a/package.json b/package.json index 2aea0b9..a5c1298 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "#USER", "/" ], - "_resolved": "https://registry.npmjs.org/pwd-strength/-/pwd-strength-1.0.6.tgz", + "_resolved": "https://registry.npmjs.org/pwd-strength/-/pwd-strength-1.1.1.tgz", "_shasum": "1ccc118c7a90b02d22c3ae1815af237321e32878", "_spec": "pwd-strength", "_where": "C:\\nodejs\\pwd-strength", @@ -57,5 +57,5 @@ "demo": "node ./index.js", "test": "tape ./test/*spec.js | tap-spec" }, - "version": "1.0.6" + "version": "1.1.1" } diff --git a/src/index.js b/src/index.js index 7b92383..eea95d8 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ module.exports = function passwordStrength(value, options) { let defaults = { debug: false, + allErrors: false, // Return all errors instead the first error found minUpperChars: 1, // Upper characters ([A-Z]) minLowerChars: 1, // Lower characters ([a-z]) minNumberChars: 1, // Numbers ([0-9]) @@ -148,57 +149,68 @@ module.exports = function passwordStrength(value, options) { let result = { success: false, key: 'error', - message: '', + message: [], color: '' }; - if (value === '') { - result.message = settings.enterPassword; + if(strPassword === '') { + let message = lang.enterPassword; + result.message.push(message); result.color = colors.error; - } else if (charPassword.length < settings.minPasswordLength) { - result.message = settings.minPasswordLength !== 1 ? lang.minPasswordChars : lang.minPasswordChar; - result.message = format(result.message, 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; - } else if (num.lower < settings.minLowerChars) { - result.message = settings.minLowerChars !== 1 ? lang.minLowerChars : lang.minLowerChar; - result.message = format(result.message, 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; - } else if (num.upper < settings.minUpperChars) { - result.message = settings.minUpperChars !== 1 ? lang.minUpperChars : lang.minUpperChar; - result.message = format(result.message, 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; - } else if (num.numbers < settings.minNumberChars) { - result.message = settings.minNumberChars !== 1 ? lang.minNumberChars : lang.minNumberChar; - result.message = format(result.message, 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; - } else if (num.symbols < settings.minSpecialChars) { - result.message = settings.minSpecialChars !== 1 ? lang.minSpecialChars : lang.minSpecialChar; - result.message = format(result.message, 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; - } else if (settings.maxConsecutiveRepeatingChars > 1 && num.repeatingChars > settings.maxConsecutiveRepeatingChars) { - result.message = lang.maxConsecutiveRepeatingChars; - result.message = format(result.message, 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; - } else if (score < 50) { - result.success = true; - result.key = 'weak'; - result.message = lang.weak; - result.color = colors.weak; - } else if (score >= 50 && score < 75) { - result.success = true; - result.key = 'average'; - result.message = lang.average; - result.color = colors.average; - } else if (score >= 75 && score < 100) { - result.success = true; - result.key = 'strong'; - result.message = lang.strong; - result.color = colors.strong; - } else if (score >= 100) { - result.success = true; - result.key = 'secure'; - result.message = lang.secure; - result.color = colors.secure; + } + + if(result.message.length === 0) { + if (score < 50) { + result.success = true; + result.key = 'weak'; + result.message = lang.weak; + result.color = colors.weak; + } else if (score >= 50 && score < 75) { + result.success = true; + result.key = 'average'; + result.message = lang.average; + result.color = colors.average; + } else if (score >= 75 && score < 100) { + result.success = true; + result.key = 'strong'; + result.message = lang.strong; + result.color = colors.strong; + } else if (score >= 100) { + result.success = true; + result.key = 'secure'; + result.message = lang.secure; + result.color = colors.secure; + } } if(settings.debug) { @@ -217,6 +229,10 @@ module.exports = function passwordStrength(value, options) { console.log(result.debug); } + if(!result.success && !settings.allErrors) { + result.message = result.message.length > 0 ? result.message[0] : ''; + } + return result; }