Checking if user is registered, finalising leveldb.

This commit is contained in:
samo_lego 2020-04-11 19:27:49 +02:00
parent 65a2c5de15
commit 1083665fac
6 changed files with 33 additions and 20 deletions

View File

@ -54,7 +54,7 @@ public class ChangepwCommand {
player.sendMessage(cannotChangePassword); player.sendMessage(cannotChangePassword);
return 0; return 0;
} }
else if (AuthHelper.checkPass(player.getUuidAsString(), oldPass.toCharArray())) { else if (AuthHelper.checkPass(player.getUuidAsString(), oldPass.toCharArray()) == 1) {
SimpleAuth.db.update( SimpleAuth.db.update(
player.getUuidAsString(), player.getUuidAsString(),
AuthHelper.hashPass(newPass.toCharArray()) AuthHelper.hashPass(newPass.toCharArray())

View File

@ -18,6 +18,7 @@ public class LoginCommand {
private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword); private static Text enterPassword = new LiteralText(SimpleAuth.config.lang.enterPassword);
private static Text wrongPassword = new LiteralText(SimpleAuth.config.lang.wrongPassword); private static Text wrongPassword = new LiteralText(SimpleAuth.config.lang.wrongPassword);
private static Text alreadyAuthenticated = new LiteralText(SimpleAuth.config.lang.alreadyAuthenticated); private static Text alreadyAuthenticated = new LiteralText(SimpleAuth.config.lang.alreadyAuthenticated);
private static Text notRegistered = new LiteralText(SimpleAuth.config.lang.notRegistered);
private static Text loginTriesExceeded = new LiteralText(SimpleAuth.config.lang.loginTriesExceeded); private static Text loginTriesExceeded = new LiteralText(SimpleAuth.config.lang.loginTriesExceeded);
private static Text successfullyAuthenticated = new LiteralText(SimpleAuth.config.lang.successfullyAuthenticated); private static Text successfullyAuthenticated = new LiteralText(SimpleAuth.config.lang.successfullyAuthenticated);
private static int maxLoginTries = SimpleAuth.config.main.maxLoginTries; private static int maxLoginTries = SimpleAuth.config.main.maxLoginTries;
@ -48,15 +49,13 @@ public class LoginCommand {
player.networkHandler.disconnect(loginTriesExceeded); player.networkHandler.disconnect(loginTriesExceeded);
return 0; return 0;
} }
else if(SimpleAuth.config.main.enableGlobalPassword) { else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray()) == 1) {
if (AuthHelper.checkPass(null, pass.toCharArray())) {
SimpleAuth.authenticatePlayer(player, successfullyAuthenticated); SimpleAuth.authenticatePlayer(player, successfullyAuthenticated);
return 1; return 1;
} }
} else if(AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray()) == -1) {
else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) { player.sendMessage(notRegistered);
SimpleAuth.authenticatePlayer(player, successfullyAuthenticated); return 0;
return 1;
} }
// Kicking the player out // Kicking the player out
else if(maxLoginTries == 1) { else if(maxLoginTries == 1) {

View File

@ -45,7 +45,7 @@ public class UnregisterCommand {
player.sendMessage(cannotUnregister); player.sendMessage(cannotUnregister);
return 0; return 0;
} }
else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) { else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray()) == 1) {
SimpleAuth.db.delete(player.getUuidAsString()); SimpleAuth.db.delete(player.getUuidAsString());
player.sendMessage(accountDeleted); player.sendMessage(accountDeleted);
return 1; return 1;

View File

@ -3,6 +3,7 @@ package org.samo_lego.simpleauth.database;
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.iq80.leveldb.DB; import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBException;
import org.iq80.leveldb.Options; import org.iq80.leveldb.Options;
import org.samo_lego.simpleauth.SimpleAuth; import org.samo_lego.simpleauth.SimpleAuth;
@ -41,9 +42,8 @@ public class SimpleAuthDatabase {
// When player registers, we insert the data into DB // When player registers, we insert the data into DB
public boolean registerUser(String uuid, String password) { public boolean registerUser(String uuid, String password) {
System.out.println(Arrays.toString(levelDBStore.get(bytes("UUID:" + uuid))));
try { try {
if(levelDBStore.get(bytes("UUID:" + uuid)) == null) { if(!this.isRegistered(uuid)) {
levelDBStore.put(bytes("UUID:" + uuid), bytes("password:" + password)); levelDBStore.put(bytes("UUID:" + uuid), bytes("password:" + password));
return true; return true;
} }
@ -54,6 +54,16 @@ public class SimpleAuthDatabase {
} }
} }
// Checks if user is registered
private boolean isRegistered(String uuid) {
try {
return levelDBStore.get(bytes("UUID:" + uuid)) != null;
} catch (DBException e) {
LOGGER.error("[SimpleAuth] " + e.getMessage());
}
return false;
}
// Deletes row containing the username provided // Deletes row containing the username provided
public void delete(String uuid) { public void delete(String uuid) {
try { try {
@ -74,13 +84,12 @@ public class SimpleAuthDatabase {
// Gets the hashed password from DB // Gets the hashed password from DB
public String getPassword(String uuid){ public String getPassword(String uuid){
String password = null;
try { try {
// Gets password from db and removes "password:" prefix from it if(this.isRegistered(uuid)) // Gets password from db and removes "password:" prefix from it
password = new String(levelDBStore.get(bytes("UUID:" + uuid))).substring(9); return new String(levelDBStore.get(bytes("UUID:" + uuid))).substring(9);
} catch (Error e) { } catch (Error e) {
LOGGER.error("[SimpleAuth] Error getting password: " + e.getMessage()); LOGGER.error("[SimpleAuth] Error getting password: " + e.getMessage());
} }
return password; return "";
} }
} }

View File

@ -90,6 +90,7 @@ public class AuthConfig {
public String accountDeleted = "§aYour account was successfully deleted!"; public String accountDeleted = "§aYour account was successfully deleted!";
public String configurationReloaded = "§aConfiguration file was reloaded successfully."; public String configurationReloaded = "§aConfiguration file was reloaded successfully.";
public String successfulPortalRescue = "§aYou were rescued from nether portal successfully!"; public String successfulPortalRescue = "§aYou were rescued from nether portal successfully!";
public String notRegistered = "§6This account is not yet registered! Type `/register` first";
} }
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
private static final Gson gson = new GsonBuilder() private static final Gson gson = new GsonBuilder()

View File

@ -12,15 +12,17 @@ public class AuthHelper {
// Creating the instance // Creating the instance
private static Argon2 argon2 = Argon2Factory.create(); private static Argon2 argon2 = Argon2Factory.create();
public static boolean checkPass(String uuid, char[] pass) { // Returns 1 if password is correct, 0 if not
// and -1 if user is not registered yet
public static int checkPass(String uuid, char[] pass) {
if(SimpleAuth.config.main.enableGlobalPassword) { if(SimpleAuth.config.main.enableGlobalPassword) {
// We have global password enabled // We have global password enabled
try { try {
return argon2.verify(SimpleAuth.config.main.globalPassword, pass); return argon2.verify(SimpleAuth.config.main.globalPassword, pass) ? 1 : 0;
} }
catch (Error e) { catch (Error e) {
LOGGER.error("[SimpleAuth] Argon2 error: " + e); LOGGER.error("[SimpleAuth] Argon2 error: " + e);
return false; return 0;
} finally { } finally {
// Wipe confidential data // Wipe confidential data
argon2.wipeArray(pass); argon2.wipeArray(pass);
@ -30,11 +32,13 @@ public class AuthHelper {
try { try {
// Hashed password from DB // Hashed password from DB
String hashed = SimpleAuth.db.getPassword(uuid); String hashed = SimpleAuth.db.getPassword(uuid);
if(hashed.equals(""))
return -1; // User is not yet registered
// Verify password // Verify password
return argon2.verify(hashed, pass); return argon2.verify(hashed, pass) ? 1 : 0;
} catch (Error e) { } catch (Error e) {
LOGGER.error("[SimpleAuth] error: " + e); LOGGER.error("[SimpleAuth] error: " + e);
return false; return 0;
} finally { } finally {
// Wipe confidential data // Wipe confidential data
argon2.wipeArray(pass); argon2.wipeArray(pass);