Make WordPress Core

Ticket #25174: 25174.4.diff

File 25174.4.diff, 4.4 KB (added by nacin, 13 years ago)
  • src/wp-admin/js/password-strength-meter.js

     
    1 function passwordStrength(password1, username, password2) {
    2         if (password1 != password2 && password2.length > 0)
    3                 return 5;
    4         var result = zxcvbn( password1, [ username ] );
    5         return result.score;
    6 }
     1window.wp = window.wp || {};
     2
     3var passwordStrength;
     4(function($){
     5        /**
     6         * Determine the strength of a given password
     7         *
     8         * @param string password1 The password
     9         * @param array blacklist An array of words that will lower the entropy of the password
     10         * @param string password2 The confirmed password
     11         */
     12        wp.passwordStrength = {
     13                meter: function( password1, blacklist, password2 ) {
     14                        if ( ! $.isArray( blacklist ) )
     15                                blacklist = [ blacklist.toString() ];
     16
     17                        if (password1 != password2 && password2.length > 0)
     18                                return 5;
     19
     20                        var result = zxcvbn( password1, blacklist );
     21                        return result.score;
     22                },
     23
     24                /**
     25                 * Builds an array of data that should be penalized, because it would lower the entropy of a password if it were used
     26                 *
     27                 * @return array The array of data to be blacklisted
     28                 */
     29
     30                userInputBlacklist : function() {
     31                        var i, userInputFieldsLength, rawValuesLength, currentField,
     32                                rawValues       = [],
     33                                blacklist       = [],
     34                                userInputFields = [ 'user_login', 'first_name', 'last_name', 'nickname', 'display_name', 'email', 'url', 'description', 'weblog_title', 'admin_email' ];
     35
     36                        // Collect all the strings we want to blacklist
     37                        rawValues.push( $( 'title' ).text() );
     38                        rawValues.push( document.URL );
     39
     40                        userInputFieldsLength = userInputFields.length;
     41                        for ( i = 0; i < userInputFieldsLength; i++ ) {
     42                                currentField = $( '#' + userInputFields[ i ] );
     43
     44                                if ( 0 == currentField.length ) {
     45                                        continue;
     46                                }
     47
     48                                rawValues.push( currentField[0].defaultValue );
     49                                rawValues.push( currentField.val() );
     50                        }
     51
     52                        // Strip out non-alphanumeric characters and convert each word to an individual entry
     53                        rawValuesLength = rawValues.length;
     54                        for ( i = 0; i < rawValuesLength; i++ ) {
     55                                if ( rawValues[ i ] ) {
     56                                        blacklist = blacklist.concat( rawValues[ i ].replace( /\W/g, ' ' ).split( ' ' ) );
     57                                }
     58                        }
     59
     60                        // Remove empty values, short words, and duplicates. Short words are likely to cause many false positives.
     61                        blacklist = $.grep( blacklist, function( value, key ) {
     62                                if ( '' == value || 4 > value.length ) {
     63                                        return false;
     64                                }
     65
     66                                return $.inArray( value, blacklist ) === key;
     67                        });
     68
     69                        return blacklist;
     70                }
     71        }
     72
     73        // Backwards compatibility.
     74        passwordStrength = wp.passwordStrength.meter;
     75})(jQuery);
     76 No newline at end of file
  • src/wp-admin/js/user-profile.js

     
    99                        return;
    1010                }
    1111
    12                 strength = passwordStrength(pass1, user, pass2);
     12                strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputBlacklist(), pass2 );
    1313
    1414                switch ( strength ) {
    1515                        case 2:
  • tests/qunit/wp-admin/js/password-strength-meter.js

     
    7777                }
    7878        });
    7979
    80         test( 'username in password should be penalized', function() {
     80        test( 'blacklisted words in password should be penalized', function() {
    8181                var allowedPasswordScore, penalizedPasswordScore,
    8282                        allowedPassword   = 'a[janedoe]4',
    8383                        penalizedPassword = 'a[johndoe]4',
    84                         username          = 'johndoe';
     84                        blacklist         = [ 'extra', 'johndoe', 'superfluous' ];
    8585
    86                 allowedPasswordScore = passwordStrength( allowedPassword, username, allowedPassword );
    87                 penalizedPasswordScore = passwordStrength( penalizedPassword, username, penalizedPassword );
     86                allowedPasswordScore = passwordStrength( allowedPassword, blacklist, allowedPassword );
     87                penalizedPasswordScore = passwordStrength( penalizedPassword, blacklist, penalizedPassword );
    8888
    8989                ok( penalizedPasswordScore < allowedPasswordScore, 'Penalized password scored ' + penalizedPasswordScore + '; allowed password scored: ' + allowedPasswordScore );
    9090        });