|
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\Builder; |
|
14
|
|
|
use Lcobucci\JWT\Signer\Key; |
|
15
|
|
|
use Lcobucci\JWT\Signer\Rsa\Sha256; |
|
16
|
|
|
use Lcobucci\JWT\Token; |
|
17
|
|
|
use League\OAuth2\Server\CryptKey; |
|
18
|
|
|
use League\OAuth2\Server\Entities\ClaimEntityInterface; |
|
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
|
|
|
* Set the private key used to encrypt this access token. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setPrivateKey(CryptKey $privateKey) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->privateKey = $privateKey; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Generate a JWT from the access token |
|
39
|
|
|
* |
|
40
|
|
|
* @param CryptKey $privateKey |
|
41
|
|
|
* |
|
42
|
|
|
* @return Token |
|
43
|
|
|
*/ |
|
44
|
|
|
private function convertToJWT(CryptKey $privateKey) |
|
45
|
|
|
{ |
|
46
|
|
|
$builder = new Builder(); |
|
47
|
|
|
$builder->permittedFor($this->getClient()->getIdentifier()) |
|
48
|
|
|
->identifiedBy($this->getIdentifier()) |
|
49
|
|
|
->issuedAt(\time()) |
|
50
|
|
|
->canOnlyBeUsedAfter(\time()) |
|
51
|
|
|
->expiresAt($this->getExpiryDateTime()->getTimestamp()) |
|
52
|
|
|
->relatedTo((string)$this->getUserIdentifier()); |
|
53
|
|
|
|
|
54
|
|
|
foreach ($this->getClaims() as $claim){ |
|
55
|
|
|
$builder->withClaim($claim->getName(), $claim->getValue()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $builder |
|
59
|
|
|
// Set scope claim late to prevent it from being overridden. |
|
60
|
|
|
->withClaim('scopes', $this->getScopes()) |
|
61
|
|
|
->getToken(new Sha256(), new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase())); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Generate a string representation from the access token |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __toString() |
|
68
|
|
|
{ |
|
69
|
|
|
return (string) $this->convertToJWT($this->privateKey); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return ClientEntityInterface |
|
74
|
|
|
*/ |
|
75
|
|
|
abstract public function getClient(); |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return DateTimeImmutable |
|
79
|
|
|
*/ |
|
80
|
|
|
abstract public function getExpiryDateTime(); |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string|int |
|
84
|
|
|
*/ |
|
85
|
|
|
abstract public function getUserIdentifier(); |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return ScopeEntityInterface[] |
|
89
|
|
|
*/ |
|
90
|
|
|
abstract public function getScopes(); |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return ClaimEntityInterface[] |
|
94
|
|
|
*/ |
|
95
|
|
|
abstract public function getClaims(); |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
abstract public function getIdentifier(); |
|
101
|
|
|
} |
|
102
|
|
|
|