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

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