|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Tests\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\DependencyInjection\Configuration; |
|
8
|
|
|
use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ConfigurationTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
use ConfigurationTestCaseTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @test |
|
18
|
|
|
*/ |
|
19
|
|
|
public function it_processes_empty_config() |
|
20
|
|
|
{ |
|
21
|
|
|
$config = []; |
|
22
|
|
|
|
|
23
|
|
|
$this->assertProcessedConfigurationEquals([$config], [ |
|
24
|
|
|
'api_key' => [ |
|
25
|
|
|
'enabled' => false, |
|
26
|
|
|
'tokens' => [], |
|
27
|
|
|
'extractors' => [ |
|
28
|
|
|
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Token'], |
|
29
|
|
|
], |
|
30
|
|
|
], |
|
31
|
|
|
'jwt' => [ |
|
32
|
|
|
'enabled' => false, |
|
33
|
|
|
'extractors' => [ |
|
34
|
|
|
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Bearer'], |
|
35
|
|
|
], |
|
36
|
|
|
'builder' => [ |
|
37
|
|
|
'ttl' => 3600, |
|
38
|
|
|
], |
|
39
|
|
|
], |
|
40
|
|
|
'format_exceptions' => true, |
|
41
|
|
|
]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @test |
|
46
|
|
|
*/ |
|
47
|
|
|
public function it_processes_simplified_api_key_config() |
|
48
|
|
|
{ |
|
49
|
|
|
$config = [ |
|
50
|
|
|
'api_key' => ['foo' => 'bar', 'baz' => 'qux'], |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
$this->assertProcessedConfigurationEquals([$config], [ |
|
54
|
|
|
'api_key' => [ |
|
55
|
|
|
'enabled' => true, |
|
56
|
|
|
'tokens' => ['foo' => 'bar', 'baz' => 'qux'], |
|
57
|
|
|
'extractors' => [ |
|
58
|
|
|
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Token'], |
|
59
|
|
|
], |
|
60
|
|
|
], |
|
61
|
|
|
], 'api_key'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @test |
|
66
|
|
|
*/ |
|
67
|
|
|
public function it_processes_api_key_config() |
|
68
|
|
|
{ |
|
69
|
|
|
$config = [ |
|
70
|
|
|
'api_key' => [ |
|
71
|
|
|
'tokens' => ['foo' => 'bar', 'baz' => 'qux'], |
|
72
|
|
|
'extractors' => [ |
|
73
|
|
|
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Token'], |
|
74
|
|
|
['type' => 'query', 'name' => 'api_key'], |
|
75
|
|
|
['type' => 'cookie', 'name' => 'api_key'], |
|
76
|
|
|
], |
|
77
|
|
|
], |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
$this->assertProcessedConfigurationEquals([$config], [ |
|
81
|
|
|
'api_key' => [ |
|
82
|
|
|
'enabled' => true, |
|
83
|
|
|
'tokens' => ['foo' => 'bar', 'baz' => 'qux'], |
|
84
|
|
|
'extractors' => [ |
|
85
|
|
|
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Token'], |
|
86
|
|
|
['type' => 'query', 'name' => 'api_key'], |
|
87
|
|
|
['type' => 'cookie', 'name' => 'api_key'], |
|
88
|
|
|
], |
|
89
|
|
|
], |
|
90
|
|
|
], 'api_key'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @test |
|
95
|
|
|
*/ |
|
96
|
|
|
public function it_processes_basic_jwt_config() |
|
97
|
|
|
{ |
|
98
|
|
|
$config = [ |
|
99
|
|
|
'jwt' => [ |
|
100
|
|
|
'builder' => [ |
|
101
|
|
|
'issuer' => 'damax-api-auth-bundle', |
|
102
|
|
|
'audience' => 'symfony', |
|
103
|
|
|
'ttl' => 600, |
|
104
|
|
|
], |
|
105
|
|
|
'parser' => [ |
|
106
|
|
|
'issuers' => ['symfony', 'zend'], |
|
107
|
|
|
'audience' => 'zend', |
|
108
|
|
|
], |
|
109
|
|
|
'extractors' => [ |
|
110
|
|
|
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Bearer'], |
|
111
|
|
|
['type' => 'query', 'name' => 'token'], |
|
112
|
|
|
['type' => 'cookie', 'name' => 'token'], |
|
113
|
|
|
], |
|
114
|
|
|
'signer' => [ |
|
115
|
|
|
'signing_key' => 'secret', |
|
116
|
|
|
], |
|
117
|
|
|
], |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
|
|
$this->assertProcessedConfigurationEquals([$config], [ |
|
121
|
|
|
'jwt' => [ |
|
122
|
|
|
'enabled' => true, |
|
123
|
|
|
'builder' => [ |
|
124
|
|
|
'issuer' => 'damax-api-auth-bundle', |
|
125
|
|
|
'audience' => 'symfony', |
|
126
|
|
|
'ttl' => 600, |
|
127
|
|
|
], |
|
128
|
|
|
'parser' => [ |
|
129
|
|
|
'issuers' => ['symfony', 'zend'], |
|
130
|
|
|
'audience' => 'zend', |
|
131
|
|
|
], |
|
132
|
|
|
'extractors' => [ |
|
133
|
|
|
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Bearer'], |
|
134
|
|
|
['type' => 'query', 'name' => 'token'], |
|
135
|
|
|
['type' => 'cookie', 'name' => 'token'], |
|
136
|
|
|
], |
|
137
|
|
|
'signer' => [ |
|
138
|
|
|
'type' => 'symmetric', |
|
139
|
|
|
'algorithm' => 'HS256', |
|
140
|
|
|
'signing_key' => 'secret', |
|
141
|
|
|
'passphrase' => '', |
|
142
|
|
|
], |
|
143
|
|
|
], |
|
144
|
|
|
], 'jwt'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @test |
|
149
|
|
|
*/ |
|
150
|
|
|
public function it_requires_verification_key_for_asymmetric_signer() |
|
151
|
|
|
{ |
|
152
|
|
|
$config = [ |
|
153
|
|
|
'jwt' => [ |
|
154
|
|
|
'signer' => [ |
|
155
|
|
|
'type' => 'asymmetric', |
|
156
|
|
|
'signing_key' => 'secret', |
|
157
|
|
|
], |
|
158
|
|
|
], |
|
159
|
|
|
]; |
|
160
|
|
|
|
|
161
|
|
|
$this->assertPartialConfigurationIsInvalid([$config], 'jwt', 'Verification key must be specified for "asymmetric" signer.'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @test |
|
166
|
|
|
*/ |
|
167
|
|
|
public function it_requires_hmac_algorithm_for_symmetric_signer() |
|
168
|
|
|
{ |
|
169
|
|
|
$config = [ |
|
170
|
|
|
'jwt' => [ |
|
171
|
|
|
'signer' => [ |
|
172
|
|
|
'type' => 'symmetric', |
|
173
|
|
|
'algorithm' => 'RS256', |
|
174
|
|
|
'signing_key' => 'secret', |
|
175
|
|
|
], |
|
176
|
|
|
], |
|
177
|
|
|
]; |
|
178
|
|
|
|
|
179
|
|
|
$this->assertPartialConfigurationIsInvalid([$config], 'jwt', 'HMAC algorithm must be specified for "symmetric" signer.'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @test |
|
184
|
|
|
*/ |
|
185
|
|
|
public function it_requires_rsa_algorithm_for_asymmetric_signer() |
|
186
|
|
|
{ |
|
187
|
|
|
$config = [ |
|
188
|
|
|
'jwt' => [ |
|
189
|
|
|
'signer' => [ |
|
190
|
|
|
'type' => 'asymmetric', |
|
191
|
|
|
'algorithm' => 'HS256', |
|
192
|
|
|
'signing_key' => 'signing_secret', |
|
193
|
|
|
'verification_key' => 'verification_secret', |
|
194
|
|
|
], |
|
195
|
|
|
], |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
$this->assertPartialConfigurationIsInvalid([$config], 'jwt', 'RSA or ECDSA algorithm must be specified for "asymmetric" signer.'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @test |
|
203
|
|
|
*/ |
|
204
|
|
|
public function it_requires_readable_signing_and_verification_key() |
|
205
|
|
|
{ |
|
206
|
|
|
$config = [ |
|
207
|
|
|
'jwt' => [ |
|
208
|
|
|
'signer' => [ |
|
209
|
|
|
'type' => 'asymmetric', |
|
210
|
|
|
'algorithm' => 'RS256', |
|
211
|
|
|
'signing_key' => 'signing_secret', |
|
212
|
|
|
'verification_key' => 'verification_secret', |
|
213
|
|
|
], |
|
214
|
|
|
], |
|
215
|
|
|
]; |
|
216
|
|
|
|
|
217
|
|
|
$this->assertPartialConfigurationIsInvalid([$config], 'jwt', 'Signing and/or verification key is not readable.'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @test |
|
222
|
|
|
*/ |
|
223
|
|
|
public function it_processes_jwt_config() |
|
224
|
|
|
{ |
|
225
|
|
|
$filename = tempnam(sys_get_temp_dir(), 'key_'); |
|
226
|
|
|
|
|
227
|
|
|
$config = [ |
|
228
|
|
|
'jwt' => [ |
|
229
|
|
|
'signer' => [ |
|
230
|
|
|
'type' => 'asymmetric', |
|
231
|
|
|
'algorithm' => 'RS256', |
|
232
|
|
|
'signing_key' => $filename, |
|
233
|
|
|
'verification_key' => $filename, |
|
234
|
|
|
], |
|
235
|
|
|
], |
|
236
|
|
|
]; |
|
237
|
|
|
|
|
238
|
|
|
$this->assertProcessedConfigurationEquals([$config], [ |
|
239
|
|
|
'jwt' => [ |
|
240
|
|
|
'enabled' => true, |
|
241
|
|
|
'builder' => [ |
|
242
|
|
|
'ttl' => 3600, |
|
243
|
|
|
], |
|
244
|
|
|
'extractors' => [ |
|
245
|
|
|
['type' => 'header', 'name' => 'Authorization', 'prefix' => 'Bearer'], |
|
246
|
|
|
], |
|
247
|
|
|
'signer' => [ |
|
248
|
|
|
'type' => 'asymmetric', |
|
249
|
|
|
'algorithm' => 'RS256', |
|
250
|
|
|
'signing_key' => 'file://' . $filename, |
|
251
|
|
|
'verification_key' => 'file://' . $filename, |
|
252
|
|
|
'passphrase' => '', |
|
253
|
|
|
], |
|
254
|
|
|
], |
|
255
|
|
|
], 'jwt'); |
|
256
|
|
|
|
|
257
|
|
|
unlink($filename); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
protected function getConfiguration(): ConfigurationInterface |
|
261
|
|
|
{ |
|
262
|
|
|
return new Configuration(); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|