From ef301883b78f553b16043ba3bf573ae9c9501d2b Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Sun, 12 Apr 2020 17:54:52 +0200 Subject: [PATCH] Starting with password caching. --- .../org/samo_lego/simpleauth/SimpleAuth.java | 16 ++++++------ .../simpleauth/commands/AuthCommand.java | 2 +- .../simpleauth/commands/ChangepwCommand.java | 2 +- .../simpleauth/commands/LoginCommand.java | 13 +++++----- .../commands/UnregisterCommand.java | 2 +- .../{utils => storage}/AuthConfig.java | 2 +- .../simpleauth/storage/PlayerCache.java | 25 +++++++++++++++++++ .../SimpleAuthDatabase.java | 4 +-- .../simpleauth/utils/AuthHelper.java | 9 +++++-- 9 files changed, 53 insertions(+), 22 deletions(-) rename src/main/java/org/samo_lego/simpleauth/{utils => storage}/AuthConfig.java (99%) create mode 100644 src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java rename src/main/java/org/samo_lego/simpleauth/{database => storage}/SimpleAuthDatabase.java (96%) diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 335ac4d..9ce4860 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -5,21 +5,21 @@ import net.fabricmc.fabric.api.event.player.*; import net.fabricmc.fabric.api.event.server.ServerStopCallback; import net.fabricmc.fabric.api.registry.CommandRegistry; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.samo_lego.simpleauth.commands.*; -import org.samo_lego.simpleauth.database.SimpleAuthDatabase; import org.samo_lego.simpleauth.event.AuthEventHandler; import org.samo_lego.simpleauth.event.entity.player.ChatCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerMoveCallback; import org.samo_lego.simpleauth.event.item.DropItemCallback; import org.samo_lego.simpleauth.event.item.TakeItemCallback; -import org.samo_lego.simpleauth.utils.AuthConfig; +import org.samo_lego.simpleauth.storage.AuthConfig; +import org.samo_lego.simpleauth.storage.PlayerCache; +import org.samo_lego.simpleauth.storage.SimpleAuthDatabase; import java.io.File; import java.util.HashMap; @@ -33,11 +33,12 @@ public class SimpleAuth implements DedicatedServerModInitializer { // HashMap of players that are not authenticated // Rather than storing all the authenticated players, we just store ones that are not authenticated - public static HashMap deauthenticatedUsers = new HashMap<>(); + // It stores some data as well, e.g. login tries and user password + public static HashMap deauthenticatedUsers = new HashMap<>(); // Boolean for easier checking if player is authenticated public static boolean isAuthenticated(ServerPlayerEntity player) { - return !deauthenticatedUsers.containsKey(player); + return !deauthenticatedUsers.containsKey(player.getUuidAsString()); } // Getting game directory @@ -94,7 +95,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { // Authenticates player and sends the message public static void authenticatePlayer(ServerPlayerEntity player, Text msg) { - deauthenticatedUsers.remove(player); + deauthenticatedUsers.remove(player.getUuidAsString()); // Player no longer needs to be invisible and invulnerable player.setInvulnerable(false); player.setInvisible(false); @@ -112,7 +113,8 @@ public class SimpleAuth implements DedicatedServerModInitializer { // De-authenticates player public static void deauthenticatePlayer(ServerPlayerEntity player) { // Marking player as not authenticated, (re)setting login tries to zero - SimpleAuth.deauthenticatedUsers.put(player, 0); + String uuid = player.getUuidAsString(); + SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid)); // Player is now not authenticated player.sendMessage(notAuthenticated()); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java index a7a2084..21fa8b1 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java @@ -8,7 +8,7 @@ import net.minecraft.text.Text; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.samo_lego.simpleauth.SimpleAuth; -import org.samo_lego.simpleauth.utils.AuthConfig; +import org.samo_lego.simpleauth.storage.AuthConfig; import org.samo_lego.simpleauth.utils.AuthHelper; import java.io.File; diff --git a/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java index 8ff5321..41c7ba3 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/ChangepwCommand.java @@ -67,7 +67,7 @@ public class ChangepwCommand { )); return 0; } - SimpleAuth.db.update( + SimpleAuth.db.updateUserData( player.getUuidAsString(), AuthHelper.hashPass(newPass.toCharArray()) ); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java index c27bd19..5b7b58d 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -7,6 +7,7 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import org.samo_lego.simpleauth.SimpleAuth; +import org.samo_lego.simpleauth.storage.PlayerCache; import org.samo_lego.simpleauth.utils.AuthHelper; import static com.mojang.brigadier.arguments.StringArgumentType.getString; @@ -39,20 +40,21 @@ public class LoginCommand { private static int login(ServerCommandSource source, String pass) throws CommandSyntaxException { // Getting the player who send the command ServerPlayerEntity player = source.getPlayer(); + String uuid = player.getUuidAsString(); if(SimpleAuth.isAuthenticated(player)) { player.sendMessage(alreadyAuthenticated); return 0; } - else if(SimpleAuth.deauthenticatedUsers.get(player) >= maxLoginTries && maxLoginTries != -1) { + else if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) { player.networkHandler.disconnect(loginTriesExceeded); return 0; } - else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray()) == 1) { + else if (AuthHelper.checkPass(uuid, pass.toCharArray()) == 1) { SimpleAuth.authenticatePlayer(player, successfullyAuthenticated); return 1; } - else if(AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray()) == -1) { + else if(AuthHelper.checkPass(uuid, pass.toCharArray()) == -1) { player.sendMessage(notRegistered); return 0; } @@ -64,10 +66,7 @@ public class LoginCommand { // Sending wrong pass message player.sendMessage(wrongPassword); // ++ the login tries - SimpleAuth.deauthenticatedUsers.replace( - player, - SimpleAuth.deauthenticatedUsers.getOrDefault(player, 0) + 1 - ); + SimpleAuth.deauthenticatedUsers.getOrDefault(uuid, new PlayerCache(uuid)).loginTries += 1; return 0; } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java index b5c8677..6fc65c8 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java @@ -47,7 +47,7 @@ public class UnregisterCommand { } else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray()) == 1) { SimpleAuth.deauthenticatePlayer(player); - SimpleAuth.db.delete(player.getUuidAsString()); + SimpleAuth.db.deleteUserData(player.getUuidAsString()); player.sendMessage(accountDeleted); return 1; } diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java similarity index 99% rename from src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java rename to src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java index 5664005..eaeeaae 100644 --- a/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -package org.samo_lego.simpleauth.utils; +package org.samo_lego.simpleauth.storage; import com.google.gson.Gson; import com.google.gson.GsonBuilder; diff --git a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java new file mode 100644 index 0000000..04aba4e --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java @@ -0,0 +1,25 @@ +package org.samo_lego.simpleauth.storage; + +import org.samo_lego.simpleauth.SimpleAuth; + +public class PlayerCache { + public boolean isRegistered; + public boolean isAuthenticated; + public String password; + public int loginTries; + + public PlayerCache(String uuid) { + SimpleAuthDatabase db = SimpleAuth.db; + + this.isAuthenticated = false; + this.loginTries = 0; + if(db.isUserRegistered(uuid)) { + this.isRegistered = true; + this.password = db.getPassword(uuid); + } + else { + this.isRegistered = false; + this.password = ""; + } + } +} diff --git a/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java b/src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java similarity index 96% rename from src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java rename to src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java index 597bab5..fd9c300 100644 --- a/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java @@ -1,4 +1,4 @@ -package org.samo_lego.simpleauth.database; +package org.samo_lego.simpleauth.storage; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -54,7 +54,7 @@ public class SimpleAuthDatabase { } // Checks if user is registered - private boolean isUserRegistered(String uuid) { + public boolean isUserRegistered(String uuid) { try { return levelDBStore.get(bytes("UUID:" + uuid)) != null; } catch (DBException e) { diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java index 45cbf2e..4d28e38 100644 --- a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java +++ b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java @@ -10,7 +10,7 @@ public class AuthHelper { private static final Logger LOGGER = LogManager.getLogger(); // Creating the instance - private static Argon2 argon2 = Argon2Factory.create(); + private static final Argon2 argon2 = Argon2Factory.create(); // Returns 1 if password is correct, 0 if not // and -1 if user is not registered yet @@ -30,8 +30,13 @@ public class AuthHelper { } else { try { + String hashed; + // Password from cache + if(SimpleAuth.deauthenticatedUsers.containsKey(uuid)) + hashed = SimpleAuth.deauthenticatedUsers.get(uuid).password; // Hashed password from DB - String hashed = SimpleAuth.db.getPassword(uuid); + else + hashed = SimpleAuth.db.getPassword(uuid); if(hashed.equals("")) return -1; // User is not yet registered // Verify password