Auth command cleanup, clearer method names at db class.

This commit is contained in:
samo_lego 2020-04-12 16:55:50 +02:00
parent 30e261c767
commit 9ec2e4478c
4 changed files with 75 additions and 64 deletions

View File

@ -39,6 +39,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
public static boolean isAuthenticated(ServerPlayerEntity player) {
return !deauthenticatedUsers.containsKey(player);
}
// Getting game directory
public static final File gameDirectory = FabricLoader.getInstance().getGameDirectory();
// Mod config
@ -85,10 +86,13 @@ public class SimpleAuth implements DedicatedServerModInitializer {
UseEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onUseEntity(player));
ServerStopCallback.EVENT.register(minecraftServer -> SimpleAuth.onStopServer());
}
private static void onStopServer() {
LOGGER.info("[SimpleAuth] Shutting down SimpleAuth.");
db.close();
}
// Authenticates player and sends the message
public static void authenticatePlayer(ServerPlayerEntity player, Text msg) {
deauthenticatedUsers.remove(player);
// Player no longer needs to be invisible and invulnerable
@ -105,6 +109,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
return new LiteralText(SimpleAuth.config.lang.notAuthenticated);
}
// De-authenticates player
public static void deauthenticatePlayer(ServerPlayerEntity player) {
// Marking player as not authenticated, (re)setting login tries to zero
SimpleAuth.deauthenticatedUsers.put(player, 0);

View File

@ -41,54 +41,52 @@ public class AuthCommand {
))
)
)
.then(literal("update")
.then(literal("byUuid")
.then(argument("uuid", word())
.then(argument("password", word())
.executes( ctx -> updatePass(
ctx.getSource(),
getString(ctx, "uuid"),
null,
getString(ctx, "password")
))
)
)
)
.then(literal("byUsername")
.then(argument("username", word())
.then(argument("password", word())
.executes( ctx -> updatePass(
ctx.getSource(),
null,
getString(ctx, "username"),
getString(ctx, "password")
))
)
)
.then(literal("remove")
.then(argument("uuid", word())
.executes( ctx -> removeAccount(
ctx.getSource(),
getString(ctx, "uuid")
))
)
)
.then(literal("remove")
.then(literal("byUuid")
.then(argument("uuid", word())
.executes( ctx -> removeAccount(
.then(literal("register")
.then(argument("uuid", word())
.then(argument("password", word())
.executes( ctx -> registerUser(
ctx.getSource(),
getString(ctx, "uuid"),
null
getString(ctx, "password")
))
)
)
.then(literal("byUsername")
.then(argument("username", word())
.executes( ctx -> removeAccount(
)
.then(literal("update")
.then(argument("uuid", word())
.then(argument("password", word())
.executes( ctx -> updatePass(
ctx.getSource(),
null,
getString(ctx, "username")
getString(ctx, "uuid"),
getString(ctx, "password")
))
)
)
)
);
}
// Reloading the config
private static int reloadConfig(ServerCommandSource source) {
Entity sender = source.getEntity();
SimpleAuth.config = AuthConfig.load(new File("./mods/SimpleAuth/config.json"));
if(sender != null)
sender.sendMessage(configurationReloaded);
else
LOGGER.info(SimpleAuth.config.lang.configurationReloaded);
return 1;
}
// Setting global password
private static int setGlobalPassword(ServerCommandSource source, String pass) {
// Getting the player who send the command
Entity sender = source.getEntity();
@ -104,26 +102,10 @@ public class AuthCommand {
return 1;
}
// Method called for checking the password
private static int updatePass(ServerCommandSource source, String uuid, String username, String pass) {
// Getting the player who send the command
// Deleting (unregistering) user's account
private static int removeAccount(ServerCommandSource source, String uuid) {
Entity sender = source.getEntity();
SimpleAuth.db.update(
uuid,
AuthHelper.hashPass(pass.toCharArray())
);
if(sender != null)
sender.sendMessage(userdataUpdated);
else
LOGGER.info(SimpleAuth.config.lang.userdataUpdated);
// TODO -> Kick player whose name was changed?
return 1;
}
private static int removeAccount(ServerCommandSource source, String uuid, String username) {
Entity sender = source.getEntity();
SimpleAuth.db.delete(uuid);
SimpleAuth.db.deleteUserData(uuid);
// TODO -> Kick player that was unregistered?
@ -133,14 +115,38 @@ public class AuthCommand {
LOGGER.info(SimpleAuth.config.lang.userdataDeleted);
return 1; // Success
}
private static int reloadConfig(ServerCommandSource source) {
Entity sender = source.getEntity();
SimpleAuth.config = AuthConfig.load(new File("./mods/SimpleAuth/config.json"));
// Creating account for user
private static int registerUser(ServerCommandSource source, String uuid, String password) {
// Getting the player who send the command
Entity sender = source.getEntity();
if(SimpleAuth.db.registerUser(
uuid,
AuthHelper.hashPass(password.toCharArray())
)) {
if(sender != null)
sender.sendMessage(userdataUpdated);
else
LOGGER.info(SimpleAuth.config.lang.userdataUpdated);
return 1;
}
return 0;
}
// Force-updating the user's password
private static int updatePass(ServerCommandSource source, String uuid, String password) {
// Getting the player who send the command
Entity sender = source.getEntity();
SimpleAuth.db.updateUserData(
uuid,
AuthHelper.hashPass(password.toCharArray())
);
if(sender != null)
sender.sendMessage(configurationReloaded);
sender.sendMessage(userdataUpdated);
else
LOGGER.info(SimpleAuth.config.lang.configurationReloaded);
LOGGER.info(SimpleAuth.config.lang.userdataUpdated);
return 1;
}
}

View File

@ -42,7 +42,7 @@ public class SimpleAuthDatabase {
// When player registers, we insert the data into DB
public boolean registerUser(String uuid, String password) {
try {
if(!this.isRegistered(uuid)) {
if(!this.isUserRegistered(uuid)) {
levelDBStore.put(bytes("UUID:" + uuid), bytes("password:" + password));
return true;
}
@ -54,7 +54,7 @@ public class SimpleAuthDatabase {
}
// Checks if user is registered
private boolean isRegistered(String uuid) {
private boolean isUserRegistered(String uuid) {
try {
return levelDBStore.get(bytes("UUID:" + uuid)) != null;
} catch (DBException e) {
@ -64,7 +64,7 @@ public class SimpleAuthDatabase {
}
// Deletes row containing the username provided
public void delete(String uuid) {
public void deleteUserData(String uuid) {
try {
levelDBStore.delete(bytes("UUID:" + uuid));
} catch (Error e) {
@ -73,7 +73,7 @@ public class SimpleAuthDatabase {
}
// Updates the password of the user
public void update(String uuid, String password) {
public void updateUserData(String uuid, String password) {
try {
levelDBStore.put(bytes("UUID:" + uuid),bytes("password:" + password));
} catch (Error e) {
@ -84,7 +84,7 @@ public class SimpleAuthDatabase {
// Gets the hashed password from DB
public String getPassword(String uuid){
try {
if(this.isRegistered(uuid)) // Gets password from db and removes "password:" prefix from it
if(this.isUserRegistered(uuid)) // Gets password from db and removes "password:" prefix from it
return new String(levelDBStore.get(bytes("UUID:" + uuid))).substring(9);
} catch (Error e) {
LOGGER.error("[SimpleAuth] Error getting password: " + e.getMessage());

View File

@ -86,6 +86,7 @@ public class AuthConfig {
public String successfullyAuthenticated = "§aYou are now authenticated.";
public String successfulLogout = "§aLogged out successfully.";
public String timeExpired = "§cTime for authentication has expired.";
public String notRegistered = "§6This account is not yet registered! Type `/register` first";
public String alreadyRegistered = "§6This account name is already registered!";
public String registerSuccess = "§aYou are now authenticated.";
public String userdataDeleted = "§aUserdata deleted.";
@ -95,7 +96,6 @@ public class AuthConfig {
public String successfulPortalRescue = "§aYou were rescued from nether portal successfully!";
public String maxPasswordChars = "§6Password can be at most %d characters long!";
public String minPasswordChars = "§6Password needs to be at least %d characters long!";
public String notRegistered = "§6This account is not yet registered! Type `/register` first";
}
private static final Logger LOGGER = LogManager.getLogger();
private static final Gson gson = new GsonBuilder()