Identity   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 8
dl 0
loc 122
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A issue() 0 20 2
A claim() 0 8 2
A parse() 0 12 4
A verifyToken() 0 11 5
A tokenIdentity() 0 4 1
A resolveAudience() 0 8 2
A resolveTokenExpiration() 0 8 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/jwt/license
6
 * @link       https://www.flipboxfactory.com/jwt/organization/
7
 */
8
9
namespace flipbox\craft\jwt\services;
10
11
use Craft;
12
use craft\elements\User;
13
use flipbox\craft\jwt\Jwt;
14
use flipbox\craft\jwt\helpers\TokenHelper;
15
use flipbox\craft\jwt\helpers\UserHelper;
16
use Lcobucci\JWT\Token;
17
use yii\base\Component;
18
use yii\web\IdentityInterface;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class Identity extends Component
25
{
26
    /**
27
     * Issue an authorization JWT token on behalf of a user.
28
     *
29
     * @param string $user
30
     * @param string|null $audience
31
     * @param int|null $expiration
32
     * @return Token|null
33
     * @throws \craft\errors\SiteNotFoundException
34
     * @throws \yii\base\InvalidConfigException
35
     */
36
    public function issue(
37
        $user = 'CURRENT_USER',
38
        string $audience = null,
39
        int $expiration = null
40
    ) {
41
        if (null === ($identity = UserHelper::resolveUser($user))) {
42
            $identity = new User();
43
        }
44
45
        return Jwt::getInstance()->getBuilder()
46
            ->setIssuer(Jwt::getInstance()->getSettings()->getIssuer())
47
            ->setAudience($this->resolveAudience($audience))
48
            ->setId($identity->getId(), true)
49
            ->setIssuedAt(time())
50
            ->setNotBefore(time())
51
            ->setExpiration($this->resolveTokenExpiration($expiration))
52
            ->set(TokenHelper::CLAIM_CSRF, Craft::$app->getRequest()->getCsrfToken())
53
            ->sign(Jwt::getInstance()->getSettings()->getSigner(), TokenHelper::getSignatureKey($identity))
54
            ->getToken();
55
    }
56
57
    /**
58
     * This
59
     * @param string $token
60
     * @return bool|null|IdentityInterface
61
     * @throws \craft\errors\SiteNotFoundException
62
     */
63
    public function claim(string $token)
64
    {
65
        if (null === ($token = $this->parse($token))) {
66
            return false;
67
        }
68
69
        return $this->tokenIdentity($token);
70
    }
71
72
    /**
73
     * @param $token
74
     * @param bool $validate
75
     * @param bool $verify
76
     * @return Token|null
77
     * @throws \craft\errors\SiteNotFoundException
78
     */
79
    public function parse(string $token, bool $validate = true, bool $verify = true)
80
    {
81
        if (null === ($token = TokenHelper::parse($token, $validate))) {
82
            return null;
83
        }
84
85
        if ($verify && !$this->verifyToken($token)) {
86
            return null;
87
        }
88
89
        return $token;
90
    }
91
92
    /**
93
     * @param Token $token
94
     * @return bool
95
     * @throws \craft\errors\SiteNotFoundException
96
     */
97
    public function verifyToken(Token $token): bool
98
    {
99
        if (null === ($identity = UserHelper::resolveUser($token->getClaim(TokenHelper::CLAIM_IDENTITY)))) {
100
            $identity = new User();
101
        }
102
103
        return TokenHelper::verifyTokenCsrfClaim($token) &&
104
            TokenHelper::verifyIssuer($token, Jwt::getInstance()->getSettings()->getIdentityIssuers()) &&
105
            TokenHelper::verifyAudience($token) &&
106
            TokenHelper::verifyTokenSignature($token, $identity);
107
    }
108
109
    /**
110
     * @param Token $token
111
     * @return null|IdentityInterface
112
     */
113
    private function tokenIdentity(Token $token)
114
    {
115
        return UserHelper::resolveUser($token->getClaim(TokenHelper::CLAIM_IDENTITY));
116
    }
117
118
    /**
119
     * @param string|null $audience
120
     * @return string
121
     * @throws \craft\errors\SiteNotFoundException
122
     */
123
    private function resolveAudience(string $audience = null): string
124
    {
125
        if ($audience === null) {
126
            $audience = Jwt::getInstance()->getSettings()->getIdentityAudience();
127
        }
128
129
        return (string)$audience;
130
    }
131
132
    /**
133
     * @param int|null $expiration
134
     * @return int
135
     * @throws \yii\base\InvalidConfigException
136
     */
137
    private function resolveTokenExpiration(int $expiration = null): int
138
    {
139
        if ($expiration === null) {
140
            $expiration = Jwt::getInstance()->getSettings()->getIdentityTokenDuration();
141
        }
142
143
        return time() + (int)$expiration;
144
    }
145
}
146