-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.php
70 lines (62 loc) · 1.82 KB
/
verify.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
require 'vendor/autoload.php';
use PragmaRX\Google2FA\Google2FA;
session_start();
// Ensure session data is present
if (!isset($_SESSION['user'], $_SESSION['user']['google2fa_secret'], $_SESSION['user_type'])) {
echo json_encode([
'result' => false,
'message' => 'Session data is incomplete.',
'debug' => $_SESSION, // Debug the session for troubleshooting
]);
exit();
}
// Retrieve session data
$user_secret = $_SESSION['user']['google2fa_secret'];
$user_type = $_SESSION['user_type'];
// Check if the OTP was provided via POST
if (isset($_POST['otp'])) {
$otp = $_POST['otp'];
// Input validation
if (!preg_match('/^\d{6}$/', $otp)) {
echo json_encode([
'result' => false,
'message' => 'Invalid OTP format. OTP must be a 6-digit number and do not have speacial characters.',
]);
exit();
}
// Validate the OTP
$googleOTP = new Google2FA();
if ($googleOTP->verifyKey($user_secret, $otp)) {
// Prepare the redirect URL based on user type
$redirect_url = null;
if ($user_type === 'admin') {
$redirect_url = 'admin_page.php';
} elseif ($user_type === 'user') {
$redirect_url = 'user_page.php';
}
if ($redirect_url) {
echo json_encode([
'result' => true,
'redirect' => $redirect_url,
]);
} else {
echo json_encode([
'result' => false,
'message' => 'Unknown user type.',
]);
}
} else {
echo json_encode([
'result' => false,
'message' => 'Invalid OTP.',
]);
}
exit();
} else {
echo json_encode([
'result' => false,
'message' => 'No OTP provided.',
]);
exit();
}