Skip to content

Commit b2f8661

Browse files
return associative array when validating token
1 parent 82a49d6 commit b2f8661

File tree

6 files changed

+50
-43
lines changed

6 files changed

+50
-43
lines changed

src/Handlers/AbstractTokenHandler.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ protected function getBearerTokenValidator(): BearerTokenValidatorInterface
4343
}
4444

4545
/**
46-
* @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|array{0:null, 1:null}
46+
* @return array{type: non-empty-string, data: array<non-empty-string, mixed>}|null
4747
*
4848
* @throws OAuthServerException
4949
*/
5050
protected function validateToken(
5151
ServerRequestInterface $request,
5252
ClientEntityInterface $client
53-
): array {
53+
): ?array {
5454
$token = $this->getRequestParameter('token', $request)
5555
?? throw OAuthServerException::invalidRequest('token');
5656

@@ -60,17 +60,15 @@ protected function validateToken(
6060
// the search across all supported token types according to the RFC spec.
6161
if ($tokenTypeHint === 'refresh_token') {
6262
return $this->validateRefreshToken($request, $token, $client)
63-
?? $this->validateAccessToken($request, $token, $client)
64-
?? [null, null];
63+
?? $this->validateAccessToken($request, $token, $client);
6564
}
6665

6766
return $this->validateAccessToken($request, $token, $client)
68-
?? $this->validateRefreshToken($request, $token, $client)
69-
?? [null, null];
67+
?? $this->validateRefreshToken($request, $token, $client);
7068
}
7169

7270
/**
73-
* @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null
71+
* @return array{type: non-empty-string, data: array<non-empty-string, mixed>}|null
7472
*/
7573
private function validateRefreshToken(
7674
ServerRequestInterface $request,
@@ -79,8 +77,8 @@ private function validateRefreshToken(
7977
): ?array {
8078
try {
8179
return [
82-
'refresh_token',
83-
$this->validateEncryptedRefreshToken($request, $refreshToken, $client->getIdentifier()),
80+
'type' => 'refresh_token',
81+
'data' => $this->validateEncryptedRefreshToken($request, $refreshToken, $client->getIdentifier()),
8482
];
8583
} catch (Throwable) {
8684
return null;
@@ -90,7 +88,7 @@ private function validateRefreshToken(
9088
/**
9189
* @param non-empty-string $accessToken
9290
*
93-
* @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null
91+
* @return array{type: non-empty-string, data: array<non-empty-string, mixed>}|null
9492
*/
9593
private function validateAccessToken(
9694
ServerRequestInterface $request,
@@ -99,8 +97,12 @@ private function validateAccessToken(
9997
): ?array {
10098
try {
10199
return [
102-
'access_token',
103-
$this->getBearerTokenValidator()->validateBearerToken($request, $accessToken, $client->getIdentifier()),
100+
'type' => 'access_token',
101+
'data' => $this->getBearerTokenValidator()->validateBearerToken(
102+
$request,
103+
$accessToken,
104+
$client->getIdentifier()
105+
),
104106
];
105107
} catch (Throwable) {
106108
return null;

src/Handlers/TokenIntrospectionHandler.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ public function setResponseType(IntrospectionResponseTypeInterface $responseType
2626
public function respondToRequest(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
2727
{
2828
$client = $this->validateClient($request);
29-
[$tokenType, $tokenData] = $this->validateToken($request, $client);
29+
$token = $this->validateToken($request, $client);
3030

3131
$responseType = $this->getResponseType();
3232

33-
if ($tokenType === null || $tokenData === null) {
33+
if ($token === null) {
3434
$responseType->setActive(false);
3535
} else {
3636
$responseType->setActive(true);
37-
$responseType->setTokenType($tokenType);
38-
$responseType->setTokenData($tokenData);
37+
$responseType->setTokenType($token['type']);
38+
$responseType->setTokenData($token['data']);
3939
}
4040

4141
return $responseType->generateHttpResponse($response);

src/Handlers/TokenRevocationHandler.php

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

55
namespace League\OAuth2\Server\Handlers;
66

7+
use League\OAuth2\Server\Exception\OAuthServerException;
78
use Psr\Http\Message\ResponseInterface;
89
use Psr\Http\Message\ServerRequestInterface;
910

@@ -12,14 +13,16 @@ class TokenRevocationHandler extends AbstractTokenHandler
1213
public function respondToRequest(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
1314
{
1415
$client = $this->validateClient($request);
15-
[$tokenType, $tokenData] = $this->validateToken($request, $client);
16+
$token = $this->validateToken($request, $client);
1617

17-
if ($tokenType !== null && $tokenData !== null) {
18-
if ($tokenType === 'refresh_token') {
19-
$this->refreshTokenRepository->revokeRefreshToken($tokenData['refresh_token_id']);
20-
$this->accessTokenRepository->revokeAccessToken($tokenData['access_token_id']);
21-
} elseif ($tokenType === 'access_token') {
22-
$this->accessTokenRepository->revokeAccessToken($tokenData['jti']);
18+
if ($token !== null) {
19+
if ($token['type'] === 'refresh_token') {
20+
$this->refreshTokenRepository->revokeRefreshToken($token['data']['refresh_token_id']);
21+
$this->accessTokenRepository->revokeAccessToken($token['data']['access_token_id']);
22+
} elseif ($token['type'] === 'access_token') {
23+
$this->accessTokenRepository->revokeAccessToken($token['data']['jti']);
24+
} else {
25+
throw OauthServerException::unsupportedTokenType();
2326
}
2427
}
2528

tests/Handlers/AbstractTokenHandlerTest.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testSetBearerTokenValidator(): void
5454

5555
$result = (fn () => $this->validateToken($request, $client))->call($handler);
5656

57-
self::assertSame(['access_token', ['foo' => 'bar']], $result);
57+
self::assertSame(['type' => 'access_token', 'data' => ['foo' => 'bar']], $result);
5858
}
5959

6060
public function testValidateToken(): void
@@ -122,11 +122,10 @@ public function testValidateAccessToken(): void
122122
$client = new ClientEntity();
123123
$client->setIdentifier('client1');
124124

125-
/** @var array{0:non-empty-string, 1:array<non-empty-string, mixed>} $result */
126125
$result = (fn () => $this->validateToken($request, $client))->call($handler);
127-
$result[1]['exp'] = $result[1]['exp']->getTimestamp();
126+
$result['data']['exp'] = $result['data']['exp']->getTimestamp();
128127

129-
self::assertSame(['access_token', [
128+
self::assertSame(['type' => 'access_token', 'data' => [
130129
'aud' => ['client1'],
131130
'sub' => 'user1',
132131
'jti' => 'access1',
@@ -161,7 +160,7 @@ public function testValidateAccessTokenIsRevoked(): void
161160

162161
$result = (fn () => $this->validateToken($request, $client))->call($handler);
163162

164-
self::assertSame([null, null], $result);
163+
self::assertNull($result);
165164
}
166165

167166
public function testValidateAccessTokenIsExpired(): void
@@ -186,7 +185,7 @@ public function testValidateAccessTokenIsExpired(): void
186185

187186
$result = (fn () => $this->validateToken($request, $client))->call($handler);
188187

189-
self::assertSame([null, null], $result);
188+
self::assertNull($result);
190189
}
191190

192191
public function testValidateAccessTokenWithMismatchClient(): void
@@ -211,7 +210,7 @@ public function testValidateAccessTokenWithMismatchClient(): void
211210

212211
$result = (fn () => $this->validateToken($request, $client))->call($handler);
213212

214-
self::assertSame([null, null], $result);
213+
self::assertNull($result);
215214
}
216215

217216
public function testValidateAccessTokenWithInvalidToken(): void
@@ -230,7 +229,7 @@ public function testValidateAccessTokenWithInvalidToken(): void
230229

231230
$result = (fn () => $this->validateToken($request, $client))->call($handler);
232231

233-
self::assertSame([null, null], $result);
232+
self::assertNull($result);
234233
}
235234

236235
public function testValidateRefreshToken(): void
@@ -259,7 +258,7 @@ public function testValidateRefreshToken(): void
259258

260259
$result = (fn () => $this->validateToken($request, $client))->call($handler);
261260

262-
self::assertSame(['refresh_token', [
261+
self::assertSame(['type' => 'refresh_token', 'data' => [
263262
'refresh_token_id' => 'refresh1',
264263
'expire_time' => $expireTime,
265264
'client_id' => 'client1',
@@ -292,7 +291,7 @@ public function testValidateRefreshTokenIsRevoked(): void
292291

293292
$result = (fn () => $this->validateToken($request, $client))->call($handler);
294293

295-
self::assertSame([null, null], $result);
294+
self::assertNull($result);
296295
}
297296

298297
public function testValidateRefreshTokenIsExpired(): void
@@ -316,7 +315,7 @@ public function testValidateRefreshTokenIsExpired(): void
316315

317316
$result = (fn () => $this->validateToken($request, $client))->call($handler);
318317

319-
self::assertSame([null, null], $result);
318+
self::assertNull($result);
320319
}
321320

322321
public function testValidateRefreshTokenWithMismatchClient(): void
@@ -340,7 +339,7 @@ public function testValidateRefreshTokenWithMismatchClient(): void
340339

341340
$result = (fn () => $this->validateToken($request, $client))->call($handler);
342341

343-
self::assertSame([null, null], $result);
342+
self::assertNull($result);
344343
}
345344

346345
public function testValidateRefreshTokenWithInvalidToken(): void
@@ -359,7 +358,7 @@ public function testValidateRefreshTokenWithInvalidToken(): void
359358

360359
$result = (fn () => $this->validateToken($request, $client))->call($handler);
361360

362-
self::assertSame([null, null], $result);
361+
self::assertNull($result);
363362
}
364363

365364
/**

tests/Handlers/TokenIntrospectionHandlerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testRespondToRequestForAccessToken(): void
4343
$handler->expects(self::once())
4444
->method('validateToken')
4545
->with($request, $client)
46-
->willReturn(['access_token', ['jti' => 'access1']]);
46+
->willReturn(['type' => 'access_token', 'data' => ['jti' => 'access1']]);
4747

4848
$response = $handler->respondToRequest($request, new Response());
4949
$response->getBody()->rewind();
@@ -86,7 +86,7 @@ public function testRespondToRequestForRefreshToken(): void
8686
$handler->expects(self::once())
8787
->method('validateToken')
8888
->with($request, $client)
89-
->willReturn(['refresh_token', ['refresh_token_id' => 'refresh1']]);
89+
->willReturn(['type' => 'refresh_token', 'data' => ['refresh_token_id' => 'refresh1']]);
9090

9191
$response = $handler->respondToRequest($request, new Response());
9292
$response->getBody()->rewind();
@@ -129,7 +129,7 @@ public function testRespondToRequestForInvalidToken(): void
129129
$handler->expects(self::once())
130130
->method('validateToken')
131131
->with($request, $client)
132-
->willReturn([null, null]);
132+
->willReturn(null);
133133

134134
$response = $handler->respondToRequest($request, new Response());
135135
$response->getBody()->rewind();
@@ -176,7 +176,7 @@ public function testSetResponseType(): void
176176
$handler->expects(self::once())
177177
->method('validateToken')
178178
->with($request, $client)
179-
->willReturn(['foo', ['bar' => 'baz']]);
179+
->willReturn(['type' => 'foo', 'data' => ['bar' => 'baz']]);
180180

181181
$result = $handler->respondToRequest($request, $response);
182182

tests/Handlers/TokenRevocationHandlerTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testRespondToRequestForAccessToken(): void
4848
$handler->expects(self::once())
4949
->method('validateToken')
5050
->with($request, $client)
51-
->willReturn(['access_token', ['jti' => 'access1']]);
51+
->willReturn(['type' => 'access_token', 'data' => ['jti' => 'access1']]);
5252

5353
$response = $handler->respondToRequest($request, new Response());
5454
$response->getBody()->rewind();
@@ -92,7 +92,10 @@ public function testRespondToRequestForRefreshToken(): void
9292
$handler->expects(self::once())
9393
->method('validateToken')
9494
->with($request, $client)
95-
->willReturn(['refresh_token', ['refresh_token_id' => 'refresh1', 'access_token_id' => 'access1']]);
95+
->willReturn(['type' => 'refresh_token', 'data' => [
96+
'refresh_token_id' => 'refresh1',
97+
'access_token_id' => 'access1',
98+
]]);
9699

97100
$response = $handler->respondToRequest($request, new Response());
98101
$response->getBody()->rewind();
@@ -137,7 +140,7 @@ public function testRespondToRequestForInvalidToken(): void
137140
$handler->expects(self::once())
138141
->method('validateToken')
139142
->with($request, $client)
140-
->willReturn([null, null]);
143+
->willReturn(null);
141144

142145
$response = $handler->respondToRequest($request, new Response());
143146
$response->getBody()->rewind();

0 commit comments

Comments
 (0)