Password Sheriff
Node.js (and browserify supported) library to enforce password policies.
Install
npm install password-sheriff
Usage
var PasswordPolicy = require('password-sheriff').PasswordPolicy;
// Create a length password policy
var lengthPolicy = new PasswordPolicy({length: {minLength: 6}});
// will throw as the password does not meet criteria
lengthPolicy.assert('hello');
// returns false if password does not meet rules
assert.equal(false, lengthPolicy.check('hello'));
// explains the policy
var explained = lengthPolicy.explain();
assert.equal(1, explained.length);
// easier i18n
assert.equal('lengthAtLeast', explained[0].code);
assert.equal('At least 6 characters in length',
format(explained[0].message, explained[0].format));
API
Password Rules
Password Rules are objects that implement the following methods:
-
rule.validate(options)
: method called after the rule was created in order to validateoptions
arguments. -
rule.assert(options, password)
: returns true ifpassword
is valid. -
rule.explain(options)
: returns an object withcode
,message
andformat
attributes:-
code
: Identifier of the rule. This attribute is meant to aid i18n. -
message
: Description of the rule that must be formatted usingutil.format
. -
format
: Array ofstring
orNumber
that will be used for the replacements required inmessage
.
-
-
rule.missing(options, password)
: returns an object similar torule.explain
plus an additional fieldverified
that informs whether the password meets the rule.
Example of rule.explain
method:
FooRule.prototype.explain = function (options) {
return {
// identifier rule (to make i18n easier)
code: 'foo',
message: 'Foo should be present at least %d times.',
format: [options.count]
};
};
When explained:
var explained = fooRule.explain({count: 5});
// "Foo should be present at least 5 times"
util.format(explained.message, explained.format[0]);
See the custom-rule example section for more information.
Built-in Password Rules
Password Sheriff includes some default rules:
-
length
: The minimum amount of characters a password must have.
var lengthPolicy = new PasswordPolicy({length: {minLength: 3}});
-
contains
: Password should contain all of the charsets specified. There are 4 predefined charsets:upperCase
,lowerCase
,numbers
andspecialCharacters
(specialCharacters
are the ones defined in OWASP Password Policy recommendation document).
var charsets = require('password-sheriff').charsets;
var containsPolicy = new PasswordPolicy({contains: {
expressions: [charsets.upperCase, charsets.numbers]
}});
-
containsAtLeast
: Passwords should contain at leastatLeast
of a total ofexpressions.length
groups.
var charsets = require('password-sheriff').charsets;
var containsAtLeastPolicy = new PasswordPolicy({
containsAtLeast: {
atLeast: 2,
expressions: [ charsets.lowerCase, charsets.upperCase, charsets.numbers ]
}
});
-
identicalChars
: Passwords should not contain any character repeated continuouslymax + 1
times.
var identitcalCharsPolicy = new PasswordPolicy({
identicalChars: {
max: 3
}
});
See the default-rules example section for more information.
Issue Reporting
If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
Author
License
This project is licensed under the MIT license. See the LICENSE file for more info.