Passed
Pull Request — master (#1328)
by
unknown
31:56
created

AccessTokenTrait::getSubjectIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * @author      Alex Bilbie <[email protected]>
5
 * @copyright   Copyright (c) Alex Bilbie
6
 * @license     http://mit-license.org/
7
 *
8
 * @link        https://github.com/thephpleague/oauth2-server
9
 */
10
11
declare(strict_types=1);
12
13
namespace League\OAuth2\Server\Entities\Traits;
14
15
use DateTimeImmutable;
16
use Lcobucci\JWT\Builder;
17
use Lcobucci\JWT\Configuration;
18
use Lcobucci\JWT\Signer\Key\InMemory;
19
use Lcobucci\JWT\Signer\Rsa\Sha256;
20
use Lcobucci\JWT\Token;
21
use League\OAuth2\Server\CryptKeyInterface;
22
use League\OAuth2\Server\Entities\ClientEntityInterface;
23
use League\OAuth2\Server\Entities\ScopeEntityInterface;
24
use RuntimeException;
25
26
trait AccessTokenTrait
27
{
28
    private CryptKeyInterface $privateKey;
29
30
    private Configuration $jwtConfiguration;
31
32
    /**
33
     * Set the private key used to encrypt this access token.
34 33
     */
35
    public function setPrivateKey(CryptKeyInterface $privateKey): void
36 33
    {
37
        $this->privateKey = $privateKey;
38
    }
39
40
    /**
41
     * Initialise the JWT Configuration.
42 9
     */
43
    public function initJwtConfiguration(): void
44 9
    {
45
        $privateKeyContents = $this->privateKey->getKeyContents();
46 9
47
        if ($privateKeyContents === '') {
48
            throw new RuntimeException('Private key is empty');
49
        }
50 9
51 9
        $this->jwtConfiguration = Configuration::forAsymmetricSigner(
52 9
            new Sha256(),
53 9
            InMemory::plainText($privateKeyContents, $this->privateKey->getPassPhrase() ?? ''),
54 9
            InMemory::plainText('empty', 'empty')
55
        );
56
    }
57
58
    /**
59
     * Configure the JWT builder instance.
60 9
     *
61
     * @return Builder
62 9
     */
63
    protected function withBuilder(Builder $builder)
64 9
    {
65 9
        return $builder;
66 9
    }
67 9
68 9
    /**
69 9
     * Generate a JWT from the access token
70 9
     */
71 9
    private function convertToJWT(): Token
72 9
    {
73
        $this->initJwtConfiguration();
74
75
        $builder = $this->jwtConfiguration->builder()
76
            ->permittedFor($this->getClient()->getIdentifier())
77
            ->identifiedBy($this->getIdentifier())
78 9
            ->issuedAt(new DateTimeImmutable())
79
            ->canOnlyBeUsedAfter(new DateTimeImmutable())
80 9
            ->expiresAt($this->getExpiryDateTime())
81
            ->relatedTo($this->getSubjectIdentifier())
82
            ->withClaim('scopes', $this->getScopes());
83
84
        return $this->withBuilder($builder)
85
            ->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
86
    }
87
88
    /**
89
     * Generate a string representation from the access token
90
     */
91
    public function toString(): string
92
    {
93
        return $this->convertToJWT()->toString();
94
    }
95
96
    abstract public function getClient(): ClientEntityInterface;
97
98
    abstract public function getExpiryDateTime(): DateTimeImmutable;
99
100
    /**
101
     * @return non-empty-string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
102
     */
103
    abstract public function getUserIdentifier(): string|null;
104
105 9
    /**
106
     * @return ScopeEntityInterface[]
107 9
     */
108
    abstract public function getScopes(): array;
109
110
    /**
111
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
112
     */
113
    abstract public function getIdentifier(): string;
114
115
    /**
116
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
117
     */
118
    private function getSubjectIdentifier(): string
119
    {
120
        return $this->getUserIdentifier() ?? $this->getClient()->getIdentifier();
121
    }
122
}
123