diff --git a/README.md b/README.md index d90ae69..4c86b33 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ try { } ``` -> see more examples on [League/oauth2-client](https://oauth2-client.thephpleague.com/usage/) +> see more examples on [league/oauth2-client](https://oauth2-client.thephpleague.com/usage/) +## Error handling + +Unfortunately the response body in case of an error is not consistent. To better understand the reason a request is +failed call the `IdentityProviderException::getResponseBody` method. diff --git a/src/Provider/Trustpilot.php b/src/Provider/Trustpilot.php index f15b07a..a57f58c 100644 --- a/src/Provider/Trustpilot.php +++ b/src/Provider/Trustpilot.php @@ -38,6 +38,10 @@ public function getBaseAuthorizationUrl() */ public function getBaseAccessTokenUrl(array $params) { + if (!empty($params['grant_type']) && $params['grant_type'] === 'refresh_token') { + return 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/refresh'; + } + return 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken'; } @@ -76,8 +80,7 @@ protected function getDefaultScopes() protected function checkResponse(ResponseInterface $response, $data) { if ($response->getStatusCode() >= 400) { - $message = $data['fault']['faultstring'] ?? $response->getReasonPhrase(); - throw new IdentityProviderException($message, $response->getStatusCode(), $data); + throw new IdentityProviderException($response->getReasonPhrase(), $response->getStatusCode(), $data); } } diff --git a/tests/Provider/TrustpilotTest.php b/tests/Provider/TrustpilotTest.php index 5081e4a..042a748 100644 --- a/tests/Provider/TrustpilotTest.php +++ b/tests/Provider/TrustpilotTest.php @@ -92,6 +92,38 @@ public function testGetAccessToken() $this->assertNotEmpty($accessToken->getResourceOwnerId()); } + public function testRefreshAccessToken() + { + $accessToken = new AccessToken([ + 'access_token' => '52f30f0e9bd8d74c8a7e7eb2', + 'resource_owner_id' => '2d41b60214b2fc6f2e2647e8', + 'refresh_token' => 'c91edc1ad4d069b117ed7dc7', + 'expires_in' => "359999", + ]); + + $response = new Response(200, ['Content-Type' => 'application/json']); + $response->getBody()->write(file_get_contents(__DIR__ . '/../data/token.json')); + + $client = new Client([ + 'handler' => HandlerStack::create( + new MockHandler([$response]) + ) + ]); + + $this->provider->setHttpClient($client); + + $accessToken = $this->provider->getAccessToken( + 'refresh_token', [ + 'refresh_token' => $accessToken->getRefreshToken() + ] + ); + + $this->assertFalse($accessToken->hasExpired()); + $this->assertNotEmpty($accessToken->getToken()); + $this->assertNotEmpty($accessToken->getRefreshToken()); + $this->assertNotEmpty($accessToken->getResourceOwnerId()); + } + public function testGetResourceOwner() { $accessToken = new AccessToken([ @@ -131,10 +163,9 @@ public function testGetAccessTokenFailure(ResponseInterface $response) { $body = $response->getBody()->getContents(); $data = $body ? json_decode($body, true) : []; - $message = $data['fault']['faultstring'] ?? $response->getReasonPhrase(); $this->expectExceptionObject( - new IdentityProviderException($message, $response->getStatusCode(), $data) + new IdentityProviderException($response->getReasonPhrase(), $response->getStatusCode(), $data) ); $client = new Client([ @@ -156,9 +187,9 @@ public function testGetAccessTokenFailure(ResponseInterface $response) public function provideFailure() { return [ - [new Response(401, [], json_encode(['fault' => ['faultstring' => 'Token Expired']]))], - [new Response(401, ['Content-Type' => 'text/plain'])], + [new Response(401, [], '{"reason": "Authentication Failed"}')], [new Response(429, ['Content-Type' => 'text/plain'])], + [new Response(503, ['Content-Type' => 'text/plain'])], ]; }