You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PasswordStrength.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { useEffect, useRef, useState } from 'react';
  2. import owasp from 'owasp-password-strength-test';
  3. import i18next from 'i18next';
  4. owasp.config({
  5. minOptionalTestsToPass: 3,
  6. });
  7. const passwordStrengthOptions = [
  8. {
  9. strength: 'weak',
  10. color: '#FF5028',
  11. },
  12. {
  13. strength: 'average',
  14. color: '#FDB942',
  15. },
  16. {
  17. strength: 'good',
  18. color: '#06BEE7',
  19. },
  20. {
  21. strength: 'strong',
  22. color: '#00876A',
  23. },
  24. ];
  25. /**
  26. * User must pass a required test and at least 3 optional.
  27. * @param result - owasp result
  28. * @returns {number} - index of password strength 0-3
  29. */
  30. function getPasswordStrengthIndex(result) {
  31. // requirement for strong password is required test passed and at least 3 optional tests
  32. if (result.strong) {
  33. return 3;
  34. }
  35. if (!result.strong && result.optionalTestsPassed >= 3) {
  36. return 2;
  37. }
  38. if (result.optionalTestsPassed <= 0) {
  39. return 0;
  40. }
  41. return result.optionalTestsPassed - 1;
  42. }
  43. const PasswordStrength = ({
  44. shouldTestPasswordStrength,
  45. passwordValue,
  46. passwordStrengthTestsRequired,
  47. }) => {
  48. const strengthContainer = useRef(null);
  49. const [passwordStrength, setPasswordStrength] = useState({
  50. width: 0,
  51. color: 'red',
  52. });
  53. const [error, setError] = useState('');
  54. useEffect(() => {
  55. if (shouldTestPasswordStrength && passwordValue) {
  56. const bBox = strengthContainer.current.getBoundingClientRect();
  57. const result = owasp.test(passwordValue);
  58. console.log(typeof result);
  59. const passwordStrengthIndex = getPasswordStrengthIndex(result);
  60. const passwordOption = passwordStrengthOptions[passwordStrengthIndex];
  61. const width = !passwordValue
  62. ? 0
  63. : (bBox.width * (passwordStrengthIndex + 1)) /
  64. passwordStrengthTestsRequired;
  65. setPasswordStrength({ width, color: passwordOption.color });
  66. const strength = i18next.t(`password.${passwordOption.strength}`);
  67. setError(i18next.t('login.passwordStrength', { strength }));
  68. }
  69. }, [
  70. passwordValue,
  71. shouldTestPasswordStrength,
  72. passwordStrengthTestsRequired,
  73. ]);
  74. if (!shouldTestPasswordStrength || !passwordValue) {
  75. return null;
  76. }
  77. const renderError = () => {
  78. if (!error) {
  79. return null;
  80. }
  81. return (
  82. <div
  83. className="c-input--error"
  84. style={{
  85. color: passwordStrength.color,
  86. }}
  87. >
  88. {error}
  89. </div>
  90. );
  91. };
  92. return (
  93. <div ref={strengthContainer} className="c-password-strength__container">
  94. <div className="c-password-strength__line--wrapper">
  95. <div
  96. className="c-password-strength__line"
  97. style={{
  98. backgroundColor: passwordStrength.color,
  99. width: passwordStrength.width,
  100. }}
  101. />
  102. </div>
  103. {renderError()}
  104. </div>
  105. );
  106. };
  107. PasswordStrength.defaultProps = {
  108. passwordStrengthTestsRequired: 4,
  109. };
  110. export default PasswordStrength;