More Javadoc

This commit is contained in:
samo_lego 2020-08-19 23:09:13 +02:00
parent eb2b83f3d7
commit 2de71f924f
4 changed files with 153 additions and 50 deletions

View File

@ -57,11 +57,11 @@ public class SimpleAuth implements DedicatedServerModInitializer {
public static HashMap<String, PlayerCache> deauthenticatedUsers = new HashMap<>(); public static HashMap<String, PlayerCache> deauthenticatedUsers = new HashMap<>();
/** /**
* Checks whether player is authenticated * Checks whether player is authenticated.
* Fake players always count as authenticated * Fake players always count as authenticated.
* *
* @param player player that needs to be checked * @param player player that needs to be checked
* @return false if player is de-authenticated, otherwise false * @return false if player is not authenticated, otherwise true
*/ */
public static boolean isAuthenticated(ServerPlayerEntity player) { public static boolean isAuthenticated(ServerPlayerEntity player) {
String uuid = convertUuid(player); String uuid = convertUuid(player);
@ -160,7 +160,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
/** /**
* Gets the text which tells the player * Gets the text which tells the player
* to login or register, depending on account status * to login or register, depending on account status.
* *
* @param player player who will get the message * @param player player who will get the message
* @return LiteralText with appropriate string (login or register) * @return LiteralText with appropriate string (login or register)
@ -177,7 +177,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
} }
/** /**
* Authenticates player and sends the success message * Authenticates player and sends the success message.
* *
* @param player player that needs to be authenticated * @param player player that needs to be authenticated
* @param msg message to be send to the player * @param msg message to be send to the player
@ -216,7 +216,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
} }
/** /**
* De-authenticates the player * De-authenticates the player.
* *
* @param player player that needs to be de-authenticated * @param player player that needs to be de-authenticated
*/ */
@ -248,11 +248,11 @@ public class SimpleAuth implements DedicatedServerModInitializer {
if(!isAuthenticated(player) && player.networkHandler.getConnection().isOpen()) if(!isAuthenticated(player) && player.networkHandler.getConnection().isOpen())
player.networkHandler.disconnect(new LiteralText(SimpleAuth.config.lang.timeExpired)); player.networkHandler.disconnect(new LiteralText(SimpleAuth.config.lang.timeExpired));
} }
}, SimpleAuth.config.main.delay * 1000); }, SimpleAuth.config.main.kickTime * 1000);
} }
/** /**
* Checks whether player is a fake player (from CarpetMod) * Checks whether player is a fake player (from CarpetMod).
* *
* @param player player that needs to be checked * @param player player that needs to be checked
* @return true if player is fake, otherwise false * @return true if player is fake, otherwise false
@ -263,8 +263,8 @@ public class SimpleAuth implements DedicatedServerModInitializer {
} }
/** /**
* Teleports player to spawn or last location that is recorded * Teleports player to spawn or last location that is recorded.
* Last location means the location before de-authentication * Last location means the location before de-authentication.
* *
* @param player player that needs to be teleported * @param player player that needs to be teleported
* @param toSpawn whether to teleport player to spawn (provided in config) or last recorded position * @param toSpawn whether to teleport player to spawn (provided in config) or last recorded position

View File

@ -26,41 +26,78 @@ import java.nio.charset.StandardCharsets;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError; import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
public class AuthConfig { public class AuthConfig {
private static final Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
// If player is not authenticated, following conditions apply // If player is not authenticated, following conditions apply
public static class MainConfig { public static class MainConfig {
// Allows "right-clicking" on an entity (e.g. clicking on villagers) /**
* Allows "right-clicking" on an entity (e.g. clicking on villagers).
*/
public boolean allowEntityInteract = false; public boolean allowEntityInteract = false;
// Maximum login tries before kicking the player from server /**
// Set to -1 to allow unlimited, not recommended however * Maximum login tries before kicking the player from server.
* Set to -1 to allow unlimited, not recommended however.
*/
public int maxLoginTries = 1; public int maxLoginTries = 1;
// Time after which player will be kicked if not authenticated - in seconds /**
public int delay = 60; * Time after which player will be kicked if not authenticated - in seconds
// Disables registering and forces logging in with global password */
// Visit https://github.com/samolego/SimpleAuth/wiki/Locking-server-with-global-password for more info public int kickTime = 60;
/**
* Disables registering and forces logging in with global password.
* @see <a href="https://github.com/samolego/SimpleAuth/wiki/Locking-server-with-global-password">wiki</a>
*/
public boolean enableGlobalPassword = false; public boolean enableGlobalPassword = false;
/**
* Hashed global password.
*/
public String globalPassword; public String globalPassword;
// Tries to rescue players if they are stuck inside a portal on logging in /**
// Visit https://github.com/samolego/SimpleAuth/wiki/Portal-Rescue for more info * Tries to rescue players if they are stuck inside a portal on logging in.
* @see <a href="https://github.com/samolego/SimpleAuth/wiki/Portal-Rescue">wiki</a>
*/
public boolean tryPortalRescue = true; public boolean tryPortalRescue = true;
// Minimum and maximum length of password. Set -1 to disable max chars /**
* Minimum length of password.
*/
public int minPasswordChars = 4; public int minPasswordChars = 4;
/**
* Maximum length of password.
* Set -1 to disable.
*/
public int maxPasswordChars = -1; public int maxPasswordChars = -1;
// Regex of valid playername characters. You probably don't want to change this. /**
// Visit https://github.com/samolego/SimpleAuth/wiki/Username-Restriction for more info * Regex of valid playername characters. You probably don't want to change this.
* @see <a href="https://github.com/samolego/SimpleAuth/wiki/Username-Restriction">wiki</a>
*/
public String usernameRegex = "^[a-zA-Z0-9_]{3,16}$"; public String usernameRegex = "^[a-zA-Z0-9_]{3,16}$";
// How long to keep session (auto-logging in the player), in seconds /**
// Set to -1 to disable * How long to keep session (auto-logging in the player), in seconds
// Visit https://github.com/samolego/SimpleAuth/wiki/Sessions for more info * Set to -1 to disable
* @see <a href="https://github.com/samolego/SimpleAuth/wiki/Sessions">wiki</a>
*/
public int sessionTimeoutTime = 60; public int sessionTimeoutTime = 60;
// Should deauthenticated players fall if the login mid-air? /**
* Should deauthenticated players fall if the login mid-air?
*/
public boolean allowFalling = false; public boolean allowFalling = false;
// Whether to tp player to spawn when joining (to hide coordinates) /**
* Whether to tp player to spawn when joining (to hide coordinates)
*/
public boolean spawnOnJoin = false; public boolean spawnOnJoin = false;
// Data for spawn (where deauthenticated players are teleported) /**
* Data for spawn (where deauthenticated players are teleported).
* @see <a href="https://github.com/samolego/SimpleAuth/wiki/Coordinate-Hiding">wiki</a>
*/
public static class WorldSpawn { public static class WorldSpawn {
/**
* Dimension id, e.g. "minecraft:overworld"
*/
public String dimension; public String dimension;
public double x; public double x;
public double y; public double y;
@ -98,40 +135,69 @@ public class AuthConfig {
public String corruptedPlayerData = "§cYour data is probably corrupted. Please contact admin."; public String corruptedPlayerData = "§cYour data is probably corrupted. Please contact admin.";
} }
public static class ExperimentalConfig { public static class ExperimentalConfig {
// Prevents player being kicked because another player with the same name has joined the server /**
* Prevents player being kicked because another player with the same name has joined the server.
*/
public boolean disableAnotherLocationKick = true; public boolean disableAnotherLocationKick = true;
// If player should be invulnerable before authentication /**
* If player should be invulnerable before authentication.
*/
public boolean playerInvulnerable = true; public boolean playerInvulnerable = true;
// If player should be invisible to mobs before authentication /**
* If player should be invisible to mobs before authentication.
*/
public boolean playerInvisible = true; public boolean playerInvisible = true;
// Allows chat (but not commands, except for /login and /register) /**
* Allows chat (but not commands, except for /login and /register).
*/
public boolean allowChat = false; public boolean allowChat = false;
// Allows player movement /**
* Allows player movement.
*/
public boolean allowMovement = false; public boolean allowMovement = false;
// Allows block "use" - right clicking (e.g. opening a chest) /**
* Allows block "use" - right clicking (e.g. opening a chest).
*/
public boolean allowBlockUse = false; public boolean allowBlockUse = false;
// Allows mining || punching blocks /**
* Allows mining or punching blocks.
*/
public boolean allowBlockPunch = false; public boolean allowBlockPunch = false;
// Allows dropping items from inventory /**
* Allows dropping items from inventory.
*/
public boolean allowItemDrop = false; public boolean allowItemDrop = false;
// Allows moving item through inventory /**
* Allows moving item through inventory.
*/
public boolean allowItemMoving = false; public boolean allowItemMoving = false;
// Allows item "use" - right click function (e.g. using a bow) /**
* Allows item "use" - right click function (e.g. using a bow).
*/
public boolean allowItemUse = false; public boolean allowItemUse = false;
// Allows attacking mobs /**
* Allows attacking mobs.
*/
public boolean allowEntityPunch = false; public boolean allowEntityPunch = false;
// Whether to use BCrypt instead of Argon2 (GLIBC_2.25 error) /**
* Whether to use BCrypt instead of Argon2 (GLIBC_2.25 error).
* @see <a href="https://github.com/samolego/SimpleAuth/wiki/GLIBC-problems">wiki</a>
*/
public boolean useBCryptLibrary = false; public boolean useBCryptLibrary = false;
} }
private static final Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
public MainConfig main = new MainConfig(); public MainConfig main = new MainConfig();
public MainConfig.WorldSpawn worldSpawn = new MainConfig.WorldSpawn(); public MainConfig.WorldSpawn worldSpawn = new MainConfig.WorldSpawn();
public LangConfig lang = new LangConfig(); public LangConfig lang = new LangConfig();
public ExperimentalConfig experimental = new ExperimentalConfig(); public ExperimentalConfig experimental = new ExperimentalConfig();
/**
* Loads SimpleAuth's config file.
*
* @param file file to load config from
* @return AuthConfig config object
*/
public static AuthConfig load(File file) { public static AuthConfig load(File file) {
AuthConfig config; AuthConfig config;
if (file.exists()) { if (file.exists()) {
@ -150,6 +216,12 @@ public class AuthConfig {
return config; return config;
} }
/**
* Saves the config to the given file.
*
* @param file file to save config to
*/
public void save(File file) { public void save(File file) {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) { try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
gson.toJson(this, writer); gson.toJson(this, writer);

View File

@ -70,6 +70,8 @@ public class PlayerCache {
if(passwordElement instanceof JsonNull) { if(passwordElement instanceof JsonNull) {
if(player != null) { if(player != null) {
player.sendMessage(new LiteralText(config.lang.corruptedPlayerData), false); player.sendMessage(new LiteralText(config.lang.corruptedPlayerData), false);
this.isRegistered = false;
this.password = "";
} }
// This shouldn't have happened, data seems to be corrupted // This shouldn't have happened, data seems to be corrupted

View File

@ -20,7 +20,9 @@ public class SimpleAuthDatabase {
return this.levelDBStore; return this.levelDBStore;
} }
// Connects to the DB /**
* Connects to the DB.
*/
public void openConnection() { public void openConnection() {
try { try {
@ -30,7 +32,10 @@ public class SimpleAuthDatabase {
logError(e.getMessage()); logError(e.getMessage());
} }
} }
// Closing connection
/**
* Closes database connection.
*/
public void close() { public void close() {
if (levelDBStore != null) { if (levelDBStore != null) {
try { try {
@ -42,13 +47,23 @@ public class SimpleAuthDatabase {
} }
} }
// Tells whether db connection is closed /**
* Tells whether DB connection is closed.
*
* @return false if connection is open, otherwise false
*/
public boolean isClosed() { public boolean isClosed() {
return levelDBStore == null; return levelDBStore == null;
} }
// When player registers, we insert the data into DB /**
* Inserts the data for the player.
*
* @param uuid uuid of the player to insert data for
* @param data data to put inside database
* @return true if operation was successful, otherwise false
*/
public boolean registerUser(String uuid, String data) { public boolean registerUser(String uuid, String data) {
try { try {
if(!this.isUserRegistered(uuid)) { if(!this.isUserRegistered(uuid)) {
@ -62,7 +77,12 @@ public class SimpleAuthDatabase {
} }
} }
// Checks if user is registered /**
* Checks if player is registered.
*
* @param uuid player's uuid
* @return true if registered, otherwise false
*/
public boolean isUserRegistered(String uuid) { public boolean isUserRegistered(String uuid) {
try { try {
return levelDBStore.get(bytes("UUID:" + uuid)) != null; return levelDBStore.get(bytes("UUID:" + uuid)) != null;
@ -72,7 +92,11 @@ public class SimpleAuthDatabase {
return false; return false;
} }
// Deletes row containing the username provided /**
* Deletes data for the provided uuid.
*
* @param uuid uuid of player to delete data for
*/
public void deleteUserData(String uuid) { public void deleteUserData(String uuid) {
try { try {
levelDBStore.delete(bytes("UUID:" + uuid)); levelDBStore.delete(bytes("UUID:" + uuid));
@ -81,7 +105,12 @@ public class SimpleAuthDatabase {
} }
} }
// Updates the password of the user /**
* Updates player's data.
*
* @param uuid uuid of the player to update data for
* @param data data to put inside database
*/
public void updateUserData(String uuid, String data) { public void updateUserData(String uuid, String data) {
try { try {
levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data)); levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data));