1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
4
|
|
|
use Illuminate\Support\Facades\Config; |
5
|
|
|
use WWON\JwtGuard\Claim; |
6
|
|
|
use WWON\JwtGuard\Contract\ClaimManager; |
7
|
|
|
use WWON\JwtGuard\JwtService; |
8
|
|
|
|
9
|
|
|
class JwtServiceTest extends PHPUnit_Framework_TestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var Mockery\MockInterface |
14
|
|
|
*/ |
15
|
|
|
private $claimManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var JwtService |
19
|
|
|
*/ |
20
|
|
|
private $jwtService; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* setUp method |
24
|
|
|
*/ |
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
|
29
|
|
|
Config::shouldReceive('get') |
30
|
|
|
->once() |
31
|
|
|
->with('jwt.secret') |
32
|
|
|
->andReturn('abcdefg'); |
33
|
|
|
|
34
|
|
|
Config::shouldReceive('get') |
35
|
|
|
->once() |
36
|
|
|
->with('jwt.leeway') |
37
|
|
|
->andReturn(0); |
38
|
|
|
|
39
|
|
|
$this->claimManager = Mockery::mock(ClaimManager::class); |
40
|
|
|
$this->jwtService = new JwtService($this->claimManager); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* tearDown method |
45
|
|
|
*/ |
46
|
|
|
public function tearDown() |
47
|
|
|
{ |
48
|
|
|
unset($this->jwtService); |
49
|
|
|
|
50
|
|
|
parent::tearDown(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* testGetTokenForClaim method |
55
|
|
|
*/ |
56
|
|
|
public function testGetTokenForClaim() |
57
|
|
|
{ |
58
|
|
|
$now = Carbon::now()->timestamp; |
59
|
|
|
|
60
|
|
|
$token = $this->getToken(); |
61
|
|
|
|
62
|
|
|
$items = explode('.', $token); |
63
|
|
|
$claimBody = json_decode(base64_decode($items[1])); |
64
|
|
|
|
65
|
|
|
$this->assertEquals('http://www.test.com', $claimBody->iss); |
66
|
|
|
$this->assertEquals($now, $claimBody->iat); |
67
|
|
|
$this->assertEquals($now + 6000, $claimBody->exp); |
68
|
|
|
$this->assertEquals($now + 6000, $claimBody->nat); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* testGetClaimFromToken method |
73
|
|
|
*/ |
74
|
|
|
public function testGetClaimFromToken() |
75
|
|
|
{ |
76
|
|
|
$token = $this->getToken(); |
77
|
|
|
|
78
|
|
|
$this->claimManager->shouldReceive('check') |
79
|
|
|
->once() |
80
|
|
|
->with(Mockery::on(function($claim) { |
81
|
|
|
return $claim->sub == 5 |
82
|
|
|
&& $claim->iss == 'http://www.test.com'; |
83
|
|
|
})) |
84
|
|
|
->andReturn(true); |
85
|
|
|
|
86
|
|
|
$claim = $this->jwtService->getClaimFromToken($token); |
87
|
|
|
|
88
|
|
|
$this->assertEquals(5, $claim->sub); |
89
|
|
|
$this->assertEquals('User', $claim->aud); |
90
|
|
|
$this->assertEquals('http://www.test.com', $claim->iss); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* testGetUserIdFromTokenWithInvalidClaim method |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function testGetUserIdFromTokenWithInvalidClaim() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$token = $this->getToken(); |
99
|
|
|
|
100
|
|
|
$this->claimManager->shouldReceive('check') |
101
|
|
|
->once() |
102
|
|
|
->with(Mockery::on(function($claim) { |
103
|
|
|
return $claim->sub == 5 |
104
|
|
|
&& $claim->iss == 'http://www.test.com'; |
105
|
|
|
})) |
106
|
|
|
->andReturn(false); |
107
|
|
|
|
108
|
|
|
$this->setExpectedException(\WWON\JwtGuard\Exceptions\InvalidTokenException::class); |
109
|
|
|
|
110
|
|
|
$claim = $this->jwtService->getClaimFromToken($token); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* testRefreshToken method |
115
|
|
|
*/ |
116
|
|
|
public function testRefreshToken() |
117
|
|
|
{ |
118
|
|
|
$now = Carbon::now()->timestamp; |
119
|
|
|
|
120
|
|
|
$token = $this->getToken(true); |
121
|
|
|
|
122
|
|
|
$this->claimManager->shouldReceive('check') |
123
|
|
|
->once() |
124
|
|
|
->with(Mockery::on(function($claim) { |
125
|
|
|
return $claim->sub == 5 |
126
|
|
|
&& $claim->iss == 'http://www.test.com'; |
127
|
|
|
})) |
128
|
|
|
->andReturn(true); |
129
|
|
|
|
130
|
|
|
$this->claimManager->shouldReceive('remove') |
131
|
|
|
->once() |
132
|
|
|
->with(Mockery::on(function($claim) { |
133
|
|
|
return $claim->sub == 5 |
134
|
|
|
&& $claim->iss == 'http://www.test.com'; |
135
|
|
|
})); |
136
|
|
|
|
137
|
|
|
$newToken = $this->jwtService->refreshToken($token); |
138
|
|
|
|
139
|
|
|
$items = explode('.', $newToken); |
140
|
|
|
$claimBody = json_decode(base64_decode($items[1])); |
141
|
|
|
|
142
|
|
|
$this->assertEquals('http://www.test.com', $claimBody->iss); |
143
|
|
|
$this->assertEquals($now, $claimBody->iat); |
144
|
|
|
$this->assertEquals($now + 60000, $claimBody->exp); |
145
|
|
|
$this->assertEquals($now + 6000, $claimBody->nat); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* testRefreshUnRefreshableToken method |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function testRefreshUnRefreshableToken() |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$now = Carbon::now()->timestamp; |
|
|
|
|
154
|
|
|
|
155
|
|
|
$token = $this->getToken(); |
156
|
|
|
|
157
|
|
|
$this->claimManager->shouldReceive('check') |
158
|
|
|
->once() |
159
|
|
|
->with(Mockery::on(function($claim) { |
160
|
|
|
return $claim->sub == 5 |
161
|
|
|
&& $claim->iss == 'http://www.test.com'; |
162
|
|
|
})) |
163
|
|
|
->andReturn(true); |
164
|
|
|
|
165
|
|
|
$this->setExpectedException(\WWON\JwtGuard\Exceptions\UnRefreshableException::class); |
166
|
|
|
|
167
|
|
|
$newToken = $this->jwtService->refreshToken($token); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* testInvalidateToken method |
172
|
|
|
*/ |
173
|
|
|
public function testInvalidateToken() |
174
|
|
|
{ |
175
|
|
|
$now = Carbon::now()->timestamp; |
|
|
|
|
176
|
|
|
|
177
|
|
|
$token = $this->getToken(); |
178
|
|
|
|
179
|
|
|
$this->claimManager->shouldReceive('check') |
180
|
|
|
->once() |
181
|
|
|
->with(Mockery::on(function($claim) { |
182
|
|
|
return $claim->sub == 5 |
183
|
|
|
&& $claim->iss == 'http://www.test.com'; |
184
|
|
|
})) |
185
|
|
|
->andReturn(true); |
186
|
|
|
|
187
|
|
|
$this->claimManager->shouldReceive('remove') |
188
|
|
|
->once() |
189
|
|
|
->with(Mockery::on(function($claim) { |
190
|
|
|
return $claim->sub == 5 |
191
|
|
|
&& $claim->iss == 'http://www.test.com'; |
192
|
|
|
})); |
193
|
|
|
|
194
|
|
|
$this->jwtService->invalidateToken($token); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* testWipeUserTokens method |
199
|
|
|
*/ |
200
|
|
|
public function testWipeUserTokens() |
201
|
|
|
{ |
202
|
|
|
$claim = $this->getClaim(); |
203
|
|
|
|
204
|
|
|
$this->claimManager->shouldReceive('removeAll') |
205
|
|
|
->once() |
206
|
|
|
->with(Mockery::on(function($claim) { |
207
|
|
|
return $claim->sub == 5 |
208
|
|
|
&& $claim->iss == 'http://www.test.com'; |
209
|
|
|
})); |
210
|
|
|
|
211
|
|
|
$this->jwtService->wipeUserTokens($claim); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* getToken method |
216
|
|
|
* |
217
|
|
|
* @param bool $refreshable |
218
|
|
|
* @param int $ttl |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
protected function getToken($refreshable = false, $ttl = 100) |
222
|
|
|
{ |
223
|
|
|
$claim = $this->getClaim($refreshable, $ttl); |
224
|
|
|
|
225
|
|
|
$this->claimManager->shouldReceive('add') |
226
|
|
|
->once() |
227
|
|
|
->with(Mockery::on(function($claim) { |
228
|
|
|
return $claim->sub == 5 |
229
|
|
|
&& $claim->iss == 'http://www.test.com'; |
230
|
|
|
})); |
231
|
|
|
|
232
|
|
|
$token = $this->jwtService->getTokenForClaim($claim); |
233
|
|
|
|
234
|
|
|
return $token; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* getClaim method |
239
|
|
|
* |
240
|
|
|
* @param bool $refreshable |
241
|
|
|
* @param int $ttl |
242
|
|
|
* @return Claim |
243
|
|
|
*/ |
244
|
|
|
protected function getClaim($refreshable = false, $ttl = 100) |
245
|
|
|
{ |
246
|
|
|
Config::shouldReceive('get') |
247
|
|
|
->once() |
248
|
|
|
->with('app.url') |
249
|
|
|
->andReturn('http://www.test.com'); |
250
|
|
|
|
251
|
|
|
Config::shouldReceive('get') |
252
|
|
|
->once() |
253
|
|
|
->with('jwt.ttl') |
254
|
|
|
->andReturn($ttl); |
255
|
|
|
|
256
|
|
|
Config::shouldReceive('get') |
257
|
|
|
->once() |
258
|
|
|
->with('jwt.ttl') |
259
|
|
|
->andReturn($ttl); |
260
|
|
|
|
261
|
|
|
Config::shouldReceive('get') |
262
|
|
|
->once() |
263
|
|
|
->with('jwt.leeway') |
264
|
|
|
->andReturn(0); |
265
|
|
|
|
266
|
|
|
Config::shouldReceive('get') |
267
|
|
|
->once() |
268
|
|
|
->with('jwt.algo') |
269
|
|
|
->andReturn('HS256'); |
270
|
|
|
|
271
|
|
|
$claim = new Claim([ |
272
|
|
|
'sub' => 5, |
273
|
|
|
'aud' => 'User', |
274
|
|
|
'refresh' => $refreshable |
275
|
|
|
]); |
276
|
|
|
|
277
|
|
|
return $claim; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.