1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 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\DependencyInjection\Source\KeyManagement; |
15
|
|
|
|
16
|
|
|
use Http\HttplugBundle\HttplugBundle; |
17
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Compiler; |
18
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source; |
19
|
|
|
use Jose\Component\KeyManagement\JWKFactory; |
20
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
21
|
|
|
use Symfony\Component\Config\FileLocator; |
22
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
24
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class KeyManagementSource. |
28
|
|
|
*/ |
29
|
|
|
final class KeyManagementSource implements Source |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var Source[] |
33
|
|
|
*/ |
34
|
|
|
private $sources; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* KeyManagementSource constructor. |
38
|
|
|
*/ |
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
$this->sources = [ |
42
|
|
|
new JWKSetSource(), |
43
|
|
|
new JWKSource(), |
44
|
|
|
new JWKUriSource(), |
45
|
|
|
]; |
46
|
|
|
if (class_exists(HttplugBundle::class)) { |
47
|
|
|
$this->sources[] = new JKUSource(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function name(): string |
55
|
|
|
{ |
56
|
|
|
return 'key_mgmt'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function load(array $configs, ContainerBuilder $container) |
63
|
|
|
{ |
64
|
|
|
if (!$this->isEnabled()) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config')); |
68
|
|
|
$loader->load('analyzers.yml'); |
69
|
|
|
$loader->load('jwk_factory.yml'); |
70
|
|
|
$loader->load('jwk_services.yml'); |
71
|
|
|
|
72
|
|
|
foreach ($this->sources as $source) { |
73
|
|
|
$source->load($configs, $container); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function getNodeDefinition(ArrayNodeDefinition $node) |
81
|
|
|
{ |
82
|
|
|
if (!$this->isEnabled()) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
foreach ($this->sources as $source) { |
86
|
|
|
$source->getNodeDefinition($node); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function prepend(ContainerBuilder $container, array $config): array |
94
|
|
|
{ |
95
|
|
|
if (!$this->isEnabled()) { |
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
$result = []; |
99
|
|
|
foreach ($this->sources as $source) { |
100
|
|
|
$prepend = $source->prepend($container, $config); |
101
|
|
|
if (!empty($prepend)) { |
102
|
|
|
$result[$source->name()] = $prepend; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
private function isEnabled(): bool |
113
|
|
|
{ |
114
|
|
|
return class_exists(JWKFactory::class); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return CompilerPassInterface[] |
119
|
|
|
*/ |
120
|
|
|
public function getCompilerPasses(): array |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
new Compiler\KeyAnalyzerCompilerPass(), |
124
|
|
|
new Compiler\KeySetControllerCompilerPass(), |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|