Skip to content

Commit

Permalink
Dispatch EmailValidatedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
mdzwigaladp committed Sep 5, 2024
1 parent 9828337 commit a76c801
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/email/Event/EmailCodeValidated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Scheb\TwoFactorBundle\Event;

class EmailCodeValidated
{
/**
* @var string
*/
private $email;

public function __construct(
string $email
) {
$this->email = $email;
}

public function getEmail(): string
{
return $this->email;
}
}
14 changes: 14 additions & 0 deletions src/email/Event/EmailTwoFactorEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Scheb\TwoFactorBundle\Event;

class EmailTwoFactorEvents
{
public const EMAIL_CODE_VALIDATED = 'scheb_two_factor.email_code.validated';

private function __construct()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email;

use Scheb\TwoFactorBundle\Event\EmailCodeValidated;
use Scheb\TwoFactorBundle\Event\EmailTwoFactorEvents;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Generator\CodeGeneratorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorFormRendererInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use function str_replace;

/**
* @final
Expand All @@ -25,10 +29,19 @@ class EmailTwoFactorProvider implements TwoFactorProviderInterface
*/
private $formRenderer;

public function __construct(CodeGeneratorInterface $codeGenerator, TwoFactorFormRendererInterface $formRenderer)
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

public function __construct(
CodeGeneratorInterface $codeGenerator,
TwoFactorFormRendererInterface $formRenderer,
EventDispatcherInterface $eventDispatcher
) {
$this->codeGenerator = $codeGenerator;
$this->formRenderer = $formRenderer;
$this->eventDispatcher = $eventDispatcher;
}

public function beginAuthentication(AuthenticationContextInterface $context): bool
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down

0 comments on commit a76c801

Please sign in to comment.