|
11 | 11 |
|
12 | 12 | class VerifyAccessToken |
13 | 13 | { |
| 14 | + protected $accessTokenCacheKey = 'access_token'; |
| 15 | + |
14 | 16 | private $client = null; |
15 | 17 |
|
16 | 18 | private function getClient(): Client |
@@ -48,32 +50,33 @@ protected function getIntrospect($accessToken) |
48 | 50 |
|
49 | 51 | protected function getAccessToken(): string |
50 | 52 | { |
51 | | - $accessToken = Cache::get('access_token'); |
| 53 | + $accessToken = Cache::get($this->accessTokenCacheKey); |
52 | 54 |
|
53 | | - if (!$accessToken) { |
54 | | - $guzzle = $this->getClient(); |
| 55 | + return $accessToken ?: $this->getNewAccessToken(); |
| 56 | + } |
55 | 57 |
|
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 | + ]); |
64 | 68 |
|
65 | | - $result = json_decode((string) $response->getBody(), true); |
| 69 | + $result = json_decode((string) $response->getBody(), true); |
66 | 70 |
|
67 | | - if ($result && isset($result['access_token'])) { |
68 | | - $accessToken = $result['access_token']; |
| 71 | + if (isset($result['access_token'])) { |
| 72 | + $accessToken = $result['access_token']; |
69 | 73 |
|
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; |
74 | 77 | } |
75 | 78 |
|
76 | | - return $accessToken; |
| 79 | + throw new InvalidEndpointException('Did not receive an access token'); |
77 | 80 | } |
78 | 81 |
|
79 | 82 | /** |
|
0 commit comments