vendor/nbgrp/onelogin-saml-bundle/src/Controller/Login.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. namespace Nbgrp\OneloginSamlBundle\Controller;
  4. use Nbgrp\OneloginSamlBundle\Security\Http\Authenticator\SamlAuthenticator;
  5. use OneLogin\Saml2\Auth;
  6. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpKernel\Attribute\AsController;
  11. use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
  12. use Symfony\Component\Security\Core\Security;
  13. #[AsController]
  14. class Login
  15. {
  16.     public function __construct(
  17.         private FirewallMap $firewallMap,
  18.     ) {}
  19.     public function __invoke(Request $requestAuth $auth): RedirectResponse
  20.     {
  21.         $targetPath null;
  22.         $session null;
  23.         /** @var \Throwable|null $error */
  24.         $error $request->attributes->get(Security::AUTHENTICATION_ERROR);
  25.         if ($request->hasSession()) {
  26.             $session $request->getSession();
  27.             $targetPath $this->getTargetPath($request$session);
  28.             if ($session->has(Security::AUTHENTICATION_ERROR)) {
  29.                 /** @var \Throwable|null $error */
  30.                 $error $session->get(Security::AUTHENTICATION_ERROR);
  31.                 $session->remove(Security::AUTHENTICATION_ERROR);
  32.             }
  33.         }
  34.         if ($error instanceof \Throwable) {
  35.             throw new \RuntimeException($error->getMessage());
  36.         }
  37.         return new RedirectResponse($this->processLoginAndGetRedirectUrl($auth$targetPath$session));
  38.     }
  39.     /** @psalm-suppress MixedInferredReturnType, MixedReturnStatement */
  40.     private function getTargetPath(Request $requestSessionInterface $session): ?string
  41.     {
  42.         $firewallName $this->firewallMap->getFirewallConfig($request)?->getName();
  43.         if (!$firewallName) {
  44.             throw new ServiceUnavailableHttpException(message'Unknown firewall.');
  45.         }
  46.         /** @phpstan-ignore-next-line */
  47.         return $session->get('_security.'.$firewallName.'.target_path');
  48.     }
  49.     private function processLoginAndGetRedirectUrl(Auth $auth, ?string $targetPath, ?SessionInterface $session): string
  50.     {
  51.         $redirectUrl $auth->login(returnTo$targetPathstaytrue);
  52.         if ($redirectUrl === null) {
  53.             throw new \RuntimeException('Login cannot be performed: Auth did not returned redirect url.');
  54.         }
  55.         $security $auth->getSettings()->getSecurityData();
  56.         if (($security['rejectUnsolicitedResponsesWithInResponseTo'] ?? false) && $session instanceof SessionInterface) {
  57.             $session->set(SamlAuthenticator::LAST_REQUEST_ID$auth->getLastRequestID());
  58.         }
  59.         return $redirectUrl;
  60.     }
  61. }