Skip to content

Commit 0e0515a

Browse files
dt-thomas-durandscheb
authored andcommitted
fix: stateless 2fa provider preparation listener
1 parent 8cc44a8 commit 0e0515a

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/bundle/Resources/config/security.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
->tag('kernel.event_subscriber')
8888

8989
->set('scheb_two_factor.security.provider_preparation_listener', TwoFactorProviderPreparationListener::class)
90+
->tag('kernel.reset', ['method' => 'reset'])
9091
->args([
9192
service('scheb_two_factor.provider_registry'),
9293
service('scheb_two_factor.provider_preparation_recorder'),

src/bundle/Security/TwoFactor/Provider/TwoFactorProviderPreparationListener.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1717
use Symfony\Component\Security\Core\AuthenticationEvents;
1818
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
19+
use Symfony\Contracts\Service\ResetInterface;
1920
use function assert;
2021
use function sprintf;
2122
use const PHP_INT_MAX;
2223

2324
/**
2425
* @final
2526
*/
26-
class TwoFactorProviderPreparationListener implements EventSubscriberInterface
27+
class TwoFactorProviderPreparationListener implements EventSubscriberInterface, ResetInterface
2728
{
2829
// This must trigger very first, followed by AuthenticationSuccessEventSuppressor
2930
public const int AUTHENTICATION_SUCCESS_LISTENER_PRIORITY = PHP_INT_MAX;
@@ -136,4 +137,9 @@ public static function getSubscribedEvents(): array
136137
KernelEvents::RESPONSE => ['onKernelResponse', self::RESPONSE_LISTENER_PRIORITY],
137138
];
138139
}
140+
141+
public function reset(): void
142+
{
143+
$this->twoFactorToken = null;
144+
}
139145
}

tests/Security/TwoFactor/Provider/TwoFactorProviderPreparationListenerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,19 @@ public function onKernelResponse_recorderThrowsUnexpectedTokenException_doNothin
230230
$this->listener->onTwoFactorForm($event);
231231
$this->listener->onKernelResponse($this->createResponseEvent());
232232
}
233+
234+
#[Test]
235+
public function reset_tokenPreviouslySetted_resetToken(): void
236+
{
237+
$this->initTwoFactorProviderPreparationListener(true, false);
238+
$event = $this->createAuthenticationEvent();
239+
240+
$this->expectNotPrepareCurrentProvider();
241+
242+
$this->listener->onLogin($event);
243+
244+
$this->listener->reset();
245+
246+
$this->listener->onKernelResponse($this->createResponseEvent());
247+
}
233248
}

0 commit comments

Comments
 (0)