|
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
|
|
|
use Throwable; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractTokenHandler extends AbstractHandler implements TokenHandlerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private CryptKeyInterface $publicKey; |
|
19
|
|
|
|
|
20
|
|
|
private ?JwtValidatorInterface $jwtValidator = null; |
|
21
|
|
|
|
|
22
|
|
|
public function setPublicKey(CryptKeyInterface $publicKey): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->publicKey = $publicKey; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function setJwtValidator(JwtValidatorInterface $jwtValidator): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->jwtValidator = $jwtValidator; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function getJwtValidator(): JwtValidatorInterface |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->jwtValidator instanceof JwtValidatorInterface === false) { |
|
35
|
|
|
$this->jwtValidator = new BearerTokenValidator($this->accessTokenRepository); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($this->jwtValidator instanceof BearerTokenValidator === true) { |
|
39
|
|
|
$this->jwtValidator->setPublicKey($this->publicKey); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $this->jwtValidator; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|array{0:null, 1:null} |
|
|
|
|
|
|
47
|
|
|
* |
|
48
|
|
|
* @throws OAuthServerException |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function validateToken( |
|
51
|
|
|
ServerRequestInterface $request, |
|
52
|
|
|
ClientEntityInterface $client |
|
53
|
|
|
): array { |
|
54
|
|
|
$token = $this->getRequestParameter('token', $request) |
|
55
|
|
|
?? throw OAuthServerException::invalidRequest('token'); |
|
56
|
|
|
|
|
57
|
|
|
$tokenTypeHint = $this->getRequestParameter('token_type_hint', $request, 'access_token'); |
|
58
|
|
|
|
|
59
|
|
|
if ($tokenTypeHint === 'refresh_token') { |
|
60
|
|
|
return $this->validateRefreshToken($request, $token, $client) |
|
61
|
|
|
?? $this->validateAccessToken($request, $token, $client) |
|
62
|
|
|
?? [null, null]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->validateAccessToken($request, $token, $client) |
|
66
|
|
|
?? $this->validateRefreshToken($request, $token, $client) |
|
67
|
|
|
?? [null, null]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null |
|
|
|
|
|
|
72
|
|
|
*/ |
|
73
|
|
|
protected function validateRefreshToken( |
|
74
|
|
|
ServerRequestInterface $request, |
|
75
|
|
|
string $refreshToken, |
|
76
|
|
|
ClientEntityInterface $client |
|
77
|
|
|
): ?array { |
|
78
|
|
|
try { |
|
79
|
|
|
return [ |
|
80
|
|
|
'refresh_token', |
|
81
|
|
|
$this->validateEncryptedRefreshToken($request, $refreshToken, $client->getIdentifier()), |
|
82
|
|
|
]; |
|
83
|
|
|
} catch (Throwable) { |
|
84
|
|
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param non-empty-string $accessToken |
|
|
|
|
|
|
90
|
|
|
* |
|
91
|
|
|
* @return array{0:non-empty-string, 1:array<non-empty-string, mixed>}|null |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
|
|
protected function validateAccessToken( |
|
94
|
|
|
ServerRequestInterface $request, |
|
95
|
|
|
string $accessToken, |
|
96
|
|
|
ClientEntityInterface $client |
|
97
|
|
|
): ?array { |
|
98
|
|
|
try { |
|
99
|
|
|
return [ |
|
100
|
|
|
'access_token', |
|
101
|
|
|
$this->getJwtValidator()->validateJwt($request, $accessToken, $client->getIdentifier()), |
|
102
|
|
|
]; |
|
103
|
|
|
} catch (Throwable) { |
|
104
|
|
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.