From 04d9613facceebe270bd24dc404404c55646af5e Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Sat, 14 May 2022 12:39:21 -0400 Subject: [PATCH] External authentication --- .../java/emu/grasscutter/Grasscutter.java | 2 -- .../auth/AuthenticationSystem.java | 20 +++++++++++ .../auth/DefaultAuthentication.java | 6 ++++ .../auth/DefaultAuthenticators.java | 20 +++++++++++ .../auth/ExternalAuthenticator.java | 33 +++++++++++++++++++ .../server/http/dispatch/DispatchHandler.java | 6 ++++ .../http/handlers/LegacyAuthHandler.java | 17 ---------- 7 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 src/main/java/emu/grasscutter/auth/ExternalAuthenticator.java delete mode 100644 src/main/java/emu/grasscutter/server/http/handlers/LegacyAuthHandler.java diff --git a/src/main/java/emu/grasscutter/Grasscutter.java b/src/main/java/emu/grasscutter/Grasscutter.java index 327aa174c..bc5144d97 100644 --- a/src/main/java/emu/grasscutter/Grasscutter.java +++ b/src/main/java/emu/grasscutter/Grasscutter.java @@ -35,7 +35,6 @@ import emu.grasscutter.utils.Language; import emu.grasscutter.server.game.GameServer; import emu.grasscutter.tools.Tools; import emu.grasscutter.utils.Crypto; -import emu.grasscutter.BuildConfig; import javax.annotation.Nullable; @@ -129,7 +128,6 @@ public final class Grasscutter { httpServer.addRouter(GenericHandler.class); httpServer.addRouter(AnnouncementsHandler.class); httpServer.addRouter(DispatchHandler.class); - httpServer.addRouter(LegacyAuthHandler.class); httpServer.addRouter(GachaHandler.class); // TODO: find a better place? diff --git a/src/main/java/emu/grasscutter/auth/AuthenticationSystem.java b/src/main/java/emu/grasscutter/auth/AuthenticationSystem.java index 096c4124c..41aba1c8e 100644 --- a/src/main/java/emu/grasscutter/auth/AuthenticationSystem.java +++ b/src/main/java/emu/grasscutter/auth/AuthenticationSystem.java @@ -2,6 +2,7 @@ package emu.grasscutter.auth; import emu.grasscutter.server.http.objects.*; import express.http.Request; +import express.http.Response; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -52,12 +53,20 @@ public interface AuthenticationSystem { */ Authenticator getSessionKeyAuthenticator(); + /** + * This is the authenticator used for handling external authentication requests. + * @return An authenticator. + */ + ExternalAuthenticator getExternalAuthenticator(); + /** * A data container that holds relevant data for authenticating a client. */ @Builder @AllArgsConstructor @Getter class AuthenticationRequest { private final Request request; + @Nullable private final Response response; + @Nullable private final LoginAccountRequestJson passwordRequest; @Nullable private final LoginTokenRequestJson tokenRequest; @Nullable private final ComboTokenReqJson sessionKeyRequest; @@ -104,4 +113,15 @@ public interface AuthenticationSystem { .sessionKeyData(tokenData) .build(); } + + /** + * Generates an authentication request from a {@link Response} object. + * @param request The Express request. + * @param response the Express response. + * @return An authentication request. + */ + static AuthenticationRequest fromExternalRequest(Request request, Response response) { + return AuthenticationRequest.builder().request(request) + .response(response).build(); + } } diff --git a/src/main/java/emu/grasscutter/auth/DefaultAuthentication.java b/src/main/java/emu/grasscutter/auth/DefaultAuthentication.java index b5e853cb0..08958d8e9 100644 --- a/src/main/java/emu/grasscutter/auth/DefaultAuthentication.java +++ b/src/main/java/emu/grasscutter/auth/DefaultAuthentication.java @@ -15,6 +15,7 @@ public final class DefaultAuthentication implements AuthenticationSystem { private final Authenticator passwordAuthenticator = new PasswordAuthenticator(); private final Authenticator tokenAuthenticator = new TokenAuthenticator(); private final Authenticator sessionKeyAuthenticator = new SessionKeyAuthenticator(); + private final ExternalAuthenticator externalAuthenticator = new ExternalAuthentication(); @Override public void createAccount(String username, String password) { @@ -46,4 +47,9 @@ public final class DefaultAuthentication implements AuthenticationSystem { public Authenticator getSessionKeyAuthenticator() { return this.sessionKeyAuthenticator; } + + @Override + public ExternalAuthenticator getExternalAuthenticator() { + return this.externalAuthenticator; + } } diff --git a/src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java b/src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java index 0239b6e09..e1d5fddf0 100644 --- a/src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java +++ b/src/main/java/emu/grasscutter/auth/DefaultAuthenticators.java @@ -158,4 +158,24 @@ public final class DefaultAuthenticators { return response; } } + + /** + * Handles authentication requests from external sources. + */ + public static class ExternalAuthentication implements ExternalAuthenticator { + @Override public void handleLogin(AuthenticationRequest request) { + assert request.getResponse() != null; + request.getResponse().send("Authentication is not available with the default authentication method."); + } + + @Override public void handleAccountCreation(AuthenticationRequest request) { + assert request.getResponse() != null; + request.getResponse().send("Authentication is not available with the default authentication method."); + } + + @Override public void handlePasswordReset(AuthenticationRequest request) { + assert request.getResponse() != null; + request.getResponse().send("Authentication is not available with the default authentication method."); + } + } } diff --git a/src/main/java/emu/grasscutter/auth/ExternalAuthenticator.java b/src/main/java/emu/grasscutter/auth/ExternalAuthenticator.java new file mode 100644 index 000000000..6bf78af6e --- /dev/null +++ b/src/main/java/emu/grasscutter/auth/ExternalAuthenticator.java @@ -0,0 +1,33 @@ +package emu.grasscutter.auth; + +import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest; + +/** + * Handles authentication via external routes. + */ +public interface ExternalAuthenticator { + + /** + * Called when an external login request is made. + * @param request The authentication request. + */ + void handleLogin(AuthenticationRequest request); + + /** + * Called when an external account creation request is made. + * @param request The authentication request. + * + * For developers: Use {@link AuthenticationRequest#getRequest()} to get the request body. + * Use {@link AuthenticationRequest#getResponse()} to get the response body. + */ + void handleAccountCreation(AuthenticationRequest request); + + /** + * Called when an external password reset request is made. + * @param request The authentication request. + * + * For developers: Use {@link AuthenticationRequest#getRequest()} to get the request body. + * Use {@link AuthenticationRequest#getResponse()} to get the response body. + */ + void handlePasswordReset(AuthenticationRequest request); +} diff --git a/src/main/java/emu/grasscutter/server/http/dispatch/DispatchHandler.java b/src/main/java/emu/grasscutter/server/http/dispatch/DispatchHandler.java index 22a31fe6a..5b012c4c3 100644 --- a/src/main/java/emu/grasscutter/server/http/dispatch/DispatchHandler.java +++ b/src/main/java/emu/grasscutter/server/http/dispatch/DispatchHandler.java @@ -24,6 +24,12 @@ public final class DispatchHandler implements Router { express.post("/hk4e_global/mdk/shield/api/verify", DispatchHandler::tokenLogin); // Combo token login (from session key). express.post("/hk4e_global/combo/granter/login/v2/login", DispatchHandler::sessionKeyLogin); + + // External login (from other clients). + express.get("/authentication/type", (request, response) -> response.send(Grasscutter.getAuthenticationSystem().getClass().getSimpleName())); + express.post("/authentication/login", (request, response) -> response.status(500).send("{\"notice\":\"This API is deprecated.\"}")); + express.post("/authentication/register", (request, response) -> response.status(500).send("{\"notice\":\"This API is deprecated.\"}")); + express.post("/authentication/change_password", (request, response) -> response.status(500).send("{\"notice\":\"This API is deprecated.\"}")); } /** diff --git a/src/main/java/emu/grasscutter/server/http/handlers/LegacyAuthHandler.java b/src/main/java/emu/grasscutter/server/http/handlers/LegacyAuthHandler.java deleted file mode 100644 index 943a56d7e..000000000 --- a/src/main/java/emu/grasscutter/server/http/handlers/LegacyAuthHandler.java +++ /dev/null @@ -1,17 +0,0 @@ -package emu.grasscutter.server.http.handlers; - -import emu.grasscutter.server.http.Router; -import express.Express; -import io.javalin.Javalin; - -/** - * Handles the legacy authentication system routes. - */ -public final class LegacyAuthHandler implements Router { - @Override public void applyRoutes(Express express, Javalin handle) { - express.get("/authentication/type", (request, response) -> response.status(500).send("{\"notice\":\"This API is deprecated.\"}")); - express.post("/authentication/login", (request, response) -> response.status(500).send("{\"notice\":\"This API is deprecated.\"}")); - express.post("/authentication/register", (request, response) -> response.status(500).send("{\"notice\":\"This API is deprecated.\"}")); - express.post("/authentication/change_password", (request, response) -> response.status(500).send("{\"notice\":\"This API is deprecated.\"}")); - } -} \ No newline at end of file