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 fabric_version=0.11.8+build.357-1.16
# Mod Properties # Mod Properties
mod_version = 1.4.3 mod_version = 1.4.4
maven_group = org.samo_lego maven_group = org.samo_lego
archives_base_name = simpleauth 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); 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)); deauthenticatedUsers.remove(convertUuid(player));
// Player no longer needs to be invisible and invulnerable // Player no longer needs to be invisible and invulnerable
@ -194,6 +198,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Player is now not authenticated // Player is now not authenticated
player.sendMessage(notAuthenticated(), false); player.sendMessage(notAuthenticated(), false);
// Setting the player to be invisible to mobs and also invulnerable // Setting the player to be invisible to mobs and also invulnerable
player.setInvulnerable(SimpleAuth.config.experimental.playerInvulnerable); player.setInvulnerable(SimpleAuth.config.experimental.playerInvulnerable);
player.setInvisible(SimpleAuth.config.experimental.playerInvisible); player.setInvisible(SimpleAuth.config.experimental.playerInvisible);

View File

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

View File

@ -114,10 +114,13 @@ public class AuthCommand {
private static int setGlobalPassword(ServerCommandSource source, String pass) { private static int setGlobalPassword(ServerCommandSource source, String pass) {
// Getting the player who send the command // Getting the player who send the command
Entity sender = source.getEntity(); Entity sender = source.getEntity();
// Writing the global pass to config // New thread to avoid lag spikes
config.main.globalPassword = AuthHelper.hashPass(pass.toCharArray()); new Thread(() -> {
config.main.enableGlobalPassword = true; // Writing the global pass to config
config.save(new File("./mods/SimpleAuth/config.json")); config.main.globalPassword = AuthHelper.hashPass(pass.toCharArray());
config.main.enableGlobalPassword = true;
config.save(new File("./mods/SimpleAuth/config.json"));
}).start();
if(sender != null) if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.globalPasswordSet), false); ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.globalPasswordSet), false);
@ -147,8 +150,10 @@ public class AuthCommand {
// Deleting (unregistering) user's account // Deleting (unregistering) user's account
private static int removeAccount(ServerCommandSource source, String uuid) { private static int removeAccount(ServerCommandSource source, String uuid) {
Entity sender = source.getEntity(); Entity sender = source.getEntity();
db.deleteUserData(uuid); new Thread(() -> {
SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, null)); db.deleteUserData(uuid);
SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, null));
}).start();
if(sender != null) if(sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataDeleted), false); ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataDeleted), false);
@ -162,18 +167,19 @@ 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();
// JSON object holding password (may hold some other info in the future) new Thread(() -> {
JsonObject playerdata = new JsonObject(); // JSON object holding password (may hold some other info in the future)
String hash = AuthHelper.hashPass(password.toCharArray()); JsonObject playerdata = new JsonObject();
playerdata.addProperty("password", hash); String hash = AuthHelper.hashPass(password.toCharArray());
playerdata.addProperty("password", hash);
if(db.registerUser(uuid, playerdata.toString())) { if (db.registerUser(uuid, playerdata.toString())) {
if(sender != null) if (sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
else else
LOGGER.info(config.lang.userdataUpdated); LOGGER.info(config.lang.userdataUpdated);
return 1; }
} }).start();
return 0; return 0;
} }
@ -182,17 +188,18 @@ 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();
// JSON object holding password (may hold some other info in the future) new Thread(() -> {
JsonObject playerdata = new JsonObject(); // JSON object holding password (may hold some other info in the future)
String hash = AuthHelper.hashPass(password.toCharArray()); JsonObject playerdata = new JsonObject();
playerdata.addProperty("password", hash); String hash = AuthHelper.hashPass(password.toCharArray());
playerdata.addProperty("password", hash);
db.updateUserData(uuid, playerdata.toString()); db.updateUserData(uuid, playerdata.toString());
if(sender != null) if (sender != null)
((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false); ((PlayerEntity) sender).sendMessage(new LiteralText(config.lang.userdataUpdated), false);
else else
LOGGER.info(config.lang.userdataUpdated); LOGGER.info(config.lang.userdataUpdated);
return 1; }).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 { private static int login(ServerCommandSource source, String pass) throws CommandSyntaxException {
// Getting the player who send the command // Getting the player who send the command
ServerPlayerEntity player = source.getPlayer(); ServerPlayerEntity player = source.getPlayer();
String uuid = convertUuid(player);
if(SimpleAuth.isAuthenticated(player)) { if (SimpleAuth.isAuthenticated(player)) {
player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false); player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false);
return 0; 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); if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) {
int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray()); player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded));
int maxLoginTries = config.main.maxLoginTries; return;
}
if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) { else if(passwordResult == 1) {
player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded)); SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.successfullyAuthenticated));
return 0; return;
} }
else if(passwordResult == 1) { else if(passwordResult == -1) {
SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.successfullyAuthenticated)); player.sendMessage(new LiteralText(config.lang.notRegistered), false);
return 1; return;
} }
else if(passwordResult == -1) { // Kicking the player out
player.sendMessage(new LiteralText(config.lang.notRegistered), false); else if(maxLoginTries == 1) {
return 0; player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword));
} return;
// Kicking the player out }
else if(maxLoginTries == 1) { // Sending wrong pass message
player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword)); player.sendMessage(new LiteralText(config.lang.wrongPassword), false);
return 0; // ++ the login tries
} SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1;
// Sending wrong pass message }).start();
player.sendMessage(new LiteralText(config.lang.wrongPassword), false);
// ++ the login tries
SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1;
return 0; return 0;
} }
} }

View File

@ -44,18 +44,23 @@ public class RegisterCommand {
player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false); player.sendMessage(new LiteralText(config.lang.alreadyAuthenticated), false);
return 0; 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) { if(pass1.length() < config.main.minPasswordChars) {
player.sendMessage(new LiteralText( player.sendMessage(new LiteralText(
String.format(config.lang.minPasswordChars, config.main.minPasswordChars) String.format(config.lang.minPasswordChars, config.main.minPasswordChars)
), false); ), false);
return 0; return;
} }
else if(pass1.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) { else if(pass1.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) {
player.sendMessage(new LiteralText( player.sendMessage(new LiteralText(
String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars) String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars)
), false); ), false);
return 0; return;
} }
String hash = AuthHelper.hashPass(pass1.toCharArray()); String hash = AuthHelper.hashPass(pass1.toCharArray());
// JSON object holding password (may hold some other info in the future) // 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())) { if (SimpleAuth.db.registerUser(convertUuid(player), playerdata.toString())) {
SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.registerSuccess)); SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.registerSuccess));
return 1; return;
} }
player.sendMessage(new LiteralText(config.lang.alreadyRegistered), false); player.sendMessage(new LiteralText(config.lang.alreadyRegistered), false);
return 0; }).start();
}
player.sendMessage(new LiteralText(config.lang.matchPassword), false);
return 0; return 0;
} }
} }

View File

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

View File

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