This commit is contained in:
Clercq 2022-07-22 19:29:00 +03:00
parent b7a6cfda6d
commit 915f6ff50d

View file

@ -1,5 +1,6 @@
package pro.gravit.launchserver.auth.core;
import com.mysql.cj.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pro.gravit.launcher.ClientPermissions;
@ -7,24 +8,24 @@
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;
import pro.gravit.launchserver.manangers.AuthManager;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.auth.AuthResponse;
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<GetAvailabilityAuthRequestEvent.AuthAvailabilityDetails> 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,124 @@ public AuthManager.AuthReport authorize(String login, AuthResponse.AuthContext c
return result.getOrThrow().toAuthReport();
}
@Override
public UserHardware getHardwareInfoByPublicKey(byte[] publicKey) {
if(StringUtils.isNullOrEmpty(getHardwareInfoByPublicKeyUrl)) {
return null;
}
try {
return requester.send(requester.post(getHardwareInfoByPublicKeyUrl, new HardwareRequest(publicKey),
null), HttpUserHardware.class).getOrThrow();
} catch (IOException e) {
logger.error(e);
return null;
}
}
@Override
public UserHardware getHardwareInfoByData(HardwareReportRequest.HardwareInfo info) {
if(StringUtils.isNullOrEmpty(getHardwareInfoByDataUrl)) {
return null;
}
try {
return requester.send(requester.post(getHardwareInfoByDataUrl, new HardwareRequest(new HttpUserHardware(info)),
null), HttpUserHardware.class).getOrThrow();
} catch (IOException e) {
logger.error(e);
return null;
}
}
@Override
public UserHardware getHardwareInfoById(String id) {
if(StringUtils.isNullOrEmpty(getHardwareInfoByIdUrl)) {
return null;
}
try {
return requester.send(requester.post(getHardwareInfoByIdUrl, new HardwareRequest(new HttpUserHardware(Long.parseLong(id))),
null), HttpUserHardware.class).getOrThrow();
} catch (IOException | NumberFormatException e) {
logger.error(e);
return null;
}
}
@Override
public UserHardware createHardwareInfo(HardwareReportRequest.HardwareInfo info, byte[] publicKey) {
if(StringUtils.isNullOrEmpty(createHardwareInfoUrl)) {
return null;
}
try {
return requester.send(requester.post(createHardwareInfoUrl, new HardwareRequest(new HttpUserHardware(info, publicKey, false)),
null), HttpUserHardware.class).getOrThrow();
} catch (IOException e) {
logger.error(e);
return null;
}
}
@Override
public void connectUserAndHardware(UserSession userSession, UserHardware hardware) {
if(StringUtils.isNullOrEmpty(connectUserAndHardwareUrl)) {
return;
}
try {
requester.send(requester.post(connectUserAndHardwareUrl, new HardwareRequest(userSession, hardware), null), Void.class);
} catch (IOException e) {
logger.error(e);
}
}
@Override
public void addPublicKeyToHardwareInfo(UserHardware hardware, byte[] publicKey) {
if(StringUtils.isNullOrEmpty(addPublicKeyToHardwareInfoUrl)) {
return;
}
try {
requester.send(requester.post(addPublicKeyToHardwareInfoUrl, new HardwareRequest(hardware, publicKey), null), Void.class);
} catch (IOException e) {
logger.error(e);
}
}
@Override
public Iterable<User> getUsersByHardwareInfo(UserHardware hardware) {
if(StringUtils.isNullOrEmpty(getUsersByHardwareInfoUrl)) {
return null;
}
try {
return requester.send(requester.post(getUsersByHardwareInfoUrl, new HardwareRequest(hardware),
null), List.class).getOrThrow();
} catch (IOException e) {
logger.error(e);
return null;
}
}
@Override
public void banHardware(UserHardware hardware) {
if(StringUtils.isNullOrEmpty(banHardwareUrl)) {
return;
}
try {
requester.send(requester.post(banHardwareUrl, new HardwareRequest(hardware), null), Void.class);
} catch (IOException e) {
logger.error(e);
}
}
@Override
public void unbanHardware(UserHardware hardware) {
if(StringUtils.isNullOrEmpty(unbanHardwareUrl)) {
return;
}
try {
requester.send(requester.post(unbanHardwareUrl, new HardwareRequest(hardware), null), Void.class);
} catch (IOException e) {
logger.error(e);
}
}
public record HttpAuthReport(String minecraftAccessToken, String oauthAccessToken,
String oauthRefreshToken, long oauthExpire,
HttpUserSession session) {
@ -246,7 +376,31 @@ public RefreshTokenRequest(String refreshToken, AuthResponse.AuthContext context
}
}
public static class HttpUser implements User, UserSupportTextures, UserSupportProperties {
public static class HardwareRequest {
public UserHardware userHardware;
public byte[] key;
public UserSession userSession;
public HardwareRequest(UserHardware userHardware) {
this.userHardware = userHardware;
}
public HardwareRequest(UserHardware userHardware, byte[] key) {
this.userHardware = userHardware;
this.key = key;
}
public HardwareRequest(UserSession session, UserHardware hardware) {
this.userSession = session;
this.userHardware = hardware;
}
public HardwareRequest(byte[] key) {
this.key = key;
}
}
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<String, Texture> assets;
private Map<String, String> 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<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.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<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.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
@ -376,6 +536,14 @@ public String toString() {
", properties=" + properties +
'}';
}
@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 +584,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 +
'}';
}
}
}