Moved utility functions (rn just auth) to utils package.
Also started playing with unregister cmd.
This commit is contained in:
samo_lego 2019-11-19 22:19:36 +01:00
parent 8054a8fa6d
commit 5ef361da64
7 changed files with 167 additions and 29 deletions

View File

@ -7,10 +7,7 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.samo_lego.simpleauth.commands.AuthCommand; import org.samo_lego.simpleauth.commands.*;
import org.samo_lego.simpleauth.commands.ChangepwCommand;
import org.samo_lego.simpleauth.commands.LoginCommand;
import org.samo_lego.simpleauth.commands.RegisterCommand;
import org.samo_lego.simpleauth.database.SimpleAuthDatabase; import org.samo_lego.simpleauth.database.SimpleAuthDatabase;
import org.samo_lego.simpleauth.event.AuthEventHandler; import org.samo_lego.simpleauth.event.AuthEventHandler;
import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback;
@ -42,13 +39,14 @@ public class SimpleAuth implements DedicatedServerModInitializer {
RegisterCommand.registerCommand(dispatcher); RegisterCommand.registerCommand(dispatcher);
LoginCommand.registerCommand(dispatcher); LoginCommand.registerCommand(dispatcher);
ChangepwCommand.registerCommand(dispatcher); ChangepwCommand.registerCommand(dispatcher);
UnregisterCommand.registerCommand(dispatcher);
AuthCommand.registerCommand(dispatcher); AuthCommand.registerCommand(dispatcher);
}); });
// Registering the events // Registering the events
PlayerJoinServerCallback.EVENT.register(AuthEventHandler::onPlayerJoin); PlayerJoinServerCallback.EVENT.register(AuthEventHandler::onPlayerJoin);
PlayerLeaveServerCallback.EVENT.register(AuthEventHandler::onPlayerLeave); PlayerLeaveServerCallback.EVENT.register(AuthEventHandler::onPlayerLeave);
DropItemCallback.EVENT.register(player -> AuthEventHandler.onDropItem(player)); DropItemCallback.EVENT.register(AuthEventHandler::onDropItem);
// From Fabric API // From Fabric API
AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> AuthEventHandler.onAttackBlock(playerEntity)); AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> AuthEventHandler.onAttackBlock(playerEntity));
UseBlockCallback.EVENT.register((player, world, hand, blockHitResult) -> AuthEventHandler.onUseBlock(player)); UseBlockCallback.EVENT.register((player, world, hand, blockHitResult) -> AuthEventHandler.onUseBlock(player));

View File

@ -14,6 +14,7 @@ import net.minecraft.text.TranslatableText;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.SimpleAuth;
import org.samo_lego.simpleauth.utils.AuthHelper;
import static com.mojang.brigadier.arguments.StringArgumentType.*; import static com.mojang.brigadier.arguments.StringArgumentType.*;
import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.argument;
@ -85,7 +86,16 @@ public class AuthCommand {
// Getting the player who send the command // Getting the player who send the command
Entity sender = source.getEntity(); Entity sender = source.getEntity();
// Create instance SimpleAuth.db.update(
uuid,
username,
AuthHelper.hashPass(pass.toCharArray())
);
if(sender != null)
sender.sendMessage(userdataUpdated);
else
LOGGER.info(userdataUpdated);
/*// Create instance
Argon2 argon2 = Argon2Factory.create(); Argon2 argon2 = Argon2Factory.create();
char[] password = pass.toCharArray(); char[] password = pass.toCharArray();
try { try {
@ -101,7 +111,7 @@ public class AuthCommand {
} finally { } finally {
// Wipe confidential data // Wipe confidential data
argon2.wipeArray(password); argon2.wipeArray(password);
} }*/
// TODO -> Kick player whose name was changed? // TODO -> Kick player whose name was changed?
return 1; // Success return 1; // Success
} }

View File

