Skip to content

Commit

Permalink
WIP - Refresh Akeneo oauth token when it is expired
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianaromagnoli committed Feb 22, 2021
1 parent 0495008 commit 0655faf
Showing 1 changed file with 62 additions and 21 deletions.
83 changes: 62 additions & 21 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
final class ApiClient implements ApiClientInterface, AttributeOptionsApiClientInterface
{
/** @var string */
private $token;
private $accessToken;

/** @var string */
private $refreshToken;

/** @var ClientInterface */
private $httpClient;
Expand Down Expand Up @@ -74,21 +77,30 @@ public function authenticatedRequest(string $uri, string $method, array $headers
$uri = $this->baseUrl . $uri;
}

if (!(bool) $this->token) {
if (!(bool) $this->accessToken) {
$this->login();
}

$headers = array_merge(
$headers,
[
'Content-Type' => 'application/json',
'Authorization' => sprintf('Bearer %s', $this->token),
'Authorization' => sprintf('Bearer %s', $this->accessToken),
]
);
$request = new Request($method, $uri, $headers);
$response = $this->httpClient->send($request);
$statusClass = (int) ($response->getStatusCode() / 100);
$responseResult = json_decode($response->getBody()->getContents(), true);

$accessTokenHasExpired = $response->getStatusCode() === 401
&& (string) $responseResult['message'] === 'The access token provided has expired.';
if ($accessTokenHasExpired) {
$this->refreshAccessToken();

return $this->authenticatedRequest($uri, $method, $headers);
}

if ($statusClass !== 2) {
throw new \HttpException($responseResult['message'], $responseResult['code']);
}
Expand Down Expand Up @@ -132,7 +144,7 @@ public function findAttribute(string $code): ?array
public function downloadFile(string $code): \SplFileInfo
{
$endpoint = sprintf('/api/rest/v1/media-files/%s/download', $code);
$headers = ['Authorization' => sprintf('Bearer %s', $this->token)];
$headers = ['Authorization' => sprintf('Bearer %s', $this->accessToken)];
$request = new Request('GET', $this->baseUrl . $endpoint, $headers);
$response = $this->httpClient->send($request);
$statusClass = (int) ($response->getStatusCode() / 100);
Expand Down Expand Up @@ -202,25 +214,25 @@ private function login(): void
]
);
Assert::string($body);
$headers = [
'Content-Type' => 'application/json',
];
$request = new Request(
'POST',
$this->baseUrl . '/api/oauth/v1/token',
$headers,
$body
$responseResult = $this->makeOauthRequest($body);

$this->accessToken = $responseResult['access_token'];
$this->refreshToken = $responseResult['refresh_token'];
}

private function refreshAccessToken(): void
{
$body = json_encode(
[
'grant_type' => 'refresh_token',
'refresh_token' => $this->refreshToken,
]
);
$options = [
'auth' => [
$this->clientId,
$this->secret,
],
];
$rawResponse = $this->httpClient->send($request, $options);
$responseResult = json_decode($rawResponse->getBody()->getContents(), true);
Assert::string($body);
$responseResult = $this->makeOauthRequest($body);

$this->token = $responseResult['access_token'];
$this->accessToken = $responseResult['access_token'];
$this->refreshToken = $responseResult['refresh_token'];
}

/**
Expand Down Expand Up @@ -268,4 +280,33 @@ private function generateTempFilePath(): string

return $this->temporaryFilesManager->generateTemporaryFilePath();
}

/**
* @param bool $body
*
* @return mixed
*
* @throws GuzzleException
*/
private function makeOauthRequest(string $body)
{
$headers = [
'Content-Type' => 'application/json',
];
$request = new Request(
'POST',
$this->baseUrl . '/api/oauth/v1/token',
$headers,
$body
);
$options = [
'auth' => [
$this->clientId,
$this->secret,
],
];
$rawResponse = $this->httpClient->send($request, $options);

return json_decode($rawResponse->getBody()->getContents(), true);
}
}

0 comments on commit 0655faf

Please sign in to comment.