Started with new uuid system.

This commit is contained in:
samo_lego 2020-04-19 09:38:43 +02:00
parent b54a27b93a
commit 0fc462b642
8 changed files with 67 additions and 21 deletions

View File

@ -25,6 +25,8 @@ import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
public class SimpleAuth implements DedicatedServerModInitializer {
private static final Logger LOGGER = LogManager.getLogger();
@ -36,8 +38,8 @@ public class SimpleAuth implements DedicatedServerModInitializer {
public static HashMap<String, PlayerCache> deauthenticatedUsers = new HashMap<>();
// Boolean for easier checking if player is authenticated
public static boolean isAuthenticated(ServerPlayerEntity player) {
return !deauthenticatedUsers.containsKey(player.getUuidAsString());
public static boolean isAuthenticated(PlayerEntity player) {
return !deauthenticatedUsers.containsKey(convertUuid(player));
}
// Getting game directory
@ -105,7 +107,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Authenticates player and sends the message
public static void authenticatePlayer(ServerPlayerEntity player, Text msg) {
deauthenticatedUsers.remove(player.getUuidAsString());
deauthenticatedUsers.remove(convertUuid(player));
// Player no longer needs to be invisible and invulnerable
player.setInvulnerable(false);
player.setInvisible(false);
@ -117,7 +119,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
if(db.isClosed())
return;
// Marking player as not authenticated, (re)setting login tries to zero
String uuid = player.getUuidAsString();
String uuid = convertUuid(player);
SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, player.getIp()));
// Player is now not authenticated

View File

@ -14,6 +14,7 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
public class ChangepwCommand {
private static Text enterNewPassword = new LiteralText(SimpleAuth.config.lang.enterNewPassword);
@ -55,7 +56,7 @@ public class ChangepwCommand {
player.sendMessage(cannotChangePassword, false);
return 0;
}
else if (AuthHelper.checkPass(player.getUuidAsString(), oldPass.toCharArray()) == 1) {
else if (AuthHelper.checkPass(convertUuid(player), oldPass.toCharArray()) == 1) {
if(newPass.length() < SimpleAuth.config.main.minPasswordChars) {
player.sendMessage(new LiteralText(
String.format(SimpleAuth.config.lang.minPasswordChars, SimpleAuth.config.main.minPasswordChars)
@ -73,7 +74,7 @@ public class ChangepwCommand {
String hash = AuthHelper.hashPass(newPass.toCharArray());
playerdata.addProperty("password", hash);
SimpleAuth.db.updateUserData(player.getUuidAsString(), playerdata.toString());
SimpleAuth.db.updateUserData(convertUuid(player), playerdata.toString());
player.sendMessage(passwordUpdated, false);
return 1;
}

View File

@ -2,6 +2,7 @@ package org.samo_lego.simpleauth.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
@ -13,6 +14,7 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
public class LoginCommand {
private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword);
@ -39,7 +41,8 @@ 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();
String uuid = convertUuid(player);
int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray());
if(SimpleAuth.isAuthenticated(player)) {

View File

@ -14,6 +14,7 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
public class RegisterCommand {
@ -67,7 +68,7 @@ public class RegisterCommand {
JsonObject playerdata = new JsonObject();
playerdata.addProperty("password", hash);
if (SimpleAuth.db.registerUser(player.getUuidAsString(), playerdata.toString())) {
if (SimpleAuth.db.registerUser(convertUuid(player), playerdata.toString())) {
SimpleAuth.authenticatePlayer(player, registerSuccess);
return 1;
}

View File

@ -13,6 +13,7 @@ import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
public class UnregisterCommand {
private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword);
@ -45,9 +46,9 @@ public class UnregisterCommand {
player.sendMessage(cannotUnregister, false);
return 0;
}
else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray()) == 1) {
else if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) {
SimpleAuth.deauthenticatePlayer(player);
SimpleAuth.db.deleteUserData(player.getUuidAsString());
SimpleAuth.db.deleteUserData(convertUuid(player));
player.sendMessage(accountDeleted, false);
return 1;
}

View File

@ -23,6 +23,7 @@ import java.util.regex.Pattern;
import static net.minecraft.block.NetherPortalBlock.AXIS;
import static net.minecraft.util.math.Direction.Axis.Z;
import static org.samo_lego.simpleauth.SimpleAuth.*;
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
/**
* This class will take care of actions players try to do,
@ -73,7 +74,7 @@ public class AuthEventHandler {
// Player joining the server
public static void onPlayerJoin(ServerPlayerEntity player) {
// Checking if session is still valid
String uuid = player.getUuidAsString();
String uuid = convertUuid(player);
PlayerCache playerCache = deauthenticatedUsers.getOrDefault(uuid, null);
if(
playerCache != null &&
@ -166,7 +167,9 @@ public class AuthEventHandler {
deauthenticatePlayer(player);
// Setting that player was actually authenticated before leaving
PlayerCache playerCache = deauthenticatedUsers.get(player.getUuidAsString());
PlayerCache playerCache = deauthenticatedUsers.get(convertUuid(player));
if(playerCache == null)
return;
playerCache.wasAuthenticated = true;
// Setting the session expire time
playerCache.validUntil = System.currentTimeMillis() + config.main.sessionTimeoutTime * 1000;
@ -177,7 +180,7 @@ public class AuthEventHandler {
// Getting the message to then be able to check it
String msg = chatMessageC2SPacket.getChatMessage();
if(
!isAuthenticated((ServerPlayerEntity) player) &&
!isAuthenticated(player) &&
!msg.startsWith("/login") &&
!msg.startsWith("/register") &&
(!config.experimental.allowChat || msg.startsWith("/"))
@ -190,7 +193,7 @@ public class AuthEventHandler {
// Player movement
public static ActionResult onPlayerMove(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowMovement) {
if(!isAuthenticated(player) && !config.experimental.allowMovement) {
return ActionResult.FAIL;
}
return ActionResult.PASS;
@ -198,7 +201,7 @@ public class AuthEventHandler {
// Using a block (right-click function)
public static ActionResult onUseBlock(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockUse) {
if(!isAuthenticated(player) && !config.experimental.allowBlockUse) {
player.sendMessage(notAuthenticated(), false);
return ActionResult.FAIL;
}
@ -207,7 +210,7 @@ public class AuthEventHandler {
// Punching a block
public static ActionResult onAttackBlock(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockPunch) {
if(!isAuthenticated(player) && !config.experimental.allowBlockPunch) {
player.sendMessage(notAuthenticated(), false);
return ActionResult.FAIL;
}
@ -216,7 +219,7 @@ public class AuthEventHandler {
// Using an item
public static TypedActionResult<ItemStack> onUseItem(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemUse) {
if(!isAuthenticated(player) && !config.experimental.allowItemUse) {
player.sendMessage(notAuthenticated(), false);
return TypedActionResult.fail(ItemStack.EMPTY);
}
@ -225,7 +228,7 @@ public class AuthEventHandler {
}
// Dropping an item
public static ActionResult onDropItem(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemDrop) {
if(!isAuthenticated(player) && !config.experimental.allowItemDrop) {
player.sendMessage(notAuthenticated(), false);
return ActionResult.FAIL;
}
@ -233,7 +236,7 @@ public class AuthEventHandler {
}
// Changing inventory (item moving etc.)
public static ActionResult onTakeItem(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowItemMoving) {
if(!isAuthenticated(player) && !config.experimental.allowItemMoving) {
player.sendMessage(notAuthenticated(), false);
return ActionResult.FAIL;
}
@ -242,7 +245,7 @@ public class AuthEventHandler {
}
// Attacking an entity
public static ActionResult onAttackEntity(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowEntityPunch) {
if(!isAuthenticated(player) && !config.experimental.allowEntityPunch) {
player.sendMessage(notAuthenticated(), false);
return ActionResult.FAIL;
}
@ -251,7 +254,7 @@ public class AuthEventHandler {
}
// Interacting with entity
public static ActionResult onUseEntity(PlayerEntity player) {
if(!isAuthenticated((ServerPlayerEntity) player) && !config.main.allowEntityInteract) {
if(!isAuthenticated(player) && !config.main.allowEntityInteract) {
player.sendMessage(notAuthenticated(), false);
return ActionResult.FAIL;
}

View File

@ -23,6 +23,9 @@ import java.net.SocketAddress;
@Mixin(PlayerManager.class)
public abstract class MixinPlayerManager {
@Final @Shadow
private MinecraftServer server;
@Inject(method = "onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;)V", at = @At("RETURN"))
private void onPlayerConnect(ClientConnection clientConnection, ServerPlayerEntity serverPlayerEntity, CallbackInfo ci) {
PlayerJoinServerCallback.EVENT.invoker().onPlayerJoin(serverPlayerEntity);

View File

@ -0,0 +1,32 @@
package org.samo_lego.simpleauth.utils;
import net.minecraft.entity.player.PlayerEntity;
import java.util.UUID;
/**
* Converts player uuid, to ensure player with "nAmE" and "NamE" get same uuid
* Both players are not allowed to play, since mod mimics Mojang behaviour
* of not allowing accounts with same names but different capitalization
*/
public class UuidConverter {
/** Converts player UUID to offline mode style.
*
* @param playername name of the player to get UUID for
* @return converted UUID as string
*/
public static String convertUuid(String playername) {
return PlayerEntity.getOfflinePlayerUuid(playername).toString();
}
/** Converts player UUID to offline mode style.
*
* @param player player to get UUID for
* @return converted UUID as string
*/
public static String convertUuid(PlayerEntity player) {
System.out.println("Playeruuid: " + player.getUuidAsString() + " converted: " + PlayerEntity.getOfflinePlayerUuid(player.getName().asString().toLowerCase()).toString());
return convertUuid(player.getName().asString().toLowerCase());
}
}