validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

[imporve] isStrongPassword

Open Shawon-bsws opened this issue 2 years ago • 4 comments

In the isStrongPassword code you are checking if each type of the character and it's count and then comparing it with the minimum value but it will much more help if you also share the count value of each character is returned. In that case we can use it and make a much more vast error messages for missing the particular character.

shot If the above object is also available to use that will much help.

Shawon-bsws avatar May 13 '22 05:05 Shawon-bsws

Hi all.

Would the team be keen in adding the following to possibly address this issue. Won't break existing functionalities and could be an additional option for individual scores.

Changes under src/lib/isStrongPassword.js

// new function
function scoreResults(analysis, scoringOptions) {
  let points = {
    uniqueCharsPoints: analysis.uniqueChars * scoringOptions.pointsPerUnique,
    pointsPerUniqueCharRepeat: (analysis.length - analysis.uniqueChars) * scoringOptions.pointsPerRepeat,
  };
  if (analysis.lowercaseCount > 0) {
    points.pointsForContainingLower = scoringOptions.pointsForContainingLower;
  }
  if (analysis.uppercaseCount > 0) {
    points.pointsForContainingUpper = scoringOptions.pointsForContainingUpper;
  }
  if (analysis.numberCount > 0) {
    points.pointsForContainingNumber = scoringOptions.pointsForContainingNumber;
  }
  if (analysis.symbolCount > 0) {
    points.pointsForContainingSymbol = 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);
  }
  if (options.returnIndividualScores) { // <---------------------------- new if block using new function
    return scoreResults(analysis, options);
  }
  return analysis.length >= options.minLength
        && analysis.lowercaseCount >= options.minLowercase
        && analysis.uppercaseCount >= options.minUppercase
        && analysis.numberCount >= options.minNumbers
        && analysis.symbolCount >= options.minSymbols;
}

Could raise a PR and add tests for it if interested. Thanks.

kvaithin avatar May 29 '22 16:05 kvaithin

FYI @chriso @profnandaa @ezkemboi @tux-tn

kvaithin avatar Jun 05 '22 18:06 kvaithin

I have resolved the suggestions and added individualScoreResults function.

ajinkyac03 avatar Sep 01 '22 05:09 ajinkyac03