From f270a954a6f311e553662cf5b31fbd2fa882c813 Mon Sep 17 00:00:00 2001 From: muhammadeko Date: Sun, 15 May 2022 00:25:05 +0700 Subject: [PATCH] refactor plugin to match 1.1.2-dev --- README.md | 2 +- src/main/java/me/exzork/gcauth/GCAuth.java | 4 +- .../gcauth/handler/ChangePasswordHandler.java | 71 ------- .../gcauth/handler/GCAuthAuthentication.java | 50 +++++ .../handler/GCAuthAuthenticationHandler.java | 198 +++++++++++++++--- .../exzork/gcauth/handler/LoginHandler.java | 61 ------ .../gcauth/handler/RegisterHandler.java | 91 -------- .../me/exzork/gcauth/router/GCAuthRouter.java | 17 -- 8 files changed, 226 insertions(+), 268 deletions(-) delete mode 100644 src/main/java/me/exzork/gcauth/handler/ChangePasswordHandler.java create mode 100644 src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java delete mode 100644 src/main/java/me/exzork/gcauth/handler/LoginHandler.java delete mode 100644 src/main/java/me/exzork/gcauth/handler/RegisterHandler.java delete mode 100644 src/main/java/me/exzork/gcauth/router/GCAuthRouter.java diff --git a/README.md b/README.md index 8e344d9..d63d75c 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Grasscutter Authentication System - To use access control, you need set the `ACCESS_KEY` in config.json inside plugins/GCAuth. (Optional) - All payload must be send with `application/json` and Compact JSON format ( without unnecessary spaces ) - Auth endpoint is: - - Authentication Checking : `/authentication/type` (GET) , it'll return `me.exzork.gcauth.handler.GCAuthAuthenticationHandler` if GCAuth is loaded and enabled. + - Authentication Checking : `/authentication/type` (GET) , it'll return `me.exzork.gcauth.handler.GCAuthAuthentication` if GCAuth is loaded and enabled. - Register: `/authentication/register` (POST) ``` {"username":"username","password":"password","password_confirmation":"password_confirmation"} diff --git a/src/main/java/me/exzork/gcauth/GCAuth.java b/src/main/java/me/exzork/gcauth/GCAuth.java index ea15a85..922ce48 100644 --- a/src/main/java/me/exzork/gcauth/GCAuth.java +++ b/src/main/java/me/exzork/gcauth/GCAuth.java @@ -5,7 +5,6 @@ import com.google.gson.GsonBuilder; import emu.grasscutter.Grasscutter; import emu.grasscutter.plugin.Plugin; import me.exzork.gcauth.handler.*; -import me.exzork.gcauth.router.GCAuthRouter; import me.exzork.gcauth.utils.Authentication; import java.io.File; @@ -31,8 +30,7 @@ public class GCAuth extends Plugin { } } loadConfig(); - Grasscutter.setAuthenticationSystem(new GCAuthAuthenticationHandler()); - //Grasscutter.getHttpServer().addRouter(GCAuthRouter.class); + Grasscutter.setAuthenticationSystem(new GCAuthAuthentication()); Grasscutter.getLogger().info("[GCAuth] GCAuth Enabled!"); config.jwtSecret = Authentication.generateRandomString(32); saveConfig(); diff --git a/src/main/java/me/exzork/gcauth/handler/ChangePasswordHandler.java b/src/main/java/me/exzork/gcauth/handler/ChangePasswordHandler.java deleted file mode 100644 index 3821e4b..0000000 --- a/src/main/java/me/exzork/gcauth/handler/ChangePasswordHandler.java +++ /dev/null @@ -1,71 +0,0 @@ -package me.exzork.gcauth.handler; - -import com.google.gson.Gson; -import emu.grasscutter.Grasscutter; -import emu.grasscutter.game.Account; -import express.http.HttpContextHandler; -import express.http.Request; -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.utils.Authentication; - -import java.io.IOException; - -public class ChangePasswordHandler implements HttpContextHandler { - @Override - public void handle(Request request, Response response) throws IOException { - AuthResponseJson authResponse = new AuthResponseJson(); - - try { - String requestBody = request.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); - } -} diff --git a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java new file mode 100644 index 0000000..c439ac5 --- /dev/null +++ b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthentication.java @@ -0,0 +1,50 @@ +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.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 handler = new GCAuthAuthenticationHandler(); + + @Override + public void createAccount(String username, String password) { + + } + + @Override + public void resetPassword(String s) { + + } + + @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 handler; + } +} diff --git a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java index b85e591..7c34bca 100644 --- a/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java +++ b/src/main/java/me/exzork/gcauth/handler/GCAuthAuthenticationHandler.java @@ -1,38 +1,188 @@ package me.exzork.gcauth.handler; +import com.google.gson.Gson; +import emu.grasscutter.Grasscutter; import emu.grasscutter.auth.AuthenticationSystem; -import emu.grasscutter.auth.Authenticator; -import emu.grasscutter.auth.DefaultAuthenticators; -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(); +import emu.grasscutter.auth.ExternalAuthenticator; +import emu.grasscutter.database.DatabaseHelper; +import emu.grasscutter.game.Account; +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 GCAuthAuthenticationHandler implements ExternalAuthenticator { @Override - public void createAccount(String username, String password) { - + public void handleLogin(AuthenticationSystem.AuthenticationRequest authenticationRequest) { + AuthResponseJson authResponse = new AuthResponseJson(); + try { + String requestBody = authenticationRequest.getRequest().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(); + } + authenticationRequest.getResponse().send(authResponse); } @Override - public void resetPassword(String s) { - + public void handleAccountCreation(AuthenticationSystem.AuthenticationRequest authenticationRequest) { + AuthResponseJson authResponse = new AuthResponseJson(); + Account account = null; + try { + String requestBody = authenticationRequest.getRequest().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); + } + } + } + authenticationRequest.getResponse().send(authResponse); } @Override - public Authenticator getPasswordAuthenticator() { - return gcAuthAuthenticator; - } + public void handlePasswordReset(AuthenticationSystem.AuthenticationRequest authenticationRequest) { + AuthResponseJson authResponse = new AuthResponseJson(); + try { + String requestBody = authenticationRequest.getRequest().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 getTokenAuthenticator() { - return tokenAuthenticator; - } - - @Override - public Authenticator getSessionKeyAuthenticator() { - return sessionKeyAuthenticator; + authenticationRequest.getResponse().send(authResponse); } } diff --git a/src/main/java/me/exzork/gcauth/handler/LoginHandler.java b/src/main/java/me/exzork/gcauth/handler/LoginHandler.java deleted file mode 100644 index 1121a1e..0000000 --- a/src/main/java/me/exzork/gcauth/handler/LoginHandler.java +++ /dev/null @@ -1,61 +0,0 @@ -package me.exzork.gcauth.handler; - -import com.google.gson.Gson; -import emu.grasscutter.Grasscutter; -import emu.grasscutter.game.Account; -import express.http.HttpContextHandler; -import express.http.Request; -import express.http.Response; -import me.exzork.gcauth.GCAuth; -import me.exzork.gcauth.json.AuthResponseJson; -import me.exzork.gcauth.json.LoginGenerateToken; -import me.exzork.gcauth.utils.Authentication; - -import java.io.IOException; - -public class LoginHandler implements HttpContextHandler { - @Override - public void handle(Request request, Response response) throws IOException { - AuthResponseJson authResponse = new AuthResponseJson(); - try { - String requestBody = request.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); - } -} diff --git a/src/main/java/me/exzork/gcauth/handler/RegisterHandler.java b/src/main/java/me/exzork/gcauth/handler/RegisterHandler.java deleted file mode 100644 index beb88a8..0000000 --- a/src/main/java/me/exzork/gcauth/handler/RegisterHandler.java +++ /dev/null @@ -1,91 +0,0 @@ -package me.exzork.gcauth.handler; - -import com.google.gson.Gson; -import emu.grasscutter.Grasscutter; -import emu.grasscutter.database.DatabaseHelper; -import emu.grasscutter.game.Account; -import express.http.HttpContextHandler; -import express.http.Request; -import express.http.Response; -import me.exzork.gcauth.GCAuth; -import me.exzork.gcauth.json.AuthResponseJson; -import me.exzork.gcauth.json.RegisterAccount; -import me.exzork.gcauth.utils.Authentication; - -import java.io.IOException; - -public class RegisterHandler implements HttpContextHandler { - @Override - public void handle(Request request, Response response) throws IOException { - AuthResponseJson authResponse = new AuthResponseJson(); - Account account = null; - try { - String requestBody = request.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); - } -} diff --git a/src/main/java/me/exzork/gcauth/router/GCAuthRouter.java b/src/main/java/me/exzork/gcauth/router/GCAuthRouter.java deleted file mode 100644 index 0c679ad..0000000 --- a/src/main/java/me/exzork/gcauth/router/GCAuthRouter.java +++ /dev/null @@ -1,17 +0,0 @@ -package me.exzork.gcauth.router; - -import emu.grasscutter.server.http.Router; -import express.Express; -import io.javalin.Javalin; -import me.exzork.gcauth.handler.ChangePasswordHandler; -import me.exzork.gcauth.handler.LoginHandler; -import me.exzork.gcauth.handler.RegisterHandler; - -public class GCAuthRouter implements Router { - @Override - public void applyRoutes(Express express, Javalin javalin) { - express.post("/authentication/login", new LoginHandler()); - express.post("/authentication/register", new RegisterHandler()); - express.post("/authentication/change_password", new ChangePasswordHandler()); - } -}