Skip to content

Commit

Permalink
[TASK] Redirect to requested url after login
Browse files Browse the repository at this point in the history
  • Loading branch information
buchmarv committed Sep 20, 2024
1 parent 2c5fe4f commit 6605922
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/Security/KeyCloakAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
);
}
Expand All @@ -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();
}
}
3 changes: 2 additions & 1 deletion src/Service/RedirectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

class RedirectService
{
public const DEFAULT_SCOPES = ['openid', 'profile', 'roles', 'email'];
private ClientRegistry $clientRegistry;
private RouterInterface $router;
private string $clientId;
Expand All @@ -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');
Expand Down

0 comments on commit 6605922

Please sign in to comment.