|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Framgia\Jwt; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Lcobucci\JWT\Token; |
|
7
|
|
|
use Framgia\Jwt\Contracts\Storage; |
|
8
|
|
|
|
|
9
|
|
|
class Blacklist |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Framgia\Jwt\Contracts\Storage |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $storage; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The grace period when a token is blacklisted. In seconds. |
|
18
|
|
|
* |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $gracePeriod = 0; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Number of minutes from issue date in which a JWT can be refreshed. |
|
25
|
|
|
* |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $refreshTTL = 20160; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The unique key held within the blacklist. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $key = 'jti'; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param \Framgia\Jwt\Contracts\Storage $storage |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(Storage $storage) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->storage = $storage; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add the token (jti claim) to the blacklist. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \Lcobucci\JWT\Token $token |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public function add(Token $token) |
|
55
|
|
|
{ |
|
56
|
|
|
// if there is no exp claim then add the jwt to |
|
57
|
|
|
// the blacklist indefinitely |
|
58
|
|
|
if (! $token->hasClaim('exp')) { |
|
59
|
|
|
return $this->addForever($token); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->storage->add( |
|
63
|
|
|
$this->getKey($token), |
|
64
|
|
|
['valid_until' => $this->getGraceTimestamp()], |
|
65
|
|
|
$this->getMinutesUntilExpired($token) |
|
66
|
|
|
); |
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the number of minutes until the token expiry. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \Lcobucci\JWT\Token $token |
|
74
|
|
|
* |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getMinutesUntilExpired(Token $token) |
|
78
|
|
|
{ |
|
79
|
|
|
$exp = Carbon::createFromTimestamp($token->getClaim('exp')); |
|
80
|
|
|
$iat = Carbon::createFromTimestamp($token->hasClaim('iat') ? $token->getClaim('iat') : 0); |
|
81
|
|
|
// get the latter of the two expiration dates and find |
|
82
|
|
|
// the number of minutes until the expiration date, |
|
83
|
|
|
// plus 1 minute to avoid overlap |
|
84
|
|
|
return $exp->max($iat->addMinutes($this->refreshTTL))->addMinute()->diffInMinutes(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Add the token (jti claim) to the blacklist indefinitely. |
|
89
|
|
|
* |
|
90
|
|
|
* @param \Lcobucci\JWT\Token $token |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function addForever(Token $token) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->storage->forever($this->getKey($token), 'forever'); |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Determine whether the token has been blacklisted. |
|
102
|
|
|
* |
|
103
|
|
|
* @param \Lcobucci\JWT\Token $token |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
public function has(Token $token) |
|
108
|
|
|
{ |
|
109
|
|
|
$val = $this->storage->get($this->getKey($token)); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
// exit early if the token was blacklisted forever |
|
112
|
|
|
if ($val === 'forever') { |
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// check whether the expiry + grace has past |
|
117
|
|
|
return $val !== null && ! Carbon::createFromTimestamp($val['valid_until'])->isFuture(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Remove the token (jti claim) from the blacklist. |
|
122
|
|
|
* |
|
123
|
|
|
* @param \Lcobucci\JWT\Token $token |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
public function remove(Token $token) |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->storage->destroy($this->getKey($token)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Remove all tokens from the blacklist. |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function clear() |
|
138
|
|
|
{ |
|
139
|
|
|
$this->storage->flush(); |
|
140
|
|
|
return true; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get the timestamp when the blacklist comes into effect |
|
145
|
|
|
* This defaults to immediate (0 seconds). |
|
146
|
|
|
* |
|
147
|
|
|
* @return int |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function getGraceTimestamp() |
|
150
|
|
|
{ |
|
151
|
|
|
return Carbon::now()->addSeconds($this->gracePeriod)->getTimestamp(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Set the grace period. |
|
156
|
|
|
* |
|
157
|
|
|
* @param int $gracePeriod |
|
158
|
|
|
* |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setGracePeriod($gracePeriod) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->gracePeriod = (int) $gracePeriod; |
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get the unique key held within the blacklist. |
|
169
|
|
|
* |
|
170
|
|
|
* @param \Lcobucci\JWT\Token $token |
|
171
|
|
|
* |
|
172
|
|
|
* @return mixed |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getKey(Token $token) |
|
175
|
|
|
{ |
|
176
|
|
|
return $token->getClaim($this->key); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Set the unique key held within the blacklist. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $key |
|
183
|
|
|
* |
|
184
|
|
|
* @return $this |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setKey($key) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->key = value($key); |
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Set the refresh time limit. |
|
194
|
|
|
* |
|
195
|
|
|
* @param int $ttl |
|
196
|
|
|
* |
|
197
|
|
|
* @return $this |
|
198
|
|
|
*/ |
|
199
|
|
|
public function setRefreshTTL($ttl) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->refreshTTL = (int) $ttl; |
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.