【问题标题】:HybridAuth with Google provider randomly returns "invalid_request" when authenticating使用 Google 提供商的 HybridAuth 在进行身份验证时随机返回“invalid_request”
【发布时间】:2017-04-21 04:54:14
【问题描述】:

我们使用 Google OAuth2 对内部应用程序的用户进行身份验证,使用 HybridAuth 2.4.0,直到大约一周前,我们开始看到来自https://accounts.google.com/o/oauth2/token 的越来越多的随机“invalid_request”响应。

当我说“随机”时,今天更有可能:不起作用然后系统地工作大约一分钟(连续多次身份验证将成功)再次停止工作(即:“invalid_request”响应)。

我们尝试升级到最新版本的 HybridAuth(2.8.0 和现在的 2.8.1),但没有解决遇到的问题。

还检查了服务器时间(它是 NTP 同步的并且很好),尝试生成一个新的 Google API 密钥,创建一个新的 Google API 项目,但没有任何更好的运气。

我们认为这是一个环境/服务器问题,因为我们有一个本地 DEV 环境,OAuth2 身份验证在该环境中一直有效。此外,当使用 Google API Playground 并使用与 PROD 服务器相同的有效负载向https://accounts.google.com/o/oauth2/token 发出请求时,它可以正常工作,我们将获得“Bearer”access_token。

一些 HybridAuth 调试日志:

2016-12-06T13:34:56+00:00 -- Endpoint: call adapter [Google] loginFinish()
2016-12-06T13:34:56+00:00 -- Enter OAuth2Client::request( https://accounts.google.com/o/oauth2/token )
2016-12-06T13:34:56+00:00 -- OAuth2Client::request(). dump request params:  -- a:5:{s:9:"client_id";s:72:"[obfuscated].apps.googleusercontent.com";s:13:"client_secret";s:24:"[obfuscated]";s:10:"grant_type";s:18:"authorization_code";s:12:"redirect_uri";s:55:"https://[obfuscated]/endpoint?hauth.done=Google";s:4:"code";s:45:"[obfuscated]";}
2016-12-06T13:34:58+00:00 -- OAuth2Client::request(). dump request info:  -- a:26:{s:3:"url";s:42:"https://accounts.google.com/o/oauth2/token";s:12:"content_type";s:31:"application/json; charset=utf-8";s:9:"http_code";i:400;s:11:"header_size";i:428;s:12:"request_size";i:318;s:8:"filetime";i:-1;s:17:"ssl_verify_result";i:0;s:14:"redirect_count";i:0;s:10:"total_time";d:2.5824850000000001;s:15:"namelookup_time";d:2.0000000000000002E-5;s:12:"connect_time";d:0.14088800000000001;s:16:"pretransfer_time";d:0.426647;s:11:"size_upload";d:753;s:13:"size_download";d:33;s:14:"speed_download";d:12;s:12:"speed_upload";d:291;s:23:"download_content_length";d:-1;s:21:"upload_content_length";d:753;s:18:"starttransfer_time";d:2.427991;s:13:"redirect_time";d:0;s:12:"redirect_url";s:0:"";s:10:"primary_ip";s:14:"[obfuscated]";s:8:"certinfo";a:0:{}s:12:"primary_port";i:443;s:8:"local_ip";s:11:"[obfuscated]";s:10:"local_port";i:35617;}
2016-12-06T13:34:58+00:00 -- OAuth2Client::request(). dump request result:  -- s:33:"{
  "error" : "invalid_request"
}";

任何关于进一步调查的方向的线索将不胜感激:)

【问题讨论】:

    标签: google-api hybridauth oauth2client


    【解决方案1】:

    设法解决了这个问题。看起来 Hybridauth 正在将一个数组传递到 POSTFIELDS

    curl_setopt($ch, CURLOPT_POSTFIELDS, array( 
        'code='. urlencode($code),
        'client_id=' . urlencode($clientID),
        'client_secret=' . urlencode($clientSecret),
        'redirect_uri=http%3A%2F%2Flocalhost%2Fexperiments%2FnewGALogin.php',
        'grant_type=authorization_code'
    )); 
    

    当输入是一个数组时,生成的 Content-Type 将是 multipart/form-data 不符合 OAuth 2.0 规范,服务器将忽略它。当输入是查询编码的字符串(例如使用 http_build_query 构建)时,Content-Type: 将是 application/x-www-form-urlencoded,这是规范所要求的。

    请参阅“注释”部分:http://php.net/manual/en/function.curl-setopt.php

    因此,如果我们将其作为查询字符串传递:

    curl_setopt($ch, CURLOPT_POSTFIELDS,
        'code=' . urlencode($code) . '&' .
        'client_id=' . urlencode($clientID) . '&' .
        'client_secret=' . urlencode($clientSecret) . '&' .
        'redirect_uri=http%3A%2F%2Flocalhost%2Fexperiments%2FnewGALogin.php' . '&'     .
        'grant_type=authorization_code' 
    ); 
    

    我们不再看到此问题。

    希望对你有帮助!

    【讨论】:

    【解决方案2】:

    @Adzzz 的答案是正确的(非常感谢)。一些修补 hybridAuth 的代码...

    //file hybridauth/hybridauth/Hybrid/thirdparty/OAuth/OAuth2Client.php line 234   
    if( $type == "POST" ){
      curl_setopt($ch, CURLOPT_POST, 1);
      $paramsString="";
      if($params){
        foreach($params as $k=>$v){
            $paramsString.=$k."=";
            $paramsString.=$v."&";
        }
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $paramsString );
      }
      //original code curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2012-04-25
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 2018-01-22
      • 2020-01-10
      相关资源
      最近更新 更多