@ -8,6 +8,7 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.SimpleAuth;
import org.samo_lego.simpleauth.utils.AuthHelper;
import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word; import static com.mojang.brigadier.arguments.StringArgumentType.word;
@ -49,12 +50,21 @@ public class ChangepwCommand {
// Getting the player who send the command // Getting the player who send the command
ServerPlayerEntity player = source.getPlayer(); ServerPlayerEntity player = source.getPlayer();
// Create instance if (AuthHelper.checkPass(player.getUuidAsString(), oldPass.toCharArray())) {
Argon2 argon2 = Argon2Factory.create(); SimpleAuth.db.update(
// Read password from user player.getUuidAsString(),
char[] password = oldPass.toCharArray(); null,
AuthHelper.hashPass(newPass.toCharArray())
);
player.sendMessage(passwordUpdated);
return 1;
}
player.sendMessage(wrongPassword);
return 0;
try { // Create instance
//Argon2 argon2 = Argon2Factory.create();
/*try {
// Hashed password from DB // Hashed password from DB
String hashedOld = SimpleAuth.db.getPassword(player.getUuidAsString()); String hashedOld = SimpleAuth.db.getPassword(player.getUuidAsString());
@ -70,7 +80,6 @@ public class ChangepwCommand {
} finally { } finally {
// Wipe confidential data // Wipe confidential data
argon2.wipeArray(password); argon2.wipeArray(password);
} }*/
return 1;
} }
} }

View File

@ -11,6 +11,7 @@ import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.SimpleAuth;
import org.samo_lego.simpleauth.utils.AuthHelper;
import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word; import static com.mojang.brigadier.arguments.StringArgumentType.word;
@ -18,7 +19,6 @@ import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal; import static net.minecraft.server.command.CommandManager.literal;
public class LoginCommand { public class LoginCommand {
private static LiteralText pleaseLogin = new LiteralText("§4Type /login <password> to login.");
private static TranslatableText enterPassword = new TranslatableText("command.simpleauth.password"); private static TranslatableText enterPassword = new TranslatableText("command.simpleauth.password");
private static TranslatableText wrongPassword = new TranslatableText("command.simpleauth.wrongPassword"); private static TranslatableText wrongPassword = new TranslatableText("command.simpleauth.wrongPassword");
private static TranslatableText alreadyAuthenticated = new TranslatableText("command.simpleauth.alreadyAuthenticated"); private static TranslatableText alreadyAuthenticated = new TranslatableText("command.simpleauth.alreadyAuthenticated");
@ -32,7 +32,7 @@ public class LoginCommand {
)) ))
.executes(ctx -> { .executes(ctx -> {
ctx.getSource().getPlayer().sendMessage(enterPassword); ctx.getSource().getPlayer().sendMessage(enterPassword);
return 1; return 0;
})); }));
} }
@ -43,9 +43,15 @@ public class LoginCommand {
if(SimpleAuth.isAuthenticated(player)) { if(SimpleAuth.isAuthenticated(player)) {
player.sendMessage(alreadyAuthenticated); player.sendMessage(alreadyAuthenticated);
return 0;
} }
else { else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) {
// Create instance player.sendMessage(text);
return 1;
}
player.sendMessage(wrongPassword);
return 0;
/*// Create instance
Argon2 argon2 = Argon2Factory.create(); Argon2 argon2 = Argon2Factory.create();
// Read password from user // Read password from user
char[] password = pass.toCharArray(); char[] password = pass.toCharArray();
@ -65,7 +71,6 @@ public class LoginCommand {
// Wipe confidential data // Wipe confidential data
argon2.wipeArray(password); argon2.wipeArray(password);
} }
} return 1;*/
return 1; // Success
} }
} }

View File

