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\Configuration; |
17
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory; |
18
|
|
|
use Lcobucci\JWT\Signer\Rsa\Sha256; |
19
|
|
|
use Lcobucci\JWT\Token; |
20
|
|
|
use League\OAuth2\Server\CryptKeyInterface; |
21
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
22
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
23
|
|
|
use RuntimeException; |
24
|
|
|
|
25
|
|
|
trait AccessTokenTrait |
26
|
|
|
{ |
27
|
|
|
private CryptKeyInterface $privateKey; |
28
|
|
|
|
29
|
|
|
private Configuration $jwtConfiguration; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set the private key used to encrypt this access token. |
33
|
|
|
*/ |
34
|
33 |
|
public function setPrivateKey(CryptKeyInterface $privateKey): void |
35
|
|
|
{ |
36
|
33 |
|
$this->privateKey = $privateKey; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Initialise the JWT Configuration. |
41
|
|
|
*/ |
42
|
9 |
|
public function initJwtConfiguration(): void |
43
|
|
|
{ |
44
|
9 |
|
$privateKeyContents = $this->privateKey->getKeyContents(); |
45
|
|
|
|
46
|
9 |
|
if ($privateKeyContents === '') { |
47
|
|
|
throw new RuntimeException('Private key is empty'); |
48
|
|
|
} |
49
|
|
|
|
50
|
9 |
|
$this->jwtConfiguration = Configuration::forAsymmetricSigner( |
51
|
9 |
|
new Sha256(), |
52
|
9 |
|
InMemory::plainText($privateKeyContents, $this->privateKey->getPassPhrase() ?? ''), |
53
|
9 |
|
InMemory::plainText('empty', 'empty') |
54
|
9 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Generate a JWT from the access token |
59
|
|
|
*/ |
60
|
9 |
|
private function convertToJWT(): Token |
61
|
|
|
{ |
62
|
9 |
|
$this->initJwtConfiguration(); |
63
|
|
|
|
64
|
9 |
|
return $this->jwtConfiguration->builder() |
65
|
9 |
|
->permittedFor($this->getClient()->getIdentifier()) |
66
|
9 |
|
->identifiedBy($this->getIdentifier()) |
67
|
9 |
|
->issuedAt(new DateTimeImmutable()) |
68
|
9 |
|
->canOnlyBeUsedAfter(new DateTimeImmutable()) |
69
|
9 |
|
->expiresAt($this->getExpiryDateTime()) |
70
|
9 |
|
->relatedTo($this->getSubjectIdentifier()) |
71
|
9 |
|
->withClaim('scopes', $this->getScopes()) |
72
|
9 |
|
->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Generate a string representation from the access token |
77
|
|
|
*/ |
78
|
9 |
|
public function toString(): string |
79
|
|
|
{ |
80
|
9 |
|
return $this->convertToJWT()->toString(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
abstract public function getClient(): ClientEntityInterface; |
84
|
|
|
|
85
|
|
|
abstract public function getExpiryDateTime(): DateTimeImmutable; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return non-empty-string|null |
|
|
|
|
89
|
|
|
*/ |
90
|
|
|
abstract public function getUserIdentifier(): string|null; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return ScopeEntityInterface[] |
94
|
|
|
*/ |
95
|
|
|
abstract public function getScopes(): array; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return non-empty-string |
|
|
|
|
99
|
|
|
*/ |
100
|
|
|
abstract public function getIdentifier(): string; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return non-empty-string |
|
|
|
|
104
|
|
|
*/ |
105
|
9 |
|
private function getSubjectIdentifier(): string |
106
|
|
|
{ |
107
|
9 |
|
return $this->getUserIdentifier() ?? $this->getClient()->getIdentifier(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|