1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Alex Bilbie <[email protected]> |
4
|
|
|
* @copyright Copyright (c) Alex Bilbie |
5
|
|
|
* @license http://mit-license.org/ |
6
|
|
|
* |
7
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace League\OAuth2\Server\AuthorizationValidators; |
11
|
|
|
|
12
|
|
|
use DateTimeZone; |
13
|
|
|
use Lcobucci\Clock\SystemClock; |
14
|
|
|
use Lcobucci\JWT\Configuration; |
15
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory; |
16
|
|
|
use Lcobucci\JWT\Signer\Key\LocalFileReference; |
17
|
|
|
use Lcobucci\JWT\Signer\Rsa\Sha256; |
18
|
|
|
use Lcobucci\JWT\Validation\Constraint\SignedWith; |
19
|
|
|
use Lcobucci\JWT\Validation\Constraint\ValidAt; |
20
|
|
|
use Lcobucci\JWT\Validation\RequiredConstraintsViolated; |
21
|
|
|
use League\OAuth2\Server\CryptKey; |
22
|
|
|
use League\OAuth2\Server\CryptTrait; |
23
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
24
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
25
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
26
|
|
|
|
27
|
|
|
class BearerTokenValidator implements AuthorizationValidatorInterface |
28
|
|
|
{ |
29
|
|
|
use CryptTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AccessTokenRepositoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $accessTokenRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var CryptKey |
38
|
|
|
*/ |
39
|
|
|
protected $publicKey; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Configuration |
43
|
|
|
*/ |
44
|
|
|
private $jwtConfiguration; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param AccessTokenRepositoryInterface $accessTokenRepository |
48
|
|
|
*/ |
49
|
11 |
|
public function __construct(AccessTokenRepositoryInterface $accessTokenRepository) |
50
|
|
|
{ |
51
|
11 |
|
$this->accessTokenRepository = $accessTokenRepository; |
52
|
11 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the public key |
56
|
|
|
* |
57
|
|
|
* @param CryptKey $key |
58
|
|
|
*/ |
59
|
11 |
|
public function setPublicKey(CryptKey $key) |
60
|
|
|
{ |
61
|
11 |
|
$this->publicKey = $key; |
62
|
|
|
|
63
|
11 |
|
$this->initJwtConfiguration(); |
64
|
11 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initialise the JWT configuration. |
68
|
|
|
*/ |
69
|
11 |
|
private function initJwtConfiguration() |
70
|
|
|
{ |
71
|
11 |
|
$this->jwtConfiguration = Configuration::forSymmetricSigner( |
72
|
11 |
|
new Sha256(), |
73
|
11 |
|
InMemory::plainText('') |
74
|
|
|
); |
75
|
|
|
|
76
|
11 |
|
$this->jwtConfiguration->setValidationConstraints( |
77
|
11 |
|
new ValidAt(new SystemClock(new DateTimeZone(\date_default_timezone_get()))), |
78
|
11 |
|
new SignedWith(new Sha256(), LocalFileReference::file($this->publicKey->getKeyPath())) |
79
|
|
|
); |
80
|
11 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
11 |
|
public function validateAuthorization(ServerRequestInterface $request) |
86
|
|
|
{ |
87
|
11 |
|
if ($request->hasHeader('authorization') === false) { |
88
|
1 |
|
throw OAuthServerException::accessDenied('Missing "Authorization" header'); |
89
|
|
|
} |
90
|
|
|
|
91
|
10 |
|
$header = $request->getHeader('authorization'); |
92
|
10 |
|
$jwt = \trim((string) \preg_replace('/^(?:\s+)?Bearer\s/', '', $header[0])); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
// Attempt to parse the JWT |
96
|
10 |
|
$token = $this->jwtConfiguration->parser()->parse($jwt); |
97
|
3 |
|
} catch (\Lcobucci\JWT\Exception $exception) { |
98
|
3 |
|
throw OAuthServerException::accessDenied($exception->getMessage(), null, $exception); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
try { |
102
|
|
|
// Attempt to validate the JWT |
103
|
7 |
|
$constraints = $this->jwtConfiguration->validationConstraints(); |
104
|
7 |
|
$this->jwtConfiguration->validator()->assert($token, ...$constraints); |
105
|
3 |
|
} catch (RequiredConstraintsViolated $exception) { |
106
|
3 |
|
throw OAuthServerException::accessDenied('Access token could not be verified'); |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
$claims = $token->claims(); |
110
|
|
|
|
111
|
|
|
// Check if token has been revoked |
112
|
4 |
|
if ($this->accessTokenRepository->isAccessTokenRevoked($claims->get('jti'))) { |
113
|
1 |
|
throw OAuthServerException::accessDenied('Access token has been revoked'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Return the request with additional attributes |
117
|
|
|
return $request |
118
|
3 |
|
->withAttribute('oauth_access_token_id', $claims->get('jti')) |
119
|
3 |
|
->withAttribute('oauth_client_id', $this->convertSingleRecordAudToString($claims->get('aud'))) |
120
|
3 |
|
->withAttribute('oauth_user_id', $claims->get('sub')) |
121
|
3 |
|
->withAttribute('oauth_scopes', $claims->get('scopes')) |
122
|
|
|
->withAttribute('oauth_custom_claims', $this->extractCustomClaims($claims->all())); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Convert single record arrays into strings to ensure backwards compatibility between v4 and v3.x of lcobucci/jwt |
127
|
|
|
* |
128
|
|
|
* @param mixed $aud |
129
|
|
|
* |
130
|
|
|
* @return array|string |
131
|
3 |
|
*/ |
132
|
|
|
private function convertSingleRecordAudToString($aud) |
133
|
3 |
|
{ |
134
|
|
|
return \is_array($aud) && \count($aud) === 1 ? $aud[0] : $aud; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Extract custom claims |
139
|
|
|
* |
140
|
|
|
* @param array $claims |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
private function extractCustomClaims($claims) |
145
|
|
|
{ |
146
|
|
|
return \array_diff_key($claims, \array_flip(['jti', 'aud', 'sub', 'scopes', 'iat', 'nbf', 'exp'])); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|