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