<?php
namespace App\EventListener;
use App\Security\Dto\TokensBag;
use App\Model\User;
use App\Service\KeycloakApiService;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\Event\LogoutEvent;
class KeycloakLogoutListener implements EventSubscriberInterface
{
private ParameterBagInterface $param;
private LoggerInterface $logger;
private KeycloakApiService $keycloakApiService;
private TokenStorageInterface $tokenStorage;
private UrlGeneratorInterface $urlGenerator;
private string|bool $vistoryRedirect;
private string $mcotPortailRedirect;
public function __construct(
UrlGeneratorInterface $urlGenerator,
TokenStorageInterface $tokenStorage,
KeycloakApiService $keycloakApiService,
LoggerInterface $logger,
) {
$this->urlGenerator = $urlGenerator;
$this->tokenStorage = $tokenStorage;
$this->keycloakApiService = $keycloakApiService;
$this->logger = $logger;
}
public function logoutFromOidcProvider(LogoutEvent $event): void
{
$token = $this->tokenStorage->getToken();
$user = $token->getUser();
if (!$user instanceof User) {
return;
}
$tokens = $token->getAttribute(TokensBag::class);
if (null === $tokens) {
throw new \LogicException(sprintf('%s token attribute is empty', TokensBag::class));
}
$this->logger->info("***** Keycloak logout *****");
$this->keycloakApiService->logout($tokens->getRefreshToken());
$redirect = $this->urlGenerator->generate('login');
$event->setResponse(new RedirectResponse($redirect));
}
public static function getSubscribedEvents(): array
{
return [LogoutEvent::class => 'logoutFromOidcProvider'];
}
}