Skip to content

Refactor mailcrypt creation #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ services:
$mailLocation: '%env(DOVECOT_MAIL_LOCATION)%'

App\Command\VoucherCreateCommand:
arguments:
$appUrl: "%env(APP_URL)%"
arguments:
$appUrl: '%env(APP_URL)%'

App\Controller\DovecotController:
arguments:
Expand All @@ -82,6 +82,10 @@ services:
tags:
- { name: kernel.event_subscriber }

App\EventListener\LoginListener:
arguments:
$mailCryptEnv: '%env(MAIL_CRYPT)%'

App\EventListener\LocaleListener:
arguments:
$defaultLocale: '%locale%'
Expand Down Expand Up @@ -122,14 +126,14 @@ services:
$to: '%env(NOTIFICATION_ADDRESS)%'

App\Handler\UserRestoreHandler:
arguments:
$mailCryptEnv: "%env(MAIL_CRYPT)%"
arguments:
$mailCryptEnv: '%env(MAIL_CRYPT)%'

App\Handler\RegistrationHandler:
public: true
arguments:
$registrationOpen: "%env(REGISTRATION_OPEN)%"
$mailCrypt: '%env(MAIL_CRYPT)%'
public: true
arguments:
$registrationOpen: '%env(REGISTRATION_OPEN)%'
$mailCrypt: '%env(MAIL_CRYPT)%'

App\Handler\SuspiciousChildrenHandler:
arguments:
Expand Down
13 changes: 0 additions & 13 deletions src/Controller/DovecotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,6 @@ public function authenticate(
return $this->json(['message' => self::MESSAGE_AUTHENTICATION_FAILED], Response::HTTP_UNAUTHORIZED);
}

