|
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 DateTime; |
|
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
|
|
|
* Generate a JWT from the access token |
|
25
|
|
|
* |
|
26
|
|
|
* @param CryptKey $privateKey |
|
27
|
|
|
* |
|
28
|
|
|
* @return Token |
|
29
|
|
|
*/ |
|
30
|
9 |
|
public function convertToJWT(CryptKey $privateKey) |
|
31
|
|
|
{ |
|
32
|
9 |
|
$builder = new Builder(); |
|
33
|
|
|
|
|
34
|
9 |
|
$this->setDataToBuilder($builder); |
|
35
|
9 |
|
$this->signDataInBuider($builder, $privateKey); |
|
36
|
|
|
|
|
37
|
9 |
|
return $builder->getToken(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return ClientEntityInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract public function getClient(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return DateTime |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract public function getExpiryDateTime(); |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return string|int |
|
52
|
|
|
*/ |
|
53
|
|
|
abstract public function getUserIdentifier(); |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return ScopeEntityInterface[] |
|
57
|
|
|
*/ |
|
58
|
|
|
abstract public function getScopes(); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set data parameters to token builder. |
|
62
|
|
|
* |
|
63
|
|
|
* @param Builder $builder |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
9 |
|
protected function setDataToBuilder(Builder $builder) |
|
68
|
|
|
{ |
|
69
|
|
|
$builder |
|
70
|
9 |
|
->setAudience($this->getClient()->getIdentifier()) |
|
71
|
9 |
|
->setId($this->getIdentifier(), true) |
|
|
|
|
|
|
72
|
9 |
|
->setIssuedAt(time()) |
|
73
|
9 |
|
->setNotBefore(time()) |
|
74
|
9 |
|
->setExpiration($this->getExpiryDateTime()->getTimestamp()) |
|
75
|
9 |
|
->setSubject($this->getUserIdentifier()) |
|
76
|
9 |
|
->set('scopes', $this->getScopes()) |
|
77
|
|
|
; |
|
78
|
9 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Sign data in token builder. |
|
82
|
|
|
* |
|
83
|
|
|
* @param Builder $builder |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
9 |
|
protected function signDataInBuider(Builder $builder, CryptKey $privateKey) |
|
88
|
|
|
{ |
|
89
|
9 |
|
$signer = new Sha256(); |
|
90
|
9 |
|
$key = new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase()); |
|
91
|
|
|
|
|
92
|
9 |
|
$builder->sign($signer, $key); |
|
93
|
9 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.