Cleanup
Moved utility functions (rn just auth) to utils package. Also started playing with unregister cmd.
This commit is contained in:
parent
8054a8fa6d
commit
5ef361da64
|
@ -7,10 +7,7 @@ import net.minecraft.entity.player.PlayerEntity;
|
|||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.samo_lego.simpleauth.commands.AuthCommand;
|
||||
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.commands.*;
|
||||
import org.samo_lego.simpleauth.database.SimpleAuthDatabase;
|
||||
import org.samo_lego.simpleauth.event.AuthEventHandler;
|
||||
import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback;
|
||||
|
@ -42,13 +39,14 @@ public class SimpleAuth implements DedicatedServerModInitializer {
|
|||
RegisterCommand.registerCommand(dispatcher);
|
||||
LoginCommand.registerCommand(dispatcher);
|
||||
ChangepwCommand.registerCommand(dispatcher);
|
||||
UnregisterCommand.registerCommand(dispatcher);
|
||||
AuthCommand.registerCommand(dispatcher);
|
||||
});
|
||||
|
||||
// Registering the events
|
||||
PlayerJoinServerCallback.EVENT.register(AuthEventHandler::onPlayerJoin);
|
||||
PlayerLeaveServerCallback.EVENT.register(AuthEventHandler::onPlayerLeave);
|
||||
DropItemCallback.EVENT.register(player -> AuthEventHandler.onDropItem(player));
|
||||
DropItemCallback.EVENT.register(AuthEventHandler::onDropItem);
|
||||
// From Fabric API
|
||||
AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> AuthEventHandler.onAttackBlock(playerEntity));
|
||||
UseBlockCallback.EVENT.register((player, world, hand, blockHitResult) -> AuthEventHandler.onUseBlock(player));
|
||||
|
|
|
@ -14,6 +14,7 @@ import net.minecraft.text.TranslatableText;
|
|||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.samo_lego.simpleauth.SimpleAuth;
|
||||
import org.samo_lego.simpleauth.utils.AuthHelper;
|
||||
|
||||
import static com.mojang.brigadier.arguments.StringArgumentType.*;
|
||||
import static net.minecraft.server.command.CommandManager.argument;
|
||||
|
@ -85,7 +86,16 @@ public class AuthCommand {
|
|||
// Getting the player who send the command
|
||||
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();
|
||||
char[] password = pass.toCharArray();
|
||||
try {
|
||||
|
@ -101,7 +111,7 @@ public class AuthCommand {
|
|||
} finally {
|
||||
// Wipe confidential data
|
||||
argon2.wipeArray(password);
|
||||
}
|
||||
}*/
|
||||
// TODO -> Kick player whose name was changed?
|
||||
return 1; // Success
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import net.minecraft.server.command.ServerCommandSource;
|
|||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.TranslatableText;
|
||||
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.word;
|
||||
|
@ -49,12 +50,21 @@ public class ChangepwCommand {
|
|||
// Getting the player who send the command
|
||||
ServerPlayerEntity player = source.getPlayer();
|
||||
|
||||
// Create instance
|
||||
Argon2 argon2 = Argon2Factory.create();
|
||||
// Read password from user
|
||||
char[] password = oldPass.toCharArray();
|
||||
if (AuthHelper.checkPass(player.getUuidAsString(), oldPass.toCharArray())) {
|
||||
SimpleAuth.db.update(
|
||||
player.getUuidAsString(),
|
||||
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
|
||||
String hashedOld = SimpleAuth.db.getPassword(player.getUuidAsString());
|
||||
|
||||
|
@ -70,7 +80,6 @@ public class ChangepwCommand {
|
|||
} finally {
|
||||
// Wipe confidential data
|
||||
argon2.wipeArray(password);
|
||||
}
|
||||
return 1;
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import net.minecraft.text.LiteralText;
|
|||
import net.minecraft.text.Text;
|
||||
import net.minecraft.text.TranslatableText;
|
||||
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.word;
|
||||
|
@ -18,7 +19,6 @@ import static net.minecraft.server.command.CommandManager.argument;
|
|||
import static net.minecraft.server.command.CommandManager.literal;
|
||||
|
||||
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 wrongPassword = new TranslatableText("command.simpleauth.wrongPassword");
|
||||
private static TranslatableText alreadyAuthenticated = new TranslatableText("command.simpleauth.alreadyAuthenticated");
|
||||
|
@ -32,7 +32,7 @@ public class LoginCommand {
|
|||
))
|
||||
.executes(ctx -> {
|
||||
ctx.getSource().getPlayer().sendMessage(enterPassword);
|
||||
return 1;
|
||||
return 0;
|
||||
}));
|
||||
}
|
||||
|
||||
|
@ -43,9 +43,15 @@ public class LoginCommand {
|
|||
|
||||
if(SimpleAuth.isAuthenticated(player)) {
|
||||
player.sendMessage(alreadyAuthenticated);
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
// Create instance
|
||||
else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) {
|
||||
player.sendMessage(text);
|
||||
return 1;
|
||||
}
|
||||
player.sendMessage(wrongPassword);
|
||||
return 0;
|
||||
/*// Create instance
|
||||
Argon2 argon2 = Argon2Factory.create();
|
||||
// Read password from user
|
||||
char[] password = pass.toCharArray();
|
||||
|
@ -65,7 +71,6 @@ public class LoginCommand {
|
|||
// Wipe confidential data
|
||||
argon2.wipeArray(password);
|
||||
}
|
||||
}
|
||||
return 1; // Success
|
||||
return 1;*/
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import net.minecraft.server.network.ServerPlayerEntity;
|
|||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.text.TranslatableText;
|
||||
import org.samo_lego.simpleauth.SimpleAuth;
|
||||
import org.samo_lego.simpleauth.utils.AuthHelper;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -23,6 +24,7 @@ public class RegisterCommand {
|
|||
private static TranslatableText enterPassword = new TranslatableText("command.simpleauth.passwordTwice");
|
||||
private static TranslatableText alreadyAuthenticated = new TranslatableText("command.simpleauth.alreadyAuthenticated");
|
||||
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) {
|
||||
|
||||
|
@ -34,7 +36,7 @@ public class RegisterCommand {
|
|||
))
|
||||
.executes(ctx -> {
|
||||
ctx.getSource().getPlayer().sendMessage(enterPassword);
|
||||
return 1;
|
||||
return 0;
|
||||
}));
|
||||
}
|
||||
|
||||
|
@ -44,8 +46,16 @@ public class RegisterCommand {
|
|||
if(SimpleAuth.isAuthenticated(player)) {
|
||||
player.sendMessage(alreadyAuthenticated);
|
||||
}
|
||||
else if(pass1.equals(pass2)) { // Hashing the password with the Argon2 power
|
||||
// Create instance
|
||||
else if(pass1.equals(pass2)) {
|
||||
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();
|
||||
|
||||
// Read password from user
|
||||
|
@ -69,12 +79,11 @@ public class RegisterCommand {
|
|||
} finally {
|
||||
// Wipe confidential data
|
||||
argon2.wipeArray(password);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
else
|
||||
player.sendMessage(
|
||||
new LiteralText(source.getName() + ", passwords must match!")
|
||||
);
|
||||
return 1;
|
||||
player.sendMessage(
|
||||
new LiteralText(source.getName() + ", passwords must match!")
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue