password-strength-meter.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @output wp-admin/js/password-strength-meter.js
  3. */
  4. /* global zxcvbn */
  5. window.wp = window.wp || {};
  6. (function($){
  7. var __ = wp.i18n.__,
  8. sprintf = wp.i18n.sprintf;
  9. /**
  10. * Contains functions to determine the password strength.
  11. *
  12. * @since 3.7.0
  13. *
  14. * @namespace
  15. */
  16. wp.passwordStrength = {
  17. /**
  18. * Determines the strength of a given password.
  19. *
  20. * Compares first password to the password confirmation.
  21. *
  22. * @since 3.7.0
  23. *
  24. * @param {string} password1 The subject password.
  25. * @param {Array} disallowedList An array of words that will lower the entropy of
  26. * the password.
  27. * @param {string} password2 The password confirmation.
  28. *
  29. * @return {number} The password strength score.
  30. */
  31. meter : function( password1, disallowedList, password2 ) {
  32. if ( ! Array.isArray( disallowedList ) )
  33. disallowedList = [ disallowedList.toString() ];
  34. if (password1 != password2 && password2 && password2.length > 0)
  35. return 5;
  36. if ( 'undefined' === typeof window.zxcvbn ) {
  37. // Password strength unknown.
  38. return -1;
  39. }
  40. var result = zxcvbn( password1, disallowedList );
  41. return result.score;
  42. },
  43. /**
  44. * Builds an array of words that should be penalized.
  45. *
  46. * Certain words need to be penalized because it would lower the entropy of a
  47. * password if they were used. The disallowedList is based on user input fields such
  48. * as username, first name, email etc.
  49. *
  50. * @since 3.7.0
  51. * @deprecated 5.5.0 Use {@see 'userInputDisallowedList()'} instead.
  52. *
  53. * @return {string[]} The array of words to be disallowed.
  54. */
  55. userInputBlacklist : function() {
  56. window.console.log(
  57. sprintf(
  58. /* translators: 1: Deprecated function name, 2: Version number, 3: Alternative function name. */
  59. __( '%1$s is deprecated since version %2$s! Use %3$s instead. Please consider writing more inclusive code.' ),
  60. 'wp.passwordStrength.userInputBlacklist()',
  61. '5.5.0',
  62. 'wp.passwordStrength.userInputDisallowedList()'
  63. )
  64. );
  65. return wp.passwordStrength.userInputDisallowedList();
  66. },
  67. /**
  68. * Builds an array of words that should be penalized.
  69. *
  70. * Certain words need to be penalized because it would lower the entropy of a
  71. * password if they were used. The disallowed list is based on user input fields such
  72. * as username, first name, email etc.
  73. *
  74. * @since 5.5.0
  75. *
  76. * @return {string[]} The array of words to be disallowed.
  77. */
  78. userInputDisallowedList : function() {
  79. var i, userInputFieldsLength, rawValuesLength, currentField,
  80. rawValues = [],
  81. disallowedList = [],
  82. userInputFields = [ 'user_login', 'first_name', 'last_name', 'nickname', 'display_name', 'email', 'url', 'description', 'weblog_title', 'admin_email' ];
  83. // Collect all the strings we want to disallow.
  84. rawValues.push( document.title );
  85. rawValues.push( document.URL );
  86. userInputFieldsLength = userInputFields.length;
  87. for ( i = 0; i < userInputFieldsLength; i++ ) {
  88. currentField = $( '#' + userInputFields[ i ] );
  89. if ( 0 === currentField.length ) {
  90. continue;
  91. }
  92. rawValues.push( currentField[0].defaultValue );
  93. rawValues.push( currentField.val() );
  94. }
  95. /*
  96. * Strip out non-alphanumeric characters and convert each word to an
  97. * individual entry.
  98. */
  99. rawValuesLength = rawValues.length;
  100. for ( i = 0; i < rawValuesLength; i++ ) {
  101. if ( rawValues[ i ] ) {
  102. disallowedList = disallowedList.concat( rawValues[ i ].replace( /\W/g, ' ' ).split( ' ' ) );
  103. }
  104. }
  105. /*
  106. * Remove empty values, short words and duplicates. Short words are likely to
  107. * cause many false positives.
  108. */
  109. disallowedList = $.grep( disallowedList, function( value, key ) {
  110. if ( '' === value || 4 > value.length ) {
  111. return false;
  112. }
  113. return $.inArray( value, disallowedList ) === key;
  114. });
  115. return disallowedList;
  116. }
  117. };
  118. // Backward compatibility.
  119. /**
  120. * Password strength meter function.
  121. *
  122. * @since 2.5.0
  123. * @deprecated 3.7.0 Use wp.passwordStrength.meter instead.
  124. *
  125. * @global
  126. *
  127. * @type {wp.passwordStrength.meter}
  128. */
  129. window.passwordStrength = wp.passwordStrength.meter;
  130. })(jQuery);