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.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed Jun 4, 2022
1 parent 33e02e2 commit d94bbb2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 50 deletions.
4 changes: 2 additions & 2 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.1.3'
version '1.2.0'

repositories {
mavenCentral()
}

dependencies {
implementation files('lib/grasscutter-1.1.2-dev.jar')
implementation files('lib/gcauth-2.2.1.jar')
implementation files('lib/gcauth-2.4.0.jar')
}

test {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/xtaolabs/gcauth_oauth/GCAuth_OAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@


public class GCAuth_OAuth extends Plugin {
public static GCAuth_OAuth getInstance() {
return (GCAuth_OAuth) Grasscutter.getPluginManager().getPlugin("GCAuth_OAuth");
}

@Override
public void onEnable() {
String Login_Html_Path = PLUGIN("GCAuth/OAuth/login.html");
File Login_Html = new File(Login_Html_Path);
if(!Login_Html.exists()) {
Grasscutter.getLogger().warn(String.format("[GCAuth_OAuth] %s not found", Login_Html_Path));
getLogger().warn(String.format("%s not found", Login_Html_Path));
} else {
loadTwitterLogin();
Grasscutter.getLogger().info("[GCAuth_OAuth] Enabled");
getLogger().info("GCAuth_OAuth Enabled");
}
}

@Override
public void onDisable() {
Grasscutter.setAuthenticationSystem(new DefaultAuthentication());
Grasscutter.getLogger().info("[GCAuth_OAuth] Disabled");
getLogger().info("GCAuth_OAuth Disabled");
}

public void loadTwitterLogin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import static emu.grasscutter.Configuration.*;


public final class JsonHandler implements Router {
public final class DesktopRedirectHandler implements Router {

@Override
public void applyRoutes(Express express, Javalin javalin) {
express.get("/Api/twitter_login", JsonHandler::handle);
express.get("/Api/twitter_login", DesktopRedirectHandler::handle);
}

public static void handle(Request req, Response res) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.xtaolabs.gcauth_oauth.handler;

import emu.grasscutter.auth.*;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.server.http.objects.ComboTokenResJson;
import emu.grasscutter.server.http.objects.LoginResultJson;

import me.exzork.gcauth.handler.GCAuthExternalAuthenticator;
import me.exzork.gcauth.utils.Authentication;

public class GCAuthAuthenticationHandler implements AuthenticationSystem {
private final Authenticator<LoginResultJson> gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator();
Expand All @@ -16,7 +18,8 @@ public class GCAuthAuthenticationHandler implements AuthenticationSystem {

@Override
public void createAccount(String username, String password) {
// Unhandled.
password = Authentication.generateHash(password);
DatabaseHelper.createAccountWithPassword(username, password);
}

@Override
Expand All @@ -26,8 +29,11 @@ public void resetPassword(String username) {

@Override
public Account verifyUser(String s) {
// Unhandled.
return null;
String uid = Authentication.getUsernameFromJwt(s);
if (uid == null) {
return null;
}
return DatabaseHelper.getAccountById(uid);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.xtaolabs.gcauth_oauth.handler;

import com.xtaolabs.gcauth_oauth.GCAuth_OAuth;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.AuthenticationSystem;
import emu.grasscutter.auth.Authenticator;
import emu.grasscutter.auth.OAuthAuthenticator;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.server.http.objects.LoginResultJson;
import static emu.grasscutter.utils.Language.translate;

import me.exzork.gcauth.GCAuth;
import me.exzork.gcauth.utils.Authentication;

public class GCAuthenticators {
import static emu.grasscutter.Configuration.ACCOUNT;
import static emu.grasscutter.utils.Language.translate;

public final class GCAuthenticators {

public static class GCAuthAuthenticator implements Authenticator<LoginResultJson> {
@Override
Expand All @@ -21,11 +25,10 @@ public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest a
var requestData = authenticationRequest.getPasswordRequest();
assert requestData != null; // This should never be null.

Account account = Authentication.getAccountByOneTimeToken(requestData.account);
Account account = Authentication.getAccountByOTP(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";
response.message = "OTP invalid";
return response;
}

Expand All @@ -34,9 +37,8 @@ public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest a
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");
GCAuth.getInstance().getLogger().info("[GCAuth] Client " + requestData.account + " logged in");
return response;
}
}
Expand All @@ -54,34 +56,46 @@ public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest r

boolean successfulLogin;
String address = request.getRequest().ip();
String loggerMessage;
int playerCount = Grasscutter.getGameServer().getPlayers().size();

// Log the attempt.
Grasscutter.getLogger().info(translate("messages.dispatch.account.login_token_attempt", address));
GCAuth_OAuth.getInstance().getLogger().info(translate("messages.dispatch.account.login_token_attempt", address));

if (ACCOUNT.maxPlayer <= -1 || playerCount < ACCOUNT.maxPlayer) {

// Get account from database.
Account account = DatabaseHelper.getAccountById(requestData.uid);
// Get account from database.
Account account = DatabaseHelper.getAccountById(requestData.uid);

// Check if account exists/token is valid.
successfulLogin = account != null && account.getSessionKey().equals(requestData.token);
// Check if account exists/token is valid.
successfulLogin = account != null && account.getSessionKey().equals(requestData.token);

// Set response data.
if(successfulLogin) {
response.message = "OK";
response.data.account.uid = account.getId();
response.data.account.token = account.getSessionKey();
response.data.account.email = account.getEmail();
response.data.account.twitter_name = account.getUsername();
// Set response data.
if (successfulLogin) {
response.message = "OK";
response.data.account.uid = account.getId();
response.data.account.token = account.getSessionKey();
response.data.account.email = account.getEmail();
response.data.account.twitter_name = account.getUsername();

// Log the login.
loggerMessage = translate("messages.dispatch.account.login_token_success", address, requestData.uid);
} else {
response.retcode = -201;
response.message = translate("messages.dispatch.account.account_cache_error");

// Log the failure.
loggerMessage = translate("messages.dispatch.account.login_token_error", address);
}

// Log the login.
Grasscutter.getLogger().info(translate("messages.dispatch.account.login_token_success", address, requestData.uid));
} else {
response.retcode = -201;
response.message = translate("messages.dispatch.account.account_cache_error");
response.message = translate("messages.dispatch.account.server_max_player_limit");

// Log the failure.
Grasscutter.getLogger().info(translate("messages.dispatch.account.login_token_error", address));
loggerMessage = translate("messages.dispatch.account.login_max_player_limit", address);
}

GCAuth_OAuth.getInstance().getLogger().info(loggerMessage);
return response;
}
}
Expand All @@ -95,18 +109,20 @@ public void handleLogin(AuthenticationSystem.AuthenticationRequest request) {
assert request.getResponse() != null;
VerifyHandler.handle(request.getRequest(), request.getResponse());
}

@Override public void handleDesktopRedirection(AuthenticationSystem.AuthenticationRequest request) {
assert request.getResponse() != null;
JsonHandler.handle(request.getRequest(), request.getResponse());
}

@Override public void handleMobileRedirection(AuthenticationSystem.AuthenticationRequest request) {
/**
* The type of the client.
* Used for handling redirection.
*/
@Override
public void handleRedirection(AuthenticationSystem.AuthenticationRequest request, ClientType type) {
assert request.getResponse() != null;
sdkHandler.handle(request.getRequest(), request.getResponse());
switch (type) {
case DESKTOP -> DesktopRedirectHandler.handle(request.getRequest(), request.getResponse());
case MOBILE -> MobileRedirectHandler.handle(request.getRequest(), request.getResponse());
}
}

@Override public void handleTokenProcess(AuthenticationSystem.AuthenticationRequest request) {
@Override
public void handleTokenProcess(AuthenticationSystem.AuthenticationRequest request) {
assert request.getResponse() != null;
request.getResponse().send("Authentication is not available with the default authentication method.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import io.javalin.Javalin;


public final class sdkHandler implements Router {
public final class MobileRedirectHandler implements Router {

@Override
public void applyRoutes(Express express, Javalin javalin) {
express.get("/sdkTwitterLogin.html", sdkHandler::handle);
express.get("/sdkTwitterLogin.html", MobileRedirectHandler::handle);
}

public static void handle(Request req, Response res) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.auth0.jwt.interfaces.DecodedJWT;

import com.xtaolabs.gcauth_oauth.GCAuth_OAuth;
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 express.Express;
Expand All @@ -32,11 +32,11 @@ public static void handle(Request req, Response res) {
DecodedJWT jwt = parse.deToken(request.access_token);
Account account = null;
if (jwt != null) {
account = Authentication.getAccountByOneTimeToken(jwt.getClaim("token").asString());
account = Authentication.getAccountByOTP(jwt.getClaim("token").asString());
}
// Login
if(account == null) {
Grasscutter.getLogger().info("[GCAuth] Client " + req.ip() + " failed to log in");
GCAuth_OAuth.getInstance().getLogger().info("Client " + req.ip() + " failed to log in");
responseData.retcode = -201;
responseData.message = "Token is invalid";
res.send(responseData);
Expand All @@ -50,7 +50,7 @@ public static void handle(Request req, Response res) {
responseData.data.account.email = account.getEmail();
responseData.data.account.twitter_name = account.getUsername();

Grasscutter.getLogger().info(String.format("[GCAuth] Client %s logged in as %s", req.ip(), responseData.data.account.uid));
GCAuth_OAuth.getInstance().getLogger().info(String.format("Client %s logged in as %s", req.ip(), responseData.data.account.uid));

res.send(responseData);
}
Expand Down

0 comments on commit d94bbb2

Please sign in to comment.