| 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\Loa; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\Config\Definition\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Config\FileLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\DependencyInjection\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Component\DependencyInjection\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Symfony\Component\DependencyInjection\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Symfony\Component\HttpKernel\DependencyInjection\Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class SurfnetStepupExtension extends Extension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function load(array $config, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $processor = new Processor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $config = $processor->processConfiguration(new Configuration(), $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $container->setParameter('logging.application_name', $config['logging']['application_name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $loader = new YamlFileLoader( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $container, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             new FileLocator(__DIR__ . '/../Resources/config') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $loader->load('services.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (isset($config['loa_definition'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $this->defineLoas($config['loa_definition'], $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $container->removeDefinition('surfnet_stepup.service.loa_resolution'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if ($config['sms']['enabled'] === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $container->removeDefinition('surfnet_stepup.service.sms_second_factor'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $container->removeDefinition('surfnet_stepup.service.challenge_handler'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $container->removeDefinition('surfnet_stepup.service.sms_second_factor'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $this->configureSmsSecondFactorServices($config, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             if (!$config['gateway_api']['enabled'] && $config['sms']['service'] === Configuration::DEFAULT_SMS_SERVICE) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     'The gateway API is not enabled and no replacement SMS service is configured' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if ($config['gateway_api']['enabled']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->configureGatewayApiClient($config, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             // Remove the Gateway API SMS service and its Guzzle client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $container->removeDefinition('surfnet_stepup.service.gateway_api_sms'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $container->removeDefinition('surfnet_stepup.guzzle.gateway_api'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($config['locale_cookie']['enabled']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->configureLocaleCookieSettings($config, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $container->removeDefinition('surfnet_stepup.locale_cookie_helper'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $container->removeDefinition('surfnet_stepup.locale_cookie_settings'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $container->getDefinition('surfnet_stepup.form.choice_list.locales') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             ->replaceArgument(0, $container->getParameter('locales')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     private function defineLoas(array $loaDefinitions, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $loaService = $container->getDefinition('surfnet_stepup.service.loa_resolution'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $loa1 = new Definition('Surfnet\StepupBundle\Value\Loa', [Loa::LOA_1, $loaDefinitions['loa1']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $loa2 = new Definition('Surfnet\StepupBundle\Value\Loa', [Loa::LOA_2, $loaDefinitions['loa2']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $loa3 = new Definition('Surfnet\StepupBundle\Value\Loa', [Loa::LOA_3, $loaDefinitions['loa3']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $loaService->addArgument([$loa1, $loa2, $loa3]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param array            $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param ContainerBuilder $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     private function configureLocaleCookieSettings(array $config, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $container->getDefinition('surfnet_stepup.locale_cookie_settings') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ->setArguments( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     $config['locale_cookie']['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     $config['locale_cookie']['expire'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                     $config['locale_cookie']['path'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     $config['locale_cookie']['domain'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     $config['locale_cookie']['secure'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                     $config['locale_cookie']['http_only'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param array            $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param ContainerBuilder $container | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     private function configureGatewayApiClient(array $config, ContainerBuilder $container) | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         // Configure the Gateway API SMS service's Guzzle client. | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         $gatewayGuzzleOptions = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             'base_url' => $config['gateway_api']['url'], | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |             'defaults' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |                 'auth'    => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |                     $config['gateway_api']['credentials']['username'], | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |                     $config['gateway_api']['credentials']['password'], | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |                     'basic' | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |                 'headers' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |                     'Accept' => 'application/json' | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |                 ] | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         $gatewayGuzzle = $container->getDefinition('surfnet_stepup.guzzle.gateway_api'); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |         $gatewayGuzzle->replaceArgument(0, $gatewayGuzzleOptions); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param array            $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param ContainerBuilder $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     private function configureSmsSecondFactorServices(array $config, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $smsSecondFactorService = $container->getDefinition('surfnet_stepup.service.sms_second_factor'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $smsSecondFactorService->replaceArgument(2, $config['sms']['originator']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             ->getDefinition('surfnet_stepup.service.challenge_handler') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             ->replaceArgument(2, $config['sms']['otp_expiry_interval']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             ->replaceArgument(3, $config['sms']['maximum_otp_requests']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             ->getDefinition('surfnet_stepup.service.sms_second_factor') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             ->replaceArgument(0, new Reference($config['sms']['service'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 159 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |  | 
            
                        
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.