Passed
Pull Request — master (#1122)
by Sebastian
02:10
created

AccessTokenTrait::convertToJWT()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
crap 2.0023
rs 9.8666
c 0
b 0
f 0
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\Builder;
14
use Lcobucci\JWT\Signer\Key;
15
use Lcobucci\JWT\Signer\Rsa\Sha256;
16
use Lcobucci\JWT\Token;
17
use League\OAuth2\Server\CryptKey;
18
use League\OAuth2\Server\Entities\ClaimEntityInterface;
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
     * Set the private key used to encrypt this access token.
31
     */
32 29
    public function setPrivateKey(CryptKey $privateKey)
33
    {
34 29
        $this->privateKey = $privateKey;
35 29
    }
36
37
    /**
38
     * Generate a JWT from the access token
39
     *
40
     * @param CryptKey $privateKey
41
     *
42
     * @return Token
43
     */
44 9
    private function convertToJWT(CryptKey $privateKey)
45
    {
46 9
        $builder = new Builder();
47 9
        $builder->permittedFor($this->getClient()->getIdentifier())
48 9
            ->identifiedBy($this->getIdentifier())
49 9
            ->issuedAt(\time())
50 9
            ->canOnlyBeUsedAfter(\time())
51 9
            ->expiresAt($this->getExpiryDateTime()->getTimestamp())
52 9
            ->relatedTo((string) $this->getUserIdentifier());
53
54 9
        foreach ($this->getClaims() as $claim) {
55
            $builder->withClaim($claim->getName(), $claim->getValue());
56
        }
57
58
        return $builder
59
            // Set scope claim late to prevent it from being overridden.
60 9
            ->withClaim('scopes', $this->getScopes())
61 9
            ->getToken(new Sha256(), new Key($privateKey->getKeyPath(), $privateKey->getPassPhrase()));
62
    }
63
64
    /**
65
     * Generate a string representation from the access token
66
     */
67 9
    public function __toString()
68
    {
69 9
        return (string) $this->convertToJWT($this->privateKey);
70
    }
71
72
    /**
73
     * @return ClientEntityInterface
74
     */
75
    abstract public function getClient();
76
77
    /**
78
     * @return DateTimeImmutable
79
     */
80
    abstract public function getExpiryDateTime();
81
82
    /**
83
     * @return string|int
84
     */
85
    abstract public function getUserIdentifier();
86
87
    /**
88
     * @return ScopeEntityInterface[]
89
     */
90
    abstract public function getScopes();
91
92
    /**
93
     * @return ClaimEntityInterface[]
94
     */
95
    abstract public function getClaims();
96
97
    /**
98
     * @return string
99
     */
100
    abstract public function getIdentifier();
101
}
102