1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace League\OAuth2\Server\Handlers; |
6
|
|
|
|
7
|
|
|
use League\OAuth2\Server\AbstractHandler; |
8
|
|
|
use League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator; |
9
|
|
|
use League\OAuth2\Server\AuthorizationValidators\JwtValidatorInterface; |
10
|
|
|
use League\OAuth2\Server\CryptKeyInterface; |
11
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
12
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
|
15
|
|
|
abstract class AbstractTokenHandler extends AbstractHandler implements TokenHandlerInterface |
16
|
|
|
{ |
17
|
|
|
private CryptKeyInterface $publicKey; |
18
|
|
|
|
19
|
|
|
private ?JwtValidatorInterface $jwtValidator = null; |
20
|
|
|
|
21
|
|
|
public function setPublicKey(CryptKeyInterface $publicKey): void |
22
|
|
|
{ |
23
|
|
|
$this->publicKey = $publicKey; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setJwtValidator(JwtValidatorInterface $jwtValidator): void |
27
|
|
|
{ |
28
|
|
|
$this->jwtValidator = $jwtValidator; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function getJwtValidator(): JwtValidatorInterface |
32
|
|
|
{ |
33
|
|
|
if ($this->jwtValidator instanceof JwtValidatorInterface === false) { |
34
|
|
|
$this->jwtValidator = new BearerTokenValidator($this->accessTokenRepository); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($this->jwtValidator instanceof BearerTokenValidator === true) { |
38
|
|
|
$this->jwtValidator->setPublicKey($this->publicKey); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this->jwtValidator; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|array{0:null, 1:null} |
|
|
|
|
46
|
|
|
* |
47
|
|
|
* @throws OAuthServerException |
48
|
|
|
*/ |
49
|
|
|
protected function validateToken( |
50
|
|
|
ServerRequestInterface $request, |
51
|
|
|
ClientEntityInterface $client |
52
|
|
|
): array { |
53
|
|
|
$token = $this->getRequestParameter('token', $request) |
54
|
|
|
?? throw OAuthServerException::invalidRequest('token'); |
55
|
|
|
|
56
|
|
|
$tokenTypeHint = $this->getRequestParameter('token_type_hint', $request, 'access_token'); |
57
|
|
|
|
58
|
|
|
if ($tokenTypeHint === 'refresh_token') { |
59
|
|
|
return $this->validateRefreshToken($request, $token, $client) |
60
|
|
|
?? $this->validateAccessToken($request, $token, $client) |
61
|
|
|
?? [null, null]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->validateAccessToken($request, $token, $client) |
65
|
|
|
?? $this->validateRefreshToken($request, $token, $client) |
66
|
|
|
?? [null, null]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
protected function validateRefreshToken( |
73
|
|
|
ServerRequestInterface $request, |
74
|
|
|
string $refreshToken, |
75
|
|
|
ClientEntityInterface $client |
76
|
|
|
): ?array { |
77
|
|
|
try { |
78
|
|
|
return [ |
79
|
|
|
'refresh_token', |
80
|
|
|
$this->validateEncryptedRefreshToken($request, $refreshToken, $client->getIdentifier()), |
81
|
|
|
]; |
82
|
|
|
} catch (OAuthServerException) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param non-empty-string $accessToken |
|
|
|
|
89
|
|
|
* |
90
|
|
|
* @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
protected function validateAccessToken( |
93
|
|
|
ServerRequestInterface $request, |
94
|
|
|
string $accessToken, |
95
|
|
|
ClientEntityInterface $client |
96
|
|
|
): ?array { |
97
|
|
|
try { |
98
|
|
|
return [ |
99
|
|
|
'access_token', |
100
|
|
|
$this->getJwtValidator()->validateJwt($request, $accessToken, $client->getIdentifier()), |
101
|
|
|
]; |
102
|
|
|
} catch (OAuthServerException) { |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|