|
15 | 15 | use App\Entity\User; |
16 | 16 | use App\Entity\UserRepository; |
17 | 17 | use App\Form\RegistrationFormType; |
| 18 | +use App\Form\UpdateEmailFormType; |
18 | 19 | use App\Security\BruteForceLoginFormAuthenticator; |
19 | 20 | use App\Security\EmailVerifier; |
20 | 21 | use App\Security\UserChecker; |
| 22 | +use Psr\Clock\ClockInterface; |
21 | 23 | use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
22 | 24 | use Symfony\Component\HttpFoundation\Request; |
23 | 25 | use Symfony\Component\HttpFoundation\Response; |
|
30 | 32 |
|
31 | 33 | class RegistrationController extends Controller |
32 | 34 | { |
33 | | - public function __construct(private EmailVerifier $emailVerifier) |
| 35 | + public function __construct(private EmailVerifier $emailVerifier, private string $internalSecret, private ClockInterface $clock) |
34 | 36 | { |
35 | 37 | } |
36 | 38 |
|
@@ -68,16 +70,83 @@ public function register(Request $request, UserPasswordHasherInterface $password |
68 | 70 | ->htmlTemplate('registration/confirmation_email.html.twig') |
69 | 71 | ->textTemplate('registration/confirmation_email.txt.twig') |
70 | 72 | ); |
71 | | - $this->addFlash('success', 'Your account has been created. Please check your email inbox to confirm the account.'); |
72 | 73 |
|
73 | | - return $this->redirectToRoute('home'); |
| 74 | + // Redirect to confirmation page with signed token |
| 75 | + $token = $this->generateRegistrationToken($user); |
| 76 | + |
| 77 | + return $this->redirectToRoute('register_check_email', ['token' => $token]); |
74 | 78 | } |
75 | 79 |
|
76 | 80 | return $this->render('registration/register.html.twig', [ |
77 | 81 | 'registrationForm' => $form, |
78 | 82 | ]); |
79 | 83 | } |
80 | 84 |
|
| 85 | + #[Route(path: '/register/check-email/{token}', name: 'register_check_email')] |
| 86 | + public function checkEmailConfirmation(string $token, UserRepository $userRepository): Response |
| 87 | + { |
| 88 | + $result = $this->validateRegistrationToken($token, $userRepository); |
| 89 | + |
| 90 | + if ($result === null) { |
| 91 | + $this->addFlash('error', 'This link is invalid or has expired. Please register again.'); |
| 92 | + return $this->redirectToRoute('register'); |
| 93 | + } |
| 94 | + |
| 95 | + $form = $this->createForm(UpdateEmailFormType::class, $result['user']); |
| 96 | + |
| 97 | + return $this->render('registration/check_email.html.twig', [ |
| 98 | + 'email' => $result['email'], |
| 99 | + 'token' => $token, |
| 100 | + 'form' => $form, |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + #[Route(path: '/register/resend/{token}', name: 'register_resend', methods: ['POST'])] |
| 105 | + public function resendConfirmation(string $token, Request $request, UserRepository $userRepository, string $mailFromEmail, string $mailFromName): Response |
| 106 | + { |
| 107 | + $result = $this->validateRegistrationToken($token, $userRepository); |
| 108 | + |
| 109 | + if ($result === null) { |
| 110 | + $this->addFlash('error', 'This link is invalid or has expired. Please register again.'); |
| 111 | + return $this->redirectToRoute('register'); |
| 112 | + } |
| 113 | + |
| 114 | + $user = $result['user']; |
| 115 | + $form = $this->createForm(UpdateEmailFormType::class, $user); |
| 116 | + $form->handleRequest($request); |
| 117 | + |
| 118 | + if ($form->isSubmitted() && $form->isValid()) { |
| 119 | + // Persist email change if it was modified |
| 120 | + $this->getEM()->flush(); |
| 121 | + |
| 122 | + // Resend confirmation email |
| 123 | + $this->emailVerifier->sendEmailConfirmation( |
| 124 | + 'register_confirm_email', |
| 125 | + $user, |
| 126 | + new TemplatedEmail() |
| 127 | + ->from(new Address($mailFromEmail, $mailFromName)) |
| 128 | + ->to($user->getEmail()) |
| 129 | + ->subject('Please confirm your email') |
| 130 | + ->htmlTemplate('registration/confirmation_email.html.twig') |
| 131 | + ->textTemplate('registration/confirmation_email.txt.twig') |
| 132 | + ); |
| 133 | + |
| 134 | + // Generate new token with updated email |
| 135 | + $newToken = $this->generateRegistrationToken($user); |
| 136 | + |
| 137 | + $this->addFlash('success', 'Confirmation email has been sent to ' . $user->getEmail()); |
| 138 | + |
| 139 | + return $this->redirectToRoute('register_check_email', ['token' => $newToken]); |
| 140 | + } |
| 141 | + |
| 142 | + // If form is invalid, redisplay the page with errors |
| 143 | + return $this->render('registration/check_email.html.twig', [ |
| 144 | + 'email' => $user->getEmail(), |
| 145 | + 'token' => $token, |
| 146 | + 'form' => $form, |
| 147 | + ]); |
| 148 | + } |
| 149 | + |
81 | 150 | /** |
82 | 151 | * @param BruteForceLoginFormAuthenticator<User> $authenticator |
83 | 152 | */ |
@@ -119,4 +188,57 @@ public function confirmEmail(Request $request, UserRepository $userRepository, U |
119 | 188 |
|
120 | 189 | return $this->redirectToRoute('home'); |
121 | 190 | } |
| 191 | + |
| 192 | + private function generateRegistrationToken(User $user): string |
| 193 | + { |
| 194 | + $timestamp = $this->clock->now()->getTimestamp(); |
| 195 | + $data = $user->getId() . '|' . $user->getEmail() . '|' . $timestamp; |
| 196 | + $signature = hash_hmac('sha256', $data, $this->internalSecret); |
| 197 | + |
| 198 | + return base64_encode($data . '|' . $signature); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * @return array{user: User, email: string}|null |
| 203 | + */ |
| 204 | + private function validateRegistrationToken(string $token, UserRepository $userRepository): ?array |
| 205 | + { |
| 206 | + $decoded = base64_decode($token, true); |
| 207 | + if ($decoded === false) { |
| 208 | + return null; |
| 209 | + } |
| 210 | + |
| 211 | + $parts = explode('|', $decoded); |
| 212 | + if (count($parts) !== 4) { |
| 213 | + return null; |
| 214 | + } |
| 215 | + |
| 216 | + [$userId, $email, $timestamp, $providedSignature] = $parts; |
| 217 | + |
| 218 | + // Check expiration (10 minutes = 600 seconds) |
| 219 | + $now = $this->clock->now()->getTimestamp(); |
| 220 | + if ($now - (int) $timestamp > 600) { |
| 221 | + return null; |
| 222 | + } |
| 223 | + |
| 224 | + // Verify signature |
| 225 | + $data = $userId . '|' . $email . '|' . $timestamp; |
| 226 | + $expectedSignature = hash_hmac('sha256', $data, $this->internalSecret); |
| 227 | + if (!hash_equals($expectedSignature, $providedSignature)) { |
| 228 | + return null; |
| 229 | + } |
| 230 | + |
| 231 | + // Load user |
| 232 | + $user = $userRepository->find((int) $userId); |
| 233 | + if ($user === null) { |
| 234 | + return null; |
| 235 | + } |
| 236 | + |
| 237 | + // Check if user is already enabled |
| 238 | + if ($user->isEnabled()) { |
| 239 | + return null; |
| 240 | + } |
| 241 | + |
| 242 | + return ['user' => $user, 'email' => $email]; |
| 243 | + } |
122 | 244 | } |
0 commit comments