Failed Conditions
Push — master ( e08481...7ad838 )
by Florent
03:52 queued 01:57
created

ConfigurationHelper::addJWSVerifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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
    const BUNDLE_ALIAS = 'jose';
21
22
    /**
23
     * @param ContainerBuilder $container
24
     * @param string           $name
25
     * @param string[]         $signatureAlgorithms
26
     * @param bool             $is_public
27
     * @param array            $tags
28
     */
29
    public static function addJWSBuilder(ContainerBuilder $container, string $name, array $signatureAlgorithms, bool $is_public = true, array $tags = [])
30
    {
31
        $config = [
32
            self::BUNDLE_ALIAS => [
33
                'jws' => [
34
                    'builders' => [
35
                        $name => [
36
                            'is_public'            => $is_public,
37
                            'signature_algorithms' => $signatureAlgorithms,
38
                            'tags'                 => $tags,
39
                        ],
40
                    ],
41
                ],
42
            ],
43
        ];
44
        self::updateJoseConfiguration($container, $config, 'jws');
45
    }
46
47
    /**
48
     * @param ContainerBuilder $container
49
     * @param string           $name
50
     * @param string[]         $signatureAlgorithms
51
     * @param bool             $is_public
52
     * @param array            $tags
53
     */
54
    public static function addJWSVerifier(ContainerBuilder $container, string $name, array $signatureAlgorithms, bool $is_public = true, array $tags = [])
55
    {
56
        $config = [
57
            self::BUNDLE_ALIAS => [
58
                'jws' => [
59
                    'verifiers' => [
60
                        $name => [
61
                            'is_public'            => $is_public,
62
                            'signature_algorithms' => $signatureAlgorithms,
63
                            'tags'                 => $tags,
64
                        ],
65
                    ],
66
                ],
67
            ],
68
        ];
69
70
        self::updateJoseConfiguration($container, $config, 'jws');
71
    }
72
73
    /**
74
     * @param ContainerBuilder $container
75
     * @param string           $name
76
     * @param string[]         $serializers
77
     * @param bool             $is_public
78
     * @param array            $tags
79
     */
80
    public static function addJWSSerializer(ContainerBuilder $container, string $name, array $serializers, bool $is_public = true, array $tags = [])
81
    {
82
        $config = [
83
            self::BUNDLE_ALIAS => [
84
                'jws' => [
85
                    'serializers' => [
86
                        $name => [
87
                            'is_public'   => $is_public,
88
                            'serializers' => $serializers,
89
                            'tags'        => $tags,
90
                        ],
91
                    ],
92
                ],
93
            ],
94
        ];
95
96
        self::updateJoseConfiguration($container, $config, 'jws');
97
    }
98
99
    /**
100
     * @param ContainerBuilder $container
101
     * @param string           $name
102
     * @param string[]         $serializers
103
     * @param string[]         $signature_algorithms
104
     * @param string[]         $header_checkers
105
     * @param bool             $is_public
106
     * @param array            $tags
107
     */
108
    public static function addJWSLoader(ContainerBuilder $container, string $name, array $serializers, array $signature_algorithms, array $header_checkers, bool $is_public = true, array $tags = [])
109
    {
110
        $config = [
111
            self::BUNDLE_ALIAS => [
112
                'jws' => [
113
                    'loaders' => [
114
                        $name => [
115
                            'is_public'            => $is_public,
116
                            'serializers'          => $serializers,
117
                            'signature_algorithms' => $signature_algorithms,
118
                            'header_checkers'      => $header_checkers,
119
                            'tags'                 => $tags,
120
                        ],
121
                    ],
122
                ],
123
            ],
124
        ];
125
126
        self::updateJoseConfiguration($container, $config, 'jws');
127
    }
128
129
    /**
130
     * @param ContainerBuilder $container
131
     * @param string           $name
132
     * @param string[]         $jwe_serializers
133
     * @param string[]         $key_encryption_algorithms
134
     * @param string[]         $content_encryption_algorithms
135
     * @param string[]         $compression_methods
136
     * @param string[]         $jwe_header_checkers
137
     * @param string[]         $jws_serializers
138
     * @param string[]         $signature_algorithms
139
     * @param string[]         $jws_header_checkers
140
     * @param bool             $is_public
141
     * @param array            $tags
142
     */
143
    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 = [])
144
    {
145
        $config = [
146
            self::BUNDLE_ALIAS => [
147
                'nested_token' => [
148
                    'loaders' => [
149
                        $name => [
150
                            'is_public'                     => $is_public,
151
                            'jwe_serializers'               => $jwe_serializers,
152
                            'key_encryption_algorithms'     => $key_encryption_algorithms,
153
                            'content_encryption_algorithms' => $content_encryption_algorithms,
154
                            'compression_methods'           => $compression_methods,
155
                            'jwe_header_checkers'           => $jwe_header_checkers,
156
                            'jws_serializers'               => $jws_serializers,
157
                            'signature_algorithms'          => $signature_algorithms,
158
                            'jws_header_checkers'           => $jws_header_checkers,
159
                            'tags'                          => $tags,
160
                        ],
161
                    ],
162
                ],
163
            ],
164
        ];
165
166
        self::updateJoseConfiguration($container, $config, 'nested_token');
167
    }
