Skip to content

Commit 66f7cda

Browse files
authored
Merge pull request #819 from google/fix/818-refresh-errors
Properly handle refresh token response errors
2 parents 7683bdc + 3857701 commit 66f7cda

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

includes/Core/Authentication/Authentication.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ private function refresh_auth_token_on_login() {
465465
// If 'invalid_grant' error, disconnect the account.
466466
if ( 'invalid_grant' === $this->user_options->get( Clients\OAuth_Client::OPTION_ERROR_CODE ) ) {
467467
$this->disconnect();
468+
469+
// We need to re-set this error so that it is displayed to the user.
470+
$this->user_options->set( Clients\OAuth_Client::OPTION_ERROR_CODE, 'invalid_grant' );
468471
}
469472
}
470473

includes/Core/Authentication/Clients/OAuth_Client.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ public function refresh_token() {
246246
$this->user_options->set( self::OPTION_PROXY_ACCESS_CODE, $e->getAccessCode() );
247247
return;
248248
} catch ( \Exception $e ) {
249-
$this->user_options->set( self::OPTION_ERROR_CODE, 'invalid_grant' );
249+
$error_code = 'invalid_grant';
250+
if ( $this->using_proxy() ) { // Only the Google_Proxy_Client exposes the real error response.
251+
$error_code = $e->getMessage();
252+
}
253+
$this->user_options->set( self::OPTION_ERROR_CODE, $error_code );
250254
return;
251255
}
252256

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Fake HTTP Client
4+
*
5+
* @package Google\Site_Kit\Tests
6+
* @copyright 2019 Google LLC
7+
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
8+
* @link https://sitekit.withgoogle.com
9+
*/
10+
11+
namespace Google\Site_Kit\Tests;
12+
13+
use Google\Site_Kit_Dependencies\GuzzleHttp\Client;
14+
use Google\Site_Kit_Dependencies\GuzzleHttp\Message\RequestInterface;
15+
use Google\Site_Kit_Dependencies\GuzzleHttp\Message\Response;
16+
17+
/**
18+
* Class FakeHttpClient
19+
*/
20+
class FakeHttpClient extends Client {
21+
/**
22+
* Handler function for overriding requests.
23+
*
24+
* @var callable
25+
*/
26+
protected $request_handler;
27+
28+
/**
29+
* Sets the handler for all requests.
30+
*
31+
* @param callable $handler
32+
*/
33+
public function set_request_handler( callable $handler ) {
34+
$this->request_handler = $handler;
35+
}
36+
37+
/**
38+
* @param RequestInterface $request
39+
*
40+
* @return \Google\Site_Kit_Dependencies\GuzzleHttp\Message\ResponseInterface
41+
*/
42+
public function send( RequestInterface $request ) {
43+
if ( $this->request_handler ) {
44+
return call_user_func( $this->request_handler, $request );
45+
}
46+
47+
return new Response( 200 );
48+
}
49+
}

tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Google\Site_Kit\Context;
1414
use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client;
1515
use Google\Site_Kit\Tests\Exception\RedirectException;
16+
use Google\Site_Kit\Tests\FakeHttpClient;
1617
use Google\Site_Kit\Tests\TestCase;
1718

1819
/**
@@ -30,6 +31,7 @@ public function test_get_client() {
3031
}
3132

3233
public function test_refresh_token() {
34+
$this->fake_authentication();
3335
$user_id = $this->factory()->user->create();
3436
wp_set_current_user( $user_id );
3537
$client = new OAuth_Client( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) );
@@ -49,7 +51,7 @@ public function test_refresh_token() {
4951
// Google client must be initialized first
5052
$this->assertEquals( 'refresh_token_not_exist', get_user_option( OAuth_Client::OPTION_ERROR_CODE, $user_id ) );
5153

52-
$client->get_client();
54+
$client->get_client()->setHttpClient( new FakeHttpClient() );
5355
$client->refresh_token();
5456

5557
// At this point an error is triggered internally due to undefined indexes on $authentication_token

0 commit comments

Comments
 (0)