Passed
Push — master ( e886a2...8837ed )
by Andrew
29:07 queued 27:32
created

AccessTokenTrait::initJwtConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 2
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\Key\LocalFileReference;
16
use Lcobucci\JWT\Signer\Rsa\Sha256;
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 29
     */
32
    private $jwtConfiguration;
33 29
34 29
    /**
35
     * Set the private key used to encrypt this access token.
36
     */
37
    public function setPrivateKey(CryptKey $privateKey)
38
    {
39
        $this->privateKey = $privateKey;
40
    }
41
42
    /**
43 9
     * Initialise the JWT Configuration.
44
     */
45 9
    public function initJwtConfiguration()
46 9
    {
47 9
        $privateKeyPassPhrase = $this->privateKey->getPassPhrase();
48 9
49 9
        $verificationKey = empty($privateKeyPassPhrase) ? InMemory::plainText('') : $privateKeyPassPhrase;
50 9
51 9
        $this->jwtConfiguration = Configuration::forAsymmetricSigner(
52 9
            new Sha256(),
53 9
            LocalFileReference::file($this->privateKey->getKeyPath()),
54
            $verificationKey
0 ignored issues
show
Bug introduced by
It seems like $verificationKey can also be of type string; however, parameter $verificationKey of Lcobucci\JWT\Configuration::forAsymmetricSigner() does only seem to accept Lcobucci\JWT\Signer\Key, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            /** @scrutinizer ignore-type */ $verificationKey
Loading history...
55
        );
56
    }
57
58
    /**
59 9
     * Generate a JWT from the access token
60
     *
61 9
     * @return Token
62
     */
63
    private function convertToJWT()
64
    {
65
        $this->initJwtConfiguration();
66
67
        return $this->jwtConfiguration->builder()
68
            ->permittedFor($this->getClient()->getIdentifier())
69
            ->identifiedBy($this->getIdentifier())
70
            ->issuedAt(new DateTimeImmutable())
71
            ->canOnlyBeUsedAfter(new DateTimeImmutable())
72
            ->expiresAt($this->getExpiryDateTime())
73
            ->relatedTo((string) $this->getUserIdentifier())
74
            ->withClaim('scopes', $this->getScopes())
75
            ->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
76
    }
77
78
    /**
79
     * Generate a string representation from the access token
80
     */
81
    public function __toString()
82
    {
83
        return $this->convertToJWT()->toString();
84
    }
85
86
    /**
87
     * @return ClientEntityInterface
88
     */
89
    abstract public function getClient();
90
91
    /**
92
     * @return DateTimeImmutable
93
     */
94
    abstract public function getExpiryDateTime();
95
96
    /**
97
     * @return string|int
98
     */
99
    abstract public function getUserIdentifier();
100
101
    /**
102
     * @return ScopeEntityInterface[]
103
     */
104
    abstract public function getScopes();
105
106
    /**
107
     * @return string
108
     */
109
    abstract public function getIdentifier();
110
}
111