1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
4
|
|
|
use Illuminate\Support\Facades\Config; |
5
|
|
|
use WWON\JwtGuard\Contract\TokenManager; |
6
|
|
|
use WWON\JwtGuard\JwtService; |
7
|
|
|
|
8
|
|
|
class JwtServiceTest extends PHPUnit_Framework_TestCase |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var Mockery\MockInterface |
13
|
|
|
*/ |
14
|
|
|
private $tokenManager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var JwtService |
18
|
|
|
*/ |
19
|
|
|
private $jwtService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* setUp method |
23
|
|
|
*/ |
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
Config::shouldReceive('get') |
29
|
|
|
->once() |
30
|
|
|
->with('jwt.secret') |
31
|
|
|
->andReturn('abcdefg'); |
32
|
|
|
|
33
|
|
|
Config::shouldReceive('get') |
34
|
|
|
->once() |
35
|
|
|
->with('jwt.leeway') |
36
|
|
|
->andReturn(0); |
37
|
|
|
|
38
|
|
|
$this->tokenManager = Mockery::mock(TokenManager::class); |
39
|
|
|
$this->jwtService = new JwtService($this->tokenManager); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* tearDown method |
44
|
|
|
*/ |
45
|
|
|
public function tearDown() |
46
|
|
|
{ |
47
|
|
|
unset($this->jwtService); |
48
|
|
|
|
49
|
|
|
parent::tearDown(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* testGetTokenForUser method |
54
|
|
|
*/ |
55
|
|
|
public function testGetTokenForUser() |
56
|
|
|
{ |
57
|
|
|
$now = Carbon::now()->timestamp; |
58
|
|
|
|
59
|
|
|
$token = $this->getToken(); |
60
|
|
|
|
61
|
|
|
$items = explode('.', $token); |
62
|
|
|
$claimBody = json_decode(base64_decode($items[1])); |
63
|
|
|
|
64
|
|
|
$this->assertEquals('http://www.test.com', $claimBody->iss); |
65
|
|
|
$this->assertEquals($now, $claimBody->iat); |
66
|
|
|
$this->assertEquals($now + 6000, $claimBody->exp); |
67
|
|
|
$this->assertEquals($now + 6000, $claimBody->nat); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* testGetUserIdFromToken method |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function testGetUserIdFromToken() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$token = $this->getToken(); |
76
|
|
|
|
77
|
|
|
$this->tokenManager->shouldReceive('check') |
78
|
|
|
->once() |
79
|
|
|
->with(Mockery::on(function($claim) { |
80
|
|
|
return $claim->sub == 5 |
81
|
|
|
&& $claim->iss == 'http://www.test.com'; |
82
|
|
|
})) |
83
|
|
|
->andReturn(true); |
84
|
|
|
|
85
|
|
|
$userId = $this->jwtService->getUserIdFromToken($token); |
86
|
|
|
|
87
|
|
|
$this->assertEquals(5, $userId); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* testGetUserIdFromTokenWithInvalidClaim method |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function testGetUserIdFromTokenWithInvalidClaim() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$token = $this->getToken(); |
96
|
|
|
|
97
|
|
|
$this->tokenManager->shouldReceive('check') |
98
|
|
|
->once() |
99
|
|
|
->with(Mockery::on(function($claim) { |
100
|
|
|
return $claim->sub == 5 |
101
|
|
|
&& $claim->iss == 'http://www.test.com'; |
102
|
|
|
})) |
103
|
|
|
->andReturn(false); |
104
|
|
|
|
105
|
|
|
$this->setExpectedException(\WWON\JwtGuard\Exceptions\InvalidTokenException::class); |
106
|
|
|
|
107
|
|
|
$userId = $this->jwtService->getUserIdFromToken($token); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* testRefreshToken method |
112
|
|
|
*/ |
113
|
|
|
public function testRefreshToken() |
114
|
|
|
{ |
115
|
|
|
$now = Carbon::now()->timestamp; |
116
|
|
|
|
117
|
|
|
$token = $this->getToken(true); |
118
|
|
|
|
119
|
|
|
$this->tokenManager->shouldReceive('check') |
120
|
|
|
->once() |
121
|
|
|
->with(Mockery::on(function($claim) { |
122
|
|
|
return $claim->sub == 5 |
123
|
|
|
&& $claim->iss == 'http://www.test.com'; |
124
|
|
|
})) |
125
|
|
|
->andReturn(true); |
126
|
|
|
|
127
|
|
|
$this->tokenManager->shouldReceive('remove') |
128
|
|
|
->once() |
129
|
|
|
->with(Mockery::on(function($claim) { |
130
|
|
|
return $claim->sub == 5 |
131
|
|
|
&& $claim->iss == 'http://www.test.com'; |
132
|
|
|
})); |
133
|
|
|
|
134
|
|
|
$newToken = $this->jwtService->refreshToken($token); |
135
|
|
|
|
136
|
|
|
$items = explode('.', $newToken); |
137
|
|
|
$claimBody = json_decode(base64_decode($items[1])); |
138
|
|
|
|
139
|
|
|
$this->assertEquals('http://www.test.com', $claimBody->iss); |
140
|
|
|
$this->assertEquals($now, $claimBody->iat); |
141
|
|
|
$this->assertEquals($now + 6000, $claimBody->exp); |
142
|
|
|
$this->assertEquals($now + 6000, $claimBody->nat); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* testRefreshUnRefreshableToken method |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
public function testRefreshUnRefreshableToken() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$now = Carbon::now()->timestamp; |
|
|
|
|
151
|
|
|
|
152
|
|
|
$token = $this->getToken(); |
153
|
|
|
|
154
|
|
|
$this->tokenManager->shouldReceive('check') |
155
|
|
|
->once() |
156
|
|
|
->with(Mockery::on(function($claim) { |
157
|
|
|
return $claim->sub == 5 |
158
|
|
|
&& $claim->iss == 'http://www.test.com'; |
159
|
|
|
})) |
160
|
|
|
->andReturn(true); |
161
|
|
|
|
162
|
|
|
$this->setExpectedException(\WWON\JwtGuard\Exceptions\UnRefreshableException::class); |
163
|
|
|
|
164
|
|
|
$newToken = $this->jwtService->refreshToken($token); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* getToken method |
169
|
|
|
* |
170
|
|
|
* @param bool $refreshable |
171
|
|
|
* @param int $ttl |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
protected function getToken($refreshable = false, $ttl = 100) |
175
|
|
|
{ |
176
|
|
|
Config::shouldReceive('get') |
177
|
|
|
->once() |
178
|
|
|
->with('app.url') |
179
|
|
|
->andReturn('http://www.test.com'); |
180
|
|
|
|
181
|
|
|
Config::shouldReceive('get') |
182
|
|
|
->once() |
183
|
|
|
->with('jwt.ttl') |
184
|
|
|
->andReturn($ttl); |
185
|
|
|
|
186
|
|
|
Config::shouldReceive('get') |
187
|
|
|
->once() |
188
|
|
|
->with('jwt.ttl') |
189
|
|
|
->andReturn($ttl); |
190
|
|
|
|
191
|
|
|
Config::shouldReceive('get') |
192
|
|
|
->once() |
193
|
|
|
->with('jwt.leeway') |
194
|
|
|
->andReturn(0); |
195
|
|
|
|
196
|
|
|
Config::shouldReceive('get') |
197
|
|
|
->once() |
198
|
|
|
->with('jwt.algo') |
199
|
|
|
->andReturn('HS256'); |
200
|
|
|
|
201
|
|
|
$this->tokenManager->shouldReceive('add') |
202
|
|
|
->once() |
203
|
|
|
->with(Mockery::on(function($claim) { |
204
|
|
|
return $claim->sub == 5 |
205
|
|
|
&& $claim->iss == 'http://www.test.com'; |
206
|
|
|
})); |
207
|
|
|
|
208
|
|
|
$token = $this->jwtService->getTokenForUser(new User(), $refreshable); |
209
|
|
|
|
210
|
|
|
return $token; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
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.