1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Bundle\JoseFramework\Helper; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
|
18
|
|
|
class ConfigurationHelper |
19
|
|
|
{ |
20
|
|
|
public const BUNDLE_ALIAS = 'jose'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string[] $signatureAlgorithms |
24
|
|
|
*/ |
25
|
|
|
public static function addJWSBuilder(ContainerBuilder $container, string $name, array $signatureAlgorithms, bool $is_public = true, array $tags = []): void |
26
|
|
|
{ |
27
|
|
|
$config = [ |
28
|
|
|
self::BUNDLE_ALIAS => [ |
29
|
|
|
'jws' => [ |
30
|
|
|
'builders' => [ |
31
|
|
|
$name => [ |
32
|
|
|
'is_public' => $is_public, |
33
|
|
|
'signature_algorithms' => $signatureAlgorithms, |
34
|
|
|
'tags' => $tags, |
35
|
|
|
], |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
], |
39
|
|
|
]; |
40
|
|
|
self::updateJoseConfiguration($container, $config, 'jws'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string[] $signatureAlgorithms |
45
|
|
|
*/ |
46
|
|
|
public static function addJWSVerifier(ContainerBuilder $container, string $name, array $signatureAlgorithms, bool $is_public = true, array $tags = []): void |
47
|
|
|
{ |
48
|
|
|
$config = [ |
49
|
|
|
self::BUNDLE_ALIAS => [ |
50
|
|
|
'jws' => [ |
51
|
|
|
'verifiers' => [ |
52
|
|
|
$name => [ |
53
|
|
|
'is_public' => $is_public, |
54
|
|
|
'signature_algorithms' => $signatureAlgorithms, |
55
|
|
|
'tags' => $tags, |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
], |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
self::updateJoseConfiguration($container, $config, 'jws'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string[] $serializers |
67
|
|
|
*/ |
68
|
|
|
public static function addJWSSerializer(ContainerBuilder $container, string $name, array $serializers, bool $is_public = true, array $tags = []): void |
69
|
|
|
{ |
70
|
|
|
$config = [ |
71
|
|
|
self::BUNDLE_ALIAS => [ |
72
|
|
|
'jws' => [ |
73
|
|
|
'serializers' => [ |
74
|
|
|
$name => [ |
75
|
|
|
'is_public' => $is_public, |
76
|
|
|
'serializers' => $serializers, |
77
|
|
|
'tags' => $tags, |
78
|
|
|
], |
79
|
|
|
], |
80
|
|
|
], |
81
|
|
|
], |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
self::updateJoseConfiguration($container, $config, 'jws'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string[] $serializers |
89
|
|
|
* @param string[] $signature_algorithms |
90
|
|
|
* @param string[] $header_checkers |
91
|
|
|
*/ |
92
|
|
|
public static function addJWSLoader(ContainerBuilder $container, string $name, array $serializers, array $signature_algorithms, array $header_checkers, bool $is_public = true, array $tags = []): void |
93
|
|
|
{ |
94
|
|
|
$config = [ |
95
|
|
|
self::BUNDLE_ALIAS => [ |
96
|
|
|
'jws' => [ |
97
|
|
|
'loaders' => [ |
98
|
|
|
$name => [ |
99
|
|
|
'is_public' => $is_public, |
100
|
|
|
'serializers' => $serializers, |
101
|
|
|
'signature_algorithms' => $signature_algorithms, |
102
|
|
|
'header_checkers' => $header_checkers, |
103
|
|
|
'tags' => $tags, |
104
|
|
|
], |
105
|
|
|
], |
106
|
|
|
], |
107
|
|
|
], |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
self::updateJoseConfiguration($container, $config, 'jws'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string[] $jwe_serializers |
115
|
|
|
* @param string[] $key_encryption_algorithms |
116
|
|
|
* @param string[] $content_encryption_algorithms |
117
|
|
|
* @param string[] $compression_methods |
118
|
|
|
* @param string[] $jwe_header_checkers |
119
|
|
|
* @param string[] $jws_serializers |
120
|
|
|
* @param string[] $signature_algorithms |
121
|
|
|
* @param string[] $jws_header_checkers |
122
|
|
|
*/ |
123
|
|
|
public static function addNestedTokenLoader(ContainerBuilder $container, string $name, array $jwe_serializers, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods, array $jwe_header_checkers, array $jws_serializers, array $signature_algorithms, array $jws_header_checkers, bool $is_public = true, array $tags = []): void |
124
|
|
|
{ |
125
|
|
|
$config = [ |
126
|
|
|
self::BUNDLE_ALIAS => [ |
127
|
|
|
'nested_token' => [ |
128
|
|
|
'loaders' => [ |
129
|
|
|
$name => [ |
130
|
|
|
'is_public' => $is_public, |
131
|
|
|
'jwe_serializers' => $jwe_serializers, |
132
|
|
|
'key_encryption_algorithms' => $key_encryption_algorithms, |
133
|
|
|
'content_encryption_algorithms' => $content_encryption_algorithms, |
134
|
|
|
'compression_methods' => $compression_methods, |
135
|
|
|
'jwe_header_checkers' => $jwe_header_checkers, |
136
|
|
|
'jws_serializers' => $jws_serializers, |
137
|
|
|
'signature_algorithms' => $signature_algorithms, |
138
|
|
|
'jws_header_checkers' => $jws_header_checkers, |
139
|
|
|
'tags' => $tags, |
140
|
|
|
], |
141
|
|
|
], |
142
|
|
|
], |
143
|
|
|
], |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
self::updateJoseConfiguration($container, $config, 'nested_token'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string[] $jwe_serializers |
151
|
|
|
* @param string[] $key_encryption_algorithms |
152
|
|
|
* @param string[] $content_encryption_algorithms |
153
|
|
|
* @param string[] $compression_methods |
154
|
|
|
* @param string[] $jws_serializers |
155
|
|
|
* @param string[] $signature_algorithms |
156
|
|
|
*/ |
157
|
|
|
public static function addNestedTokenBuilder(ContainerBuilder $container, string $name, array $jwe_serializers, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods, array $jws_serializers, array $signature_algorithms, bool $is_public = true, array $tags = []): void |
158
|
|
|
{ |
159
|
|
|
$config = [ |
160
|
|
|
self::BUNDLE_ALIAS => [ |
161
|
|
|
'nested_token' => [ |
162
|
|
|
'builders' => [ |
163
|
|
|
$name => [ |
164
|
|
|
'is_public' => $is_public, |
165
|
|
|
'jwe_serializers' => $jwe_serializers, |
166
|
|
|
'key_encryption_algorithms' => $key_encryption_algorithms, |
167
|
|
|
'content_encryption_algorithms' => $content_encryption_algorithms, |
168
|
|
|
'compression_methods' => $compression_methods, |
169
|
|
|
'jws_serializers' => $jws_serializers, |
170
|
|
|
'signature_algorithms' => $signature_algorithms, |
171
|
|
|
'tags' => $tags, |
172
|
|
|
], |
173
|
|
|
], |
174
|
|
|
], |
175
|
|
|
], |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
self::updateJoseConfiguration($container, $config, 'nested_token'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string[] $serializers |
183
|
|
|
*/ |
184
|
|
|
public static function addJWESerializer(ContainerBuilder $container, string $name, array $serializers, bool $is_public = true, array $tags = []): void |
185
|
|
|
{ |
186
|
|
|
$config = [ |
187
|
|
|
self::BUNDLE_ALIAS => [ |
188
|
|
|
'jwe' => [ |
189
|
|
|
'serializers' => [ |
190
|
|
|
$name => [ |
191
|
|
|
'is_public' => $is_public, |
192
|
|
|
'serializers' => $serializers, |
193
|
|
|
'tags' => $tags, |
194
|
|
|
], |
195
|
|
|
], |
196
|
|
|
], |
197
|
|
|
], |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
self::updateJoseConfiguration($container, $config, 'jwe'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string[] $serializers |
205
|
|
|
* @param string[] $key_encryption_algorithms |
206
|
|
|
* @param string[] $content_encryption_algorithms |
207
|
|
|
* @param string[] $compression_methods |
208
|
|
|
* @param string[] $header_checkers |
209
|
|
|
*/ |
210
|
|
|
public static function addJWELoader(ContainerBuilder $container, string $name, array $serializers, array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods, array $header_checkers, bool $is_public = true, array $tags = []): void |
211
|
|
|
{ |
212
|
|
|
$config = [ |
213
|
|
|
self::BUNDLE_ALIAS => [ |
214
|
|
|
'jwe' => [ |
215
|
|
|
'loaders' => [ |
216
|
|
|
$name => [ |
217
|
|
|
'is_public' => $is_public, |
218
|
|
|
'serializers' => $serializers, |
219
|
|
|
'key_encryption_algorithms' => $key_encryption_algorithms, |
220
|
|
|
'content_encryption_algorithms' => $content_encryption_algorithms, |
221
|
|
|
'compression_methods' => $compression_methods, |
222
|
|
|
'header_checkers' => $header_checkers, |
223
|
|
|
'tags' => $tags, |
224
|
|
|
], |
225
|
|
|
], |
226
|
|
|
], |
227
|
|
|
], |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
self::updateJoseConfiguration($container, $config, 'jwe'); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string[] $claimCheckers |
235
|
|
|
*/ |
236
|
|
|
public static function addClaimChecker(ContainerBuilder $container, string $name, array $claimCheckers, bool $is_public = true, array $tags = []): void |
237
|
|
|
{ |
238
|
|
|
$config = [ |
239
|
|
|
self::BUNDLE_ALIAS => [ |
240
|
|
|
'checkers' => [ |
241
|
|
|
'claims' => [ |
242
|
|
|
$name => [ |
243
|
|
|
'is_public' => $is_public, |
244
|
|
|
'claims' => $claimCheckers, |
245
|
|
|
'tags' => $tags, |
246
|
|
|
], |
247
|
|
|
], |
248
|
|
|
], |
249
|
|
|
], |
250
|
|
|
]; |
251
|
|
|
|
252
|
|
|
self::updateJoseConfiguration($container, $config, 'checkers'); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string[] $headerCheckers |
257
|
|
|
*/ |
258
|
|
|
public static function addHeaderChecker(ContainerBuilder $container, string $name, array $headerCheckers, bool $is_public = true, array $tags = []): void |
259
|
|
|
{ |
260
|
|
|
$config = [ |
261
|
|
|
self::BUNDLE_ALIAS => [ |
262
|
|
|
'checkers' => [ |
263
|
|
|
'headers' => [ |
264
|
|
|
$name => [ |
265
|
|
|
'is_public' => $is_public, |
266
|
|
|
'headers' => $headerCheckers, |
267
|
|
|
'tags' => $tags, |
268
|
|
|
], |
269
|
|
|
], |
270
|
|
|
], |
271
|
|
|
], |
272
|
|
|
]; |
273
|
|
|
|
274
|
|
|
self::updateJoseConfiguration($container, $config, 'checkers'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public static function addKey(ContainerBuilder $container, string $name, string $type, array $parameters, bool $is_public = true, array $tags = []): void |
278
|
|
|
{ |
279
|
|
|
$parameters['is_public'] = $is_public; |
280
|
|
|
$parameters['tags'] = $tags; |
281
|
|
|
$config = [ |
282
|
|
|
self::BUNDLE_ALIAS => [ |
283
|
|
|
'keys' => [ |
284
|
|
|
$name => [ |
285
|
|
|
$type => $parameters, |
286
|
|
|
], |
287
|
|
|
], |
288
|
|
|
], |
289
|
|
|
]; |
290
|
|
|
|
291
|
|
|
self::updateJoseConfiguration($container, $config, 'keys'); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public static function addKeyset(ContainerBuilder $container, string $name, string $type, array $parameters, bool $is_public = true, array $tags = []): void |
295
|
|
|
{ |
296
|
|
|
$parameters['is_public'] = $is_public; |
297
|
|
|
$parameters['tags'] = $tags; |
298
|
|
|
$config = [ |
299
|
|
|
self::BUNDLE_ALIAS => [ |
300
|
|
|
'key_sets' => [ |
301
|
|
|
$name => [ |
302
|
|
|
$type => $parameters, |
303
|
|
|
], |
304
|
|
|
], |
305
|
|
|
], |
306
|
|
|
]; |
307
|
|
|
|
308
|
|
|
self::updateJoseConfiguration($container, $config, 'key_sets'); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public static function addKeyUri(ContainerBuilder $container, string $name, array $parameters, bool $is_public = true, array $tags = []): void |
312
|
|
|
{ |
313
|
|
|
$parameters['is_public'] = $is_public; |
314
|
|
|
$parameters['tags'] = $tags; |
315
|
|
|
$config = [ |
316
|
|
|
self::BUNDLE_ALIAS => [ |
317
|
|
|
'jwk_uris' => [ |
318
|
|
|
$name => $parameters, |
319
|
|
|
], |
320
|
|
|
], |
321
|
|
|
]; |
322
|
|
|
|
323
|
|
|
self::updateJoseConfiguration($container, $config, 'jwk_uris'); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public static function addJWEBuilder(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], bool $is_public = true, array $tags = []): void |
327
|
|
|
{ |
328
|
|
|
$config = [ |
329
|
|
|
self::BUNDLE_ALIAS => [ |
330
|
|
|
'jwe' => [ |
331
|
|
|
'builders' => [ |
332
|
|
|
$name => [ |
333
|
|
|
'is_public' => $is_public, |
334
|
|
|
'key_encryption_algorithms' => $keyEncryptionAlgorithm, |
335
|
|
|
'content_encryption_algorithms' => $contentEncryptionAlgorithms, |
336
|
|
|
'compression_methods' => $compressionMethods, |
337
|
|
|
'tags' => $tags, |
338
|
|
|
], |
339
|
|
|
], |
340
|
|
|
], |
341
|
|
|
], |
342
|
|
|
]; |
343
|
|
|
|
344
|
|
|
self::updateJoseConfiguration($container, $config, 'jwe'); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
public static function addJWEDecrypter(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], bool $is_public = true, array $tags = []): void |
348
|
|
|
{ |
349
|
|
|
$config = [ |
350
|
|
|
self::BUNDLE_ALIAS => [ |
351
|
|
|
'jwe' => [ |
352
|
|
|
'decrypters' => [ |
353
|
|
|
$name => [ |
354
|
|
|
'is_public' => $is_public, |
355
|
|
|
'key_encryption_algorithms' => $keyEncryptionAlgorithm, |
356
|
|
|
'content_encryption_algorithms' => $contentEncryptionAlgorithms, |
357
|
|
|
'compression_methods' => $compressionMethods, |
358
|
|
|
'tags' => $tags, |
359
|
|
|
], |
360
|
|
|
], |
361
|
|
|
], |
362
|
|
|
], |
363
|
|
|
]; |
364
|
|
|
|
365
|
|
|
self::updateJoseConfiguration($container, $config, 'jwe'); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
private static function updateJoseConfiguration(ContainerBuilder $container, array $config, string $element): void |
369
|
|
|
{ |
370
|
|
|
$jose_config = current($container->getExtensionConfig(self::BUNDLE_ALIAS)); |
371
|
|
|
if (!isset($jose_config[$element])) { |
372
|
|
|
$jose_config[$element] = []; |
373
|
|
|
} |
374
|
|
|
$jose_config[$element] = array_merge($jose_config[$element], $config[self::BUNDLE_ALIAS][$element]); |
375
|
|
|
$container->prependExtensionConfig(self::BUNDLE_ALIAS, $jose_config); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|