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