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

Commit

Permalink
🐛 Fix android cannot get username
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed May 15, 2022
1 parent 7ad2538 commit a8dc9b9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sourceCompatibility = 17
targetCompatibility = 17

group 'com.xtaolabs.gcauth_oauth'
version '1.1.0'
version '1.1.1'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class GCAuthAuthenticationHandler implements AuthenticationSystem {
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 GCAuthExternalAuthenticator externalAuthenticator = new GCAuthExternalAuthenticator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
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<LoginResultJson> {

@Override
public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest authenticationRequest) {
var response = new LoginResultJson();
Expand All @@ -38,4 +39,49 @@ public LoginResultJson authenticate(AuthenticationSystem.AuthenticationRequest a
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;
}
}
}
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.1.0",
"version": "1.1.1",
"author": ["omg-xtao"],
"mainClass": "com.xtaolabs.gcauth_oauth.GCAuth_OAuth"
}

0 comments on commit a8dc9b9

Please sign in to comment.