|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Alex Bilbie <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) Alex Bilbie |
|
6
|
|
|
* @license http://mit-license.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace League\OAuth2\Server\Entities\Traits; |
|
14
|
|
|
|
|
15
|
|
|
use DateTimeImmutable; |
|
16
|
|
|
use Lcobucci\JWT\Builder; |
|
17
|
|
|
use Lcobucci\JWT\Configuration; |
|
18
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory; |
|
19
|
|
|
use Lcobucci\JWT\Signer\Rsa\Sha256; |
|
20
|
|
|
use Lcobucci\JWT\Token; |
|
21
|
|
|
use League\OAuth2\Server\CryptKeyInterface; |
|
22
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
23
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
|
24
|
|
|
use RuntimeException; |
|
25
|
|
|
|
|
26
|
|
|
trait AccessTokenTrait |
|
27
|
|
|
{ |
|
28
|
|
|
private CryptKeyInterface $privateKey; |
|
29
|
|
|
|
|
30
|
|
|
private Configuration $jwtConfiguration; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Set the private key used to encrypt this access token. |
|
34
|
34 |
|
*/ |
|
35
|
|
|
public function setPrivateKey(CryptKeyInterface $privateKey): void |
|
36
|
34 |
|
{ |
|
37
|
|
|
$this->privateKey = $privateKey; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Initialise the JWT Configuration. |
|
42
|
10 |
|
*/ |
|
43
|
|
|
public function initJwtConfiguration(): void |
|
44
|
10 |
|
{ |
|
45
|
|
|
$privateKeyContents = $this->privateKey->getKeyContents(); |
|
46
|
10 |
|
|
|
47
|
|
|
if ($privateKeyContents === '') { |
|
48
|
|
|
throw new RuntimeException('Private key is empty'); |
|
49
|
|
|
} |
|
50
|
10 |
|
|
|
51
|
10 |
|
$this->jwtConfiguration = Configuration::forAsymmetricSigner( |
|
52
|
10 |
|
new Sha256(), |
|
53
|
10 |
|
InMemory::plainText($privateKeyContents, $this->privateKey->getPassPhrase() ?? ''), |
|
54
|
10 |
|
InMemory::plainText('empty', 'empty') |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Configure the JWT builder instance. |
|
60
|
10 |
|
*/ |
|
61
|
|
|
protected function withJwtBuilder(Builder $builder): Builder |
|
62
|
10 |
|
{ |
|
63
|
|
|
return $builder; |
|
64
|
10 |
|
} |
|
65
|
10 |
|
|
|
66
|
10 |
|
/** |
|
67
|
10 |
|
* Generate a JWT from the access token |
|
68
|
10 |
|
*/ |
|
69
|
10 |
|
private function convertToJWT(): Token |
|
70
|
10 |
|
{ |
|
71
|
10 |
|
$this->initJwtConfiguration(); |
|
72
|
10 |
|
|
|
73
|
|
|
return $this->withJwtBuilder($this->jwtConfiguration->builder() |
|
74
|
|
|
->permittedFor($this->getClient()->getIdentifier()) |
|
75
|
|
|
->identifiedBy($this->getIdentifier()) |
|
76
|
|
|
->issuedAt(new DateTimeImmutable()) |
|
77
|
|
|
->canOnlyBeUsedAfter(new DateTimeImmutable()) |
|
78
|
10 |
|
->expiresAt($this->getExpiryDateTime()) |
|
79
|
|
|
->relatedTo($this->getSubjectIdentifier()) |
|
80
|
10 |
|
->withClaim('scopes', $this->getScopes())) |
|
81
|
|
|
->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Generate a string representation from the access token |
|
86
|
|
|
*/ |
|
87
|
|
|
public function toString(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->convertToJWT()->toString(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
abstract public function getClient(): ClientEntityInterface; |
|
93
|
|
|
|
|
94
|
|
|
abstract public function getExpiryDateTime(): DateTimeImmutable; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return non-empty-string|null |
|
|
|
|
|
|
98
|
|
|
*/ |
|
99
|
|
|
abstract public function getUserIdentifier(): string|null; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return ScopeEntityInterface[] |
|
103
|
|
|
*/ |
|
104
|
|
|
abstract public function getScopes(): array; |
|
105
|
10 |
|
|
|
106
|
|
|
/** |
|
107
|
10 |
|
* @return non-empty-string |
|
|
|
|
|
|
108
|
|
|
*/ |
|
109
|
|
|
abstract public function getIdentifier(): string; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return non-empty-string |
|
|
|
|
|
|
113
|
|
|
*/ |
|
114
|
|
|
private function getSubjectIdentifier(): string |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->getUserIdentifier() ?? $this->getClient()->getIdentifier(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|