[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.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> 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;
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<String> 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();
}
}

View file

@ -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<User> 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<User> 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> T isSupport(Class<T> clazz) {
if (clazz.isAssignableFrom(getClass())) return (T) this;
return null;
}
@Override
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.UserSession;
public interface AuthSupportExit {
public interface AuthSupportExit extends AuthSupport {
boolean deleteSession(UserSession session);
boolean exitUser(User user);

View file

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

View file

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

View file

@ -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);

View file

@ -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;
}

View file

@ -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<String, String> properties);
}

View file

@ -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) {

View file

@ -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) {

View file

@ -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");

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