[FEATURE] AuthCoreProvider features

This commit is contained in:
Gravita 2021-06-15 19:24:28 +07:00
parent 201b6826ed
commit 9fe1fc4f23
13 changed files with 88 additions and 71 deletions

View file

@ -12,8 +12,8 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
public class AuthProviderPair { public final class AuthProviderPair {
public final boolean isDefault = true; public boolean isDefault = true;
public AuthProvider provider; public AuthProvider provider;
public AuthHandler handler; public AuthHandler handler;
public TextureProvider textureProvider; public TextureProvider textureProvider;
@ -30,7 +30,12 @@ public AuthProviderPair(AuthProvider provider, AuthHandler handler, TextureProvi
this.textureProvider = textureProvider; this.textureProvider = textureProvider;
} }
public void init(LaunchServer srv, String name) { public final <T> T isSupport(Class<T> clazz) {
if (core == null) return null;
return core.isSupport(clazz);
}
public final void init(LaunchServer srv, String name) {
this.name = name; this.name = name;
if (links != null) link(srv); if (links != null) link(srv);
if (core == null) { 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) -> { links.forEach((k, v) -> {
AuthProviderPair pair = srv.config.getAuthProviderPair(v); AuthProviderPair pair = srv.config.getAuthProviderPair(v);
if (pair == null) { 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) { if (social != null) {
social.close(); social.close();
} }
@ -117,15 +122,15 @@ public static void getFeatures(Class<?> clazz, Set<String> list) {
} }
} }
public boolean isUseCore() { public final boolean isUseCore() {
return core != null; return core != null;
} }
public boolean isUseSocial() { public final boolean isUseSocial() {
return core != null && social != null; return core != null && social != null;
} }
public boolean isUseProviderAndHandler() { public final boolean isUseProviderAndHandler() {
return !isUseCore(); return !isUseCore();
} }
} }

View file

@ -155,8 +155,9 @@ public void invoke(String... args) throws Exception {
} }
} }
}); });
if (this instanceof AuthSupportGetAllUsers) { {
AuthSupportGetAllUsers instance = (AuthSupportGetAllUsers) this; var instance = isSupport(AuthSupportGetAllUsers.class);
if (instance != null) {
map.put("getallusers", new SubCommand("(limit)", "print all users information") { map.put("getallusers", new SubCommand("(limit)", "print all users information") {
@Override @Override
public void invoke(String... args) throws Exception { public void invoke(String... args) throws Exception {
@ -173,8 +174,9 @@ public void invoke(String... args) throws Exception {
} }
}); });
} }
if (this instanceof AuthSupportHardware) { }
AuthSupportHardware instance = (AuthSupportHardware) this; {
var instance = isSupport(AuthSupportHardware.class);
map.put("gethardwarebyid", new SubCommand("[id]", "get hardware by id") { map.put("gethardwarebyid", new SubCommand("[id]", "get hardware by id") {
@Override @Override
public void invoke(String... args) throws Exception { 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); return user.getUsername().equals(username) && user.getAccessToken().equals(accessToken) && updateServerID(user, serverID);
} }
@SuppressWarnings("unchecked")
public <T> T isSupport(Class<T> clazz) {
if (clazz.isAssignableFrom(getClass())) return (T) this;
return null;
}
@Override @Override
public abstract void close() throws IOException; public abstract void close() throws IOException;

View file

@ -0,0 +1,4 @@
package pro.gravit.launchserver.auth.core.interfaces.provider;
public interface AuthSupport {
}

View file

@ -3,7 +3,7 @@
import pro.gravit.launchserver.auth.core.User; import pro.gravit.launchserver.auth.core.User;
import pro.gravit.launchserver.auth.core.UserSession; import pro.gravit.launchserver.auth.core.UserSession;
public interface AuthSupportExit { public interface AuthSupportExit extends AuthSupport {
boolean deleteSession(UserSession session); boolean deleteSession(UserSession session);
boolean exitUser(User user); boolean exitUser(User user);

View file

@ -4,6 +4,6 @@
import pro.gravit.launchserver.auth.core.User; import pro.gravit.launchserver.auth.core.User;
@Feature("users") @Feature("users")
public interface AuthSupportGetAllUsers { public interface AuthSupportGetAllUsers extends AuthSupport {
Iterable<User> getAllUsers(); Iterable<User> getAllUsers();
} }

View file

@ -7,8 +7,6 @@
import java.util.List; import java.util.List;
@Feature("sessions") @Feature("sessions")
public interface AuthSupportGetSessionsFromUser { public interface AuthSupportGetSessionsFromUser extends AuthSupport {
List<UserSession> getSessionsByUser(User user); List<UserSession> getSessionsByUser(User user);
void clearSessionsByUser(User user);
} }

View file

@ -9,7 +9,7 @@
import java.util.Arrays; import java.util.Arrays;
public interface AuthSupportHardware { public interface AuthSupportHardware extends AuthSupport {
UserHardware getHardwareInfoByPublicKey(byte[] publicKey); UserHardware getHardwareInfoByPublicKey(byte[] publicKey);
UserHardware getHardwareInfoByData(HardwareReportRequest.HardwareInfo info); UserHardware getHardwareInfoByData(HardwareReportRequest.HardwareInfo info);

View file

@ -5,7 +5,7 @@
import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportMoney; import pro.gravit.launchserver.auth.core.interfaces.user.UserSupportMoney;
@Feature("money") @Feature("money")
public interface AuthSupportMoney { public interface AuthSupportMoney extends AuthSupport {
default UserSupportMoney fetchUserMoney(User user) { default UserSupportMoney fetchUserMoney(User user) {
return (UserSupportMoney) user; return (UserSupportMoney) user;
} }

View file

@ -7,6 +7,6 @@
import java.util.Map; import java.util.Map;
@Feature("registration") @Feature("registration")
public interface AuthSupportRegistration { public interface AuthSupportRegistration extends AuthSupport {
User registration(String login, String email, AuthRequest.AuthPasswordInterface password, Map<String, String> properties); User registration(String login, String email, AuthRequest.AuthPasswordInterface password, Map<String, String> properties);
} }

View file

@ -5,7 +5,7 @@
import java.time.LocalDateTime; 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); UserSupportBanInfo.UserBanInfo banUser(User user, String reason, String moderator, LocalDateTime startTime, LocalDateTime endTime);
default UserSupportBanInfo.UserBanInfo banUser(User user) { default UserSupportBanInfo.UserBanInfo banUser(User user) {

View file

@ -67,8 +67,9 @@ public void onHardwareReport(HardwareReportResponse response, Client client) {
return; return;
} }
logger.debug("HardwareInfo received"); logger.debug("HardwareInfo received");
if (client.auth.core instanceof AuthSupportHardware) { {
AuthSupportHardware authSupportHardware = (AuthSupportHardware) client.auth; var authSupportHardware = client.auth.isSupport(AuthSupportHardware.class);
if (authSupportHardware != null) {
UserHardware hardware = authSupportHardware.getHardwareInfoByData(response.hardware); UserHardware hardware = authSupportHardware.getHardwareInfoByData(response.hardware);
if (hardware == null) { if (hardware == null) {
hardware = authSupportHardware.createHardwareInfo(response.hardware, client.trustLevel.publicKey); hardware = authSupportHardware.createHardwareInfo(response.hardware, client.trustLevel.publicKey);
@ -88,6 +89,7 @@ public void onHardwareReport(HardwareReportResponse response, Client client) {
provider.createHardwareInfo(response.hardware, client.trustLevel.publicKey, client); provider.createHardwareInfo(response.hardware, client.trustLevel.publicKey, client);
client.trustLevel.hardwareInfo = response.hardware; client.trustLevel.hardwareInfo = response.hardware;
} }
}
} catch (HWIDException e) { } catch (HWIDException e) {
throw new SecurityException(e.getMessage()); throw new SecurityException(e.getMessage());
} }
@ -97,28 +99,21 @@ public void onHardwareReport(HardwareReportResponse response, Client client) {
@Override @Override
public VerifySecureLevelKeyRequestEvent onSuccessVerify(Client client) { public VerifySecureLevelKeyRequestEvent onSuccessVerify(Client client) {
if (enableHardwareFeature) { if (enableHardwareFeature) {
if (client.isAuth && client.auth.core instanceof AuthSupportHardware) { var authSupportHardware = client.auth.isSupport(AuthSupportHardware.class);
UserHardware hardware = ((AuthSupportHardware) client.auth.core).getHardwareInfoByPublicKey(client.trustLevel.publicKey); if (authSupportHardware != null) {
UserHardware hardware = authSupportHardware.getHardwareInfoByPublicKey(client.trustLevel.publicKey);
if (hardware == null) //HWID not found? if (hardware == null) //HWID not found?
return new VerifySecureLevelKeyRequestEvent(true, false, createPublicKeyToken(client.username, client.trustLevel.publicKey)); return new VerifySecureLevelKeyRequestEvent(true, false, createPublicKeyToken(client.username, client.trustLevel.publicKey));
if (hardware.isBanned()) { if (hardware.isBanned()) {
throw new SecurityException("Your hardware banned"); throw new SecurityException("Your hardware banned");
} }
client.trustLevel.hardwareInfo = hardware.getHardwareInfo(); client.trustLevel.hardwareInfo = hardware.getHardwareInfo();
authSupportHardware.connectUserAndHardware(client.getUser(), hardware);
} else if (provider == null) { } else if (provider == null) {
logger.warn("HWIDProvider null. HardwareInfo not checked!"); logger.warn("HWIDProvider null. HardwareInfo not checked!");
} else { } else {
try { 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? if (client.trustLevel.hardwareInfo == null) //HWID not found?
return new VerifySecureLevelKeyRequestEvent(true, false, createPublicKeyToken(client.username, client.trustLevel.publicKey)); return new VerifySecureLevelKeyRequestEvent(true, false, createPublicKeyToken(client.username, client.trustLevel.publicKey));
} catch (HWIDException e) { } catch (HWIDException e) {

View file

@ -6,7 +6,8 @@
import pro.gravit.launcher.events.RequestEvent; import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.launcher.events.request.ExitRequestEvent; import pro.gravit.launcher.events.request.ExitRequestEvent;
import pro.gravit.launchserver.LaunchServer; 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.Client;
import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler; import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler;
import pro.gravit.launchserver.socket.response.SimpleResponse; import pro.gravit.launchserver.socket.response.SimpleResponse;
@ -48,12 +49,18 @@ public void execute(ChannelHandlerContext ctx, Client client) {
Client newClient = new Client(null); Client newClient = new Client(null);
newClient.checkSign = client.checkSign; newClient.checkSign = client.checkSign;
handler.setClient(newClient); handler.setClient(newClient);
AuthSupportExit supportExit = client.auth.core.isSupport(AuthSupportExit.class);
if (supportExit != null) {
if (exitAll) { if (exitAll) {
if (client.auth instanceof AuthSupportGetSessionsFromUser) { supportExit.exitUser(client.getUser());
AuthSupportGetSessionsFromUser support = (AuthSupportGetSessionsFromUser) client.auth; } else {
support.clearSessionsByUser(client.getUser()); UserSession session = client.sessionObject;
if (session != null) {
supportExit.deleteSession(session);
} }
} }
}
sendResult(new ExitRequestEvent(ExitRequestEvent.ExitReason.CLIENT));
} else { } else {
if (client.session == null && exitAll) { if (client.session == null && exitAll) {
sendError("Session invalid"); sendError("Session invalid");

@ -1 +1 @@
Subproject commit 566a42d1a09a2504ab4f9f6fb8c9a95d99c7fdca Subproject commit ca22fda40f690015edf7650f4365b8efa8e96ec8