Skip to content

Commit 6eac316

Browse files
committed
refactor: move API call to get new access token into another method
1 parent a4a0574 commit 6eac316

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/VerifyAccessToken.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class VerifyAccessToken
1313
{
14+
protected $accessTokenCacheKey = 'access_token';
15+
1416
private $client = null;
1517

1618
private function getClient(): Client
@@ -48,32 +50,33 @@ protected function getIntrospect($accessToken)
4850

4951
protected function getAccessToken(): string
5052
{
51-
$accessToken = Cache::get('access_token');
53+
$accessToken = Cache::get($this->accessTokenCacheKey);
5254

53-
if (!$accessToken) {
54-
$guzzle = $this->getClient();
55+
return $accessToken ?: $this->getNewAccessToken();
56+
}
5557

56-
$response = $guzzle->post(config('authorizationserver.token_url'), [
57-
'form_params' => [
58-
'grant_type' => 'client_credentials',
59-
'client_id' => config('authorizationserver.client_id'),
60-
'client_secret' => config('authorizationserver.client_secret'),
61-
'scope' => '',
62-
],
63-
]);
58+
protected function getNewAccessToken(): string
59+
{
60+
$response = $this->getClient()->post(config('authorizationserver.token_url'), [
61+
'form_params' => [
62+
'grant_type' => 'client_credentials',
63+
'client_id' => config('authorizationserver.client_id'),
64+
'client_secret' => config('authorizationserver.client_secret'),
65+
'scope' => '',
66+
],
67+
]);
6468

65-
$result = json_decode((string) $response->getBody(), true);
69+
$result = json_decode((string) $response->getBody(), true);
6670

67-
if ($result && isset($result['access_token'])) {
68-
$accessToken = $result['access_token'];
71+
if (isset($result['access_token'])) {
72+
$accessToken = $result['access_token'];
6973

70-
Cache::add('access_token', $accessToken, intVal($result['expires_in']) / 60);
71-
} else {
72-
throw new InvalidEndpointException('Did not receive an access token');
73-
}
74+
Cache::add($this->accesstokenCacheKey, $accessToken, intVal($result['expires_in']) / 60);
75+
76+
return $accessToken;
7477
}
7578

76-
return $accessToken;
79+
throw new InvalidEndpointException('Did not receive an access token');
7780
}
7881

7982
/**

0 commit comments

Comments
 (0)