mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-17 22:28:13 +01:00
#347 Use NewSetting properties for change password command
This commit is contained in:
parent
fb6cff2e07
commit
e18be3024a
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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 "";
|
||||
}
|
||||
|
@ -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<String> ALLOWED_PASSWORD_CHARS =
|
||||
public static final Property<String> ALLOWED_PASSWORD_REGEX =
|
||||
newProperty("settings.restrictions.allowedPasswordCharacters", "[\\x21-\\x7E]*");
|
||||
|
||||
@Comment("Force survival gamemode when player joins?")
|
||||
|
@ -51,6 +51,10 @@ public class SecuritySettings implements SettingsClass {
|
||||
public static final Property<Integer> MIN_PASSWORD_LENGTH =
|
||||
newProperty("settings.security.minPasswordLength", 5);
|
||||
|
||||
@Comment("Maximum length of password")
|
||||
public static final Property<Integer> 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.",
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user