Rewamped UUIDs + gradle bugfix

This commit is contained in:
samo_lego 2020-04-19 17:00:23 +02:00
commit efe8f5727c
10 changed files with 78 additions and 21 deletions

View File

@ -3,6 +3,12 @@ plugins {
id 'maven-publish'
}
repositories {
// Carpet mod
maven {
url 'https://masa.dy.fi/maven'
}
}
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
@ -36,6 +42,9 @@ dependencies {
// JNA lib
include 'net.java.dev.jna:jna:5.5.0'
// carpetMod
modImplementation "carpet:fabric-carpet:${project.minecraft_version}-${project.carpet_core_version}"
}
processResources {

View File

@ -12,4 +12,7 @@ fabric_version=0.5.10+build.320-1.16
# Mod Properties
mod_version = 1.4.1
maven_group = org.samo_lego
archives_base_name = simpleauth
archives_base_name = simpleauth
# Carpet for debugging
carpet_core_version = 1.3.19+v200415

View File

@ -26,6 +26,7 @@ import java.util.Timer;
import java.util.TimerTask;
import static org.samo_lego.simpleauth.utils.CarpetHelper.isPlayerCarpetFake;
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
public class SimpleAuth implements DedicatedServerModInitializer {
private static final Logger LOGGER = LogManager.getLogger();
@ -39,7 +40,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Boolean for easier checking if player is authenticated
public static boolean isAuthenticated(ServerPlayerEntity player) {
String uuid = player.getUuidAsString();
String uuid = convertUuid(player);
return !deauthenticatedUsers.containsKey(uuid) || deauthenticatedUsers.get(uuid).wasAuthenticated;
}
@ -108,7 +109,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);
@ -120,7 +121,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,
@ -76,7 +77,7 @@ public class AuthEventHandler {
if(isPlayerFake(player))
return;
// Checking if session is still valid
String uuid = player.getUuidAsString();
String uuid = convertUuid(player);
PlayerCache playerCache = deauthenticatedUsers.getOrDefault(uuid, null);
if (
@ -174,7 +175,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;
@ -185,7 +188,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("/"))
@ -198,7 +201,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;
@ -206,7 +209,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;
}
@ -215,7 +218,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;
}
@ -224,7 +227,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);
}
@ -233,7 +236,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;
}
@ -241,7 +244,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;
}
@ -250,7 +253,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;
}
@ -259,7 +262,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());
}
}