Add to HttpAuthCoreProvider the AuthSupportHardware implementation (#601)

* initial

* test fixes

* review fixes

* review fixes

* test fixes

* review fixes

* review fixes
This commit is contained in:
Clercq 2022-08-01 11:54:07 +03:00 committed by GitHub
parent 81b80a7938
commit 385f2d9ec5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,9 +7,13 @@
import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent;
import pro.gravit.launcher.profiles.Texture; import pro.gravit.launcher.profiles.Texture;
import pro.gravit.launcher.request.auth.AuthRequest; import pro.gravit.launcher.request.auth.AuthRequest;
import pro.gravit.launcher.request.secure.HardwareReportRequest;
import pro.gravit.launchserver.HttpRequester; import pro.gravit.launchserver.HttpRequester;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.auth.AuthException; 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.UserSupportProperties;
import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportTextures; import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportTextures;
import pro.gravit.launchserver.helper.HttpHelper; import pro.gravit.launchserver.helper.HttpHelper;
@ -19,12 +23,9 @@
import pro.gravit.utils.helper.CommonHelper; import pro.gravit.utils.helper.CommonHelper;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class HttpAuthCoreProvider extends AuthCoreProvider { public class HttpAuthCoreProvider extends AuthCoreProvider implements AuthSupportHardware {
private transient final Logger logger = LogManager.getLogger(); private transient final Logger logger = LogManager.getLogger();
private transient HttpRequester requester; private transient HttpRequester requester;
public String bearerToken; public String bearerToken;
@ -32,12 +33,23 @@ public class HttpAuthCoreProvider extends AuthCoreProvider {
public String getUserByLoginUrl; public String getUserByLoginUrl;
public String getUserByUUIDUrl; public String getUserByUUIDUrl;
public String getUserByTokenUrl; public String getUserByTokenUrl;
public String getAuthDetails; public String getAuthDetailsUrl;
public String refreshTokenUrl; public String refreshTokenUrl;
public String authorizeUrl; public String authorizeUrl;
public String joinServerUrl; public String joinServerUrl;
public String checkServerUrl; public String checkServerUrl;
public String updateServerIdUrl; 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 @Override
public User getUserByUsername(String username) { public User getUserByUsername(String username) {
try { try {
@ -73,11 +85,11 @@ public User getUserByUUID(UUID uuid) {
@Override @Override
public List<GetAvailabilityAuthRequestEvent.AuthAvailabilityDetails> getDetails(Client client) { public List<GetAvailabilityAuthRequestEvent.AuthAvailabilityDetails> getDetails(Client client) {
if(getAuthDetails == null) { if(getAuthDetailsUrl == null) {
return super.getDetails(client); return super.getDetails(client);
} }
try { 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; return result.details;
} catch (IOException e) { } catch (IOException e) {
logger.error(e); logger.error(e);
@ -133,6 +145,128 @@ public AuthManager.AuthReport authorize(String login, AuthResponse.AuthContext c
return result.getOrThrow().toAuthReport(); 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<HttpUserHardware, HttpRequester.SimpleError> 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<User> 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, public record HttpAuthReport(String minecraftAccessToken, String oauthAccessToken,
String oauthRefreshToken, long oauthExpire, String oauthRefreshToken, long oauthExpire,
HttpUserSession session) { 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 String username;
private UUID uuid; private UUID uuid;
private String serverId; private String serverId;
@ -258,19 +412,22 @@ public static class HttpUser implements User, UserSupportTextures, UserSupportPr
private Texture cloak; private Texture cloak;
private Map<String, Texture> assets; private Map<String, Texture> assets;
private Map<String, String> properties; private Map<String, String> properties;
private long hwidId;
private transient HttpUserHardware hardware;
public HttpUser() { 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.username = username;
this.uuid = uuid; this.uuid = uuid;
this.serverId = serverId; this.serverId = serverId;
this.accessToken = accessToken; this.accessToken = accessToken;
this.permissions = permissions; 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.username = username;
this.uuid = uuid; this.uuid = uuid;
this.serverId = serverId; this.serverId = serverId;
@ -278,9 +435,10 @@ public HttpUser(String username, UUID uuid, String serverId, String accessToken,
this.permissions = permissions; this.permissions = permissions;
this.skin = skin; this.skin = skin;
this.cloak = cloak; this.cloak = cloak;
this.hwidId = hwidId;
} }
public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Texture skin, Texture cloak, Map<String, String> properties) { public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Texture skin, Texture cloak, Map<String, String> properties, long hwidId) {
this.username = username; this.username = username;
this.uuid = uuid; this.uuid = uuid;
this.serverId = serverId; this.serverId = serverId;
@ -289,9 +447,10 @@ public HttpUser(String username, UUID uuid, String serverId, String accessToken,
this.skin = skin; this.skin = skin;
this.cloak = cloak; this.cloak = cloak;
this.properties = properties; this.properties = properties;
this.hwidId = hwidId;
} }
public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Map<String, Texture> assets, Map<String, String> properties) { public HttpUser(String username, UUID uuid, String serverId, String accessToken, ClientPermissions permissions, Map<String, Texture> assets, Map<String, String> properties, long hwidId) {
this.username = username; this.username = username;
this.uuid = uuid; this.uuid = uuid;
this.serverId = serverId; this.serverId = serverId;
@ -299,6 +458,7 @@ public HttpUser(String username, UUID uuid, String serverId, String accessToken,
this.permissions = permissions; this.permissions = permissions;
this.assets = assets; this.assets = assets;
this.properties = properties; this.properties = properties;
this.hwidId = hwidId;
} }
@Override @Override
@ -374,8 +534,17 @@ public String toString() {
", permissions=" + permissions + ", permissions=" + permissions +
", assets=" + getAssets() + ", assets=" + getAssets() +
", properties=" + properties + ", 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 { 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 +
'}';
}
}
} }