Minor fixes(важные блять)
This commit is contained in:
@@ -14,6 +14,7 @@ import java.net.URI;
|
|||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
@@ -24,9 +25,10 @@ public class AuthManager {
|
|||||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||||
private static final HttpClient HTTP = HttpClient.newBuilder()
|
private static final HttpClient HTTP = HttpClient.newBuilder()
|
||||||
.connectTimeout(Duration.ofSeconds(15))
|
.connectTimeout(Duration.ofSeconds(15))
|
||||||
|
.version(HttpClient.Version.HTTP_1_1)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
private static AuthSession session = null;
|
private static volatile AuthSession session = null; // ← volatile для потокобезопасности
|
||||||
|
|
||||||
public static boolean loadSavedSession() {
|
public static boolean loadSavedSession() {
|
||||||
if (!Files.exists(AUTH_FILE)) return false;
|
if (!Files.exists(AUTH_FILE)) return false;
|
||||||
@@ -56,25 +58,37 @@ public class AuthManager {
|
|||||||
private static AuthResult authRequest(String endpoint, String username, String password) {
|
private static AuthResult authRequest(String endpoint, String username, String password) {
|
||||||
try {
|
try {
|
||||||
String body = GSON.toJson(new LoginRequest(username, password));
|
String body = GSON.toJson(new LoginRequest(username, password));
|
||||||
|
|
||||||
|
// Логируем для отладки
|
||||||
|
System.out.println(ZAnsi.cyan("[AUTH] Отправка запроса: " + endpoint));
|
||||||
|
|
||||||
HttpResponse<String> resp = post(endpoint, body);
|
HttpResponse<String> resp = post(endpoint, body);
|
||||||
|
|
||||||
|
System.out.println(ZAnsi.cyan("[AUTH] Ответ: HTTP " + resp.statusCode()));
|
||||||
|
|
||||||
if (resp.statusCode() == 200) {
|
if (resp.statusCode() == 200) {
|
||||||
session = GSON.fromJson(resp.body(), AuthSession.class);
|
session = GSON.fromJson(resp.body(), AuthSession.class);
|
||||||
session.expiresAt = System.currentTimeMillis() / 1000L + session.expiresIn;
|
session.expiresAt = System.currentTimeMillis() / 1000L + session.expiresIn;
|
||||||
saveSession();
|
saveSession();
|
||||||
return AuthResult.ok();
|
return AuthResult.ok();
|
||||||
|
} else if (resp.statusCode() == 422) {
|
||||||
|
// Ошибка валидации — парсим детали
|
||||||
|
return AuthResult.fail("Ошибка валидации: " + extractError(resp.body()));
|
||||||
} else {
|
} else {
|
||||||
return AuthResult.fail(extractError(resp.body()));
|
return AuthResult.fail(extractError(resp.body()));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
System.err.println(ZAnsi.red("[AUTH] Исключение: " + e.getMessage()));
|
||||||
|
e.printStackTrace();
|
||||||
return AuthResult.fail("Ошибка соединения: " + e.getMessage());
|
return AuthResult.fail("Ошибка соединения: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void logout() {
|
public static void logout() {
|
||||||
if (session != null && session.refreshToken != null) {
|
if (session != null && session.refreshToken != null) {
|
||||||
try { post("/auth/logout", "{\"refresh_token\":\"" + session.refreshToken + "\"}"); }
|
try {
|
||||||
catch (Exception ignored) {}
|
post("/auth/logout", "{\"refresh_token\":\"" + session.refreshToken + "\"}");
|
||||||
|
} catch (Exception ignored) {}
|
||||||
}
|
}
|
||||||
session = null;
|
session = null;
|
||||||
try { Files.deleteIfExists(AUTH_FILE); } catch (Exception ignored) {}
|
try { Files.deleteIfExists(AUTH_FILE); } catch (Exception ignored) {}
|
||||||
@@ -94,8 +108,11 @@ public class AuthManager {
|
|||||||
|
|
||||||
public static String getAccessToken() {
|
public static String getAccessToken() {
|
||||||
if (session == null) return "0";
|
if (session == null) return "0";
|
||||||
if (isAccessTokenExpired()) tryRefresh();
|
if (isAccessTokenExpired()) {
|
||||||
return session.accessToken != null ? session.accessToken : "0";
|
// ВАЖНО: в UI-контексте это плохо, но пока оставим
|
||||||
|
tryRefresh();
|
||||||
|
}
|
||||||
|
return session != null && session.accessToken != null ? session.accessToken : "0";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isAccessTokenExpired() {
|
private static boolean isAccessTokenExpired() {
|
||||||
@@ -132,59 +149,52 @@ public class AuthManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static HttpResponse<String> post(String endpoint, String jsonBody) throws Exception {
|
private static HttpResponse<String> post(String endpoint, String jsonBody) throws Exception {
|
||||||
|
String fullUrl = ZHttpClient.getBaseUrl() + endpoint;
|
||||||
|
|
||||||
HttpRequest req = HttpRequest.newBuilder()
|
HttpRequest req = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(ZHttpClient.getBaseUrl() + endpoint))
|
.uri(URI.create(fullUrl))
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json; charset=utf-8")
|
||||||
|
.header("Accept", "application/json")
|
||||||
|
.header("User-Agent", "ZernMC-Launcher/1.0")
|
||||||
|
.header("Connection", "close")
|
||||||
.timeout(Duration.ofSeconds(15))
|
.timeout(Duration.ofSeconds(15))
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
|
.POST(HttpRequest.BodyPublishers.ofString(jsonBody, StandardCharsets.UTF_8))
|
||||||
.build();
|
.build();
|
||||||
return HTTP.send(req, HttpResponse.BodyHandlers.ofString());
|
|
||||||
|
// Логируем для отладки
|
||||||
|
System.out.println(ZAnsi.cyan("[HTTP] → POST " + fullUrl));
|
||||||
|
System.out.println(ZAnsi.cyan("[HTTP] Body: " + jsonBody));
|
||||||
|
|
||||||
|
return HTTP.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String extractError(String body) {
|
private static String extractError(String body) {
|
||||||
try {
|
try {
|
||||||
JsonObject json = JsonParser.parseString(body).getAsJsonObject();
|
JsonObject json = JsonParser.parseString(body).getAsJsonObject();
|
||||||
|
|
||||||
|
// FastAPI формат
|
||||||
if (json.has("detail")) {
|
if (json.has("detail")) {
|
||||||
|
// Может быть строкой или массивом
|
||||||
|
if (json.get("detail").isJsonArray()) {
|
||||||
|
return json.getAsJsonArray("detail").get(0).getAsJsonObject()
|
||||||
|
.get("msg").getAsString();
|
||||||
|
}
|
||||||
return json.get("detail").getAsString();
|
return json.get("detail").getAsString();
|
||||||
}
|
}
|
||||||
|
// Наш формат
|
||||||
if (json.has("error")) {
|
if (json.has("error")) {
|
||||||
return json.get("error").getAsString();
|
return json.get("error").getAsString();
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {}
|
} catch (Exception ignored) {}
|
||||||
|
|
||||||
|
// Если не смогли распарсить — возвращаем первые 200 символов
|
||||||
return body.length() > 200 ? body.substring(0, 200) + "..." : body;
|
return body.length() > 200 ? body.substring(0, 200) + "..." : body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ====================== ВНУТРЕННИЕ КЛАССЫ ======================
|
|
||||||
|
|
||||||
public static class AuthSession {
|
|
||||||
@SerializedName("access_token") public String accessToken;
|
|
||||||
@SerializedName("refresh_token") public String refreshToken;
|
|
||||||
@SerializedName("expires_in") public int expiresIn;
|
|
||||||
public long expiresAt;
|
|
||||||
public String username;
|
|
||||||
public String uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class LoginRequest {
|
|
||||||
final String username;
|
|
||||||
final String password;
|
|
||||||
LoginRequest(String u, String p) { this.username = u; this.password = p; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class AuthResult {
|
|
||||||
public final boolean success;
|
|
||||||
public final String error;
|
|
||||||
private AuthResult(boolean s, String e) { success = s; error = e; }
|
|
||||||
public static AuthResult ok() { return new AuthResult(true, null); }
|
|
||||||
public static AuthResult fail(String msg) { return new AuthResult(false, msg); }
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean hasActivePass() {
|
public static boolean hasActivePass() {
|
||||||
if (!isLoggedIn()) return false;
|
if (!isLoggedIn()) return false;
|
||||||
try {
|
try {
|
||||||
String response = ZHttpClient.get("/auth/pass/my");
|
String response = ZHttpClient.get("/auth/pass/my");
|
||||||
// Простая проверка — есть ли хотя бы одна активная проходка
|
|
||||||
return response.contains("\"is_active\":true");
|
return response.contains("\"is_active\":true");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("Не удалось проверить проходки: " + e.getMessage());
|
System.err.println("Не удалось проверить проходки: " + e.getMessage());
|
||||||
@@ -207,4 +217,29 @@ public class AuthManager {
|
|||||||
return "Ошибка соединения: " + e.getMessage();
|
return "Ошибка соединения: " + e.getMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ====================== ВНУТРЕННИЕ КЛАССЫ ======================
|
||||||
|
|
||||||
|
public static class AuthSession {
|
||||||
|
@SerializedName("access_token") public String accessToken;
|
||||||
|
@SerializedName("refresh_token") public String refreshToken;
|
||||||
|
@SerializedName("expires_in") public int expiresIn;
|
||||||
|
public transient long expiresAt;
|
||||||
|
public String username;
|
||||||
|
public String uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class LoginRequest {
|
||||||
|
final String username;
|
||||||
|
final String password;
|
||||||
|
LoginRequest(String u, String p) { this.username = u; this.password = p; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class AuthResult {
|
||||||
|
public final boolean success;
|
||||||
|
public final String error;
|
||||||
|
private AuthResult(boolean s, String e) { success = s; error = e; }
|
||||||
|
public static AuthResult ok() { return new AuthResult(true, null); }
|
||||||
|
public static AuthResult fail(String msg) { return new AuthResult(false, msg); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user