Bug fixes; /logout command didn't work

This commit is contained in:
samo_lego 2020-03-31 22:32:26 +02:00
parent aba4100297
commit af1c51bf73
6 changed files with 40 additions and 20 deletions

View File

@ -108,9 +108,6 @@ public class SimpleAuth implements DedicatedServerModInitializer {
} }
return new LiteralText(SimpleAuth.config.lang.notAuthenticated); return new LiteralText(SimpleAuth.config.lang.notAuthenticated);
} }
private static Text timeExpired = new LiteralText(SimpleAuth.config.lang.timeExpired);
private static int delay = SimpleAuth.config.main.delay;
public static void deauthenticatePlayer(ServerPlayerEntity player) { public static void deauthenticatePlayer(ServerPlayerEntity player) {
// Marking player as not authenticated, (re)setting login tries to zero // Marking player as not authenticated, (re)setting login tries to zero
@ -126,8 +123,8 @@ public class SimpleAuth implements DedicatedServerModInitializer {
@Override @Override
public void run() { public void run() {
if(!SimpleAuth.isAuthenticated(player)) // Kicking player if not authenticated if(!SimpleAuth.isAuthenticated(player)) // Kicking player if not authenticated
player.networkHandler.disconnect(timeExpired); player.networkHandler.disconnect(new LiteralText(SimpleAuth.config.lang.timeExpired));
} }
}, delay * 1000); }, SimpleAuth.config.main.delay * 1000);
} }
} }

View File

@ -37,17 +37,20 @@ public class LoginCommand {
// Method called for checking the password // Method called for checking the password
private static int login(ServerCommandSource source, String pass) throws CommandSyntaxException { private static int login(ServerCommandSource source, String pass) throws CommandSyntaxException {
// Getting the player who send the command // Getting the player who send the command
System.out.println(maxLoginTries);
ServerPlayerEntity player = source.getPlayer(); ServerPlayerEntity player = source.getPlayer();
if(SimpleAuth.isAuthenticated(player)) { if(SimpleAuth.isAuthenticated(player)) {
player.sendMessage(alreadyAuthenticated); player.sendMessage(alreadyAuthenticated);
return 0; return 0;
} }
else if(SimpleAuth.deauthenticatedUsers.get(player) >= maxLoginTries && maxLoginTries != -1) { else if(SimpleAuth.deauthenticatedUsers.get(player) >= maxLoginTries && maxLoginTries != -1) {
SimpleAuth.deauthenticatePlayer(player);
player.networkHandler.disconnect(loginTriesExceeded); player.networkHandler.disconnect(loginTriesExceeded);
return 0; return 0;
} }
else if(SimpleAuth.config.main.enableGlobalPassword) { else if(SimpleAuth.config.main.enableGlobalPassword) {
if (AuthHelper.checkPass("globalPass", pass.toCharArray())) { if (AuthHelper.checkPass(null, pass.toCharArray())) {
SimpleAuth.authenticatePlayer(player, successfullyAuthenticated); SimpleAuth.authenticatePlayer(player, successfullyAuthenticated);
return 1; return 1;
} }
@ -58,10 +61,10 @@ public class LoginCommand {
} }
// Kicking the player out // Kicking the player out
else if(maxLoginTries == 1) { else if(maxLoginTries == 1) {
SimpleAuth.deauthenticatePlayer(player);
player.networkHandler.disconnect(wrongPassword); player.networkHandler.disconnect(wrongPassword);
return 0; return 0;
} }
// Sending wrong pass message // Sending wrong pass message
player.sendMessage(wrongPassword); player.sendMessage(wrongPassword);
// ++ the login tries // ++ the login tries
@ -69,7 +72,6 @@ public class LoginCommand {
player, player,
SimpleAuth.deauthenticatedUsers.getOrDefault(player, 0) + 1 SimpleAuth.deauthenticatedUsers.getOrDefault(player, 0) + 1
); );
return 0; return 0;
} }
} }

View File

@ -14,15 +14,16 @@ public class LogoutCommand {
private static Text successfulLogout = new LiteralText(SimpleAuth.config.lang.successfulLogout); private static Text successfulLogout = new LiteralText(SimpleAuth.config.lang.successfulLogout);
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) { public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
// Registering the "/login" command // Registering the "/logout" command
dispatcher.register(literal("login") dispatcher.register(literal("logout")
.executes(ctx -> logout(ctx.getSource())) // Tries to authenticate user .executes(ctx -> logout(ctx.getSource())) // Tries to deauthenticate user
); );
} }
private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException { private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException {
ServerPlayerEntity player = serverCommandSource.getPlayer(); ServerPlayerEntity player = serverCommandSource.getPlayer();
SimpleAuth.deauthenticatePlayer(player); SimpleAuth.deauthenticatePlayer(player);
player.sendMessage(successfulLogout);
return 1; return 1;
} }
} }

View File

