From 9fe1fc4f23829c3cf7f3a16016afdb9fcc87f704 Mon Sep 17 00:00:00 2001 From: Gravita Date: Tue, 15 Jun 2021 19:24:28 +0700 Subject: [PATCH] [FEATURE] AuthCoreProvider features --- .../launchserver/auth/AuthProviderPair.java | 21 ++++--- .../auth/core/AuthCoreProvider.java | 44 +++++++++------ .../core/interfaces/provider/AuthSupport.java | 4 ++ .../interfaces/provider/AuthSupportExit.java | 2 +- .../provider/AuthSupportGetAllUsers.java | 2 +- .../AuthSupportGetSessionsFromUser.java | 4 +- .../provider/AuthSupportHardware.java | 2 +- .../interfaces/provider/AuthSupportMoney.java | 2 +- .../provider/AuthSupportRegistration.java | 2 +- .../provider/AuthSupportUserBan.java | 2 +- .../auth/protect/AdvancedProtectHandler.java | 55 +++++++++---------- .../socket/response/auth/ExitResponse.java | 17 ++++-- modules | 2 +- 13 files changed, 88 insertions(+), 71 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupport.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 7096991d..2426fd50 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/AuthProviderPair.java @@ -12,8 +12,8 @@ import java.util.Map; import java.util.Set; -public class AuthProviderPair { - public final boolean isDefault = true; +public final class AuthProviderPair { + public boolean isDefault = true; public AuthProvider provider; public AuthHandler handler; public TextureProvider textureProvider; @@ -30,7 +30,12 @@ public AuthProviderPair(AuthProvider provider, AuthHandler handler, TextureProvi this.textureProvider = textureProvider; } - public void init(LaunchServer srv, String name) { + public final T isSupport(Class clazz) { + if (core == null) return null; + return core.isSupport(clazz); + } + + public final void init(LaunchServer srv, String name) { this.name = name; if (links != null) link(srv); if (core == null) { @@ -53,7 +58,7 @@ public void init(LaunchServer srv, String name) { } } - public void link(LaunchServer srv) { + public final void link(LaunchServer srv) { links.forEach((k, v) -> { AuthProviderPair pair = srv.config.getAuthProviderPair(v); if (pair == null) { @@ -79,7 +84,7 @@ public void link(LaunchServer srv) { }); } - public void close() throws IOException { + public final void close() throws IOException { if (social != null) { social.close(); } @@ -117,15 +122,15 @@ public static void getFeatures(Class clazz, Set list) { } } - public boolean isUseCore() { + public final boolean isUseCore() { return core != null; } - public boolean isUseSocial() { + public final boolean isUseSocial() { return core != null && social != null; } - public boolean isUseProviderAndHandler() { + public final boolean isUseProviderAndHandler() { return !isUseCore(); } } 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 7b88b3d2..92b16aeb 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 @@ -155,26 +155,28 @@ public void invoke(String... args) throws Exception { } } }); - 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]); - Iterable users = instance.getAllUsers(); - int counter = 0; - for (User u : users) { - logger.info("User {}", u.toString()); - counter++; - if (counter == max) break; + { + var instance = isSupport(AuthSupportGetAllUsers.class); + if (instance != null) { + 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]); + Iterable 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); } - logger.info("Found {} users", counter); - } - }); + }); + } } - if (this instanceof AuthSupportHardware) { - AuthSupportHardware instance = (AuthSupportHardware) this; + { + var instance = isSupport(AuthSupportHardware.class); map.put("gethardwarebyid", new SubCommand("[id]", "get hardware by id") { @Override public void invoke(String... args) throws Exception { @@ -294,6 +296,12 @@ public boolean joinServer(Client client, String username, String accessToken, St return user.getUsername().equals(username) && user.getAccessToken().equals(accessToken) && updateServerID(user, serverID); } + @SuppressWarnings("unchecked") + public T isSupport(Class clazz) { + if (clazz.isAssignableFrom(getClass())) return (T) this; + return null; + } + @Override public abstract void close() throws IOException; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupport.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupport.java new file mode 100644 index 00000000..d0e04b97 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupport.java @@ -0,0 +1,4 @@ +package pro.gravit.launchserver.auth.core.interfaces.provider; + +public interface AuthSupport { +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportExit.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportExit.java index fdfa0c35..4cd58a05 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportExit.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportExit.java @@ -3,7 +3,7 @@ import pro.gravit.launchserver.auth.core.User; import pro.gravit.launchserver.auth.core.UserSession; -public interface AuthSupportExit { +public interface AuthSupportExit extends AuthSupport { boolean deleteSession(UserSession session); boolean exitUser(User user); 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 index 8d9db103..16b4469a 100644 --- 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 @@ -4,6 +4,6 @@ import pro.gravit.launchserver.auth.core.User; @Feature("users") -public interface AuthSupportGetAllUsers { +public interface AuthSupportGetAllUsers extends AuthSupport { Iterable getAllUsers(); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportGetSessionsFromUser.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportGetSessionsFromUser.java index 68cb7978..a8155eea 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportGetSessionsFromUser.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportGetSessionsFromUser.java @@ -7,8 +7,6 @@ import java.util.List; @Feature("sessions") -public interface AuthSupportGetSessionsFromUser { +public interface AuthSupportGetSessionsFromUser extends AuthSupport { List getSessionsByUser(User user); - - void clearSessionsByUser(User user); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportHardware.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportHardware.java index c30491c0..e8daa0e8 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportHardware.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportHardware.java @@ -9,7 +9,7 @@ import java.util.Arrays; -public interface AuthSupportHardware { +public interface AuthSupportHardware extends AuthSupport { UserHardware getHardwareInfoByPublicKey(byte[] publicKey); UserHardware getHardwareInfoByData(HardwareReportRequest.HardwareInfo info); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportMoney.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportMoney.java index 16a941c0..3dd2e1fc 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportMoney.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportMoney.java @@ -5,7 +5,7 @@ import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportMoney; @Feature("money") -public interface AuthSupportMoney { +public interface AuthSupportMoney extends AuthSupport { default UserSupportMoney fetchUserMoney(User user) { return (UserSupportMoney) user; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportRegistration.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportRegistration.java index 9f237b00..6277ae2b 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportRegistration.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportRegistration.java @@ -7,6 +7,6 @@ import java.util.Map; @Feature("registration") -public interface AuthSupportRegistration { +public interface AuthSupportRegistration extends AuthSupport { User registration(String login, String email, AuthRequest.AuthPasswordInterface password, Map properties); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportUserBan.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportUserBan.java index 1877471d..489d6cbd 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportUserBan.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/interfaces/provider/AuthSupportUserBan.java @@ -5,7 +5,7 @@ import java.time.LocalDateTime; -public interface AuthSupportUserBan { +public interface AuthSupportUserBan extends AuthSupport { UserSupportBanInfo.UserBanInfo banUser(User user, String reason, String moderator, LocalDateTime startTime, LocalDateTime endTime); default UserSupportBanInfo.UserBanInfo banUser(User user) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/AdvancedProtectHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/AdvancedProtectHandler.java index ab69c9d3..fba11ad9 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/AdvancedProtectHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/AdvancedProtectHandler.java @@ -67,26 +67,28 @@ public void onHardwareReport(HardwareReportResponse response, Client client) { return; } logger.debug("HardwareInfo received"); - if (client.auth.core instanceof AuthSupportHardware) { - AuthSupportHardware authSupportHardware = (AuthSupportHardware) client.auth; - UserHardware hardware = authSupportHardware.getHardwareInfoByData(response.hardware); - if (hardware == null) { - hardware = authSupportHardware.createHardwareInfo(response.hardware, client.trustLevel.publicKey); + { + var authSupportHardware = client.auth.isSupport(AuthSupportHardware.class); + if (authSupportHardware != null) { + UserHardware hardware = authSupportHardware.getHardwareInfoByData(response.hardware); + if (hardware == null) { + hardware = authSupportHardware.createHardwareInfo(response.hardware, client.trustLevel.publicKey); + } else { + authSupportHardware.addPublicKeyToHardwareInfo(hardware, client.trustLevel.publicKey); + } + authSupportHardware.connectUserAndHardware(client.getUser(), hardware); + if (hardware.isBanned()) { + throw new SecurityException("Your hardware banned"); + } + client.trustLevel.hardwareInfo = hardware.getHardwareInfo(); } else { - authSupportHardware.addPublicKeyToHardwareInfo(hardware, client.trustLevel.publicKey); + provider.normalizeHardwareInfo(response.hardware); + boolean needCreate = !provider.addPublicKeyToHardwareInfo(response.hardware, client.trustLevel.publicKey, client); + logger.debug("HardwareInfo needCreate: {}", needCreate ? "true" : "false"); + if (needCreate) + provider.createHardwareInfo(response.hardware, client.trustLevel.publicKey, client); + client.trustLevel.hardwareInfo = response.hardware; } - authSupportHardware.connectUserAndHardware(client.getUser(), hardware); - if (hardware.isBanned()) { - throw new SecurityException("Your hardware banned"); - } - client.trustLevel.hardwareInfo = hardware.getHardwareInfo(); - } else { - provider.normalizeHardwareInfo(response.hardware); - boolean needCreate = !provider.addPublicKeyToHardwareInfo(response.hardware, client.trustLevel.publicKey, client); - logger.debug("HardwareInfo needCreate: {}", needCreate ? "true" : "false"); - if (needCreate) - provider.createHardwareInfo(response.hardware, client.trustLevel.publicKey, client); - client.trustLevel.hardwareInfo = response.hardware; } } catch (HWIDException e) { throw new SecurityException(e.getMessage()); @@ -97,28 +99,21 @@ public void onHardwareReport(HardwareReportResponse response, Client client) { @Override public VerifySecureLevelKeyRequestEvent onSuccessVerify(Client client) { if (enableHardwareFeature) { - if (client.isAuth && client.auth.core instanceof AuthSupportHardware) { - UserHardware hardware = ((AuthSupportHardware) client.auth.core).getHardwareInfoByPublicKey(client.trustLevel.publicKey); + var authSupportHardware = client.auth.isSupport(AuthSupportHardware.class); + if (authSupportHardware != null) { + UserHardware hardware = authSupportHardware.getHardwareInfoByPublicKey(client.trustLevel.publicKey); if (hardware == null) //HWID not found? return new VerifySecureLevelKeyRequestEvent(true, false, createPublicKeyToken(client.username, client.trustLevel.publicKey)); if (hardware.isBanned()) { throw new SecurityException("Your hardware banned"); } client.trustLevel.hardwareInfo = hardware.getHardwareInfo(); + authSupportHardware.connectUserAndHardware(client.getUser(), hardware); } else if (provider == null) { logger.warn("HWIDProvider null. HardwareInfo not checked!"); } else { try { - if (client.auth.core instanceof AuthSupportHardware) { - AuthSupportHardware authSupportHardware = (AuthSupportHardware) client.auth; - UserHardware hardware = authSupportHardware.getHardwareInfoByPublicKey(client.trustLevel.publicKey); - if (hardware != null) { - client.trustLevel.hardwareInfo = hardware.getHardwareInfo(); - authSupportHardware.connectUserAndHardware(client.getUser(), hardware); - } - } else { - client.trustLevel.hardwareInfo = provider.findHardwareInfoByPublicKey(client.trustLevel.publicKey, client); - } + client.trustLevel.hardwareInfo = provider.findHardwareInfoByPublicKey(client.trustLevel.publicKey, client); if (client.trustLevel.hardwareInfo == null) //HWID not found? return new VerifySecureLevelKeyRequestEvent(true, false, createPublicKeyToken(client.username, client.trustLevel.publicKey)); } catch (HWIDException e) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java index 448d66d6..7ccdeff2 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/ExitResponse.java @@ -6,7 +6,8 @@ import pro.gravit.launcher.events.RequestEvent; import pro.gravit.launcher.events.request.ExitRequestEvent; import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.auth.core.interfaces.provider.AuthSupportGetSessionsFromUser; +import pro.gravit.launchserver.auth.core.UserSession; +import pro.gravit.launchserver.auth.core.interfaces.provider.AuthSupportExit; import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler; import pro.gravit.launchserver.socket.response.SimpleResponse; @@ -48,12 +49,18 @@ public void execute(ChannelHandlerContext ctx, Client client) { Client newClient = new Client(null); newClient.checkSign = client.checkSign; handler.setClient(newClient); - if (exitAll) { - if (client.auth instanceof AuthSupportGetSessionsFromUser) { - AuthSupportGetSessionsFromUser support = (AuthSupportGetSessionsFromUser) client.auth; - support.clearSessionsByUser(client.getUser()); + AuthSupportExit supportExit = client.auth.core.isSupport(AuthSupportExit.class); + if (supportExit != null) { + if (exitAll) { + supportExit.exitUser(client.getUser()); + } else { + UserSession session = client.sessionObject; + if (session != null) { + supportExit.deleteSession(session); + } } } + sendResult(new ExitRequestEvent(ExitRequestEvent.ExitReason.CLIENT)); } else { if (client.session == null && exitAll) { sendError("Session invalid"); diff --git a/modules b/modules index 566a42d1..ca22fda4 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit 566a42d1a09a2504ab4f9f6fb8c9a95d99c7fdca +Subproject commit ca22fda40f690015edf7650f4365b8efa8e96ec8