diff --git a/src/email/Event/EmailCodeValidated.php b/src/email/Event/EmailCodeValidated.php new file mode 100644 index 00000000..8215949d --- /dev/null +++ b/src/email/Event/EmailCodeValidated.php @@ -0,0 +1,24 @@ +email = $email; + } + + public function getEmail(): string + { + return $this->email; + } +} diff --git a/src/email/Event/EmailTwoFactorEvents.php b/src/email/Event/EmailTwoFactorEvents.php new file mode 100644 index 00000000..d4ec084c --- /dev/null +++ b/src/email/Event/EmailTwoFactorEvents.php @@ -0,0 +1,14 @@ +codeGenerator = $codeGenerator; $this->formRenderer = $formRenderer; + $this->eventDispatcher = $eventDispatcher; } public function beginAuthentication(AuthenticationContextInterface $context): bool @@ -55,7 +68,15 @@ public function validateAuthenticationCode($user, string $authenticationCode): b // Strip any user added spaces $authenticationCode = str_replace(' ', '', $authenticationCode); - return $user->getEmailAuthCode() === $authenticationCode; + $isCodeValid = $user->getEmailAuthCode() === $authenticationCode; + + if (false === $isCodeValid) { + return false; + } + + $this->eventDispatcher->dispatch(new EmailCodeValidated($user->getEmailAuthRecipient()), EmailTwoFactorEvents::EMAIL_CODE_VALIDATED); + + return true; } public function getFormRenderer(): TwoFactorFormRendererInterface diff --git a/tests/Security/TwoFactor/Provider/Email/EmailTwoFactorProviderTest.php b/tests/Security/TwoFactor/Provider/Email/EmailTwoFactorProviderTest.php index 724af7f3..135f0603 100644 --- a/tests/Security/TwoFactor/Provider/Email/EmailTwoFactorProviderTest.php +++ b/tests/Security/TwoFactor/Provider/Email/EmailTwoFactorProviderTest.php @@ -11,6 +11,9 @@ use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Generator\CodeGeneratorInterface; use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorFormRendererInterface; use Scheb\TwoFactorBundle\Tests\TestCase; +use stdClass; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Security\Core\User\UserInterface; class EmailTwoFactorProviderTest extends TestCase { @@ -28,11 +31,17 @@ class EmailTwoFactorProviderTest extends TestCase */ private $provider; + /** + * @var EventDispatcherInterface + */ + private $eventDispatcher; + protected function setUp(): void { $this->generator = $this->createMock(CodeGeneratorInterface::class); $formRenderer = $this->createMock(TwoFactorFormRendererInterface::class); - $this->provider = new EmailTwoFactorProvider($this->generator, $formRenderer); + $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); + $this->provider = new EmailTwoFactorProvider($this->generator, $formRenderer, $this->eventDispatcher); } private function createUser(bool $emailAuthEnabled = true): MockObject