diff --git a/src/main/java/emu/grasscutter/auth/AuthenticationSystem.java b/src/main/java/emu/grasscutter/auth/AuthenticationSystem.java index ee27dbca0..90f2648c5 100644 --- a/src/main/java/emu/grasscutter/auth/AuthenticationSystem.java +++ b/src/main/java/emu/grasscutter/auth/AuthenticationSystem.java @@ -131,16 +131,4 @@ public interface AuthenticationSystem { return AuthenticationRequest.builder().request(request) .response(response).build(); } - - - /** - * Generates an authentication request from a {@link Response} object. - * @param request The Express request. - * @param jsonData The JSON data. - * @return An authentication request. - */ - static AuthenticationRequest fromOAuthRequest(Request request, Response response) { - return AuthenticationRequest.builder().request(request) - .response(response).build(); - } } diff --git a/src/main/java/emu/grasscutter/auth/OAuthAuthenticator.java b/src/main/java/emu/grasscutter/auth/OAuthAuthenticator.java index 394d14371..2d7a73454 100644 --- a/src/main/java/emu/grasscutter/auth/OAuthAuthenticator.java +++ b/src/main/java/emu/grasscutter/auth/OAuthAuthenticator.java @@ -14,15 +14,22 @@ public interface OAuthAuthenticator { void handleLogin(AuthenticationRequest request); /** - * Called when an client requests to redirect to login page. + * Called when a client requests to redirect to login page. * @param request The authentication request. */ - void handleDesktopRedirection(AuthenticationRequest request); - void handleMobileRedirection(AuthenticationRequest request); + void handleRedirection(AuthenticationRequest request, ClientType clientType); /** * Called when an OAuth login requests callback. * @param request The authentication request. */ void handleTokenProcess(AuthenticationRequest request); + + /** + * The type of the client. + * Used for handling redirection. + */ + enum ClientType { + DESKTOP, MOBILE + } } 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 96e1fda84..a5545877f 100644 --- a/src/main/java/emu/grasscutter/server/http/dispatch/DispatchHandler.java +++ b/src/main/java/emu/grasscutter/server/http/dispatch/DispatchHandler.java @@ -2,6 +2,8 @@ package emu.grasscutter.server.http.dispatch; import emu.grasscutter.Grasscutter; import emu.grasscutter.auth.AuthenticationSystem; +import emu.grasscutter.auth.OAuthAuthenticator; +import emu.grasscutter.auth.OAuthAuthenticator.ClientType; import emu.grasscutter.server.http.Router; import emu.grasscutter.server.http.objects.*; import emu.grasscutter.server.http.objects.ComboTokenReqJson.LoginTokenData; @@ -34,13 +36,15 @@ public final class DispatchHandler implements Router { express.post("/authentication/change_password", (request, response) -> Grasscutter.getAuthenticationSystem().getExternalAuthenticator() .handlePasswordReset(AuthenticationSystem.fromExternalRequest(request, response))); - // OAuth login - express.post("/hk4e_global/mdk/shield/api/loginByThirdparty", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator().handleLogin(AuthenticationSystem.fromOAuthRequest(request, response))); - // OAuth querystring convert redirection - express.get("/authentication/openid/redirect", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator().handleTokenProcess(AuthenticationSystem.fromOAuthRequest(request, response))); - // OAuth redirection - express.get("/Api/twitter_login", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator().handleDesktopRedirection(AuthenticationSystem.fromOAuthRequest(request, response))); - express.get("/sdkTwitterLogin.html", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator().handleMobileRedirection(AuthenticationSystem.fromOAuthRequest(request, response))); + // External login (from OAuth2). + express.post("/hk4e_global/mdk/shield/api/loginByThirdparty", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator() + .handleLogin(AuthenticationSystem.fromExternalRequest(request, response))); + express.get("/authentication/openid/redirect", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator() + .handleTokenProcess(AuthenticationSystem.fromExternalRequest(request, response))); + express.get("/Api/twitter_login", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator() + .handleRedirection(AuthenticationSystem.fromExternalRequest(request, response), ClientType.DESKTOP)); + express.get("/sdkTwitterLogin.html", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator() + .handleRedirection(AuthenticationSystem.fromExternalRequest(request, response), ClientType.MOBILE)); } /**