1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UniSharp\JWT\Auth\Guards; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Tymon\JWTAuth\JWT; |
7
|
|
|
use Tymon\JWTAuth\JWTGuard; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Tymon\JWTAuth\Facades\JWTAuth; |
10
|
|
|
use Illuminate\Support\Facades\Cache; |
11
|
|
|
use Illuminate\Support\Facades\Config; |
12
|
|
|
use Illuminate\Contracts\Auth\UserProvider; |
13
|
|
|
|
14
|
|
|
class JWTAuthGuard extends JWTGuard |
15
|
|
|
{ |
16
|
|
|
protected $cachedToken; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create a new authentication guard. |
20
|
|
|
* |
21
|
|
|
* @param \Tymon\JWTAuth\JWT $jwt |
22
|
|
|
* @param \Illuminate\Contracts\Auth\UserProvider $provider |
23
|
|
|
* @param \Illuminate\Http\Request $request |
24
|
|
|
*/ |
25
|
10 |
|
public function __construct(JWT $jwt, UserProvider $provider, Request $request) |
26
|
|
|
{ |
27
|
10 |
|
parent::__construct($jwt, $provider, $request); |
28
|
10 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the currently authenticated user. |
32
|
|
|
* |
33
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null |
34
|
|
|
*/ |
35
|
2 |
|
public function user() |
36
|
|
|
{ |
37
|
2 |
|
if (!is_null($this->user)) { |
38
|
|
|
return $this->user; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
$token = $this->jwt->setRequest($this->request)->getToken(); |
42
|
2 |
|
if (!$token) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// get cached freshed token if exists for concurrency requests |
47
|
2 |
|
if ($this->getCachedToken()) { |
48
|
|
|
$this->jwt = $this->jwt->setToken($this->getCachedToken()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// token validation |
52
|
2 |
|
if ($this->getToken() && |
53
|
2 |
|
($payload = $this->jwt->check(true)) && |
54
|
2 |
|
$this->validateSubject() |
55
|
|
|
) { |
56
|
2 |
|
return $this->user = $this->provider->retrieveById($payload['sub']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->user; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Logout the user, thus invalidating the token. |
64
|
|
|
* |
65
|
|
|
* @param bool $forceForever |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function logout($forceForever = false) |
70
|
|
|
{ |
71
|
|
|
if ($this->getToken()) { |
72
|
|
|
$this->requireToken()->invalidate($forceForever); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->user = null; |
76
|
|
|
$this->jwt->unsetToken(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return cached token. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
6 |
|
public function getCachedToken() |
85
|
|
|
{ |
86
|
6 |
|
if ($this->cachedToken) { |
87
|
2 |
|
return $this->cachedToken; |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
$key = md5($this->jwt->parser()->parseToken()); |
91
|
6 |
|
$this->cachedToken = Cache::get($key); |
92
|
|
|
|
93
|
6 |
|
return $this->cachedToken; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set cached token. |
98
|
|
|
* |
99
|
|
|
* @return this |
100
|
|
|
*/ |
101
|
4 |
|
public function setCachedToken($key, $refreshToken, $expiresAt = null) |
102
|
|
|
{ |
103
|
4 |
|
if (is_null($expiresAt)) { |
104
|
2 |
|
$expiresAt = ((int) Config::get('laravel_jwt.cache_ttl') / 60); |
105
|
|
|
} |
106
|
4 |
|
Cache::put($key, $refreshToken, $expiresAt); |
107
|
4 |
|
$this->cachedToken = $refreshToken; |
108
|
|
|
|
109
|
4 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Refresh current expired token. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
2 |
|
public function refresh($forceForever = false, $resetClaims = false) |
118
|
|
|
{ |
119
|
2 |
|
if ($cache = $this->getCachedToken()) { |
120
|
|
|
return $cache; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// refresh token |
124
|
2 |
|
$token = $this->jwt->parser()->parseToken(); |
125
|
2 |
|
$key = md5($token); |
126
|
2 |
|
$refreshToken = JWTAuth::refresh($token, $forceForever, $resetClaims); |
127
|
|
|
|
128
|
|
|
// cache newly refreshed token |
129
|
2 |
|
$this->setCachedToken($key, $refreshToken); |
130
|
|
|
|
131
|
2 |
|
return $this->cachedToken; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Ensure the JWTSubject matches what is in the token. |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
2 |
|
protected function validateSubject() |
140
|
|
|
{ |
141
|
|
|
// If the provider doesn't have the necessary method |
142
|
|
|
// to get the underlying model name then allow. |
143
|
2 |
|
if (! method_exists($this->provider, 'getModel')) { |
144
|
2 |
|
return true; |
145
|
|
|
} |
146
|
|
|
return $this->jwt->checkSubjectModel($this->provider->getModel()); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the token. |
151
|
|
|
* |
152
|
|
|
* @return false|Token |
153
|
|
|
*/ |
154
|
4 |
|
public function getToken() |
155
|
|
|
{ |
156
|
4 |
|
return $this->jwt->setRequest($this->request)->getToken(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: