vendor/hwi/oauth-bundle/src/Controller/RedirectToServiceController.php line 57

  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\Controller;
  11. use HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMapLocator;
  12. use HWI\Bundle\OAuthBundle\Security\OAuthUtils;
  13. use HWI\Bundle\OAuthBundle\Util\DomainWhitelist;
  14. use RuntimeException;
  15. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  19. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  20. /**
  21.  * @author Alexander <iam.asm89@gmail.com>
  22.  *
  23.  * @internal
  24.  */
  25. final class RedirectToServiceController
  26. {
  27.     private OAuthUtils $oauthUtils;
  28.     private DomainWhitelist $domainWhitelist;
  29.     private ResourceOwnerMapLocator $resourceOwnerMapLocator;
  30.     private ?string $targetPathParameter null;
  31.     private bool $failedUseReferer;
  32.     private bool $useReferer;
  33.     public function __construct(
  34.         OAuthUtils $oauthUtils,
  35.         DomainWhitelist $domainWhitelist,
  36.         ResourceOwnerMapLocator $resourceOwnerMapLocator,
  37.         ?string $targetPathParameter,
  38.         bool $failedUseReferer,
  39.         bool $useReferer
  40.     ) {
  41.         $this->oauthUtils $oauthUtils;
  42.         $this->domainWhitelist $domainWhitelist;
  43.         $this->resourceOwnerMapLocator $resourceOwnerMapLocator;
  44.         $this->targetPathParameter $targetPathParameter;
  45.         $this->failedUseReferer $failedUseReferer;
  46.         $this->useReferer $useReferer;
  47.     }
  48.     /**
  49.      * @throws NotFoundHttpException
  50.      */
  51.     public function redirectToServiceAction(Request $requeststring $service): RedirectResponse
  52.     {
  53.         try {
  54.             $authorizationUrl $this->oauthUtils->getAuthorizationUrl($request$service);
  55.         } catch (RuntimeException $e) {
  56.             throw new NotFoundHttpException($e->getMessage(), $e);
  57.         }
  58.         $this->storeReturnPath($request$authorizationUrl);
  59.         return new RedirectResponse($authorizationUrl);
  60.     }
  61.     private function storeReturnPath(Request $requeststring $authorizationUrl): void
  62.     {
  63.         try {
  64.             $session $request->getSession();
  65.         } catch (SessionNotFoundException $e) {
  66.             return;
  67.         }
  68.         $param $this->targetPathParameter;
  69.         foreach ($this->resourceOwnerMapLocator->getFirewallNames() as $firewallName) {
  70.             $sessionKey '_security.'.$firewallName.'.target_path';
  71.             $sessionKeyFailure '_security.'.$firewallName.'.failed_target_path';
  72.             if (!empty($param) && $targetUrl $request->get($param)) {
  73.                 if (!$this->domainWhitelist->isValidTargetUrl($targetUrl)) {
  74.                     throw new AccessDeniedHttpException('Not allowed to redirect to '.$targetUrl);
  75.                 }
  76.                 $session->set($sessionKey$targetUrl);
  77.             }
  78.             if ($this->failedUseReferer && !$session->has($sessionKeyFailure) && ($targetUrl $request->headers->get('Referer')) && $targetUrl !== $authorizationUrl) {
  79.                 $session->set($sessionKeyFailure$targetUrl);
  80.             }
  81.             if ($this->useReferer && !$session->has($sessionKey) && ($targetUrl $request->headers->get('Referer')) && $targetUrl !== $authorizationUrl) {
  82.                 $session->set($sessionKey$targetUrl);
  83.             }
  84.         }
  85.     }
  86. }