Check valid password via service

- Create validation service; fixes same code being duplicated in four places
   - Goal is to remove Utils class, by moving methods to validation service or other services
 - Remove unused properties in legacy settings
This commit is contained in:
ljacqu
2016-04-02 22:44:20 +02:00
parent 73161de808
commit 4f86604699
15 changed files with 263 additions and 246 deletions
@@ -41,7 +41,7 @@ public class Management {
}
public void performRegister(final Player player, final String password, final String email) {
runTask(new AsyncRegister(player, password, email, plugin, dataSource, processService));
runTask(new AsyncRegister(player, password, email, plugin, dataSource, playerCache, processService));
}
public void performUnregister(final Player player, final String password, final boolean force) {
@@ -11,6 +11,7 @@ import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.util.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Event;
import org.bukkit.scheduler.BukkitTask;
@@ -28,10 +29,11 @@ public class ProcessService {
private final PasswordSecurity passwordSecurity;
private final PluginHooks pluginHooks;
private final SpawnLoader spawnLoader;
private final ValidationService validationService;
public ProcessService(NewSetting settings, Messages messages, AuthMe authMe, DataSource dataSource,
IpAddressManager ipAddressManager, PasswordSecurity passwordSecurity, PluginHooks pluginHooks,
SpawnLoader spawnLoader) {
SpawnLoader spawnLoader, ValidationService validationService) {
this.settings = settings;
this.messages = messages;
this.authMe = authMe;
@@ -40,6 +42,7 @@ public class ProcessService {
this.passwordSecurity = passwordSecurity;
this.pluginHooks = pluginHooks;
this.spawnLoader = spawnLoader;
this.validationService = validationService;
}
/**
@@ -199,4 +202,15 @@ public class ProcessService {
return dataSource;
}
/**
* Verifies whether a password is valid according to the plugin settings.
*
* @param password the password to verify
* @param username the username the password is associated with
* @return message key with the password error, or {@code null} if password is valid
*/
public MessageKey validatePassword(String password, String username) {
return validationService.validatePassword(password, username);
}
}
@@ -12,6 +12,8 @@ import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.security.crypts.TwoFactor;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.Bukkit;
@@ -30,10 +32,11 @@ public class AsyncRegister implements Process {
private final String email;
private final AuthMe plugin;
private final DataSource database;
private final PlayerCache playerCache;
private final ProcessService service;
public AsyncRegister(Player player, String password, String email, AuthMe plugin, DataSource data,
ProcessService service) {
PlayerCache playerCache, ProcessService service) {
this.player = player;
this.password = password;
this.name = player.getName().toLowerCase();
@@ -41,32 +44,24 @@ public class AsyncRegister implements Process {
this.plugin = plugin;
this.database = data;
this.ip = service.getIpAddressManager().getPlayerIp(player);
this.playerCache = playerCache;
this.service = service;
}
private boolean preRegisterCheck() {
String passLow = password.toLowerCase();
if (PlayerCache.getInstance().isAuthenticated(name)) {
if (playerCache.isAuthenticated(name)) {
service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return false;
} else if (!Settings.isRegistrationEnabled) {
} else if (!service.getProperty(RegistrationSettings.IS_ENABLED)) {
service.send(player, MessageKey.REGISTRATION_DISABLED);
return false;
}
//check the password safety only if it's not a automatically generated password
if (service.getProperty(SecuritySettings.PASSWORD_HASH) != HashAlgorithm.TWO_FACTOR) {
if (!passLow.matches(Settings.getPassRegex)) {
service.send(player, MessageKey.PASSWORD_MATCH_ERROR);
return false;
} else if (passLow.equalsIgnoreCase(player.getName())) {
service.send(player, MessageKey.PASSWORD_IS_USERNAME_ERROR);
return false;
} else if (password.length() < Settings.getPasswordMinLen || password.length() > Settings.passwordMaxLength) {
service.send(player, MessageKey.INVALID_PASSWORD_LENGTH);
return false;
} else if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(password.toLowerCase())) {
service.send(player, MessageKey.PASSWORD_UNSAFE_ERROR);
MessageKey passwordError = service.validatePassword(password, player.getName());
if (passwordError != null) {
service.send(player, passwordError);
return false;
}
}
@@ -75,15 +70,17 @@ public class AsyncRegister implements Process {
if (database.isAuthAvailable(name)) {
service.send(player, MessageKey.NAME_ALREADY_REGISTERED);
return false;
} else if(Settings.getmaxRegPerIp > 0
&& !ip.equalsIgnoreCase("127.0.0.1")
&& !ip.equalsIgnoreCase("localhost")
}
final int maxRegPerIp = service.getProperty(RestrictionSettings.MAX_REGISTRATION_PER_IP);
if (maxRegPerIp > 0
&& !"127.0.0.1".equalsIgnoreCase(ip)
&& !"localhost".equalsIgnoreCase(ip)
&& !plugin.getPermissionsManager().hasPermission(player, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)) {
int maxReg = Settings.getmaxRegPerIp;
List<String> otherAccounts = database.getAllAuthsByIp(ip);
if (otherAccounts.size() >= maxReg) {
service.send(player, MessageKey.MAX_REGISTER_EXCEEDED, Integer.toString(maxReg),
Integer.toString(otherAccounts.size()), StringUtils.join(", ", otherAccounts.toString()));
if (otherAccounts.size() >= maxRegPerIp) {
service.send(player, MessageKey.MAX_REGISTER_EXCEEDED, Integer.toString(maxRegPerIp),
Integer.toString(otherAccounts.size()), StringUtils.join(", ", otherAccounts));
return false;
}
}