These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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; |
||
0 ignored issues
–
show
|
|||
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 ClaimManager |
||
25 | */ |
||
26 | protected $claimManager; |
||
27 | |||
28 | /** |
||
29 | * JwtService constructor |
||
30 | * |
||
31 | * @param ClaimManager $claimManager |
||
32 | */ |
||
33 | public function __construct(ClaimManager $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 mixed |
||
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 | } |
||
147 | |||
148 | $claim = new Claim((array) $payload); |
||
149 | |||
150 | if (!$this->claimManager->check($claim)) { |
||
151 | throw new InvalidTokenException; |
||
152 | } |
||
153 | |||
154 | return $claim; |
||
155 | } |
||
156 | |||
157 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: