new authentication system
This commit is contained in:
@@ -22,7 +22,6 @@ public class SaturnClient implements ModInitializer {
|
||||
public void onInitialize() {
|
||||
LOGGER.info("Initializing " + MOD_ID);
|
||||
|
||||
// Attempt authentication before initializing other features
|
||||
KeyInputHandler.register();
|
||||
if (SaturnApi.authenticate()) {
|
||||
Cloaks.initialize();
|
||||
|
||||
@@ -1,28 +1,23 @@
|
||||
package org.saturnclient.saturnclient.auth;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import org.saturnclient.saturnclient.SaturnClient;
|
||||
import org.saturnclient.saturnclient.cloaks.Cloaks;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SaturnApi {
|
||||
private static final String URL = "http://localhost:3000";
|
||||
|
||||
private static String token = null;
|
||||
private static final String server = "127.0.0.1";
|
||||
private static final int port = 8080;
|
||||
|
||||
public static Map<String, SaturnPlayer> players = new HashMap<>();
|
||||
public static Map<String, String> playerNames = new HashMap<>();
|
||||
public static Socket socket;
|
||||
|
||||
public static boolean authenticate() {
|
||||
try {
|
||||
@@ -34,47 +29,32 @@ public class SaturnApi {
|
||||
|
||||
String accessToken = client.getSession().getAccessToken();
|
||||
|
||||
token = URLEncoder.encode(accessToken, StandardCharsets.UTF_8);
|
||||
SaturnClient.LOGGER.error(accessToken);
|
||||
|
||||
String authUrl = String.format("%s/auth?token=%s", URL, token);
|
||||
try {
|
||||
socket = new Socket(server, port);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
|
||||
writer.println(accessToken);
|
||||
writer.flush();
|
||||
|
||||
URL url = URI.create(authUrl).toURL();
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
String response = reader.readLine();
|
||||
SaturnParser parser = new SaturnParser(response);
|
||||
|
||||
BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream()));
|
||||
StringBuilder response = new StringBuilder();
|
||||
String line;
|
||||
String uuid = parser.getString("uuid");
|
||||
String cloak = parser.getString("cloak");
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
JsonObject jsonResponse = JsonParser.parseString(response.toString()).getAsJsonObject();
|
||||
boolean success = jsonResponse.get("success").getAsBoolean();
|
||||
String uuid = jsonResponse.get("uuid").getAsString().replace("-", "");
|
||||
String name = jsonResponse.get("name").getAsString();
|
||||
|
||||
playerNames.put(name, uuid);
|
||||
|
||||
if (success) {
|
||||
SaturnClient.LOGGER.info("Successfully authenticated");
|
||||
}
|
||||
|
||||
// Update available cloaks list
|
||||
Cloaks.availableCloaks.clear();
|
||||
jsonResponse.get("cloaks").getAsJsonArray()
|
||||
.forEach(element -> Cloaks.availableCloaks.add(element.getAsString()));
|
||||
|
||||
String cloak = jsonResponse.get("cloak").getAsString();
|
||||
if (cloak != null) {
|
||||
SaturnClient.LOGGER.info("Setting cloak to " + cloak + " for " + uuid);
|
||||
MinecraftClient.getInstance().execute(() -> Cloaks.setCloak(uuid, cloak));
|
||||
}
|
||||
|
||||
return success;
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
SaturnClient.LOGGER.error("Authentication failed", e);
|
||||
return false;
|
||||
@@ -82,38 +62,13 @@ public class SaturnApi {
|
||||
}
|
||||
|
||||
public static void setCloak(String cloak) {
|
||||
if (token == null) {
|
||||
SaturnClient.LOGGER.error("No token found");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String setCloakUrl = String.format("%s/cloak/%s?token=%s", URL, cloak, token);
|
||||
|
||||
URL url = URI.create(setCloakUrl).toURL();
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
|
||||
BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream()));
|
||||
StringBuilder response = new StringBuilder();
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.append(line);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
|
||||
JsonObject jsonResponse = JsonParser.parseString(response.toString()).getAsJsonObject();
|
||||
boolean success = jsonResponse.get("success").getAsBoolean();
|
||||
|
||||
if (success) {
|
||||
SaturnClient.LOGGER.info("Successfully set cloak");
|
||||
} else {
|
||||
SaturnClient.LOGGER.error("Failed to set cloak");
|
||||
SaturnClient.LOGGER.error(jsonResponse.get("error").getAsString());
|
||||
}
|
||||
// if (success) {
|
||||
// SaturnClient.LOGGER.info("Successfully set cloak");
|
||||
// } else {
|
||||
// SaturnClient.LOGGER.error("Failed to set cloak");
|
||||
// SaturnClient.LOGGER.error(jsonResponse.get("error").getAsString());
|
||||
// }
|
||||
|
||||
} catch (Exception e) {
|
||||
SaturnClient.LOGGER.error("Failed to set cloak", e);
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.saturnclient.saturnclient.auth;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SaturnParser {
|
||||
private Map<String, String> params = new HashMap<>();
|
||||
|
||||
public SaturnParser(String input) throws IOException {
|
||||
String[] parts = input.split("@");
|
||||
|
||||
if (parts.length == 0) {
|
||||
throw new IOException("Invalid input string");
|
||||
}
|
||||
|
||||
for (int i = 1; i < parts.length; i++) {
|
||||
String[] keyValue = parts[i].split("=");
|
||||
if (keyValue.length == 2) {
|
||||
params.put(keyValue[0].trim(), keyValue[1].trim());
|
||||
} else {
|
||||
throw new IOException("Invalid parameter format: " + parts[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getString(String i) throws IOException {
|
||||
String val = params.get(i);
|
||||
if (val != null) {
|
||||
return val;
|
||||
} else {
|
||||
throw new IOException("Parameter not found " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user