diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/JsonAuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/JsonAuthHandler.java index 0f7ae1ea..d113a47d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/JsonAuthHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/JsonAuthHandler.java @@ -11,25 +11,26 @@ public class JsonAuthHandler extends CachedAuthHandler { public URL getUrl; public URL updateAuthUrl; public URL updateServerIdUrl; + public String apiKey; @Override protected Entry fetchEntry(String username) throws IOException { - return Launcher.gsonManager.configGson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.configGson.toJsonTree(new EntryRequestByUsername(username)), getUrl), Entry.class); + return Launcher.gsonManager.gson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(new EntryRequestByUsername(username, apiKey)), getUrl), Entry.class); } @Override protected Entry fetchEntry(UUID uuid) throws IOException { - return Launcher.gsonManager.configGson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.configGson.toJsonTree(new EntryRequestByUUID(uuid)), getUrl), Entry.class); + return Launcher.gsonManager.gson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(new EntryRequestByUUID(uuid, apiKey)), getUrl), Entry.class); } @Override protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { - return Launcher.gsonManager.configGson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.configGson.toJsonTree(new UpdateAuthRequest(uuid, username, accessToken)), updateAuthUrl), SuccessResponse.class).success; + return Launcher.gsonManager.gson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(new UpdateAuthRequest(uuid, username, accessToken, apiKey)), updateAuthUrl), SuccessResponse.class).success; } @Override protected boolean updateServerID(UUID uuid, String serverID) throws IOException { - return Launcher.gsonManager.configGson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.configGson.toJsonTree(new UpdateServerIDRequest(uuid, serverID)), updateServerIdUrl), SuccessResponse.class).success; + return Launcher.gsonManager.gson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(new UpdateServerIDRequest(uuid, serverID, apiKey)), updateServerIdUrl), SuccessResponse.class).success; } @Override @@ -39,17 +40,20 @@ public void close() { public static class EntryRequestByUsername { public final String username; + public final String apiKey; - public EntryRequestByUsername(String username) { + public EntryRequestByUsername(String username, String apiKey) { this.username = username; + this.apiKey = apiKey; } } public static class EntryRequestByUUID { public final UUID uuid; - - public EntryRequestByUUID(UUID uuid) { + public final String apiKey; + public EntryRequestByUUID(UUID uuid, String apiKey) { this.uuid = uuid; + this.apiKey = apiKey; } } @@ -57,21 +61,25 @@ public static class UpdateAuthRequest { public final UUID uuid; public final String username; public final String accessToken; + public final String apiKey; - public UpdateAuthRequest(UUID uuid, String username, String accessToken) { + public UpdateAuthRequest(UUID uuid, String username, String accessToken, String apiKey) { this.uuid = uuid; this.username = username; this.accessToken = accessToken; + this.apiKey = apiKey; } } public static class UpdateServerIDRequest { public final UUID uuid; public final String serverID; + public final String apiKey; - public UpdateServerIDRequest(UUID uuid, String serverID) { + public UpdateServerIDRequest(UUID uuid, String serverID, String apiKey) { this.uuid = uuid; this.serverID = serverID; + this.apiKey = apiKey; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/HWIDProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/HWIDProvider.java index 1c46f734..3801422a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/HWIDProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/HWIDProvider.java @@ -17,6 +17,7 @@ public static void registerProviders() { { providers.register("memory", MemoryHWIDProvider.class); providers.register("mysql", MysqlHWIDProvider.class); + providers.register("json", JsonHWIDProvider.class); registredProv = true; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/JsonHWIDProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/JsonHWIDProvider.java new file mode 100644 index 00000000..54ff0231 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/hwid/JsonHWIDProvider.java @@ -0,0 +1,92 @@ +package pro.gravit.launchserver.auth.protect.hwid; + +import pro.gravit.launcher.HTTPRequest; +import pro.gravit.launcher.Launcher; +import pro.gravit.launcher.request.secure.HardwareReportRequest; +import pro.gravit.launchserver.socket.Client; + +import java.net.URL; + +public class JsonHWIDProvider extends HWIDProvider { + public URL findHardwareInfoByPublicKeyRequest; + public URL createHardwareInfoRequest; + public URL addPublicKeyToHardwareInfoRequest; + public String apiKey; + public static class RequestFind { + public byte[] publicKey; + public Client client; + public String apiKey; + } + public static class ResultFind { + public String error; + public HardwareReportRequest.HardwareInfo info; + } + @Override + public HardwareReportRequest.HardwareInfo findHardwareInfoByPublicKey(byte[] publicKey, Client client) throws HWIDException { + try { + RequestFind req = new RequestFind(); + req.publicKey = publicKey; + req.client = client; + req.apiKey = apiKey; + ResultFind r = Launcher.gsonManager.gson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(req), findHardwareInfoByPublicKeyRequest), ResultFind.class); + if (r.error != null) throw new HWIDException(r.error); + return r.info; + } catch (HWIDException t) { + throw t; + } catch (Throwable t) { + throw new HWIDException(t); + } + } + public static class RequestCreate { + public byte[] publicKey; + public Client client; + public HardwareReportRequest.HardwareInfo hardwareInfo; + public String apiKey; + } + public static class ResultCreate { + public String error; + } + @Override + public void createHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, Client client) throws HWIDException { + try { + RequestCreate req = new RequestCreate(); + req.publicKey = publicKey; + req.client = client; + req.hardwareInfo = hardwareInfo; + req.apiKey = apiKey; + ResultCreate r = Launcher.gsonManager.gson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(req), createHardwareInfoRequest), ResultCreate.class); + if (r.error != null) throw new HWIDException(r.error); + } catch (HWIDException t) { + throw t; + } catch (Throwable t) { + throw new HWIDException(t); + } + } + public static class RequestAddKey { + public byte[] publicKey; + public Client client; + public HardwareReportRequest.HardwareInfo hardwareInfo; + public String apiKey; + } + public static class ResultAddKey { + public String error; + public boolean success; + } + @Override + public boolean addPublicKeyToHardwareInfo(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, Client client) throws HWIDException { + try { + RequestAddKey req = new RequestAddKey(); + req.publicKey = publicKey; + req.client = client; + req.hardwareInfo = hardwareInfo; + req.apiKey = apiKey; + ResultAddKey r = Launcher.gsonManager.gson.fromJson(HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(req), addPublicKeyToHardwareInfoRequest), ResultAddKey.class); + if (r.error != null) throw new HWIDException(r.error); + return r.success; + } catch (HWIDException t) { + throw t; + } catch (Throwable t) { + throw new HWIDException(t); + } + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java index 435069c6..2b9756c7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java @@ -4,6 +4,7 @@ import com.google.gson.JsonElement; import pro.gravit.launcher.ClientPermissions; import pro.gravit.launcher.HTTPRequest; +import pro.gravit.launcher.Launcher; import pro.gravit.launcher.request.auth.AuthRequest; import pro.gravit.launcher.request.auth.password.AuthPlainPassword; import pro.gravit.launchserver.auth.AuthException; @@ -13,20 +14,16 @@ import java.net.URL; public final class JsonAuthProvider extends AuthProvider { - private static final Gson gson = new Gson(); - private URL url; - private String apiKey; + public URL url; + public String apiKey; @Override public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws IOException { if (!(password instanceof AuthPlainPassword)) throw new AuthException("This password type not supported"); - authRequest authRequest = new authRequest(login, ((AuthPlainPassword) password).password, ip, apiKey); - JsonElement request = gson.toJsonTree(authRequest); - JsonElement content = HTTPRequest.jsonRequest(request, url); + JsonElement content = HTTPRequest.jsonRequest(Launcher.gsonManager.gson.toJsonTree(new authRequest(login, ((AuthPlainPassword) password).password, ip, apiKey)), url); if (!content.isJsonObject()) return authError("Authentication server response is malformed"); - - authResult result = gson.fromJson(content, authResult.class); + authResult result = Launcher.gsonManager.gson.fromJson(content, authResult.class); if (result.username != null) return new AuthProviderResult(result.username, SecurityHelper.randomStringToken(), new ClientPermissions(result.permissions, result.flags)); else if (result.error != null) diff --git a/modules b/modules index ddca1c9d..42538b3f 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit ddca1c9d23fe72d3929cd5416fbceeb2edcb41bc +Subproject commit 42538b3f13e6b70df1bfdc0692d989c097cf4dea