diff --git a/build.gradle b/build.gradle index c862b58..f86a40c 100644 --- a/build.gradle +++ b/build.gradle @@ -10,15 +10,15 @@ sourceCompatibility = 17 targetCompatibility = 17 group 'com.xtaolabs.gcauth_oauth' -version '1.0.0' +version '1.1.0' repositories { mavenCentral() } dependencies { - implementation files('lib/grasscutter-1.1.1-dev.jar') - implementation files('lib/gcauth-2.1.5.jar') + implementation files('lib/grasscutter-1.1.2-dev.jar') + implementation files('lib/gcauth-2.2.1.jar') } test { diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/GCAuth_OAuth.java b/src/main/java/com/xtaolabs/gcauth_oauth/GCAuth_OAuth.java index 952a391..a618e4f 100644 --- a/src/main/java/com/xtaolabs/gcauth_oauth/GCAuth_OAuth.java +++ b/src/main/java/com/xtaolabs/gcauth_oauth/GCAuth_OAuth.java @@ -1,16 +1,13 @@ package com.xtaolabs.gcauth_oauth; +import com.xtaolabs.gcauth_oauth.handler.*; + import emu.grasscutter.Grasscutter; +import emu.grasscutter.auth.DefaultAuthentication; import emu.grasscutter.plugin.Plugin; +import emu.grasscutter.server.http.HttpServer; import static emu.grasscutter.Configuration.*; -import com.xtaolabs.gcauth_oauth.handler.JsonHandler; -import com.xtaolabs.gcauth_oauth.handler.VerifyHandler; -import com.xtaolabs.gcauth_oauth.handler.RequestHandler; - -import emu.grasscutter.server.dispatch.DispatchHttpJsonHandler; -import express.Express; - import io.javalin.http.staticfiles.Location; import java.io.File; @@ -31,28 +28,21 @@ public class GCAuth_OAuth extends Plugin { @Override public void onDisable() { - Express app = Grasscutter.getDispatchServer().getServer(); - app.disable("/Api/twitter_login"); + Grasscutter.setAuthenticationSystem(new DefaultAuthentication()); Grasscutter.getLogger().info("[GCAuth_OAuth] Disabled"); } public void loadTwitterLogin() { String folder_name = PLUGINS_FOLDER + "/GCAuth/OAuth/"; - String Login_Url = ("http" + (DISPATCH_ENCRYPTION.useEncryption ? "s" : "") + "://" - + lr(DISPATCH_INFO.accessAddress, DISPATCH_INFO.bindAddress) + ":" - + lr(DISPATCH_INFO.accessPort, DISPATCH_INFO.bindPort) + "/gcauth_oauth/login.html"); - Express app = Grasscutter.getDispatchServer().getServer(); + Grasscutter.setAuthenticationSystem(new GCAuthAuthenticationHandler()); - app.get("/Api/twitter_login", new JsonHandler()); + HttpServer app = Grasscutter.getHttpServer(); - app.get("/sdkTwitterLogin.html", new DispatchHttpJsonHandler( - String.format("", Login_Url) - )); + app.addRouter(JsonHandler.class); + app.addRouter(RequestHandler.class); + app.addRouter(sdkHandler.class); + app.addRouter(VerifyHandler.class); - app.post("/gcauth_oauth/login", new RequestHandler()); - - app.post("/hk4e_global/mdk/shield/api/loginByThirdparty", new VerifyHandler()); - - app.raw().config.addStaticFiles("/gcauth_oauth", folder_name, Location.EXTERNAL); + app.getHandle().config.addStaticFiles("/gcauth_oauth", folder_name, Location.EXTERNAL); } } diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthAuthenticationHandler.java b/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthAuthenticationHandler.java new file mode 100644 index 0000000..c4afa4f --- /dev/null +++ b/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthAuthenticationHandler.java @@ -0,0 +1,49 @@ +package com.xtaolabs.gcauth_oauth.handler; + +import emu.grasscutter.auth.*; +import emu.grasscutter.server.http.objects.ComboTokenResJson; +import emu.grasscutter.server.http.objects.LoginResultJson; + +import me.exzork.gcauth.handler.GCAuthExternalAuthenticator; + +public class GCAuthAuthenticationHandler implements AuthenticationSystem { + private final Authenticator gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator(); + private final Authenticator tokenAuthenticator = new DefaultAuthenticators.TokenAuthenticator(); + private final Authenticator sessionKeyAuthenticator = new DefaultAuthenticators.SessionKeyAuthenticator(); + private final GCAuthExternalAuthenticator externalAuthenticator = new GCAuthExternalAuthenticator(); + + @Override + public void createAccount(String username, String password) { + // Unhandled. + } + + @Override + public void resetPassword(String username) { + // Unhandled. + } + + @Override + public boolean verifyUser(String s) { + return false; + } + + @Override + public Authenticator getPasswordAuthenticator() { + return gcAuthAuthenticator; + } + + @Override + public Authenticator getTokenAuthenticator() { + return tokenAuthenticator; + } + + @Override + public Authenticator getSessionKeyAuthenticator() { + return sessionKeyAuthenticator; + } + + @Override + public ExternalAuthenticator getExternalAuthenticator() { + return externalAuthenticator; + } +} diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthenticators.java b/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthenticators.java new file mode 100644 index 0000000..7fe2454 --- /dev/null +++ b/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthenticators.java @@ -0,0 +1,41 @@ +package com.xtaolabs.gcauth_oauth.handler; + +import emu.grasscutter.Grasscutter; +import emu.grasscutter.auth.AuthenticationSystem; +import emu.grasscutter.auth.Authenticator; +import emu.grasscutter.game.Account; +import emu.grasscutter.server.http.objects.LoginResultJson; + +import me.exzork.gcauth.utils.Authentication; + +public class GCAuthenticators { + + public static class GCAuthAuthenticator implements Authenticator { + + @Override + public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest authenticationRequest) { + var response = new LoginResultJson(); + + var requestData = authenticationRequest.getPasswordRequest(); + assert requestData != null; // This should never be null. + + Account account = Authentication.getAccountByOneTimeToken(requestData.account); + if(account == null) { + Grasscutter.getLogger().info("[GCAuth] Client " + requestData.account + " tried to login with invalid one time token."); + response.retcode = -201; + response.message = "Token is invalid"; + return response; + } + + // Account was found, log the player in + response.message = "OK"; + response.data.account.uid = account.getId(); + response.data.account.token = account.generateSessionKey(); + response.data.account.email = account.getEmail(); + response.data.account.twitter_name = account.getUsername(); + + Grasscutter.getLogger().info("[GCAuth] Client " + requestData.account + " logged in"); + return response; + } + } +} diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/handler/JsonHandler.java b/src/main/java/com/xtaolabs/gcauth_oauth/handler/JsonHandler.java index f45c24b..d2ae085 100644 --- a/src/main/java/com/xtaolabs/gcauth_oauth/handler/JsonHandler.java +++ b/src/main/java/com/xtaolabs/gcauth_oauth/handler/JsonHandler.java @@ -1,21 +1,26 @@ package com.xtaolabs.gcauth_oauth.handler; -import java.io.IOException; - -import express.http.HttpContextHandler; +import emu.grasscutter.server.http.Router; +import express.Express; import express.http.Request; import express.http.Response; +import io.javalin.Javalin; + import static emu.grasscutter.Configuration.*; -import static emu.grasscutter.Configuration.DISPATCH_INFO; -public final class JsonHandler implements HttpContextHandler { +public final class JsonHandler implements Router { + @Override - public void handle(Request req, Response res) throws IOException { - String Login_Url = ("http" + (DISPATCH_ENCRYPTION.useEncryption ? "s" : "") + "://" - + lr(DISPATCH_INFO.accessAddress, DISPATCH_INFO.bindAddress) + ":" - + lr(DISPATCH_INFO.accessPort, DISPATCH_INFO.bindPort) + "/gcauth_oauth/login.html"); + public void applyRoutes(Express express, Javalin javalin) { + express.get("/Api/twitter_login", JsonHandler::handle); + } + + public static void handle(Request req, Response res) { + String Login_Url = ("http" + (HTTP_ENCRYPTION.useEncryption ? "s" : "") + "://" + + lr(HTTP_INFO.accessAddress, HTTP_INFO.bindAddress) + ":" + + lr(HTTP_INFO.accessPort, HTTP_INFO.bindPort) + "/gcauth_oauth/login.html"); res.set("server", "tsa_m"); res.set("Content-Type", "application/json; charset=utf-8"); res.set("access-control-allow-credentials", "true"); diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/handler/RequestHandler.java b/src/main/java/com/xtaolabs/gcauth_oauth/handler/RequestHandler.java index 626b45d..ca05274 100644 --- a/src/main/java/com/xtaolabs/gcauth_oauth/handler/RequestHandler.java +++ b/src/main/java/com/xtaolabs/gcauth_oauth/handler/RequestHandler.java @@ -1,18 +1,25 @@ package com.xtaolabs.gcauth_oauth.handler; -import java.io.IOException; - import emu.grasscutter.game.Account; -import express.http.HttpContextHandler; +import emu.grasscutter.server.http.Router; + +import express.Express; import express.http.Request; import express.http.Response; +import io.javalin.Javalin; + import me.exzork.gcauth.utils.Authentication; -public final class RequestHandler implements HttpContextHandler { +public final class RequestHandler implements Router { + @Override - public void handle(Request req, Response res) throws IOException { + public void applyRoutes(Express express, Javalin javalin) { + express.post("/gcauth_oauth/login", RequestHandler::handle); + } + + public static void handle(Request req, Response res) { String username = req.formData("username"); String password = req.formData("password"); diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/handler/VerifyHandler.java b/src/main/java/com/xtaolabs/gcauth_oauth/handler/VerifyHandler.java index 1494483..ecd9c30 100644 --- a/src/main/java/com/xtaolabs/gcauth_oauth/handler/VerifyHandler.java +++ b/src/main/java/com/xtaolabs/gcauth_oauth/handler/VerifyHandler.java @@ -1,22 +1,32 @@ package com.xtaolabs.gcauth_oauth.handler; -import java.io.IOException; - import com.auth0.jwt.interfaces.DecodedJWT; + import com.xtaolabs.gcauth_oauth.json.VerifyJson; import com.xtaolabs.gcauth_oauth.utils.parse; + +import emu.grasscutter.server.http.Router; +import emu.grasscutter.server.http.objects.LoginResultJson; import emu.grasscutter.Grasscutter; import emu.grasscutter.game.Account; -import emu.grasscutter.server.dispatch.json.LoginResultJson; -import express.http.HttpContextHandler; + +import express.Express; import express.http.Request; import express.http.Response; + +import io.javalin.Javalin; + import me.exzork.gcauth.utils.Authentication; -public final class VerifyHandler implements HttpContextHandler { +public final class VerifyHandler implements Router { + @Override - public void handle(Request req, Response res) throws IOException { + public void applyRoutes(Express express, Javalin javalin) { + express.post("/hk4e_global/mdk/shield/api/loginByThirdparty", VerifyHandler::handle); + } + + public static void handle(Request req, Response res) { VerifyJson request = req.body(VerifyJson.class); LoginResultJson responseData = new LoginResultJson(); DecodedJWT jwt = parse.deToken(request.access_token); diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/handler/sdkHandler.java b/src/main/java/com/xtaolabs/gcauth_oauth/handler/sdkHandler.java new file mode 100644 index 0000000..524d1e3 --- /dev/null +++ b/src/main/java/com/xtaolabs/gcauth_oauth/handler/sdkHandler.java @@ -0,0 +1,26 @@ +package com.xtaolabs.gcauth_oauth.handler; + +import emu.grasscutter.server.http.Router; +import static emu.grasscutter.Configuration.*; + +import express.Express; +import express.http.Request; +import express.http.Response; + +import io.javalin.Javalin; + + +public final class sdkHandler implements Router { + + @Override + public void applyRoutes(Express express, Javalin javalin) { + express.get("/sdkTwitterLogin.html", sdkHandler::handle); + } + + public static void handle(Request req, Response res) { + String Login_Url = ("http" + (HTTP_ENCRYPTION.useEncryption ? "s" : "") + "://" + + lr(HTTP_INFO.accessAddress, HTTP_INFO.bindAddress) + ":" + + lr(HTTP_INFO.accessPort, HTTP_INFO.bindPort) + "/gcauth_oauth/login.html"); + res.send(String.format("", Login_Url)); + } +} diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/utils/parse.java b/src/main/java/com/xtaolabs/gcauth_oauth/utils/parse.java index 158f3d0..2ce4d7b 100644 --- a/src/main/java/com/xtaolabs/gcauth_oauth/utils/parse.java +++ b/src/main/java/com/xtaolabs/gcauth_oauth/utils/parse.java @@ -20,4 +20,4 @@ public class parse { } return jwt; } -} \ No newline at end of file +} diff --git a/src/main/resources/plugin.json b/src/main/resources/plugin.json index b4e588e..79a54fd 100644 --- a/src/main/resources/plugin.json +++ b/src/main/resources/plugin.json @@ -1,7 +1,7 @@ { "name": "GCAuth_OAuth", "description": "The in-game login system for Grasscutter is based on oauth and GCAuth.", - "version": "1.0.0", + "version": "1.1.0", "author": ["omg-xtao"], "mainClass": "com.xtaolabs.gcauth_oauth.GCAuth_OAuth" }