Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
⬆️ Support gc-dev-1.1.2 gcauth-2.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed May 15, 2022
1 parent c0ede83 commit 7ad2538
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 47 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 12 additions & 22 deletions src/main/java/com/xtaolabs/gcauth_oauth/GCAuth_OAuth.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -31,28 +28,21 @@ public void onEnable() {

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

app.get("/Api/twitter_login", new JsonHandler());

app.get("/sdkTwitterLogin.html", new DispatchHttpJsonHandler(
String.format("<meta http-equiv=\"refresh\" content=\"0;url=%s\">", Login_Url)
));
Grasscutter.setAuthenticationSystem(new GCAuthAuthenticationHandler());

app.post("/gcauth_oauth/login", new RequestHandler());
HttpServer app = Grasscutter.getHttpServer();

app.post("/hk4e_global/mdk/shield/api/loginByThirdparty", new VerifyHandler());
app.addRouter(JsonHandler.class);
app.addRouter(RequestHandler.class);
app.addRouter(sdkHandler.class);
app.addRouter(VerifyHandler.class);

app.raw().config.addStaticFiles("/gcauth_oauth", folder_name, Location.EXTERNAL);
app.getHandle().config.addStaticFiles("/gcauth_oauth", folder_name, Location.EXTERNAL);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
23 changes: 14 additions & 9 deletions src/main/java/com/xtaolabs/gcauth_oauth/handler/JsonHandler.java
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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");

Expand Down
22 changes: 16 additions & 6 deletions src/main/java/com/xtaolabs/gcauth_oauth/handler/VerifyHandler.java
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/xtaolabs/gcauth_oauth/handler/sdkHandler.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/xtaolabs/gcauth_oauth/utils/parse.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ public static DecodedJWT deToken(final String token) {
}
return jwt;
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 7ad2538

Please sign in to comment.