从第三方网站进行程序化登录

时间:2020-06-05 作者:jprice92

我正在尝试从单独的web应用程序中为wordpress站点提供一种SSO/编程登录解决方案。我对wordpress没有太多经验,所以我认为我缺少了一些东西或者配置不正确。

首先,这是我的测试代码,当我直接点击它时,它工作得很好www.mywordpresssite.com/test_login.php

test\\u登录。php:

<?php
require __DIR__ . \'/wp-load.php\';

$username = \'myuserid\';

$user = get_user_by(\'login\', $username);

if (!is_wp_error( $user )) {
  wp_clear_auth_cookie();
  wp_set_current_user($user->ID);
  wp_set_auth_cookie($user->ID, TRUE);
  $redirect_to = user_admin_url();
  wp_safe_redirect( $redirect_to );
}

exit();
上面相应地设置cookie并登录“myuserid”用户没有问题。它重定向到user\\u admin\\u url(),这对我的测试很好,但没有必要,因为我的目的只是转到主页,因为一旦这样做起作用,整个网站将被锁定,以供未登录的用户使用。

现在使用与test\\u登录相同的概念。php我已经创建了prod\\u登录名。php应该通过来自web应用程序的cURL请求应用相同的逻辑,该web应用程序通过身份验证头传递CRED。

prod\\u登录。php:

<?php
require __DIR__ . \'/wp-load.php\';

$response = array(\'http_code\' => 500, \'message\' => \'Failed to update response\');

if ($_SERVER[\'PHP_AUTH_PW\'] !== \'password123\') {
  $response[\'http_code\'] = 401;
  $response[\'message\'] = \'Unauthorized login attempt\';
  echo json_encode($response);
  die();
}

$username = $_SERVER[\'PHP_AUTH_USER\'];
$user = get_user_by(\'login\', $username);

// Redirect URL //
if (!is_wp_error( $user )) {
  wp_clear_auth_cookie();
  wp_set_current_user($user->ID);
  wp_set_auth_cookie($user->ID, TRUE);
  //$redirect_to = user_admin_url();
  //wp_safe_redirect( $redirect_to ); //commented out as unnecessary
  $response[\'http_code\'] = 200;
  $response[\'message\'] = \'Login Successful\';
  $response[\'user\'] = $user;
  echo json_encode($response);
} else {
  $response[\'http_code\'] = 403;
  $response[\'message\'] = \'Invalid User\';
  echo json_encode($response);
}

exit();
现在,我的卷曲请求似乎运行良好,我始终得到http_code === 200 响应,用户对象将在cURL响应中正确返回。然而,在从web应用重定向到wordpress站点后,cookies没有设置,我也没有实际登录。

web app cURL请求:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERPWD, \'myuserid\' . \':\' . \'password123\');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(\'Content-Type:application/json\'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL,\'http://mywordpresssite.com/prod_login.php\');
    $server_output = curl_exec($ch);
    if (curl_errno($ch)) {
        // handle failed cURL
    }
    $decoded_response = json_decode($server_output);

    if ($decoded_response->http_code === 200) {
        // cURL response indicated successful login
        // I can consistently get to this point and it will do the redirect but I will not be logged in nor will the cookies be set
        header(\'Location: mywordpresssite.com\'); // go to the wp homepage now that we should be logged in
    }
我不知道我做错了什么。我认为这与曲奇被吹走或在卷曲和重定向之间没有保存有关。任何关于如何防止这种情况发生或我所做的错误的见解都将受到赞赏。这甚至可以用卷曲来实现吗?

我还尝试了许多add_filter(‘auth_cookie_expiration’,...apply_filters(‘auth_cookie_expiration’,... 那些在互联网上可用的都没有用。

1 个回复
SO网友:jprice92

据我所知,我想做的是不可能的。

cURL请求实际上从未接触到浏览器,它从web应用服务器传递到wordpress站点服务器,因此在这种情况下,web应用服务器就是客户端。因此,当发生重定向时,浏览器中不会设置cookie。

相反,我所做的是在查询字符串中传递标记化值的GET请求。这种方式并不安全,因为从技术上讲,任何获得代币的人都可以访问该网站并登录,但似乎没有办法绕过这一点。至少标记化的URL在地址栏中从来都不可见,所以典型的用户不会知道发生了什么。

相关推荐

我在WordPress单页php编码中遇到错误

我在WordPress single page中遇到错误当移动网站时,此页面在另一台服务器中正确加载其他服务器出现错误致命错误:未捕获错误:语法错误,意外的“endwhile”(T\\u endwhile)在/单个产品中。php第149行下面的php代码,如果有人能帮我解决,我会非常感谢,提前谢谢,我已经尝试解决了无法解决的问题,下面是我的代码<?php /** * The template for displaying all single product *