1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\DependencyInjection; |
20
|
|
|
|
21
|
|
|
use GuzzleHttp\HandlerStack; |
22
|
|
|
use GuzzleHttp\Middleware; |
23
|
|
|
use Surfnet\StepupBundle\Form\ChoiceList\LocaleChoiceList; |
24
|
|
|
use Surfnet\StepupBundle\Http\CookieHelper; |
25
|
|
|
use Surfnet\StepupBundle\Service\LoaResolutionService; |
26
|
|
|
use Surfnet\StepupBundle\Service\SecondFactorTypeService; |
27
|
|
|
use Surfnet\StepupBundle\Service\SmsSecondFactorService; |
28
|
|
|
use Surfnet\StepupBundle\Value\Loa; |
29
|
|
|
use Symfony\Component\Config\Definition\Processor; |
30
|
|
|
use Symfony\Component\Config\FileLocator; |
31
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
32
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
33
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
34
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
35
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
36
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class SurfnetStepupExtension |
40
|
|
|
* |
41
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
42
|
|
|
*/ |
43
|
|
|
class SurfnetStepupExtension extends Extension |
44
|
|
|
{ |
45
|
|
|
public function load(array $config, ContainerBuilder $container) |
46
|
|
|
{ |
47
|
|
|
$processor = new Processor(); |
48
|
|
|
$config = $processor->processConfiguration(new Configuration(), $config); |
49
|
|
|
|
50
|
|
|
$container->setParameter('logging.application_name', $config['logging']['application_name']); |
51
|
|
|
|
52
|
|
|
$loader = new YamlFileLoader( |
53
|
|
|
$container, |
54
|
|
|
new FileLocator(__DIR__ . '/../Resources/config') |
55
|
|
|
); |
56
|
|
|
$loader->load('services.yml'); |
57
|
|
|
|
58
|
|
|
if (isset($config['loa_definition']) && $config['loa_definition']['enabled']) { |
59
|
|
|
$this->defineLoas($config['loa_definition'], $container); |
60
|
|
|
} else { |
61
|
|
|
$container->removeDefinition('surfnet_stepup.service.loa_resolution'); |
62
|
|
|
$container->removeAlias(LoaResolutionService::class); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($config['sms']['enabled'] === false) { |
66
|
|
|
$container->removeDefinition('surfnet_stepup.service.sms_second_factor'); |
67
|
|
|
$container->removeAlias(SmsSecondFactorService::class); |
68
|
|
|
$container->removeDefinition('surfnet_stepup.service.challenge_handler'); |
69
|
|
|
} else { |
70
|
|
|
$this->configureSmsSecondFactorServices($config, $container); |
71
|
|
|
if (!$config['gateway_api']['enabled'] && $config['sms']['service'] === Configuration::DEFAULT_SMS_SERVICE) { |
72
|
|
|
throw new RuntimeException( |
73
|
|
|
'The gateway API is not enabled and no replacement SMS service is configured' |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($config['gateway_api']['enabled']) { |
79
|
|
|
$this->configureGatewayApiClient($config, $container); |
80
|
|
|
} else { |
81
|
|
|
// Remove the Gateway API SMS service and its Guzzle client. |
82
|
|
|
$container->removeDefinition('surfnet_stepup.service.gateway_api_sms'); |
83
|
|
|
$container->removeDefinition('surfnet_stepup.guzzle.gateway_api'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($config['locale_cookie']['enabled']) { |
87
|
|
|
$this->configureLocaleCookieSettings($config, $container); |
88
|
|
|
} else { |
89
|
|
|
$container->removeDefinition('surfnet_stepup.locale_cookie_helper'); |
90
|
|
|
$container->removeAlias(CookieHelper::class); |
91
|
|
|
$container->removeDefinition('surfnet_stepup.locale_cookie_settings'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->configureLocaleSelectionWidget($config, $container); |
95
|
|
|
$this->configureSecondFactorTypeService($config, $container); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function defineLoas(array $loaDefinitions, ContainerBuilder $container) |
99
|
|
|
{ |
100
|
|
|
$loaService = $container->getDefinition('surfnet_stepup.service.loa_resolution'); |
101
|
|
|
|
102
|
|
|
$loa1 = new Definition('Surfnet\StepupBundle\Value\Loa', [Loa::LOA_1, $loaDefinitions['loa1']]); |
103
|
|
|
$loa2 = new Definition('Surfnet\StepupBundle\Value\Loa', [Loa::LOA_2, $loaDefinitions['loa2']]); |
104
|
|
|
$loa3 = new Definition('Surfnet\StepupBundle\Value\Loa', [Loa::LOA_3, $loaDefinitions['loa3']]); |
105
|
|
|
|
106
|
|
|
$loaService->addArgument([$loa1, $loa2, $loa3]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array $config |
111
|
|
|
* @param ContainerBuilder $container |
112
|
|
|
*/ |
113
|
|
|
private function configureLocaleCookieSettings(array $config, ContainerBuilder $container) |
114
|
|
|
{ |
115
|
|
|
$container->getDefinition('surfnet_stepup.locale_cookie_settings') |
116
|
|
|
->setArguments( |
117
|
|
|
[ |
118
|
|
|
$config['locale_cookie']['name'], |
119
|
|
|
null, |
120
|
|
|
$config['locale_cookie']['expire'], |
121
|
|
|
$config['locale_cookie']['path'], |
122
|
|
|
$config['locale_cookie']['domain'], |
123
|
|
|
$config['locale_cookie']['secure'], |
124
|
|
|
$config['locale_cookie']['http_only'], |
125
|
|
|
] |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $config |
131
|
|
|
* @param ContainerBuilder $container |
132
|
|
|
*/ |
133
|
|
|
private function configureGatewayApiClient(array $config, ContainerBuilder $container) |
134
|
|
|
{ |
135
|
|
|
$handlerStack = $container->getDefinition('surfnet_stepup.guzzle.handler_stack'); |
136
|
|
|
|
137
|
|
|
// Configure the Gateway API SMS service's Guzzle client. |
138
|
|
|
$gatewayGuzzleOptions = [ |
139
|
|
|
'base_uri' => $config['gateway_api']['url'], |
140
|
|
|
'auth' => [ |
141
|
|
|
$config['gateway_api']['credentials']['username'], |
142
|
|
|
$config['gateway_api']['credentials']['password'], |
143
|
|
|
'basic' |
144
|
|
|
], |
145
|
|
|
'headers' => [ |
146
|
|
|
'Accept' => 'application/json' |
147
|
|
|
], |
148
|
|
|
'handler' => $handlerStack, |
149
|
|
|
'http_errors' => false |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
$gatewayGuzzle = $container->getDefinition('surfnet_stepup.guzzle.gateway_api'); |
153
|
|
|
$gatewayGuzzle->replaceArgument(0, $gatewayGuzzleOptions); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param array $config |
158
|
|
|
* @param ContainerBuilder $container |
159
|
|
|
*/ |
160
|
|
|
private function configureSmsSecondFactorServices(array $config, ContainerBuilder $container) |
161
|
|
|
{ |
162
|
|
|
$smsSecondFactorService = $container->getDefinition('surfnet_stepup.service.sms_second_factor'); |
163
|
|
|
$smsSecondFactorService->replaceArgument(2, $config['sms']['originator']); |
164
|
|
|
|
165
|
|
|
$container |
166
|
|
|
->getDefinition('surfnet_stepup.service.challenge_handler') |
167
|
|
|
->replaceArgument(2, $config['sms']['otp_expiry_interval']) |
168
|
|
|
->replaceArgument(3, $config['sms']['maximum_otp_requests']); |
169
|
|
|
|
170
|
|
|
$container |
171
|
|
|
->getDefinition('surfnet_stepup.service.sms_second_factor') |
172
|
|
|
->replaceArgument(0, new Reference($config['sms']['service'])); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
private function configureLocaleSelectionWidget(array $loaDefinitions, ContainerBuilder $container) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
if ($container->hasParameter('locales')) { |
178
|
|
|
$container->getDefinition('surfnet_stepup.form.choice_list.locales') |
179
|
|
|
->replaceArgument(0, $container->getParameter('locales')); |
180
|
|
|
} else { |
181
|
|
|
$container->removeDefinition('surfnet_stepup.form.choice_list.locales'); |
182
|
|
|
$container->removeAlias(LocaleChoiceList::class); |
183
|
|
|
$container->removeDefinition('surfnet_stepup.form.switch_locale'); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
private function configureSecondFactorTypeService(array $loaDefinitions, ContainerBuilder $container) |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
if (!$container->hasParameter('enabled_generic_second_factors')) { |
190
|
|
|
$container->removeDefinition('surfnet_stepup.service.second_factor_type'); |
191
|
|
|
$container->removeAlias(SecondFactorTypeService::class); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.