From e18be3024adbffc7ab9e6c497ee643f4b31b4ee0 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 17 Jan 2016 12:33:29 +0100 Subject: [PATCH] #347 Use NewSetting properties for change password command --- .../authme/ChangePasswordAdminCommand.java | 11 +++++----- .../authme/RegisterAdminCommand.java | 5 +++-- .../changepassword/ChangePasswordCommand.java | 12 ++++++----- .../fr/xephi/authme/settings/Settings.java | 3 ++- .../settings/custom/RestrictionSettings.java | 2 +- .../settings/custom/SecuritySettings.java | 4 ++++ src/main/resources/config.yml | 4 +++- .../ChangePasswordCommandTest.java | 20 ++++++++++--------- 8 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/ChangePasswordAdminCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/ChangePasswordAdminCommand.java index ec2b7d98..a1b27f16 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/ChangePasswordAdminCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/ChangePasswordAdminCommand.java @@ -8,7 +8,8 @@ import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.security.crypts.HashedPassword; -import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.custom.RestrictionSettings; +import fr.xephi.authme.settings.custom.SecuritySettings; import org.bukkit.command.CommandSender; import java.util.List; @@ -27,7 +28,7 @@ public class ChangePasswordAdminCommand implements ExecutableCommand { // Validate the password String playerPassLowerCase = playerPass.toLowerCase(); - if (!playerPassLowerCase.matches(Settings.getPassRegex)) { + if (!playerPassLowerCase.matches(commandService.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX))) { commandService.send(sender, MessageKey.PASSWORD_MATCH_ERROR); return; } @@ -35,12 +36,12 @@ public class ChangePasswordAdminCommand implements ExecutableCommand { commandService.send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR); return; } - if (playerPassLowerCase.length() < Settings.getPasswordMinLen - || playerPassLowerCase.length() > Settings.passwordMaxLength) { + if (playerPassLowerCase.length() < commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH) + || playerPassLowerCase.length() > commandService.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)) { commandService.send(sender, MessageKey.INVALID_PASSWORD_LENGTH); return; } - if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(playerPassLowerCase)) { + if (commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS).contains(playerPassLowerCase)) { commandService.send(sender, MessageKey.PASSWORD_UNSAFE_ERROR); return; } diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/RegisterAdminCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/RegisterAdminCommand.java index 46916b7b..107f8c7a 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/RegisterAdminCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/RegisterAdminCommand.java @@ -7,6 +7,7 @@ import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.custom.SecuritySettings; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -35,8 +36,8 @@ public class RegisterAdminCommand implements ExecutableCommand { commandService.send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR); return; } - if (playerPassLowerCase.length() < Settings.getPasswordMinLen - || playerPassLowerCase.length() > Settings.passwordMaxLength) { + if (playerPassLowerCase.length() < commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH) + || playerPassLowerCase.length() > commandService.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)) { commandService.send(sender, MessageKey.INVALID_PASSWORD_LENGTH); return; } diff --git a/src/main/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommand.java b/src/main/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommand.java index 0d1cdc48..a3b7f445 100644 --- a/src/main/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommand.java @@ -5,7 +5,8 @@ import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.PlayerCommand; import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.custom.RestrictionSettings; +import fr.xephi.authme.settings.custom.SecuritySettings; import fr.xephi.authme.task.ChangePasswordTask; import fr.xephi.authme.util.Wrapper; import org.bukkit.entity.Player; @@ -32,7 +33,7 @@ public class ChangePasswordCommand extends PlayerCommand { // Make sure the password is allowed String playerPassLowerCase = newPassword.toLowerCase(); - if (!playerPassLowerCase.matches(Settings.getPassRegex)) { + if (!playerPassLowerCase.matches(commandService.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX))) { commandService.send(player, MessageKey.PASSWORD_MATCH_ERROR); return; } @@ -40,17 +41,18 @@ public class ChangePasswordCommand extends PlayerCommand { commandService.send(player, MessageKey.PASSWORD_IS_USERNAME_ERROR); return; } - if (playerPassLowerCase.length() < Settings.getPasswordMinLen - || playerPassLowerCase.length() > Settings.passwordMaxLength) { + if (playerPassLowerCase.length() < commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH) + || playerPassLowerCase.length() > commandService.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)) { commandService.send(player, MessageKey.INVALID_PASSWORD_LENGTH); return; } - if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(playerPassLowerCase)) { + if (commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS).contains(playerPassLowerCase)) { commandService.send(player, MessageKey.PASSWORD_UNSAFE_ERROR); return; } AuthMe plugin = AuthMe.getInstance(); + // TODO ljacqu 20160117: Call async task via Management commandService.runTaskAsynchronously(new ChangePasswordTask(plugin, player, oldPassword, newPassword)); } } diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 4adfe15b..ebad00fa 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -7,6 +7,7 @@ import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource.DataSourceType; import fr.xephi.authme.security.HashAlgorithm; +import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Wrapper; import org.bukkit.configuration.file.YamlConfiguration; @@ -311,7 +312,7 @@ public final class Settings { try { return Files.toString(EMAIL_FILE, Charsets.UTF_8); } catch (IOException e) { - ConsoleLogger.showError(e.getMessage()); + ConsoleLogger.showError("Error loading email text: " + StringUtils.formatException(e)); ConsoleLogger.writeStackTrace(e); return ""; } diff --git a/src/main/java/fr/xephi/authme/settings/custom/RestrictionSettings.java b/src/main/java/fr/xephi/authme/settings/custom/RestrictionSettings.java index 651c7833..32b68586 100644 --- a/src/main/java/fr/xephi/authme/settings/custom/RestrictionSettings.java +++ b/src/main/java/fr/xephi/authme/settings/custom/RestrictionSettings.java @@ -158,7 +158,7 @@ public class RestrictionSettings implements SettingsClass { newProperty("settings.restrictions.noTeleport", false); @Comment("Regex syntax for allowed chars in passwords") - public static final Property ALLOWED_PASSWORD_CHARS = + public static final Property ALLOWED_PASSWORD_REGEX = newProperty("settings.restrictions.allowedPasswordCharacters", "[\\x21-\\x7E]*"); @Comment("Force survival gamemode when player joins?") diff --git a/src/main/java/fr/xephi/authme/settings/custom/SecuritySettings.java b/src/main/java/fr/xephi/authme/settings/custom/SecuritySettings.java index dcb44b73..94e33258 100644 --- a/src/main/java/fr/xephi/authme/settings/custom/SecuritySettings.java +++ b/src/main/java/fr/xephi/authme/settings/custom/SecuritySettings.java @@ -51,6 +51,10 @@ public class SecuritySettings implements SettingsClass { public static final Property MIN_PASSWORD_LENGTH = newProperty("settings.security.minPasswordLength", 5); + @Comment("Maximum length of password") + public static final Property MAX_PASSWORD_LENGTH = + newProperty("settings.security.passwordMaxLength", 30); + @Comment({ "This is a very important option: every time a player joins the server,", "if they are registered, AuthMe will switch him to unLoggedInGroup.", diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 22dd7431..d2e0a9e7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -156,8 +156,10 @@ settings: # ForceSurvivalMode to player when join ? ForceSurvivalMode: false security: - # minimum Length of password + # Minimum length of password minPasswordLength: 5 + # Maximum length of password + passwordMaxLength: 30 # this is very important options, # every time player join the server, # if they are registered, AuthMe will switch him diff --git a/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java index 380480ae..d00a4e37 100644 --- a/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java @@ -4,7 +4,8 @@ import fr.xephi.authme.ReflectionTestUtils; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.custom.RestrictionSettings; +import fr.xephi.authme.settings.custom.SecuritySettings; import fr.xephi.authme.task.ChangePasswordTask; import fr.xephi.authme.util.WrapperMock; import org.bukkit.Server; @@ -19,9 +20,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.any; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; @@ -44,11 +45,11 @@ public class ChangePasswordCommandTest { cacheMock = wrapperMock.getPlayerCache(); commandService = mock(CommandService.class); + when(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).thenReturn(2); + when(commandService.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)).thenReturn(50); // Only allow passwords with alphanumerical characters for the test - Settings.getPassRegex = "[a-zA-Z0-9]+"; - Settings.getPasswordMinLen = 2; - Settings.passwordMaxLength = 50; - Settings.unsafePasswords = Collections.EMPTY_LIST; + when(commandService.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)).thenReturn("[a-zA-Z0-9]+"); + when(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)).thenReturn(Collections.EMPTY_LIST); } @Test @@ -112,7 +113,7 @@ public class ChangePasswordCommandTest { // given CommandSender sender = initPlayerWithName("abc12", true); ChangePasswordCommand command = new ChangePasswordCommand(); - Settings.passwordMaxLength = 3; + given(commandService.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)).willReturn(3); // when command.executeCommand(sender, Arrays.asList("12", "test"), commandService); @@ -127,7 +128,7 @@ public class ChangePasswordCommandTest { // given CommandSender sender = initPlayerWithName("abc12", true); ChangePasswordCommand command = new ChangePasswordCommand(); - Settings.getPasswordMinLen = 7; + given(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).willReturn(7); // when command.executeCommand(sender, Arrays.asList("oldverylongpassword", "tester"), commandService); @@ -142,7 +143,8 @@ public class ChangePasswordCommandTest { // given CommandSender sender = initPlayerWithName("player", true); ChangePasswordCommand command = new ChangePasswordCommand(); - Settings.unsafePasswords = asList("test", "abc123"); + given(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)) + .willReturn(Arrays.asList("test", "abc123")); // when command.executeCommand(sender, Arrays.asList("oldpw", "abc123"), commandService);