vendor/hwi/oauth-bundle/DependencyInjection/HWIOAuthExtension.php line 126

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the HWIOAuthBundle package.
  4.  *
  5.  * (c) Hardware Info <opensource@hardware.info>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace HWI\Bundle\OAuthBundle\DependencyInjection;
  11. use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
  12. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  13. use Symfony\Component\Config\Definition\Processor;
  14. use Symfony\Component\Config\FileLocator;
  15. use Symfony\Component\DependencyInjection\Alias;
  16. use Symfony\Component\DependencyInjection\ChildDefinition;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  19. use Symfony\Component\DependencyInjection\Reference;
  20. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  21. /**
  22.  * @author Geoffrey Bachelet <geoffrey.bachelet@gmail.com>
  23.  * @author Alexander <iam.asm89@gmail.com>
  24.  * @author Joseph Bielawski <stloyd@gmail.com>
  25.  */
  26. final class HWIOAuthExtension extends Extension
  27. {
  28.     /**
  29.      * {@inheritdoc}
  30.      *
  31.      * @throws \Exception
  32.      * @throws \RuntimeException
  33.      * @throws InvalidConfigurationException
  34.      * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
  35.      * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  36.      * @throws \Symfony\Component\DependencyInjection\Exception\OutOfBoundsException
  37.      * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
  38.      */
  39.     public function load(array $configsContainerBuilder $container)
  40.     {
  41.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/'));
  42.         $loader->load('controller.xml');
  43.         $loader->load('http_client.xml');
  44.         $loader->load('oauth.xml');
  45.         $loader->load('templating.xml');
  46.         $loader->load('twig.xml');
  47.         $loader->load('util.xml');
  48.         $processor = new Processor();
  49.         $config $processor->processConfiguration(new Configuration(), $configs);
  50.         $this->createHttplugClient($container$config);
  51.         // set current firewall
  52.         if (empty($config['firewall_names'])) {
  53.             throw new InvalidConfigurationException('The child node "firewall_names" at path "hwi_oauth" must be configured.');
  54.         }
  55.         $container->setParameter('hwi_oauth.firewall_names'$config['firewall_names']);
  56.         // set target path parameter
  57.         $container->setParameter('hwi_oauth.target_path_parameter'$config['target_path_parameter']);
  58.         // set target path domains whitelist parameter
  59.         $container->setParameter('hwi_oauth.target_path_domains_whitelist'$config['target_path_domains_whitelist']);
  60.         // set use referer parameter
  61.         $container->setParameter('hwi_oauth.use_referer'$config['use_referer']);
  62.         // set failed use referer parameter
  63.         $container->setParameter('hwi_oauth.failed_use_referer'$config['failed_use_referer']);
  64.         // set failed auth path
  65.         $container->setParameter('hwi_oauth.failed_auth_path'$config['failed_auth_path']);
  66.         // set grant rule
  67.         $container->setParameter('hwi_oauth.grant_rule'$config['grant_rule']);
  68.         // setup services for all configured resource owners
  69.         $resourceOwners = [];
  70.         foreach ($config['resource_owners'] as $name => $options) {
  71.             $resourceOwners[$name] = $name;
  72.             $this->createResourceOwnerService($container$name$options);
  73.         }
  74.         $container->setParameter('hwi_oauth.resource_owners'$resourceOwners);
  75.         $oauthUtils $container->getDefinition('hwi_oauth.security.oauth_utils');
  76.         foreach ($config['firewall_names'] as $firewallName) {
  77.             $oauthUtils->addMethodCall('addResourceOwnerMap', [new Reference('hwi_oauth.resource_ownermap.'.$firewallName)]);
  78.         }
  79.         $this->createConnectIntegration($container$config);
  80.         $container->setAlias('hwi_oauth.user_checker', new Alias('security.user_checker'true));
  81.     }
  82.     /**
  83.      * Creates a resource owner service.
  84.      *
  85.      * @param ContainerBuilder $container The container builder
  86.      * @param string           $name      The name of the service
  87.      * @param array            $options   Additional options of the service
  88.      *
  89.      * @throws InvalidConfigurationException
  90.      * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
  91.      * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  92.      */
  93.     public function createResourceOwnerService(ContainerBuilder $container$name, array $options)
  94.     {
  95.         // alias services
  96.         if (isset($options['service'])) {
  97.             // set the appropriate name for aliased services, compiler pass depends on it
  98.             $container->setAlias('hwi_oauth.resource_owner.'.$name, new Alias($options['service'], true));
  99.             return;
  100.         }
  101.         $type $options['type'];
  102.         unset($options['type']);
  103.         // handle external resource owners with given class
  104.         if (isset($options['class'])) {
  105.             if (!is_subclass_of($options['class'], ResourceOwnerInterface::class)) {
  106.                 throw new InvalidConfigurationException(sprintf('Class "%s" must implement interface "HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface".'$options['class']));
  107.             }
  108.             $definition = new ChildDefinition('hwi_oauth.abstract_resource_owner.'.$type);
  109.             $definition->setClass($options['class']);
  110.             unset($options['class']);
  111.         } else {
  112.             $definition = new ChildDefinition('hwi_oauth.abstract_resource_owner.'.Configuration::getResourceOwnerType($type));
  113.             $definition->setClass("%hwi_oauth.resource_owner.$type.class%");
  114.         }
  115.         $definition->replaceArgument(2$options);
  116.         $definition->replaceArgument(3$name);
  117.         $definition->setPublic(true);
  118.         $container->setDefinition('hwi_oauth.resource_owner.'.$name$definition);
  119.     }
  120.     public function getAlias(): string
  121.     {
  122.         return 'hwi_oauth';
  123.     }
  124.     protected function createHttplugClient(ContainerBuilder $container, array $config)
  125.     {
  126.         $httpClientId $config['http']['client'];
  127.         $httpMessageFactoryId $config['http']['message_factory'];
  128.         $bundles $container->getParameter('kernel.bundles');
  129.         if ('httplug.client.default' === $httpClientId && !isset($bundles['HttplugBundle'])) {
  130.             throw new InvalidConfigurationException('You must setup php-http/httplug-bundle to use the default http client service.');
  131.         }
  132.         if ('httplug.message_factory.default' === $httpMessageFactoryId && !isset($bundles['HttplugBundle'])) {
  133.             throw new InvalidConfigurationException('You must setup php-http/httplug-bundle to use the default http message factory service.');
  134.         }
  135.         $container->setAlias('hwi_oauth.http.client', new Alias($config['http']['client'], true));
  136.         $container->setAlias('hwi_oauth.http.message_factory', new Alias($config['http']['message_factory'], true));
  137.     }
  138.     /**
  139.      * Check of the connect controllers etc should be enabled.
  140.      *
  141.      * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
  142.      * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  143.      */
  144.     private function createConnectIntegration(ContainerBuilder $container, array $config)
  145.     {
  146.         $container->setParameter('hwi_oauth.connect.confirmation'false);
  147.         if (isset($config['connect'])) {
  148.             $container->setParameter('hwi_oauth.connect'true);
  149.             if (isset($config['fosub'])) {
  150.                 $container->setParameter('hwi_oauth.fosub_enabled'true);
  151.                 $definition $container->setDefinition('hwi_oauth.user.provider.fosub_bridge', new ChildDefinition('hwi_oauth.user.provider.fosub_bridge.def'));
  152.                 $definition->addArgument($config['fosub']['properties']);
  153.                 // setup fosub bridge services
  154.                 $container->setAlias('hwi_oauth.account.connector', new Alias('hwi_oauth.user.provider.fosub_bridge'true));
  155.                 $definition $container->setDefinition('hwi_oauth.registration.form.handler.fosub_bridge', new ChildDefinition('hwi_oauth.registration.form.handler.fosub_bridge.def'));
  156.                 $definition->addArgument($config['fosub']['username_iterations']);
  157.                 $container->setAlias('hwi_oauth.registration.form.handler', new Alias('hwi_oauth.registration.form.handler.fosub_bridge'true));
  158.                 $container->setAlias('hwi_oauth.registration.form.factory', new Alias('fos_user.registration.form.factory'true));
  159.             } else {
  160.                 $container->setParameter('hwi_oauth.fosub_enabled'false);
  161.             }
  162.             foreach ($config['connect'] as $key => $serviceId) {
  163.                 if ('confirmation' === $key) {
  164.                     $container->setParameter('hwi_oauth.connect.confirmation'$config['connect']['confirmation']);
  165.                     continue;
  166.                 }
  167.                 $container->setAlias('hwi_oauth.'.str_replace('_''.'$key), new Alias($serviceIdtrue));
  168.             }
  169.         } else {
  170.             $container->setParameter('hwi_oauth.fosub_enabled'false);
  171.             $container->setParameter('hwi_oauth.connect'false);
  172.         }
  173.     }
  174. }