From d1e671a935c7aeb1c8232d80160dae8003989a38 Mon Sep 17 00:00:00 2001 From: Gravita Date: Tue, 13 Apr 2021 19:30:06 +0700 Subject: [PATCH] [FEATURE] AuthDetails support --- .../launchserver/LaunchServerStarter.java | 2 ++ .../auth/provider/AuthProvider.java | 9 ++++++ .../auth/GetAvailabilityAuthResponse.java | 2 +- .../pro/gravit/launcher/LauncherEngine.java | 2 ++ .../client/ClientLauncherEntryPoint.java | 2 ++ .../GetAvailabilityAuthRequestEvent.java | 23 ++++++++++++--- .../launcher/request/auth/AuthRequest.java | 3 ++ .../auth/GetAvailabilityAuthRequest.java | 16 ++++++++++ .../auth/details/AuthPasswordDetails.java | 10 +++++++ .../request/auth/details/AuthTotpDetails.java | 23 +++++++++++++++ .../auth/details/AuthWebViewDetails.java | 29 +++++++++++++++++++ .../gravit/launcher/server/ServerWrapper.java | 2 ++ 12 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthPasswordDetails.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthTotpDetails.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthWebViewDetails.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java index ff1351f3..c1b185a7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java @@ -8,6 +8,7 @@ import pro.gravit.launcher.modules.events.PreConfigPhase; import pro.gravit.launcher.profiles.optional.actions.OptionalAction; import pro.gravit.launcher.request.auth.AuthRequest; +import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest; import pro.gravit.launchserver.auth.handler.AuthHandler; import pro.gravit.launchserver.auth.protect.ProtectHandler; import pro.gravit.launchserver.auth.protect.hwid.HWIDProvider; @@ -233,6 +234,7 @@ public static void registerAll() { WebSocketService.registerResponses(); DaoProvider.registerProviders(); AuthRequest.registerProviders(); + GetAvailabilityAuthRequest.registerProviders(); HWIDProvider.registerProviders(); OptionalAction.registerProviders(); SessionStorage.registerProviders(); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/AuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/AuthProvider.java index ffe549b9..4a71331c 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/AuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/AuthProvider.java @@ -2,11 +2,14 @@ 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.launchserver.LaunchServer; import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.launchserver.socket.Client; import pro.gravit.utils.ProviderMap; import java.io.IOException; +import java.util.List; public abstract class AuthProvider implements AutoCloseable { public static final ProviderMap providers = new ProviderMap<>("AuthProvider"); @@ -31,14 +34,20 @@ public static void registerProviders() { } } + @Deprecated public GetAvailabilityAuthRequestEvent.AuthAvailability.AuthType getFirstAuthType() { return GetAvailabilityAuthRequestEvent.AuthAvailability.AuthType.PASSWORD; } + @Deprecated public GetAvailabilityAuthRequestEvent.AuthAvailability.AuthType getSecondAuthType() { return GetAvailabilityAuthRequestEvent.AuthAvailability.AuthType.NONE; } + public List getDetails(Client client) { + return List.of(new AuthPasswordDetails()); + } + /** * Verifies the username and password * diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/GetAvailabilityAuthResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/GetAvailabilityAuthResponse.java index f617d67c..32aacf7e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/GetAvailabilityAuthResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/GetAvailabilityAuthResponse.java @@ -19,7 +19,7 @@ public String getType() { public void execute(ChannelHandlerContext ctx, Client client) { List list = new ArrayList<>(); for (AuthProviderPair pair : server.config.auth.values()) { - list.add(new GetAvailabilityAuthRequestEvent.AuthAvailability(pair.name, pair.displayName, pair.provider.getFirstAuthType(), pair.provider.getSecondAuthType())); + list.add(new GetAvailabilityAuthRequestEvent.AuthAvailability(pair.name, pair.displayName, pair.provider.getDetails(client))); } sendResult(new GetAvailabilityAuthRequestEvent(list)); } diff --git a/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java b/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java index d4a378b7..a4816629 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java +++ b/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java @@ -17,6 +17,7 @@ import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.RequestException; import pro.gravit.launcher.request.auth.AuthRequest; +import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest; import pro.gravit.launcher.request.auth.RestoreSessionRequest; import pro.gravit.launcher.request.websockets.StdWebSocketService; import pro.gravit.launcher.utils.NativeJVMHalt; @@ -116,6 +117,7 @@ public static void main(String... args) throws Throwable { public static void initGson(ClientModuleManager modulesManager) { AuthRequest.registerProviders(); + GetAvailabilityAuthRequest.registerProviders(); OptionalAction.registerProviders(); Launcher.gsonManager = new ClientGsonManager(modulesManager); Launcher.gsonManager.initGson(); diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncherEntryPoint.java b/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncherEntryPoint.java index ca9d057e..fa5ca088 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncherEntryPoint.java +++ b/Launcher/src/main/java/pro/gravit/launcher/client/ClientLauncherEntryPoint.java @@ -21,6 +21,7 @@ import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.RequestException; import pro.gravit.launcher.request.auth.AuthRequest; +import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest; import pro.gravit.launcher.request.auth.RestoreSessionRequest; import pro.gravit.launcher.serialize.HInput; import pro.gravit.launcher.utils.DirWatcher; @@ -183,6 +184,7 @@ public static void main(String[] args) throws Throwable { private static void initGson(ClientModuleManager moduleManager) { AuthRequest.registerProviders(); + GetAvailabilityAuthRequest.registerProviders(); OptionalAction.registerProviders(); Launcher.gsonManager = new ClientGsonManager(moduleManager); Launcher.gsonManager.initGson(); diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/GetAvailabilityAuthRequestEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/GetAvailabilityAuthRequestEvent.java index 85eb03a5..5f1aed4f 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/GetAvailabilityAuthRequestEvent.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/GetAvailabilityAuthRequestEvent.java @@ -2,6 +2,7 @@ import pro.gravit.launcher.LauncherNetworkAPI; import pro.gravit.launcher.events.RequestEvent; +import pro.gravit.utils.TypeSerializeInterface; import java.util.List; @@ -37,22 +38,34 @@ public enum ServerFeature { public static class AuthAvailability { @LauncherNetworkAPI - public final String name; + public String name; @LauncherNetworkAPI - public final String displayName; + public String displayName; + @Deprecated @LauncherNetworkAPI - public final AuthType firstType; + public AuthType firstType; + @Deprecated @LauncherNetworkAPI - public final AuthType secondType; + public AuthType secondType; + public final List details; + + @Deprecated public AuthAvailability(String name, String displayName, AuthType firstType, AuthType secondType) { this.name = name; this.displayName = displayName; this.firstType = firstType; this.secondType = secondType; + this.details = null; } + public AuthAvailability(String name, String displayName, List details) { + this.name = name; + this.displayName = displayName; + this.details = details; + } + @Deprecated public enum AuthType { @LauncherNetworkAPI PASSWORD, @@ -68,4 +81,6 @@ public enum AuthType { OTHER } } + public interface AuthAvailabilityDetails extends TypeSerializeInterface { + } } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/AuthRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/AuthRequest.java index c3b1bbd0..73fa7668 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/AuthRequest.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/AuthRequest.java @@ -2,7 +2,10 @@ import pro.gravit.launcher.LauncherNetworkAPI; import pro.gravit.launcher.events.request.AuthRequestEvent; +import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; import pro.gravit.launcher.request.Request; +import pro.gravit.launcher.request.auth.details.AuthPasswordDetails; +import pro.gravit.launcher.request.auth.details.AuthWebViewDetails; import pro.gravit.launcher.request.auth.password.*; import pro.gravit.launcher.request.websockets.WebSocketRequest; import pro.gravit.utils.ProviderMap; diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/GetAvailabilityAuthRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/GetAvailabilityAuthRequest.java index aad6870b..02b2bf8c 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/GetAvailabilityAuthRequest.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/GetAvailabilityAuthRequest.java @@ -2,10 +2,26 @@ import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; import pro.gravit.launcher.request.Request; +import pro.gravit.launcher.request.auth.details.AuthPasswordDetails; +import pro.gravit.launcher.request.auth.details.AuthTotpDetails; +import pro.gravit.launcher.request.auth.details.AuthWebViewDetails; import pro.gravit.launcher.request.websockets.WebSocketRequest; +import pro.gravit.utils.ProviderMap; public class GetAvailabilityAuthRequest extends Request implements WebSocketRequest { + public static final ProviderMap providers = new ProviderMap<>(); + private static boolean registeredProviders = false; + + public static void registerProviders() { + if(!registeredProviders) { + providers.register("password", AuthPasswordDetails.class); + providers.register("webview", AuthWebViewDetails.class); + providers.register("totp", AuthTotpDetails.class); + registeredProviders = true; + } + } + @Override public String getType() { return "getAvailabilityAuth"; diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthPasswordDetails.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthPasswordDetails.java new file mode 100644 index 00000000..985ce451 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthPasswordDetails.java @@ -0,0 +1,10 @@ +package pro.gravit.launcher.request.auth.details; + +import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; + +public class AuthPasswordDetails implements GetAvailabilityAuthRequestEvent.AuthAvailabilityDetails { + @Override + public String getType() { + return "password"; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthTotpDetails.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthTotpDetails.java new file mode 100644 index 00000000..df0c5ce2 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthTotpDetails.java @@ -0,0 +1,23 @@ +package pro.gravit.launcher.request.auth.details; + +import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; + +public class AuthTotpDetails implements GetAvailabilityAuthRequestEvent.AuthAvailabilityDetails { + public final String alg; + public final int maxKeyLength; + + public AuthTotpDetails(String alg, int maxKeyLength) { + this.alg = alg; + this.maxKeyLength = maxKeyLength; + } + + public AuthTotpDetails(String alg) { + this.alg = alg; + this.maxKeyLength = 6; + } + + @Override + public String getType() { + return "totp"; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthWebViewDetails.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthWebViewDetails.java new file mode 100644 index 00000000..25d30cec --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/details/AuthWebViewDetails.java @@ -0,0 +1,29 @@ +package pro.gravit.launcher.request.auth.details; + +import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; + +public class AuthWebViewDetails implements GetAvailabilityAuthRequestEvent.AuthAvailabilityDetails { + public final String url; + public final String redirectUrl; + public final boolean canBrowser; + public final boolean onlyBrowser; + + public AuthWebViewDetails(String url, String redirectUrl, boolean canBrowser, boolean onlyBrowser) { + this.url = url; + this.redirectUrl = redirectUrl; + this.canBrowser = canBrowser; + this.onlyBrowser = onlyBrowser; + } + + public AuthWebViewDetails(String url, String redirectUrl) { + this.url = url; + this.redirectUrl = redirectUrl; + this.canBrowser = true; + this.onlyBrowser = false; + } + + @Override + public String getType() { + return "webview"; + } +} diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java index 66ca428a..78485fb1 100644 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java @@ -14,6 +14,7 @@ import pro.gravit.launcher.request.Request; import pro.gravit.launcher.request.RequestException; import pro.gravit.launcher.request.auth.AuthRequest; +import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest; import pro.gravit.launcher.request.update.ProfilesRequest; import pro.gravit.launcher.server.setup.ServerWrapperSetup; import pro.gravit.utils.PublicURLClassLoader; @@ -127,6 +128,7 @@ public boolean loopAuth(int count, int sleeptime) { public void run(String... args) throws Throwable { initGson(modulesManager); AuthRequest.registerProviders(); + GetAvailabilityAuthRequest.registerProviders(); OptionalAction.registerProviders(); if (args.length > 0 && args[0].equals("setup") && !disableSetup) { LogHelper.debug("Read ServerWrapperConfig.json");