[CORE][SECURITY][EMAIL] Move email confirmation functionality to it's own static wrapper, in preparation for adding password reset functionality

This commit is contained in:
Hugo Sales 2021-07-29 15:03:52 +00:00
parent 176d604abb
commit bdbd588de9
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 38 additions and 37 deletions

View File

@ -13,7 +13,6 @@ use App\Entity\GSActor;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Entity\Note; use App\Entity\Note;
use App\Security\Authenticator; use App\Security\Authenticator;
use App\Security\EmailVerifier;
use app\Util\Common; use app\Util\Common;
use App\Util\Exception\EmailTakenException; use App\Util\Exception\EmailTakenException;
use App\Util\Exception\NicknameTakenException; use App\Util\Exception\NicknameTakenException;
@ -67,7 +66,6 @@ class Security extends Controller
* possibly sending a confirmation email * possibly sending a confirmation email
*/ */
public function register(Request $request, public function register(Request $request,
EmailVerifier $email_verifier,
GuardAuthenticatorHandler $guard_handler, GuardAuthenticatorHandler $guard_handler,
Authenticator $authenticator) Authenticator $authenticator)
{ {
@ -142,16 +140,8 @@ class Security extends Controller
} }
// generate a signed url and email it to the user // generate a signed url and email it to the user
if (Common::config('site', 'use_email')) { if ($_ENV['APP_ENV'] === 'dev' || Common::config('site', 'use_email')) {
$email_verifier->sendEmailConfirmation( Common::sendVerificationEmail();
'verify_email',
$user,
(new TemplatedEmail())
->from(new Address(Common::config('site', 'email'), Common::config('site', 'nickname')))
->to($user->getOutgoingEmail())
->subject(_m('Please Confirm your Email'))
->htmlTemplate('security/confirmation_email.html.twig')
);
} else { } else {
$user->setIsEmailVerified(true); $user->setIsEmailVerified(true);
} }

View File

@ -69,6 +69,7 @@ use Symfony\Component\Security\Core\Security as SSecurity;
use Symfony\Component\Security\Http\Util\TargetPathTrait; use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
use Twig\Environment; use Twig\Environment;
/** /**
@ -95,6 +96,7 @@ class GNUsocial implements EventSubscriberInterface
protected ContainerBagInterface $config; protected ContainerBagInterface $config;
protected Environment $twig; protected Environment $twig;
protected ?Request $request; protected ?Request $request;
protected VerifyEmailHelperInterface $email_verify_helper;
/** /**
* Symfony dependency injection gives us access to these services * Symfony dependency injection gives us access to these services
@ -114,24 +116,26 @@ class GNUsocial implements EventSubscriberInterface
SanitizerInterface $san, SanitizerInterface $san,
ContainerBagInterface $conf, ContainerBagInterface $conf,
Environment $twig, Environment $twig,
RequestStack $request_stack) RequestStack $request_stack,
VerifyEmailHelperInterface $email_helper)
{ {
$this->logger = $logger; $this->logger = $logger;
$this->translator = $trans; $this->translator = $trans;
$this->entity_manager = $em; $this->entity_manager = $em;
$this->router = $router; $this->router = $router;
$this->url_generator = $url_gen; $this->url_generator = $url_gen;
$this->form_factory = $ff; $this->form_factory = $ff;
$this->message_bus = $mb; $this->message_bus = $mb;
$this->event_dispatcher = $ed; $this->event_dispatcher = $ed;
$this->session = $sess; $this->session = $sess;
$this->security = $sec; $this->security = $sec;
$this->module_manager = $mm; $this->module_manager = $mm;
$this->client = $cl; $this->client = $cl;
$this->sanitizer = $san; $this->sanitizer = $san;
$this->config = $conf; $this->config = $conf;
$this->twig = $twig; $this->twig = $twig;
$this->request = $request_stack->getCurrentRequest(); $this->request = $request_stack->getCurrentRequest();
$this->email_verify_helper = $email_helper;
$this->initialize(); $this->initialize();
} }
@ -159,6 +163,7 @@ class GNUsocial implements EventSubscriberInterface
HTTPClient::setClient($this->client); HTTPClient::setClient($this->client);
Formatting::setTwig($this->twig); Formatting::setTwig($this->twig);
Cache::setupCache(); Cache::setupCache();
EmailVerifier::setVerifyEmailHelper($this->email_verify_helper);
DB::initTableMap(); DB::initTableMap();

View File

@ -6,23 +6,29 @@ use App\Core\DB\DB;
use App\Core\Mailer; use App\Core\Mailer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mime\Address;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface; use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface; use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
class EmailVerifier abstract class EmailVerifier
{ {
private $verify_email_helper; private static ?VerifyEmailHelperInterface $verify_email_helper;
public function setVerifyEmailHelper(VerifyEmailHelperInterface $helper)
public function __construct(VerifyEmailHelperInterface $helper)
{ {
$this->verifyEmailHelper = $helper; self::$verifyEmailHelper = $helper;
} }
public function sendEmailConfirmation(string $verify_email_route_name, UserInterface $user, TemplatedEmail $email): void public static function sendEmailConfirmation(UserInterface $user): void
{ {
$signatureComponents = $this->verify_email_helper->generateSignature( $email = (new TemplatedEmail())
$verify_email_route_name, ->from(new Address(Common::config('site', 'email'), Common::config('site', 'nickname')))
->to($user->getOutgoingEmail())
->subject(_m('Please Confirm your Email'))
->htmlTemplate('security/confirmation_email.html.twig');
$signatureComponents = self::$verify_email_helper->generateSignature(
'verify_email',
$user->getId(), $user->getId(),
$user->getOutgoingEmail() $user->getOutgoingEmail()
); );