Skip to content

Commit

Permalink
applied refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
proggeler committed Apr 12, 2022
1 parent ee7cfdd commit 4b5dc9a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

7 changes: 5 additions & 2 deletions src/Provider/Trustpilot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
39 changes: 35 additions & 4 deletions tests/Provider/TrustpilotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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([
Expand All @@ -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'])],
];
}

Expand Down

0 comments on commit 4b5dc9a

Please sign in to comment.