<?php
namespace Harmonizely\Core\Security\Subscriber;
use Harmonizely\Core\Security\Contract\ISecurityHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
/**
* Class CancelSubscriptionSubscriber
* @package Harmonizely\Service\Subscription\Subscriber
*/
class RememberMeLoginSubscriber implements EventSubscriberInterface
{
/**
* @var ISecurityHelper
*/
private ISecurityHelper $securityHelper;
/**
* ReceiveSubscriptionSubscriber constructor.
* @param ISecurityHelper $securityHelper
*/
public function __construct(ISecurityHelper $securityHelper)
{
$this->securityHelper = $securityHelper;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
];
}
/**
* @param InteractiveLoginEvent $event
* @return void
*/
public function onInteractiveLogin(InteractiveLoginEvent $event): void
{
$token = $event->getAuthenticationToken();
if ($token instanceof RememberMeToken) {
$user = $this->securityHelper->getUser();
$this->securityHelper->setLoginType($token, ISecurityHelper::LOGIN_TYPE_REMEMBER_ME);
if ($user) {
$codeFromCookie = $event->getRequest()->cookies->get(ISecurityHelper::LAST_TWO_FACTOR_CODE);
$codeFromDb = $user->getLastTwoFactorCode();
if ($codeFromCookie && $codeFromDb) {
if ($codeFromCookie === $codeFromDb) {
$this->securityHelper->setSecondFactorPassed($token, true);
}
}
}
}
}
}