1 | <?php |
||
2 | |||
3 | namespace Sludio\HelperBundle\DependencyInjection\Component; |
||
4 | |||
5 | use Sludio\HelperBundle\Openidconnect\Provider\OpenIDConnectProvider; |
||
6 | use Sludio\HelperBundle\Openidconnect\Provider\BaseProvider; |
||
7 | use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
||
8 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
||
9 | use Symfony\Component\Config\Definition\Processor; |
||
10 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
11 | use Symfony\Component\DependencyInjection\Reference; |
||
12 | |||
13 | class Openidconnect extends AbstractComponent implements ExtensionInterface |
||
14 | { |
||
15 | protected $alias; |
||
16 | |||
17 | public function buildClientConfiguration(NodeDefinition $node) |
||
18 | { |
||
19 | $node->addDefaultsIfNotSet(); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
20 | $optionsNode = $node->children(); |
||
21 | |||
22 | // @formatter:off |
||
23 | $optionsNode |
||
24 | ->scalarNode('client_key')->isRequired()->defaultValue(null)->end() |
||
25 | ->scalarNode('client_secret')->defaultValue(null)->end() |
||
26 | ->scalarNode('id_token_issuer')->isRequired()->defaultValue(null)->end() |
||
27 | ->scalarNode('public_key')->isRequired()->cannotBeEmpty()->end() |
||
28 | ->scalarNode('base_uri')->isRequired()->end() |
||
29 | ->scalarNode('user_provider')->defaultValue(BaseProvider::class)->end() |
||
30 | ->scalarNode('use_session')->defaultValue(false)->end() |
||
31 | ->arrayNode('redirect') |
||
32 | ->addDefaultsIfNotSet() |
||
33 | ->children() |
||
34 | ->enumNode('type') |
||
35 | ->values(array('route', 'uri')) |
||
36 | ->defaultValue('route') |
||
37 | ->end() |
||
38 | ->scalarNode('route')->defaultValue(null)->end() |
||
39 | ->scalarNode('uri')->defaultValue(null)->end() |
||
40 | ->arrayNode('params')->prototype('variable')->end()->end() |
||
41 | ->end() |
||
42 | ->end() |
||
43 | ->arrayNode('uris') |
||
44 | ->prototype('array') |
||
45 | ->prototype('variable')->end() |
||
46 | ->end() |
||
47 | ->end() |
||
48 | ; |
||
49 | // @formatter:on |
||
50 | |||
51 | $optionsNode->end(); |
||
52 | } |
||
53 | |||
54 | private function buildUri(NodeDefinition $node) |
||
55 | { |
||
56 | $node->addDefaultsIfNotSet(); |
||
57 | $optionsNode = $node->children(); |
||
58 | |||
59 | // @formatter:off |
||
60 | $optionsNode |
||
61 | ->arrayNode('params') |
||
62 | ->prototype('variable')->end() |
||
63 | ->end() |
||
64 | ->arrayNode('url_params') |
||
65 | ->prototype('variable')->end() |
||
66 | ->end() |
||
67 | ->enumNode('method')->values(array(OpenIDConnectProvider::METHOD_GET, OpenIDConnectProvider::METHOD_POST))->cannotBeEmpty()->end() |
||
68 | ; |
||
69 | // @formatter:on |
||
70 | |||
71 | $optionsNode->end(); |
||
72 | } |
||
73 | |||
74 | public function configureClient(ContainerBuilder $container, $clientServiceKey, array $options = []) |
||
75 | { |
||
76 | $clientDefinition = $container->register($clientServiceKey, $container->getParameter($clientServiceKey.'.user_provider')); |
||
77 | $clientDefinition->setArguments([ |
||
78 | $container->getParameter($clientServiceKey), |
||
79 | [], |
||
80 | new Reference('router'), |
||
81 | new Reference('session') |
||
82 | ]); |
||
83 | } |
||
84 | |||
85 | public function configure(ContainerBuilder $container, $alias) |
||
86 | { |
||
87 | $this->alias = $alias.'.openidconnect'; |
||
88 | $clientConfigurations = $container->getParameter($this->alias.'.clients'); |
||
89 | $clientServiceKeys = []; |
||
90 | /** @var $clientConfigurations array */ |
||
91 | foreach ($clientConfigurations as $key => $clientConfig) { |
||
92 | $tree = new TreeBuilder(); |
||
93 | $processor = new Processor(); |
||
94 | $node = $tree->root('sludio_helper_openidconnect_client/clients/'.$key); |
||
95 | $this->buildClientConfiguration($node); |
||
96 | /** @var array $config */ |
||
97 | $config = $processor->process($tree->buildTree(), [$clientConfig]); |
||
98 | $clientServiceKey = $this->alias.'.client.'.$key; |
||
99 | $container->setParameter($clientServiceKey, $clientConfig); |
||
100 | $service = [ |
||
101 | 'key' => $clientServiceKey, |
||
102 | ]; |
||
103 | if (isset($config['options']['name'])) { |
||
104 | $service['name'] = $config['options']['name']; |
||
105 | } else { |
||
106 | $service['name'] = ucfirst($key); |
||
107 | } |
||
108 | |||
109 | $clientServiceKeys[$key] = $service; |
||
110 | foreach ($config as $configKey => $configValue) { |
||
111 | if ('options' === $configKey) { |
||
112 | if (\is_array($configValue)) { |
||
113 | foreach ($configValue as $parameterKey => $parameterValue) { |
||
114 | $container->setParameter($clientServiceKey.'.option.'.$parameterKey, $parameterValue); |
||
115 | } |
||
116 | } |
||
117 | } else { |
||
118 | $container->setParameter($clientServiceKey.'.'.$configKey, $configValue); |
||
119 | } |
||
120 | } |
||
121 | $uriConfigurations = $container->getParameter($this->alias.'.client.'.$key.'.uris'); |
||
122 | /** @var $uriConfigurations array */ |
||
123 | foreach ($uriConfigurations as $subKey => $uriConfig) { |
||
124 | $tree = new TreeBuilder(); |
||
125 | $processor = new Processor(); |
||
126 | $node = $tree->root('sludio_helper_openidconnect_client/clients/'.$key.'/uris/'.$subKey); |
||
127 | $this->buildUri($node); |
||
128 | $config = $processor->process($tree->buildTree(), [$uriConfig]); |
||
129 | $params = []; |
||
130 | foreach ($config as $subConfigKey => $subConfigValue) { |
||
131 | if ($subConfigKey === 'params') { |
||
132 | if (\is_array($subConfigValue)) { |
||
133 | foreach ($subConfigValue as $subParameterKey => $subParameterValue) { |
||
134 | $params[$subParameterKey] = $subParameterValue; |
||
135 | } |
||
136 | if (!empty($params)) { |
||
137 | $params['client_id'] = $container->getParameter($this->alias.'.client.'.$key.'.client_key'); |
||
138 | $container->setParameter($clientServiceKey.'.'.$subKey.'.'.$subConfigKey, $params); |
||
139 | } |
||
140 | } |
||
141 | } else { |
||
142 | $container->setParameter($clientServiceKey.'.'.$subKey.'.'.$subConfigKey, $subConfigValue); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | $this->configureClient($container, $clientServiceKey); |
||
147 | } |
||
148 | $container->getDefinition($this->alias.'.registry')->replaceArgument(1, $clientServiceKeys); |
||
149 | } |
||
150 | } |
||
151 |