Passed
Pull Request — develop (#295)
by Peter
04:30
created

SurfnetStepupGatewaySamlStepupProviderExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 106
dl 0
loc 216
rs 10
c 2
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createMetadataDefinition() 0 30 1
A createHostedDefinitions() 0 20 1
A buildHostedEntityDefinition() 0 25 1
A createRouteConfig() 0 5 1
A createRemoteDefinition() 0 12 1
A load() 0 19 3
A loadProviderConfiguration() 0 61 2
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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupGateway\SamlStepupProviderBundle\DependencyInjection;
20
21
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
22
use Symfony\Component\Config\FileLocator;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Definition;
25
use Symfony\Component\DependencyInjection\Loader;
26
use Symfony\Component\DependencyInjection\Reference;
27
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
28
29
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
 * @SuppressWarnings(PHPMD.LongClassName)
31
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
32
class SurfnetStepupGatewaySamlStepupProviderExtension extends Extension
33
{
34
    const VIEW_CONFIG_TAG_NAME = 'gssp.view_config';
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $configs should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $container should have a doc-comment as per coding-style.
Loading history...
37
     * {@inheritdoc}
38
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
39
    public function load(array $configs, ContainerBuilder $container)
40
    {
41
        $configuration = new Configuration();
42
        $config = $this->processConfiguration($configuration, $configs);
43
44
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
45
        $loader->load('services.yml');
46
47
        $connectedServiceProviders = $container->getDefinition('gssp.allowed_sps');
48
        $connectedServiceProviders->replaceArgument(0, $config['allowed_sps']);
49
50
        foreach ($config['providers'] as $provider => $providerConfiguration) {
51
            // may seem a bit strange, but this prevents casing issue when getting/setting/creating provider
52
            // service definitions etc.
53
            if ($provider !== strtolower($provider)) {
54
                throw new InvalidConfigurationException('The provider name must be completely lowercase');
55
            }
56
57
            $this->loadProviderConfiguration($provider, $providerConfiguration, $config['routes'], $container);
58
        }
59
    }
60
61
    private function loadProviderConfiguration(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function loadProviderConfiguration()
Loading history...
Coding Style introduced by
Private method name "SurfnetStepupGatewaySamlStepupProviderExtension::loadProviderConfiguration" must be prefixed with an underscore
Loading history...
62
        $provider,
63
        array $configuration,
64
        array $routes,
65
        ContainerBuilder $container
66
    ) {
67
        if ($container->has('gssp.provider.' . $provider)) {
68
            throw new InvalidConfigurationException(sprintf('Cannot create the same provider "%s" twice', $provider));
69
        }
70
71
        $this->createHostedDefinitions($provider, $configuration['hosted'], $routes, $container);
72
        $this->createMetadataDefinition($provider, $configuration['hosted'], $routes, $container);
73
        $this->createRemoteDefinition($provider, $configuration['remote'], $container);
74
75
        $stateHandlerDefinition = new Definition('Surfnet\StepupGateway\SamlStepupProviderBundle\Saml\StateHandler', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
76
            new Reference('gssp.session'),
77
            $provider
78
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
79
        $container->setDefinition('gssp.provider.' . $provider . '.statehandler', $stateHandlerDefinition);
80
81
        $providerDefinition = new Definition('Surfnet\StepupGateway\SamlStepupProviderBundle\Provider\Provider', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
82
            $provider,
83
            new Reference('gssp.provider.' . $provider . '.hosted.idp'),
84
            new Reference('gssp.provider.' . $provider . '.hosted.sp'),
85
            new Reference('gssp.provider.' . $provider . '.remote.idp'),
86
            new Reference('gssp.provider.' . $provider . '.statehandler')
87
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
88
89
        $providerDefinition->setPublic(false);
90
        $container->setDefinition('gssp.provider.' . $provider, $providerDefinition);
91
92
        $assertionSigningService = new Definition('Surfnet\StepupGateway\GatewayBundle\Saml\AssertionSigningService', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
93
            new Reference('gssp.provider.' . $provider . '.hosted.idp')
94
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
95
        $assertionSigningService->setPublic('false');
0 ignored issues
show
Bug introduced by
'false' of type string is incompatible with the type boolean expected by parameter $boolean of Symfony\Component\Depend...Definition::setPublic(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        $assertionSigningService->setPublic(/** @scrutinizer ignore-type */ 'false');
Loading history...
96
        $container->setDefinition('gssp.provider.' . $provider . '.assertion_signing', $assertionSigningService);
97
98
        $proxyResponseFactory = new Definition(
99
            'Surfnet\StepupGateway\SamlStepupProviderBundle\Saml\ProxyResponseFactory',
100
            [
101
                new Reference('logger'),
102
                new Reference('gssp.provider.' . $provider . '.hosted.idp'),
103
                new Reference('gssp.provider.' . $provider . '.statehandler'),
104
                new Reference('gssp.provider.' . $provider . '.assertion_signing')
105
            ]
106
        );
107
        $proxyResponseFactory->setPublic(true);
108
        $container->setDefinition('gssp.provider.' . $provider . '.response_proxy', $proxyResponseFactory);
109
110
        $container
111
            ->getDefinition('gssp.provider_repository')
112
            ->addMethodCall('addProvider', [new Reference('gssp.provider.' . $provider)]);
113
114
        $viewConfigDefinition = new Definition('Surfnet\StepupGateway\SamlStepupProviderBundle\Provider\ViewConfig', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
115
            new Reference('request_stack'),
116
            $configuration['view_config']['logo'],
117
            $configuration['view_config']['title'],
118
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
119
        $viewConfigDefinition->addTag(self::VIEW_CONFIG_TAG_NAME);
120
121
        $container->setDefinition('gssp.view_config.' . $provider, $viewConfigDefinition);
122
    }
123
124
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
125
     * @param string           $provider
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
126
     * @param array            $configuration
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
127
     * @param array            $routes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
128
     * @param ContainerBuilder $container
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
130
    private function createHostedDefinitions(
0 ignored issues
show
Coding Style introduced by
Private method name "SurfnetStepupGatewaySamlStepupProviderExtension::createHostedDefinitions" must be prefixed with an underscore
Loading history...
131
        $provider,
132
        array $configuration,
133
        array $routes,
134
        ContainerBuilder $container
135
    ) {
136
        $hostedDefinition = $this->buildHostedEntityDefinition($provider, $configuration, $routes);
137
        $container->setDefinition('gssp.provider.' . $provider . '.hosted_entities', $hostedDefinition);
138
139
        $hostedSpDefinition  = (new Definition())
140
            ->setClass('Surfnet\SamlBundle\Entity\ServiceProvider')
141
            ->setFactory([new Reference('gssp.provider.' . $provider . '.hosted_entities'), 'getServiceProvider'])
142
            ->setPublic(false);
143
        $container->setDefinition('gssp.provider.' . $provider . '.hosted.sp', $hostedSpDefinition);
144
145
        $hostedIdPDefinition = (new Definition())
146
            ->setClass('Surfnet\SamlBundle\Entity\IdentityProvider')
147
            ->setFactory([new Reference('gssp.provider.' . $provider . '.hosted_entities'), 'getIdentityProvider'])
148
            ->setPublic(false);
149
        $container->setDefinition('gssp.provider.' . $provider . '.hosted.idp', $hostedIdPDefinition);
150
    }
151
152
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
153
     * @param string $provider
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
154
     * @param array  $configuration
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
155
     * @param array  $routes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
156
     * @return Definition
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
157
     */
158
    private function buildHostedEntityDefinition($provider, array $configuration, array $routes)
0 ignored issues
show
Coding Style introduced by
Private method name "SurfnetStepupGatewaySamlStepupProviderExtension::buildHostedEntityDefinition" must be prefixed with an underscore
Loading history...
159
    {
160
        $entityId = ['entity_id_route' => $this->createRouteConfig($provider, $routes['metadata'])];
161
        $spAdditional = [
162
            'enabled' => true,
163
            'assertion_consumer_route' => $this->createRouteConfig($provider, $routes['consume_assertion'])
164
        ];
165
        $idpAdditional = [
166
            'enabled' => true,
167
            'sso_route' => $this->createRouteConfig($provider, $routes['sso'])
168
        ];
169
170
        $serviceProvider  = array_merge($configuration['service_provider'], $spAdditional, $entityId);
171
        $identityProvider = array_merge($configuration['identity_provider'], $idpAdditional, $entityId);
172
173
        $hostedDefinition = new Definition('Surfnet\SamlBundle\Entity\HostedEntities', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
174
            new Reference('router'),
175
            new Reference('request_stack'),
176
            $serviceProvider,
177
            $identityProvider
178
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
179
180
        $hostedDefinition->setPublic(false);
181
182
        return $hostedDefinition;
183
    }
184
185
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
186
     * @param string           $provider
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
187
     * @param array            $configuration
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
188
     * @param ContainerBuilder $container
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
189
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
190
    private function createRemoteDefinition($provider, array $configuration, ContainerBuilder $container)
0 ignored issues
show
Coding Style introduced by
Private method name "SurfnetStepupGatewaySamlStepupProviderExtension::createRemoteDefinition" must be prefixed with an underscore
Loading history...
191
    {
192
        $definition    = new Definition('Surfnet\SamlBundle\Entity\IdentityProvider', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
193
            [
194
                'entityId'        => $configuration['entity_id'],
195
                'ssoUrl'          => $configuration['sso_url'],
196
                'certificateData' => $configuration['certificate'],
197
            ]
198
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
199
200
        $definition->setPublic(false);
201
        $container->setDefinition('gssp.provider.' . $provider . '.remote.idp', $definition);
202
    }
203
204
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
205
     * @param string           $provider
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
206
     * @param array            $configuration
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
207
     * @param array            $routes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
208
     * @param ContainerBuilder $container
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
209
     * @return Definition
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
210
     */
211
    private function createMetadataDefinition(
0 ignored issues
show
Coding Style introduced by
Private method name "SurfnetStepupGatewaySamlStepupProviderExtension::createMetadataDefinition" must be prefixed with an underscore
Loading history...
212
        $provider,
213
        array $configuration,
214
        array $routes,
215
        ContainerBuilder $container
216
    ) {
217
        $metadataConfiguration = new Definition('Surfnet\SamlBundle\Metadata\MetadataConfiguration');
218
219
        $propertyMap = [
220
            'entityIdRoute'          => $this->createRouteConfig($provider, $routes['metadata']),
221
            'isSp'                   => true,
222
            'assertionConsumerRoute' => $this->createRouteConfig($provider, $routes['consume_assertion']),
223
            'isIdP'                  => true,
224
            'ssoRoute'               => $this->createRouteConfig($provider, $routes['sso']),
225
            'publicKey'              => $configuration['metadata']['public_key'],
226
            'privateKey'             => $configuration['metadata']['private_key'],
227
        ];
228
229
        $metadataConfiguration->setProperties($propertyMap);
230
        $metadataConfiguration->setPublic(false);
231
        $container->setDefinition('gssp.provider.' . $provider . 'metadata.configuration', $metadataConfiguration);
232
233
        $metadataFactory = new Definition('Surfnet\SamlBundle\Metadata\MetadataFactory', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
234
            new Reference('twig'),
235
            new Reference('router'),
236
            new Reference('surfnet_saml.signing_service'),
237
            new Reference('gssp.provider.' . $provider . 'metadata.configuration')
238
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
239
        $metadataFactory->setPublic(true);
240
        $container->setDefinition('gssp.provider.' . $provider . '.metadata.factory', $metadataFactory);
241
    }
242
243
    private function createRouteConfig($provider, $routeName)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createRouteConfig()
Loading history...
Coding Style introduced by
Private method name "SurfnetStepupGatewaySamlStepupProviderExtension::createRouteConfig" must be prefixed with an underscore
Loading history...
244
    {
245
        return [
246
            'route'      => $routeName,
247
            'parameters' => ['provider' => $provider]
248
        ];
249
    }
250
}
251