🐛 Fix android cannot get username

This commit is contained in:
xtaodada 2022-05-15 23:00:22 +08:00
parent 7ad25381be
commit a8dc9b9234
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 50 additions and 4 deletions

View File

@ -10,7 +10,7 @@ sourceCompatibility = 17
targetCompatibility = 17 targetCompatibility = 17
group 'com.xtaolabs.gcauth_oauth' group 'com.xtaolabs.gcauth_oauth'
version '1.1.0' version '1.1.1'
repositories { repositories {
mavenCentral() mavenCentral()

View File

@ -8,7 +8,7 @@ import me.exzork.gcauth.handler.GCAuthExternalAuthenticator;
public class GCAuthAuthenticationHandler implements AuthenticationSystem { public class GCAuthAuthenticationHandler implements AuthenticationSystem {
private final Authenticator<LoginResultJson> gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator(); private final Authenticator<LoginResultJson> gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator();
private final Authenticator<LoginResultJson> tokenAuthenticator = new DefaultAuthenticators.TokenAuthenticator(); private final Authenticator<LoginResultJson> tokenAuthenticator = new GCAuthenticators.TokenAuthenticator();
private final Authenticator<ComboTokenResJson> sessionKeyAuthenticator = new DefaultAuthenticators.SessionKeyAuthenticator(); private final Authenticator<ComboTokenResJson> sessionKeyAuthenticator = new DefaultAuthenticators.SessionKeyAuthenticator();
private final GCAuthExternalAuthenticator externalAuthenticator = new GCAuthExternalAuthenticator(); private final GCAuthExternalAuthenticator externalAuthenticator = new GCAuthExternalAuthenticator();

View File

@ -3,15 +3,16 @@ package com.xtaolabs.gcauth_oauth.handler;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.AuthenticationSystem; import emu.grasscutter.auth.AuthenticationSystem;
import emu.grasscutter.auth.Authenticator; import emu.grasscutter.auth.Authenticator;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account; import emu.grasscutter.game.Account;
import emu.grasscutter.server.http.objects.LoginResultJson; import emu.grasscutter.server.http.objects.LoginResultJson;
import static emu.grasscutter.utils.Language.translate;
import me.exzork.gcauth.utils.Authentication; import me.exzork.gcauth.utils.Authentication;
public class GCAuthenticators { public class GCAuthenticators {
public static class GCAuthAuthenticator implements Authenticator<LoginResultJson> { public static class GCAuthAuthenticator implements Authenticator<LoginResultJson> {
@Override @Override
public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest authenticationRequest) { public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest authenticationRequest) {
var response = new LoginResultJson(); var response = new LoginResultJson();
@ -38,4 +39,49 @@ public class GCAuthenticators {
return response; return response;
} }
} }
/**
* Handles the authentication request from the game when using a registry token.
*/
public static class TokenAuthenticator implements Authenticator<LoginResultJson> {
@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;
}
}
} }

View File

@ -1,7 +1,7 @@
{ {
"name": "GCAuth_OAuth", "name": "GCAuth_OAuth",
"description": "The in-game login system for Grasscutter is based on oauth and GCAuth.", "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"], "author": ["omg-xtao"],
"mainClass": "com.xtaolabs.gcauth_oauth.GCAuth_OAuth" "mainClass": "com.xtaolabs.gcauth_oauth.GCAuth_OAuth"
} }