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