Auth command cleanup, clearer method names at db class.
This commit is contained in:
parent
30e261c767
commit
9ec2e4478c
|
@ -39,6 +39,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
|
||||||
public static boolean isAuthenticated(ServerPlayerEntity player) {
|
public static boolean isAuthenticated(ServerPlayerEntity player) {
|
||||||
return !deauthenticatedUsers.containsKey(player);
|
return !deauthenticatedUsers.containsKey(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getting game directory
|
// Getting game directory
|
||||||
public static final File gameDirectory = FabricLoader.getInstance().getGameDirectory();
|
public static final File gameDirectory = FabricLoader.getInstance().getGameDirectory();
|
||||||
// Mod config
|
// Mod config
|
||||||
|
@ -85,10 +86,13 @@ public class SimpleAuth implements DedicatedServerModInitializer {
|
||||||
UseEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onUseEntity(player));
|
UseEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onUseEntity(player));
|
||||||
ServerStopCallback.EVENT.register(minecraftServer -> SimpleAuth.onStopServer());
|
ServerStopCallback.EVENT.register(minecraftServer -> SimpleAuth.onStopServer());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void onStopServer() {
|
private static void onStopServer() {
|
||||||
LOGGER.info("[SimpleAuth] Shutting down SimpleAuth.");
|
LOGGER.info("[SimpleAuth] Shutting down SimpleAuth.");
|
||||||
db.close();
|
db.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Authenticates player and sends the message
|
||||||
public static void authenticatePlayer(ServerPlayerEntity player, Text msg) {
|
public static void authenticatePlayer(ServerPlayerEntity player, Text msg) {
|
||||||
deauthenticatedUsers.remove(player);
|
deauthenticatedUsers.remove(player);
|
||||||
// Player no longer needs to be invisible and invulnerable
|
// 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);
|
return new LiteralText(SimpleAuth.config.lang.notAuthenticated);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// De-authenticates player
|
||||||
public static void deauthenticatePlayer(ServerPlayerEntity player) {
|
public static void deauthenticatePlayer(ServerPlayerEntity player) {
|
||||||
// Marking player as not authenticated, (re)setting login tries to zero
|
// Marking player as not authenticated, (re)setting login tries to zero
|
||||||
SimpleAuth.deauthenticatedUsers.put(player, 0);
|
SimpleAuth.deauthenticatedUsers.put(player, 0);
|
||||||
|
|
|
@ -41,54 +41,52 @@ public class AuthCommand {
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.then(literal("update")
|
.then(literal("remove")
|
||||||
.then(literal("byUuid")
|
.then(argument("uuid", word())
|
||||||
.then(argument("uuid", word())
|
.executes( ctx -> removeAccount(
|
||||||
.then(argument("password", word())
|
ctx.getSource(),
|
||||||
.executes( ctx -> updatePass(
|
getString(ctx, "uuid")
|
||||||
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(literal("register")
|
||||||
.then(literal("byUuid")
|
.then(argument("uuid", word())
|
||||||
.then(argument("uuid", word())
|
.then(argument("password", word())
|
||||||
.executes( ctx -> removeAccount(
|
.executes( ctx -> registerUser(
|
||||||
ctx.getSource(),
|
ctx.getSource(),
|
||||||
getString(ctx, "uuid"),
|
getString(ctx, "uuid"),
|
||||||
null
|
getString(ctx, "password")
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.then(literal("byUsername")
|
)
|
||||||
.then(argument("username", word())
|
.then(literal("update")
|
||||||
.executes( ctx -> removeAccount(
|
.then(argument("uuid", word())
|
||||||
|
.then(argument("password", word())
|
||||||
|
.executes( ctx -> updatePass(
|
||||||
ctx.getSource(),
|
ctx.getSource(),
|
||||||
null,
|
getString(ctx, "uuid"),
|
||||||
getString(ctx, "username")
|
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) {
|
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();
|
||||||
|
@ -104,26 +102,10 @@ public class AuthCommand {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deleting (unregistering) user's account
|
||||||
// Method called for checking the password
|
private static int removeAccount(ServerCommandSource source, String uuid) {
|
||||||
private static int updatePass(ServerCommandSource source, String uuid, String username, String pass) {
|
|
||||||
// Getting the player who send the command
|
|
||||||
Entity sender = source.getEntity();
|
Entity sender = source.getEntity();
|
||||||
|
SimpleAuth.db.deleteUserData(uuid);
|
||||||
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);
|
|
||||||
|
|
||||||
// TODO -> Kick player that was unregistered?
|
// TODO -> Kick player that was unregistered?
|
||||||
|
|
||||||
|
@ -133,14 +115,38 @@ public class AuthCommand {
|
||||||
LOGGER.info(SimpleAuth.config.lang.userdataDeleted);
|
LOGGER.info(SimpleAuth.config.lang.userdataDeleted);
|
||||||
return 1; // Success
|
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)
|
if(sender != null)
|
||||||
sender.sendMessage(configurationReloaded);
|
sender.sendMessage(userdataUpdated);
|
||||||
else
|
else
|
||||||
LOGGER.info(SimpleAuth.config.lang.configurationReloaded);
|
LOGGER.info(SimpleAuth.config.lang.userdataUpdated);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ 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) {
|
||||||
try {
|
try {
|
||||||
if(!this.isRegistered(uuid)) {
|
if(!this.isUserRegistered(uuid)) {
|
||||||
levelDBStore.put(bytes("UUID:" + uuid), bytes("password:" + password));
|
levelDBStore.put(bytes("UUID:" + uuid), bytes("password:" + password));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ public class SimpleAuthDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if user is registered
|
// Checks if user is registered
|
||||||
private boolean isRegistered(String uuid) {
|
private boolean isUserRegistered(String uuid) {
|
||||||
try {
|
try {
|
||||||
return levelDBStore.get(bytes("UUID:" + uuid)) != null;
|
return levelDBStore.get(bytes("UUID:" + uuid)) != null;
|
||||||
} catch (DBException e) {
|
} catch (DBException e) {
|
||||||
|
@ -64,7 +64,7 @@ public class SimpleAuthDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes row containing the username provided
|
// Deletes row containing the username provided
|
||||||
public void delete(String uuid) {
|
public void deleteUserData(String uuid) {
|
||||||
try {
|
try {
|
||||||
levelDBStore.delete(bytes("UUID:" + uuid));
|
levelDBStore.delete(bytes("UUID:" + uuid));
|
||||||
} catch (Error e) {
|
} catch (Error e) {
|
||||||
|
@ -73,7 +73,7 @@ public class SimpleAuthDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates the password of the user
|
// Updates the password of the user
|
||||||
public void update(String uuid, String password) {
|
public void updateUserData(String uuid, String password) {
|
||||||
try {
|
try {
|
||||||
levelDBStore.put(bytes("UUID:" + uuid),bytes("password:" + password));
|
levelDBStore.put(bytes("UUID:" + uuid),bytes("password:" + password));
|
||||||
} catch (Error e) {
|
} catch (Error e) {
|
||||||
|
@ -84,7 +84,7 @@ 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){
|
||||||
try {
|
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);
|
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());
|
||||||
|
|
|
@ -86,6 +86,7 @@ public class AuthConfig {
|
||||||
public String successfullyAuthenticated = "§aYou are now authenticated.";
|
public String successfullyAuthenticated = "§aYou are now authenticated.";
|
||||||
public String successfulLogout = "§aLogged out successfully.";
|
public String successfulLogout = "§aLogged out successfully.";
|
||||||
public String timeExpired = "§cTime for authentication has expired.";
|
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 alreadyRegistered = "§6This account name is already registered!";
|
||||||
public String registerSuccess = "§aYou are now authenticated.";
|
public String registerSuccess = "§aYou are now authenticated.";
|
||||||
public String userdataDeleted = "§aUserdata deleted.";
|
public String userdataDeleted = "§aUserdata deleted.";
|
||||||
|
@ -95,7 +96,6 @@ public class AuthConfig {
|
||||||
public String successfulPortalRescue = "§aYou were rescued from nether portal successfully!";
|
public String successfulPortalRescue = "§aYou were rescued from nether portal successfully!";
|
||||||
public String maxPasswordChars = "§6Password can be at most %d characters long!";
|
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 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 Logger LOGGER = LogManager.getLogger();
|
||||||
private static final Gson gson = new GsonBuilder()
|
private static final Gson gson = new GsonBuilder()
|
||||||
|
|
Loading…
Reference in New Issue