From a8dc9b9234a15681552ba9751c2155aa9aceb329 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 15 May 2022 23:00:22 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20android=20cannot=20get=20u?= =?UTF-8?q?sername?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../handler/GCAuthAuthenticationHandler.java | 2 +- .../handler/GCAuthenticators.java | 48 ++++++++++++++++++- src/main/resources/plugin.json | 2 +- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index f86a40c..0c5d843 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ sourceCompatibility = 17 targetCompatibility = 17 group 'com.xtaolabs.gcauth_oauth' -version '1.1.0' +version '1.1.1' repositories { mavenCentral() diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthAuthenticationHandler.java b/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthAuthenticationHandler.java index c4afa4f..0b6d65e 100644 --- a/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthAuthenticationHandler.java +++ b/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthAuthenticationHandler.java @@ -8,7 +8,7 @@ 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 tokenAuthenticator = new GCAuthenticators.TokenAuthenticator(); private final Authenticator sessionKeyAuthenticator = new DefaultAuthenticators.SessionKeyAuthenticator(); private final GCAuthExternalAuthenticator externalAuthenticator = new GCAuthExternalAuthenticator(); diff --git a/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthenticators.java b/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthenticators.java index 7fe2454..7e7ed12 100644 --- a/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthenticators.java +++ b/src/main/java/com/xtaolabs/gcauth_oauth/handler/GCAuthenticators.java @@ -3,15 +3,16 @@ package com.xtaolabs.gcauth_oauth.handler; import emu.grasscutter.Grasscutter; import emu.grasscutter.auth.AuthenticationSystem; import emu.grasscutter.auth.Authenticator; +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.utils.Authentication; public class GCAuthenticators { public static class GCAuthAuthenticator implements Authenticator { - @Override public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest authenticationRequest) { var response = new LoginResultJson(); @@ -38,4 +39,49 @@ public class GCAuthenticators { return response; } } + + /** + * Handles the authentication request from the game when using a registry token. + */ + public static class TokenAuthenticator implements Authenticator { + @Override + public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest request) { + var response = new LoginResultJson(); + + var requestData = request.getTokenRequest(); + assert requestData != null; + + boolean successfulLogin; + String address = request.getRequest().ip(); + + // Log the attempt. + Grasscutter.getLogger().info(translate("messages.dispatch.account.login_token_attempt", address)); + + // 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); + + // 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. + 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"); + + // Log the failure. + Grasscutter.getLogger().info(translate("messages.dispatch.account.login_token_error", address)); + } + + return response; + } + } } diff --git a/src/main/resources/plugin.json b/src/main/resources/plugin.json index 79a54fd..44424e5 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.1.0", + "version": "1.1.1", "author": ["omg-xtao"], "mainClass": "com.xtaolabs.gcauth_oauth.GCAuth_OAuth" }