Skip to content

Commit b50d721

Browse files
authored
Merge pull request #225 from akeneo/API-1734-fix-unauthorized-error-when-token-cant-be-refresh
API-1734: Throw proper exception when automatically refreshing token fails
2 parents 5b8e8c9 + 70392c5 commit b50d721

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/Client/AuthenticatedHttpClient.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Akeneo\Pim\ApiClient\Api\AuthenticationApiInterface;
66
use Akeneo\Pim\ApiClient\Exception\UnauthorizedHttpException;
7+
use Akeneo\Pim\ApiClient\Exception\UnprocessableEntityHttpException;
78
use Akeneo\Pim\ApiClient\Security\Authentication;
89
use Psr\Http\Message\ResponseInterface;
910

@@ -62,11 +63,7 @@ public function sendRequest(string $httpMethod, $uri, array $headers = [], $body
6263
$headers['Authorization'] = sprintf('Bearer %s', $this->authentication->getAccessToken());
6364
$response = $this->basicHttpClient->sendRequest($httpMethod, $uri, $headers, $body);
6465
} catch (UnauthorizedHttpException $e) {
65-
$tokens = $this->authenticationApi->authenticateByRefreshToken(
66-
$this->authentication->getClientId(),
67-
$this->authentication->getSecret(),
68-
$this->authentication->getRefreshToken()
69-
);
66+
$tokens = $this->renewTokens($e);
7067

7168
$this->authentication
7269
->setAccessToken($tokens['access_token'])
@@ -78,4 +75,17 @@ public function sendRequest(string $httpMethod, $uri, array $headers = [], $body
7875

7976
return $response;
8077
}
78+
79+
private function renewTokens(UnauthorizedHttpException $unauthorizedHttpException): array
80+
{
81+
try {
82+
return $this->authenticationApi->authenticateByRefreshToken(
83+
$this->authentication->getClientId(),
84+
$this->authentication->getSecret(),
85+
$this->authentication->getRefreshToken()
86+
);
87+
} catch (UnprocessableEntityHttpException $e) {
88+
throw $unauthorizedHttpException;
89+
}
90+
}
8191
}

0 commit comments

Comments
 (0)