This repository was archived by the owner on Nov 1, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathFakeAuth.php
More file actions
215 lines (199 loc) · 7.86 KB
/
Copy pathFakeAuth.php
File metadata and controls
215 lines (199 loc) · 7.86 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/***
*** FakeAuth - Minecraft Yggrasil Authentication Server
***
*** FakeAuth.php - Global settings and functions
***/
class FakeAuth {
public $dbConn;
private $rootDir;
private $configFile;
private $errCodes = [
400 => '{"error":"Unsupported Media Type","errorMessage":"The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method"}',
401 => '{"error":"ForbiddenOperationException","errorMessage":"Invalid credentials. Invalid username or password."}',
403 => '{"error":"IllegalArgumentException","errorMessage":"credentials is null"}',
404 => '{"error":"Not Found","errorMessage":"The server has not found anything matching the request URI"}',
405 => '{"error":"Method Not Allowed","errorMessage":"The method specified in the request is not allowed for the resource identified by the request URI"}',
'4002' => '{"error":"IllegalArgumentException","errorMessage":"Invalid timestamp."}'
];
public $endpoint;
public $config;
public $log;
public $users;
function __construct (string $ep = 'general') {
$this->endpoint = $ep;
$this->rootDir = dirname(__FILE__);
$this->configFile = '../config.php';
require_once($this->configFile);
$rawConfig = getConfig();
if ($ep == 'general') $this->log = 'FA_';
else if ($ep == 'index') $this->log = 'WB_';
else $this->log = $rawConfig['loggingPrefix'][$this->endpoint];
$dbInfo = $rawConfig['db'] or die('Failed to load configuration');
$this->dbConn = new mysqli($dbInfo['host'], $dbInfo['user'], $dbInfo['pass'], $dbInfo['name']);
if ($this->dbConn->connect_errno) {
print "Error: Failed to make a MySQL connection: \n";
print 'Errno: ' . $this->dbConn->connect_errno . "\n";
print 'Error: ' . $this->dbConn->connect_error . "\n";
die('MySQL Connection Error');
}
}
function __destruct () {
$this->dbConn->close();
}
public function genUUID ($dashes = true) {
if ($dashes) return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
else return sprintf( '%04x%04x%04x%04x%04x%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
public function errOut (int $errCode = 404) {
if (!isset($this->errCodes[$errCode])) $errCode = 404;
http_response_code($errCode);
echo $this->errCodes[$errCode];
exit;
}
public function checkInput (string $method = 'POST', bool $checkType = true) {
if ($_SERVER['REQUEST_METHOD'] != $method) $this->errOut(405);
if ($checkType && $_SERVER['CONTENT_TYPE'] != 'application/json') $this->errOut(400);
}
public function getInput ($checking = false) {
$sh = fopen('php://input', 'r');
$rawInput = stream_get_contents($sh);
$input = json_decode($rawInput);
fclose($sh);
if (!$input) {
if ($checking) $this->errOut(403);
else return 403;
}
return $input;
}
public function hash (string $password) {
return hash("sha384", $password);
}
public function getUserByClientToken (string $tokenVal) {
$tokenVal = $this->dbConn->real_escape_string(htmlspecialchars(strip_tags($tokenVal)));
$sql = "SELECT * FROM `users` WHERE `clientToken` = '$tokenVal'";
if (!$result = $this->dbConn->query($sql)) {
// Get the error information
print "Error: Query failed to execute: \n";
print "Query: " . $sql . "\n";
print "Errno: " . $this->dbConn->errno . "\n";
print "Error: " . $this->dbConn->error . "\n";
exit;
}
if ($result->num_rows === 0) return false;
return $result->fetch_assoc();
}
public function getUserByAccessToken (string $tokenVal) {
$tokenVal = $this->dbConn->real_escape_string(htmlspecialchars(strip_tags($tokenVal)));
$sql = "SELECT * FROM `users` WHERE `accessToken` = '$tokenVal'";
if (!$result = $this->dbConn->query($sql)) {
// Get the error information
print "Error: Query failed to execute: \n";
print "Query: " . $sql . "\n";
print "Errno: " . $this->dbConn->errno . "\n";
print "Error: " . $this->dbConn->error . "\n";
exit;
}
if ($result->num_rows === 0) return false;
return $result->fetch_assoc();
}
public function getUserByUUID (string $uuidVal) {
$uuidVal = $this->dbConn->real_escape_string(htmlspecialchars(strip_tags($uuidVal)));
$sql = "SELECT * FROM `users` WHERE REPLACE(clientToken, \"-\", \"\") = '$uuidVal'";
if (!$result = $this->dbConn->query($sql)) {
// Get the error information
print "Error: Query failed to execute: \n";
print "Query: " . $sql . "\n";
print "Errno: " . $this->dbConn->errno . "\n";
print "Error: " . $this->dbConn->error . "\n";
exit;
}
if ($result->num_rows === 0) return false;
return $result->fetch_assoc();
}
public function getUserByDisplayName (string $dNameVal) {
$dNameVal = $this->dbConn->real_escape_string(htmlspecialchars(strip_tags($dNameVal)));
$sql = "SELECT * FROM `users` WHERE `displayName` = '$dNameVal'";
if (!$result = $this->dbConn->query($sql)) {
// Get the error information
print "Error: Query failed to execute: \n";
print "Query: " . $sql . "\n";
print "Errno: " . $this->dbConn->errno . "\n";
print "Error: " . $this->dbConn->error . "\n";
exit;
}
if ($result->num_rows === 0) return false;
return $result->fetch_assoc();
}
public function getUserByName (string $name) {
$name = $this->dbConn->real_escape_string(htmlspecialchars(strip_tags($name)));
$sql = "SELECT * FROM `users` WHERE `username` = '$name'";
if (!$result = $this->dbConn->query($sql)) {
// Get the error information
print "Error: Query failed to execute: \n";
print "Query: " . $sql . "\n";
print "Errno: " . $this->dbConn->errno . "\n";
print "Error: " . $this->dbConn->error . "\n";
exit;
}
if ($result->num_rows === 0) return false;
return $result->fetch_assoc();
}
public function getAllUsers () {
if (!$result = $this->dbConn->query("SELECT * FROM `users`")) {
// Get the error information
print "Error: Query failed to execute: \n";
print 'Query: ' . $sql . "\n";
print 'Errno: ' . $this->dbConn->errno . "\n";
print 'Error: ' . $this->dbConn->error . "\n";
exit;
}
if ($result->num_rows === 0) return false;
$allUsers = [];
while ($user = mysqli_fetch_assoc($result)) $allUsers[] = $user;
return $allUsers;
}
public function setUser (string $name, $userInfo) {
$sql = "UPDATE `users` SET ";
foreach ($userInfo as $key => $val) {
if (!in_array($key, ['password', 'displayName', 'clientToken', 'accessToken', 'skinPath', 'capePath'])) continue;
$sql .= "`$key` = '$val', ";
}
$sql = substr($sql, 0, -2);
$sql .= " WHERE `username` = '$name'";
return $this->dbConn->query($sql) ? TRUE : $conn->error;
}
public function newUser (string $name, string $pass, string $dispName) {
$pass = $this->hash($pass);
$clientToken = $this->GenUUID();
$sql = "INSERT INTO `users` (`username`,`password`,`clientToken`,`displayName`) VALUES ('$name','$pass','$clientToken','$dispName')";
return $this->dbConn->query($sql) ? TRUE : $this->dbConn->error;
}
}
?>