| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Krtv\Bundle\SingleSignOnServiceProviderBundle\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\DependencyInjection\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\DependencyInjection\DefinitionDecorator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AbstractFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /*** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class SingleSignOnFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package Krtv\Bundle\SingleSignOnServiceProviderBundle\Factory | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class SingleSignOnFactory extends AbstractFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 1 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |         $this->addOption('sso_scheme', 'http'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 1 |  |         $this->addOption('sso_host'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |         $this->addOption('sso_path', '/_sso/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 1 |  |         $this->addOption('sso_failure_path', '/login'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |         $this->addOption('sso_service', ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1 |  |         $this->addOption('sso_service_parameter', 'service'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |         $this->addOption('sso_service_extra', array()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |         $this->addOption('sso_service_extra_parameter', 'service_extra'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         $this->addOption('sso_login_required', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         $this->addOption('sso_login_required_parameter', 'login_required'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         // Host where OTP validation will be checked. Keep null for current host & scheme | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |         $this->addOption('sso_otp_scheme'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |         $this->addOption('sso_otp_host'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function getPosition() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return 'pre_auth'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function getKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return 'sso'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected function getListenerId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return 'krtv_single_sign_on_service_provider.security.authentication.listener'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param ContainerBuilder $container | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @param $id | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @param $config | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @param $userProviderId | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @param $defaultEntryPointId | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function create(ContainerBuilder $container, $id, $config, $userProviderId, $defaultEntryPointId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         list($authProviderId, $listenerId, $entryPointId) = parent::create($container, $id, $config, $userProviderId, $defaultEntryPointId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return array($authProviderId, $listenerId, $entryPointId); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @param ContainerBuilder $container | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param string $id | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @param array $config | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @param string $userProviderId | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $providerId = 'security.authentication.provider.krtv_single_sign_on_service_provider.' . $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ->setDefinition($providerId, new DefinitionDecorator('krtv_single_sign_on_service_provider.security.authentication.provider')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ->replaceArgument(0, new Reference($userProviderId)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ->replaceArgument(4, $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $providerId; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * @param ContainerBuilder $container | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $id | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @param array $config | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * @param string $defaultEntryPointId | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     protected function createEntryPoint($container, $id, $config, $defaultEntryPointId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $entryPointId = 'security.authentication.entry_point.krtv_single_sign_on_service_provider.' . $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         // add firewall id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $config['firewall_id'] = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $config = $this->getOptions($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             ->setDefinition($entryPointId, new DefinitionDecorator('krtv_single_sign_on_service_provider.security.authentication.entry_point')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             ->setDefinition('krtv_single_sign_on_service_provider.context_factory', new DefinitionDecorator('krtv_single_sign_on_service_provider.context_factory.abstract')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             ->replaceArgument(0, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         // set options to container for use by other classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $container->setParameter('krtv_single_sign_on_service_provider.options.' . $id, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return $entryPointId; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @param $container | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * @param $id | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * @param $config | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     protected function createAuthenticationFailureHandler($container, $id, $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if (isset($config['failure_handler'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             return $config['failure_handler']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $options = array_intersect_key($this->getOptions($config), $this->defaultFailureHandlerOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $id = $this->getFailureHandlerId($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $failureHandler = $container->setDefinition($id, new DefinitionDecorator('krtv_single_sign_on_service_provider.authentication.handler.authentication_failure.abstract')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $failureHandler->replaceArgument(2, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $failureHandler->addMethodCall('setUriSigner', array(new Reference('krtv_single_sign_on_service_provider.uri_signer'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         return $id; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * @param $options | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     protected function getOptions($options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         if ($options['sso_failure_path'] && strpos($options['sso_failure_path'], 'http') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $options['failure_path'] = $options['sso_failure_path']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } elseif ($options['sso_failure_path'] && $options['sso_scheme'] && $options['sso_host']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             $options['failure_path'] = sprintf('%s://%s%s', $options['sso_scheme'], $options['sso_host'], $options['sso_failure_path']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } elseif ($options['sso_failure_path']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             $options['failure_path'] = $options['sso_failure_path']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return $options; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 166 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |  |