|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Happyr\Auth0Bundle\Security\Factory; |
| 5 | + |
| 6 | +use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface; |
| 7 | +use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
| 8 | +use Symfony\Component\DependencyInjection\ChildDefinition; |
| 9 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 10 | +use Symfony\Component\DependencyInjection\Reference; |
| 11 | + |
| 12 | +final class Auth0LogoutFactory implements SecurityFactoryInterface |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Configures the container services required to use the authentication listener. |
| 16 | + * |
| 17 | + * @param ContainerBuilder $container |
| 18 | + * @param string $id The unique id of the firewall |
| 19 | + * @param array $config The options array for the listener |
| 20 | + * @param string $userProvider The service id of the user provider |
| 21 | + * @param string $defaultEntryPoint |
| 22 | + * |
| 23 | + * @return array containing three values: |
| 24 | + * - the provider id |
| 25 | + * - the listener id |
| 26 | + * - the entry point id |
| 27 | + */ |
| 28 | + public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint) |
| 29 | + { |
| 30 | + $listenerKeys[] = 'logout'; |
| 31 | + $listenerId = 'happyr_auth0.security.logout_listener.'.$id; |
| 32 | + $listener = $container->setDefinition($listenerId, new ChildDefinition('happyr.auth0.security.authentication.listener.logout')); |
| 33 | + $listener->replaceArgument(3, array( |
| 34 | + 'csrf_parameter' => $config['csrf_parameter'], |
| 35 | + 'csrf_token_id' => $config['csrf_token_id'], |
| 36 | + 'logout_path' => $config['path'], |
| 37 | + )); |
| 38 | + $listeners[] = new Reference($listenerId); |
| 39 | + |
| 40 | + // always use default success handler |
| 41 | + $logoutSuccessHandlerId = 'happyr_auth0.security.logout.success_handler.'.$id; |
| 42 | + $logoutSuccessHandler = $container->setDefinition($logoutSuccessHandlerId, new ChildDefinition('security.logout.success_handler')); |
| 43 | + $logoutSuccessHandler->replaceArgument(1, $config['target']); |
| 44 | + $listener->replaceArgument(2, new Reference($logoutSuccessHandlerId)); |
| 45 | + |
| 46 | + // add CSRF provider |
| 47 | + if (isset($config['csrf_token_generator'])) { |
| 48 | + $listener->addArgument(new Reference($config['csrf_token_generator'])); |
| 49 | + } |
| 50 | + |
| 51 | + // add session logout handler |
| 52 | + if (true === $config['invalidate_session']) { |
| 53 | + $listener->addMethodCall('addHandler', array(new Reference('security.logout.handler.session'))); |
| 54 | + } |
| 55 | + |
| 56 | + // add cookie logout handler |
| 57 | + if (count($config['delete_cookies']) > 0) { |
| 58 | + $cookieHandlerId = 'happyr_auth0.security.logout.handler.cookie_clearing.'.$id; |
| 59 | + $cookieHandler = $container->setDefinition($cookieHandlerId, new ChildDefinition('security.logout.handler.cookie_clearing')); |
| 60 | + $cookieHandler->addArgument($config['delete_cookies']); |
| 61 | + |
| 62 | + $listener->addMethodCall('addHandler', array(new Reference($cookieHandlerId))); |
| 63 | + } |
| 64 | + |
| 65 | + // add custom handlers |
| 66 | + foreach ($config['handlers'] as $handlerId) { |
| 67 | + $listener->addMethodCall('addHandler', array(new Reference($handlerId))); |
| 68 | + } |
| 69 | + |
| 70 | + // register with LogoutUrlGenerator |
| 71 | + $container |
| 72 | + ->getDefinition('security.logout_url_generator') |
| 73 | + ->addMethodCall('registerListener', array( |
| 74 | + $id, |
| 75 | + $config['path'], |
| 76 | + $config['csrf_token_id'], |
| 77 | + $config['csrf_parameter'], |
| 78 | + isset($config['csrf_token_generator']) ? new Reference($config['csrf_token_generator']) : null, |
| 79 | + null, // This is wrong in Symfony 4.0. We should be able to detect and pass the firewall context somehow. |
| 80 | + )) |
| 81 | + ; |
| 82 | + |
| 83 | + |
| 84 | + return [ |
| 85 | + 'happyr.auth0.security.authentication.provider.null', |
| 86 | + $listenerId, |
| 87 | + $defaultEntryPoint |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + public function addConfiguration(NodeDefinition $builder) |
| 92 | + { |
| 93 | + $builder |
| 94 | + ->treatTrueLike(array()) |
| 95 | + ->canBeUnset() |
| 96 | + ->children() |
| 97 | + ->scalarNode('csrf_parameter')->defaultValue('_csrf_token')->end() |
| 98 | + ->scalarNode('csrf_token_generator')->cannotBeEmpty()->end() |
| 99 | + ->scalarNode('csrf_token_id')->defaultValue('logout')->end() |
| 100 | + ->scalarNode('path')->defaultValue('/logout')->end() |
| 101 | + ->scalarNode('target')->defaultValue('/')->end() |
| 102 | + ->scalarNode('success_handler')->end() |
| 103 | + ->booleanNode('invalidate_session')->defaultTrue()->end() |
| 104 | + ->end() |
| 105 | + ->fixXmlConfig('delete_cookie') |
| 106 | + ->children() |
| 107 | + ->arrayNode('delete_cookies') |
| 108 | + ->beforeNormalization() |
| 109 | + ->ifTrue(function ($v) { return is_array($v) && is_int(key($v)); }) |
| 110 | + ->then(function ($v) { return array_map(function ($v) { return array('name' => $v); }, $v); }) |
| 111 | + ->end() |
| 112 | + ->useAttributeAsKey('name') |
| 113 | + ->prototype('array') |
| 114 | + ->children() |
| 115 | + ->scalarNode('path')->defaultNull()->end() |
| 116 | + ->scalarNode('domain')->defaultNull()->end() |
| 117 | + ->end() |
| 118 | + ->end() |
| 119 | + ->end() |
| 120 | + ->end() |
| 121 | + ->fixXmlConfig('handler') |
| 122 | + ->children() |
| 123 | + ->arrayNode('handlers') |
| 124 | + ->prototype('scalar')->end() |
| 125 | + ->end() |
| 126 | + ->end() |
| 127 | + ; |
| 128 | + } |
| 129 | + |
| 130 | + public function getPosition() |
| 131 | + { |
| 132 | + return 'remember_me'; |
| 133 | + } |
| 134 | + |
| 135 | + public function getKey() |
| 136 | + { |
| 137 | + return 'auth0_logout'; |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments