Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Redirect to requested url after login #16

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading