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
|
|
|
public function convertToJWT(CryptKey $privateKey) |
31
|
|
|
{ |
32
|
|
|
return (new Builder()) |
33
|
|
|
->setAudience($this->getClient()->getIdentifier()) |
34
|
|
|
->setId($this->getIdentifier(), true) |
|
|
|
|
35
|
|
|
->setIssuedAt(time()) |
36
|
|
|
->setNotBefore(time()) |
37
|
|
|
->setExpiration($this->getExpiryDateTime()->getTimestamp()) |
38
|
|
|
->setSubject($this->getUserIdentifier()) |
39
|
|
|
->set('scopes', $this->getScopes()) |
40
|
|
|
->sign(new Sha256(), new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase())) |
41
|
|
|
->getToken(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return ClientEntityInterface |
46
|
|
|
*/ |
47
|
|
|
abstract public function getClient(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return DateTime |
51
|
|
|
*/ |
52
|
|
|
abstract public function getExpiryDateTime(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string|int |
56
|
|
|
*/ |
57
|
|
|
abstract public function getUserIdentifier(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return ScopeEntityInterface[] |
61
|
|
|
*/ |
62
|
|
|
abstract public function getScopes(); |
63
|
|
|
} |
64
|
|
|
|
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
Idable
provides a methodequalsId
that 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.