mirror of
https://github.com/exzork/GCAuth.git
synced 2024-11-24 00:23:58 +00:00
change class name
This commit is contained in:
parent
0310d47939
commit
2c0fd486e1
@ -33,7 +33,7 @@ public class GCAuth extends Plugin {
|
||||
}
|
||||
}
|
||||
loadConfig();
|
||||
Grasscutter.setAuthenticationSystem(new GCAuthAuthentication());
|
||||
Grasscutter.setAuthenticationSystem(new GCAuthAuthenticationHandler());
|
||||
getLogger().info("GCAuth Enabled!");
|
||||
config.jwtSecret = Authentication.generateRandomString(32);
|
||||
saveConfig();
|
||||
|
@ -1,47 +0,0 @@
|
||||
package me.exzork.gcauth.handler;
|
||||
|
||||
import emu.grasscutter.auth.*;
|
||||
import emu.grasscutter.server.http.objects.ComboTokenResJson;
|
||||
import emu.grasscutter.server.http.objects.LoginResultJson;
|
||||
|
||||
public class GCAuthAuthentication implements AuthenticationSystem {
|
||||
private final Authenticator<LoginResultJson> gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator();
|
||||
private final Authenticator<LoginResultJson> tokenAuthenticator = new DefaultAuthenticators.TokenAuthenticator();
|
||||
private final Authenticator<ComboTokenResJson> sessionKeyAuthenticator = new DefaultAuthenticators.SessionKeyAuthenticator();
|
||||
private final GCAuthAuthenticationHandler externalAuthenticator = new GCAuthAuthenticationHandler();
|
||||
|
||||
@Override
|
||||
public void createAccount(String username, String password) {
|
||||
// Unhandled.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetPassword(String username) {
|
||||
// Unhandled.
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verifyUser(String s) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Authenticator<LoginResultJson> getPasswordAuthenticator() {
|
||||
return gcAuthAuthenticator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Authenticator<LoginResultJson> getTokenAuthenticator() {
|
||||
return tokenAuthenticator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Authenticator<ComboTokenResJson> getSessionKeyAuthenticator() {
|
||||
return sessionKeyAuthenticator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExternalAuthenticator getExternalAuthenticator() {
|
||||
return externalAuthenticator;
|
||||
}
|
||||
}
|
@ -1,198 +1,47 @@
|
||||
package me.exzork.gcauth.handler;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.auth.AuthenticationSystem;
|
||||
import emu.grasscutter.auth.ExternalAuthenticator;
|
||||
import emu.grasscutter.database.DatabaseHelper;
|
||||
import emu.grasscutter.game.Account;
|
||||
import express.http.Response;
|
||||
import me.exzork.gcauth.GCAuth;
|
||||
import me.exzork.gcauth.json.AuthResponseJson;
|
||||
import me.exzork.gcauth.json.ChangePasswordAccount;
|
||||
import me.exzork.gcauth.json.LoginGenerateToken;
|
||||
import me.exzork.gcauth.json.RegisterAccount;
|
||||
import me.exzork.gcauth.utils.Authentication;
|
||||
import emu.grasscutter.auth.*;
|
||||
import emu.grasscutter.server.http.objects.ComboTokenResJson;
|
||||
import emu.grasscutter.server.http.objects.LoginResultJson;
|
||||
|
||||
public class GCAuthAuthenticationHandler implements AuthenticationSystem {
|
||||
private final Authenticator<LoginResultJson> gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator();
|
||||
private final Authenticator<LoginResultJson> tokenAuthenticator = new DefaultAuthenticators.TokenAuthenticator();
|
||||
private final Authenticator<ComboTokenResJson> sessionKeyAuthenticator = new DefaultAuthenticators.SessionKeyAuthenticator();
|
||||
private final GCAuthExternalAuthenticator externalAuthenticator = new GCAuthExternalAuthenticator();
|
||||
|
||||
public class GCAuthAuthenticationHandler implements ExternalAuthenticator {
|
||||
@Override
|
||||
public void handleLogin(AuthenticationSystem.AuthenticationRequest authenticationRequest) {
|
||||
AuthResponseJson authResponse = new AuthResponseJson();
|
||||
Response response = authenticationRequest.getResponse();
|
||||
assert response != null; // This should never be null.
|
||||
|
||||
try {
|
||||
String requestBody = response.ctx().body();
|
||||
if (requestBody.isEmpty()) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "EMPTY_BODY"; // ENG = "No data was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
LoginGenerateToken loginGenerateToken = new Gson().fromJson(requestBody, LoginGenerateToken.class);
|
||||
if (!GCAuth.getConfigStatic().ACCESS_KEY.isEmpty() && !GCAuth.getConfigStatic().ACCESS_KEY.equals(loginGenerateToken.access_key)){
|
||||
authResponse.success = false;
|
||||
authResponse.message = "ERROR_ACCESS_KEY"; // ENG = "Error access key was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
Account account = Authentication.getAccountByUsernameAndPassword(loginGenerateToken.username, loginGenerateToken.password);
|
||||
if (account == null) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "INVALID_ACCOUNT"; // ENG = "Invalid username or password"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
if (account.getPassword() != null && !account.getPassword().isEmpty()) {
|
||||
authResponse.success = true;
|
||||
authResponse.message = "";
|
||||
authResponse.jwt = Authentication.generateJwt(account);
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "NO_PASSWORD"; // ENG = "There is no account password set. Please create a password by resetting it."
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "UNKNOWN"; // ENG = "An unknown error has occurred..."
|
||||
authResponse.jwt = "";
|
||||
Grasscutter.getLogger().error("[Dispatch] An error occurred while a user was logging in.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
response.send(authResponse);
|
||||
public void createAccount(String username, String password) {
|
||||
// Unhandled.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleAccountCreation(AuthenticationSystem.AuthenticationRequest authenticationRequest) {
|
||||
AuthResponseJson authResponse = new AuthResponseJson();
|
||||
Response response = authenticationRequest.getResponse();
|
||||
assert response != null; // This should never be null.
|
||||
|
||||
Account account = null;
|
||||
try {
|
||||
String requestBody = response.ctx().body();
|
||||
if (requestBody.isEmpty()) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "EMPTY_BODY"; // ENG = "No data was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
RegisterAccount registerAccount = new Gson().fromJson(requestBody, RegisterAccount.class);
|
||||
if (!GCAuth.getConfigStatic().ACCESS_KEY.isEmpty() && !GCAuth.getConfigStatic().ACCESS_KEY.equals(registerAccount.access_key)){
|
||||
authResponse.success = false;
|
||||
authResponse.message = "ERROR_ACCESS_KEY"; // ENG = "Error access key was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
if (registerAccount.password.equals(registerAccount.password_confirmation)) {
|
||||
if (registerAccount.password.length() >= 8) {
|
||||
String password = Authentication.generateHash(registerAccount.password);
|
||||
try{
|
||||
account = Authentication.getAccountByUsernameAndPassword(registerAccount.username, "");
|
||||
if (account != null) {
|
||||
account.setPassword(password);
|
||||
account.save();
|
||||
authResponse.success = true;
|
||||
authResponse.message = "";
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
account = DatabaseHelper.createAccountWithPassword(registerAccount.username, password);
|
||||
if (account == null) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "USERNAME_TAKEN"; // ENG = "Username has already been taken by another user."
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
authResponse.success = true;
|
||||
authResponse.message = "";
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
}catch (Exception ignored){
|
||||
authResponse.success = false;
|
||||
authResponse.message = "UNKNOWN"; // ENG = "Username has already been taken by another user."
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "PASSWORD_INVALID"; // ENG = "Password must be at least 8 characters long"
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "PASSWORD_MISMATCH"; // ENG = "Passwords do not match."
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "UNKNOWN"; // ENG = "An unknown error has occurred..."
|
||||
authResponse.jwt = "";
|
||||
Grasscutter.getLogger().error("[Dispatch] An error occurred while creating an account.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (authResponse.success) {
|
||||
if (GCAuth.getConfigStatic().defaultPermissions.length > 0) {
|
||||
for (String permission : GCAuth.getConfigStatic().defaultPermissions) {
|
||||
account.addPermission(permission);
|
||||
}
|
||||
}
|
||||
}
|
||||
response.send(authResponse);
|
||||
public void resetPassword(String username) {
|
||||
// Unhandled.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePasswordReset(AuthenticationSystem.AuthenticationRequest authenticationRequest) {
|
||||
AuthResponseJson authResponse = new AuthResponseJson();
|
||||
Response response = authenticationRequest.getResponse();
|
||||
assert response != null; // This should never be null.
|
||||
public boolean verifyUser(String s) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
String requestBody = response.ctx().body();
|
||||
if (requestBody.isEmpty()) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "EMPTY_BODY"; // ENG = "No data was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
ChangePasswordAccount changePasswordAccount = new Gson().fromJson(requestBody, ChangePasswordAccount.class);
|
||||
if (!GCAuth.getConfigStatic().ACCESS_KEY.isEmpty() && !GCAuth.getConfigStatic().ACCESS_KEY.equals(changePasswordAccount.access_key)){
|
||||
authResponse.success = false;
|
||||
authResponse.message = "ERROR_ACCESS_KEY"; // ENG = "Error access key was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
if (changePasswordAccount.new_password.equals(changePasswordAccount.new_password_confirmation)) {
|
||||
Account account = Authentication.getAccountByUsernameAndPassword(changePasswordAccount.username, changePasswordAccount.old_password);
|
||||
if (account == null) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "INVALID_ACCOUNT"; // ENG = "Invalid username or password"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
if (changePasswordAccount.new_password.length() >= 8) {
|
||||
String newPassword = Authentication.generateHash(changePasswordAccount.new_password);
|
||||
account.setPassword(newPassword);
|
||||
account.save();
|
||||
authResponse.success = true;
|
||||
authResponse.message = "";
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "PASSWORD_INVALID"; // ENG = "Password must be at least 8 characters long"
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "PASSWORD_MISMATCH"; // ENG = "Passwords do not match."
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "UNKNOWN"; // ENG = "An unknown error has occurred..."
|
||||
authResponse.jwt = "";
|
||||
Grasscutter.getLogger().error("[Dispatch] Error while changing user password.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
@Override
|
||||
public Authenticator<LoginResultJson> getPasswordAuthenticator() {
|
||||
return gcAuthAuthenticator;
|
||||
}
|
||||
|
||||
response.send(authResponse);
|
||||
@Override
|
||||
public Authenticator<LoginResultJson> getTokenAuthenticator() {
|
||||
return tokenAuthenticator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Authenticator<ComboTokenResJson> getSessionKeyAuthenticator() {
|
||||
return sessionKeyAuthenticator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExternalAuthenticator getExternalAuthenticator() {
|
||||
return externalAuthenticator;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,198 @@
|
||||
package me.exzork.gcauth.handler;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.auth.AuthenticationSystem;
|
||||
import emu.grasscutter.auth.ExternalAuthenticator;
|
||||
import emu.grasscutter.database.DatabaseHelper;
|
||||
import emu.grasscutter.game.Account;
|
||||
import express.http.Response;
|
||||
import me.exzork.gcauth.GCAuth;
|
||||
import me.exzork.gcauth.json.AuthResponseJson;
|
||||
import me.exzork.gcauth.json.ChangePasswordAccount;
|
||||
import me.exzork.gcauth.json.LoginGenerateToken;
|
||||
import me.exzork.gcauth.json.RegisterAccount;
|
||||
import me.exzork.gcauth.utils.Authentication;
|
||||
|
||||
public class GCAuthExternalAuthenticator implements ExternalAuthenticator {
|
||||
@Override
|
||||
public void handleLogin(AuthenticationSystem.AuthenticationRequest authenticationRequest) {
|
||||
AuthResponseJson authResponse = new AuthResponseJson();
|
||||
Response response = authenticationRequest.getResponse();
|
||||
assert response != null; // This should never be null.
|
||||
|
||||
try {
|
||||
String requestBody = response.ctx().body();
|
||||
if (requestBody.isEmpty()) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "EMPTY_BODY"; // ENG = "No data was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
LoginGenerateToken loginGenerateToken = new Gson().fromJson(requestBody, LoginGenerateToken.class);
|
||||
if (!GCAuth.getConfigStatic().ACCESS_KEY.isEmpty() && !GCAuth.getConfigStatic().ACCESS_KEY.equals(loginGenerateToken.access_key)){
|
||||
authResponse.success = false;
|
||||
authResponse.message = "ERROR_ACCESS_KEY"; // ENG = "Error access key was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
Account account = Authentication.getAccountByUsernameAndPassword(loginGenerateToken.username, loginGenerateToken.password);
|
||||
if (account == null) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "INVALID_ACCOUNT"; // ENG = "Invalid username or password"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
if (account.getPassword() != null && !account.getPassword().isEmpty()) {
|
||||
authResponse.success = true;
|
||||
authResponse.message = "";
|
||||
authResponse.jwt = Authentication.generateJwt(account);
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "NO_PASSWORD"; // ENG = "There is no account password set. Please create a password by resetting it."
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "UNKNOWN"; // ENG = "An unknown error has occurred..."
|
||||
authResponse.jwt = "";
|
||||
Grasscutter.getLogger().error("[Dispatch] An error occurred while a user was logging in.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
response.send(authResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleAccountCreation(AuthenticationSystem.AuthenticationRequest authenticationRequest) {
|
||||
AuthResponseJson authResponse = new AuthResponseJson();
|
||||
Response response = authenticationRequest.getResponse();
|
||||
assert response != null; // This should never be null.
|
||||
|
||||
Account account = null;
|
||||
try {
|
||||
String requestBody = response.ctx().body();
|
||||
if (requestBody.isEmpty()) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "EMPTY_BODY"; // ENG = "No data was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
RegisterAccount registerAccount = new Gson().fromJson(requestBody, RegisterAccount.class);
|
||||
if (!GCAuth.getConfigStatic().ACCESS_KEY.isEmpty() && !GCAuth.getConfigStatic().ACCESS_KEY.equals(registerAccount.access_key)){
|
||||
authResponse.success = false;
|
||||
authResponse.message = "ERROR_ACCESS_KEY"; // ENG = "Error access key was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
if (registerAccount.password.equals(registerAccount.password_confirmation)) {
|
||||
if (registerAccount.password.length() >= 8) {
|
||||
String password = Authentication.generateHash(registerAccount.password);
|
||||
try{
|
||||
account = Authentication.getAccountByUsernameAndPassword(registerAccount.username, "");
|
||||
if (account != null) {
|
||||
account.setPassword(password);
|
||||
account.save();
|
||||
authResponse.success = true;
|
||||
authResponse.message = "";
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
account = DatabaseHelper.createAccountWithPassword(registerAccount.username, password);
|
||||
if (account == null) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "USERNAME_TAKEN"; // ENG = "Username has already been taken by another user."
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
authResponse.success = true;
|
||||
authResponse.message = "";
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
}catch (Exception ignored){
|
||||
authResponse.success = false;
|
||||
authResponse.message = "UNKNOWN"; // ENG = "Username has already been taken by another user."
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "PASSWORD_INVALID"; // ENG = "Password must be at least 8 characters long"
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "PASSWORD_MISMATCH"; // ENG = "Passwords do not match."
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "UNKNOWN"; // ENG = "An unknown error has occurred..."
|
||||
authResponse.jwt = "";
|
||||
Grasscutter.getLogger().error("[Dispatch] An error occurred while creating an account.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (authResponse.success) {
|
||||
if (GCAuth.getConfigStatic().defaultPermissions.length > 0) {
|
||||
for (String permission : GCAuth.getConfigStatic().defaultPermissions) {
|
||||
account.addPermission(permission);
|
||||
}
|
||||
}
|
||||
}
|
||||
response.send(authResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePasswordReset(AuthenticationSystem.AuthenticationRequest authenticationRequest) {
|
||||
AuthResponseJson authResponse = new AuthResponseJson();
|
||||
Response response = authenticationRequest.getResponse();
|
||||
assert response != null; // This should never be null.
|
||||
|
||||
try {
|
||||
String requestBody = response.ctx().body();
|
||||
if (requestBody.isEmpty()) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "EMPTY_BODY"; // ENG = "No data was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
ChangePasswordAccount changePasswordAccount = new Gson().fromJson(requestBody, ChangePasswordAccount.class);
|
||||
if (!GCAuth.getConfigStatic().ACCESS_KEY.isEmpty() && !GCAuth.getConfigStatic().ACCESS_KEY.equals(changePasswordAccount.access_key)){
|
||||
authResponse.success = false;
|
||||
authResponse.message = "ERROR_ACCESS_KEY"; // ENG = "Error access key was sent with the request"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
if (changePasswordAccount.new_password.equals(changePasswordAccount.new_password_confirmation)) {
|
||||
Account account = Authentication.getAccountByUsernameAndPassword(changePasswordAccount.username, changePasswordAccount.old_password);
|
||||
if (account == null) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "INVALID_ACCOUNT"; // ENG = "Invalid username or password"
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
if (changePasswordAccount.new_password.length() >= 8) {
|
||||
String newPassword = Authentication.generateHash(changePasswordAccount.new_password);
|
||||
account.setPassword(newPassword);
|
||||
account.save();
|
||||
authResponse.success = true;
|
||||
authResponse.message = "";
|
||||
authResponse.jwt = "";
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "PASSWORD_INVALID"; // ENG = "Password must be at least 8 characters long"
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "PASSWORD_MISMATCH"; // ENG = "Passwords do not match."
|
||||
authResponse.jwt = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
authResponse.success = false;
|
||||
authResponse.message = "UNKNOWN"; // ENG = "An unknown error has occurred..."
|
||||
authResponse.jwt = "";
|
||||
Grasscutter.getLogger().error("[Dispatch] Error while changing user password.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
response.send(authResponse);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user