|
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\ClientEntityInterface; |
|
19
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
|
20
|
|
|
|
|
21
|
|
|
trait AccessTokenTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var CryptKey |
|
25
|
|
|
*/ |
|
26
|
|
|
private $privateKey; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Set the private key used to encrypt this access token. |
|
30
|
|
|
*/ |
|
31
|
29 |
|
public function setPrivateKey(CryptKey $privateKey) |
|
32
|
|
|
{ |
|
33
|
29 |
|
$this->privateKey = $privateKey; |
|
34
|
29 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Generate a JWT from the access token |
|
38
|
|
|
* |
|
39
|
|
|
* @param CryptKey $privateKey |
|
40
|
|
|
* |
|
41
|
|
|
* @return Token |
|
42
|
|
|
*/ |
|
43
|
9 |
|
private function convertToJWT(CryptKey $privateKey) |
|
44
|
|
|
{ |
|
45
|
9 |
|
return (new Builder()) |
|
46
|
9 |
|
->setAudience($this->getClient()->getIdentifier()) |
|
47
|
9 |
|
->setId($this->getIdentifier()) |
|
|
|
|
|
|
48
|
9 |
|
->setIssuedAt(time()) |
|
49
|
9 |
|
->setNotBefore(time()) |
|
50
|
9 |
|
->setExpiration($this->getExpiryDateTime()->getTimestamp()) |
|
51
|
9 |
|
->setSubject((string) $this->getUserIdentifier()) |
|
52
|
9 |
|
->set('scopes', $this->getScopes()) |
|
53
|
9 |
|
->sign(new Sha256(), new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase())) |
|
54
|
9 |
|
->getToken(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Generate a string representation from the access token |
|
59
|
|
|
*/ |
|
60
|
9 |
|
public function __toString() |
|
61
|
|
|
{ |
|
62
|
9 |
|
return (string) $this->convertToJWT($this->privateKey); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return ClientEntityInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
abstract public function getClient(); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return DateTimeImmutable |
|
72
|
|
|
*/ |
|
73
|
|
|
abstract public function getExpiryDateTime(); |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string|int |
|
77
|
|
|
*/ |
|
78
|
|
|
abstract public function getUserIdentifier(); |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return ScopeEntityInterface[] |
|
82
|
|
|
*/ |
|
83
|
|
|
abstract public function getScopes(); |
|
84
|
|
|
} |
|
85
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.