-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathLightSamlSymfonyBridgeExtension.php
More file actions
208 lines (186 loc) · 8.96 KB
/
Copy pathLightSamlSymfonyBridgeExtension.php
File metadata and controls
208 lines (186 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
* This file is part of the LightSAML Symfony Bridge Bundle package.
*
* (c) Milos Tomic <tmilos@lightsaml.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace LightSaml\SymfonyBridgeBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class LightSamlSymfonyBridgeExtension extends Extension
{
/**
* Loads a specific configuration.
*
* @param array $config An array of configuration values
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
*
* @api
*/
public function load(array $config, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $config);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('container.yml');
$loader->load('own.yml');
$loader->load('system.yml');
$loader->load('party.yml');
$loader->load('store.yml');
$loader->load('credential.yml');
$loader->load('service.yml');
$loader->load('provider.yml');
$loader->load('profile.yml');
$this->configureOwn($container, $config);
$this->configureSystem($container, $config);
$this->configureParty($container, $config);
$this->configureStore($container, $config);
$this->configureCredential($container, $config);
$this->configureService($container, $config);
}
private function configureCredential(ContainerBuilder $container, array $config)
{
$this->configureCredentialStore($container, $config);
}
private function configureCredentialStore(ContainerBuilder $container, array $config)
{
$factoryReference = new Reference('lightsaml.credential.credential_store_factory');
$definition = $container->getDefinition('lightsaml.credential.credential_store');
$this->setFactoryCompatibleWay($definition, $factoryReference, 'buildFromOwnCredentialStore');
}
private function configureService(ContainerBuilder $container, array $config)
{
$this->configureServiceCredentialResolver($container, $config);
}
private function configureServiceCredentialResolver(ContainerBuilder $container, array $config)
{
$factoryReference = new Reference('lightsaml.service.credential_resolver_factory');
$definition = $container->getDefinition('lightsaml.service.credential_resolver');
$this->setFactoryCompatibleWay($definition, $factoryReference, 'build');
}
private function configureOwn(ContainerBuilder $container, array $config)
{
$container->setParameter('lightsaml.own.entity_id', $config['own']['entity_id']);
$this->configureOwnEntityDescriptor($container, $config);
$this->configureOwnCredentials($container, $config);
}
private function configureOwnEntityDescriptor(ContainerBuilder $container, array $config)
{
if (isset($config['own']['entity_descriptor_provider']['id'])) {
$container->setAlias('lightsaml.own.entity_descriptor_provider', $config['own']['entity_descriptor_provider']['id']);
} elseif (isset($config['own']['entity_descriptor_provider']['filename'])) {
if (isset($config['own']['entity_descriptor_provider']['entity_id'])) {
$definition = $container->setDefinition('lightsaml.own.entity_descriptor_provider', new Definition());
$definition
->addArgument($config['own']['entity_descriptor_provider']['filename'])
->addArgument($config['own']['entity_descriptor_provider']['entity_id']);
$this->setFactoryCompatibleWay($definition, 'LightSaml\Provider\EntityDescriptor\FileEntityDescriptorProviderFactory', 'fromEntitiesDescriptorFile');
} else {
$definition = $container->setDefinition('lightsaml.own.entity_descriptor_provider', new Definition())
->addArgument($config['own']['entity_descriptor_provider']['filename']);
$this->setFactoryCompatibleWay($definition, 'LightSaml\Provider\EntityDescriptor\FileEntityDescriptorProviderFactory', 'fromEntityDescriptorFile');
}
} else {
$definition = $container->getDefinition('lightsaml.own.entity_descriptor_provider');
$definition
->addArgument('%lightsaml.own.entity_id%')
->addArgument(new Reference('router'))
->addArgument('%lightsaml.route.login_check%')
->addArgument(null)
->addArgument(new Reference('lightsaml.own.credential_store'))
;
$this->setFactoryCompatibleWay($definition, 'LightSaml\SymfonyBridgeBundle\Factory\OwnEntityDescriptorProviderFactory', 'build');
}
}
private function configureOwnCredentials(ContainerBuilder $container, array $config)
{
if (false === isset($config['own']['credentials'])) {
return;
}
foreach ($config['own']['credentials'] as $id => $data) {
$definition = new Definition(
'LightSaml\Store\Credential\X509FileCredentialStore',
[
$config['own']['entity_id'],
$data['certificate'],
$data['key'],
$data['password'],
]
);
$definition->addTag('lightsaml.own_credential_store');
$container->setDefinition('lightsaml.own.credential_store.'.$id, $definition);
}
}
private function configureSystem(ContainerBuilder $container, array $config)
{
if (isset($config['system']['event_dispatcher'])) {
$container->removeDefinition('lightsaml.system.event_dispatcher');
$container->setAlias('lightsaml.system.event_dispatcher', $config['system']['event_dispatcher']);
}
if (isset($config['system']['logger'])) {
$container->setAlias('lightsaml.system.logger', $config['system']['logger']);
}
}
private function configureParty(ContainerBuilder $container, array $config)
{
if (isset($config['party']['idp']['files'])) {
$store = $container->getDefinition('lightsaml.party.idp_entity_descriptor_store');
foreach ($config['party']['idp']['files'] as $id => $file) {
$id = sprintf('lightsaml.party.idp_entity_descriptor_store.file.%s', $id);
if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
// Symfony >= 3.3
$container
->setDefinition($id, new ChildDefinition('lightsaml.party.idp_entity_descriptor_store.file'))
->replaceArgument(0, $file);
} else {
// Symfony < 3.3
$container
->setDefinition($id, new DefinitionDecorator('lightsaml.party.idp_entity_descriptor_store.file'))
->replaceArgument(0, $file);
}
$store->addMethodCall('add', [new Reference($id)]);
}
}
}
private function configureStore(ContainerBuilder $container, array $config)
{
if (isset($config['store']['request'])) {
$container->setDefinition('lightsaml.store.request', new ChildDefinition($config['store']['request']));
}
if (isset($config['store']['id_state'])) {
$container->setDefinition('lightsaml.store.id_state', new ChildDefinition($config['store']['id_state']));
}
if (isset($config['store']['sso_state'])) {
$container->setDefinition('lightsaml.store.sso_state', new ChildDefinition($config['store']['sso_state']));
}
}
/**
* @param Definition $definition
* @param string $classOrReference
* @param string $method
*/
private function setFactoryCompatibleWay(Definition $definition, $classOrReference, $method)
{
if (method_exists($definition, 'setFactory')) {
$definition->setFactory([$classOrReference, $method]);
} else {
if ($classOrReference instanceof Reference) {
$definition->setFactoryService((string) $classOrReference);
} else {
$definition->setFactoryClass($classOrReference);
}
$definition->setFactoryMethod($method);
}
}
}