2013-11-15 00:12:09 -05:00
|
|
|
/* global zxcvbn */
|
2013-09-28 02:47:10 -04:00
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
|
|
|
var passwordStrength;
|
|
|
|
(function($){
|
2018-01-18 08:39:33 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains functions to determine the password strength.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2013-09-28 02:47:10 -04:00
|
|
|
wp.passwordStrength = {
|
|
|
|
/**
|
2018-01-18 08:39:33 -05:00
|
|
|
* Determines the strength of a given password.
|
|
|
|
*
|
|
|
|
* Compares first password to the password confirmation.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @param {string} password1 The subject password.
|
|
|
|
* @param {Array} blacklist An array of words that will lower the entropy of
|
|
|
|
* the password.
|
|
|
|
* @param {string} password2 The password confirmation.
|
2013-09-28 02:47:10 -04:00
|
|
|
*
|
2018-01-18 08:39:33 -05:00
|
|
|
* @returns {number} The password strength score.
|
2013-09-28 02:47:10 -04:00
|
|
|
*/
|
|
|
|
meter : function( password1, blacklist, password2 ) {
|
|
|
|
if ( ! $.isArray( blacklist ) )
|
|
|
|
blacklist = [ blacklist.toString() ];
|
|
|
|
|
2013-10-24 18:55:12 -04:00
|
|
|
if (password1 != password2 && password2 && password2.length > 0)
|
2013-09-28 02:47:10 -04:00
|
|
|
return 5;
|
|
|
|
|
2016-07-01 08:45:29 -04:00
|
|
|
if ( 'undefined' === typeof window.zxcvbn ) {
|
|
|
|
// Password strength unknown.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-28 02:47:10 -04:00
|
|
|
var result = zxcvbn( password1, blacklist );
|
|
|
|
return result.score;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-01-18 08:39:33 -05:00
|
|
|
* Builds an array of words that should be penalized.
|
2013-09-28 02:47:10 -04:00
|
|
|
*
|
2018-01-18 08:39:33 -05:00
|
|
|
* Certain words need to be penalized because it would lower the entropy of a
|
|
|
|
* password if they were used. The blacklist is based on user input fields such
|
|
|
|
* as username, first name, email etc.
|
|
|
|
*
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @returns {string[]} The array of words to be blacklisted.
|
2013-09-28 02:47:10 -04:00
|
|
|
*/
|
|
|
|
userInputBlacklist : function() {
|
|
|
|
var i, userInputFieldsLength, rawValuesLength, currentField,
|
|
|
|
rawValues = [],
|
|
|
|
blacklist = [],
|
|
|
|
userInputFields = [ 'user_login', 'first_name', 'last_name', 'nickname', 'display_name', 'email', 'url', 'description', 'weblog_title', 'admin_email' ];
|
|
|
|
|
2018-01-18 08:39:33 -05:00
|
|
|
// Collect all the strings we want to blacklist.
|
2013-09-28 02:47:10 -04:00
|
|
|
rawValues.push( document.title );
|
|
|
|
rawValues.push( document.URL );
|
|
|
|
|
|
|
|
userInputFieldsLength = userInputFields.length;
|
|
|
|
for ( i = 0; i < userInputFieldsLength; i++ ) {
|
|
|
|
currentField = $( '#' + userInputFields[ i ] );
|
|
|
|
|
2013-11-15 00:12:09 -05:00
|
|
|
if ( 0 === currentField.length ) {
|
2013-09-28 02:47:10 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
rawValues.push( currentField[0].defaultValue );
|
|
|
|
rawValues.push( currentField.val() );
|
|
|
|
}
|
|
|
|
|
2018-01-18 08:39:33 -05:00
|
|
|
/*
|
|
|
|
* Strip out non-alphanumeric characters and convert each word to an
|
|
|
|
* individual entry.
|
|
|
|
*/
|
2013-09-28 02:47:10 -04:00
|
|
|
rawValuesLength = rawValues.length;
|
|
|
|
for ( i = 0; i < rawValuesLength; i++ ) {
|
|
|
|
if ( rawValues[ i ] ) {
|
|
|
|
blacklist = blacklist.concat( rawValues[ i ].replace( /\W/g, ' ' ).split( ' ' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 08:39:33 -05:00
|
|
|
/*
|
|
|
|
* Remove empty values, short words and duplicates. Short words are likely to
|
|
|
|
* cause many false positives.
|
|
|
|
*/
|
2013-09-28 02:47:10 -04:00
|
|
|
blacklist = $.grep( blacklist, function( value, key ) {
|
2013-11-15 00:12:09 -05:00
|
|
|
if ( '' === value || 4 > value.length ) {
|
2013-09-28 02:47:10 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $.inArray( value, blacklist ) === key;
|
|
|
|
});
|
|
|
|
|
|
|
|
return blacklist;
|
|
|
|
}
|
2013-11-15 00:12:09 -05:00
|
|
|
};
|
2013-09-28 02:47:10 -04:00
|
|
|
|
2018-01-18 08:39:33 -05:00
|
|
|
// Backward compatibility.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Password strength meter function.
|
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
* @deprecated 3.7.0 Use wp.passwordStrength.meter instead.
|
|
|
|
*
|
|
|
|
* @global
|
|
|
|
*
|
|
|
|
* @type {wp.passwordStrength.meter}
|
|
|
|
*/
|
2013-09-28 02:47:10 -04:00
|
|
|
passwordStrength = wp.passwordStrength.meter;
|
2016-05-13 14:41:31 -04:00
|
|
|
})(jQuery);
|