Request AuthHandler (#218)

[FEATURE] RequestAuthHandler и php скрипт к нему
This commit is contained in:
sasha0552 2019-04-12 06:01:17 +06:00 committed by Gravit
parent 0201c8d474
commit 2180bd989e
3 changed files with 191 additions and 0 deletions

View file

@ -43,6 +43,7 @@ public static void registerHandlers() {
registerHandler("json", JsonAuthHandler.class);
registerHandler("memory", MemoryAuthHandler.class);
registerHandler("mysql", MySQLAuthHandler.class);
registerHandler("request", RequestAuthHandler.class);
registredHandl = true;
}
}

View file

@ -0,0 +1,110 @@
package ru.gravit.launchserver.auth.handler;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;
public final class RequestAuthHandler extends CachedAuthHandler {
private String url;
private String secretKey;
private String typeLine;
private String uuidLine;
private String usernameLine;
private String accessTokenLine;
private String serverIDLine;
private String secretKeyLine;
private String TypeFetchByUUID;
private String TypeFetchByUsername;
private String TypeSetAccessTokenAndUUID;
private String TypeSetServerID;
private String splitSymbol;
@Override
public void init() {
if (url == null)
LogHelper.error("[Verify][AuthHandler] url cannot be null");
if (secretKey == null)
LogHelper.error("[Verify][AuthHandler] secretKey cannot be null");
// Default
if (typeLine == null)
typeLine = "type";
if (uuidLine == null)
uuidLine = "uuid";
if (usernameLine == null)
usernameLine = "username";
if (accessTokenLine == null)
accessTokenLine = "accessToken";
if (serverIDLine == null)
serverIDLine = "serverID";
if (secretKeyLine == null)
secretKeyLine = "secretKey";
if (TypeFetchByUUID == null)
TypeFetchByUUID = "FetchByUUID";
if (TypeFetchByUsername == null)
TypeFetchByUsername = "FetchByUsername";
if (TypeSetAccessTokenAndUUID == null)
TypeSetAccessTokenAndUUID = "SetAccessTokenAndUUID";
if (TypeSetServerID == null)
TypeSetServerID = "SetServerID";
if (splitSymbol == null)
splitSymbol = ":";
}
@Override
protected Entry fetchEntry(UUID uuid) throws IOException {
String response = IOHelper.request(new URL(url + "?" + IOHelper.urlEncode(typeLine) + "=" + TypeFetchByUUID + "&" + secretKeyLine + "=" + IOHelper.urlEncode(secretKey) + "&" + IOHelper.urlEncode(uuidLine) + "=" + IOHelper.urlEncode(uuid.toString())));
String[] parts = response.split(splitSymbol);
String username = parts[0];
String accessToken = parts[1];
String serverID = parts[2];
LogHelper.debug("[AuthHandler] Getted username: " + username);
LogHelper.debug("[AuthHandler] Getted accessToken: " + accessToken);
LogHelper.debug("[AuthHandler] Getted serverID: " + serverID);
LogHelper.debug("[AuthHandler] Getted UUID: " + uuid);
return query(uuid, username, accessToken, serverID);
}
@Override
protected Entry fetchEntry(String username) throws IOException {
String response = IOHelper.request(new URL(url + "?" + IOHelper.urlEncode(typeLine) + "=" + TypeFetchByUsername + "&" + secretKeyLine + "=" + IOHelper.urlEncode(secretKey) + "&" + IOHelper.urlEncode(usernameLine) + "=" + IOHelper.urlEncode(username)));
String[] parts = response.split(splitSymbol);
UUID uuid = UUID.fromString(parts[0]);
String accessToken = parts[1];
String serverID = parts[2];
LogHelper.debug("[AuthHandler] Getted username: " + username);
LogHelper.debug("[AuthHandler] Getted accessToken: " + accessToken);
LogHelper.debug("[AuthHandler] Getted serverID: " + serverID);
LogHelper.debug("[AuthHandler] Getted UUID: " + uuid);
return query(uuid, username, accessToken, serverID);
}
private Entry query(UUID uuid, String username, String accessToken, String serverID) {
return new Entry(uuid, username, accessToken, serverID);
}
@Override
protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException {
String response = IOHelper.request(new URL(url + "?" + IOHelper.urlEncode(typeLine) + "=" + TypeSetAccessTokenAndUUID + "&" + secretKeyLine + "=" + IOHelper.urlEncode(secretKey) + "&" + IOHelper.urlEncode(uuidLine) + "=" + IOHelper.urlEncode(uuid.toString()) + "&" + IOHelper.urlEncode(accessTokenLine) + "=" + IOHelper.urlEncode(accessToken) + "&" + IOHelper.urlEncode(usernameLine) + "=" + IOHelper.urlEncode(username)));
LogHelper.debug("[AuthHandler] Set accessToken: " + accessToken);
LogHelper.debug("[AuthHandler] Set UUID: " + uuid);
LogHelper.debug("[AuthHandler] For this username: " + username);
return response.equals("OK");
}
@Override
protected boolean updateServerID(UUID uuid, String serverID) throws IOException {
String response = IOHelper.request(new URL(url + "?" + IOHelper.urlEncode(typeLine) + "=" + TypeSetServerID + "&" + secretKeyLine + "=" + IOHelper.urlEncode(secretKey) + "&" + IOHelper.urlEncode(uuidLine) + "=" + IOHelper.urlEncode(uuid.toString()) + "&" + IOHelper.urlEncode(serverIDLine) + "=" + IOHelper.urlEncode(serverID)));
LogHelper.debug("[AuthHandler] Set serverID: " + serverID);
LogHelper.debug("[AuthHandler] For this UUID: " + uuid);
return response.equals("OK");
}
@Override
public void close() {
}
}

