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

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