password-strength-meter.js 3.1 KB

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