Passed
Push — master ( 2b2d6e...305862 )
by Rutger
13:36
created

Oauth2AccessToken::convertToJWT()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\models;
4
5
use DateTimeImmutable;
6
use Lcobucci\JWT\Builder;
7
use League\OAuth2\Server\Entities\ClientEntityInterface;
8
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
9
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenInterface;
10
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenScopeInterface;
11
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ScopeInterface;
12
use rhertogh\Yii2Oauth2Server\models\behaviors\DateTimeBehavior;
13
use rhertogh\Yii2Oauth2Server\models\traits\Oauth2ClientRelationTrait;
14
use rhertogh\Yii2Oauth2Server\models\traits\Oauth2EntityIdentifierTrait;
15
use rhertogh\Yii2Oauth2Server\models\traits\Oauth2ExpiryDateTimeTrait;
16
use rhertogh\Yii2Oauth2Server\models\traits\Oauth2ScopesRelationTrait;
17
use rhertogh\Yii2Oauth2Server\models\traits\Oauth2TokenTrait;
18
use rhertogh\Yii2Oauth2Server\models\traits\Oauth2UserIdentifierTrait;
19
use yii\helpers\ArrayHelper;
20
21
/**
22
 * @property DateTimeImmutable $expiry_date_time
23
 * @property Oauth2Client $clientRelation
24
 * @property Oauth2ScopeInterface[] $scopesRelation
25
 */
26
class Oauth2AccessToken extends base\Oauth2AccessToken implements Oauth2AccessTokenInterface
27
{
28
    use Oauth2EntityIdentifierTrait;
29
    use AccessTokenTrait;
30
    use Oauth2TokenTrait;
31
    use Oauth2ExpiryDateTimeTrait;
32
    use Oauth2UserIdentifierTrait;
33
    use Oauth2ScopesRelationTrait;
34
    use Oauth2ClientRelationTrait {
35
        __set as clientRelationSetter;
36
        setClient as clientSetter;
37
    }
38
39
    public const TYPE_BEARER = 1;
40
    public const TYPE_MAC = 2;
41
42
    /////////////////////////////
43
    /// ActiveRecord Settings ///
44
    /////////////////////////////
45
46
    /**
47
     * @inheritDoc
48 18
     */
49
    public function behaviors()
50 18
    {
51 18
        return ArrayHelper::merge(parent::behaviors(), [
52 18
            'dateTimeBehavior' => DateTimeBehavior::class
53
        ]);
54
    }
55
56
    /////////////////////////
57
    /// Getters & Setters ///
58
    /////////////////////////
59
60
    /**
61
     * @inheritDoc
62 5
     */
63
    public function setClient(ClientEntityInterface $client)
64 5
    {
65 4
        $this->clientSetter($client);
66
        $this->type = Oauth2AccessToken::TYPE_BEARER; // Fixed for now.
67
    }
68
69
    /**
70
     * @inheritDoc
71 18
     */
72
    public function __set($name, $value)
73
    {
74 18
        // wrapper function to ensure the __set function of the Oauth2ClientRelationTrait is never overwritten.
75
        $this->clientRelationSetter($name, $value);
76
    }
77
78
    /**
79
     * @inheritDoc
80 1
     */
81
    public function getScopesRelationClassName()
82 1
    {
83
        return Oauth2AccessTokenScopeInterface::class;
84
    }
85
86
    /**
87
     * @return \Lcobucci\JWT\Token\Plain
88
     * @see https://github.com/thephpleague/oauth2-server/issues/885
89
     */
90
    private function convertToJWT()
0 ignored issues
show
Unused Code introduced by
The method convertToJWT() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
91
    {
92
        $this->initJwtConfiguration();
93
        $builder = $this->jwtConfiguration->builder();
94
        return $this->buildJwt($builder)->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
95
    }
96
97
    protected function buildJwt(Builder $builder)
98
    {
99
        // Default claims added by `\League\OAuth2\Server\Entities\Traits\AccessTokenTrait::convertToJWT`.
100
        $builder
101
            ->permittedFor($this->getClient()->getIdentifier())
102
            ->identifiedBy($this->getIdentifier())
103
            ->issuedAt(new DateTimeImmutable())
104
            ->canOnlyBeUsedAfter(new DateTimeImmutable())
105
            ->expiresAt($this->getExpiryDateTime())
106
            ->relatedTo((string) $this->getUserIdentifier())
107
            ->withClaim('scopes', $this->getScopes());
108
109
        // Additional claims
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
110
        $builder
111
            ->withClaim(static::TOKEN_CLAIM_CLIENT_ID, $this->getClient()->getIdentifier());
112
113
        return $builder;
114
    }
115
}
116