⬆️ Support gc-dev-1.1.2 gcauth-2.2.1

This commit is contained in:
xtaodada 2022-05-15 11:35:15 +08:00
parent c0ede83066
commit 7ad25381be
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
10 changed files with 175 additions and 47 deletions

View File

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

View File

@ -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("<meta http-equiv=\"refresh\" content=\"0;url=%s\">", 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);
}
}

View File

@ -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<LoginResultJson> gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator();
private final Authenticator<LoginResultJson> tokenAuthenticator = new DefaultAuthenticators.TokenAuthenticator();
private final Authenticator<ComboTokenResJson> 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<LoginResultJson> getPasswordAuthenticator() {
return gcAuthAuthenticator;
}
@Override
public Authenticator<LoginResultJson> getTokenAuthenticator() {
return tokenAuthenticator;
}
@Override
public Authenticator<ComboTokenResJson> getSessionKeyAuthenticator() {
return sessionKeyAuthenticator;
}
@Override
public ExternalAuthenticator getExternalAuthenticator() {
return externalAuthenticator;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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("<meta http-equiv=\"refresh\" content=\"0;url=%s\">", Login_Url));
}
}

View File

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