From e1f356feb77bd996502b97a80f37f7559befe07f Mon Sep 17 00:00:00 2001 From: Gravita Date: Thu, 20 May 2021 22:35:34 +0700 Subject: [PATCH] [FEATURE] Default commands in AuthCoreProvider --- .../auth/core/AuthCoreProvider.java | 65 ++++++++++++++++++- .../provider/AuthSupportGetAllUsers.java | 9 +++ .../config/LaunchServerConfig.java | 2 + 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportGetAllUsers.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/AuthCoreProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/AuthCoreProvider.java index 84c07018..9903a098 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/AuthCoreProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/AuthCoreProvider.java @@ -1,26 +1,37 @@ package pro.gravit.launchserver.auth.core; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import pro.gravit.launcher.Launcher; import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; import pro.gravit.launcher.request.auth.AuthRequest; import pro.gravit.launcher.request.auth.details.AuthPasswordDetails; +import pro.gravit.launcher.request.auth.password.AuthPlainPassword; import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.Reconfigurable; import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.launchserver.auth.core.interfaces.provider.AuthSupportGetAllUsers; import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.response.auth.AuthResponse; import pro.gravit.utils.ProviderMap; +import pro.gravit.utils.command.Command; +import pro.gravit.utils.command.CommandException; +import pro.gravit.utils.command.SubCommand; import pro.gravit.utils.helper.SecurityHelper; import java.io.IOException; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.UUID; /* All-In-One provider */ -public abstract class AuthCoreProvider implements AutoCloseable { +public abstract class AuthCoreProvider implements AutoCloseable, Reconfigurable { public static final ProviderMap providers = new ProviderMap<>("AuthCoreProvider"); private static boolean registredProviders = false; + private static final Logger logger = LogManager.getLogger(); public static void registerProviders() { if (!registredProviders) { providers.register("reject", RejectAuthCoreProvider.class); @@ -41,6 +52,58 @@ public List getDetails( return List.of(new AuthPasswordDetails()); } + @Override + public Map getCommands() { + Map map = defaultCommandsMap(); + map.put("checkpassword", new SubCommand("[username] [json/plain password data]", "check password") { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 2); + User user = getUserByUsername(args[0]); + if(user == null) throw new CommandException("User not found"); + AuthRequest.AuthPasswordInterface password; + if(args[1].startsWith("{")) { + password = Launcher.gsonManager.gson.fromJson(args[1], AuthRequest.AuthPasswordInterface.class); + } else { + password = new AuthPlainPassword(args[1]); + } + PasswordVerifyReport report = verifyPassword(user, password); + if(report.success) { + logger.info("Password correct"); + } else { + if(report.needMoreFactor) { + if(report.factors.size() == 1 && report.factors.get(0) == -1) { + logger.info("Password not correct: Required 2FA"); + } else { + logger.info("Password not correct: Required more factors: {}", report.factors.toString()); + } + } else { + logger.info("Password incorrect"); + } + } + } + }); + if(this instanceof AuthSupportGetAllUsers) { + AuthSupportGetAllUsers instance = (AuthSupportGetAllUsers) this; + map.put("getallusers", new SubCommand("(limit)", "print all users information") { + @Override + public void invoke(String... args) throws Exception { + int max = Integer.MAX_VALUE; + if(args.length > 0) max = Integer.parseInt(args[0]); + List users = instance.getAllUsers(); + int counter = 0; + for(User u : users) { + logger.info("User {}", u.toString()); + counter++; + if(counter == max) break; + } + logger.info("Found {} users", counter); + } + }); + } + return map; + } + public UUID checkServer(Client client, String username, String serverID) throws IOException { User user = getUserByUsername(username); if(user.getUsername().equals(username) && user.getServerId().equals(serverID)) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportGetAllUsers.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportGetAllUsers.java new file mode 100644 index 00000000..0b5213cd --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportGetAllUsers.java @@ -0,0 +1,9 @@ +package pro.gravit.launchserver.auth.core.interfaces.provider; + +import pro.gravit.launchserver.auth.core.User; + +import java.util.List; + +public interface AuthSupportGetAllUsers { + List getAllUsers(); +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java index 117c7b8d..5ff4e027 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java @@ -207,6 +207,7 @@ public void init(LaunchServer.ReloadType type) { for (AuthProviderPair pair : auth.values()) { server.registerObject("auth.".concat(pair.name).concat(".provider"), pair.provider); server.registerObject("auth.".concat(pair.name).concat(".handler"), pair.handler); + server.registerObject("auth.".concat(pair.name).concat(".core"), pair.core); server.registerObject("auth.".concat(pair.name).concat(".texture"), pair.textureProvider); } } @@ -219,6 +220,7 @@ public void close(LaunchServer.ReloadType type) { for (AuthProviderPair pair : auth.values()) { server.unregisterObject("auth.".concat(pair.name).concat(".provider"), pair.provider); server.unregisterObject("auth.".concat(pair.name).concat(".handler"), pair.handler); + server.unregisterObject("auth.".concat(pair.name).concat(".core"), pair.core); server.unregisterObject("auth.".concat(pair.name).concat(".texture"), pair.textureProvider); pair.close(); }