1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WWON\JwtGuard; |
4
|
|
|
|
5
|
|
|
use Firebase\JWT\JWT; |
6
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
use WWON\JwtGuard\Contract\TokenManager; |
|
|
|
|
9
|
|
|
|
10
|
|
|
class JwtService |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $key; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var TokenManager |
20
|
|
|
*/ |
21
|
|
|
protected $tokenManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* JwtService constructor |
25
|
|
|
* |
26
|
|
|
* @param TokenManager $tokenManager |
27
|
|
|
*/ |
28
|
|
|
public function __construct(TokenManager $tokenManager) |
29
|
|
|
{ |
30
|
|
|
$this->key = Config::get('jwt.secret'); |
31
|
|
|
$this->tokenManager = $tokenManager; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* getTokenForUser method |
36
|
|
|
* |
37
|
|
|
* @param Authenticatable $user |
38
|
|
|
* @return null|string |
39
|
|
|
*/ |
40
|
|
|
public function getTokenForUser(Authenticatable $user) |
41
|
|
|
{ |
42
|
|
|
$claim = new Claim([ |
43
|
|
|
'sub' => $user->getAuthIdentifier() |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
return $this->getTokenForClaim($claim); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* getUserIdFromToken method |
51
|
|
|
* |
52
|
|
|
* @param $token |
53
|
|
|
* @return mixed|null |
54
|
|
|
*/ |
55
|
|
|
public function getUserIdFromToken($token) |
56
|
|
|
{ |
57
|
|
|
$claim = $this->getClaimFromToken($token); |
58
|
|
|
|
59
|
|
|
if (!empty($claim) && $this->tokenManager->check($claim)) { |
60
|
|
|
return $claim->sub; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* refreshToken method |
68
|
|
|
* |
69
|
|
|
* @param $token |
70
|
|
|
* @return null|string |
71
|
|
|
*/ |
72
|
|
|
public function refreshToken($token) |
73
|
|
|
{ |
74
|
|
|
$claim = $this->getClaimFromToken($token); |
75
|
|
|
|
76
|
|
|
if (empty($claim)) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->tokenManager->remove($claim); |
81
|
|
|
|
82
|
|
|
$newClaim = new Claim([ |
83
|
|
|
'sub' => $claim->sub |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
return $this->getTokenForClaim($newClaim); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* getTokenForUser method |
91
|
|
|
* |
92
|
|
|
* @param Claim $claim |
93
|
|
|
* @return null|string |
94
|
|
|
*/ |
95
|
|
|
protected function getTokenForClaim(Claim $claim) |
96
|
|
|
{ |
97
|
|
|
$token = JWT::encode($claim->toArray(), $this->key); |
98
|
|
|
$this->tokenManager->add($claim); |
99
|
|
|
|
100
|
|
|
return $token; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* getClaimFromToken method |
105
|
|
|
* |
106
|
|
|
* @param $token |
107
|
|
|
* @return null|Claim |
108
|
|
|
*/ |
109
|
|
|
protected function getClaimFromToken($token) |
110
|
|
|
{ |
111
|
|
|
try { |
112
|
|
|
$payload = JWT::decode($token, $this->key); |
113
|
|
|
|
114
|
|
|
return new Claim((array) $payload); |
115
|
|
|
|
116
|
|
|
} catch (\Exception $e) {} |
|
|
|
|
117
|
|
|
|
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
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: