From 0310d47939c4ddefc0e7f115b8022ed7de1a9cd9 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 15 May 2022 08:35:37 +0800 Subject: [PATCH 1/2] Improve some code --- src/main/java/me/exzork/gcauth/GCAuth.java | 8 ++++--- .../gcauth/handler/GCAuthAuthentication.java | 15 +++++-------- .../handler/GCAuthAuthenticationHandler.java | 22 ++++++++++++++----- .../gcauth/handler/GCAuthenticators.java | 4 ++-- src/main/resources/plugin.json | 4 ++-- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/main/java/me/exzork/gcauth/GCAuth.java b/src/main/java/me/exzork/gcauth/GCAuth.java index b8c3f58..a0390b4 100644 --- a/src/main/java/me/exzork/gcauth/GCAuth.java +++ b/src/main/java/me/exzork/gcauth/GCAuth.java @@ -3,7 +3,10 @@ package me.exzork.gcauth; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import emu.grasscutter.Grasscutter; +import emu.grasscutter.auth.DefaultAuthentication; import emu.grasscutter.plugin.Plugin; +import static emu.grasscutter.Configuration.ACCOUNT; + import me.exzork.gcauth.handler.*; import me.exzork.gcauth.utils.Authentication; @@ -12,7 +15,6 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; -import java.util.logging.Logger; public class GCAuth extends Plugin { @@ -35,14 +37,14 @@ public class GCAuth extends Plugin { getLogger().info("GCAuth Enabled!"); config.jwtSecret = Authentication.generateRandomString(32); saveConfig(); - if (Grasscutter.getConfig().account.autoCreate) { + if (ACCOUNT.autoCreate) { getLogger().warn("GCAuth does not support automatic account creation. Please disable in the server's config.json or just ignore this warning."); } } @Override public void onDisable() { - + Grasscutter.setAuthenticationSystem(new DefaultAuthentication()); } public void loadConfig() { diff --git a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java index c439ac5..fcecaa8 100644 --- a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java +++ b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java @@ -1,9 +1,6 @@ package me.exzork.gcauth.handler; -import emu.grasscutter.auth.AuthenticationSystem; -import emu.grasscutter.auth.Authenticator; -import emu.grasscutter.auth.DefaultAuthenticators; -import emu.grasscutter.auth.ExternalAuthenticator; +import emu.grasscutter.auth.*; import emu.grasscutter.server.http.objects.ComboTokenResJson; import emu.grasscutter.server.http.objects.LoginResultJson; @@ -11,16 +8,16 @@ public class GCAuthAuthentication implements AuthenticationSystem { private final Authenticator gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator(); private final Authenticator tokenAuthenticator = new DefaultAuthenticators.TokenAuthenticator(); private final Authenticator sessionKeyAuthenticator = new DefaultAuthenticators.SessionKeyAuthenticator(); - private final GCAuthAuthenticationHandler handler = new GCAuthAuthenticationHandler(); + private final GCAuthAuthenticationHandler externalAuthenticator = new GCAuthAuthenticationHandler(); @Override public void createAccount(String username, String password) { - + // Unhandled. } @Override - public void resetPassword(String s) { - + public void resetPassword(String username) { + // Unhandled. } @Override @@ -45,6 +42,6 @@ public class GCAuthAuthentication implements AuthenticationSystem { @Override public ExternalAuthenticator getExternalAuthenticator() { - return handler; + return externalAuthenticator; } } diff --git a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java index 7c34bca..61e9ecf 100644 --- a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java +++ b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java @@ -6,6 +6,7 @@ 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; @@ -17,8 +18,11 @@ 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 = authenticationRequest.getRequest().ctx().body(); + String requestBody = response.ctx().body(); if (requestBody.isEmpty()) { authResponse.success = false; authResponse.message = "EMPTY_BODY"; // ENG = "No data was sent with the request" @@ -55,15 +59,18 @@ public class GCAuthAuthenticationHandler implements ExternalAuthenticator { Grasscutter.getLogger().error("[Dispatch] An error occurred while a user was logging in."); e.printStackTrace(); } - authenticationRequest.getResponse().send(authResponse); + 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 = authenticationRequest.getRequest().ctx().body(); + String requestBody = response.ctx().body(); if (requestBody.isEmpty()) { authResponse.success = false; authResponse.message = "EMPTY_BODY"; // ENG = "No data was sent with the request" @@ -129,14 +136,17 @@ public class GCAuthAuthenticationHandler implements ExternalAuthenticator { } } } - authenticationRequest.getResponse().send(authResponse); + 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 = authenticationRequest.getRequest().ctx().body(); + String requestBody = response.ctx().body(); if (requestBody.isEmpty()) { authResponse.success = false; authResponse.message = "EMPTY_BODY"; // ENG = "No data was sent with the request" @@ -183,6 +193,6 @@ public class GCAuthAuthenticationHandler implements ExternalAuthenticator { e.printStackTrace(); } - authenticationRequest.getResponse().send(authResponse); + response.send(authResponse); } } diff --git a/src/main/java/me/exzork/gcauth/handler/GCAuthenticators.java b/src/main/java/me/exzork/gcauth/handler/GCAuthenticators.java index bcc6809..468ac67 100644 --- a/src/main/java/me/exzork/gcauth/handler/GCAuthenticators.java +++ b/src/main/java/me/exzork/gcauth/handler/GCAuthenticators.java @@ -3,7 +3,6 @@ package me.exzork.gcauth.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 me.exzork.gcauth.utils.Authentication; @@ -17,7 +16,8 @@ public class GCAuthenticators { var response = new LoginResultJson(); var requestData = authenticationRequest.getPasswordRequest(); - assert requestData != null; + assert requestData != null; // This should never be null. + Account account = Authentication.getAccountByOneTimeToken(requestData.account); if(account == null) { Grasscutter.getLogger().info("[GCAuth] Client " + requestData.account + " tried to login with invalid one time token."); diff --git a/src/main/resources/plugin.json b/src/main/resources/plugin.json index 05f68d5..899645a 100644 --- a/src/main/resources/plugin.json +++ b/src/main/resources/plugin.json @@ -1,7 +1,7 @@ { "name": "GCAuth", "description": "GCAuth is a plugin that allows you to implement authentication for your server.", - "version": "1.0.0", + "version": "2.2.0", "author": ["ExZork"], "mainClass": "me.exzork.gcauth.GCAuth" -} \ No newline at end of file +} From 2c0fd486e11863d57b253d80cf04d523eba609b6 Mon Sep 17 00:00:00 2001 From: muhammadeko Date: Sun, 15 May 2022 09:30:08 +0700 Subject: [PATCH 2/2] change class name --- src/main/java/me/exzork/gcauth/GCAuth.java | 2 +- .../gcauth/handler/GCAuthAuthentication.java | 47 ---- .../handler/GCAuthAuthenticationHandler.java | 217 +++--------------- .../handler/GCAuthExternalAuthenticator.java | 198 ++++++++++++++++ 4 files changed, 232 insertions(+), 232 deletions(-) delete mode 100644 src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java create mode 100644 src/main/java/me/exzork/gcauth/handler/GCAuthExternalAuthenticator.java diff --git a/src/main/java/me/exzork/gcauth/GCAuth.java b/src/main/java/me/exzork/gcauth/GCAuth.java index a0390b4..330ab10 100644 --- a/src/main/java/me/exzork/gcauth/GCAuth.java +++ b/src/main/java/me/exzork/gcauth/GCAuth.java @@ -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(); diff --git a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java deleted file mode 100644 index fcecaa8..0000000 --- a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java +++ /dev/null @@ -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 gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator(); - private final Authenticator tokenAuthenticator = new DefaultAuthenticators.TokenAuthenticator(); - private final Authenticator 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 getPasswordAuthenticator() { - return gcAuthAuthenticator; - } - - @Override - public Authenticator getTokenAuthenticator() { - return tokenAuthenticator; - } - - @Override - public Authenticator getSessionKeyAuthenticator() { - return sessionKeyAuthenticator; - } - - @Override - public ExternalAuthenticator getExternalAuthenticator() { - return externalAuthenticator; - } -} diff --git a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java index 61e9ecf..8bef388 100644 --- a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java +++ b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java @@ -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 gcAuthAuthenticator = new GCAuthenticators.GCAuthAuthenticator(); + private final Authenticator tokenAuthenticator = new DefaultAuthenticators.TokenAuthenticator(); + private final Authenticator 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 getPasswordAuthenticator() { + return gcAuthAuthenticator; + } - response.send(authResponse); + @Override + public Authenticator getTokenAuthenticator() { + return tokenAuthenticator; + } + + @Override + public Authenticator getSessionKeyAuthenticator() { + return sessionKeyAuthenticator; + } + + @Override + public ExternalAuthenticator getExternalAuthenticator() { + return externalAuthenticator; } } diff --git a/src/main/java/me/exzork/gcauth/handler/GCAuthExternalAuthenticator.java b/src/main/java/me/exzork/gcauth/handler/GCAuthExternalAuthenticator.java new file mode 100644 index 0000000..b02a624 --- /dev/null +++ b/src/main/java/me/exzork/gcauth/handler/GCAuthExternalAuthenticator.java @@ -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); + } +}