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

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