From b0fba84fbbc1460090c1f4ab32e66aba5b058b7f Mon Sep 17 00:00:00 2001 From: Gravita <12893402+gravit0@users.noreply.github.com> Date: Fri, 20 Jan 2023 19:57:14 +0700 Subject: [PATCH] [FEATURE] MergeAuthCoreProvider --- .../launchserver/auth/AuthProviderPair.java | 1 + .../auth/core/AuthCoreProvider.java | 1 + .../auth/core/MergeAuthCoreProvider.java | 90 +++++++++++++++++++ .../auth/GetAvailabilityAuthResponse.java | 4 +- .../pro/gravit/launcher/LauncherEngine.java | 2 +- .../GetAvailabilityAuthRequestEvent.java | 9 +- 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/MergeAuthCoreProvider.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java index ca25cb89..4ee003eb 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java @@ -22,6 +22,7 @@ public final class AuthProviderPair { public transient String name; public transient Set features; public String displayName; + public boolean visible = true; private transient boolean warnOAuthShow = false; public AuthProviderPair() { 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 c43e3210..cb8860cb 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 @@ -46,6 +46,7 @@ public static void registerProviders() { providers.register("postgresql", PostgresSQLCoreProvider.class); providers.register("memory", MemoryAuthCoreProvider.class); providers.register("http", HttpAuthCoreProvider.class); + providers.register("merge", MergeAuthCoreProvider.class); registredProviders = true; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/MergeAuthCoreProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/MergeAuthCoreProvider.java new file mode 100644 index 00000000..74498515 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/MergeAuthCoreProvider.java @@ -0,0 +1,90 @@ +package pro.gravit.launchserver.auth.core; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import pro.gravit.launcher.request.auth.AuthRequest; +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.launchserver.manangers.AuthManager; +import pro.gravit.launchserver.socket.Client; +import pro.gravit.launchserver.socket.response.auth.AuthResponse; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class MergeAuthCoreProvider extends AuthCoreProvider { + private transient final Logger logger = LogManager.getLogger(MergeAuthCoreProvider.class); + public List list = new ArrayList<>(); + private transient List providers = new ArrayList<>(); + @Override + public User getUserByUsername(String username) { + for(var core : providers) { + var result = core.getUserByUsername(username); + if(result != null) { + return result; + } + } + return null; + } + + @Override + public User getUserByUUID(UUID uuid) { + for(var core : providers) { + var result = core.getUserByUUID(uuid); + if(result != null) { + return result; + } + } + return null; + } + + @Override + public UserSession getUserSessionByOAuthAccessToken(String accessToken) throws OAuthAccessTokenExpired { + throw new OAuthAccessTokenExpired(); // Authorization not supported + } + + @Override + public AuthManager.AuthReport refreshAccessToken(String refreshToken, AuthResponse.AuthContext context) { + return null; + } + + @Override + public AuthManager.AuthReport authorize(String login, AuthResponse.AuthContext context, AuthRequest.AuthPasswordInterface password, boolean minecraftAccess) throws IOException { + throw new AuthException("Authorization not supported"); + } + + @Override + public User checkServer(Client client, String username, String serverID) throws IOException { + for(var core : providers) { + var result = core.checkServer(client, username, serverID); + if(result != null) { + return result; + } + } + return null; + } + + @Override + public boolean joinServer(Client client, String username, String accessToken, String serverID) throws IOException { + return false; // Authorization not supported + } + + @Override + public void init(LaunchServer server) { + for(var e : list) { + var pair = server.config.auth.get(e); + if(pair != null) { + providers.add(pair.core); + } else { + logger.warn("Provider {} not found", e); + } + } + } + + @Override + public void close() throws IOException { + // Providers closed automatically + } +} 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 c41ae7dc..424f0e3f 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 @@ -23,10 +23,10 @@ public void execute(ChannelHandlerContext ctx, Client client) { var rca = pair.isSupport(AuthSupportRemoteClientAccess.class); if (rca != null) { list.add(new GetAvailabilityAuthRequestEvent.AuthAvailability(pair.name, pair.displayName, - pair.core.getDetails(client), rca.getClientApiUrl(), rca.getClientApiFeatures())); + pair.visible, pair.core.getDetails(client), rca.getClientApiUrl(), rca.getClientApiFeatures())); } else { list.add(new GetAvailabilityAuthRequestEvent.AuthAvailability(pair.name, pair.displayName, - pair.core.getDetails(client))); + pair.visible, pair.core.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 0294860b..793f151c 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java +++ b/Launcher/src/main/java/pro/gravit/launcher/LauncherEngine.java @@ -179,7 +179,7 @@ public static void applyBasicOfflineProcessors(OfflineRequestService service) { service.registerRequestProcessor(GetAvailabilityAuthRequest.class, (r) -> { List details = new ArrayList<>(); details.add(new AuthLoginOnlyDetails()); - GetAvailabilityAuthRequestEvent.AuthAvailability authAvailability = new GetAvailabilityAuthRequestEvent.AuthAvailability("offline", "Offline Mode", details); + GetAvailabilityAuthRequestEvent.AuthAvailability authAvailability = new GetAvailabilityAuthRequestEvent.AuthAvailability("offline", "Offline Mode", true, details); List list = new ArrayList<>(1); list.add(authAvailability); return new GetAvailabilityAuthRequestEvent(list); 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 3a81326c..b2709e04 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 @@ -45,18 +45,23 @@ public static class AuthAvailability { public String name; @LauncherNetworkAPI public String displayName; + + @LauncherNetworkAPI + public boolean visible; @LauncherNetworkAPI public String apiUrl; @LauncherNetworkAPI public List apiFeatures; - public AuthAvailability(String name, String displayName, List details) { + public AuthAvailability(String name, String displayName, boolean visible, List details) { this.name = name; this.displayName = displayName; + this.visible = visible; this.details = details; } - public AuthAvailability(String name, String displayName, List details, String apiUrl, List apiFeatures) { + public AuthAvailability(String name, String displayName, boolean visible, List details, String apiUrl, List apiFeatures) { + this.visible = visible; this.details = details; this.name = name; this.displayName = displayName;