From 7e97915fd18ede41dc145cfe72f82957985c2124 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 24 Dec 2018 17:00:09 +0700 Subject: [PATCH] =?UTF-8?q?PermissionsManager=20=D0=BD=D0=B0=20=D1=81?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B0=D0=B9,=20=D0=B5=D1=81=D0=BB=D0=B8=20au?= =?UTF-8?q?thProvider=20=D0=BD=D0=B5=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=B0=D1=82=D1=8B=D0=B2=D0=B0=D0=B5=D1=82=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=D0=B5=D0=B3=D0=B8=D0=B8=20=D0=97=D0=B0=D0=B3?= =?UTF-8?q?=D1=80=D1=83=D0=B7=D0=BA=D0=B0=20=D0=BF=D1=80=D0=B8=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B5=D0=B3=D0=B8=D0=B9=20=D0=B8=D0=B7=20=D1=84=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=B0=20permissions.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/LaunchServer.java | 8 ++-- .../JsonFilePermissionsHandler.java | 45 +++++++++++++++++++ .../auth/provider/AcceptAuthProvider.java | 3 +- .../auth/provider/AuthProviderResult.java | 3 +- .../manangers/PermissionsManager.java | 24 ++++++++++ .../response/auth/AuthResponse.java | 2 +- .../response/auth/AuthServerResponse.java | 9 ++-- 7 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/manangers/PermissionsManager.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index 1a460e15..6d9eaf82 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -13,6 +13,7 @@ import ru.gravit.launchserver.auth.handler.MemoryAuthHandler; import ru.gravit.launchserver.auth.hwid.AcceptHWIDHandler; import ru.gravit.launchserver.auth.hwid.HWIDHandler; +import ru.gravit.launchserver.auth.permissions.JsonFilePermissionsHandler; import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.RejectAuthProvider; import ru.gravit.launchserver.binary.EXEL4JLauncherBinary; @@ -26,10 +27,7 @@ import ru.gravit.launchserver.config.AuthProviderAdapter; import ru.gravit.launchserver.config.HWIDHandlerAdapter; import ru.gravit.launchserver.config.TextureProviderAdapter; -import ru.gravit.launchserver.manangers.BuildHookManager; -import ru.gravit.launchserver.manangers.MirrorManager; -import ru.gravit.launchserver.manangers.ModulesManager; -import ru.gravit.launchserver.manangers.SessionManager; +import ru.gravit.launchserver.manangers.*; import ru.gravit.launchserver.response.Response; import ru.gravit.launchserver.socket.ServerSocketHandler; import ru.gravit.launchserver.texture.RequestTextureProvider; @@ -349,6 +347,8 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException { e.printStackTrace(); } }); + JsonFilePermissionsHandler.init(); + PermissionsManager.registerPermissionsFunction(JsonFilePermissionsHandler::getPermissions); // init modules modulesManager.initModules(); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java new file mode 100644 index 00000000..ff149487 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/permissions/JsonFilePermissionsHandler.java @@ -0,0 +1,45 @@ +package ru.gravit.launchserver.auth.permissions; + +import com.google.gson.reflect.TypeToken; +import ru.gravit.launcher.Launcher; +import ru.gravit.launchserver.auth.ClientPermissions; +import ru.gravit.utils.helper.IOHelper; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.lang.reflect.Type; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +public class JsonFilePermissionsHandler { + public static String FILE_NAME = "permissions.json"; + public static Map map; + public static class Enity + { + public String username; + public ClientPermissions permissions; + } + public static ClientPermissions getPermissions(String username) + { + return map.getOrDefault(username,ClientPermissions.DEFAULT); + } + public static void init() throws IOException { + Type type = new TypeToken>(){}.getType(); + Path path = Paths.get(FILE_NAME); + if(!IOHelper.exists(path)) + { + map = new HashMap<>(); + try(Writer writer = IOHelper.newWriter(path)) + { + Launcher.gson.toJson(map,writer); + } + } + try(Reader reader = IOHelper.newReader(path)) + { + map = Launcher.gson.fromJson(reader,type); + } + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AcceptAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AcceptAuthProvider.java index 53e6d66e..5d7fa638 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AcceptAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AcceptAuthProvider.java @@ -4,11 +4,10 @@ import ru.gravit.utils.helper.SecurityHelper; public final class AcceptAuthProvider extends AuthProvider { - private boolean isAdminAccess; @Override public AuthProviderResult auth(String login, String password, String ip) { - return new AuthProviderResult(login, SecurityHelper.randomStringToken(), isAdminAccess ? ClientPermissions.getSuperuserAccount() : ClientPermissions.DEFAULT); // Same as login + return new AuthProviderResult(login, SecurityHelper.randomStringToken()); // Same as login } @Override diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProviderResult.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProviderResult.java index b80245c2..d34f90c1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProviderResult.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProviderResult.java @@ -1,6 +1,7 @@ package ru.gravit.launchserver.auth.provider; import ru.gravit.launchserver.auth.ClientPermissions; +import ru.gravit.launchserver.manangers.PermissionsManager; public class AuthProviderResult { @@ -11,7 +12,7 @@ public class AuthProviderResult { public AuthProviderResult(String username, String accessToken) { this.username = username; this.accessToken = accessToken; - permissions = ClientPermissions.DEFAULT; + permissions = PermissionsManager.getPermissions(username); } public AuthProviderResult(String username, String accessToken, ClientPermissions permissions) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/PermissionsManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/PermissionsManager.java new file mode 100644 index 00000000..ea5c75b7 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/PermissionsManager.java @@ -0,0 +1,24 @@ +package ru.gravit.launchserver.manangers; + +import ru.gravit.launchserver.auth.ClientPermissions; + +public class PermissionsManager { + private static PermissionsFunction function = PermissionsManager::returnDefault; + public static void registerPermissionsFunction(PermissionsFunction function) + { + PermissionsManager.function = function; + } + public static ClientPermissions getPermissions(String username) + { + return function.getPermission(username); + } + @FunctionalInterface + public interface PermissionsFunction + { + ClientPermissions getPermission(String username); + } + public static ClientPermissions returnDefault(String username) + { + return ClientPermissions.DEFAULT; + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthResponse.java index 9987d5a2..c97838af 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthResponse.java @@ -87,7 +87,7 @@ public void reply() throws Exception { } } if (clientData.profile == null) { - throw new AuthException("You profile not found"); + throw new AuthException("Your profile is not found"); } } server.config.hwidHandler.check(OshiHWID.gson.fromJson(hwid_str, OshiHWID.class), result.username); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthServerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthServerResponse.java index 1d2c8cb4..9fba3686 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthServerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/auth/AuthServerResponse.java @@ -67,14 +67,15 @@ public void reply() throws Exception { Collection profiles = server.getProfiles(); for (ClientProfile p : profiles) { if (p.getTitle().equals(client)) { - if (!p.isWhitelistContains(login)) { - throw new AuthException(server.config.whitelistRejectString); - } clientData.profile = p; } } if (clientData.profile == null) { - throw new AuthException("You profile not found"); + throw new AuthException("Your profile is not found"); + } + if(!clientData.permissions.canServer) + { + throw new AuthException("Your account cannot be a server"); } clientData.type = Client.Type.SERVER; clientData.username = result.username;