AccessTokenTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 83
ccs 25
cts 26
cp 0.9615
rs 10
c 6
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initJwtConfiguration() 0 12 2
A getSubjectIdentifier() 0 3 1
A convertToJWT() 0 13 1
A toString() 0 3 1
A setPrivateKey() 0 3 1
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\Configuration;
17
use Lcobucci\JWT\Signer\Key\InMemory;
18
use Lcobucci\JWT\Signer\Rsa\Sha256;
19
use Lcobucci\JWT\Token;
20
use League\OAuth2\Server\CryptKeyInterface;
21
use League\OAuth2\Server\Entities\ClientEntityInterface;
22
use League\OAuth2\Server\Entities\ScopeEntityInterface;
23
use RuntimeException;
24
25
trait AccessTokenTrait
26
{
27
    private CryptKeyInterface $privateKey;
28
29
    private Configuration $jwtConfiguration;
30
31
    /**
32
     * Set the private key used to encrypt this access token.
33
     */
34 33
    public function setPrivateKey(CryptKeyInterface $privateKey): void
35
    {
36 33
        $this->privateKey = $privateKey;
37
    }
38
39
    /**
40
     * Initialise the JWT Configuration.
41
     */
42 9
    public function initJwtConfiguration(): void
43
    {
44 9
        $privateKeyContents = $this->privateKey->getKeyContents();
45
46 9
        if ($privateKeyContents === '') {
47
            throw new RuntimeException('Private key is empty');
48
        }
49
50 9
        $this->jwtConfiguration = Configuration::forAsymmetricSigner(
51 9
            new Sha256(),
52 9
            InMemory::plainText($privateKeyContents, $this->privateKey->getPassPhrase() ?? ''),
53 9
            InMemory::plainText('empty', 'empty')
54 9
        );
55
    }
56
57
    /**
58
     * Generate a JWT from the access token
59
     */
60 9
    private function convertToJWT(): Token
61
    {
62 9
        $this->initJwtConfiguration();
63
64 9
        return $this->jwtConfiguration->builder()
65 9
            ->permittedFor($this->getClient()->getIdentifier())
66 9
            ->identifiedBy($this->getIdentifier())
67 9
            ->issuedAt(new DateTimeImmutable())
68 9
            ->canOnlyBeUsedAfter(new DateTimeImmutable())
69 9
            ->expiresAt($this->getExpiryDateTime())
70 9
            ->relatedTo($this->getSubjectIdentifier())
71 9
            ->withClaim('scopes', $this->getScopes())
72 9
            ->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
73
    }
74
75
    /**
76
     * Generate a string representation from the access token
77
     */
78 9
    public function toString(): string
79
    {
80 9
        return $this->convertToJWT()->toString();
81
    }
82
83
    abstract public function getClient(): ClientEntityInterface;
84
85
    abstract public function getExpiryDateTime(): DateTimeImmutable;
86
87
    /**
88
     * @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...
89
     */
90
    abstract public function getUserIdentifier(): string|null;
91
92
    /**
93
     * @return ScopeEntityInterface[]
94
     */
95
    abstract public function getScopes(): array;
96
97
    /**
98
     * @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...
99
     */
100
    abstract public function getIdentifier(): string;
101
102
    /**
103
     * @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...
104
     */
105 9
    private function getSubjectIdentifier(): string
106
    {
107 9
        return $this->getUserIdentifier() ?? $this->getClient()->getIdentifier();
108
    }
109
}
110