-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
add isStrongPassword method #1348
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b830f3a
Add isStrongPassword method
tbeeck f815e4b
Add tests
tbeeck 5a112a0
update README.md with isStrongPassword
tbeeck 78ae116
remove console.log
tbeeck f9330e6
Merge remote-tracking branch 'upstream/master' into strong-password
tbeeck 7100af8
add tests
tbeeck 2efe12b
allow either scoring or minimum requirements
tbeeck aed4a22
rename threshold to be more descriptive
tbeeck 21f0b26
update isStrongPassword doc
tbeeck a55d712
shorten function declaration
tbeeck 97205aa
fix confusing parameters
tbeeck 16e98a2
Merge branch 'master' into strong-password
tbeeck d78eae1
Merge branch 'master' into strong-password
tbeeck 9f9e68e
Merge branch 'master' into strong-password
tbeeck a5971b1
Merge branch 'strong-password' of github.com:door-bell/validator.js i…
tbeeck 1669bfa
combine separate options for more simple usage
tbeeck d1aedcc
remove obsolete tests
tbeeck 23ebf82
remove minstrongscore option
tbeeck 883f544
update README
tbeeck a85ea30
update isStrongPassword signature
tbeeck 19baf6d
Add default args for clarity
tbeeck 9e36980
Add tests for isStrongPassword scoring
tbeeck 8ab1d36
Fix typo
tbeeck 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 hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
| import merge from './util/merge'; | ||
| import assertString from './util/assertString'; | ||
|
|
||
| const upperCaseRegex = /^[A-Z]$/; | ||
| const lowerCaseRegex = /^[a-z]$/; | ||
| const numberRegex = /^[0-9]$/; | ||
| const symbolRegex = /^[-#!$%^&*()_+|~=`{}\[\]:";'<>?,.\/ ]$/; | ||
|
|
||
| const defaultOptions = { | ||
| minLength: 8, | ||
| minLowercase: 1, | ||
| minUppercase: 1, | ||
| minNumbers: 1, | ||
| minSymbols: 1, | ||
| returnScore: false, | ||
| pointsPerUnique: 1, | ||
| pointsPerRepeat: 0.5, | ||
| pointsForContainingLower: 10, | ||
| pointsForContainingUpper: 10, | ||
| pointsForContainingNumber: 10, | ||
| pointsForContainingSymbol: 10, | ||
| }; | ||
|
|
||
| /* Counts number of occurrences of each char in a string | ||
| * could be moved to util/ ? | ||
| */ | ||
| function countChars(str) { | ||
| let result = {}; | ||
| Array.from(str).forEach((char) => { | ||
| let curVal = result[char]; | ||
| if (curVal) { | ||
| result[char] += 1; | ||
| } else { | ||
| result[char] = 1; | ||
| } | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| /* Return information about a password */ | ||
| function analyzePassword(password) { | ||
| let charMap = countChars(password); | ||
| let analysis = { | ||
| length: password.length, | ||
| uniqueChars: Object.keys(charMap).length, | ||
| uppercaseCount: 0, | ||
| lowercaseCount: 0, | ||
| numberCount: 0, | ||
| symbolCount: 0, | ||
| }; | ||
| Object.keys(charMap).forEach((char) => { | ||
| if (upperCaseRegex.test(char)) { | ||
| analysis.uppercaseCount += charMap[char]; | ||
| } else if (lowerCaseRegex.test(char)) { | ||
| analysis.lowercaseCount += charMap[char]; | ||
| } else if (numberRegex.test(char)) { | ||
| analysis.numberCount += charMap[char]; | ||
| } else if (symbolRegex.test(char)) { | ||
| analysis.symbolCount += charMap[char]; | ||
| } | ||
| }); | ||
| return analysis; | ||
| } | ||
|
|
||
| function scorePassword(analysis, scoringOptions) { | ||
| let points = 0; | ||
| points += analysis.uniqueChars * scoringOptions.pointsPerUnique; | ||
| points += (analysis.length - analysis.uniqueChars) * scoringOptions.pointsPerRepeat; | ||
| if (analysis.lowercaseCount > 0) { | ||
| points += scoringOptions.pointsForContainingLower; | ||
| } | ||
| if (analysis.uppercaseCount > 0) { | ||
| points += scoringOptions.pointsForContainingUpper; | ||
| } | ||
| if (analysis.numberCount > 0) { | ||
| points += scoringOptions.pointsForContainingNumber; | ||
| } | ||
| if (analysis.symbolCount > 0) { | ||
| points += scoringOptions.pointsForContainingSymbol; | ||
| } | ||
| return points; | ||
| } | ||
|
|
||
| export default function isStrongPassword(str, options = null) { | ||
| assertString(str); | ||
| const analysis = analyzePassword(str); | ||
| options = merge(options || {}, defaultOptions); | ||
| if (options.returnScore) { | ||
| return scorePassword(analysis, options); | ||
| } | ||
| return analysis.length >= options.minLength | ||
| && analysis.lowercaseCount >= options.minLowercase | ||
| && analysis.uppercaseCount >= options.minUppercase | ||
| && analysis.numberCount >= options.minNumbers | ||
| && analysis.symbolCount >= options.minSymbols; | ||
| } |
This file contains hidden or 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 hidden or 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.
There is a need for tests that pass
returnScore as an optionThere 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.
Added the tests for this case in the
sanitizers.jstest file since with this option, the function essentially becomes a sanitizer, turning the string into a number. Hope that makes sense