mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE] Хуки авторизации
This commit is contained in:
parent
6ec3211450
commit
dd596805e1
9 changed files with 59 additions and 82 deletions
|
@ -6,6 +6,7 @@
|
|||
import ru.gravit.launchserver.auth.provider.AuthProvider;
|
||||
import ru.gravit.launchserver.socket.Client;
|
||||
import ru.gravit.launchserver.websocket.json.auth.AuthResponse;
|
||||
import ru.gravit.utils.HookException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
@ -18,7 +19,7 @@ public void preInit(LaunchServer launchServer) {
|
|||
|
||||
@Override
|
||||
public void init(LaunchServer launchServer) {
|
||||
launchServer.authHookManager.registerPreHook(this::preAuthHook);
|
||||
launchServer.authHookManager.preHook.registerHook(this::preAuthHook);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -26,10 +27,11 @@ public void postInit(LaunchServer launchServer) {
|
|||
|
||||
}
|
||||
|
||||
public void preAuthHook(AuthResponse.AuthContext context, Client client) throws AuthException {
|
||||
public boolean preAuthHook(AuthResponse.AuthContext context, Client client) {
|
||||
if (isLimit(context.ip)) {
|
||||
AuthProvider.authError(message);
|
||||
throw new HookException(message);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static class AuthEntry {
|
||||
|
|
|
@ -1,75 +1,16 @@
|
|||
package ru.gravit.launchserver.manangers.hook;
|
||||
|
||||
import ru.gravit.launchserver.auth.AuthException;
|
||||
import ru.gravit.launchserver.socket.Client;
|
||||
import ru.gravit.launchserver.websocket.json.auth.AuthResponse;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import ru.gravit.launchserver.websocket.json.auth.CheckServerResponse;
|
||||
import ru.gravit.launchserver.websocket.json.auth.JoinServerResponse;
|
||||
import ru.gravit.launchserver.websocket.json.auth.SetProfileResponse;
|
||||
import ru.gravit.utils.BiHookSet;
|
||||
|
||||
public class AuthHookManager {
|
||||
private Set<AuthPreHook> PRE_HOOKS = new HashSet<>();
|
||||
private Set<AuthPostHook> POST_HOOKS = new HashSet<>();
|
||||
private Set<CheckServerHook> CHECKSERVER_HOOKS = new HashSet<>();
|
||||
private Set<JoinServerHook> JOINSERVER_HOOKS = new HashSet<>();
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AuthPreHook {
|
||||
void preAuthHook(AuthResponse.AuthContext context, Client client) throws AuthException;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AuthPostHook {
|
||||
void postAuthHook(AuthResponse.AuthContext context, Client client) throws AuthException;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface CheckServerHook {
|
||||
void checkServerHook(String username, String serverID) throws AuthException;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface JoinServerHook {
|
||||
void joinServerHook(String username, String accessToken, String serverID) throws AuthException;
|
||||
}
|
||||
|
||||
public void registerPostHook(AuthPostHook hook) {
|
||||
POST_HOOKS.add(hook);
|
||||
}
|
||||
|
||||
public void registerJoinServerHook(JoinServerHook hook) {
|
||||
JOINSERVER_HOOKS.add(hook);
|
||||
}
|
||||
|
||||
public void registerCheckServerHook(CheckServerHook hook) {
|
||||
CHECKSERVER_HOOKS.add(hook);
|
||||
}
|
||||
|
||||
public void registerPreHook(AuthPreHook hook) {
|
||||
PRE_HOOKS.add(hook);
|
||||
}
|
||||
|
||||
public void preHook(AuthResponse.AuthContext context, Client client) throws AuthException {
|
||||
for (AuthPreHook preHook : PRE_HOOKS) {
|
||||
preHook.preAuthHook(context, client);
|
||||
}
|
||||
}
|
||||
|
||||
public void checkServerHook(String username, String serverID) throws AuthException {
|
||||
for (CheckServerHook hook : CHECKSERVER_HOOKS) {
|
||||
hook.checkServerHook(username, serverID);
|
||||
}
|
||||
}
|
||||
|
||||
public void joinServerHook(String username, String accessToken, String serverID) throws AuthException {
|
||||
for (JoinServerHook hook : JOINSERVER_HOOKS) {
|
||||
hook.joinServerHook(username, accessToken, serverID);
|
||||
}
|
||||
}
|
||||
|
||||
public void postHook(AuthResponse.AuthContext context, Client client) throws AuthException {
|
||||
for (AuthPostHook postHook : POST_HOOKS) {
|
||||
postHook.postAuthHook(context, client);
|
||||
}
|
||||
}
|
||||
public BiHookSet<AuthResponse.AuthContext, Client> preHook = new BiHookSet<>();
|
||||
public BiHookSet<AuthResponse.AuthContext, Client> postHook = new BiHookSet<>();
|
||||
public BiHookSet<CheckServerResponse, Client> checkServerHook = new BiHookSet<>();
|
||||
public BiHookSet<JoinServerResponse, Client> joinServerHook = new BiHookSet<>();
|
||||
public BiHookSet<SetProfileResponse, Client> setProfileHook = new BiHookSet<>();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
import ru.gravit.launcher.OshiHWID;
|
||||
import ru.gravit.launcher.events.request.AuthRequestEvent;
|
||||
import ru.gravit.launcher.profiles.ClientProfile;
|
||||
import ru.gravit.launcher.request.RequestException;
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.launchserver.auth.AuthException;
|
||||
import ru.gravit.launchserver.auth.AuthProviderPair;
|
||||
|
@ -13,6 +14,7 @@
|
|||
import ru.gravit.launchserver.socket.Client;
|
||||
import ru.gravit.launchserver.websocket.json.SimpleResponse;
|
||||
import ru.gravit.launchserver.websocket.json.profile.ProfileByUUIDResponse;
|
||||
import ru.gravit.utils.HookException;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
import ru.gravit.utils.helper.SecurityHelper;
|
||||
|
@ -84,7 +86,7 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti
|
|||
else pair = LaunchServer.server.config.getAuthProviderPair(auth_id);
|
||||
AuthContext context = new AuthContext(0, login, password.length(), customText, client, ip, null, false);
|
||||
AuthProvider provider = pair.provider;
|
||||
LaunchServer.server.authHookManager.preHook(context, clientData);
|
||||
LaunchServer.server.authHookManager.preHook.hook(context, clientData);
|
||||
provider.preAuth(login, password, customText, ip);
|
||||
AuthProviderResult aresult = provider.auth(login, password, ip);
|
||||
if (!VerifyHelper.isValidUsername(aresult.username)) {
|
||||
|
@ -105,7 +107,7 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti
|
|||
//}
|
||||
if (authType == ConnectTypes.CLIENT)
|
||||
LaunchServer.server.config.hwidHandler.check(hwid, aresult.username);
|
||||
LaunchServer.server.authHookManager.postHook(context, clientData);
|
||||
LaunchServer.server.authHookManager.postHook.hook(context, clientData);
|
||||
clientData.isAuth = true;
|
||||
clientData.permissions = aresult.permissions;
|
||||
clientData.auth_id = auth_id;
|
||||
|
@ -125,7 +127,7 @@ public void execute(ChannelHandlerContext ctx, Client clientData) throws Excepti
|
|||
LogHelper.debug("Auth: %s accessToken %s uuid: %s", login, result.accessToken, uuid.toString());
|
||||
}
|
||||
sendResult(result);
|
||||
} catch (AuthException | HWIDException e) {
|
||||
} catch (AuthException | HWIDException | HookException e) {
|
||||
sendError(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
import ru.gravit.launchserver.socket.Client;
|
||||
import ru.gravit.launchserver.websocket.json.SimpleResponse;
|
||||
import ru.gravit.launchserver.websocket.json.profile.ProfileByUUIDResponse;
|
||||
import ru.gravit.utils.HookException;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
public class CheckServerResponse extends SimpleResponse {
|
||||
|
@ -23,11 +24,12 @@ public String getType() {
|
|||
public void execute(ChannelHandlerContext ctx, Client pClient) {
|
||||
CheckServerRequestEvent result = new CheckServerRequestEvent();
|
||||
try {
|
||||
server.authHookManager.checkServerHook.hook(this, pClient);
|
||||
result.uuid = pClient.auth.handler.checkServer(username, serverID);
|
||||
if (result.uuid != null)
|
||||
result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server, result.uuid, username, client, pClient.auth.textureProvider);
|
||||
LogHelper.debug("checkServer: %s uuid: %s serverID: %s", result.playerProfile.username, result.uuid.toString(), serverID);
|
||||
} catch (AuthException e) {
|
||||
} catch (AuthException | HookException e) {
|
||||
sendError(e.getMessage());
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
import ru.gravit.launchserver.auth.AuthException;
|
||||
import ru.gravit.launchserver.socket.Client;
|
||||
import ru.gravit.launchserver.websocket.json.SimpleResponse;
|
||||
import ru.gravit.utils.HookException;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
public class JoinServerResponse extends SimpleResponse {
|
||||
|
@ -17,11 +18,11 @@ public class JoinServerResponse extends SimpleResponse {
|
|||
public String getType() {
|
||||
return "joinServer";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(ChannelHandlerContext ctx, Client client) {
|
||||
boolean success;
|
||||
try {
|
||||
server.authHookManager.joinServerHook.hook(this, client);
|
||||
if(client.auth == null)
|
||||
{
|
||||
LogHelper.warning("Client auth is null. Using default.");
|
||||
|
@ -29,7 +30,7 @@ public void execute(ChannelHandlerContext ctx, Client client) {
|
|||
}
|
||||
else success = client.auth.handler.joinServer(username, accessToken, serverID);
|
||||
LogHelper.debug("joinServer: %s accessToken: %s serverID: %s", username, accessToken, serverID);
|
||||
} catch (AuthException e) {
|
||||
} catch (AuthException | HookException e) {
|
||||
sendError(e.getMessage());
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.launchserver.socket.Client;
|
||||
import ru.gravit.launchserver.websocket.json.SimpleResponse;
|
||||
import ru.gravit.utils.HookException;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
|
@ -23,6 +24,12 @@ public void execute(ChannelHandlerContext ctx, Client client) {
|
|||
sendError("Access denied");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
server.authHookManager.setProfileHook.hook(this, client);
|
||||
} catch (HookException e)
|
||||
{
|
||||
sendError(e.getMessage());
|
||||
}
|
||||
Collection<ClientProfile> profiles = LaunchServer.server.getProfiles();
|
||||
for (ClientProfile p : profiles) {
|
||||
if (p.getTitle().equals(this.client)) {
|
||||
|
|
|
@ -8,7 +8,7 @@ public class BiHookSet<V,R> {
|
|||
@FunctionalInterface
|
||||
public interface Hook<V, R>
|
||||
{
|
||||
boolean hook(V object, R context);
|
||||
boolean hook(V object, R context) throws HookException;
|
||||
}
|
||||
public void registerHook(Hook<V, R> hook)
|
||||
{
|
||||
|
@ -18,11 +18,11 @@ public boolean unregisterHook(Hook<V, R> hook)
|
|||
{
|
||||
return list.remove(hook);
|
||||
}
|
||||
public boolean hook(V object, R context)
|
||||
public boolean hook(V context, R object) throws HookException
|
||||
{
|
||||
for(Hook<V, R> hook : list)
|
||||
{
|
||||
if(hook.hook(object, context)) return true;
|
||||
if(hook.hook(context, object)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
22
libLauncher/src/main/java/ru/gravit/utils/HookException.java
Normal file
22
libLauncher/src/main/java/ru/gravit/utils/HookException.java
Normal file
|
@ -0,0 +1,22 @@
|
|||
package ru.gravit.utils;
|
||||
|
||||
public class HookException extends RuntimeException {
|
||||
public HookException() {
|
||||
}
|
||||
|
||||
public HookException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public HookException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public HookException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public HookException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ public class HookSet<R> {
|
|||
@FunctionalInterface
|
||||
public interface Hook<R>
|
||||
{
|
||||
boolean hook(R context);
|
||||
boolean hook(R context) throws HookException;
|
||||
}
|
||||
public void registerHook(Hook<R> hook)
|
||||
{
|
||||
|
@ -18,7 +18,7 @@ public boolean unregisterHook(Hook<R> hook)
|
|||
{
|
||||
return list.remove(hook);
|
||||
}
|
||||
public boolean hook(R context)
|
||||
public boolean hook(R context) throws HookException
|
||||
{
|
||||
for(Hook<R> hook : list)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue