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,6 +71,8 @@ public class AccountCommand {
return 0; return 0;
} }
// New thread to avoid lag spikes
new Thread(() -> {
if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) { if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) {
SimpleAuth.db.deleteUserData(convertUuid(player)); SimpleAuth.db.deleteUserData(convertUuid(player));
player.sendMessage( player.sendMessage(
@ -78,12 +80,13 @@ public class AccountCommand {
false false
); );
SimpleAuth.deauthenticatePlayer(player); SimpleAuth.deauthenticatePlayer(player);
return 1; return;
} }
player.sendMessage( player.sendMessage(
new LiteralText(config.lang.wrongPassword), new LiteralText(config.lang.wrongPassword),
false false
); );
}).start();
return 0; return 0;
} }
@ -99,18 +102,20 @@ 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(() -> {
if (AuthHelper.checkPass(convertUuid(player), oldPass.toCharArray()) == 1) {
if (newPass.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(newPass.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) { else if (newPass.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;
} }
// JSON object holding password (may hold some other info in the future) // JSON object holding password (may hold some other info in the future)
JsonObject playerdata = new JsonObject(); JsonObject playerdata = new JsonObject();
@ -122,12 +127,13 @@ public class AccountCommand {
new LiteralText(config.lang.passwordUpdated), new LiteralText(config.lang.passwordUpdated),
false false
); );
return 1;
} }
else
player.sendMessage( player.sendMessage(
new LiteralText(config.lang.wrongPassword), new LiteralText(config.lang.wrongPassword),
false false
); );
}).start();
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();
// New thread to avoid lag spikes
new Thread(() -> {
// Writing the global pass to config // Writing the global pass to config
config.main.globalPassword = AuthHelper.hashPass(pass.toCharArray()); config.main.globalPassword = AuthHelper.hashPass(pass.toCharArray());
config.main.enableGlobalPassword = true; config.main.enableGlobalPassword = true;
config.save(new File("./mods/SimpleAuth/config.json")); 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();
new Thread(() -> {
db.deleteUserData(uuid); db.deleteUserData(uuid);
SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, null)); 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();
new Thread(() -> {
// JSON object holding password (may hold some other info in the future) // JSON object holding password (may hold some other info in the future)
JsonObject playerdata = new JsonObject(); JsonObject playerdata = new JsonObject();
String hash = AuthHelper.hashPass(password.toCharArray()); String hash = AuthHelper.hashPass(password.toCharArray());
playerdata.addProperty("password", hash); 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();
new Thread(() -> {
// JSON object holding password (may hold some other info in the future) // JSON object holding password (may hold some other info in the future)
JsonObject playerdata = new JsonObject(); JsonObject playerdata = new JsonObject();
String hash = AuthHelper.hashPass(password.toCharArray()); String hash = AuthHelper.hashPass(password.toCharArray());
playerdata.addProperty("password", hash); 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
String uuid = convertUuid(player); new Thread(() -> {
int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray());
int maxLoginTries = config.main.maxLoginTries; int maxLoginTries = config.main.maxLoginTries;
int passwordResult = AuthHelper.checkPass(uuid, pass.toCharArray());
if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) { if(SimpleAuth.deauthenticatedUsers.get(uuid).loginTries >= maxLoginTries && maxLoginTries != -1) {
player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded)); player.networkHandler.disconnect(new LiteralText(config.lang.loginTriesExceeded));
return 0; return;
} }
else if(passwordResult == 1) { else if(passwordResult == 1) {
SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.successfullyAuthenticated)); SimpleAuth.authenticatePlayer(player, new LiteralText(config.lang.successfullyAuthenticated));
return 1; return;
} }
else if(passwordResult == -1) { else if(passwordResult == -1) {
player.sendMessage(new LiteralText(config.lang.notRegistered), false); player.sendMessage(new LiteralText(config.lang.notRegistered), false);
return 0; return;
} }
// Kicking the player out // Kicking the player out
else if(maxLoginTries == 1) { else if(maxLoginTries == 1) {
player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword)); player.networkHandler.disconnect(new LiteralText(config.lang.wrongPassword));
return 0; return;
} }
// Sending wrong pass message // Sending wrong pass message
player.sendMessage(new LiteralText(config.lang.wrongPassword), false); player.sendMessage(new LiteralText(config.lang.wrongPassword), false);
// ++ the login tries // ++ the login tries
SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1; SimpleAuth.deauthenticatedUsers.get(uuid).loginTries += 1;
}).start();
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