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

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