Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HybridAuth with Google provider randomly returns "invalid_request" when authenticating

We use Google OAuth2 to authenticate our users into an internal application, using HybridAuth 2.4.0 and it went well until about a week ago where we started to see more and more random "invalid_request" responses from https://accounts.google.com/o/oauth2/token.

When I say "random", today it's more likely: doesn't work then works systematically for about one minute (multiple authentication in a row will succeed) to stop working again (ie: "invalid_request" response).

We tried upgrading to the latest version of HybridAuth (2.8.0 and now 2.8.1), it didn't fix the encountered issue.

Also checked the server time (it's NTP-synced and good), tried generating a new Google API secret, create a new Google API project, without any better luck.

We believe it is a environment/server issue, as we have a local DEV environment where the OAuth2 authentication works all the time. Also, when using the Google API playground and making the request to https://accounts.google.com/o/oauth2/token using the same payload as the PROD server does, it works, we would get the "Bearer" access_token.

Some HybridAuth debug log:

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"
}";

Any clue on which direction to take to further investigate would be very appreciated :)

like image 319
Christophe Deliens Avatar asked Mar 10 '23 11:03

Christophe Deliens


1 Answers

Managed to solve the issue. It looks like Hybridauth was passing an array into the 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'
)); 

When the input is an array the resulting Content-Type will be multipart/form-data which is not compliant with the OAuth 2.0 spec and the server will ignore it. When the input is a query-encoded string (e.g built using http_build_query) the Content-Type: will be application/x-www-form-urlencoded, which is what the spec requires.

See the "Notes" section at: http://php.net/manual/en/function.curl-setopt.php

Therefore, if we pass it as a querystring :

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' 
); 

We are no longer seeing this issue.

Hope it helps!

like image 197
Adzzz Avatar answered May 16 '23 09:05

Adzzz