verify-otp.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Security\Csrf;
  5. use App\Security\FormAccess;
  6. require dirname(__DIR__) . '/src/autoload.php';
  7. Bootstrap::init();
  8. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  9. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Method not allowed'], 405);
  10. }
  11. $csrf = $_POST['csrf'] ?? '';
  12. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  13. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Ungültiges CSRF-Token.'], 419);
  14. }
  15. if (trim((string) ($_POST['website'] ?? '')) !== '') {
  16. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Anfrage blockiert.'], 400);
  17. }
  18. $email = strtolower(trim((string) ($_POST['email'] ?? '')));
  19. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  20. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Bitte gültige E-Mail eingeben.'], 422);
  21. }
  22. $code = trim((string) ($_POST['otp_code'] ?? ''));
  23. $formAccess = new FormAccess();
  24. $result = $formAccess->verifyOtp($email, $code);
  25. if (($result['ok'] ?? false) !== true) {
  26. $reason = (string) ($result['reason'] ?? '');
  27. Bootstrap::jsonResponse([
  28. 'ok' => false,
  29. 'message' => (string) ($result['message'] ?? 'Code konnte nicht bestätigt werden.'),
  30. 'auth_required' => in_array($reason, ['auth_required', 'expired', 'attempt_limit'], true),
  31. 'auth_expired' => false,
  32. 'attempts_left' => isset($result['attempts_left']) ? (int) $result['attempts_left'] : null,
  33. ], (int) ($result['status_code'] ?? 422));
  34. }
  35. Bootstrap::jsonResponse([
  36. 'ok' => true,
  37. 'message' => 'E-Mail erfolgreich bestätigt.',
  38. ]);