168
169
    /**
170
     * @param ContainerBuilder $container
171
     * @param string           $name
172
     * @param string[]         $jwe_serializers
173
     * @param string[]         $key_encryption_algorithms
174
     * @param string[]         $content_encryption_algorithms
175
     * @param string[]         $compression_methods
176
     * @param string[]         $jws_serializers
177
     * @param string[]         $signature_algorithms
178
     * @param bool             $is_public
179
     * @param array            $tags
180
     */
181
    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 = [])
182
    {
183
        $config = [
184
            self::BUNDLE_ALIAS => [
185
                'nested_token' => [
186
                    'builders' => [
187
                        $name => [
188
                            'is_public'                     => $is_public,
189
                            'jwe_serializers'               => $jwe_serializers,
190
                            'key_encryption_algorithms'     => $key_encryption_algorithms,
191
                            'content_encryption_algorithms' => $content_encryption_algorithms,
192
                            'compression_methods'           => $compression_methods,
193
                            'jws_serializers'               => $jws_serializers,
194
                            'signature_algorithms'          => $signature_algorithms,
195
                            'tags'                          => $tags,
196
                        ],
197
                    ],
198
                ],
199
            ],
200
        ];
201
202
        self::updateJoseConfiguration($container, $config, 'nested_token');
203
    }
204
205
    /**
206
     * @param ContainerBuilder $container
207
     * @param string           $name
208
     * @param string[]         $serializers
209
     * @param bool             $is_public
210
     * @param array            $tags
211
     */
212
    public static function addJWESerializer(ContainerBuilder $container, string $name, array $serializers, bool $is_public = true, array $tags = [])
213
    {
214
        $config = [
215
            self::BUNDLE_ALIAS => [
216
                'jwe' => [
217
                    'serializers' => [
218
                        $name => [
219
                            'is_public'   => $is_public,
220
                            'serializers' => $serializers,
221
                            'tags'        => $tags,
222
                        ],
223
                    ],
224
                ],
225
            ],
226
        ];
227
228
        self::updateJoseConfiguration($container, $config, 'jwe');
229
    }
230
231
    /**
232
     * @param ContainerBuilder $container
233
     * @param string           $name
234
     * @param string[]         $serializers
235
     * @param string[]         $key_encryption_algorithms
236
     * @param string[]         $content_encryption_algorithms
237
     * @param string[]         $compression_methods
238
     * @param string[]         $header_checkers
239
     * @param bool             $is_public
240
     * @param array            $tags
241
     */
242
    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 = [])
243
    {
244
        $config = [
245
            self::BUNDLE_ALIAS => [
246
                'jwe' => [
247
                    'loaders' => [
248
                        $name => [
249
                            'is_public'                     => $is_public,
250
                            'serializers'                   => $serializers,
251
                            'key_encryption_algorithms'     => $key_encryption_algorithms,
252
                            'content_encryption_algorithms' => $content_encryption_algorithms,
253
                            'compression_methods'           => $compression_methods,
254
                            'header_checkers'               => $header_checkers,
255
                            'tags'                          => $tags,
256
                        ],
257
                    ],
258
                ],
259
            ],
260
        ];
261
262
        self::updateJoseConfiguration($container, $config, 'jwe');
263
    }
264
265
    /**
266
     * @param ContainerBuilder $container
267
     * @param string           $name
268
     * @param string[]         $claimCheckers
269
     * @param bool             $is_public
270
     * @param array            $tags
271
     */
272
    public static function addClaimChecker(ContainerBuilder $container, string $name, array  $claimCheckers, bool $is_public = true, array $tags = [])
273
    {
274
        $config = [
275
            self::BUNDLE_ALIAS => [
276
                'checkers' => [
277
                    'claims' => [
278
                        $name => [
279
                            'is_public' => $is_public,
280
                            'claims'    => $claimCheckers,
281
                            'tags'      => $tags,
282
                        ],
283
                    ],
284
                ],
285
            ],
286
        ];
287
288
        self::updateJoseConfiguration($container, $config, 'checkers');
289
    }
290
291
    /**
292
     * @param ContainerBuilder $container
293
     * @param string           $name
294
     * @param string[]         $headerCheckers
295
     * @param bool             $is_public
296
     * @param array            $tags
297
     */
298
    public static function addHeaderChecker(ContainerBuilder $container, string $name, array  $headerCheckers, bool $is_public = true, array $tags = [])
299
    {
300
        $config = [
301
            self::BUNDLE_ALIAS => [
302
                'checkers' => [
303
                    'headers' => [
304
                        $name => [
305
                            'is_public' => $is_public,
306
                            'headers'   => $headerCheckers,
307
                            'tags'      => $tags,
308
                        ],
309
                    ],
310
                ],
311
            ],
312
        ];
313
314
        self::updateJoseConfiguration($container, $config, 'checkers');
315
    }
