Skip to content

Commit

Permalink
Reuse UserNotifier for 2fa emails
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Mar 21, 2024
1 parent 60062b5 commit 910e99d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 61 deletions.
2 changes: 0 additions & 2 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ services:
App\Security\TwoFactorAuthManager:
public: true
class: App\Security\TwoFactorAuthManager
arguments:
$options: { from: '%env(APP_MAILER_FROM_EMAIL)%', fromName: '%env(APP_MAILER_FROM_NAME)%' }

App\Service\SecurityAdvisoryWorker:
$sources:
Expand Down
59 changes: 15 additions & 44 deletions src/Security/TwoFactorAuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@
namespace App\Security;

use App\Entity\User;
use Psr\Log\LoggerInterface;
use Scheb\TwoFactorBundle\Model\BackupCodeInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Backup\BackupCodeManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Twig\Environment;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;

/**
* @author Colin O'Dell <[email protected]>
Expand All @@ -32,12 +26,8 @@ class TwoFactorAuthManager implements BackupCodeManagerInterface
{
public function __construct(
private ManagerRegistry $doctrine,
private MailerInterface $mailer,
private Environment $twig,
private LoggerInterface $logger,
private RequestStack $requestStack,
/** @var array{from: string, fromName: string} */
private array $options
private UserNotifier $userNotifier,
) {
}

Expand All @@ -49,22 +39,12 @@ public function enableTwoFactorAuth(User $user, string $secret): void
$user->setTotpSecret($secret);
$this->doctrine->getManager()->flush();

$body = $this->twig->render('email/two_factor_enabled.txt.twig', [
'username' => $user->getUsername(),
]);

$message = (new Email())
->subject('[Packagist] Two-factor authentication enabled')
->from(new Address($this->options['from'], $this->options['fromName']))
->to($user->getEmail())
->text($body)
;

try {
$this->mailer->send($message);
} catch (TransportExceptionInterface $e) {
$this->logger->error('['.get_class($e).'] '.$e->getMessage());
}
$this->userNotifier->notifyChange(
$user->getEmail(),
template: 'email/two_factor_enabled.txt.twig',
subject: 'Two-factor authentication enabled on Packagist.org',
username: $user->getUsername()
);
}

/**
Expand All @@ -76,23 +56,14 @@ public function disableTwoFactorAuth(User $user, string $reason): void
$user->invalidateAllBackupCodes();
$this->doctrine->getManager()->flush();

$body = $this->twig->render('email/two_factor_disabled.txt.twig', [
'username' => $user->getUsername(),
'reason' => $reason,
]);

$message = (new Email())
->subject('[Packagist] Two-factor authentication disabled')
->from(new Address($this->options['from'], $this->options['fromName']))
->to($user->getEmail())
->text($body)
;

try {
$this->mailer->send($message);
} catch (TransportExceptionInterface $e) {
$this->logger->error('['.get_class($e).'] '.$e->getMessage());
}

$this->userNotifier->notifyChange(
$user->getEmail(),
template: 'email/two_factor_disabled.txt.twig',
subject: 'Two-factor authentication disabled on Packagist.org',
username: $user->getUsername(),
reason: $reason,
);
}

/**
Expand Down
34 changes: 20 additions & 14 deletions src/Security/UserNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

namespace App\Security;

use Psr\Log\LoggerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;

Expand All @@ -21,28 +23,32 @@
*/
class UserNotifier
{
private MailerInterface $mailer;
private string $mailFromEmail;
private string $mailFromName;
public function __construct(
private string $mailFromEmail,
private string $mailFromName,
private MailerInterface $mailer,
private LoggerInterface $logger,
) {}

public function __construct(string $mailFromEmail, string $mailFromName, MailerInterface $mailer)
{
$this->mailer = $mailer;
$this->mailFromEmail = $mailFromEmail;
$this->mailFromName = $mailFromName;
}

public function notifyChange(string $email, string $reason): void
/**
* @param array<string, mixed> $templateVars
*/
public function notifyChange(string $email, string $reason = '', string $template = 'email/alert_change.txt.twig', string $subject = 'A change has been made to your Packagist.org account', ...$templateVars): void
{
$email = (new TemplatedEmail())
->from(new Address($this->mailFromEmail, $this->mailFromName))
->to($email)
->subject('A change has been made to your account')
->textTemplate('email/alert_change.txt.twig')
->subject($subject)
->textTemplate($template)
->context([
'reason' => $reason,
...$templateVars
]);

$this->mailer->send($email);
try {
$this->mailer->send($email);
} catch (TransportExceptionInterface $e) {
$this->logger->error('['.get_class($e).'] '.$e->getMessage());
}
}
}
2 changes: 1 addition & 1 deletion templates/email/two_factor_disabled.txt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Two-factor authentication has been disabled on your Packagist account.

-------------------------------
Time: {{ 'now'|date('c') }}
Reason: {{ reason }}
Time: {{ 'now'|date('c') }}
-------------------------------

You can re-enable this at any time from your account page: {{ url('user_2fa_configure', { 'name': username }) }}
Expand Down

0 comments on commit 910e99d

Please sign in to comment.