From 385f2d9ec5bf302a02166d89098f99dbce36f544 Mon Sep 17 00:00:00 2001 From: Clercq <50501391+Clercq@users.noreply.github.com> Date: Mon, 1 Aug 2022 11:54:07 +0300 Subject: [PATCH] Add to HttpAuthCoreProvider the AuthSupportHardware implementation (#601) * initial * test fixes * review fixes * review fixes * test fixes * review fixes * review fixes --- .../auth/core/HttpAuthCoreProvider.java | 256 +++++++++++++++++- 1 file changed, 243 insertions(+), 13 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/HttpAuthCoreProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/HttpAuthCoreProvider.java index 21b5b6b0..a4b81ae4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/HttpAuthCoreProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/HttpAuthCoreProvider.java @@ -7,9 +7,13 @@ import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; import pro.gravit.launcher.profiles.Texture; import pro.gravit.launcher.request.auth.AuthRequest; +import pro.gravit.launcher.request.secure.HardwareReportRequest; import pro.gravit.launchserver.HttpRequester; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.launchserver.auth.core.interfaces.UserHardware; +import pro.gravit.launchserver.auth.core.interfaces.provider.AuthSupportHardware; +import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportHardware; import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportProperties; import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportTextures; import pro.gravit.launchserver.helper.HttpHelper; @@ -19,12 +23,9 @@ import pro.gravit.utils.helper.CommonHelper; import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; -public class HttpAuthCoreProvider extends AuthCoreProvider { +public class HttpAuthCoreProvider extends AuthCoreProvider implements AuthSupportHardware { private transient final Logger logger = LogManager.getLogger(); private transient HttpRequester requester; public String bearerToken; @@ -32,12 +33,23 @@ public class HttpAuthCoreProvider extends AuthCoreProvider { public String getUserByLoginUrl; public String getUserByUUIDUrl; public String getUserByTokenUrl; - public String getAuthDetails; + public String getAuthDetailsUrl; public String refreshTokenUrl; public String authorizeUrl; public String joinServerUrl; public String checkServerUrl; public String updateServerIdUrl; + //below fields can be empty if advanced protect handler disabled + public String getHardwareInfoByPublicKeyUrl; + public String getHardwareInfoByDataUrl; + public String getHardwareInfoByIdUrl; + public String createHardwareInfoUrl; + public String connectUserAndHardwareUrl; + public String addPublicKeyToHardwareInfoUrl; + public String getUsersByHardwareInfoUrl; + public String banHardwareUrl; + public String unbanHardwareUrl; + @Override public User getUserByUsername(String username) { try { @@ -73,11 +85,11 @@ public User getUserByUUID(UUID uuid) { @Override public List getDetails(Client client) { - if(getAuthDetails == null) { + if(getAuthDetailsUrl == null) { return super.getDetails(client); } try { - var result = requester.send(requester.get(getAuthDetails, bearerToken), GetAuthDetailsResponse.class).getOrThrow(); + var result = requester.send(requester.get(getAuthDetailsUrl, bearerToken), GetAuthDetailsResponse.class).getOrThrow(); return result.details; } catch (IOException e) { logger.error(e); @@ -133,6 +145,128 @@ public AuthManager.AuthReport authorize(String login, AuthResponse.AuthContext c return result.getOrThrow().toAuthReport(); } + @Override + public UserHardware getHardwareInfoByPublicKey(byte[] publicKey) { + if(getHardwareInfoByPublicKeyUrl == null) { + return null; + } + try { + return requester.send(requester.post(getHardwareInfoByPublicKeyUrl, new HardwareRequest(publicKey), + bearerToken), HttpUserHardware.class).getOrThrow(); + } catch (IOException e) { + logger.error(e); + return null; + } + } + + @Override + public UserHardware getHardwareInfoByData(HardwareReportRequest.HardwareInfo info) { + if(getHardwareInfoByDataUrl == null) { + return null; + } + try { + HardwareRequest request = new HardwareRequest(new HttpUserHardware(info)); + HttpHelper.HttpOptional hardware = + requester.send(requester.post(getHardwareInfoByDataUrl, request, + bearerToken), HttpUserHardware.class); + //should return null if not found + return hardware.isSuccessful() ? hardware.getOrThrow() : null; + } catch (IOException e) { + logger.error(e); + return null; + } + } + + @Override + public UserHardware getHardwareInfoById(String id) { + if(getHardwareInfoByIdUrl == null) { + return null; + } + try { + return requester.send(requester.post(getHardwareInfoByIdUrl, new HardwareRequest(new HttpUserHardware(Long.parseLong(id))), + bearerToken), HttpUserHardware.class).getOrThrow(); + } catch (IOException | NumberFormatException e) { + logger.error(e); + return null; + } + } + + @Override + public UserHardware createHardwareInfo(HardwareReportRequest.HardwareInfo info, byte[] publicKey) { + if(createHardwareInfoUrl == null) { + return null; + } + try { + return requester.send(requester.post(createHardwareInfoUrl, new HardwareRequest(new HttpUserHardware(info, + publicKey, false)), bearerToken), HttpUserHardware.class).getOrThrow(); + } catch (IOException e) { + logger.error(e); + return null; + } + } + + @Override + public void connectUserAndHardware(UserSession userSession, UserHardware hardware) { + if(connectUserAndHardwareUrl == null) { + return; + } + try { + requester.send(requester.post(connectUserAndHardwareUrl, new HardwareRequest((HttpUserHardware) hardware, (HttpUserSession) userSession), bearerToken), Void.class); + } catch (IOException e) { + logger.error(e); + } + } + + @Override + public void addPublicKeyToHardwareInfo(UserHardware hardware, byte[] publicKey) { + if(addPublicKeyToHardwareInfoUrl == null) { + return; + } + try { + requester.send(requester.post(addPublicKeyToHardwareInfoUrl, new HardwareRequest((HttpUserHardware)hardware, publicKey), bearerToken), Void.class); + } catch (IOException e) { + logger.error(e); + } + } + + @Override + public Iterable getUsersByHardwareInfo(UserHardware hardware) { + if(getUsersByHardwareInfoUrl == null) { + return null; + } + try { + return requester.send(requester + .post(getUsersByHardwareInfoUrl, new HardwareRequest((HttpUserHardware) hardware), bearerToken), List.class).getOrThrow(); + } catch (IOException e) { + logger.error(e); + return null; + } + } + + @Override + public void banHardware(UserHardware hardware) { + if(banHardwareUrl == null) { + return; + } + try { + requester.send(requester.post(banHardwareUrl, new HardwareRequest((HttpUserHardware) hardware), bearerToken), Void.class); + } catch (IOException e) { + logger.error(e); + } + } + + @Override + public void unbanHardware(UserHardware hardware) { + if(unbanHardwareUrl == null) { + return; + } + try { + requester.send(requester.post(unbanHardwareUrl, new HardwareRequest((HttpUserHardware) hardware), bearerToken), Void.class); + } catch (IOException e) { + logger.error(e); + } + } + public record HttpAuthReport(String minecraftAccessToken, String oauthAccessToken, String oauthRefreshToken, long oauthExpire, HttpUserSession session) { @@ -246,7 +380,27 @@ public RefreshTokenRequest(String refreshToken, AuthResponse.AuthContext context } } - public static class HttpUser implements User, UserSupportTextures, UserSupportProperties { + public record HardwareRequest(HttpUserHardware userHardware, byte[] key, HttpUserSession userSession) { + + public HardwareRequest(HttpUserHardware userHardware) { + this(userHardware, null, null); + } + + public HardwareRequest(HttpUserHardware userHardware, byte[] key) { + this(userHardware, key, null); + } + + public HardwareRequest(HttpUserHardware userHardware, HttpUserSession userSession) { + this(userHardware, null, userSession); + } + + public HardwareRequest(byte[] key) { + this(null, key, null); + } + + } + + public class HttpUser implements User, UserSupportTextures, UserSupportProperties, UserSupportHardware { private String username; private UUID uuid; private String serverId; @@ -258,19 +412,22 @@ public static class HttpUser implements User, UserSupportTextures, UserSupportPr private Texture cloak; private Map assets; private Map properties; + private long hwidId; + private transient HttpUserHardware hardware; public HttpUser() { } - public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions) { + public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, long hwidId) { this.username = username; this.uuid = uuid; this.serverId = serverId; this.accessToken = accessToken; this.permissions = permissions; + this.hwidId = hwidId; } - public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Texture skin, Texture cloak) { + public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Texture skin, Texture cloak, long hwidId) { this.username = username; this.uuid = uuid; this.serverId = serverId; @@ -278,9 +435,10 @@ public HttpUser(String username, UUID uuid, String serverId, String accessToken, this.permissions = permissions; this.skin = skin; this.cloak = cloak; + this.hwidId = hwidId; } - public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Texture skin, Texture cloak, Map properties) { + public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Texture skin, Texture cloak, Map properties, long hwidId) { this.username = username; this.uuid = uuid; this.serverId = serverId; @@ -289,9 +447,10 @@ public HttpUser(String username, UUID uuid, String serverId, String accessToken, this.skin = skin; this.cloak = cloak; this.properties = properties; + this.hwidId = hwidId; } - public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Map assets, Map properties) { + public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Map assets, Map properties, long hwidId) { this.username = username; this.uuid = uuid; this.serverId = serverId; @@ -299,6 +458,7 @@ public HttpUser(String username, UUID uuid, String serverId, String accessToken, this.permissions = permissions; this.assets = assets; this.properties = properties; + this.hwidId = hwidId; } @Override @@ -374,8 +534,17 @@ public String toString() { ", permissions=" + permissions + ", assets=" + getAssets() + ", properties=" + properties + + ", hwidId=" + hwidId + '}'; } + + @Override + public UserHardware getHardware() { + if (hardware != null) return hardware; + HttpAuthCoreProvider.HttpUserHardware result = (HttpUserHardware) getHardwareInfoById(String.valueOf(hwidId)); + hardware = result; + return result; + } } public static class HttpUserSession implements UserSession { @@ -416,4 +585,65 @@ public String toString() { '}'; } } + + public static class HttpUserHardware implements UserHardware { + private final HardwareReportRequest.HardwareInfo hardwareInfo; + private final long id; + private byte[] publicKey; + private boolean banned; + + public HttpUserHardware(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, long id, boolean banned) { + this.hardwareInfo = hardwareInfo; + this.publicKey = publicKey; + this.id = id; + this.banned = banned; + } + + public HttpUserHardware(HardwareReportRequest.HardwareInfo hardwareInfo) { + this.hardwareInfo = hardwareInfo; + this.id = Long.MIN_VALUE; + } + + public HttpUserHardware(HardwareReportRequest.HardwareInfo hardwareInfo, byte[] publicKey, boolean banned) { + this.hardwareInfo = hardwareInfo; + this.publicKey = publicKey; + this.banned = banned; + this.id = Long.MIN_VALUE; + } + + public HttpUserHardware(long id) { + this.id = id; + this.hardwareInfo = null; + } + + @Override + public HardwareReportRequest.HardwareInfo getHardwareInfo() { + return hardwareInfo; + } + + @Override + public byte[] getPublicKey() { + return publicKey; + } + + @Override + public String getId() { + return String.valueOf(id); + } + + @Override + public boolean isBanned() { + return banned; + } + + @Override + public String toString() { + return "HttpUserHardware{" + + "hardwareInfo=" + hardwareInfo + + ", publicKey=" + (publicKey == null ? null : new String(Base64.getEncoder().encode(publicKey))) + + ", id=" + id + + ", banned=" + banned + + '}'; + } + } }