<?php declare(strict_types=1);
namespace App\Security\Http\Logout;
use App\Security\TwoFactor\AllowedDevice\ReferenceStorage;
use App\Security\TwoFactor\TwoFactorManager;
use App\Security\TwoFactor\TwoFactorUserInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\LogoutEvent;
class TwoFactorLogoutSubscriber implements EventSubscriberInterface
{
private TwoFactorManager $twoFactorManager;
private ReferenceStorage $referenceStorage;
public function __construct(TwoFactorManager $twoFactorManager, ReferenceStorage $referenceStorage)
{
$this->twoFactorManager = $twoFactorManager;
$this->referenceStorage = $referenceStorage;
}
public function onLogout(LogoutEvent $event): void
{
$token = $event->getToken();
if (null === $token) {
return;
}
$user = $token->getUser();
if ($user instanceof TwoFactorUserInterface) {
$this->twoFactorManager->unauthenticate($event->getRequest());
}
if (null === $this->referenceStorage->fetch()) {
return;
}
$this->referenceStorage->forget();
}
/**
* @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
*/
public static function getSubscribedEvents(): array
{
return [
LogoutEvent::class => 'onLogout',
];
}
}