Passed
Pull Request — master (#1146)
by Andrew
39:17 queued 04:26
created

AccessTokenTrait::initJwtConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
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 8
cts 8
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
    public function initJwtConfiguration()
43 9
    {
44
        $privateKeyPassPhrase = $this->privateKey->getPassPhrase();
45 9
46 9
        $verificationKey = empty($privateKeyPassPhrase) ? InMemory::plainText('') : $privateKeyPassPhrase;
47 9
48 9
        $this->jwtConfiguration = Configuration::forAsymmetricSigner(
49 9
            new Sha256(),
50 9
            LocalFileReference::file($this->privateKey->getKeyPath()),
51 9
            $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

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