vendor/hwi/oauth-bundle/Security/Http/Firewall/OAuthListener.php line 28

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\Firewall;
  11. use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
  12. use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
  13. use HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMapInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Http\Firewall\AbstractAuthenticationListener;
  18. /**
  19.  * OAuthListener.
  20.  *
  21.  * @author Geoffrey Bachelet <geoffrey.bachelet@gmail.com>
  22.  * @author Alexander <iam.asm89@gmail.com>
  23.  */
  24. class OAuthListener extends AbstractAuthenticationListener
  25. {
  26.     /**
  27.      * @var ResourceOwnerMapInterface
  28.      */
  29.     private $resourceOwnerMap;
  30.     /**
  31.      * @var array
  32.      */
  33.     private $checkPaths;
  34.     /**
  35.      * @param ResourceOwnerMapInterface $resourceOwnerMap
  36.      */
  37.     public function setResourceOwnerMap(ResourceOwnerMapInterface $resourceOwnerMap)
  38.     {
  39.         $this->resourceOwnerMap $resourceOwnerMap;
  40.     }
  41.     /**
  42.      * @param array $checkPaths
  43.      */
  44.     public function setCheckPaths(array $checkPaths)
  45.     {
  46.         $this->checkPaths $checkPaths;
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public function requiresAuthentication(Request $request)
  52.     {
  53.         // Check if the route matches one of the check paths
  54.         foreach ($this->checkPaths as $checkPath) {
  55.             if ($this->httpUtils->checkRequestPath($request$checkPath)) {
  56.                 return true;
  57.             }
  58.         }
  59.         return false;
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     protected function attemptAuthentication(Request $request)
  65.     {
  66.         /* @var ResourceOwnerInterface $resourceOwner */
  67.         list($resourceOwner$checkPath) = $this->resourceOwnerMap->getResourceOwnerByRequest($request);
  68.         if (!$resourceOwner) {
  69.             throw new AuthenticationException('No resource owner match the request.');
  70.         }
  71.         if (!$resourceOwner->handles($request)) {
  72.             throw new AuthenticationException('No oauth code in the request.');
  73.         }
  74.         // If resource owner supports only one url authentication, call redirect
  75.         if ($request->query->has('authenticated') && $resourceOwner->getOption('auth_with_one_url')) {
  76.             $request->attributes->set('service'$resourceOwner->getName());
  77.             return new RedirectResponse(sprintf('%s?code=%s&authenticated=true'$this->httpUtils->generateUri($request'hwi_oauth_connect_service'), $request->query->get('code')));
  78.         }
  79.         $resourceOwner->isCsrfTokenValid($request->get('state'));
  80.         $accessToken $resourceOwner->getAccessToken(
  81.             $request,
  82.             $this->httpUtils->createRequest($request$checkPath)->getUri()
  83.         );
  84.         $token = new OAuthToken($accessToken);
  85.         $token->setResourceOwnerName($resourceOwner->getName());
  86.         return $this->authenticationManager->authenticate($token);
  87.     }
  88. }