From 6605922fbe850a3ce23a5b06aa36b079fd7d2c0c Mon Sep 17 00:00:00 2001 From: Marvin Buchmann Date: Fri, 20 Sep 2024 11:37:48 +0200 Subject: [PATCH] [TASK] Redirect to requested url after login --- src/Controller/LoginController.php | 2 +- src/Security/KeyCloakAuthenticator.php | 14 +++++++++++--- src/Service/RedirectService.php | 3 ++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index 1f25a8c..8add49b 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -28,7 +28,7 @@ public function login(): RedirectResponse return $this->redirectToRoute($this->getParameter('t3g_keycloak.routes.success')); } - return $this->redirectService->generateLoginRedirectResponse(['openid', 'profile', 'roles', 'email']); + return $this->redirectService->generateLoginRedirectResponse(); } public function oauthCallback(): RedirectResponse diff --git a/src/Security/KeyCloakAuthenticator.php b/src/Security/KeyCloakAuthenticator.php index 4fac813..fb4cee8 100644 --- a/src/Security/KeyCloakAuthenticator.php +++ b/src/Security/KeyCloakAuthenticator.php @@ -26,6 +26,7 @@ use Symfony\Component\Security\Http\Authenticator\Passport\Passport; use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; +use T3G\Bundle\Keycloak\Service\RedirectService; use T3G\Bundle\Keycloak\Service\TokenService; class KeyCloakAuthenticator extends OAuth2Authenticator implements AuthenticationEntrypointInterface @@ -36,19 +37,21 @@ class KeyCloakAuthenticator extends OAuth2Authenticator implements Authenticatio private RouterInterface $router; private UserProviderInterface $userProvider; private TokenService $tokenService; + private RedirectService $redirectService; private ?string $routeAuthentication; private ?string $routeSuccess; /** * @param KeyCloakUserProvider $userProvider */ - public function __construct(ClientRegistry $clientRegistry, RequestStack $requestStack, RouterInterface $router, UserProviderInterface $userProvider, TokenService $tokenService, ?string $routeAuthentication = null, ?string $routeSuccess = null) + public function __construct(ClientRegistry $clientRegistry, RequestStack $requestStack, RouterInterface $router, UserProviderInterface $userProvider, TokenService $tokenService, RedirectService $redirectService, ?string $routeAuthentication = null, ?string $routeSuccess = null) { $this->client = $clientRegistry->getClient('keycloak'); $this->session = $requestStack->getSession(); $this->router = $router; $this->userProvider = $userProvider; $this->tokenService = $tokenService; + $this->redirectService = $redirectService; $this->routeAuthentication = $routeAuthentication; $this->routeSuccess = $routeSuccess; } @@ -82,8 +85,13 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token, return null; } + $redirectUrl = $this->getPreviousUrl($request, $firewallName); + if (null === $redirectUrl || '' === $redirectUrl) { + $redirectUrl = $this->router->generate($this->routeSuccess); + } + return new RedirectResponse( - $this->router->generate($this->routeSuccess), + $redirectUrl, Response::HTTP_TEMPORARY_REDIRECT ); } @@ -101,6 +109,6 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio */ public function start(Request $request, AuthenticationException $authException = null): Response { - return new RedirectResponse('/', Response::HTTP_TEMPORARY_REDIRECT); + return $this->redirectService->generateLoginRedirectResponse(); } } diff --git a/src/Service/RedirectService.php b/src/Service/RedirectService.php index d7476e9..32587b5 100644 --- a/src/Service/RedirectService.php +++ b/src/Service/RedirectService.php @@ -20,6 +20,7 @@ class RedirectService { + public const DEFAULT_SCOPES = ['openid', 'profile', 'roles', 'email']; private ClientRegistry $clientRegistry; private RouterInterface $router; private string $clientId; @@ -34,7 +35,7 @@ public function __construct(ClientRegistry $clientRegistry, RouterInterface $rou /** * @param string[] $scopes */ - public function generateLoginRedirectResponse(array $scopes): RedirectResponse + public function generateLoginRedirectResponse(array $scopes = self::DEFAULT_SCOPES): RedirectResponse { /** @var OAuth2Client $client */ $client = $this->clientRegistry->getClient('keycloak');