Ticket #8938: wp-auth.diff

File wp-auth.diff, 5.8 KB (added by wnorris, 3 years ago)
  • wp-includes/user.php

     
    3232                        $credentials['remember'] = $_POST['rememberme']; 
    3333        } 
    3434 
    35         if ( !empty($credentials['user_login']) ) 
    36                 $credentials['user_login'] = sanitize_user($credentials['user_login']); 
    37         if ( !empty($credentials['user_password']) ) 
    38                 $credentials['user_password'] = trim($credentials['user_password']); 
    3935        if ( !empty($credentials['remember']) ) 
    4036                $credentials['remember'] = true; 
    4137        else 
    4238                $credentials['remember'] = false; 
    4339 
     40        // TODO do we deprecate the wp_authentication action? 
    4441        do_action_ref_array('wp_authenticate', array(&$credentials['user_login'], &$credentials['user_password'])); 
    4542 
    4643        if ( '' === $secure_cookie ) 
    4744                $secure_cookie = is_ssl() ? true : false; 
    4845 
    49         // If no credential info provided, check cookie. 
    50         if ( empty($credentials['user_login']) && empty($credentials['user_password']) ) { 
    51                 $user = wp_validate_auth_cookie(); 
    52                 if ( $user ) 
    53                         return new WP_User($user); 
     46        global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie 
     47        $auth_secure_cookie = $secure_cookie; 
    5448 
    55                 if ( $secure_cookie ) 
    56                         $auth_cookie = SECURE_AUTH_COOKIE; 
    57                 else 
    58                         $auth_cookie = AUTH_COOKIE; 
     49        add_filter('authenticate', 'wp_authenticate_cookie', 30, 3); 
    5950 
    60                 if ( !empty($_COOKIE[$auth_cookie]) ) 
    61                         return new WP_Error('expired_session', __('Please log in again.')); 
     51        $user = wp_authenticate($credentials['user_login'], $credentials['user_password']); 
    6252 
    63                 // If the cookie is not set, be silent. 
    64                 return new WP_Error(); 
     53        if ( is_wp_error($user) ) 
     54                return $user; 
     55 
     56        wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie); 
     57        do_action('wp_login', $credentials['user_login']); 
     58        return $user; 
     59} 
     60 
     61 
     62/** 
     63 * Authenticate the user using the username and password. 
     64 */ 
     65add_filter('authenticate', 'wp_authenticate_username_password', 20, 3); 
     66function wp_authenticate_username_password($user, $username, $password) { 
     67        if ( is_a($user, 'WP_User') ) { return $user; } 
     68 
     69        // XXX slight hack to handle initial load of wp-login.php 
     70        if ( (empty($username) && empty($password)) && $GLOBALS['pagenow'] == 'wp-login.php' ) { 
     71                return $user; 
    6572        } 
    6673 
    67         if ( empty($credentials['user_login']) || empty($credentials['user_password']) ) { 
     74        if ( empty($username) || empty($password) ) { 
    6875                $error = new WP_Error(); 
    6976 
    70                 if ( empty($credentials['user_login']) ) 
     77                if ( empty($username) ) 
    7178                        $error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.')); 
    72                 if ( empty($credentials['user_password']) ) 
     79 
     80                if ( empty($password) ) 
    7381                        $error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.')); 
     82 
    7483                return $error; 
    7584        } 
    7685 
    77         $user = wp_authenticate($credentials['user_login'], $credentials['user_password']); 
    78         if ( is_wp_error($user) ) 
     86        $userdata = get_userdatabylogin($username); 
     87 
     88        if ( !$userdata || ($userdata->user_login != $username) ) { 
     89                return new WP_Error('invalid_username', __('<strong>ERROR</strong>: Invalid username.')); 
     90        } 
     91 
     92        $user = apply_filters('wp_authenticate_user', $user, $password); 
     93        if ( is_wp_error($user) ) { 
    7994                return $user; 
     95        } 
    8096 
    81         wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie); 
    82         do_action('wp_login', $credentials['user_login']); 
     97        if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) ) { 
     98                return new WP_Error('incorrect_password', __('<strong>ERROR</strong>: Incorrect password.')); 
     99        } 
     100 
     101        $user =  new WP_User($userdata->ID); 
    83102        return $user; 
    84103} 
    85104 
    86105/** 
     106 * Authenticate the user using the WordPress auth cookie. 
     107 */ 
     108function wp_authenticate_cookie($user, $username, $password) { 
     109        if ( is_a($user, 'WP_User') ) { return $user; } 
     110 
     111        if ( empty($username) && empty($password) ) { 
     112                $user_id = wp_validate_auth_cookie(); 
     113                if ( $user_id ) 
     114                        return new WP_User($user_id); 
     115 
     116                global $auth_secure_cookie; 
     117 
     118                if ( $auth_secure_cookie ) 
     119                        $auth_cookie = SECURE_AUTH_COOKIE; 
     120                else 
     121                        $auth_cookie = AUTH_COOKIE; 
     122 
     123                if ( !empty($_COOKIE[$auth_cookie]) ) 
     124                        return new WP_Error('expired_session', __('Please log in again.')); 
     125 
     126                // If the cookie is not set, be silent. 
     127        } 
     128 
     129        return $user; 
     130} 
     131 
     132/** 
    87133 * Retrieve user data based on field. 
    88134 * 
    89135 * Use get_profile() will make a database query to get the value of the table 
  • wp-includes/pluggable.php

     
    437437 */ 
    438438function wp_authenticate($username, $password) { 
    439439        $username = sanitize_user($username); 
     440        $password = trim($password); 
    440441 
    441         if ( '' == $username ) 
    442                 return new WP_Error('empty_username', __('<strong>ERROR</strong>: The username field is empty.')); 
     442        $user = apply_filters('authenticate', null, $username, $password); 
    443443 
    444         if ( '' == $password ) 
    445                 return new WP_Error('empty_password', __('<strong>ERROR</strong>: The password field is empty.')); 
    446  
    447         $user = get_userdatabylogin($username); 
    448  
    449         if ( !$user || ($user->user_login != $username) ) { 
    450                 do_action( 'wp_login_failed', $username ); 
    451                 return new WP_Error('invalid_username', __('<strong>ERROR</strong>: Invalid username.')); 
     444        if ($user == null) { 
     445                $user = new WP_Error(); // TODO what should the error message be? (Or would these even happen?) 
    452446        } 
    453447 
    454         $user = apply_filters('wp_authenticate_user', $user, $password); 
    455         if ( is_wp_error($user) ) { 
    456                 do_action( 'wp_login_failed', $username ); 
    457                 return $user; 
     448        if (is_wp_error($user)) { 
     449                do_action('wp_login_failed', $username); 
    458450        } 
    459451 
    460         if ( !wp_check_password($password, $user->user_pass, $user->ID) ) { 
    461                 do_action( 'wp_login_failed', $username ); 
    462                 return new WP_Error('incorrect_password', __('<strong>ERROR</strong>: Incorrect password.')); 
    463         } 
    464  
    465         return new WP_User($user->ID); 
     452        return $user; 
    466453} 
    467454endif; 
    468455