JwtService   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 145
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getTokenForClaim() 0 7 1
A getClaimFromToken() 0 8 1
A refreshToken() 0 18 2
A invalidateToken() 0 6 1
A wipeUserTokens() 0 4 1
A getValidClaimFromToken() 0 21 4
1
<?php
2
3
namespace WWON\JwtGuard;
4
5
use Firebase\JWT\ExpiredException;
6
use Firebase\JWT\JWT;
7
use Illuminate\Support\Facades\Config;
8
use WWON\JwtGuard\Contract\ClaimManager as ClaimManagerContract;
9
use WWON\JwtGuard\Exceptions\InaccessibleException;
10
use WWON\JwtGuard\Exceptions\InvalidTokenException;
11
use WWON\JwtGuard\Exceptions\MalformedException;
12
use WWON\JwtGuard\Exceptions\TokenExpiredException;
13
use WWON\JwtGuard\Exceptions\UnRefreshableException;
14
15
class JwtService
16
{
17
18
    /**
19
     * @var string
20
     */
21
    private $key;
22
23
    /**
24
     * @var ClaimManagerContract
25
     */
26
    protected $claimManager;
27
28
    /**
29
     * JwtService constructor
30
     *
31
     * @param ClaimManagerContract $claimManager
32
     */
33
    public function __construct(ClaimManagerContract $claimManager)
34
    {
35
        $this->key = Config::get('jwt.secret');
36
        $this->claimManager = $claimManager;
37
38
        JWT::$leeway = Config::get('jwt.leeway');
39
    }
40
41
    /**
42
     * getTokenForUser method
43
     *
44
     * @param Claim $claim
45
     * @return string
46
     */
47
    public function getTokenForClaim(Claim $claim)
48
    {
49
        $token = JWT::encode($claim->toArray(), $this->key, Config::get('jwt.algo'));
50
        $this->claimManager->add($claim);
51
52
        return $token;
53
    }
54
55
    /**
56
     * getEntityFromToken method
57
     *
58
     * @param string $token
59
     * @return Claim
60
     * @throws InaccessibleException
61
     * @throws MalformedException
62
     * @throws TokenExpiredException
63
     * @throws InvalidTokenException
64
     */
65
    public function getClaimFromToken($token)
66
    {
67
        $claim = $this->getValidClaimFromToken($token);
68
69
        $claim->validateAccessible();
70
71
        return $claim;
72
    }
73
74
    /**
75
     * refreshToken method
76
     *
77
     * @param string $token
78
     * @return string
79
     * @throws MalformedException
80
     * @throws TokenExpiredException
81
     * @throws InvalidTokenException
82
     * @throws UnRefreshableException
83
     */
84
    public function refreshToken($token)
85
    {
86
        $claim = $this->getValidClaimFromToken($token);
87
        
88
        if (empty($claim->refresh)) {
89
            throw new UnRefreshableException;
90
        }
91
92
        $this->claimManager->remove($claim);
93
94
        $newClaim = new Claim([
95
            'sub' => $claim->sub,
96
            'aud' => $claim->aud,
97
            'refresh' => $claim->refresh
98
        ]);
99
100
        return $this->getTokenForClaim($newClaim);
101
    }
102
103
    /**
104
     * invalidateToken method
105
     *
106
     * @param string $token
107
     * @throws MalformedException
108
     * @throws TokenExpiredException
109
     * @throws UnRefreshableException
110
     */
111
    public function invalidateToken($token)
112
    {
113
        $claim = $this->getClaimFromToken($token);
114
115
        $this->claimManager->remove($claim);
116
    }
117
118
    /**
119
     * wipeUserTokens method
120
     *
121
     * @param Claim $claim
122
     */
123
    public function wipeUserTokens(Claim $claim)
124
    {
125
        $this->claimManager->removeAll($claim);
126
    }
127
128
    /**
129
     * getValidClaimFromToken method
130
     *
131
     * @param $token
132
     * @return Claim
133
     * @throws MalformedException
134
     * @throws TokenExpiredException
135
     * @throws InvalidTokenException
136
     */
137
    protected function getValidClaimFromToken($token)
138
    {
139
        try {
140
            $payload = JWT::decode($token, $this->key, [
141
                'HS256', 'HS384', 'HS512', 'RS256'
142
            ]);
143
144
        } catch (ExpiredException $e) {
145
            throw new TokenExpiredException($e->getMessage(), $e->getCode(), $e);
146
        } catch (\Exception $e) {
147
            throw new InvalidTokenException;
148
        }
149
150
        $claim = new Claim((array) $payload);
151
152
        if (!$this->claimManager->check($claim)) {
153
            throw new InvalidTokenException;
154
        }
155
156
        return $claim;
157
    }
158
159
}