316
317
    /**
318
     * @param ContainerBuilder $container
319
     * @param string           $name
320
     * @param string           $type
321
     * @param array            $parameters
322
     * @param bool             $is_public
323
     * @param array            $tags
324
     */
325
    public static function addKey(ContainerBuilder $container, string $name, string $type, array  $parameters, bool $is_public = true, array $tags = [])
326
    {
327
        $parameters['is_public'] = $is_public;
328
        $parameters['tags'] = $tags;
329
        $config = [
330
            self::BUNDLE_ALIAS => [
331
                'keys' => [
332
                    $name => [
333
                        $type => $parameters,
334
                    ],
335
                ],
336
            ],
337
        ];
338
339
        self::updateJoseConfiguration($container, $config, 'keys');
340
    }
341
342
    /**
343
     * @param ContainerBuilder $container
344
     * @param string           $name
345
     * @param string           $type
346
     * @param array            $parameters
347
     * @param bool             $is_public
348
     * @param array            $tags
349
     */
350
    public static function addKeyset(ContainerBuilder $container, string $name, string $type, array  $parameters, bool $is_public = true, array $tags = [])
351
    {
352
        $parameters['is_public'] = $is_public;
353
        $parameters['tags'] = $tags;
354
        $config = [
355
            self::BUNDLE_ALIAS => [
356
                'key_sets' => [
357
                    $name => [
358
                        $type => $parameters,
359
                    ],
360
                ],
361
            ],
362
        ];
363
364
        self::updateJoseConfiguration($container, $config, 'key_sets');
365
    }
366
367
    /**
368
     * @param ContainerBuilder $container
369
     * @param string           $name
370
     * @param array            $parameters
371
     * @param bool             $is_public
372
     * @param array            $tags
373
     */
374
    public static function addKeyUri(ContainerBuilder $container, string $name, array $parameters, bool $is_public = true, array $tags = [])
375
    {
376
        $parameters['is_public'] = $is_public;
377
        $parameters['tags'] = $tags;
378
        $config = [
379
            self::BUNDLE_ALIAS => [
380
                'jwk_uris' => [
381
                    $name => $parameters,
382
                ],
383
            ],
384
        ];
385
386
        self::updateJoseConfiguration($container, $config, 'jwk_uris');
387
    }
388
389
    /**
390
     * @param ContainerBuilder $container
391
     * @param string           $name
392
     * @param array            $keyEncryptionAlgorithm
393
     * @param array            $contentEncryptionAlgorithms
394
     * @param array            $compressionMethods
395
     * @param bool             $is_public
396
     * @param array            $tags
397
     */
398
    public static function addJWEBuilder(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], bool $is_public = true, array $tags = [])
399
    {
400
        $config = [
401
            self::BUNDLE_ALIAS => [
402
                'jwe' => [
403
                    'builders' => [
404
                        $name => [
405
                            'is_public'                     => $is_public,
406
                            'key_encryption_algorithms'     => $keyEncryptionAlgorithm,
407
                            'content_encryption_algorithms' => $contentEncryptionAlgorithms,
408
                            'compression_methods'           => $compressionMethods,
409
                            'tags'                          => $tags,
410
                        ],
411
                    ],
412
                ],
413
            ],
414
        ];
415
416
        self::updateJoseConfiguration($container, $config, 'jwe');
417
    }
418
419
    /**
420
     * @param ContainerBuilder $container
421
     * @param string           $name
422
     * @param array            $keyEncryptionAlgorithm
423
     * @param array            $contentEncryptionAlgorithms
424
     * @param array            $compressionMethods
425
     * @param bool             $is_public
426
     * @param array            $tags
427
     */
428
    public static function addJWEDecrypter(ContainerBuilder $container, string $name, array $keyEncryptionAlgorithm, array $contentEncryptionAlgorithms, array $compressionMethods = ['DEF'], bool $is_public = true, array $tags = [])
429
    {
430
        $config = [
431
            self::BUNDLE_ALIAS => [
432
                'jwe' => [
433
                    'decrypters' => [
434
                        $name => [
435
                            'is_public'                     => $is_public,
436
                            'key_encryption_algorithms'     => $keyEncryptionAlgorithm,
437
                            'content_encryption_algorithms' => $contentEncryptionAlgorithms,
438
                            'compression_methods'           => $compressionMethods,
439
                            'tags'                          => $tags,
440
                        ],
441
                    ],
442
                ],
443
            ],
444
        ];
445
446
        self::updateJoseConfiguration($container, $config, 'jwe');
447
    }
448
449
    /**
450
     * @param ContainerBuilder $container
451
     * @param array            $config
452
     * @param string           $element
453
     */
454
    private static function updateJoseConfiguration(ContainerBuilder $container, array $config, string $element)
455
    {
456
        $jose_config = current($container->getExtensionConfig(self::BUNDLE_ALIAS));
457
        if (!isset($jose_config[$element])) {
458
            $jose_config[$element] = [];
459
        }
460
        $jose_config[$element] = array_merge($jose_config[$element], $config[self::BUNDLE_ALIAS][$element]);
461
        $container->prependExtensionConfig(self::BUNDLE_ALIAS, $jose_config);
462
    }
463
}
464