Moving password related commands to own thread

This commit is contained in:
samo_lego 2020-06-11 13:49:33 +02:00
parent 66dcce30b7
commit a9172ed157
8 changed files with 129 additions and 103 deletions

View File

@ -10,7 +10,7 @@ loader_version=0.8.7+build.201
fabric_version=0.11.8+build.357-1.16
# Mod Properties
mod_version = 1.4.3
mod_version = 1.4.4
maven_group = org.samo_lego
archives_base_name = simpleauth

View File

@ -171,6 +171,10 @@ public class SimpleAuth implements DedicatedServerModInitializer {
world.updateListeners(pos.up(), world.getBlockState(pos.up()), world.getBlockState(pos.up()), 3);
}
// Setting last air to player
if(player.isSubmergedInWater())
player.setAir(deauthenticatedUsers.get(convertUuid(player)).lastAir);
deauthenticatedUsers.remove(convertUuid(player));
// Player no longer needs to be invisible and invulnerable
@ -194,6 +198,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Player is now not authenticated
player.sendMessage(notAuthenticated(), false);
// Setting the player to be invisible to mobs and also invulnerable
player.setInvulnerable(SimpleAuth.config.experimental.playerInvulnerable);
player.setInvisible(SimpleAuth.config.experimental.playerInvisible);

View File

@ -70,20 +70,23 @@ public class AccountCommand {
);
return 0;
}
if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) {
SimpleAuth.db.deleteUserData(convertUuid(player));
player.sendMessage(
new LiteralText(config.lang.accountDeleted),
false
);
SimpleAuth.deauthenticatePlayer(player);
return 1;
}
player.sendMessage(
new LiteralText(config.lang.wrongPassword),
// New thread to avoid lag spikes
new Thread(() -> {
if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) {
SimpleAuth.db.deleteUserData(convertUuid(player));
player.sendMessage(
new LiteralText(config.lang.accountDeleted),
false
);
SimpleAuth.deauthenticatePlayer(player);
return;
}
player.sendMessage(
new LiteralText(config.lang.wrongPassword),
false
);
}).start();
return 0;
}
@ -99,35 +102,38 @@ public class AccountCommand {
);
return 0;
}
else if (AuthHelper.checkPass(convertUuid(player), oldPass.toCharArray()) == 1) {
if(newPass.length() < config.main.minPasswordChars) {
player.sendMessage(new LiteralText(
String.format(config.lang.minPasswordChars, config.main.minPasswordChars)
), false);
return 0;
}
else if(newPass.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) {
player.sendMessage(new LiteralText(
String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars)
), false);
return 0;
}
// JSON object holding password (may hold some other info in the future)
JsonObject playerdata = new JsonObject();
String hash = AuthHelper.hashPass(newPass.toCharArray());
playerdata.addProperty("password", hash);
// New thread to avoid lag spikes
new Thread(() -> {
if (AuthHelper.checkPass(convertUuid(player), oldPass.toCharArray()) == 1) {
if (newPass.length() < config.main.minPasswordChars) {
player.sendMessage(new LiteralText(
String.format(config.lang.minPasswordChars, config.main.minPasswordChars)
), false);
return;
}
else if (newPass.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) {
player.sendMessage(new LiteralText(
String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars)
), false);
return;
}
// JSON object holding password (may hold some other info in the future)
JsonObject playerdata = new JsonObject();
String hash = AuthHelper.hashPass(newPass.toCharArray());
playerdata.addProperty("password", hash);
SimpleAuth.db.updateUserData(convertUuid(player), playerdata.toString());
player.sendMessage(
new LiteralText(config.lang.passwordUpdated),
SimpleAuth.db.updateUserData(convertUuid(player), playerdata.toString());
player.sendMessage(
new LiteralText(config.lang.passwordUpdated),
false
);
}
else
player.sendMessage(
new LiteralText(config.lang.wrongPassword),
false
);
return 1;
}
player.sendMessage(
new LiteralText(config.lang.wrongPassword),
false
);
);
}).start();
return 0;
}
}

View File

