vendor/hwi/oauth-bundle/src/DependencyInjection/HWIOAuthExtension.php line 147

  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 Symfony\Component\DependencyInjection\Loader\Configurator;
  11. // BC symfony 4.4
  12. class_exists(ContainerConfigurator::class);
  13. if (!\function_exists(__NAMESPACE__.'\\service')) {
  14.     function service($class): ReferenceConfigurator
  15.     {
  16.         /* @phpstan-ignore-next-line function ref not found */
  17.         return ref($class);
  18.     }
  19. }
  20. namespace HWI\Bundle\OAuthBundle\DependencyInjection;
  21. use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
  22. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  23. use Symfony\Component\Config\Definition\Processor;
  24. use Symfony\Component\Config\FileLocator;
  25. use Symfony\Component\DependencyInjection\Alias;
  26. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  27. use Symfony\Component\DependencyInjection\ContainerBuilder;
  28. use Symfony\Component\DependencyInjection\Definition;
  29. use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
  30. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  31. use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
  32. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  33. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  34. use Symfony\Component\DependencyInjection\Reference;
  35. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  36. /**
  37.  * @author Geoffrey Bachelet <geoffrey.bachelet@gmail.com>
  38.  * @author Alexander <iam.asm89@gmail.com>
  39.  * @author Joseph Bielawski <stloyd@gmail.com>
  40.  */
  41. final class HWIOAuthExtension extends Extension
  42. {
  43.     /**
  44.      * @var \ArrayIterator<string, true>
  45.      */
  46.     private \ArrayIterator $firewallNames;
  47.     private bool $refreshTokenListenerEnabled false;
  48.     public function __construct()
  49.     {
  50.         $this->firewallNames = new \ArrayIterator();
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      *
  55.      * @throws \Exception
  56.      * @throws \RuntimeException
  57.      * @throws InvalidConfigurationException
  58.      * @throws BadMethodCallException
  59.      * @throws InvalidArgumentException
  60.      * @throws OutOfBoundsException
  61.      * @throws ServiceNotFoundException
  62.      */
  63.     public function load(array $configsContainerBuilder $container): void
  64.     {
  65.         $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/'));
  66.         $loader->load('controller.php');
  67.         $loader->load('oauth.php');
  68.         $loader->load('resource_owners.php');
  69.         $loader->load('templating.php');
  70.         $loader->load('twig.php');
  71.         $loader->load('util.php');
  72.         $processor = new Processor();
  73.         $config $processor->processConfiguration(new Configuration(), $configs);
  74.         // set target path parameter
  75.         $container->setParameter('hwi_oauth.target_path_parameter'$config['target_path_parameter']);
  76.         // set target path domains whitelist parameter
  77.         $container->setParameter('hwi_oauth.target_path_domains_whitelist'$config['target_path_domains_whitelist']);
  78.         // set use referer parameter
  79.         $container->setParameter('hwi_oauth.use_referer'$config['use_referer']);
  80.         // set failed use referer parameter
  81.         $container->setParameter('hwi_oauth.failed_use_referer'$config['failed_use_referer']);
  82.         // set failed auth path
  83.         $container->setParameter('hwi_oauth.failed_auth_path'$config['failed_auth_path']);
  84.         // set grant rule
  85.         $container->setParameter('hwi_oauth.grant_rule'$config['grant_rule']);
  86.         // setup services for all configured resource owners
  87.         $resourceOwners = [];
  88.         $resourceOwnerReferenceMap = [];
  89.         foreach ($config['resource_owners'] as $name => $options) {
  90.             $resourceOwners[$name] = $name;
  91.             $resourceOwnerReferenceMap[$name] = $this->createResourceOwnerService($container$name$options);
  92.             if (!$this->refreshTokenListenerEnabled) {
  93.                 $this->refreshTokenListenerEnabled $options['options']['refresh_on_expire'] ?? false;
  94.             }
  95.         }
  96.         $container->setParameter('hwi_oauth.resource_owners'$resourceOwners);
  97.         $container->setAlias(
  98.             'hwi_oauth.resource_owners.locator',
  99.             (string) ServiceLocatorTagPass::register($container$resourceOwnerReferenceMap)
  100.         );
  101.         $this->createConnectIntegration($container$config);
  102.     }
  103.     /**
  104.      * Creates a resource owner service.
  105.      *
  106.      * @param ContainerBuilder $container The container builder
  107.      * @param string           $name      The name of the service
  108.      * @param array            $options   Additional options of the service
  109.      *
  110.      * @throws InvalidConfigurationException
  111.      * @throws BadMethodCallException
  112.      * @throws InvalidArgumentException
  113.      */
  114.     public function createResourceOwnerService(ContainerBuilder $containerstring $name, array $options): Reference
  115.     {
  116.         // alias services
  117.         if (isset($options['service'])) {
  118.             return new Reference($options['service']);
  119.         }
  120.         $type $options['type'];
  121.         unset($options['type']);
  122.         // handle external resource owners with given class
  123.         if (isset($options['class'])) {
  124.             if (!is_subclass_of($options['class'], ResourceOwnerInterface::class)) {
  125.                 throw new InvalidConfigurationException(sprintf('Class "%s" must implement interface "HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface".'$options['class']));
  126.             }
  127.             $definition = new Definition($options['class']);
  128.             unset($options['class']);
  129.         } else {
  130.             $definition = new Definition("%hwi_oauth.resource_owner.$type.class%");
  131.         }
  132.         $definition->setArgument('$httpClient', new Reference('hwi_oauth.http_client'));
  133.         $definition->setArgument('$httpUtils', new Reference('security.http_utils'));
  134.         $definition->setArgument('$options'$options);
  135.         $definition->setArgument('$name'$name);
  136.         $definition->setArgument('$storage', new Reference('hwi_oauth.storage.session'));
  137.         $container->setDefinition('hwi_oauth.resource_owner.'.$name$definition);
  138.         return new Reference('hwi_oauth.resource_owner.'.$name);
  139.     }
  140.     /**
  141.      * {@inheritdoc}
  142.      */
  143.     public function getAlias(): string
  144.     {
  145.         return 'hwi_oauth';
  146.     }
  147.     public function getFirewallNames(): \ArrayIterator
  148.     {
  149.         return $this->firewallNames;
  150.     }
  151.     public function isRefreshTokenListenerEnabled(): bool
  152.     {
  153.         return $this->refreshTokenListenerEnabled;
  154.     }
  155.     /**
  156.      * Check of the connect controllers etc should be enabled.
  157.      *
  158.      * @throws BadMethodCallException
  159.      * @throws InvalidArgumentException
  160.      */
  161.     private function createConnectIntegration(ContainerBuilder $container, array $config): void
  162.     {
  163.         $container->setParameter('hwi_oauth.connect', isset($config['connect']));
  164.         $container->setParameter('hwi_oauth.connect.confirmation'$config['connect']['confirmation'] ?? false);
  165.         $container->setParameter('hwi_oauth.connect.registration_form'$config['connect']['registration_form'] ?? null);
  166.         if (isset($config['connect']['account_connector'])) {
  167.             $container->setAlias('hwi_oauth.account.connector', new Alias($config['connect']['account_connector'], true));
  168.         }
  169.         if (isset($config['connect']['registration_form_handler'])) {
  170.             $container->setAlias('hwi_oauth.registration.form.handler', new Alias($config['connect']['registration_form_handler'], true));
  171.         }
  172.     }
  173. }