vendor/hwi/oauth-bundle/Security/Http/ResourceOwnerMap.php line 84

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\Security\Http;
  11. use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
  12. use HWI\Bundle\OAuthBundle\OAuth\State\State;
  13. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Security\Http\HttpUtils;
  17. /**
  18.  * ResourceOwnerMap. Holds several resource owners for a firewall. Lazy
  19.  * loads the appropriate resource owner when requested.
  20.  *
  21.  * @author Alexander <iam.asm89@gmail.com>
  22.  *
  23.  * @final since 1.4
  24.  */
  25. class ResourceOwnerMap implements ContainerAwareInterfaceResourceOwnerMapInterface
  26. {
  27.     /**
  28.      * @var HttpUtils
  29.      */
  30.     protected $httpUtils;
  31.     /**
  32.      * @var array
  33.      */
  34.     protected $resourceOwners;
  35.     /**
  36.      * @var array
  37.      */
  38.     protected $possibleResourceOwners;
  39.     /**
  40.      * @var ContainerInterface
  41.      */
  42.     protected $container;
  43.     /**
  44.      * Constructor.
  45.      *
  46.      * @param HttpUtils $httpUtils              HttpUtils
  47.      * @param array     $possibleResourceOwners array with possible resource owners names
  48.      * @param array     $resourceOwners         array with configured resource owners
  49.      */
  50.     public function __construct(HttpUtils $httpUtils, array $possibleResourceOwners$resourceOwners)
  51.     {
  52.         $this->httpUtils $httpUtils;
  53.         $this->possibleResourceOwners $possibleResourceOwners;
  54.         $this->resourceOwners $resourceOwners;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function setContainer(ContainerInterface $container null)
  60.     {
  61.         $this->container $container;
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function hasResourceOwnerByName($name)
  67.     {
  68.         return isset($this->resourceOwners[$name], $this->possibleResourceOwners[$name]);
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function getResourceOwnerByName($name)
  74.     {
  75.         if (!$this->hasResourceOwnerByName($name)) {
  76.             return null;
  77.         }
  78.         /** @var ResourceOwnerInterface $resourceOwner */
  79.         $resourceOwner $this->container->get('hwi_oauth.resource_owner.'.$name);
  80.         return $resourceOwner;
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function getResourceOwnerByRequest(Request $request)
  86.     {
  87.         foreach ($this->resourceOwners as $name => $checkPath) {
  88.             if ($this->httpUtils->checkRequestPath($request$checkPath)) {
  89.                 $resourceOwner $this->getResourceOwnerByName($name);
  90.                 // save the round-tripped state to the resource owner
  91.                 if (null !== $resourceOwner) {
  92.                     $resourceOwner->storeState(new State($request->get('state'), false));
  93.                 }
  94.                 return [$resourceOwner$checkPath];
  95.             }
  96.         }
  97.         return null;
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function getResourceOwnerCheckPath($name)
  103.     {
  104.         if (isset($this->resourceOwners[$name])) {
  105.             return $this->resourceOwners[$name];
  106.         }
  107.         return null;
  108.     }
  109.     /**
  110.      * {@inheritdoc}
  111.      */
  112.     public function getResourceOwners()
  113.     {
  114.         return $this->resourceOwners;
  115.     }
  116. }