-
-
Notifications
You must be signed in to change notification settings - Fork 162
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 password complexity checks for openhab-cloud account #80
Merged
+140
−7
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
var User = require('./models/user'); | ||
|
||
/** | ||
* A helper class for setting the password of an arbitrary user. | ||
* | ||
* @param user The user where the password change attempts should happen | ||
* @constructor | ||
*/ | ||
var UserPassword = function(user) { | ||
if (!(user instanceof User)) { | ||
throw new Error('UserPassword can only be instantiated with an User object, ' + | ||
user.constructor + ' given.'); | ||
} | ||
this.user = user; | ||
}; | ||
|
||
/** | ||
* Verifies the password complexity rules for the given plain text password and, if it | ||
* matches the requirements, sets the new password to the given user. | ||
* | ||
* The complexity rules includes: | ||
* - at least 4 characters | ||
* - has at least two characters of the following categories: uppercase letters, lowercase letters, numbers, special characters | ||
* | ||
* @param {string} password | ||
* @param fn Optional function to pass to the save function of User | ||
* @return {boolean} True, if saving the password was initiated, false otherwise. False is garantueed to mean, that the | ||
* password does not meet the complexity requirements. | ||
*/ | ||
UserPassword.prototype.setPassword = function(password, fn) { | ||
if (!UserPassword.isComplexEnough(password)) { | ||
return false; | ||
} | ||
|
||
this.user.password = password; | ||
if (typeof fn !== 'function') { | ||
fn = undefined; | ||
} | ||
this.user.save(fn); | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Helper function for checking the complexity of a password. | ||
* | ||
* @param password | ||
* @returns {boolean} | ||
*/ | ||
UserPassword.isComplexEnough = function(password) { | ||
var matches = 0; | ||
|
||
if (password.length < 4) { | ||
return false; | ||
} | ||
|
||
// match against all lowercase letters | ||
if (password.match(/^(?=.*[a-z]).+$/)) { | ||
matches++; | ||
} | ||
|
||
// match against all uppercase letters | ||
if (password.match(/^(?=.*[A-Z]).+$/)) { | ||
matches++; | ||
} | ||
|
||
// match against all numbers | ||
if (password.match(/^(?=.*\d).+$/)) { | ||
matches++; | ||
} | ||
|
||
// match against a reasonable set of special characters | ||
if (password.match(/^(?=.*[-+_!@#$%^&*.,?]).+$/)) { | ||
matches++; | ||
} | ||
|
||
if (matches < 2) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Prints a message to the given request, that the entered password was not complex enough, while giving the complexity | ||
* rules for passwords. | ||
* | ||
* @param req | ||
*/ | ||
UserPassword.printPasswordNotComplexEnoughError = function(req) { | ||
var message = []; | ||
message.push('The password does not meet the password complexity requirements. Please try again.'); | ||
message.push('Your password must be at least 4 characters long and need to contain at least 2 different characters from the following groups:'); | ||
message.push(' * Lowercase letters (a, b, c, ...)'); | ||
message.push(' * Uppercase letters (A, B, C, ...)'); | ||
message.push(' * Numbers (1, 2, 3, ...)'); | ||
message.push(' * Special characters out of: -+_!@#$%^&*.,?'); | ||
|
||
req.flash('error', message); | ||
}; | ||
|
||
module.exports = UserPassword; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be the place where a user selected a password during registration, isn't it?