// If mailCrypt is enforced for all users, optionally create mailCrypt keypair for user
if (
$this->mailCrypt === MailCrypt::ENABLED_ENFORCE_ALL_USERS &&
false === $user->getMailCryptEnabled() &&
null === $user->getMailCryptPublicKey()
) {
try {
$this->mailCryptKeyHandler->create($user, $request->getPassword(), true);
} catch (Exception $exception) {
return $this->json(['error' => $exception->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

// If mailCrypt is enabled and enabled for user, derive mailCryptPrivateKey
if ($this->mailCrypt->isAtLeast(MailCrypt::ENABLED_OPTIONAL) && $user->getMailCryptEnabled()) {
try {
Expand Down
21 changes: 14 additions & 7 deletions src/Event/LoginEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@
use App\Traits\UserAwareTrait;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Class LoginEvent.
*/
class LoginEvent extends Event
{
use UserAwareTrait;

public const NAME = 'user.login';

/**
* Constructor.
*/
public function __construct(User $user)
private string $plainPassword;

public function getPlainPassword(): string
{
return $this->plainPassword;
}

public function setPlainPassword(string $plainPassword): void
{
$this->plainPassword = $plainPassword;
}

public function __construct(User $user, string $plainPassword)
{
$this->user = $user;
$this->plainPassword = $plainPassword;
}
}
40 changes: 33 additions & 7 deletions src/EventListener/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,59 @@
namespace App\EventListener;

use App\Entity\User;
use App\Enum\MailCrypt;
use App\Event\LoginEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use App\Handler\MailCryptKeyHandler;

readonly class LoginListener implements EventSubscriberInterface
{
public function __construct(private EntityManagerInterface $manager)
{
private readonly MailCrypt $mailCrypt;

public function __construct(
private EntityManagerInterface $manager,
private readonly MailCryptKeyHandler $mailCryptKeyHandler,
private readonly int $mailCryptEnv,
) {
$this->mailCrypt = MailCrypt::from($this->mailCryptEnv);
}

public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
{
$user = $event->getAuthenticationToken()->getUser();

if ($user instanceof User) {
$this->handleLogin($user);
$password = $event->getRequest()->get('_password');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this guaranteed to work on all interactive logins? I don't know the details of this InteractiveLoginEvent and when exactly it is dispatched. Is login via the login form the only interactive login scenario? I guess there _password is accessible in the request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically InteractiveLoginEvent covers anything which is interactive, including Json login. But with our current code, the login form is the only case, yes
https://symfony.com/doc/current/security.html#other-events

$this->handleLogin($user, $password);
}
}

public function onLogin(LoginEvent $event): void
public function onAuthenticationHandlerSuccess(LoginEvent $event): void
{
$this->handleLogin($event->getUser(), $event->getPlainPassword());
}

private function handleLogin(User $user, ?string $password): void
{
$this->handleLogin($event->getUser());
if ($this->mailCrypt === MailCrypt::ENABLED_ENFORCE_ALL_USERS && null !== $password) {
$this->enableMailCrypt($user, $password);
}

$this->updateLastLogin($user);
}

private function enableMailCrypt(User $user, string $password): void
{
if ($user->getMailCryptEnabled() || null !== $user->getMailCryptPublicKey()) {
return;
}
$this->mailCryptKeyHandler->create($user, $password, true);
}

private function handleLogin(User $user): void
private function updateLastLogin(User $user): void
{
$user->updateLastLoginTime();
$this->manager->persist($user);
Expand All @@ -40,7 +66,7 @@ public static function getSubscribedEvents(): array
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
LoginEvent::NAME => 'onLogin',
LoginEvent::NAME => 'onAuthenticationHandlerSuccess',
];
}
}
19 changes: 9 additions & 10 deletions src/Handler/UserAuthenticationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* Class UserAuthenticationHandler.
*/
class UserAuthenticationHandler
{
/**
* UserAuthenticationHandler constructor.
*/
public function __construct(private readonly PasswordHasherFactoryInterface $passwordHasherFactory, protected EventDispatcherInterface $eventDispatcher)
{
}
public function __construct(
private readonly PasswordHasherFactoryInterface
$passwordHasherFactory,
protected EventDispatcherInterface $eventDispatcher
) {}

public function authenticate(User $user, string $password): ?User
{
$hasher = $this->passwordHasherFactory->getPasswordHasher($user);
if (!$hasher->verify($user->getPassword(), $password)) {
return null;
}
$this->eventDispatcher->dispatch(new LoginEvent($user), LoginEvent::NAME);

$this->eventDispatcher->dispatch(
new LoginEvent($user, $password),
LoginEvent::NAME
);
return $user;
}
}
131 changes: 121 additions & 10 deletions tests/EventListener/LoginListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,84 @@
use App\Entity\User;
use App\Event\LoginEvent;
use App\EventListener\LoginListener;
use App\Handler\MailCryptKeyHandler;
use App\Helper\PasswordUpdater;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use PHPUnit\Framework\assertCount;
use PHPUnit\Framework\assertEquals;

class LoginListenerTest extends TestCase
{
private EntityManagerInterface $manager;
private LoginListener $listener;
private LoginListener $listenerMailCrypt;
private MailCryptKeyHandler $mailCryptKeyHandler;

public function setUp(): void
{
$this->manager = $this->getMockBuilder(EntityManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->listener = new LoginListener($this->manager);
$this->passwordUpdater = $this->getMockBuilder(PasswordUpdater::class)
->disableOriginalConstructor()
->getMock();
$this->mailCryptKeyHandler = $this->getMockBuilder(MailCryptKeyHandler::class)
->disableOriginalConstructor()
->getMock();
$this->listener = new LoginListener(
$this->manager,
$this->mailCryptKeyHandler,
2
);
// Enforces creation of mailCrypt on Login
$this->listenerMailCrypt = new LoginListener(
$this->manager,
$this->mailCryptKeyHandler,
3
);
}

public function testOnSecurityInteractiveLogin(): void
/**
* @dataProvider provider
*/
public function testOnSecurityInteractiveLogin(User $user, bool $shouldCreateMailCryptKey): void
{
$user = new User();
$this->manager->expects($this->once())->method('flush');
$event = $this->getEvent($user);
$this->mailCryptKeyHandler->expects($this->never())->method('create');

$event = $this->getInteractiveEvent($user);

$this->listener->onSecurityInteractiveLogin($event);
}

/**
* @dataProvider provider
*/
public function testOnSecurityInteractiveLoginMailCrypt(User $user, bool $shouldCreateMailCryptKey): void
{
$this->manager->expects($this->once())->method('flush');

if ($shouldCreateMailCryptKey) {
$this->mailCryptKeyHandler->expects($this->once())->method('create');
} else {
$this->mailCryptKeyHandler->expects($this->never())->method('create');
}

$event = $this->getInteractiveEvent($user);

$this->listenerMailCrypt->onSecurityInteractiveLogin($event);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|InteractiveLoginEvent
*/
private function getEvent(User $user): InteractiveLoginEvent
private function getInteractiveEvent(User $user): InteractiveLoginEvent
{
$request = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
Expand All @@ -60,12 +104,79 @@ private function getEvent(User $user): InteractiveLoginEvent
return $event;
}


/**
* @dataProvider provider
*/
public function testOnAuthenticationHandlerSuccess(User $user, bool $shouldCreateMailCryptKey): void
{
$this->manager->expects($this->once())->method('flush');
$this->mailCryptKeyHandler->expects($this->never())->method('create');

$event = $this->getLoginEvent($user);

$this->listener->onAuthenticationHandlerSuccess($event);
}

/**
* @dataProvider provider
*/
public function testOnAuthenticationHandlerSuccessMailCrypt(User $user, bool $shouldCreateMailCryptKey): void
{
$this->manager->expects($this->once())->method('flush');

if ($shouldCreateMailCryptKey) {
$this->mailCryptKeyHandler->expects($this->once())->method('create');
} else {
$this->mailCryptKeyHandler->expects($this->never())->method('create');
}

$event = $this->getLoginEvent($user);

$this->listenerMailCrypt->onAuthenticationHandlerSuccess($event);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|LoginEvent
*/
private function getLoginEvent(User $user): LoginEvent
{
$event = $this->getMockBuilder(LoginEvent::class)
->disableOriginalConstructor()
->getMock();

$event->method('getUser')->willReturn($user);
$event->method('getPlainPassword')->willReturn('password');

return $event;
}

public static function provider(): array
{
$enableMailCrypt = [null, false, true];
$shouldCreateMailCryptKeys = [true, true, false];

return array_map(
function ($enable, $create) {
$user = new User();
if ($enable === false || $enable === true) {
$user->setMailCryptEnabled($enable);
}
return [$user, $create];
},
$enableMailCrypt,
$shouldCreateMailCryptKeys
);
}

public function testGetSubscribedEvents(): void
{
$this->assertEquals([
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
LoginEvent::NAME => 'onLogin',
],
$this->listener::getSubscribedEvents());
$this->assertEquals(
[
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
LoginEvent::NAME => 'onAuthenticationHandlerSuccess',
],
$this->listener::getSubscribedEvents()
);
}
}