Skip to content

Commit 81af443

Browse files
authored
Merge pull request #2 from designmynight/bit-of-tidying
Bit of tidying
2 parents 413b27e + 7e7a202 commit 81af443

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

src/VerifyAccessToken.php

Lines changed: 32 additions & 36 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
@@ -31,13 +33,9 @@ public function setClient(Client $client): self
3133

3234
protected function getIntrospect($accessToken)
3335
{
34-
$guzzle = $this->getClient();
35-
36-
$response = $guzzle->post(config('authorizationserver.introspect_url'), [
36+
$response = $this->getClient()->post(config('authorizationserver.introspect_url'), [
3737
'form_params' => [
3838
'token_type_hint' => 'access_token',
39-
40-
// This is the access token for verifying the user's access token
4139
'token' => $accessToken,
4240
],
4341
'headers' => [
@@ -50,32 +48,33 @@ protected function getIntrospect($accessToken)
5048

5149
protected function getAccessToken(): string
5250
{
53-
$accessToken = Cache::get('access_token');
51+
$accessToken = Cache::get($this->accessTokenCacheKey);
5452

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

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

67-
$result = json_decode((string) $response->getBody(), true);
67+
$result = json_decode((string) $response->getBody(), true);
6868

69-
if ($result && isset($result['access_token'])) {
70-
$accessToken = $result['access_token'];
69+
if (isset($result['access_token'])) {
70+
$accessToken = $result['access_token'];
7171

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

78-
return $accessToken;
77+
throw new InvalidEndpointException('Did not receive an access token');
7978
}
8079

8180
/**
@@ -99,39 +98,36 @@ public function handle($request, Closure $next, ...$scopes)
9998
throw new InvalidInputException('No Bearer token in the Authorization header present');
10099
}
101100

102-
// Now verify the user provided access token
103101
try {
104102
$result = $this->getIntrospect($bearerToken);
103+
105104
if (!$result['active']) {
106105
throw new InvalidAccessTokenException('Invalid token!');
107-
} else if ($scopes != null) {
106+
}
107+
108+
if ($scopes != null) {
108109
if (!\is_array($scopes)) {
109-
$scopes = [
110-
$scopes,
111-
];
110+
$scopes = [$scopes];
112111
}
113112

114113
$scopesForToken = \explode(' ', $result['scope']);
115114

116115
if (count($misingScopes = array_diff($scopes, $scopesForToken)) > 0) {
117116
throw new InvalidAccessTokenException('Missing the following required scopes: ' . implode(' ,', $misingScopes));
118-
} else {
119117
}
120118
}
121119
} catch (RequestException $e) {
122120
if ($e->hasResponse()) {
123121
$result = json_decode((string) $e->getResponse()->getBody(), true);
124122

125-
var_dump($result);exit;
126-
127123
if (isset($result['error'])) {
128124
throw new InvalidAccessTokenException($result['error']['title'] ?? 'Invalid token!');
129-
} else {
130-
throw new InvalidAccessTokenException('Invalid token!');
131125
}
132-
} else {
133-
throw new InvalidAccessTokenException($e);
126+
127+
throw new InvalidAccessTokenException('Invalid token!');
134128
}
129+
130+
throw new InvalidAccessTokenException($e);
135131
}
136132

137133
return $next($request);

0 commit comments

Comments
 (0)