2026-02-04 23:31:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
2026-02-11 19:28:12 +01:00
|
|
|
namespace MintyPHP\Service\Auth;
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
use MintyPHP\Http\Request;
|
2026-02-23 12:58:19 +01:00
|
|
|
use MintyPHP\I18n;
|
|
|
|
|
use MintyPHP\Repository\Auth\EmailVerificationRepository;
|
|
|
|
|
use MintyPHP\Repository\User\UserReadRepository;
|
|
|
|
|
use MintyPHP\Repository\User\UserWriteRepository;
|
add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00
|
|
|
use MintyPHP\Service\Mail\MailService;
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
class EmailVerificationService
|
|
|
|
|
{
|
|
|
|
|
private const CODE_LENGTH = 6;
|
|
|
|
|
private const EXPIRY_MINUTES = 30;
|
|
|
|
|
private const MAX_ATTEMPTS = 5;
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
public function __construct(
|
|
|
|
|
private readonly UserReadRepository $userReadRepository,
|
|
|
|
|
private readonly UserWriteRepository $userWriteRepository,
|
|
|
|
|
private readonly EmailVerificationRepository $emailVerificationRepository,
|
|
|
|
|
private readonly MailService $mailService
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sendVerification(int $userId, ?string $locale = null): array
|
2026-02-04 23:31:53 +01:00
|
|
|
{
|
2026-02-23 12:58:19 +01:00
|
|
|
$user = $this->userReadRepository->find($userId);
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!$user || !isset($user['id'])) {
|
|
|
|
|
return ['ok' => false, 'error' => 'user_not_found'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$email = (string) ($user['email'] ?? '');
|
|
|
|
|
if ($email === '') {
|
|
|
|
|
return ['ok' => false, 'error' => 'email_required'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
$this->emailVerificationRepository->invalidateForUser($userId);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
$code = $this->generateCode();
|
2026-02-04 23:31:53 +01:00
|
|
|
$codeHash = password_hash($code, PASSWORD_DEFAULT);
|
|
|
|
|
$expiresAt = gmdate('Y-m-d H:i:s', time() + (self::EXPIRY_MINUTES * 60));
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
$verificationId = $this->emailVerificationRepository->create($userId, $codeHash, $expiresAt);
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!$verificationId) {
|
|
|
|
|
return ['ok' => false, 'error' => 'create_failed'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$locale = $locale ?: ($user['locale'] ?? null) ?: (I18n::$locale ?? I18n::$defaultLocale);
|
|
|
|
|
$name = trim(($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? ''));
|
|
|
|
|
$isGerman = strpos((string) $locale, 'de') === 0;
|
|
|
|
|
$greeting = $isGerman ? 'Hallo' : 'Hello';
|
|
|
|
|
if ($name !== '') {
|
|
|
|
|
$greeting .= ' ' . $name;
|
|
|
|
|
}
|
|
|
|
|
$greeting .= ',';
|
|
|
|
|
$verifyPath = Request::withLocale('verify-email', $locale);
|
|
|
|
|
$verifyUrl = appUrl($verifyPath);
|
|
|
|
|
$previousLocale = I18n::$locale ?? null;
|
|
|
|
|
I18n::$locale = $locale;
|
|
|
|
|
$subject = t('Email verification code');
|
|
|
|
|
I18n::$locale = $previousLocale;
|
|
|
|
|
|
|
|
|
|
$vars = [
|
|
|
|
|
'app_name' => appTitle(),
|
|
|
|
|
'app_logo_url' => appLogoUrlAbsolute(128),
|
|
|
|
|
'imprint_url' => appUrl(Request::withLocale('imprint', $locale)),
|
|
|
|
|
'privacy_url' => appUrl(Request::withLocale('privacy', $locale)),
|
|
|
|
|
'code' => $code,
|
|
|
|
|
'expires_minutes' => self::EXPIRY_MINUTES,
|
|
|
|
|
'verify_url' => $verifyUrl,
|
|
|
|
|
'greeting' => $greeting,
|
|
|
|
|
];
|
2026-02-23 12:58:19 +01:00
|
|
|
$this->mailService->sendTemplate('email_verification', $vars, $email, $subject, $locale);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
return ['ok' => true];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
public function verifyCode(string $email, string $code): array
|
2026-02-04 23:31:53 +01:00
|
|
|
{
|
|
|
|
|
$email = trim($email);
|
|
|
|
|
$code = trim($code);
|
|
|
|
|
if ($email === '' || $code === '') {
|
|
|
|
|
return ['ok' => false, 'error' => 'invalid'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
$user = $this->userReadRepository->findByEmail($email);
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!$user || !isset($user['id'])) {
|
|
|
|
|
return ['ok' => false, 'error' => 'invalid'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userId = (int) $user['id'];
|
|
|
|
|
|
|
|
|
|
// Check if already verified
|
|
|
|
|
if (!empty($user['email_verified_at'])) {
|
|
|
|
|
return ['ok' => false, 'error' => 'already_verified'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
$verification = $this->emailVerificationRepository->findActiveByUserId($userId);
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!$verification || !isset($verification['id'])) {
|
|
|
|
|
return ['ok' => false, 'error' => 'invalid'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attempts = (int) ($verification['attempts'] ?? 0);
|
|
|
|
|
if ($attempts >= self::MAX_ATTEMPTS) {
|
|
|
|
|
return ['ok' => false, 'error' => 'too_many_attempts'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$hash = (string) ($verification['code_hash'] ?? '');
|
|
|
|
|
if ($hash === '' || !password_verify($code, $hash)) {
|
2026-02-23 12:58:19 +01:00
|
|
|
$this->emailVerificationRepository->incrementAttempts((int) $verification['id']);
|
2026-02-04 23:31:53 +01:00
|
|
|
return ['ok' => false, 'error' => 'invalid'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mark verification as used
|
2026-02-23 12:58:19 +01:00
|
|
|
$this->emailVerificationRepository->markUsed((int) $verification['id']);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
// Mark user email as verified
|
2026-02-23 12:58:19 +01:00
|
|
|
$this->userWriteRepository->setEmailVerified($userId);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
return ['ok' => true, 'user_id' => $userId];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
public function resendVerification(string $email, ?string $locale = null): array
|
2026-02-04 23:31:53 +01:00
|
|
|
{
|
|
|
|
|
$email = trim($email);
|
|
|
|
|
if ($email === '') {
|
|
|
|
|
return ['ok' => false, 'error' => 'email_required'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
$user = $this->userReadRepository->findByEmail($email);
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!$user || !isset($user['id'])) {
|
|
|
|
|
// Don't reveal if user exists
|
|
|
|
|
return ['ok' => true];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if already verified
|
|
|
|
|
if (!empty($user['email_verified_at'])) {
|
|
|
|
|
return ['ok' => false, 'error' => 'already_verified'];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
return $this->sendVerification((int) $user['id'], $locale);
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
public function getExpiryMinutes(): int
|
2026-02-04 23:31:53 +01:00
|
|
|
{
|
|
|
|
|
return self::EXPIRY_MINUTES;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-23 12:58:19 +01:00
|
|
|
private function generateCode(): string
|
2026-02-04 23:31:53 +01:00
|
|
|
{
|
|
|
|
|
$max = (10 ** self::CODE_LENGTH) - 1;
|
|
|
|
|
$code = (string) random_int(0, $max);
|
|
|
|
|
return str_pad($code, self::CODE_LENGTH, '0', STR_PAD_LEFT);
|
|
|
|
|
}
|
|
|
|
|
}
|