From bd99352bdf6d34cd16fe1de4aba26728d4262886 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 7 Nov 2019 21:41:47 +0100 Subject: [PATCH 1/5] Rely on actual error returned from refresh token request. --- includes/Core/Authentication/Clients/OAuth_Client.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/includes/Core/Authentication/Clients/OAuth_Client.php b/includes/Core/Authentication/Clients/OAuth_Client.php index 8108298790d..f93d48080ce 100644 --- a/includes/Core/Authentication/Clients/OAuth_Client.php +++ b/includes/Core/Authentication/Clients/OAuth_Client.php @@ -246,7 +246,11 @@ public function refresh_token() { $this->user_options->set( self::OPTION_PROXY_ACCESS_CODE, $e->getAccessCode() ); return; } catch ( \Exception $e ) { - $this->user_options->set( self::OPTION_ERROR_CODE, 'invalid_grant' ); + $error_code = 'invalid_grant'; + if ( $this->using_proxy() ) { // Only the Google_Proxy_Client exposes the real error response. + $error_code = $e->getMessage(); + } + $this->user_options->set( self::OPTION_ERROR_CODE, $error_code ); return; } From 0de520549ccf4d0544642706220d19baabbe35fb Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 7 Nov 2019 21:42:20 +0100 Subject: [PATCH 2/5] Ensure user sees an error message after having been disconnected because of an invalid_grant response. --- includes/Core/Authentication/Authentication.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/includes/Core/Authentication/Authentication.php b/includes/Core/Authentication/Authentication.php index f0488020b92..647f2f6562e 100644 --- a/includes/Core/Authentication/Authentication.php +++ b/includes/Core/Authentication/Authentication.php @@ -465,6 +465,9 @@ private function refresh_auth_token_on_login() { // If 'invalid_grant' error, disconnect the account. if ( 'invalid_grant' === $this->user_options->get( Clients\OAuth_Client::OPTION_ERROR_CODE ) ) { $this->disconnect(); + + // We need to re-set this error so that it is displayed to the user. + $this->user_options->set( Clients\OAuth_Client::OPTION_ERROR_CODE, 'invalid_grant' ); } } From ce0299d1c6a6293e8332aaac49f6c8f1d4b8b4d8 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 7 Nov 2019 22:00:25 +0100 Subject: [PATCH 3/5] Fix PHPUnit errors. --- .../integration/Core/Authentication/Clients/OAuth_ClientTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php b/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php index ac422b5371b..5e298301db8 100644 --- a/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php +++ b/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php @@ -30,6 +30,7 @@ public function test_get_client() { } public function test_refresh_token() { + $this->fake_authentication(); $user_id = $this->factory()->user->create(); wp_set_current_user( $user_id ); $client = new OAuth_Client( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ); From d04397dc9a2fd1a9bc1f051b2af2009c8bcb884b Mon Sep 17 00:00:00 2001 From: Evan Mattson Date: Fri, 8 Nov 2019 10:27:49 +0200 Subject: [PATCH 4/5] add FakeHttpClient --- tests/phpunit/includes/FakeHttpClient.php | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/phpunit/includes/FakeHttpClient.php diff --git a/tests/phpunit/includes/FakeHttpClient.php b/tests/phpunit/includes/FakeHttpClient.php new file mode 100644 index 00000000000..14f41a12335 --- /dev/null +++ b/tests/phpunit/includes/FakeHttpClient.php @@ -0,0 +1,49 @@ +request_handler = $handler; + } + + /** + * @param RequestInterface $request + * + * @return \Google\Site_Kit_Dependencies\GuzzleHttp\Message\ResponseInterface + */ + public function send( RequestInterface $request ) { + if ( $this->request_handler ) { + return call_user_func( $this->request_handler, $request ); + } + + return new Response( 200 ); + } +} From 3857701c27cbd6c021ebdead2b5978d996a12719 Mon Sep 17 00:00:00 2001 From: Evan Mattson Date: Fri, 8 Nov 2019 10:28:16 +0200 Subject: [PATCH 5/5] use FakeHttpClient in refresh token test --- .../Core/Authentication/Clients/OAuth_ClientTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php b/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php index 5e298301db8..5ba62345a92 100644 --- a/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php +++ b/tests/phpunit/integration/Core/Authentication/Clients/OAuth_ClientTest.php @@ -13,6 +13,7 @@ use Google\Site_Kit\Context; use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client; use Google\Site_Kit\Tests\Exception\RedirectException; +use Google\Site_Kit\Tests\FakeHttpClient; use Google\Site_Kit\Tests\TestCase; /** @@ -50,7 +51,7 @@ public function test_refresh_token() { // Google client must be initialized first $this->assertEquals( 'refresh_token_not_exist', get_user_option( OAuth_Client::OPTION_ERROR_CODE, $user_id ) ); - $client->get_client(); + $client->get_client()->setHttpClient( new FakeHttpClient() ); $client->refresh_token(); // At this point an error is triggered internally due to undefined indexes on $authentication_token