mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-17 09:25:06 +01:00
Format code [skip actions]
This commit is contained in:
@@ -1,133 +1,133 @@
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import emu.grasscutter.game.Account;
|
||||
import emu.grasscutter.server.http.objects.*;
|
||||
import io.javalin.http.Context;
|
||||
import javax.annotation.Nullable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
/** Defines an authenticator for the server. Can be changed by plugins. */
|
||||
public interface AuthenticationSystem {
|
||||
|
||||
/**
|
||||
* Generates an authentication request from a {@link LoginAccountRequestJson} object.
|
||||
*
|
||||
* @param ctx The Javalin context.
|
||||
* @param jsonData The JSON data.
|
||||
* @return An authentication request.
|
||||
*/
|
||||
static AuthenticationRequest fromPasswordRequest(Context ctx, LoginAccountRequestJson jsonData) {
|
||||
return AuthenticationRequest.builder().context(ctx).passwordRequest(jsonData).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an authentication request from a {@link LoginTokenRequestJson} object.
|
||||
*
|
||||
* @param ctx The Javalin context.
|
||||
* @param jsonData The JSON data.
|
||||
* @return An authentication request.
|
||||
*/
|
||||
static AuthenticationRequest fromTokenRequest(Context ctx, LoginTokenRequestJson jsonData) {
|
||||
return AuthenticationRequest.builder().context(ctx).tokenRequest(jsonData).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an authentication request from a {@link ComboTokenReqJson} object.
|
||||
*
|
||||
* @param ctx The Javalin context.
|
||||
* @param jsonData The JSON data.
|
||||
* @return An authentication request.
|
||||
*/
|
||||
static AuthenticationRequest fromComboTokenRequest(
|
||||
Context ctx, ComboTokenReqJson jsonData, ComboTokenReqJson.LoginTokenData tokenData) {
|
||||
return AuthenticationRequest.builder()
|
||||
.context(ctx)
|
||||
.sessionKeyRequest(jsonData)
|
||||
.sessionKeyData(tokenData)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an authentication request from a {@link Context} object.
|
||||
*
|
||||
* @param ctx The Javalin context.
|
||||
* @return An authentication request.
|
||||
*/
|
||||
static AuthenticationRequest fromExternalRequest(Context ctx) {
|
||||
return AuthenticationRequest.builder().context(ctx).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a user requests to make an account.
|
||||
*
|
||||
* @param username The provided username.
|
||||
* @param password The provided password. (SHA-256'ed)
|
||||
*/
|
||||
void createAccount(String username, String password);
|
||||
|
||||
/**
|
||||
* Called when a user requests to reset their password.
|
||||
*
|
||||
* @param username The username of the account to reset.
|
||||
*/
|
||||
void resetPassword(String username);
|
||||
|
||||
/**
|
||||
* Called by plugins to internally verify a user's identity.
|
||||
*
|
||||
* @param details A unique identifier to identify the user. (For example: a JWT token)
|
||||
* @return The user's account if the verification was successful, null if the user was unable to
|
||||
* be verified.
|
||||
*/
|
||||
Account verifyUser(String details);
|
||||
|
||||
/**
|
||||
* This is the authenticator used for password authentication.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
Authenticator<LoginResultJson> getPasswordAuthenticator();
|
||||
|
||||
/**
|
||||
* This is the authenticator used for token authentication.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
Authenticator<LoginResultJson> getTokenAuthenticator();
|
||||
|
||||
/**
|
||||
* This is the authenticator used for session authentication.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
Authenticator<ComboTokenResJson> getSessionKeyAuthenticator();
|
||||
|
||||
/**
|
||||
* This is the authenticator used for handling external authentication requests.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
ExternalAuthenticator getExternalAuthenticator();
|
||||
|
||||
/**
|
||||
* This is the authenticator used for handling OAuth authentication requests.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
OAuthAuthenticator getOAuthAuthenticator();
|
||||
|
||||
/** A data container that holds relevant data for authenticating a client. */
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
class AuthenticationRequest {
|
||||
private final Context context;
|
||||
|
||||
@Nullable private final LoginAccountRequestJson passwordRequest;
|
||||
@Nullable private final LoginTokenRequestJson tokenRequest;
|
||||
@Nullable private final ComboTokenReqJson sessionKeyRequest;
|
||||
@Nullable private final ComboTokenReqJson.LoginTokenData sessionKeyData;
|
||||
}
|
||||
}
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import emu.grasscutter.game.Account;
|
||||
import emu.grasscutter.server.http.objects.*;
|
||||
import io.javalin.http.Context;
|
||||
import javax.annotation.Nullable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
/** Defines an authenticator for the server. Can be changed by plugins. */
|
||||
public interface AuthenticationSystem {
|
||||
|
||||
/**
|
||||
* Generates an authentication request from a {@link LoginAccountRequestJson} object.
|
||||
*
|
||||
* @param ctx The Javalin context.
|
||||
* @param jsonData The JSON data.
|
||||
* @return An authentication request.
|
||||
*/
|
||||
static AuthenticationRequest fromPasswordRequest(Context ctx, LoginAccountRequestJson jsonData) {
|
||||
return AuthenticationRequest.builder().context(ctx).passwordRequest(jsonData).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an authentication request from a {@link LoginTokenRequestJson} object.
|
||||
*
|
||||
* @param ctx The Javalin context.
|
||||
* @param jsonData The JSON data.
|
||||
* @return An authentication request.
|
||||
*/
|
||||
static AuthenticationRequest fromTokenRequest(Context ctx, LoginTokenRequestJson jsonData) {
|
||||
return AuthenticationRequest.builder().context(ctx).tokenRequest(jsonData).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an authentication request from a {@link ComboTokenReqJson} object.
|
||||
*
|
||||
* @param ctx The Javalin context.
|
||||
* @param jsonData The JSON data.
|
||||
* @return An authentication request.
|
||||
*/
|
||||
static AuthenticationRequest fromComboTokenRequest(
|
||||
Context ctx, ComboTokenReqJson jsonData, ComboTokenReqJson.LoginTokenData tokenData) {
|
||||
return AuthenticationRequest.builder()
|
||||
.context(ctx)
|
||||
.sessionKeyRequest(jsonData)
|
||||
.sessionKeyData(tokenData)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an authentication request from a {@link Context} object.
|
||||
*
|
||||
* @param ctx The Javalin context.
|
||||
* @return An authentication request.
|
||||
*/
|
||||
static AuthenticationRequest fromExternalRequest(Context ctx) {
|
||||
return AuthenticationRequest.builder().context(ctx).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a user requests to make an account.
|
||||
*
|
||||
* @param username The provided username.
|
||||
* @param password The provided password. (SHA-256'ed)
|
||||
*/
|
||||
void createAccount(String username, String password);
|
||||
|
||||
/**
|
||||
* Called when a user requests to reset their password.
|
||||
*
|
||||
* @param username The username of the account to reset.
|
||||
*/
|
||||
void resetPassword(String username);
|
||||
|
||||
/**
|
||||
* Called by plugins to internally verify a user's identity.
|
||||
*
|
||||
* @param details A unique identifier to identify the user. (For example: a JWT token)
|
||||
* @return The user's account if the verification was successful, null if the user was unable to
|
||||
* be verified.
|
||||
*/
|
||||
Account verifyUser(String details);
|
||||
|
||||
/**
|
||||
* This is the authenticator used for password authentication.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
Authenticator<LoginResultJson> getPasswordAuthenticator();
|
||||
|
||||
/**
|
||||
* This is the authenticator used for token authentication.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
Authenticator<LoginResultJson> getTokenAuthenticator();
|
||||
|
||||
/**
|
||||
* This is the authenticator used for session authentication.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
Authenticator<ComboTokenResJson> getSessionKeyAuthenticator();
|
||||
|
||||
/**
|
||||
* This is the authenticator used for handling external authentication requests.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
ExternalAuthenticator getExternalAuthenticator();
|
||||
|
||||
/**
|
||||
* This is the authenticator used for handling OAuth authentication requests.
|
||||
*
|
||||
* @return An authenticator.
|
||||
*/
|
||||
OAuthAuthenticator getOAuthAuthenticator();
|
||||
|
||||
/** A data container that holds relevant data for authenticating a client. */
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
class AuthenticationRequest {
|
||||
private final Context context;
|
||||
|
||||
@Nullable private final LoginAccountRequestJson passwordRequest;
|
||||
@Nullable private final LoginTokenRequestJson tokenRequest;
|
||||
@Nullable private final ComboTokenReqJson sessionKeyRequest;
|
||||
@Nullable private final ComboTokenReqJson.LoginTokenData sessionKeyData;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import emu.grasscutter.server.http.objects.ComboTokenResJson;
|
||||
import emu.grasscutter.server.http.objects.LoginResultJson;
|
||||
|
||||
/**
|
||||
* Handles username/password authentication from the client.
|
||||
*
|
||||
* @param <T> The response object type. Should be {@link LoginResultJson} or {@link
|
||||
* ComboTokenResJson}
|
||||
*/
|
||||
public interface Authenticator<T> {
|
||||
|
||||
/**
|
||||
* Attempt to authenticate the client with the provided credentials.
|
||||
*
|
||||
* @param request The authentication request wrapped in a {@link
|
||||
* AuthenticationSystem.AuthenticationRequest} object.
|
||||
* @return The result of the login in an object.
|
||||
*/
|
||||
T authenticate(AuthenticationSystem.AuthenticationRequest request);
|
||||
}
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import emu.grasscutter.server.http.objects.ComboTokenResJson;
|
||||
import emu.grasscutter.server.http.objects.LoginResultJson;
|
||||
|
||||
/**
|
||||
* Handles username/password authentication from the client.
|
||||
*
|
||||
* @param <T> The response object type. Should be {@link LoginResultJson} or {@link
|
||||
* ComboTokenResJson}
|
||||
*/
|
||||
public interface Authenticator<T> {
|
||||
|
||||
/**
|
||||
* Attempt to authenticate the client with the provided credentials.
|
||||
*
|
||||
* @param request The authentication request wrapped in a {@link
|
||||
* AuthenticationSystem.AuthenticationRequest} object.
|
||||
* @return The result of the login in an object.
|
||||
*/
|
||||
T authenticate(AuthenticationSystem.AuthenticationRequest request);
|
||||
}
|
||||
|
||||
@@ -1,363 +1,363 @@
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.ACCOUNT;
|
||||
import static emu.grasscutter.utils.Language.translate;
|
||||
|
||||
import at.favre.lib.crypto.bcrypt.BCrypt;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
|
||||
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 emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import javax.crypto.Cipher;
|
||||
|
||||
/** A class containing default authenticators. */
|
||||
public final class DefaultAuthenticators {
|
||||
|
||||
/** Handles the authentication request from the username and password form. */
|
||||
public static class PasswordAuthenticator implements Authenticator<LoginResultJson> {
|
||||
@Override
|
||||
public LoginResultJson authenticate(AuthenticationRequest request) {
|
||||
var response = new LoginResultJson();
|
||||
|
||||
var requestData = request.getPasswordRequest();
|
||||
assert requestData != null; // This should never be null.
|
||||
int playerCount = Grasscutter.getGameServer().getPlayers().size();
|
||||
|
||||
boolean successfulLogin = false;
|
||||
String address = request.getContext().ip();
|
||||
String responseMessage = translate("messages.dispatch.account.username_error");
|
||||
String loggerMessage = "";
|
||||
|
||||
// Get account from database.
|
||||
Account account = DatabaseHelper.getAccountByName(requestData.account);
|
||||
if (ACCOUNT.maxPlayer <= -1 || playerCount < ACCOUNT.maxPlayer) {
|
||||
// Check if account exists.
|
||||
if (account == null && ACCOUNT.autoCreate) {
|
||||
// This account has been created AUTOMATICALLY. There will be no permissions added.
|
||||
account = DatabaseHelper.createAccountWithUid(requestData.account, 0);
|
||||
|
||||
// Check if the account was created successfully.
|
||||
if (account == null) {
|
||||
responseMessage = translate("messages.dispatch.account.username_create_error");
|
||||
Grasscutter.getLogger()
|
||||
.info(translate("messages.dispatch.account.account_login_create_error", address));
|
||||
} else {
|
||||
// Continue with login.
|
||||
successfulLogin = true;
|
||||
|
||||
// Log the creation.
|
||||
Grasscutter.getLogger()
|
||||
.info(
|
||||
translate(
|
||||
"messages.dispatch.account.account_login_create_success",
|
||||
address,
|
||||
response.data.account.uid));
|
||||
}
|
||||
} else if (account != null) successfulLogin = true;
|
||||
else
|
||||
loggerMessage = translate("messages.dispatch.account.account_login_exist_error", address);
|
||||
|
||||
} else {
|
||||
responseMessage = translate("messages.dispatch.account.server_max_player_limit");
|
||||
loggerMessage = translate("messages.dispatch.account.login_max_player_limit", address);
|
||||
}
|
||||
|
||||
// Set response data.
|
||||
if (successfulLogin) {
|
||||
response.message = "OK";
|
||||
response.data.account.uid = account.getId();
|
||||
response.data.account.token = account.generateSessionKey();
|
||||
response.data.account.email = account.getEmail();
|
||||
|
||||
loggerMessage =
|
||||
translate("messages.dispatch.account.login_success", address, account.getId());
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = responseMessage;
|
||||
}
|
||||
Grasscutter.getLogger().info(loggerMessage);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExperimentalPasswordAuthenticator implements Authenticator<LoginResultJson> {
|
||||
@Override
|
||||
public LoginResultJson authenticate(AuthenticationRequest request) {
|
||||
var response = new LoginResultJson();
|
||||
|
||||
var requestData = request.getPasswordRequest();
|
||||
assert requestData != null; // This should never be null.
|
||||
int playerCount = Grasscutter.getGameServer().getPlayers().size();
|
||||
|
||||
boolean successfulLogin = false;
|
||||
String address = request.getContext().ip();
|
||||
String responseMessage = translate("messages.dispatch.account.username_error");
|
||||
String loggerMessage = "";
|
||||
String decryptedPassword = "";
|
||||
try {
|
||||
byte[] key = FileUtils.readResource("/keys/auth_private-key.der");
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(key);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
RSAPrivateKey private_key = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||
|
||||
cipher.init(Cipher.DECRYPT_MODE, private_key);
|
||||
|
||||
decryptedPassword =
|
||||
new String(
|
||||
cipher.doFinal(Utils.base64Decode(request.getPasswordRequest().password)),
|
||||
StandardCharsets.UTF_8);
|
||||
} catch (Exception ignored) {
|
||||
decryptedPassword = request.getPasswordRequest().password;
|
||||
}
|
||||
|
||||
if (decryptedPassword == null) {
|
||||
successfulLogin = false;
|
||||
loggerMessage = translate("messages.dispatch.account.login_password_error", address);
|
||||
responseMessage = translate("messages.dispatch.account.password_error");
|
||||
}
|
||||
|
||||
// Get account from database.
|
||||
Account account = DatabaseHelper.getAccountByName(requestData.account);
|
||||
if (ACCOUNT.maxPlayer <= -1 || playerCount < ACCOUNT.maxPlayer) {
|
||||
// Check if account exists.
|
||||
if (account == null && ACCOUNT.autoCreate) {
|
||||
// This account has been created AUTOMATICALLY. There will be no permissions added.
|
||||
if (decryptedPassword.length() >= 8) {
|
||||
account = DatabaseHelper.createAccountWithUid(requestData.account, 0);
|
||||
account.setPassword(
|
||||
BCrypt.withDefaults().hashToString(12, decryptedPassword.toCharArray()));
|
||||
account.save();
|
||||
|
||||
// Check if the account was created successfully.
|
||||
if (account == null) {
|
||||
responseMessage = translate("messages.dispatch.account.username_create_error");
|
||||
loggerMessage =
|
||||
translate("messages.dispatch.account.account_login_create_error", address);
|
||||
} else {
|
||||
// Continue with login.
|
||||
successfulLogin = true;
|
||||
|
||||
// Log the creation.
|
||||
Grasscutter.getLogger()
|
||||
.info(
|
||||
translate(
|
||||
"messages.dispatch.account.account_login_create_success",
|
||||
address,
|
||||
response.data.account.uid));
|
||||
}
|
||||
} else {
|
||||
successfulLogin = false;
|
||||
loggerMessage = translate("messages.dispatch.account.login_password_error", address);
|
||||
responseMessage = translate("messages.dispatch.account.password_length_error");
|
||||
}
|
||||
} else if (account != null) {
|
||||
if (account.getPassword() != null && !account.getPassword().isEmpty()) {
|
||||
if (BCrypt.verifyer()
|
||||
.verify(decryptedPassword.toCharArray(), account.getPassword())
|
||||
.verified) {
|
||||
successfulLogin = true;
|
||||
} else {
|
||||
successfulLogin = false;
|
||||
loggerMessage = translate("messages.dispatch.account.login_password_error", address);
|
||||
responseMessage = translate("messages.dispatch.account.password_error");
|
||||
}
|
||||
} else {
|
||||
successfulLogin = false;
|
||||
loggerMessage =
|
||||
translate("messages.dispatch.account.login_password_storage_error", address);
|
||||
responseMessage = translate("messages.dispatch.account.password_storage_error");
|
||||
}
|
||||
} else {
|
||||
loggerMessage = translate("messages.dispatch.account.account_login_exist_error", address);
|
||||
}
|
||||
} else {
|
||||
responseMessage = translate("messages.dispatch.account.server_max_player_limit");
|
||||
loggerMessage = translate("messages.dispatch.account.login_max_player_limit", address);
|
||||
}
|
||||
|
||||
// Set response data.
|
||||
if (successfulLogin) {
|
||||
response.message = "OK";
|
||||
response.data.account.uid = account.getId();
|
||||
response.data.account.token = account.generateSessionKey();
|
||||
response.data.account.email = account.getEmail();
|
||||
|
||||
loggerMessage =
|
||||
translate("messages.dispatch.account.login_success", address, account.getId());
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = responseMessage;
|
||||
}
|
||||
Grasscutter.getLogger().info(loggerMessage);
|
||||
|
||||
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(AuthenticationRequest request) {
|
||||
var response = new LoginResultJson();
|
||||
|
||||
var requestData = request.getTokenRequest();
|
||||
assert requestData != null;
|
||||
|
||||
boolean successfulLogin;
|
||||
String address = request.getContext().ip();
|
||||
String loggerMessage;
|
||||
int playerCount = Grasscutter.getGameServer().getPlayers().size();
|
||||
|
||||
// Log the attempt.
|
||||
Grasscutter.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);
|
||||
|
||||
// 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();
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = translate("messages.dispatch.account.server_max_player_limit");
|
||||
|
||||
loggerMessage = translate("messages.dispatch.account.login_max_player_limit", address);
|
||||
}
|
||||
|
||||
Grasscutter.getLogger().info(loggerMessage);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
/** Handles the authentication request from the game when using a combo token/session key. */
|
||||
public static class SessionKeyAuthenticator implements Authenticator<ComboTokenResJson> {
|
||||
@Override
|
||||
public ComboTokenResJson authenticate(AuthenticationRequest request) {
|
||||
var response = new ComboTokenResJson();
|
||||
|
||||
var requestData = request.getSessionKeyRequest();
|
||||
var loginData = request.getSessionKeyData();
|
||||
assert requestData != null;
|
||||
assert loginData != null;
|
||||
|
||||
boolean successfulLogin;
|
||||
String address = request.getContext().ip();
|
||||
String loggerMessage;
|
||||
int playerCount = Grasscutter.getGameServer().getPlayers().size();
|
||||
|
||||
if (ACCOUNT.maxPlayer <= -1 || playerCount < ACCOUNT.maxPlayer) {
|
||||
// Get account from database.
|
||||
Account account = DatabaseHelper.getAccountById(loginData.uid);
|
||||
|
||||
// Check if account exists/token is valid.
|
||||
successfulLogin = account != null && account.getSessionKey().equals(loginData.token);
|
||||
|
||||
// Set response data.
|
||||
if (successfulLogin) {
|
||||
response.message = "OK";
|
||||
response.data.open_id = account.getId();
|
||||
response.data.combo_id = "157795300";
|
||||
response.data.combo_token = account.generateLoginToken();
|
||||
|
||||
// Log the login.
|
||||
loggerMessage = translate("messages.dispatch.account.combo_token_success", address);
|
||||
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = translate("messages.dispatch.account.session_key_error");
|
||||
|
||||
// Log the failure.
|
||||
loggerMessage = translate("messages.dispatch.account.combo_token_error", address);
|
||||
}
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = translate("messages.dispatch.account.server_max_player_limit");
|
||||
|
||||
loggerMessage = translate("messages.dispatch.account.login_max_player_limit", address);
|
||||
}
|
||||
|
||||
Grasscutter.getLogger().info(loggerMessage);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
/** Handles authentication requests from external sources. */
|
||||
public static class ExternalAuthentication implements ExternalAuthenticator {
|
||||
@Override
|
||||
public void handleLogin(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleAccountCreation(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePasswordReset(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
}
|
||||
|
||||
/** Handles authentication requests from OAuth sources.Zenlith */
|
||||
public static class OAuthAuthentication implements OAuthAuthenticator {
|
||||
@Override
|
||||
public void handleLogin(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRedirection(AuthenticationRequest request, ClientType type) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleTokenProcess(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
}
|
||||
}
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.ACCOUNT;
|
||||
import static emu.grasscutter.utils.Language.translate;
|
||||
|
||||
import at.favre.lib.crypto.bcrypt.BCrypt;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
|
||||
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 emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import javax.crypto.Cipher;
|
||||
|
||||
/** A class containing default authenticators. */
|
||||
public final class DefaultAuthenticators {
|
||||
|
||||
/** Handles the authentication request from the username and password form. */
|
||||
public static class PasswordAuthenticator implements Authenticator<LoginResultJson> {
|
||||
@Override
|
||||
public LoginResultJson authenticate(AuthenticationRequest request) {
|
||||
var response = new LoginResultJson();
|
||||
|
||||
var requestData = request.getPasswordRequest();
|
||||
assert requestData != null; // This should never be null.
|
||||
int playerCount = Grasscutter.getGameServer().getPlayers().size();
|
||||
|
||||
boolean successfulLogin = false;
|
||||
String address = request.getContext().ip();
|
||||
String responseMessage = translate("messages.dispatch.account.username_error");
|
||||
String loggerMessage = "";
|
||||
|
||||
// Get account from database.
|
||||
Account account = DatabaseHelper.getAccountByName(requestData.account);
|
||||
if (ACCOUNT.maxPlayer <= -1 || playerCount < ACCOUNT.maxPlayer) {
|
||||
// Check if account exists.
|
||||
if (account == null && ACCOUNT.autoCreate) {
|
||||
// This account has been created AUTOMATICALLY. There will be no permissions added.
|
||||
account = DatabaseHelper.createAccountWithUid(requestData.account, 0);
|
||||
|
||||
// Check if the account was created successfully.
|
||||
if (account == null) {
|
||||
responseMessage = translate("messages.dispatch.account.username_create_error");
|
||||
Grasscutter.getLogger()
|
||||
.info(translate("messages.dispatch.account.account_login_create_error", address));
|
||||
} else {
|
||||
// Continue with login.
|
||||
successfulLogin = true;
|
||||
|
||||
// Log the creation.
|
||||
Grasscutter.getLogger()
|
||||
.info(
|
||||
translate(
|
||||
"messages.dispatch.account.account_login_create_success",
|
||||
address,
|
||||
response.data.account.uid));
|
||||
}
|
||||
} else if (account != null) successfulLogin = true;
|
||||
else
|
||||
loggerMessage = translate("messages.dispatch.account.account_login_exist_error", address);
|
||||
|
||||
} else {
|
||||
responseMessage = translate("messages.dispatch.account.server_max_player_limit");
|
||||
loggerMessage = translate("messages.dispatch.account.login_max_player_limit", address);
|
||||
}
|
||||
|
||||
// Set response data.
|
||||
if (successfulLogin) {
|
||||
response.message = "OK";
|
||||
response.data.account.uid = account.getId();
|
||||
response.data.account.token = account.generateSessionKey();
|
||||
response.data.account.email = account.getEmail();
|
||||
|
||||
loggerMessage =
|
||||
translate("messages.dispatch.account.login_success", address, account.getId());
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = responseMessage;
|
||||
}
|
||||
Grasscutter.getLogger().info(loggerMessage);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExperimentalPasswordAuthenticator implements Authenticator<LoginResultJson> {
|
||||
@Override
|
||||
public LoginResultJson authenticate(AuthenticationRequest request) {
|
||||
var response = new LoginResultJson();
|
||||
|
||||
var requestData = request.getPasswordRequest();
|
||||
assert requestData != null; // This should never be null.
|
||||
int playerCount = Grasscutter.getGameServer().getPlayers().size();
|
||||
|
||||
boolean successfulLogin = false;
|
||||
String address = request.getContext().ip();
|
||||
String responseMessage = translate("messages.dispatch.account.username_error");
|
||||
String loggerMessage = "";
|
||||
String decryptedPassword = "";
|
||||
try {
|
||||
byte[] key = FileUtils.readResource("/keys/auth_private-key.der");
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(key);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
RSAPrivateKey private_key = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||
|
||||
cipher.init(Cipher.DECRYPT_MODE, private_key);
|
||||
|
||||
decryptedPassword =
|
||||
new String(
|
||||
cipher.doFinal(Utils.base64Decode(request.getPasswordRequest().password)),
|
||||
StandardCharsets.UTF_8);
|
||||
} catch (Exception ignored) {
|
||||
decryptedPassword = request.getPasswordRequest().password;
|
||||
}
|
||||
|
||||
if (decryptedPassword == null) {
|
||||
successfulLogin = false;
|
||||
loggerMessage = translate("messages.dispatch.account.login_password_error", address);
|
||||
responseMessage = translate("messages.dispatch.account.password_error");
|
||||
}
|
||||
|
||||
// Get account from database.
|
||||
Account account = DatabaseHelper.getAccountByName(requestData.account);
|
||||
if (ACCOUNT.maxPlayer <= -1 || playerCount < ACCOUNT.maxPlayer) {
|
||||
// Check if account exists.
|
||||
if (account == null && ACCOUNT.autoCreate) {
|
||||
// This account has been created AUTOMATICALLY. There will be no permissions added.
|
||||
if (decryptedPassword.length() >= 8) {
|
||||
account = DatabaseHelper.createAccountWithUid(requestData.account, 0);
|
||||
account.setPassword(
|
||||
BCrypt.withDefaults().hashToString(12, decryptedPassword.toCharArray()));
|
||||
account.save();
|
||||
|
||||
// Check if the account was created successfully.
|
||||
if (account == null) {
|
||||
responseMessage = translate("messages.dispatch.account.username_create_error");
|
||||
loggerMessage =
|
||||
translate("messages.dispatch.account.account_login_create_error", address);
|
||||
} else {
|
||||
// Continue with login.
|
||||
successfulLogin = true;
|
||||
|
||||
// Log the creation.
|
||||
Grasscutter.getLogger()
|
||||
.info(
|
||||
translate(
|
||||
"messages.dispatch.account.account_login_create_success",
|
||||
address,
|
||||
response.data.account.uid));
|
||||
}
|
||||
} else {
|
||||
successfulLogin = false;
|
||||
loggerMessage = translate("messages.dispatch.account.login_password_error", address);
|
||||
responseMessage = translate("messages.dispatch.account.password_length_error");
|
||||
}
|
||||
} else if (account != null) {
|
||||
if (account.getPassword() != null && !account.getPassword().isEmpty()) {
|
||||
if (BCrypt.verifyer()
|
||||
.verify(decryptedPassword.toCharArray(), account.getPassword())
|
||||
.verified) {
|
||||
successfulLogin = true;
|
||||
} else {
|
||||
successfulLogin = false;
|
||||
loggerMessage = translate("messages.dispatch.account.login_password_error", address);
|
||||
responseMessage = translate("messages.dispatch.account.password_error");
|
||||
}
|
||||
} else {
|
||||
successfulLogin = false;
|
||||
loggerMessage =
|
||||
translate("messages.dispatch.account.login_password_storage_error", address);
|
||||
responseMessage = translate("messages.dispatch.account.password_storage_error");
|
||||
}
|
||||
} else {
|
||||
loggerMessage = translate("messages.dispatch.account.account_login_exist_error", address);
|
||||
}
|
||||
} else {
|
||||
responseMessage = translate("messages.dispatch.account.server_max_player_limit");
|
||||
loggerMessage = translate("messages.dispatch.account.login_max_player_limit", address);
|
||||
}
|
||||
|
||||
// Set response data.
|
||||
if (successfulLogin) {
|
||||
response.message = "OK";
|
||||
response.data.account.uid = account.getId();
|
||||
response.data.account.token = account.generateSessionKey();
|
||||
response.data.account.email = account.getEmail();
|
||||
|
||||
loggerMessage =
|
||||
translate("messages.dispatch.account.login_success", address, account.getId());
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = responseMessage;
|
||||
}
|
||||
Grasscutter.getLogger().info(loggerMessage);
|
||||
|
||||
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(AuthenticationRequest request) {
|
||||
var response = new LoginResultJson();
|
||||
|
||||
var requestData = request.getTokenRequest();
|
||||
assert requestData != null;
|
||||
|
||||
boolean successfulLogin;
|
||||
String address = request.getContext().ip();
|
||||
String loggerMessage;
|
||||
int playerCount = Grasscutter.getGameServer().getPlayers().size();
|
||||
|
||||
// Log the attempt.
|
||||
Grasscutter.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);
|
||||
|
||||
// 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();
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = translate("messages.dispatch.account.server_max_player_limit");
|
||||
|
||||
loggerMessage = translate("messages.dispatch.account.login_max_player_limit", address);
|
||||
}
|
||||
|
||||
Grasscutter.getLogger().info(loggerMessage);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
/** Handles the authentication request from the game when using a combo token/session key. */
|
||||
public static class SessionKeyAuthenticator implements Authenticator<ComboTokenResJson> {
|
||||
@Override
|
||||
public ComboTokenResJson authenticate(AuthenticationRequest request) {
|
||||
var response = new ComboTokenResJson();
|
||||
|
||||
var requestData = request.getSessionKeyRequest();
|
||||
var loginData = request.getSessionKeyData();
|
||||
assert requestData != null;
|
||||
assert loginData != null;
|
||||
|
||||
boolean successfulLogin;
|
||||
String address = request.getContext().ip();
|
||||
String loggerMessage;
|
||||
int playerCount = Grasscutter.getGameServer().getPlayers().size();
|
||||
|
||||
if (ACCOUNT.maxPlayer <= -1 || playerCount < ACCOUNT.maxPlayer) {
|
||||
// Get account from database.
|
||||
Account account = DatabaseHelper.getAccountById(loginData.uid);
|
||||
|
||||
// Check if account exists/token is valid.
|
||||
successfulLogin = account != null && account.getSessionKey().equals(loginData.token);
|
||||
|
||||
// Set response data.
|
||||
if (successfulLogin) {
|
||||
response.message = "OK";
|
||||
response.data.open_id = account.getId();
|
||||
response.data.combo_id = "157795300";
|
||||
response.data.combo_token = account.generateLoginToken();
|
||||
|
||||
// Log the login.
|
||||
loggerMessage = translate("messages.dispatch.account.combo_token_success", address);
|
||||
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = translate("messages.dispatch.account.session_key_error");
|
||||
|
||||
// Log the failure.
|
||||
loggerMessage = translate("messages.dispatch.account.combo_token_error", address);
|
||||
}
|
||||
} else {
|
||||
response.retcode = -201;
|
||||
response.message = translate("messages.dispatch.account.server_max_player_limit");
|
||||
|
||||
loggerMessage = translate("messages.dispatch.account.login_max_player_limit", address);
|
||||
}
|
||||
|
||||
Grasscutter.getLogger().info(loggerMessage);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
/** Handles authentication requests from external sources. */
|
||||
public static class ExternalAuthentication implements ExternalAuthenticator {
|
||||
@Override
|
||||
public void handleLogin(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleAccountCreation(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePasswordReset(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
}
|
||||
|
||||
/** Handles authentication requests from OAuth sources.Zenlith */
|
||||
public static class OAuthAuthentication implements OAuthAuthenticator {
|
||||
@Override
|
||||
public void handleLogin(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRedirection(AuthenticationRequest request, ClientType type) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleTokenProcess(AuthenticationRequest request) {
|
||||
request
|
||||
.getContext()
|
||||
.result("Authentication is not available with the default authentication method.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
|
||||
|
||||
/** Handles authentication via external routes. */
|
||||
public interface ExternalAuthenticator {
|
||||
|
||||
/**
|
||||
* Called when an external login request is made.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
*/
|
||||
void handleLogin(AuthenticationRequest request);
|
||||
|
||||
/**
|
||||
* Called when an external account creation request is made.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
* <p>For developers: Use AuthenticationRequest#getRequest() to get the request body. Use
|
||||
* AuthenticationRequest#getResponse() to get the response body.
|
||||
*/
|
||||
void handleAccountCreation(AuthenticationRequest request);
|
||||
|
||||
/**
|
||||
* Called when an external password reset request is made.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
* <p>For developers: Use AuthenticationRequest#getRequest() to get the request body. Use
|
||||
* AuthenticationRequest#getResponse() to get the response body.
|
||||
*/
|
||||
void handlePasswordReset(AuthenticationRequest request);
|
||||
}
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
|
||||
|
||||
/** Handles authentication via external routes. */
|
||||
public interface ExternalAuthenticator {
|
||||
|
||||
/**
|
||||
* Called when an external login request is made.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
*/
|
||||
void handleLogin(AuthenticationRequest request);
|
||||
|
||||
/**
|
||||
* Called when an external account creation request is made.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
* <p>For developers: Use AuthenticationRequest#getRequest() to get the request body. Use
|
||||
* AuthenticationRequest#getResponse() to get the response body.
|
||||
*/
|
||||
void handleAccountCreation(AuthenticationRequest request);
|
||||
|
||||
/**
|
||||
* Called when an external password reset request is made.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
* <p>For developers: Use AuthenticationRequest#getRequest() to get the request body. Use
|
||||
* AuthenticationRequest#getResponse() to get the response body.
|
||||
*/
|
||||
void handlePasswordReset(AuthenticationRequest request);
|
||||
}
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
|
||||
|
||||
/** Handles authentication via OAuth routes. */
|
||||
public interface OAuthAuthenticator {
|
||||
|
||||
/**
|
||||
* Called when an OAuth login request is made.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
*/
|
||||
void handleLogin(AuthenticationRequest request);
|
||||
|
||||
/**
|
||||
* Called when a client requests to redirect to login page.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
*/
|
||||
void handleRedirection(AuthenticationRequest request, ClientType clientType);
|
||||
|
||||
/**
|
||||
* Called when an OAuth login requests callback.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
*/
|
||||
void handleTokenProcess(AuthenticationRequest request);
|
||||
|
||||
/** The type of the client. Used for handling redirection. */
|
||||
enum ClientType {
|
||||
DESKTOP,
|
||||
MOBILE
|
||||
}
|
||||
}
|
||||
package emu.grasscutter.auth;
|
||||
|
||||
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
|
||||
|
||||
/** Handles authentication via OAuth routes. */
|
||||
public interface OAuthAuthenticator {
|
||||
|
||||
/**
|
||||
* Called when an OAuth login request is made.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
*/
|
||||
void handleLogin(AuthenticationRequest request);
|
||||
|
||||
/**
|
||||
* Called when a client requests to redirect to login page.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
*/
|
||||
void handleRedirection(AuthenticationRequest request, ClientType clientType);
|
||||
|
||||
/**
|
||||
* Called when an OAuth login requests callback.
|
||||
*
|
||||
* @param request The authentication request.
|
||||
*/
|
||||
void handleTokenProcess(AuthenticationRequest request);
|
||||
|
||||
/** The type of the client. Used for handling redirection. */
|
||||
enum ClientType {
|
||||
DESKTOP,
|
||||
MOBILE
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user