用于外部身份验证的WordPress登录自定义

时间:2013-05-13 作者:夏期劇場

我不确定哪种方法是最好的,但我的情况需要与admin login 具有外部身份验证源。我的意思是,WP通常是:

wp-admin ---> Check with WP Database ---> Authenticate
现在,我需要在两者之间添加一层:

wp-admin ---> Check with WP Database ---> Check with External API (true/false) ---> Authenticate
也就是说,我需要在两者之间放置一个额外的身份验证层(类似的逻辑,比如我们应用LDAP身份验证模块),在这种情况下,它是我自己的API。

那么现在在WP中,哪个用户/核心文件处理最终的true/false 传统登录概念中的调用返回?

我需要去哪里进行核心破解或修改?

1 个回复
SO网友:Charles Clarkson

哪里在传统的登录概念中是否处理最终的真/假调用返回?

登录过程从wp-login.php 第625行附近的WordPress文件:

$user = wp_signon(\'\', $secure_cookie);
Thewp_signon() 函数来自wp-includes/user.php 文件

/**
 * Authenticate user with remember capability.
 *
 * The credentials is an array that has \'user_login\', \'user_password\', and
 * \'remember\' indices. If the credentials is not given, then the log in form
 * will be assumed and used if set.
 *
 * The various authentication cookies will be set by this function and will be
 * set for a longer period depending on if the \'remember\' credential is set to
 * true.
 *
 * @since 2.5.0
 *
 * @param array $credentials Optional. User info in order to sign on.
 * @param bool $secure_cookie Optional. Whether to use secure cookie.
 * @return object Either WP_Error on failure, or WP_User on success.
 */
function wp_signon( $credentials = \'\', $secure_cookie = \'\' ) {
    if ( empty($credentials) ) {
        if ( ! empty($_POST[\'log\']) )
            $credentials[\'user_login\'] = $_POST[\'log\'];
        if ( ! empty($_POST[\'pwd\']) )
            $credentials[\'user_password\'] = $_POST[\'pwd\'];
        if ( ! empty($_POST[\'rememberme\']) )
            $credentials[\'remember\'] = $_POST[\'rememberme\'];
    }

    if ( !empty($credentials[\'remember\']) )
        $credentials[\'remember\'] = true;
    else
        $credentials[\'remember\'] = false;

    // TODO do we deprecate the wp_authentication action?
    do_action_ref_array(\'wp_authenticate\', array(&$credentials[\'user_login\'], &$credentials[\'user_password\']));

    if ( \'\' === $secure_cookie )
        $secure_cookie = is_ssl();

    $secure_cookie = apply_filters(\'secure_signon_cookie\', $secure_cookie, $credentials);

    global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie
    $auth_secure_cookie = $secure_cookie;

    add_filter(\'authenticate\', \'wp_authenticate_cookie\', 30, 3);

    $user = wp_authenticate($credentials[\'user_login\'], $credentials[\'user_password\']);

    if ( is_wp_error($user) ) {
        if ( $user->get_error_codes() == array(\'empty_username\', \'empty_password\') ) {
            $user = new WP_Error(\'\', \'\');
        }

        return $user;
    }

    wp_set_auth_cookie($user->ID, $credentials[\'remember\'], $secure_cookie);
    do_action(\'wp_login\', $user->user_login, $user);
    return $user;
}
我不是这方面的专家,但看起来你有两个选择。

1) 重写wp_authenticate() 功能位于wp-includes/pluggable.php.

中的所有功能wp-includes/pluggable.php, 在创建函数之前,请检查该函数是否已经存在,以便您可以在主题或插件中添加重写后的函数,它将替换默认值wp_authenticate() 作用

2) 将您的层添加到位于函数末尾附近的“wp\\u login”操作挂钩。

WordPress完成所有身份验证后,会发生“wp\\u login”操作。像这样使用它。

add_action( \'wp_login\', \'my_external_authentication\', 10, 2 );
/**
 * @return object Either WP_Error on failure, or WP_User on success.
 */
function my_external_authentication( $user_login, $user ) {
    // Add external authentication here.
}

结束

相关推荐

Transients API conditional

我有以下瞬态,但如果用户更改$url的值,则不应使用瞬态。我想知道在瞬态中使用这个变量最合适的方法是什么。我是否应该使用Options API保存$url的值以将其与新值进行比较?if ( false === ( $videos_result = get_transient( \'html\' ) )){ //HTTP API $videos_result = wp_remote_get( $url ); $response_code = wp_r