1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Emarref\Jwt\Serialization; |
4
|
|
|
|
5
|
|
|
use Emarref\Jwt\Claim; |
6
|
|
|
use Emarref\Jwt\HeaderParameter; |
7
|
|
|
use Emarref\Jwt\Token; |
8
|
|
|
|
9
|
|
|
class CompactTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
13
|
|
|
*/ |
14
|
|
|
private $encoding; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
18
|
|
|
*/ |
19
|
|
|
private $headerParameterFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
23
|
|
|
*/ |
24
|
|
|
private $claimFactory; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Compact |
28
|
|
|
*/ |
29
|
|
|
private $serializer; |
30
|
|
|
|
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->encoding = $this->getMockBuilder('Emarref\Jwt\Encoding\Base64')->getMock(); |
34
|
|
|
|
35
|
|
|
$this->headerParameterFactory = $this->getMockBuilder('Emarref\Jwt\HeaderParameter\Factory')->getMock(); |
36
|
|
|
|
37
|
|
|
$this->claimFactory = $this->getMockBuilder('Emarref\Jwt\Claim\Factory')->getMock(); |
38
|
|
|
|
39
|
|
|
$this->serializer = new Compact($this->encoding, $this->headerParameterFactory, $this->claimFactory); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testDeserialize() |
43
|
|
|
{ |
44
|
|
|
$jwt = 'a.b.c'; |
45
|
|
|
|
46
|
|
|
// Configure encoding |
47
|
|
|
|
48
|
|
|
$this->encoding->expects($this->at(0)) |
49
|
|
|
->method('decode') |
50
|
|
|
->with('a') |
51
|
|
|
->will($this->returnValue('{"a":"1"}')); |
52
|
|
|
|
53
|
|
|
$this->encoding->expects($this->at(1)) |
54
|
|
|
->method('decode') |
55
|
|
|
->with('b') |
56
|
|
|
->will($this->returnValue('{"b":"2"}')); |
57
|
|
|
|
58
|
|
|
$this->encoding->expects($this->at(2)) |
59
|
|
|
->method('decode') |
60
|
|
|
->with('c') |
61
|
|
|
->will($this->returnValue('c')); |
62
|
|
|
|
63
|
|
|
// Configure headers |
64
|
|
|
|
65
|
|
|
$headerParameter = $this->getMockBuilder('Emarref\Jwt\HeaderParameter\Custom') |
66
|
|
|
->getMock(); |
67
|
|
|
|
68
|
|
|
$headerParameter->expects($this->once()) |
69
|
|
|
->method('setValue') |
70
|
|
|
->with('1'); |
71
|
|
|
|
72
|
|
|
$headerParameter->expects($this->once()) |
73
|
|
|
->method('getValue') |
74
|
|
|
->will($this->returnValue('1')); |
75
|
|
|
|
76
|
|
|
$headerParameter->expects($this->exactly(2)) |
77
|
|
|
->method('getName') |
78
|
|
|
->will($this->returnValue('a')); |
79
|
|
|
|
80
|
|
|
$this->headerParameterFactory->expects($this->once()) |
81
|
|
|
->method('get') |
82
|
|
|
->with('a') |
83
|
|
|
->will($this->returnValue($headerParameter)); |
84
|
|
|
|
85
|
|
|
// Configure claims |
86
|
|
|
|
87
|
|
|
$claim = $this->getMockBuilder('Emarref\Jwt\Claim\PrivateClaim') |
88
|
|
|
->getMock(); |
89
|
|
|
|
90
|
|
|
$claim->expects($this->once()) |
91
|
|
|
->method('setValue') |
92
|
|
|
->with('2'); |
93
|
|
|
|
94
|
|
|
$claim->expects($this->once()) |
95
|
|
|
->method('getValue') |
96
|
|
|
->will($this->returnValue('2')); |
97
|
|
|
|
98
|
|
|
$claim->expects($this->exactly(2)) |
99
|
|
|
->method('getName') |
100
|
|
|
->will($this->returnValue('b')); |
101
|
|
|
|
102
|
|
|
$this->claimFactory->expects($this->once()) |
103
|
|
|
->method('get') |
104
|
|
|
->with('b') |
105
|
|
|
->will($this->returnValue($claim)); |
106
|
|
|
|
107
|
|
|
// Assert |
108
|
|
|
|
109
|
|
|
$token = $this->serializer->deserialize($jwt); |
110
|
|
|
|
111
|
|
|
$this->assertSame('{"a":"1"}', $token->getHeader()->jsonSerialize()); |
112
|
|
|
$this->assertSame('{"b":"2"}', $token->getPayload()->jsonSerialize()); |
113
|
|
|
$this->assertSame('c', $token->getSignature()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @expectedException \InvalidArgumentException |
119
|
|
|
* @expectedExceptionMessage Not a valid JWT string passed for deserialization |
120
|
|
|
*/ |
121
|
|
|
public function testDeserializationWithEmptyToken() |
122
|
|
|
{ |
123
|
|
|
$token = ''; |
124
|
|
|
$this->serializer->deserialize($token); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @expectedException \InvalidArgumentException |
129
|
|
|
* @expectedExceptionMessage Not a valid header of JWT string passed for deserialization |
130
|
|
|
*/ |
131
|
|
|
public function testDeserializationTokenWithInvalidHeader() |
132
|
|
|
{ |
133
|
|
|
$token = 'header.payload.signature'; |
134
|
|
|
$this->encoding->expects($this->at(0)) |
135
|
|
|
->method('decode') |
136
|
|
|
->with('header') |
137
|
|
|
->will($this->returnValue('{"invalid"}')); |
138
|
|
|
|
139
|
|
|
$this->serializer->deserialize($token); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @expectedException \InvalidArgumentException |
144
|
|
|
* @expectedExceptionMessage Not a valid payload of JWT string passed for deserialization |
145
|
|
|
*/ |
146
|
|
|
public function testDeserializationTokenWithInvalidPayload() |
147
|
|
|
{ |
148
|
|
|
$token = 'header.payload.signature'; |
149
|
|
|
$this->encoding->expects($this->at(0)) |
150
|
|
|
->method('decode') |
151
|
|
|
->with('header') |
152
|
|
|
->will($this->returnValue('{"header_field":"valid_header"}')); |
153
|
|
|
|
154
|
|
|
$this->encoding->expects($this->at(1)) |
155
|
|
|
->method('decode') |
156
|
|
|
->with('payload') |
157
|
|
|
->will($this->returnValue('{"invalid"}')); |
158
|
|
|
|
159
|
|
|
$this->encoding->expects($this->at(2)) |
160
|
|
|
->method('decode') |
161
|
|
|
->with('signature') |
162
|
|
|
->will($this->returnValue('{"signature_field":"valid_signature"}')); |
163
|
|
|
|
164
|
|
|
$headerParameter = $this->getMockBuilder('Emarref\Jwt\HeaderParameter\Custom') |
165
|
|
|
->getMock(); |
166
|
|
|
|
167
|
|
|
$this->headerParameterFactory->expects($this->once()) |
168
|
|
|
->method('get') |
169
|
|
|
->with('header_field') |
170
|
|
|
->will($this->returnValue($headerParameter)); |
171
|
|
|
|
172
|
|
|
$this->serializer->deserialize($token); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testDeserializationTokenWithoutSignature() |
176
|
|
|
{ |
177
|
|
|
$token = 'header.payload'; |
178
|
|
|
$this->encoding->expects($this->at(0)) |
179
|
|
|
->method('decode') |
180
|
|
|
->with('header') |
181
|
|
|
->will($this->returnValue('{"header_field":"valid_header"}')); |
182
|
|
|
|
183
|
|
|
$this->encoding->expects($this->at(1)) |
184
|
|
|
->method('decode') |
185
|
|
|
->with('payload') |
186
|
|
|
->will($this->returnValue('{}')); |
187
|
|
|
|
188
|
|
|
$this->encoding->expects($this->at(2)) |
189
|
|
|
->method('decode') |
190
|
|
|
->with(null) |
191
|
|
|
->will($this->returnValue(null)); |
192
|
|
|
|
193
|
|
|
$headerParameter = $this->getMockBuilder('Emarref\Jwt\HeaderParameter\Custom') |
194
|
|
|
->getMock(); |
195
|
|
|
|
196
|
|
|
$this->headerParameterFactory->expects($this->once()) |
197
|
|
|
->method('get') |
198
|
|
|
->with('header_field') |
199
|
|
|
->will($this->returnValue($headerParameter)); |
200
|
|
|
|
201
|
|
|
$token = $this->serializer->deserialize($token); |
202
|
|
|
|
203
|
|
|
$this->assertNull($token->getSignature()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testSerialize() |
207
|
|
|
{ |
208
|
|
|
// Configure payload |
209
|
|
|
|
210
|
|
|
$headerParameters = $this->getMockBuilder('Emarref\Jwt\Token\PropertyList')->getMock(); |
211
|
|
|
|
212
|
|
|
$headerParameters->expects($this->once()) |
213
|
|
|
->method('jsonSerialize') |
214
|
|
|
->will($this->returnValue('{"a":"1"}')); |
215
|
|
|
|
216
|
|
|
$header = $this->getMockBuilder('Emarref\Jwt\Token\Header')->getMock(); |
217
|
|
|
|
218
|
|
|
$header->expects($this->once()) |
219
|
|
|
->method('getParameters') |
220
|
|
|
->will($this->returnValue($headerParameters)); |
221
|
|
|
|
222
|
|
|
// Configure payload |
223
|
|
|
|
224
|
|
|
$claims = $this->getMockBuilder('Emarref\Jwt\Token\PropertyList')->getMock(); |
225
|
|
|
|
226
|
|
|
$claims->expects($this->once()) |
227
|
|
|
->method('jsonSerialize') |
228
|
|
|
->will($this->returnValue('{"b":"2"}')); |
229
|
|
|
|
230
|
|
|
$payload = $this->getMockBuilder('Emarref\Jwt\Token\Payload')->getMock(); |
231
|
|
|
|
232
|
|
|
$payload->expects($this->once()) |
233
|
|
|
->method('getClaims') |
234
|
|
|
->will($this->returnValue($claims)); |
235
|
|
|
|
236
|
|
|
// Configure token |
237
|
|
|
|
238
|
|
|
$token = $this->getMockBuilder('Emarref\Jwt\Token')->getMock(); |
239
|
|
|
|
240
|
|
|
$token->expects($this->once()) |
241
|
|
|
->method('getHeader') |
242
|
|
|
->will($this->returnValue($header)); |
243
|
|
|
|
244
|
|
|
$token->expects($this->once()) |
245
|
|
|
->method('getPayload') |
246
|
|
|
->will($this->returnValue($payload)); |
247
|
|
|
|
248
|
|
|
$token->expects($this->once()) |
249
|
|
|
->method('getSignature') |
250
|
|
|
->will($this->returnValue('c')); |
251
|
|
|
|
252
|
|
|
// Configure encoding |
253
|
|
|
|
254
|
|
|
$this->encoding->expects($this->exactly(3)) |
255
|
|
|
->method('encode') |
256
|
|
|
->will($this->returnValueMap([ |
257
|
|
|
['{"a":"1"}', 'a'], |
258
|
|
|
['{"b":"2"}', 'b'], |
259
|
|
|
['c', 'c'], |
260
|
|
|
])); |
261
|
|
|
|
262
|
|
|
$jwt = $this->serializer->serialize($token); |
263
|
|
|
|
264
|
|
|
$this->assertSame('a.b.c', $jwt); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|