1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License (MIT) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace SpomkyLabs\JoseBundle\Helper; |
13
|
|
|
|
14
|
|
|
use Assert\Assertion; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This helper will help you to create services configuration. |
19
|
|
|
*/ |
20
|
|
|
final class ConfigurationHelper |
21
|
|
|
{ |
22
|
|
|
const BUNDLE_ALIAS = 'jose'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
26
|
|
|
* @param string $name |
27
|
|
|
* @param string[] $header_checkers |
28
|
|
|
* @param string[] $claim_checkers |
29
|
|
|
* @param bool $is_public |
30
|
|
|
*/ |
31
|
|
|
public static function addChecker(ContainerBuilder $container, $name, array $header_checkers, array $claim_checkers, $is_public = true) |
32
|
|
|
{ |
33
|
|
|
$config = self::getCheckerConfiguration($name, $header_checkers, $claim_checkers, $is_public); |
34
|
|
|
self::updateJoseConfiguration($container, $config, 'checkers'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
39
|
|
|
* @param string $name |
40
|
|
|
* @param string[] $signature_algorithms |
41
|
|
|
* @param bool $create_verifier |
42
|
|
|
* @param bool $is_public |
43
|
|
|
*/ |
44
|
|
|
public static function addSigner(ContainerBuilder $container, $name, array $signature_algorithms, $create_verifier = false, $is_public = true) |
45
|
|
|
{ |
46
|
|
|
$config = self::getSignerConfiguration($name, $signature_algorithms, $create_verifier, $is_public); |
47
|
|
|
self::updateJoseConfiguration($container, $config, 'signers'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
52
|
|
|
* @param string $name |
53
|
|
|
* @param string[] $signature_algorithms |
54
|
|
|
* @param bool $is_public |
55
|
|
|
*/ |
56
|
|
|
public static function addVerifier(ContainerBuilder $container, $name, array $signature_algorithms, $is_public = true) |
57
|
|
|
{ |
58
|
|
|
$config = self::getVerifierConfiguration($name, $signature_algorithms, $is_public); |
59
|
|
|
self::updateJoseConfiguration($container, $config, 'verifiers'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
64
|
|
|
* @param string $name |
65
|
|
|
* @param string[] $key_encryption_algorithms |
66
|
|
|
* @param string[] $content_encryption_algorithms |
67
|
|
|
* @param string[] $compression_methods |
68
|
|
|
* @param bool $create_decrypter |
69
|
|
|
* @param bool $is_public |
70
|
|
|
*/ |
71
|
|
|
public static function addEncrypter(ContainerBuilder $container, $name, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF'], $create_decrypter = false, $is_public = true) |
72
|
|
|
{ |
73
|
|
|
$config = self::getEncrypterConfiguration($name, $key_encryption_algorithms, $content_encryption_algorithms, $compression_methods, $create_decrypter, $is_public); |
74
|
|
|
self::updateJoseConfiguration($container, $config, 'encrypters'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
79
|
|
|
* @param string $name |
80
|
|
|
* @param string[] $key_encryption_algorithms |
81
|
|
|
* @param string[] $content_encryption_algorithms |
82
|
|
|
* @param string[] $compression_methods |
83
|
|
|
* @param bool $is_public |
84
|
|
|
*/ |
85
|
|
|
public static function addDecrypter(ContainerBuilder $container, $name, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF'], $is_public = true) |
86
|
|
|
{ |
87
|
|
|
$config = self::getDecrypterConfiguration($name, $key_encryption_algorithms, $content_encryption_algorithms, $compression_methods, $is_public); |
88
|
|
|
self::updateJoseConfiguration($container, $config, 'decrypters'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
93
|
|
|
* @param string $name |
94
|
|
|
* @param string $signer |
95
|
|
|
* @param string|null $encrypter |
96
|
|
|
* @param bool $is_public |
97
|
|
|
*/ |
98
|
|
|
public static function addJWTCreator(ContainerBuilder $container, $name, $signer, $encrypter = null, $is_public = true) |
99
|
|
|
{ |
100
|
|
|
$config = self::getJWTCreatorConfiguration($name, $signer, $encrypter, $is_public); |
101
|
|
|
self::updateJoseConfiguration($container, $config, 'jwt_creators'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
106
|
|
|
* @param string $name |
107
|
|
|
* @param string $verifier |
108
|
|
|
* @param string $checker |
109
|
|
|
* @param string|null $decrypter |
110
|
|
|
* @param bool $is_public |
111
|
|
|
*/ |
112
|
|
|
public static function addJWTLoader(ContainerBuilder $container, $name, $verifier, $checker, $decrypter = null, $is_public = true) |
113
|
|
|
{ |
114
|
|
|
$config = self::getJWTLoaderConfiguration($name, $verifier, $checker, $decrypter, $is_public); |
115
|
|
|
self::updateJoseConfiguration($container, $config, 'jwt_loaders'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
120
|
|
|
* @param string $name |
121
|
|
|
* @param string $storage_path |
122
|
|
|
* @param int $nb_keys |
123
|
|
|
* @param array $key_configuration |
124
|
|
|
* @param bool $is_rotatable |
125
|
|
|
* @param bool $is_public |
126
|
|
|
*/ |
127
|
|
|
public static function addRandomJWKSet(ContainerBuilder $container, $name, $storage_path, $nb_keys, array $key_configuration, $is_rotatable = false, $is_public = true) |
128
|
|
|
{ |
129
|
|
|
$config = self::getRandomJWKSetConfiguration($name, $storage_path, $nb_keys, $key_configuration, $is_rotatable, $is_public); |
130
|
|
|
self::updateJoseConfiguration($container, $config, 'key_sets'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
135
|
|
|
* @param string $name |
136
|
|
|
* @param string $jwkset |
137
|
|
|
* @param bool $is_public |
138
|
|
|
*/ |
139
|
|
|
public static function addPublicJWKSet(ContainerBuilder $container, $name, $jwkset, $is_public = true) |
140
|
|
|
{ |
141
|
|
|
$config = self::getPublicJWKSetConfiguration($name, $jwkset, $is_public); |
142
|
|
|
self::updateJoseConfiguration($container, $config, 'key_sets'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
147
|
|
|
* @param string $name |
148
|
|
|
* @param string[] $jwksets |
149
|
|
|
* @param bool $is_public |
150
|
|
|
*/ |
151
|
|
|
public static function addJWKSets(ContainerBuilder $container, $name, array $jwksets, $is_public = true) |
152
|
|
|
{ |
153
|
|
|
$config = self::getJWKSetsConfiguration($name, $jwksets, $is_public); |
154
|
|
|
self::updateJoseConfiguration($container, $config, 'key_sets'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $name |
159
|
|
|
* @param string[] $header_checkers |
160
|
|
|
* @param string[] $claim_checkers |
161
|
|
|
* @param bool $is_public |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
private static function getCheckerConfiguration($name, array $header_checkers, array $claim_checkers, $is_public = true) |
166
|
|
|
{ |
167
|
|
|
self::checkParameters($name, $is_public); |
168
|
|
|
Assertion::allString($header_checkers); |
169
|
|
|
Assertion::allString($claim_checkers); |
170
|
|
|
|
171
|
|
|
return [ |
172
|
|
|
self::BUNDLE_ALIAS => [ |
173
|
|
|
'checkers' => [ |
174
|
|
|
$name => [ |
175
|
|
|
'is_public' => $is_public, |
176
|
|
|
'claims' => $claim_checkers, |
177
|
|
|
'headers' => $header_checkers, |
178
|
|
|
], |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $name |
186
|
|
|
* @param string[] $signature_algorithms |
187
|
|
|
* @param bool $create_verifier |
188
|
|
|
* @param bool $is_public |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
private static function getSignerConfiguration($name, array $signature_algorithms, $create_verifier = false, $is_public = true) |
193
|
|
|
{ |
194
|
|
|
self::checkParameters($name, $is_public); |
195
|
|
|
Assertion::allString($signature_algorithms); |
196
|
|
|
Assertion::notEmpty($signature_algorithms); |
197
|
|
|
Assertion::boolean($create_verifier); |
198
|
|
|
|
199
|
|
|
return [ |
200
|
|
|
self::BUNDLE_ALIAS => [ |
201
|
|
|
'signers' => [ |
202
|
|
|
$name => [ |
203
|
|
|
'is_public' => $is_public, |
204
|
|
|
'algorithms' => $signature_algorithms, |
205
|
|
|
'create_verifier' => $create_verifier, |
206
|
|
|
], |
207
|
|
|
], |
208
|
|
|
], |
209
|
|
|
]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param string $name |
214
|
|
|
* @param string[] $signature_algorithms |
215
|
|
|
* @param bool $is_public |
216
|
|
|
* |
217
|
|
|
* @return array |
218
|
|
|
*/ |
219
|
|
|
private static function getVerifierConfiguration($name, array $signature_algorithms, $is_public = true) |
220
|
|
|
{ |
221
|
|
|
self::checkParameters($name, $is_public); |
222
|
|
|
Assertion::allString($signature_algorithms); |
223
|
|
|
Assertion::notEmpty($signature_algorithms); |
224
|
|
|
|
225
|
|
|
return [ |
226
|
|
|
self::BUNDLE_ALIAS => [ |
227
|
|
|
'verifiers' => [ |
228
|
|
|
$name => [ |
229
|
|
|
'is_public' => $is_public, |
230
|
|
|
'algorithms' => $signature_algorithms, |
231
|
|
|
], |
232
|
|
|
], |
233
|
|
|
], |
234
|
|
|
]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $name |
239
|
|
|
* @param string[] $key_encryption_algorithms |
240
|
|
|
* @param string[] $content_encryption_algorithms |
241
|
|
|
* @param string[] $compression_methods |
242
|
|
|
* @param bool $create_decrypter |
243
|
|
|
* @param bool $is_public |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
|
|
private static function getEncrypterConfiguration($name, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF'], $create_decrypter = false, $is_public = true) |
248
|
|
|
{ |
249
|
|
|
self::checkParameters($name, $is_public); |
250
|
|
|
Assertion::allString($key_encryption_algorithms); |
251
|
|
|
Assertion::notEmpty($key_encryption_algorithms); |
252
|
|
|
Assertion::allString($content_encryption_algorithms); |
253
|
|
|
Assertion::notEmpty($content_encryption_algorithms); |
254
|
|
|
Assertion::boolean($create_decrypter); |
255
|
|
|
|
256
|
|
|
return [ |
257
|
|
|
self::BUNDLE_ALIAS => [ |
258
|
|
|
'encrypters' => [ |
259
|
|
|
$name => [ |
260
|
|
|
'is_public' => $is_public, |
261
|
|
|
'key_encryption_algorithms' => $key_encryption_algorithms, |
262
|
|
|
'content_encryption_algorithms' => $content_encryption_algorithms, |
263
|
|
|
'compression_methods' => $compression_methods, |
264
|
|
|
'create_decrypter' => $create_decrypter, |
265
|
|
|
], |
266
|
|
|
], |
267
|
|
|
], |
268
|
|
|
]; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param string $name |
273
|
|
|
* @param string[] $key_encryption_algorithms |
274
|
|
|
* @param string[] $content_encryption_algorithms |
275
|
|
|
* @param string[] $compression_methods |
276
|
|
|
* @param bool $is_public |
277
|
|
|
* |
278
|
|
|
* @return array |
279
|
|
|
*/ |
280
|
|
|
private static function getDecrypterConfiguration($name, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF'], $is_public = true) |
281
|
|
|
{ |
282
|
|
|
self::checkParameters($name, $is_public); |
283
|
|
|
Assertion::allString($key_encryption_algorithms); |
284
|
|
|
Assertion::notEmpty($key_encryption_algorithms); |
285
|
|
|
Assertion::allString($content_encryption_algorithms); |
286
|
|
|
Assertion::notEmpty($content_encryption_algorithms); |
287
|
|
|
|
288
|
|
|
return [ |
289
|
|
|
self::BUNDLE_ALIAS => [ |
290
|
|
|
'decrypters' => [ |
291
|
|
|
$name => [ |
292
|
|
|
'is_public' => $is_public, |
293
|
|
|
'key_encryption_algorithms' => $key_encryption_algorithms, |
294
|
|
|
'content_encryption_algorithms' => $content_encryption_algorithms, |
295
|
|
|
'compression_methods' => $compression_methods, |
296
|
|
|
], |
297
|
|
|
], |
298
|
|
|
], |
299
|
|
|
]; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param string $name |
304
|
|
|
* @param string $signer |
305
|
|
|
* @param string|null $encrypter |
306
|
|
|
* @param bool $is_public |
307
|
|
|
* |
308
|
|
|
* @return array |
309
|
|
|
*/ |
310
|
|
|
private static function getJWTCreatorConfiguration($name, $signer, $encrypter = null, $is_public = true) |
311
|
|
|
{ |
312
|
|
|
self::checkParameters($name, $is_public); |
313
|
|
|
Assertion::string($signer); |
314
|
|
|
Assertion::notEmpty($signer); |
315
|
|
|
Assertion::nullOrString($encrypter); |
316
|
|
|
|
317
|
|
|
return [ |
318
|
|
|
self::BUNDLE_ALIAS => [ |
319
|
|
|
'jwt_creators' => [ |
320
|
|
|
$name => [ |
321
|
|
|
'is_public' => $is_public, |
322
|
|
|
'signer' => $signer, |
323
|
|
|
'encrypter' => $encrypter, |
324
|
|
|
], |
325
|
|
|
], |
326
|
|
|
], |
327
|
|
|
]; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @param string $name |
332
|
|
|
* @param string $verifier |
333
|
|
|
* @param string $checker |
334
|
|
|
* @param string|null $decrypter |
335
|
|
|
* @param bool $is_public |
336
|
|
|
* |
337
|
|
|
* @return array |
338
|
|
|
*/ |
339
|
|
|
private static function getJWTLoaderConfiguration($name, $verifier, $checker, $decrypter = null, $is_public = true) |
340
|
|
|
{ |
341
|
|
|
self::checkParameters($name, $is_public); |
342
|
|
|
Assertion::string($verifier); |
343
|
|
|
Assertion::notEmpty($verifier); |
344
|
|
|
Assertion::string($checker); |
345
|
|
|
Assertion::notEmpty($checker); |
346
|
|
|
Assertion::nullOrString($decrypter); |
347
|
|
|
|
348
|
|
|
return [ |
349
|
|
|
self::BUNDLE_ALIAS => [ |
350
|
|
|
'jwt_loaders' => [ |
351
|
|
|
$name => [ |
352
|
|
|
'is_public' => $is_public, |
353
|
|
|
'verifier' => $verifier, |
354
|
|
|
'checker' => $checker, |
355
|
|
|
'decrypter' => $decrypter, |
356
|
|
|
], |
357
|
|
|
], |
358
|
|
|
], |
359
|
|
|
]; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param string $name |
364
|
|
|
* @param string $storage_path |
365
|
|
|
* @param int $nb_keys |
366
|
|
|
* @param array $key_configuration |
367
|
|
|
* @param bool $is_rotatable |
368
|
|
|
* @param bool $is_public |
369
|
|
|
* |
370
|
|
|
* @return array |
371
|
|
|
*/ |
372
|
|
|
private static function getRandomJWKSetConfiguration($name, $storage_path, $nb_keys, array $key_configuration, $is_rotatable = false, $is_public = true) |
373
|
|
|
{ |
374
|
|
|
self::checkParameters($name, $is_public); |
375
|
|
|
Assertion::string($storage_path); |
376
|
|
|
Assertion::notEmpty($storage_path); |
377
|
|
|
Assertion::integer($nb_keys); |
378
|
|
|
Assertion::greaterThan($nb_keys, 0); |
379
|
|
|
Assertion::boolean($is_rotatable); |
380
|
|
|
|
381
|
|
|
return [ |
382
|
|
|
self::BUNDLE_ALIAS => [ |
383
|
|
|
'key_sets' => [ |
384
|
|
|
$name => [ |
385
|
|
|
'auto' => [ |
386
|
|
|
'is_rotatable' => $is_rotatable, |
387
|
|
|
'is_public' => $is_public, |
388
|
|
|
'nb_keys' => $nb_keys, |
389
|
|
|
'key_configuration' => $key_configuration, |
390
|
|
|
'storage_path' => $storage_path, |
391
|
|
|
], |
392
|
|
|
], |
393
|
|
|
], |
394
|
|
|
], |
395
|
|
|
]; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @param string $name |
400
|
|
|
* @param string $jwkset |
401
|
|
|
* @param bool $is_public |
402
|
|
|
* |
403
|
|
|
* @return array |
404
|
|
|
*/ |
405
|
|
|
private static function getPublicJWKSetConfiguration($name, $jwkset, $is_public = true) |
406
|
|
|
{ |
407
|
|
|
self::checkParameters($name, $is_public); |
408
|
|
|
Assertion::string($jwkset); |
409
|
|
|
Assertion::notEmpty($jwkset); |
410
|
|
|
|
411
|
|
|
return [ |
412
|
|
|
self::BUNDLE_ALIAS => [ |
413
|
|
|
'key_sets' => [ |
414
|
|
|
$name => [ |
415
|
|
|
'public_jwkset' => [ |
416
|
|
|
'is_public' => $is_public, |
417
|
|
|
'id' => $jwkset, |
418
|
|
|
], |
419
|
|
|
], |
420
|
|
|
], |
421
|
|
|
], |
422
|
|
|
]; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* @param string $name |
427
|
|
|
* @param string[] $jwksets |
428
|
|
|
* @param bool $is_public |
429
|
|
|
* |
430
|
|
|
* @return array |
431
|
|
|
*/ |
432
|
|
|
private static function getJWKSetsConfiguration($name, array $jwksets, $is_public = true) |
433
|
|
|
{ |
434
|
|
|
self::checkParameters($name, $is_public); |
435
|
|
|
Assertion::isArray($jwksets); |
436
|
|
|
Assertion::allString($jwksets); |
437
|
|
|
Assertion::allNotEmpty($jwksets); |
438
|
|
|
|
439
|
|
|
return [ |
440
|
|
|
self::BUNDLE_ALIAS => [ |
441
|
|
|
'key_sets' => [ |
442
|
|
|
$name => [ |
443
|
|
|
'jwksets' => [ |
444
|
|
|
'is_public' => $is_public, |
445
|
|
|
'id' => $jwksets, |
446
|
|
|
], |
447
|
|
|
], |
448
|
|
|
], |
449
|
|
|
], |
450
|
|
|
]; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* @param string $name |
455
|
|
|
* @param bool $is_public |
456
|
|
|
*/ |
457
|
|
|
private static function checkParameters($name, $is_public) |
458
|
|
|
{ |
459
|
|
|
Assertion::string($name); |
460
|
|
|
Assertion::notEmpty($name); |
461
|
|
|
Assertion::boolean($is_public); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
466
|
|
|
* @param array $config |
467
|
|
|
* @param string $element |
468
|
|
|
*/ |
469
|
|
|
private static function updateJoseConfiguration(ContainerBuilder $container, array $config, $element) |
470
|
|
|
{ |
471
|
|
|
self::checkJoseBundleEnabled($container); |
472
|
|
|
$jose_config = current($container->getExtensionConfig(self::BUNDLE_ALIAS)); |
473
|
|
|
if (!isset($jose_config[$element])) { |
474
|
|
|
$jose_config[$element] = []; |
475
|
|
|
} |
476
|
|
|
$jose_config[$element] = array_merge($jose_config[$element], $config[self::BUNDLE_ALIAS][$element]); |
477
|
|
|
$container->prependExtensionConfig(self::BUNDLE_ALIAS, $jose_config); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
482
|
|
|
* |
483
|
|
|
* @throws \InvalidArgumentException |
484
|
|
|
*/ |
485
|
|
|
private static function checkJoseBundleEnabled(ContainerBuilder $container) |
486
|
|
|
{ |
487
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
488
|
|
|
Assertion::keyExists($bundles, 'SpomkyLabsJoseBundle', 'The "Spomky-Labs/JoseBundle" must be enabled.'); |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|