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 Surfnet\StepupBundle\Value\AuthnContextClass; |
22
|
|
|
use Surfnet\StepupBundle\Value\Loa; |
23
|
|
|
use Symfony\Component\Config\Definition\Processor; |
24
|
|
|
use Symfony\Component\Config\FileLocator; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
26
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
27
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
28
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
29
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
30
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
31
|
|
|
|
32
|
|
|
class SurfnetStepupExtension extends Extension |
33
|
|
|
{ |
34
|
|
|
public function load(array $config, ContainerBuilder $container) |
35
|
|
|
{ |
36
|
|
|
$processor = new Processor(); |
37
|
|
|
$config = $processor->processConfiguration(new Configuration(), $config); |
38
|
|
|
|
39
|
|
|
$container->setParameter('logging.application_name', $config['logging']['application_name']); |
40
|
|
|
|
41
|
|
|
$loader = new YamlFileLoader( |
42
|
|
|
$container, |
43
|
|
|
new FileLocator(__DIR__ . '/../Resources/config') |
44
|
|
|
); |
45
|
|
|
$loader->load('services.yml'); |
46
|
|
|
|
47
|
|
|
if (isset($config['loa_definition'])) { |
48
|
|
|
$this->defineLoas($config['loa_definition'], $container); |
49
|
|
|
} else { |
50
|
|
|
$container->removeDefinition('surfnet_stepup.service.loa_resolution'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($config['sms']['enabled'] === false) { |
54
|
|
|
$container->removeDefinition('surfnet_stepup.service.sms_second_factor'); |
55
|
|
|
$container->removeDefinition('surfnet_stepup.service.challenge_handler'); |
56
|
|
|
$container->removeDefinition('surfnet_stepup.service.sms_second_factor'); |
57
|
|
|
} else { |
58
|
|
|
$smsSecondFactorService = $container->getDefinition('surfnet_stepup.service.sms_second_factor'); |
59
|
|
|
$smsSecondFactorService->replaceArgument(2, $config['sms']['originator']); |
60
|
|
|
|
61
|
|
|
$container |
62
|
|
|
->getDefinition('surfnet_stepup.service.challenge_handler') |
63
|
|
|
->replaceArgument(2, $config['sms']['otp_expiry_interval']) |
64
|
|
|
->replaceArgument(3, $config['sms']['maximum_otp_requests']); |
65
|
|
|
|
66
|
|
|
$container |
67
|
|
|
->getDefinition('surfnet_stepup.service.sms_second_factor') |
68
|
|
|
->replaceArgument(0, new Reference($config['sms']['service'])); |
69
|
|
|
|
70
|
|
|
if (!$config['gateway_api']['enabled'] && $config['sms']['service'] === Configuration::DEFAULT_SMS_SERVICE) { |
|
|
|
|
71
|
|
|
throw new RuntimeException('The gateway API is not enabled and no replacement SMS service is configured'); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($config['gateway_api']['enabled']) { |
76
|
|
|
# Configure the Gateway API SMS service's Guzzle client. |
|
|
|
|
77
|
|
|
$gatewayGuzzleOptions = [ |
78
|
|
|
'base_url' => $config['gateway_api']['url'], |
79
|
|
|
'defaults' => [ |
80
|
|
|
'auth' => [ |
81
|
|
|
$config['gateway_api']['credentials']['username'], |
82
|
|
|
$config['gateway_api']['credentials']['password'], |
83
|
|
|
'basic' |
84
|
|
|
], |
85
|
|
|
'headers' => [ |
86
|
|
|
'Accept' => 'application/json' |
87
|
|
|
] |
88
|
|
|
] |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
$gatewayGuzzle = $container->getDefinition('surfnet_stepup.guzzle.gateway_api'); |
92
|
|
|
$gatewayGuzzle->replaceArgument(0, $gatewayGuzzleOptions); |
93
|
|
|
} else { |
94
|
|
|
# Remove the Gateway API SMS service and its Guzzle client. |
|
|
|
|
95
|
|
|
$container->removeDefinition('surfnet_stepup.service.gateway_api_sms'); |
96
|
|
|
$container->removeDefinition('surfnet_stepup.guzzle.gateway_api'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$container->getDefinition('surfnet_stepup.form.choice_list.locales') |
100
|
|
|
->replaceArgument(0, $container->getParameter('locales')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function defineLoas(array $loaDefinitions, ContainerBuilder $container) |
104
|
|
|
{ |
105
|
|
|
$loaService = $container->getDefinition('surfnet_stepup.service.loa_resolution'); |
106
|
|
|
|
107
|
|
|
$arguments = []; |
108
|
|
|
foreach ($loaDefinitions as $level => $authnContextClassDefinitions) { |
109
|
|
|
if (!is_numeric($level)) { |
110
|
|
|
throw new \InvalidArgumentException( |
111
|
|
|
sprintf( |
112
|
|
|
'Config.yml: "%s" is not a valid level', |
113
|
|
|
$level |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$level = (int) $level; |
119
|
|
|
|
120
|
|
|
if (!in_array($level, Loa::getLevels())) { |
121
|
|
|
throw new \InvalidArgumentException( |
122
|
|
|
sprintf( |
123
|
|
|
'Config.yml: "%s" is not a valid loa level', |
124
|
|
|
$level |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$authnContextClasses = []; |
130
|
|
|
foreach ($authnContextClassDefinitions as $definition) { |
131
|
|
|
if (!in_array($definition['type'], AuthnContextClass::getTypes())) { |
132
|
|
|
throw new \InvalidArgumentException( |
133
|
|
|
sprintf( |
134
|
|
|
'Config.yml: "%s" is not a valid AuthnContextClass type', |
135
|
|
|
$definition['type'] |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
$authnContextClasses[] = new Definition( |
140
|
|
|
'Surfnet\StepupBundle\Value\AuthnContextClass', |
141
|
|
|
[ $definition['id'], $definition['type'] ] |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
$arguments[] = new Definition( |
145
|
|
|
'Surfnet\StepupBundle\Value\Loa', |
146
|
|
|
[ $level, $authnContextClasses ] |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$loaService->addArgument($arguments); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.