View file

@ -0,0 +1,80 @@
<?php
//Секретный ключ. Внимание! должен совпадать с ключем в лаунчсервере. Пожалуйста, смените его, иначе это ставит под угрозу проект.
$secretkey = '12345678';
//Настройки связи с базой данных
$link = mysqli_connect(
'localhost', // Хост
'root', // Пользователь
'', // Пароль
'test' // База данных
);
// Настройка таблицы
$settings = [
'table' => "dle_users", // Название таблицы
'usernameColumn' => "name", // Столбец с именами пользователей
'uuidColumn' => "uuid", // Столбец с uuid
'accessTokenColumn' => "accessToken", // Столбец с accessToken
'ServerIDColumn' => "serverID" // Столбец с serverID
];
// Не трогать
// Можно повредить скрипт
$AuthHandler = [
'type' => filter_input(INPUT_GET, 'type', FILTER_SANITIZE_STRING),
'username' => filter_input(INPUT_GET, 'username', FILTER_SANITIZE_STRING),
'uuid' => filter_input(INPUT_GET, 'uuid', FILTER_SANITIZE_STRING),
'accessToken' => filter_input(INPUT_GET, 'accessToken', FILTER_SANITIZE_STRING),
'ServerID' => filter_input(INPUT_GET, 'ServerID', FILTER_SANITIZE_STRING),
'secretKey' => filter_input(INPUT_GET, 'secretKey', FILTER_SANITIZE_STRING)
];
if (!isset($AuthHandler['secretKey'])) {
die('Не указан ключ!');
}
if ($secretkey != $AuthHandler['secretKey']) {
die('Неверный ключ!');
}
if(!$link) {
die('Ошибка подключения к базе данных');
}
if(isset($AuthHandler['type'])) {
if($AuthHandler['type'] == "FetchByUUID") {
if(isset($AuthHandler['uuid'])) {
$result = mysqli_query($link, 'SELECT '.$settings['usernameColumn'].','.$settings['accessTokenColumn'].','.$settings['serverID'].' FROM '.$settings['table'].' WHERE '.$settings['uuidColumn'].'="'.$AuthHandler['uuid'].'" LIMIT 1') or die($link->error);
$row = $result->fetch_assoc();
mysqli_free_result($result);
mysqli_close($link);
die($row[$settings['usernameColumn']] + ':' + $row[$settings['accessTokenColumn']] + ':' + $row[$settings['serverID']]);
}
if($AuthHandler['type'] == "FetchByUsername") {
if(isset($AuthHandler['uuid'])) {
$result = mysqli_query($link, 'SELECT '.$settings['uuidColumn'].','.$settings['accessTokenColumn'].','.$settings['serverID'].' FROM '.$settings['table'].' WHERE '.$settings['usernameColumn'].'="'.$AuthHandler['username'].'" LIMIT 1') or die($link->error);
$row = $result->fetch_assoc();
mysqli_free_result($result);
mysqli_close($link);
die($row[$settings['uuidColumn']] + ':' + $row[$settings['accessTokenColumn']] + ':' + $row[$settings['serverID']]);
}
// Обновление строк
if($AuthHandler['type'] == "SetAccessTokenAndUUID") {
$result = mysqli_query($link, 'UPDATE '.$settings['table'].' SET '.$settings['accessTokenColumn'].'="'.$AuthHandler['accessToken'].'" WHERE '.$settings['usernameColumn'].'="'.$AuthHandler['username'].'"') or die($link->error);
$result1 = mysqli_query($link, 'UPDATE '.$settings['table'].' SET '.$settings['uuidColumn'].'="'.$AuthHandler['uuid'].'" WHERE '.$settings['usernameColumn'].'="'.$AuthHandler['username'].'"') or die($link->error);
mysqli_close($link);
die('OK');
}
if($AuthHandler['type'] == "SetServerID") {
$result = mysqli_query($link, 'UPDATE '.$settings['table'].' SET '.$settings['ServerIDColumn'].'="'.$AuthHandler['serverID'].'" WHERE '.$settings['uuidColumn'].'="'.$AuthHandler['uuid'].'"') or die($link->error);
mysqli_close($link);
die('OK');
}
die('FAIL!');
} else {
die('Type not set!');
}
?>