From 7d6d238def0c5d914299a6af0e8e8afcedc421c4 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Mon, 9 Nov 2020 13:29:51 +0100 Subject: [PATCH] Full mongodb support, along with #33 --- gradle.properties | 2 +- .../simpleauth/event/AuthEventHandler.java | 31 +++++++++++-------- .../mixin/MixinServerPlayerEntity.java | 20 +++--------- .../simpleauth/storage/AuthConfig.java | 11 ++++--- .../simpleauth/storage/PlayerCache.java | 31 +++++++++---------- .../simpleauth/storage/database/MongoDB.java | 30 +++++++++++++----- 6 files changed, 67 insertions(+), 58 deletions(-) diff --git a/gradle.properties b/gradle.properties index a446c27..30f9c5b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,5 +20,5 @@ bcrypt_version = 0.9.0 bytes_version = 1.3.0 # Carpet for debugging -carpet_core_version = 1.4.0+v200623 +carpet_core_version = 1.4.16+v201105 carpet_branch = master diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index 580999f..150839d 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -67,18 +67,24 @@ public class AuthEventHandler { return; // Checking if session is still valid String uuid = ((PlayerAuth) player).getFakeUuid(); - PlayerCache playerCache = playerCacheMap.getOrDefault(uuid, null); + PlayerCache playerCache; - if (playerCache != null) { - if ( - playerCache.isAuthenticated && - playerCache.validUntil >= System.currentTimeMillis() && - player.getIp().equals(playerCache.lastIp) - ) { - // Valid session - ((PlayerAuth) player).setAuthenticated(true); - return; - } + if(!playerCacheMap.containsKey(uuid)) { + // First join + playerCache = PlayerCache.fromJson(player, uuid); + playerCacheMap.put(uuid, playerCache); + } + else { + playerCache = playerCacheMap.get(uuid); + } + + if ( + playerCache.isAuthenticated && + playerCache.validUntil >= System.currentTimeMillis() && + player.getIp().equals(playerCache.lastIp) + ) { + // Valid session + return; } ((PlayerAuth) player).setAuthenticated(false); @@ -113,9 +119,8 @@ public class AuthEventHandler { if(config.main.sessionTimeoutTime != -1) playerCache.validUntil = System.currentTimeMillis() + config.main.sessionTimeoutTime * 1000; } - else { + else if(config.main.spawnOnJoin) ((PlayerAuth) player).hidePosition(false); - } } // Player chatting diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayerEntity.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayerEntity.java index 2117ee2..1dbbce7 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayerEntity.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayerEntity.java @@ -46,10 +46,8 @@ public class MixinServerPlayerEntity implements PlayerAuth { */ @Override public void hidePosition(boolean hide) { - assert server != null; - PlayerCache cache = playerCacheMap.get(this.getFakeUuid()); - if(config.main.spawnOnJoin) + if(config.experimental.debugMode) logInfo("Teleporting " + player.getName().asString() + (hide ? " to spawn." : " to original position.")); if (hide) { // Saving position @@ -110,19 +108,11 @@ public class MixinServerPlayerEntity implements PlayerAuth { */ @Override public void setAuthenticated(boolean authenticated) { - PlayerCache playerCache; + PlayerCache playerCache = playerCacheMap.get(this.getFakeUuid()); + if(this.isAuthenticated() == authenticated) + return; + playerCache.isAuthenticated = authenticated; - if(!playerCacheMap.containsKey(this.getFakeUuid())) { - // First join - playerCache = PlayerCache.fromJson(player, this.getFakeUuid()); - playerCacheMap.put(this.getFakeUuid(), playerCache); - } - else { - playerCache = playerCacheMap.get(this.getFakeUuid()); - if(this.isAuthenticated() == authenticated) - return; - playerCache.isAuthenticated = authenticated; - } player.setInvulnerable(!authenticated && config.experimental.playerInvulnerable); player.setInvisible(!authenticated && config.experimental.playerInvisible); diff --git a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java index ed73a5e..683b1ee 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java @@ -30,6 +30,7 @@ import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo; public class AuthConfig { private static final Gson gson = new GsonBuilder() .setPrettyPrinting() + .serializeNulls() .create(); // If player is not authenticated, following conditions apply @@ -112,15 +113,15 @@ public class AuthConfig { public static class MongoDBCredentials { /** * Credentials for MongoDB database. - * Leave this as-is if you are using LevelDB or don't need - * special credentials. + * Leave this as-is if you are using LevelDB. */ + public String username = ""; + public String password = ""; public String host = "localhost"; public int port = 27017; + public String simpleAuthDatabase = "SimpleAuthPlayerData"; + public String userSourceDatabase = ""; public boolean useSsl = true; - public String databaseName = "SimpleAuthPlayerData"; - public String password = ""; - public String username = ""; } } diff --git a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java index e792e82..9d4bb49 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java @@ -3,6 +3,7 @@ package org.samo_lego.simpleauth.storage; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; import net.minecraft.block.Blocks; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; @@ -23,6 +24,7 @@ public class PlayerCache { * Used for {@link org.samo_lego.simpleauth.event.AuthEventHandler#onPlayerJoin(ServerPlayerEntity) session validation}. */ @Expose + @SerializedName("is_authenticated") public boolean isAuthenticated = false; /** * Hashed password of player. @@ -38,11 +40,13 @@ public class PlayerCache { * Used for {@link org.samo_lego.simpleauth.event.AuthEventHandler#onPlayerJoin(ServerPlayerEntity) sessions}. */ @Expose + @SerializedName("last_ip") public String lastIp; /** * Time until session is valid. */ @Expose + @SerializedName("valid_until") public long validUntil; /** @@ -72,30 +76,23 @@ public class PlayerCache { * * @param player player to create cache for */ - public PlayerCache(ServerPlayerEntity player) { - if(player != null) { - this.lastIp = player.getIp(); - // Setting position cache - this.lastLocation.dimension = player.getServerWorld(); - this.lastLocation.position = player.getPos(); - this.lastLocation.yaw = player.yaw; - this.lastLocation.pitch = player.pitch; - - this.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL); - } - } public static PlayerCache fromJson(ServerPlayerEntity player, String fakeUuid) { if(config.experimental.debugMode) logInfo("Creating cache for " + Objects.requireNonNull(player).getGameProfile().getName()); - PlayerCache playerCache = new PlayerCache(player);; - String json = DB.getUserData(fakeUuid); - if(!json.isEmpty()) { - // Parsing data from DB - playerCache = gson.fromJson(json, PlayerCache.class); + // Parsing data from DB + PlayerCache playerCache = gson.fromJson(json, PlayerCache.class); + if(player != null) { + // Setting position cache + playerCache.lastLocation.dimension = player.getServerWorld(); + playerCache.lastLocation.position = player.getPos(); + playerCache.lastLocation.yaw = player.yaw; + playerCache.lastLocation.pitch = player.pitch; + + playerCache.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL); } return playerCache; diff --git a/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java b/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java index 161d496..98398e2 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java @@ -22,20 +22,33 @@ public class MongoDB { private static MongoClient mongoClient; public static void initialize() { - /*mongoClient = MongoClients.create( + + /*MongoCredential credential = MongoCredential.createCredential( + config.mongoDBCredentials.username, + config.mongoDBCredentials.userSourceDatabase, + config.mongoDBCredentials.password.toCharArray() + ); + + mongoClient = MongoClients.create( + MongoClientSettings.builder() + .applyToClusterSettings(builder -> + builder.hosts(Collections.singletonList(new ServerAddress(config.mongoDBCredentials.host, config.mongoDBCredentials.port)))) + .applyToSslSettings(builder -> builder.enabled(config.mongoDBCredentials.useSsl)) + .credential(credential) + .build());*/ + mongoClient = MongoClients.create( String.format( - "mongodb://%s:%s@%s:%s/?authSource=db1&ssl=%s", + "mongodb://%s:%s@%s:%d/?authSource=%s&useSsl=%b", config.mongoDBCredentials.username, config.mongoDBCredentials.password, config.mongoDBCredentials.host, config.mongoDBCredentials.port, + config.mongoDBCredentials.userSourceDatabase, config.mongoDBCredentials.useSsl - ) - playerCache.wasOnFire = false; - );*/ - mongoClient = MongoClients.create(String.format("mongodb://%s:%d", config.mongoDBCredentials.host, config.mongoDBCredentials.port)); - MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.databaseName); + ); + //mongoClient = MongoClients.create(String.format("mongodb://%s:%d", config.mongoDBCredentials.host, config.mongoDBCredentials.port)); + MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.simpleAuthDatabase); collection = database.getCollection("players"); } @@ -71,6 +84,9 @@ public class MongoDB { updateList.add(new ReplaceOneModel<>(eq("UUID", uuid), new Document("UUID", uuid) .append("password", playerCache.password) + .append("is_authenticated", playerCache.isAuthenticated) + .append("last_ip", playerCache.lastIp) + .append("valid_until", playerCache.validUntil) ) ); }