-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
185 lines (157 loc) · 6.21 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Carbon\Carbon;
use Dotenv\Dotenv;
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['result' => 'Sorry. This method is not accepted.']);
exit(0);
}
$dotEnv = Dotenv::create(__DIR__);
$dotEnv->load();
$databaseDriver = getenv('DATABASE_DRIVER');
$databaseName = getenv('DATABASE_NAME');
$databaseUser = getenv('DATABASE_USER');
$databasePassword = getenv('DATABASE_PASSWORD');
$action = $_POST['action'] ?? 'none';
if ($action === 'none') {
echo json_encode(['result' => 'Sorry. The action is missing.']);
exit(0);
}
if ($action === 'login') {
$user = $_POST['account'] ?? 'none';
$password = $_POST['password'] ?? 'none';
$sql = 'select account, password from accounts where account = :account';
$dsn = sprintf("mysql:host=localhost;dbname=%s;charset=utf8mb4", $databaseName);
$options = [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
try {
$pdo = new PDO($dsn, $databaseUser, $databasePassword, $options);
$stmt = $pdo->prepare($sql);
$stmt->execute([':account' => $user]);
$result = (array) $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($result) !== 1) {
echo json_encode(['result' => 'Account Auth is failed.']);
exit(0);
} else {
$hashedPassword = $result[0]['password'];
if (password_verify($password, $hashedPassword) === false) {
echo json_encode(['result' => 'Password Auth is failed.']);
exit(0);
}
$expired = new Carbon('Asia/Taipei');
$expiredDate = $expired->addDays(1)->format('Y-m-d H:m:s');
$token = hash('sha512', $expiredDate);
$sql = 'insert into tokens(token, account, expired) values(:token, :account, :expired)';
$stmt = $pdo->prepare($sql);
$stmt->execute([':token' => $token, ':account' => $user, ':expired' => $expiredDate]);
$result = (array) $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(['result' => 'Auth is successful.', 'token' => $token]);
exit(0);
}
$stmt = null;
$pdo = null;
} catch (\Exception $e) {
error_log($e->getMessage());
echo json_encode(['result' => 'Connection is failed.']);
exit(0);
}
if ($user === 'none' || $password === 'none') {
echo json_encode(['result' => 'The user or password is missing']);
exit(0);
}
} else if ($action === 'logout') {
$dsn = sprintf("mysql:host=localhost;dbname=%s;charset=utf8mb4", $databaseName);
$options = [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
$user = $_POST['account'] ?? 'none';
$token = $_POST['token'] ?? 'none';
if ($user === 'none') {
echo json_encode(['result' => 'Account is missing.']);
exit(0);
}
if ($token === 'none') {
echo json_encode(['result' => 'Token is missing.']);
exit(0);
}
try {
$sql = 'select count(*) as count from tokens where account = :account and token = :token';
$pdo = new PDO($dsn, $databaseUser, $databasePassword, $options);
$stmt = $pdo->prepare($sql);
$stmt->execute([':account' => $user, ':token' => $token]);
$result = (array) $stmt->fetchAll(PDO::FETCH_ASSOC);
$result = $result[0]['count'];
if ($result === 0) {
echo json_encode(['result' => 'Token is invalid.']);
exit(0);
} else {
$sql = 'delete from tokens where token = :token';
$stmt = $pdo->prepare($sql);
$stmt->execute([':token' => $token]);
echo json_encode(['result' => 'Logout is done.']);
exit(0);
}
$stmt = null;
$pdo = null;
} catch (\Exception $e) {
error_log($e->getMessage());
echo json_encode(['result' => 'Connection is failed.']);
exit(0);
}
} else if ($action === 'status') {
$dsn = sprintf("mysql:host=localhost;dbname=%s;charset=utf8mb4", $databaseName);
$options = [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
$user = $_POST['account'] ?? 'none';
$token = $_POST['token'] ?? 'none';
if ($user === 'none') {
echo json_encode(['result' => 'Account is missing.']);
exit(0);
}
if ($token === 'none') {
echo json_encode(['result' => 'Token is missing.']);
exit(0);
}
try {
$sql = 'select count(*) as count from tokens where token = :token and account = :account';
$pdo = new PDO($dsn, $databaseUser, $databasePassword, $options);
$stmt = $pdo->prepare($sql);
$stmt->execute([':token' => $token, ':account' => $user]);
$result = (array) $stmt->fetchAll(PDO::FETCH_ASSOC);
$result = $result[0]['count'];
if ($result === 0) {
echo json_encode(['result' => 'It is not verified and should run logout action.']);
} else {
$expiredDate = $result[0]['expired'];
$expiredTimestamp = Carbon::parse($expiredDate)->timestamp;
$dateTimestamp = Carbon::now()->timestamp;
if ($expiredTimestamp < $dateTimestamp) {
$sql = 'delete from tokens where token = :token and account = :account';
$stmt = $pdo->prepare($sql);
$stmt->execute([':token' => $token, ':account' => $user]);
echo json_encode(['result' => 'Token is expired. It should be logout.']);
exit(0);
} else {
echo json_encode(['result' => 'Token is live.']);
exit(0);
}
}
$stmt = null;
$pdo = null;
} catch (\Exception $e) {
error_log($e->getMessage());
echo json_encode(['result' => 'Connection is failed.']);
exit(0);
}
} else {
echo json_encode(['result' => 'Invalid actions.']);
exit(0);
}