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 Lcobucci\JWT\Builder; |
13
|
|
|
use Lcobucci\JWT\Signer\Key; |
14
|
|
|
use Lcobucci\JWT\Signer\Rsa\Sha256; |
15
|
|
|
use Lcobucci\JWT\Token; |
16
|
|
|
use League\OAuth2\Server\CryptKey; |
17
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
18
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
19
|
|
|
|
20
|
|
|
trait AccessTokenTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Generate a JWT from the access token |
24
|
|
|
* |
25
|
|
|
* @param CryptKey $privateKey |
26
|
|
|
* |
27
|
|
|
* @return Token |
28
|
|
|
*/ |
29
|
|
|
public function convertToJWT(CryptKey $privateKey) |
30
|
|
|
{ |
31
|
|
|
return (new Builder()) |
32
|
|
|
->setAudience($this->getClient()->getIdentifier()) |
33
|
|
|
->setId($this->getIdentifier(), true) |
|
|
|
|
34
|
|
|
->setIssuedAt(time()) |
35
|
|
|
->setNotBefore(time()) |
36
|
|
|
->setExpiration($this->getExpiryDateTime()->getTimestamp()) |
37
|
|
|
->setSubject($this->getUserIdentifier()) |
38
|
|
|
->set('scopes', $this->getScopes()) |
39
|
|
|
->sign(new Sha256(), new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase())) |
40
|
|
|
->getToken(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return ClientEntityInterface |
45
|
|
|
*/ |
46
|
|
|
abstract public function getClient(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return \DateTime |
50
|
|
|
*/ |
51
|
|
|
abstract public function getExpiryDateTime(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string|int |
55
|
|
|
*/ |
56
|
|
|
abstract public function getUserIdentifier(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return ScopeEntityInterface[] |
60
|
|
|
*/ |
61
|
|
|
abstract public function getScopes(); |
62
|
|
|
} |
63
|
|
|
|
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.