From 8cabeb55dcfa37f31232baafccf821d2d0bb15c5 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 5 Aug 2017 21:46:29 +0200 Subject: [PATCH] first commit --- jsPasswordChecker.js | 82 ++++++++++++++ test.html | 39 +++++++ testcases.js | 251 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 372 insertions(+) create mode 100644 jsPasswordChecker.js create mode 100644 test.html create mode 100644 testcases.js diff --git a/jsPasswordChecker.js b/jsPasswordChecker.js new file mode 100644 index 0000000..aeca9e6 --- /dev/null +++ b/jsPasswordChecker.js @@ -0,0 +1,82 @@ +console.log("jsPasswordChecker"); + +var PasswordChecker = function(policies) { + this.policies = policies; +} + +PasswordChecker.prototype.policies; + +PasswordChecker.prototype.check = function(password) { + if (this.policies.minLength !== undefined) + this.checkMinLength(password); + if (this.policies.maxLength !== undefined) + this.checkMaxLength(password); + if (this.policies.containsNumbers !== undefined) + this.checkContains(password, this.policies.containsNumbers, /[0-9]/g, "numbers"); + if (this.policies.containsLowerCaseLetters !== undefined) + this.checkContains(password, this.policies.containsLowerCaseLetters, /[a-z]/g, "lowercase characters"); + if (this.policies.containsUpperCaseLetters !== undefined) + this.checkContains(password, this.policies.containsUpperCaseLetters, /[A-Z]/g, "uppercase characters"); + if (this.policies.containsWhiteSpaces !== undefined) + this.checkContains(password, this.policies.containsWhiteSpaces, /\s/g, "whitespaces"); + if (this.policies.containsSpecialCharacters !== undefined) + this.checkContains(password, this.policies.containsSpecialCharacters, /[^a-zA-Z\d\s]/g, "special characters"); + + if (this.policies.dictionary !== undefined) + this.checkDictionary(password); + if (this.policies.maxCharacterRepetition !== undefined) + this.checkCharacterRepetition(password); +} + +PasswordChecker.prototype.checkMinLength = function(password) { + if (password.length < this.policies.minLength) + throw "Password is too short."; +} + +PasswordChecker.prototype.checkMaxLength = function(password) { + if (password.length > this.policies.maxLength) + throw "Password is too long."; +} + +PasswordChecker.prototype.checkContains = function(password, attribute, regex, name) { + var contains = password.match(regex); + if (attribute === true && contains === null) + throw "Password does not contain " + name + "."; + if (attribute === false && contains !== null) + throw "Password does contain " + name + "."; + if (typeof attribute === "number" && attribute > contains.length) + throw "Password contains too few " + name + "."; +} + +PasswordChecker.prototype.checkDictionary = function(password) { + var dictionary = this.policies.dictionary; + if (typeof dictionary !== "object") // array + dictionary = [dictionary]; + + for (var i = 0; i < dictionary.length; i++) { + if (password.toLowerCase().indexOf(dictionary[i].toLowerCase()) >= 0) + throw "Password contains " + dictionary[i].toLowerCase(); + } +} + +PasswordChecker.prototype.checkCharacterRepetition = function(password) { + var repitition = this.policies.maxCharacterRepetition; + + if (password.length > 0 && repitition <= 1) + return; // policies don't make any sense + + var currentChar = password.charAt(0); + var currentRepitition = 1; + + for (var i = 1; i < password.length; i++) { + var tmp = password.charAt(i); + if (tmp === currentChar) { + currentRepitition++; + if (currentRepitition > repitition) + throw "The password contains to many identical characters in a row."; + } else { + currentChar = tmp; + currentRepitition = 1; + } + } +} diff --git a/test.html b/test.html new file mode 100644 index 0000000..c7226c0 --- /dev/null +++ b/test.html @@ -0,0 +1,39 @@ + + + + + + + + diff --git a/testcases.js b/testcases.js new file mode 100644 index 0000000..177fbc1 --- /dev/null +++ b/testcases.js @@ -0,0 +1,251 @@ +console.log("testcases.js"); + +var testcases = [ + { + password: "test", + policies: { + minLength: 8 + }, + result: false + },{ + password: "testtest", + policies: { + minLength: 8 + }, + result: true + },{ + password: "test", + policies: { + maxLength: 8 + }, + result: true + },{ + password: "testtest", + policies: { + maxLength: 7 + }, + result: false + },{ + password: "test", + policies: { + containsNumbers: true + }, + result: false + },{ + password: "test", + policies: { + containsNumbers: false + }, + result: true + },{ + password: "test1", + policies: { + containsNumbers: true + }, + result: true + },{ + password: "test1", + policies: { + containsNumbers: false + }, + result: false + },{ + password: "test1", + policies: { + containsNumbers: 2 + }, + result: false + },{ + password: "test11", + policies: { + containsNumbers: 2 + }, + result: true + },{ + password: "test", + policies: { + containsLowerCaseLetters: true + }, + result: true + },{ + password: "test", + policies: { + containsLowerCaseLetters: false + }, + result: false + },{ + password: "TEST", + policies: { + containsLowerCaseLetters: true + }, + result: false + },{ + password: "TEST", + policies: { + containsLowerCaseLetters: false + }, + result: true + },{ + password: "tesT", + policies: { + containsUpperCaseLetters: true + }, + result: true + },{ + password: "tesT", + policies: { + containsUpperCaseLetters: false + }, + result: false + },{ + password: "test", + policies: { + containsUpperCaseLetters: true + }, + result: false + },{ + password: "test", + policies: { + containsUpperCaseLetters: false + }, + result: true + },{ + password: "test", + policies: { + containsSpecialCharacters: true + }, + result: false + },{ + password: "test", + policies: { + containsSpecialCharacters: false + }, + result: true + },{ + password: "test&", + policies: { + containsSpecialCharacters: true + }, + result: true + },{ + password: "test&", + policies: { + containsSpecialCharacters: false + }, + result: false + },{ + password: "test", + policies: { + containsWhiteSpaces: true + }, + result: false + },{ + password: "test", + policies: { + containsWhiteSpaces: false + }, + result: true + },{ + password: "te st", + policies: { + containsWhiteSpaces: true + }, + result: true + },{ + password: "te st", + policies: { + containsWhiteSpaces: false + }, + result: false + },{ + password: "teest", + policies: { + maxCharacterRepetition: 2 + }, + result: true + },{ + password: "teeest", + policies: { + maxCharacterRepetition: 2 + }, + result: false + },{ + password: "testUserName", + policies: { + dictionary: [ + "UserName" + ] + }, + result: false + },{ + password: "testUseName", + policies: { + dictionary: [ + "UserName" + ] + }, + result: true + }, + + { + password: "RealyG00d&Password", + policies: { + minLength: 10, + containsLowerCaseLetters: true, + containsUpperCaseLetters: true, + containsNumbers: true, + containsSpecialCharacters: true, + maxCharacterRepetition: 2, + dictionary: [ + "username", + "1994" + ] + }, + result: true + },{ + password: "NotSoG00d&Password1994", + policies: { + minLength: 10, + containsLowerCaseLetters: true, + containsUpperCaseLetters: true, + containsNumbers: true, + containsSpecialCharacters: true, + maxCharacterRepetition: 2, + dictionary: [ + "username", + "1994" + ] + }, + result: false + },{ + password: "NotSoG000d&Password", + policies: { + minLength: 10, + containsLowerCaseLetters: true, + containsUpperCaseLetters: true, + containsNumbers: true, + containsSpecialCharacters: true, + maxCharacterRepetition: 2, + dictionary: [ + "username", + "1994" + ] + }, + result: false + },{ + password: "NotSoG00dPassword", + policies: { + minLength: 10, + containsLowerCaseLetters: true, + containsUpperCaseLetters: true, + containsNumbers: true, + containsSpecialCharacters: true, + maxCharacterRepetition: 2, + dictionary: [ + "username", + "1994" + ] + }, + result: false + } +]