@ -114,10 +114,13 @@ public class AuthCommand {
private static int setGlobalPassword(ServerCommandSource source, String pass) {
// Getting the player who send the command
Entity sender = source.getEntity();
// Writing the global pass to config
config.main.globalPassword = AuthHelper.hashPass(pass.toCharArray());
config.main.enableGlobalPassword = true;
config.save(new File("./mods/SimpleAuth/config.json"));
// New thread to avoid lag spikes
new Thread(() -> {
// Writing the global pass to config
config.main.globalPassword = AuthHelper.hashPass(pass.toCharArray());
config.main.enableGlobalPassword = true;
config.save(new File("./mods/SimpleAuth/config.json"));
}).start();
if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.globalPasswordSet), false);
@ -147,8 +150,10 @@ public class AuthCommand {
// Deleting (unregistering) user's account
private static int removeAccount(ServerCommandSource source, String uuid) {
Entity sender = source.getEntity();
db.deleteUserData(uuid);
SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, null));
new Thread(() -> {
db.deleteUserData(uuid);
SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, null));
}).start();
if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataDeleted), false);
@ -162,18 +167,19 @@ public class AuthCommand {
// Getting the player who send the command
Entity sender = source.getEntity();
// JSON object holding password (may hold some other info in the future)
JsonObject playerdata = new JsonObject();
String hash = AuthHelper.hashPass(password.toCharArray());
playerdata.addProperty("password", hash);
new Thread(() -> {
// JSON object holding password (may hold some other info in the future)
JsonObject playerdata = new JsonObject();
String hash = AuthHelper.hashPass(password.toCharArray());
playerdata.addProperty("password", hash);
if(db.registerUser(uuid, playerdata.toString())) {
if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
else
LOGGER.info(config.lang.userdataUpdated);
return 1;
}
if (db.registerUser(uuid, playerdata.toString())) {
if (sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
else
LOGGER.info(config.lang.userdataUpdated);
}
}).start();
return 0;
}
@ -182,17 +188,18 @@ public class AuthCommand {
// Getting the player who send the command
Entity sender = source.getEntity();
// JSON object holding password (may hold some other info in the future)
JsonObject playerdata = new JsonObject();
String hash = AuthHelper.hashPass(password.toCharArray());
playerdata.addProperty("password", hash);
new Thread(() -> {
// JSON object holding password (may hold some other info in the future)
JsonObject playerdata = new JsonObject();
String hash = AuthHelper.hashPass(password.toCharArray());
playerdata.addProperty("password", hash);
db.updateUserData(uuid, playerdata.toString());
if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
else
LOGGER.info(config.lang.userdataUpdated);
return 1;
db.updateUserData(uuid, playerdata.toString());
if (sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
else
LOGGER.info(config.lang.userdataUpdated);
}).start();
return 0;
}
// todo PlayerEntity.getOfflinePlayerUuid("")
}

View File

@ -33,37 +33,38 @@ public class LoginCommand {
private static int login(ServerCommandSource source, String pass) throws CommandSyntaxException {
// Getting the player who send the command
ServerPlayerEntity player = source.getPlayer();
if(SimpleAuth.isAuthenticated(player)) {
String uuid = convertUuid(player);
if (SimpleAuth.isAuthenticated(player)) {
player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false);
return 0;
}
// Putting rest of the command in new thread to avoid lag spikes
new Thread(() -> {
int maxLoginTries = config.main.maxLoginTries;
int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray());
String uuid = convertUuid(player);
int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray());
int maxLoginTries = config.main.maxLoginTries;
if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) {
player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded));
return 0;
}
else if(passwordResult == 1) {
SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.successfullyAuthenticated));
return 1;
}
else if(passwordResult == -1) {
player.sendMessage(new LiteralText(config.lang.notRegistered), false);
return 0;
}
// Kicking the player out
else if(maxLoginTries == 1) {
player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword));
return 0;
}
// Sending wrong pass message
player.sendMessage(new LiteralText(config.lang.wrongPassword), false);
// ++ the login tries
SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1;
if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) {
player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded));
return;
}
else if(passwordResult == 1) {
SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.successfullyAuthenticated));
return;
}
else if(passwordResult == -1) {
player.sendMessage(new LiteralText(config.lang.notRegistered), false);
return;
}
// Kicking the player out
else if(maxLoginTries == 1) {
player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword));
return;
}
// Sending wrong pass message
player.sendMessage(new LiteralText(config.lang.wrongPassword), false);
// ++ the login tries
SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1;
}).start();
return 0;
}
}

View File

@ -44,18 +44,23 @@ public class RegisterCommand {
player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false);
return 0;
}
else if(pass1.equals(pass2)) {
else if(!pass1.equals(pass2)) {
player.sendMessage(new LiteralText(config.lang.matchPassword), false);
return 0;
}
// New thread to avoid lag spikes
new Thread(() -> {
if(pass1.length() < config.main.minPasswordChars) {
player.sendMessage(new LiteralText(
String.format(config.lang.minPasswordChars, config.main.minPasswordChars)
), false);
return 0;
return;
}
else if(pass1.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) {
player.sendMessage(new LiteralText(
String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars)
), false);
return 0;
return;
}
String hash = AuthHelper.hashPass(pass1.toCharArray());
// JSON object holding password (may hold some other info in the future)
@ -64,12 +69,10 @@ public class RegisterCommand {
if (SimpleAuth.db.registerUser(convertUuid(player), playerdata.toString())) {
SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.registerSuccess));
return 1;
return;
}
player.sendMessage(new LiteralText(config.lang.alreadyRegistered), false);
return 0;
}
player.sendMessage(new LiteralText(config.lang.matchPassword), false);
}).start();
return 0;
}
}

View File

@ -17,6 +17,8 @@ public class PlayerCache {
public String lastIp;
public long validUntil;
public int lastAir = 300;
public String lastDim;
public double lastX;
public double lastY;
@ -33,6 +35,8 @@ public class PlayerCache {
if(player != null) {
this.lastIp = player.getIp();
this.lastAir = player.getAir();
// Setting position cache
this.lastDim = String.valueOf(player.getEntityWorld().getDimension());
this.lastX = player.getX();

View File

@ -50,7 +50,7 @@ public class AuthHelper {
// Verify password
return argon2.verify(hashed, pass) ? 1 : 0;
} catch (Error e) {
LOGGER.error("[SimpleAuth] error: " + e);
LOGGER.error("[SimpleAuth] Argon2 error: " + e);
return 0;
} finally {
// Wipe confidential data