Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add i18n 3rd party lib support #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ owasp.config({
minLength : 10,
minPhraseLength : 20,
minOptionalTestsToPass : 4,
i18nErrorKeys : false,
});
```

Expand All @@ -184,6 +185,12 @@ Whereby:
OWASP guidelines), four optional complexity tests are made, and a password
must pass at least three of them in order to be considered "strong".

- `i18nErrorKeys` is a `boolean` that toggles the i18n error keys in place of
english error messages. This can be useful when translating the errors using
a 3rd party i18n library. When true the following keys can be returned:
`failedMinLength`, `failedMaxLength`, `failedThreeRepeatedChars`,
`optionalLowercaseRequired`, `optionalUppercaseRequired`,
`optionalNumberRequired` and `optionalSpecialCharRequired`.

Extending
---------
Expand Down
29 changes: 22 additions & 7 deletions owasp-password-strength-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
minLength : 10,
minPhraseLength : 20,
minOptionalTestsToPass : 4,
i18nErrorKeys : false,
};

// This method makes it more convenient to set config parameters
Expand All @@ -42,21 +43,27 @@
// enforce a minimum length
function(password) {
if (password.length < owasp.configs.minLength) {
return 'The password must be at least ' + owasp.configs.minLength + ' characters long.';
return owasp.configs.i18nErrorKeys
? 'failedMinLength'
: 'The password must be at least ' + owasp.configs.minLength + ' characters long.';
}
},

// enforce a maximum length
function(password) {
if (password.length > owasp.configs.maxLength) {
return 'The password must be fewer than ' + owasp.configs.maxLength + ' characters.';
return owasp.configs.i18nErrorKeys
? 'failedMaxLength'
: 'The password must be fewer than ' + owasp.configs.maxLength + ' characters.';
}
},

// forbid repeating characters
function(password) {
if (/(.)\1{2,}/.test(password)) {
return 'The password may not contain sequences of three or more repeated characters.';
return owasp.configs.i18nErrorKeys
? 'failedThreeRepeatedChars'
: 'The password may not contain sequences of three or more repeated characters.';
}
},

Expand All @@ -76,28 +83,36 @@
// require at least one lowercase letter
function(password) {
if (!/[a-z]/.test(password)) {
return 'The password must contain at least one lowercase letter.';
return owasp.configs.i18nErrorKeys
? 'optionalLowercaseRequired'
: 'The password must contain at least one lowercase letter.';
}
},

// require at least one uppercase letter
function(password) {
if (!/[A-Z]/.test(password)) {
return 'The password must contain at least one uppercase letter.';
return owasp.configs.i18nErrorKeys
? 'optionalUppercaseRequired'
: 'The password must contain at least one uppercase letter.';
}
},

// require at least one number
function(password) {
if (!/[0-9]/.test(password)) {
return 'The password must contain at least one number.';
return owasp.configs.i18nErrorKeys
? 'optionalNumberRequired'
: 'The password must contain at least one number.';
}
},

// require at least one special character
function(password) {
if (!/[^A-Za-z0-9]/.test(password)) {
return 'The password must contain at least one special character.';
return owasp.configs.i18nErrorKeys
? 'optionalSpecialCharRequired'
: 'The password must contain at least one special character.';
}
},

Expand Down
23 changes: 23 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ describe('configs', function() {
minLength : 5,
minPhraseLength : 5,
minOptionalTestsToPass : 5,
i18nErrorKeys : true,
});
owasp.configs.allowPassphrases.should.be.false;
owasp.configs.maxLength.should.be.exactly(5);
owasp.configs.minLength.should.be.exactly(5);
owasp.configs.minPhraseLength.should.be.exactly(5);
owasp.configs.minOptionalTestsToPass.should.be.exactly(5);
owasp.configs.i18nErrorKeys.should.be.true;
});

it('should reject invalid parameter keys', function() {
Expand All @@ -138,3 +140,24 @@ describe('configs', function() {
});

});

describe('i18n', function() {

it('should return i18 error keys', function() {
owasp.config({
allowPassphrases : true,
maxLength : 50,
minLength : 10,
minPhraseLength : 20,
minOptionalTestsToPass : 3,
i18nErrorKeys : true,
});
var result = owasp.test('L0eSex');
result.errors.should.have.length(2);
result.requiredTestErrors.should.have.length(1);
result.requiredTestErrors[0].should.be.exactly('failedMinLength');
owasp.configs.minLength.should.be.exactly(10);
result.optionalTestErrors[0].should.be.exactly('optionalSpecialCharRequired');
});

});