-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(isStrongPassword): new validator 🎉 (#1348)
* Add isStrongPassword method * Add tests * update README.md with isStrongPassword * remove console.log * add tests * allow either scoring or minimum requirements * rename threshold to be more descriptive * update isStrongPassword doc * shorten function declaration * fix confusing parameters * combine separate options for more simple usage * remove obsolete tests * remove minstrongscore option * update README * update isStrongPassword signature * Add default args for clarity * Add tests for isStrongPassword scoring * Fix typo
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 deletions.
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,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 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