Passed
Push — master ( 3a707c...4b4eca )
by Andrew
31:16 queued 29:56
created

AccessTokenTrait::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
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 0
dl 0
loc 3
ccs 2
cts 2
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\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
     */
32
    private $jwtConfiguration;
33
34
    /**
35
     * Set the private key used to encrypt this access token.
36
     */
37 29
    public function setPrivateKey(CryptKey $privateKey)
38
    {
39 29
        $this->privateKey = $privateKey;
40 29
    }
41
42
    /**
43
     * Initialise the JWT Configuration.
44
     */
45 9
    public function initJwtConfiguration()
46
    {
47 9
        $this->jwtConfiguration = Configuration::forAsymmetricSigner(
48 9
            new Sha256(),
49 9
            LocalFileReference::file($this->privateKey->getKeyPath(), $this->privateKey->getPassPhrase() ?? ''),
50 9
            InMemory::plainText('')
51
        );
52 9
    }
53
54
    /**
55
     * Generate a JWT from the access token
56
     *
57
     * @return Token
58
     */
59 9
    private function convertToJWT()
60
    {
61 9
        $this->initJwtConfiguration();
62
63 9
        return $this->jwtConfiguration->builder()
64 9
            ->permittedFor($this->getClient()->getIdentifier())
65 9
            ->identifiedBy($this->getIdentifier())
66 9
            ->issuedAt(new DateTimeImmutable())
67 9
            ->canOnlyBeUsedAfter(new DateTimeImmutable())
68 9
            ->expiresAt($this->getExpiryDateTime())
69 9
            ->relatedTo((string) $this->getUserIdentifier())
70 9
            ->withClaim('scopes', $this->getScopes())
71 9
            ->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
72
    }
73
74
    /**
75
     * Generate a string representation from the access token
76
     */
77 9
    public function __toString()
78
    {
79 9
        return $this->convertToJWT()->toString();
80
    }
81
82
    /**
83
     * @return ClientEntityInterface
84
     */
85
    abstract public function getClient();
86
87
    /**
88
     * @return DateTimeImmutable
89
     */
90
    abstract public function getExpiryDateTime();
91
92
    /**
93
     * @return string|int
94
     */
95
    abstract public function getUserIdentifier();
96
97
    /**
98
     * @return ScopeEntityInterface[]
99
     */
100
    abstract public function getScopes();
101
102
    /**
103
     * @return string
104
     */
105
    abstract public function getIdentifier();
106
}
107