#567 Move email validation logic to validation service

This commit is contained in:
ljacqu
2016-04-03 20:44:13 +02:00
parent b6ccb3e632
commit 9ea75c502c
19 changed files with 402 additions and 253 deletions
+1 -1
View File
@@ -261,7 +261,7 @@ public class AuthMe extends JavaPlugin {
// Set up the permissions manager and command handler
permsMan = initializePermissionsManager();
ValidationService validationService = new ValidationService(newSettings);
ValidationService validationService = new ValidationService(newSettings, database, permsMan);
commandHandler = initializeCommandHandler(permsMan, messages, passwordSecurity, newSettings, ipAddressManager,
pluginHooks, spawnLoader, antiBot, validationService);
@@ -222,4 +222,12 @@ public class CommandService {
return validationService.validatePassword(password, username);
}
public boolean validateEmail(String email) {
return validationService.validateEmail(email);
}
public boolean isEmailFreeForRegistration(String email, CommandSender sender) {
return validationService.isEmailFreeForRegistration(email, sender);
}
}
@@ -6,12 +6,13 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.command.CommandSender;
import java.util.List;
/**
* Admin command for setting an email to an account.
*/
public class SetEmailCommand implements ExecutableCommand {
@Override
@@ -22,7 +23,7 @@ public class SetEmailCommand implements ExecutableCommand {
final String playerEmail = arguments.get(1);
// Validate the email address
if (!Utils.isEmailCorrect(playerEmail, commandService.getSettings())) {
if (!commandService.validateEmail(playerEmail)) {
commandService.send(sender, MessageKey.INVALID_EMAIL);
return;
}
@@ -36,8 +37,7 @@ public class SetEmailCommand implements ExecutableCommand {
if (auth == null) {
commandService.send(sender, MessageKey.UNKNOWN_USER);
return;
} else if (dataSource.countAuthsByEmail(playerEmail)
>= commandService.getProperty(EmailSettings.MAX_REG_PER_EMAIL)) {
} else if (!commandService.isEmailFreeForRegistration(playerEmail, sender)) {
commandService.send(sender, MessageKey.EMAIL_ALREADY_USED_ERROR);
return;
}
@@ -3,11 +3,13 @@ package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import java.util.List;
/**
* Command for setting an email to an account.
*/
public class AddEmailCommand extends PlayerCommand {
@Override
@@ -15,9 +17,8 @@ public class AddEmailCommand extends PlayerCommand {
String email = arguments.get(0);
String emailConfirmation = arguments.get(1);
if (!Utils.isEmailCorrect(email, commandService.getSettings())) {
commandService.send(player, MessageKey.INVALID_EMAIL);
} else if (email.equals(emailConfirmation)) {
if (email.equals(emailConfirmation)) {
// Closer inspection of the mail address handled by the async task
commandService.getManagement().performAddEmail(player, email);
} else {
commandService.send(player, MessageKey.CONFIRM_EMAIL_MESSAGE);
@@ -7,13 +7,16 @@ import fr.xephi.authme.process.Management;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import java.util.List;
import static fr.xephi.authme.settings.properties.EmailSettings.RECOVERY_PASSWORD_LENGTH;
import static fr.xephi.authme.settings.properties.RestrictionSettings.ENABLE_PASSWORD_CONFIRMATION;
public class RegisterCommand extends PlayerCommand {
@Override
@@ -24,25 +27,27 @@ public class RegisterCommand extends PlayerCommand {
return;
}
if (arguments.isEmpty() || Settings.enablePasswordConfirmation && arguments.size() < 2) {
if (arguments.isEmpty() || commandService.getProperty(ENABLE_PASSWORD_CONFIRMATION) && arguments.size() < 2) {
commandService.send(player, MessageKey.USAGE_REGISTER);
return;
}
final Management management = commandService.getManagement();
if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) {
if (Settings.doubleEmailCheck && arguments.size() < 2 || !arguments.get(0).equals(arguments.get(1))) {
if (commandService.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)
&& !commandService.getProperty(EmailSettings.MAIL_ACCOUNT).isEmpty()) {
boolean emailDoubleCheck = commandService.getProperty(RegistrationSettings.ENABLE_CONFIRM_EMAIL);
if (emailDoubleCheck && arguments.size() < 2 || !arguments.get(0).equals(arguments.get(1))) {
commandService.send(player, MessageKey.USAGE_REGISTER);
return;
}
final String email = arguments.get(0);
if (!Utils.isEmailCorrect(email, commandService.getSettings())) {
if (!commandService.validateEmail(email)) {
commandService.send(player, MessageKey.INVALID_EMAIL);
return;
}
final String thePass = RandomString.generate(Settings.getRecoveryPassLength);
final String thePass = RandomString.generate(commandService.getProperty(RECOVERY_PASSWORD_LENGTH));
management.performRegister(player, thePass, email);
return;
}
@@ -213,4 +213,12 @@ public class ProcessService {
return validationService.validatePassword(password, username);
}
public boolean validateEmail(String email) {
return validationService.validateEmail(email);
}
public boolean isEmailFreeForRegistration(String email, CommandSender sender) {
return validationService.isEmailFreeForRegistration(email, sender);
}
}
@@ -7,9 +7,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.Process;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
/**
@@ -42,9 +40,9 @@ public class AsyncAddEmail implements Process {
if (currentEmail != null && !"your@email.com".equals(currentEmail)) {
service.send(player, MessageKey.USAGE_CHANGE_EMAIL);
} else if (!Utils.isEmailCorrect(email, service.getSettings())) {
} else if (!service.validateEmail(email)) {
service.send(player, MessageKey.INVALID_EMAIL);
} else if (dataSource.countAuthsByEmail(email) >= service.getProperty(EmailSettings.MAX_REG_PER_EMAIL)) {
} else if (!service.isEmailFreeForRegistration(email, player)) {
service.send(player, MessageKey.EMAIL_ALREADY_USED_ERROR);
} else {
auth.setEmail(email);
@@ -6,9 +6,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.process.Process;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
/**
@@ -42,11 +40,11 @@ public class AsyncChangeEmail implements Process {
if (currentEmail == null) {
service.send(player, MessageKey.USAGE_ADD_EMAIL);
} else if (newEmail == null || !Utils.isEmailCorrect(newEmail, service.getSettings())) {
} else if (newEmail == null || !service.validateEmail(newEmail)) {
service.send(player, MessageKey.INVALID_NEW_EMAIL);
} else if (!oldEmail.equals(currentEmail)) {
service.send(player, MessageKey.INVALID_OLD_EMAIL);
} else if (dataSource.countAuthsByEmail(newEmail) >= service.getProperty(EmailSettings.MAX_REG_PER_EMAIL)) {
} else if (!service.isEmailFreeForRegistration(newEmail, player)) {
service.send(player, MessageKey.EMAIL_ALREADY_USED_ERROR);
} else {
saveNewEmail(auth);
@@ -39,7 +39,6 @@ public final class Settings {
public static List<String> forceRegisterCommandsAsConsole;
public static HashAlgorithm getPasswordHash;
public static Pattern nickPattern;
public static boolean useLogging = false;
public static boolean isChatAllowed, isPermissionCheckEnabled,
isForcedRegistrationEnabled, isTeleportToSpawnEnabled,
isSessionsEnabled, isAllowRestrictedIp,
@@ -50,8 +49,7 @@ public final class Settings {
protectInventoryBeforeLogInEnabled, isStopEnabled, reloadSupport,
rakamakUseIp, noConsoleSpam, removePassword, displayOtherAccounts,
emailRegistration, multiverse, bungee,
banUnsafeIp, doubleEmailCheck, sessionExpireOnIpChange,
disableSocialSpy, useEssentialsMotd,
banUnsafeIp, sessionExpireOnIpChange, useEssentialsMotd,
enableProtection, recallEmail, useWelcomeMessage,
broadcastWelcomeMessage, forceRegKick, forceRegLogin,
checkVeryGames, removeJoinMessage, removeLeaveMessage, delayJoinMessage,
@@ -59,15 +57,13 @@ public final class Settings {
kickPlayersBeforeStopping, allowAllCommandsIfRegIsOptional,
customAttributes, isRemoveSpeedEnabled, preventOtherCase, keepCollisionsDisabled;
public static String getNickRegex, getUnloggedinGroup,
unRegisteredGroup,
backupWindowsPath, getRegisteredGroup,
unRegisteredGroup, backupWindowsPath, getRegisteredGroup,
rakamakUsers, rakamakUsersIp, getmailAccount, defaultWorld,
spawnPriority, crazyloginFileName, sendPlayerTo;
public static int getWarnMessageInterval, getSessionTimeout,
getRegistrationTimeout, getMaxNickLength, getMinNickLength,
getPasswordMinLen, getMovementRadius,
getNonActivatedGroup, passwordMaxLength, getRecoveryPassLength,
getMailPort, maxLoginTry, captchaLength, saltLength,
getPasswordMinLen, getMovementRadius, getNonActivatedGroup, passwordMaxLength,
maxLoginTry, captchaLength, saltLength,
bCryptLog2Rounds, getMaxLoginPerIp, getMaxJoinPerIp;
protected static FileConfiguration configFile;
@@ -145,8 +141,6 @@ public final class Settings {
noConsoleSpam = load(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE);
removePassword = configFile.getBoolean("Security.console.removePassword", true);
getmailAccount = load(EmailSettings.MAIL_ACCOUNT);
getMailPort = configFile.getInt("Email.mailPort", 465);
getRecoveryPassLength = configFile.getInt("Email.RecoveryPasswordLength", 8);
displayOtherAccounts = configFile.getBoolean("settings.restrictions.displayOtherAccounts", true);
maxLoginTry = configFile.getInt("Security.captcha.maxLoginTry", 5);
captchaLength = configFile.getInt("Security.captcha.captchaLength", 5);
@@ -156,10 +150,7 @@ public final class Settings {
bungee = configFile.getBoolean("Hooks.bungeecord", false);
getForcedWorlds = configFile.getStringList("settings.restrictions.ForceSpawnOnTheseWorlds");
banUnsafeIp = configFile.getBoolean("settings.restrictions.banUnsafedIP", false);
doubleEmailCheck = configFile.getBoolean("settings.registration.doubleEmailCheck", false);
sessionExpireOnIpChange = configFile.getBoolean("settings.sessions.sessionExpireOnIpChange", true);
useLogging = configFile.getBoolean("Security.console.logConsole", false);
disableSocialSpy = configFile.getBoolean("Hooks.disableSocialSpy", true);
bCryptLog2Rounds = configFile.getInt("ExternalBoardOptions.bCryptLog2Round", 10);
useEssentialsMotd = configFile.getBoolean("Hooks.useEssentialsMotd", false);
defaultWorld = configFile.getString("Purge.defaultWorld", "world");
+2 -31
View File
@@ -7,9 +7,7 @@ import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer;
import fr.xephi.authme.events.AuthMeTeleportEvent;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.EmailSettings;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
@@ -21,7 +19,6 @@ import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Utility class for various operations used in the codebase.
@@ -254,38 +251,12 @@ public final class Utils {
}
}
public static boolean isEmailCorrect(String email, NewSetting settings) {
if (!email.contains("@") || "your@email.com".equalsIgnoreCase(email)) {
return false;
}
final String emailDomain = email.split("@")[1];
List<String> whitelist = settings.getProperty(EmailSettings.DOMAIN_WHITELIST);
if (!CollectionUtils.isEmpty(whitelist)) {
return containsIgnoreCase(whitelist, emailDomain);
}
List<String> blacklist = settings.getProperty(EmailSettings.DOMAIN_BLACKLIST);
return CollectionUtils.isEmpty(blacklist) || !containsIgnoreCase(blacklist, emailDomain);
}
private static boolean containsIgnoreCase(Collection<String> coll, String needle) {
for (String entry : coll) {
if (entry.equalsIgnoreCase(needle)) {
return true;
}
}
return false;
}
public static String getUUIDorName(OfflinePlayer player) {
String uuidOrName;
try {
uuidOrName = player.getUniqueId().toString();
return player.getUniqueId().toString();
} catch (Exception ignore) {
uuidOrName = player.getName();
return player.getName();
}
return uuidOrName;
}
public enum GroupType {
@@ -1,9 +1,17 @@
package fr.xephi.authme.util;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.command.CommandSender;
import java.util.Collection;
import java.util.List;
/**
* Validation service.
@@ -11,9 +19,13 @@ import fr.xephi.authme.settings.properties.SecuritySettings;
public class ValidationService {
private final NewSetting settings;
private final DataSource dataSource;
private final PermissionsManager permissionsManager;
public ValidationService(NewSetting settings) {
public ValidationService(NewSetting settings, DataSource dataSource, PermissionsManager permissionsManager) {
this.settings = settings;
this.dataSource = dataSource;
this.permissionsManager = permissionsManager;
}
/**
@@ -39,4 +51,33 @@ public class ValidationService {
}
return null;
}
public boolean validateEmail(String email) {
if (!email.contains("@") || "your@email.com".equalsIgnoreCase(email)) {
return false;
}
final String emailDomain = email.split("@")[1];
List<String> whitelist = settings.getProperty(EmailSettings.DOMAIN_WHITELIST);
if (!CollectionUtils.isEmpty(whitelist)) {
return containsIgnoreCase(whitelist, emailDomain);
}
List<String> blacklist = settings.getProperty(EmailSettings.DOMAIN_BLACKLIST);
return CollectionUtils.isEmpty(blacklist) || !containsIgnoreCase(blacklist, emailDomain);
}
public boolean isEmailFreeForRegistration(String email, CommandSender sender) {
return permissionsManager.hasPermission(sender, PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS)
|| dataSource.countAuthsByEmail(email) < settings.getProperty(EmailSettings.MAX_REG_PER_EMAIL);
}
private static boolean containsIgnoreCase(Collection<String> coll, String needle) {
for (String entry : coll) {
if (entry.equalsIgnoreCase(needle)) {
return true;
}
}
return false;
}
}