summaryrefslogtreecommitdiff
path: root/http/public/do/verify_otp.php
diff options
context:
space:
mode:
authorJoe Carstairs <me@joeac.net>2026-06-04 15:14:25 +0100
committerJoe Carstairs <me@joeac.net>2026-06-04 15:16:34 +0100
commit171c957c40172a994189cdc1824421b3a9bd379b (patch)
tree179ce57eef7d02c1d8a76e0a4bc32d895acdf75f /http/public/do/verify_otp.php
parent231ffbd0e88735bce6b7f337a09b99b07a214c86 (diff)
http: adds php api routes under do/
Diffstat (limited to 'http/public/do/verify_otp.php')
-rw-r--r--http/public/do/verify_otp.php63
1 files changed, 63 insertions, 0 deletions
diff --git a/http/public/do/verify_otp.php b/http/public/do/verify_otp.php
new file mode 100644
index 0000000..7f86858
--- /dev/null
+++ b/http/public/do/verify_otp.php
@@ -0,0 +1,63 @@
+<?php
+
+use JoeacNet\Http\Db;
+
+require_once __DIR__ . "/../../php/db.php";
+
+$db = Db\connectDb();
+
+$payload = json_decode(file_get_contents("php://input"), true);
+$guess = getGuessFromPayloadAndValidate($payload);
+$leniencySecs = getLeniencySecsFromPayloadAndValidate($payload);
+$userId = getUserIdFromPayloadAndValidate($payload);
+
+if (
+ !Db\isOtpCorrect(
+ db: $db,
+ userId: $userId,
+ guess: $guess,
+ leniencySecs: $leniencySecs,
+ )
+) {
+ http_response_code(400);
+ echo "OTP is not valid";
+ exit();
+}
+Db\deleteAllOtpsForUser($db, $userId);
+$token = bin2hex(random_bytes(256));
+Db\insertSendEmailToken(db: $db, userId: $userId, token: $token);
+http_response_code(200);
+echo $token;
+exit();
+
+/// functions ///
+
+function getGuessFromPayloadAndValidate(array $payload): string
+{
+ $guess = (string) $payload["guess"];
+ if (strlen($guess) != 6) {
+ http_response_code(400);
+ echo "guess must be six characters long";
+ exit();
+ }
+ return $guess;
+}
+
+function getLeniencySecsFromPayloadAndValidate(array $payload): int
+{
+ if ((bool) $payload["lenient"] ?? false) {
+ return 60;
+ }
+ return 0;
+}
+
+function getUserIdFromPayloadAndValidate(array $payload): string
+{
+ $userId = (string) $payload["userId"];
+ if (is_null($userId) || $userId == "") {
+ http_response_code(400);
+ echo "userId must not be empty";
+ exit();
+ }
+ return $userId;
+}