Passed
Pull Request — master (#1328)
by
unknown
34:37
created

AccessTokenTrait::withBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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\Configuration;
14
use Lcobucci\JWT\Signer\Key\InMemory;
15
use Lcobucci\JWT\Signer\Rsa\Sha256;
16
use Lcobucci\JWT\Builder;
17
use Lcobucci\JWT\Token;
18
use League\OAuth2\Server\CryptKey;
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
     * @var Configuration
31
     */
32
    private $jwtConfiguration;
33
34
    /**
35
     * Set the private key used to encrypt this access token.
36 31
     */
37
    public function setPrivateKey(CryptKey $privateKey)
38 31
    {
39 31
        $this->privateKey = $privateKey;
40
    }
41
42
    /**
43
     * Initialise the JWT Configuration.
44 9
     */
45
    public function initJwtConfiguration()
46 9
    {
47 9
        $this->jwtConfiguration = Configuration::forAsymmetricSigner(
48 9
            new Sha256(),
49 9
            InMemory::plainText($this->privateKey->getKeyContents(), $this->privateKey->getPassPhrase() ?? ''),
50
            InMemory::plainText('empty', 'empty')
51 9
        );
52
    }
53
54
    /**
55
     * Configure the JWT builder instance.
56
     *
57
     * @return Builder
58 9
     */
59
    protected function withBuilder(Builder $builder)
60 9
    {
61
        return $builder;
62 9
    }
63 9
64 9
    /**
65 9
     * Generate a JWT from the access token
66 9
     *
67 9
     * @return Token
68 9
     */
69 9
    private function convertToJWT()
70 9
    {
71
        $this->initJwtConfiguration();
72
73
        $builder = $this->jwtConfiguration->builder()
74
            ->permittedFor($this->getClient()->getIdentifier())
75
            ->identifiedBy($this->getIdentifier())
76 9
            ->issuedAt(new DateTimeImmutable())
77
            ->canOnlyBeUsedAfter(new DateTimeImmutable())
78 9
            ->expiresAt($this->getExpiryDateTime())
79
            ->relatedTo((string) $this->getUserIdentifier())
80
            ->withClaim('scopes', $this->getScopes());
81
82
        return $this->withBuilder($builder)
83
            ->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
84
    }
85
86
    /**
87
     * Generate a string representation from the access token
88
     */
89
    public function __toString()
90
    {
91
        return $this->convertToJWT()->toString();
92
    }
93
94
    /**
95
     * @return ClientEntityInterface
96
     */
97
    abstract public function getClient();
98
99
    /**
100
     * @return DateTimeImmutable
101
     */
102
    abstract public function getExpiryDateTime();
103
104
    /**
105
     * @return string|int
106
     */
107
    abstract public function getUserIdentifier();
108
109
    /**
110
     * @return ScopeEntityInterface[]
111
     */
112
    abstract public function getScopes();
113
114
    /**
115
     * @return string
116
     */
117
    abstract public function getIdentifier();
118
}
119