@ -9,6 +9,7 @@ import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.SimpleAuth;
import org.samo_lego.simpleauth.utils.AuthHelper;
import java.util.Objects; import java.util.Objects;
@ -23,6 +24,7 @@ public class RegisterCommand {
private static TranslatableText enterPassword = new TranslatableText("command.simpleauth.passwordTwice"); private static TranslatableText enterPassword = new TranslatableText("command.simpleauth.passwordTwice");
private static TranslatableText alreadyAuthenticated = new TranslatableText("command.simpleauth.alreadyAuthenticated"); private static TranslatableText alreadyAuthenticated = new TranslatableText("command.simpleauth.alreadyAuthenticated");
private static TranslatableText alreadyRegistered = new TranslatableText("command.simpleauth.alreadyRegistered"); private static TranslatableText alreadyRegistered = new TranslatableText("command.simpleauth.alreadyRegistered");
private static TranslatableText registerSuccess = new TranslatableText("command.simpleauth.registerSuccess");
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) { public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
@ -34,7 +36,7 @@ public class RegisterCommand {
)) ))
.executes(ctx -> { .executes(ctx -> {
ctx.getSource().getPlayer().sendMessage(enterPassword); ctx.getSource().getPlayer().sendMessage(enterPassword);
return 1; return 0;
})); }));
} }
@ -44,8 +46,16 @@ public class RegisterCommand {
if(SimpleAuth.isAuthenticated(player)) { if(SimpleAuth.isAuthenticated(player)) {
player.sendMessage(alreadyAuthenticated); player.sendMessage(alreadyAuthenticated);
} }
else if(pass1.equals(pass2)) { // Hashing the password with the Argon2 power else if(pass1.equals(pass2)) {
// Create instance String hash = AuthHelper.hashPass(pass1.toCharArray());
if (SimpleAuth.db.registerUser(player.getUuidAsString(), source.getName(), hash)) {
SimpleAuth.authenticatedUsers.add(player);
player.sendMessage(registerSuccess);
return 1;
}
player.sendMessage(alreadyRegistered);
return 0;
/*// Create instance
Argon2 argon2 = Argon2Factory.create(); Argon2 argon2 = Argon2Factory.create();
// Read password from user // Read password from user
@ -69,12 +79,11 @@ public class RegisterCommand {
} finally { } finally {
// Wipe confidential data // Wipe confidential data
argon2.wipeArray(password); argon2.wipeArray(password);
} }*/
} }
else player.sendMessage(
player.sendMessage( new LiteralText(source.getName() + ", passwords must match!")
new LiteralText(source.getName() + ", passwords must match!") );
); return 0;
return 1;
} }
} }

View File

@ -0,0 +1,67 @@
package org.samo_lego.simpleauth.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import de.mkammerer.argon2.Argon2;
import de.mkammerer.argon2.Argon2Factory;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.TranslatableText;
import org.samo_lego.simpleauth.SimpleAuth;
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;
public class UnregisterCommand { // TODO
private static TranslatableText enterPassword = new TranslatableText("command.simpleauth.password");
private static TranslatableText wrongPassword = new TranslatableText("command.simpleauth.wrongPassword");
private static TranslatableText accountDeleted = new TranslatableText("command.simpleauth.passwordUpdated");
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
// Registering the "/changepw" command
dispatcher.register(literal("changepw")
.executes(ctx -> {
ctx.getSource().getPlayer().sendMessage(enterPassword);
return 1;
})
.then(argument("password", word())
.executes( ctx -> unregister(
ctx.getSource(),
getString(ctx, "password")
)
)
)
);
}
// Method called for checking the password and then changing it
private static int unregister(ServerCommandSource source, String pass) throws CommandSyntaxException {
// Getting the player who send the command
ServerPlayerEntity player = source.getPlayer();
// Create instance
Argon2 argon2 = Argon2Factory.create();
// Read password from user
char[] password = pass.toCharArray();
try {
// Hashed password from DB
String hashedOld = SimpleAuth.db.getPassword(player.getUuidAsString());
// Verify password
if (argon2.verify(hashedOld, password)) {
// Writing into DB
SimpleAuth.db.delete(player.getUuidAsString(), null);
player.sendMessage(accountDeleted);
}
else
player.sendMessage(wrongPassword);
} finally {
// Wipe confidential data
argon2.wipeArray(password);
}
return 1;
}
}

View File

@ -0,0 +1,40 @@
package org.samo_lego.simpleauth.utils;
import de.mkammerer.argon2.Argon2;
import de.mkammerer.argon2.Argon2Factory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.samo_lego.simpleauth.SimpleAuth;
public class AuthHelper {
private static final Logger LOGGER = LogManager.getLogger();
// Creating the instance
private static Argon2 argon2 = Argon2Factory.create();
public static boolean checkPass(String uuid, char[] pass) {
try {
// Hashed password from DB
String hashed = SimpleAuth.db.getPassword(uuid);
// Verify password
return argon2.verify(hashed, pass);
} catch(Error e) {
LOGGER.error("SimpleAut error: " + e);
} finally {
// Wipe confidential data
argon2.wipeArray(pass);
// Todo del line
System.out.println("Pass data wiped.");
}
return false;
}
// Hashing the password with the Argon2 power
public static String hashPass(char[] pass) {
try {
return argon2.hash(10, 65536, 1, pass);
} catch (Error e) {
LOGGER.error(e);
}
return null;
}
}