diff options
author | Petr Ilin <hevav@hevav.dev> | 2022-03-25 18:30:41 +0300 |
---|---|---|
committer | Petr Ilin <hevav@hevav.dev> | 2022-03-25 18:32:05 +0300 |
commit | 82424d4408abe364cdbc3d640829b550c88d93d5 (patch) | |
tree | 39656f3ba863a6bb933b18899eb29b1ba8efd84e /src/main/java/net/elytrium | |
parent | 2435294b7232923443ecda62d2c29aa119a289eb (diff) | |
download | LimboAuth-82424d4408abe364cdbc3d640829b550c88d93d5.tar.gz LimboAuth-82424d4408abe364cdbc3d640829b550c88d93d5.tar.bz2 LimboAuth-82424d4408abe364cdbc3d640829b550c88d93d5.zip |
Ability to change auth commands "name"
Diffstat (limited to 'src/main/java/net/elytrium')
3 files changed, 41 insertions, 13 deletions
diff --git a/src/main/java/net/elytrium/limboauth/LimboAuth.java b/src/main/java/net/elytrium/limboauth/LimboAuth.java index 2530ef9..8bde70d 100644 --- a/src/main/java/net/elytrium/limboauth/LimboAuth.java +++ b/src/main/java/net/elytrium/limboauth/LimboAuth.java @@ -17,7 +17,6 @@ package net.elytrium.limboauth; -import com.google.common.collect.ImmutableList; import com.google.inject.Inject; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.DaoManager; @@ -274,9 +273,9 @@ public class LimboAuth { this.authServer = this.factory .createLimbo(authWorld) .setName("LimboAuth") - .registerCommand(new AuthCommandMeta(this, ImmutableList.of("2fa", "totp")), new AuthCommand()) - .registerCommand(new AuthCommandMeta(this, ImmutableList.of("login", "l", "log")), new AuthCommand()) - .registerCommand(new AuthCommandMeta(this, ImmutableList.of("reg", "register")), new AuthCommand()); + .registerCommand(new AuthCommandMeta(this, this.filterCommands(Settings.IMP.MAIN.REGISTER_COMMAND)), new AuthCommand()) + .registerCommand(new AuthCommandMeta(this, this.filterCommands(Settings.IMP.MAIN.LOGIN_COMMAND)), new AuthCommand()) + .registerCommand(new AuthCommandMeta(this, this.filterCommands(Settings.IMP.MAIN.TOTP_COMMAND)), new AuthCommand()); this.server.getEventManager().unregisterListeners(this); this.server.getEventManager().register(this, new AuthListener(this, this.playerDao, this.floodgateApi)); @@ -289,6 +288,10 @@ public class LimboAuth { ); } + private List<String> filterCommands(List<String> commands) { + return commands.stream().filter(e -> e.startsWith("/")).map(e -> e.substring(1)).collect(Collectors.toList()); + } + public void migrateDb(Dao<RegisteredPlayer, String> playerDao) { Set<FieldType> tables = new HashSet<>(); Collections.addAll(tables, playerDao.getTableInfo().getFieldTypes()); diff --git a/src/main/java/net/elytrium/limboauth/Settings.java b/src/main/java/net/elytrium/limboauth/Settings.java index 997cff4..f584e21 100644 --- a/src/main/java/net/elytrium/limboauth/Settings.java +++ b/src/main/java/net/elytrium/limboauth/Settings.java @@ -18,6 +18,7 @@ package net.elytrium.limboauth; import java.io.File; +import java.util.List; import net.elytrium.limboauth.config.Config; import net.kyori.adventure.title.Title; import net.kyori.adventure.util.Ticks; @@ -116,6 +117,10 @@ public class Settings extends Config { }) public String ISPREMIUM_AUTH_URL = "https://api.mojang.com/users/profiles/minecraft/%s"; + public List<String> REGISTER_COMMAND = List.of("/r", "/reg", "/register"); + public List<String> LOGIN_COMMAND = List.of("/l", "/log", "/login"); + public List<String> TOTP_COMMAND = List.of("/2fa", "/totp"); + @Create public Settings.MAIN.WORLD_COORDS WORLD_COORDS; diff --git a/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java b/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java index 03f9d5f..9a6ca8b 100644 --- a/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java +++ b/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java @@ -115,10 +115,9 @@ public class AuthSessionHandler implements LimboSessionHandler { public void onChat(String message) { String[] args = message.split(" "); if (args.length != 0 && this.checkArgsLength(args.length)) { - switch (args[0]) { - case "/reg": - case "/register": - case "/r": { + Command command = Command.parse(args[0]); + switch (command) { + case REGISTER: { if (!this.totp && this.playerInfo == null) { if (this.checkPasswordsRepeat(args) && this.checkPasswordLength(args[1]) && this.checkPasswordStrength(args[1])) { this.register(args[1]); @@ -139,9 +138,7 @@ public class AuthSessionHandler implements LimboSessionHandler { } break; } - case "/log": - case "/login": - case "/l": { + case LOGIN: { if (!this.totp && this.playerInfo != null) { if (this.checkPassword(args[1])) { this.loginOrTotp(); @@ -155,8 +152,7 @@ public class AuthSessionHandler implements LimboSessionHandler { } break; } - case "/totp": - case "/2fa": { + case TOTP: { if (this.totp) { if (verifier.isValidCode(this.playerInfo.getTotpToken(), args[1])) { this.finishLogin(); @@ -168,6 +164,7 @@ public class AuthSessionHandler implements LimboSessionHandler { } break; } + case INVALID: default: { this.sendMessage(false); } @@ -423,4 +420,27 @@ public class AuthSessionHandler implements LimboSessionHandler { public static String genHash(String password) { return BCrypt.withDefaults().hashToString(Settings.IMP.MAIN.BCRYPT_COST, password.toCharArray()); } + + private enum Command { + INVALID, + REGISTER, + LOGIN, + TOTP; + + static Command parse(String command) { + if (Settings.IMP.MAIN.REGISTER_COMMAND.contains(command)) { + return Command.REGISTER; + } + + if (Settings.IMP.MAIN.LOGIN_COMMAND.contains(command)) { + return Command.LOGIN; + } + + if (Settings.IMP.MAIN.TOTP_COMMAND.contains(command)) { + return Command.TOTP; + } + + return Command.INVALID; + } + } } |