@ -6,12 +6,15 @@ import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import org.samo_lego.simpleauth.event.entity.player.ChatCallback; import org.samo_lego.simpleauth.event.entity.player.ChatCallback;
import org.samo_lego.simpleauth.event.entity.player.PlayerMoveCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerMoveCallback;
import org.samo_lego.simpleauth.event.item.TakeItemCallback;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket.Action.SWAP_HELD_ITEMS;
@Mixin(ServerPlayNetworkHandler.class) @Mixin(ServerPlayNetworkHandler.class)
public abstract class MixinServerPlayNetworkHandler { public abstract class MixinServerPlayNetworkHandler {
@Shadow @Shadow
@ -27,13 +30,30 @@ public abstract class MixinServerPlayNetworkHandler {
), ),
cancellable = true cancellable = true
) )
private void onChatMessage(ChatMessageC2SPacket chatMessageC2SPacket_1, CallbackInfo ci) { private void onChatMessage(ChatMessageC2SPacket chatMessageC2SPacket, CallbackInfo ci) {
ActionResult result = ChatCallback.EVENT.invoker().onPlayerChat(player, chatMessageC2SPacket_1); ActionResult result = ChatCallback.EVENT.invoker().onPlayerChat(player, chatMessageC2SPacket);
if (result == ActionResult.FAIL) { if (result == ActionResult.FAIL) {
ci.cancel(); ci.cancel();
} }
} }
// onClickWindow, onPickFromInventory todo
@Inject(
method = "onPlayerAction(Lnet/minecraft/network/packet/c2s/play/PlayerActionC2SPacket;)V",
at = @At(
value = "INVOKE",
target = "net/minecraft/network/NetworkThreadUtils.forceMainThread(Lnet/minecraft/network/Packet;Lnet/minecraft/network/listener/PacketListener;Lnet/minecraft/server/world/ServerWorld;)V",
shift = At.Shift.AFTER
),
cancellable = true
)
private void onPlayerAction(PlayerActionC2SPacket packet, CallbackInfo ci) {
if(packet.getAction() == SWAP_HELD_ITEMS) {
ActionResult result = TakeItemCallback.EVENT.invoker().onTakeItem(player);
if (result == ActionResult.FAIL) {
ci.cancel();
}
}
}
@Inject( @Inject(
method="onPlayerMove(Lnet/minecraft/network/packet/c2s/play/PlayerMoveC2SPacket;)V", method="onPlayerMove(Lnet/minecraft/network/packet/c2s/play/PlayerMoveC2SPacket;)V",
at = @At( at = @At(
@ -44,11 +64,11 @@ public abstract class MixinServerPlayNetworkHandler {
), ),
cancellable = true cancellable = true
) )
private void onPlayerMove(PlayerMoveC2SPacket playerMoveC2SPacket_1, CallbackInfo ci) { private void onPlayerMove(PlayerMoveC2SPacket playerMoveC2SPacket, CallbackInfo ci) {
ActionResult result = PlayerMoveCallback.EVENT.invoker().onPlayerMove(player); ActionResult result = PlayerMoveCallback.EVENT.invoker().onPlayerMove(player);
if (result == ActionResult.FAIL) { if (result == ActionResult.FAIL) {
// A bit ugly, I know. (we need to update player position) // A bit ugly, I know. (we need to update player position)
player.teleport(player.getX(), player.getY(), player.getZ()); player.requestTeleport(player.getX(), player.getY(), player.getZ());
ci.cancel(); ci.cancel();
} }
} }

View File

@ -52,7 +52,7 @@ public class AuthConfig {
public boolean playerInvulnerable = true; public boolean playerInvulnerable = true;
// If player should be invisible to mobs before authentication // If player should be invisible to mobs before authentication
public boolean playerInvisible = true; public boolean playerInvisible = true;
// Maximum login tries before kicking the player // Maximum login tries before kicking the player from server
// Set to -1 to allow unlimited, not recommended however // Set to -1 to allow unlimited, not recommended however
public int maxLoginTries = 1; public int maxLoginTries = 1;
// Time after which player will be kicked if not authenticated - in seconds // Time after which player will be kicked if not authenticated - in seconds
@ -77,8 +77,9 @@ public class AuthConfig {
public String cannotChangePassword = "§aYou cannot change password!"; public String cannotChangePassword = "§aYou cannot change password!";
public String cannotUnregister = "§aYou cannot unregister this account!"; public String cannotUnregister = "§aYou cannot unregister this account!";
public String notAuthenticated = "§cYou are not authenticated!\n§6Try with /login or /register."; public String notAuthenticated = "§cYou are not authenticated!\n§6Try with /login or /register.";
public String alreadyAuthenticated = "§4You are already authenticated."; public String alreadyAuthenticated = "§6You are already authenticated.";
public String successfullyAuthenticated = "§aYou are now authenticated."; public String successfullyAuthenticated = "§aYou are now authenticated.";
public String successfulLogout = "§aLogged out successfully.";
public String timeExpired = "§cTime for authentication has expired."; public String timeExpired = "§cTime for authentication has expired.";
public String alreadyRegistered = "§6This account name is already registered!"; public String alreadyRegistered = "§6This account name is already registered!";
public String registerSuccess = "§aYou are now authenticated."; public String registerSuccess = "§aYou are now authenticated.";
@ -86,7 +87,6 @@ public class AuthConfig {
public String userdataUpdated = "§aUserdata updated."; public String userdataUpdated = "§aUserdata updated.";
public String accountDeleted = "§4Your account was successfully deleted!"; public String accountDeleted = "§4Your account was successfully deleted!";
public String configurationReloaded = "§aConfiguration file was reloaded successfully."; public String configurationReloaded = "§aConfiguration file was reloaded successfully.";
public String successfulLogout;
} }
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
private static final Gson gson = new GsonBuilder() private static final Gson gson = new GsonBuilder()

View File

@ -13,7 +13,7 @@ public class AuthHelper {
private static Argon2 argon2 = Argon2Factory.create(); private static Argon2 argon2 = Argon2Factory.create();
public static boolean checkPass(String uuid, char[] pass) { public static boolean checkPass(String uuid, char[] pass) {
if(uuid.equals("globalPass") && SimpleAuth.config.main.enableGlobalPassword) { if(SimpleAuth.config.main.enableGlobalPassword) {
// We have global password enabled // We have global password enabled
try { try {
return argon2.verify(SimpleAuth.config.main.globalPassword, pass); return argon2.verify(SimpleAuth.config.main.globalPassword, pass);