mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-24 11:15:19 +01:00
#450 Move Settings#isEmailCorrect to Utils class
This commit is contained in:
parent
9653354135
commit
42ae30ed3a
@ -49,6 +49,8 @@ import fr.xephi.authme.settings.OtherAccounts;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.Spawn;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.CollectionUtils;
|
||||
@ -292,7 +294,7 @@ public class AuthMe extends JavaPlugin {
|
||||
setupApi();
|
||||
|
||||
// Set up the management
|
||||
management = new Management(this);
|
||||
management = new Management(this, newSettings);
|
||||
|
||||
// Set up the BungeeCord hook
|
||||
setupBungeeCordHook();
|
||||
@ -339,12 +341,13 @@ public class AuthMe extends JavaPlugin {
|
||||
*/
|
||||
private void showSettingsWarnings() {
|
||||
// Force single session disabled
|
||||
if (!Settings.isForceSingleSessionEnabled) {
|
||||
if (!newSettings.getProperty(RestrictionSettings.FORCE_SINGLE_SESSION)) {
|
||||
ConsoleLogger.showError("WARNING!!! By disabling ForceSingleSession, your server protection is inadequate!");
|
||||
}
|
||||
|
||||
// Session timeout disabled
|
||||
if (Settings.getSessionTimeout == 0 && Settings.isSessionsEnabled) {
|
||||
if (newSettings.getProperty(PluginSettings.SESSIONS_TIMEOUT) == 0
|
||||
&& newSettings.getProperty(PluginSettings.SESSIONS_ENABLED)) {
|
||||
ConsoleLogger.showError("WARNING!!! You set session timeout to 0, this may cause security issues!");
|
||||
}
|
||||
}
|
||||
@ -399,7 +402,7 @@ public class AuthMe extends JavaPlugin {
|
||||
* Set up the BungeeCord hook.
|
||||
*/
|
||||
private void setupBungeeCordHook() {
|
||||
if (Settings.bungee) {
|
||||
if (newSettings.getProperty(HooksSettings.BUNGEECORD)) {
|
||||
Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
|
||||
Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new BungeeCordMessage(this));
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.util.Utils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
@ -20,7 +20,7 @@ public class SetEmailCommand implements ExecutableCommand {
|
||||
final String playerEmail = arguments.get(1);
|
||||
|
||||
// Validate the email address
|
||||
if (!Settings.isEmailCorrect(playerEmail)) {
|
||||
if (!Utils.isEmailCorrect(playerEmail, commandService.getSettings())) {
|
||||
commandService.send(sender, MessageKey.INVALID_EMAIL);
|
||||
return;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.security.RandomString;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.util.Utils;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
@ -26,7 +27,7 @@ public class RegisterCommand extends PlayerCommand {
|
||||
return;
|
||||
}
|
||||
final String email = arguments.get(0);
|
||||
if (!Settings.isEmailCorrect(email)) {
|
||||
if (!Utils.isEmailCorrect(email, commandService.getSettings())) {
|
||||
commandService.send(player, MessageKey.INVALID_EMAIL);
|
||||
return;
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import fr.xephi.authme.process.logout.AsynchronousLogout;
|
||||
import fr.xephi.authme.process.quit.AsynchronousQuit;
|
||||
import fr.xephi.authme.process.register.AsyncRegister;
|
||||
import fr.xephi.authme.process.unregister.AsynchronousUnregister;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
@ -17,15 +18,17 @@ public class Management {
|
||||
|
||||
private final AuthMe plugin;
|
||||
private final BukkitScheduler sched;
|
||||
private final NewSetting settings;
|
||||
|
||||
/**
|
||||
* Constructor for Management.
|
||||
*
|
||||
* @param plugin AuthMe
|
||||
*/
|
||||
public Management(AuthMe plugin) {
|
||||
public Management(AuthMe plugin, NewSetting settings) {
|
||||
this.plugin = plugin;
|
||||
this.sched = this.plugin.getServer().getScheduler();
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public void performLogin(final Player player, final String password, final boolean forceLogin) {
|
||||
@ -94,7 +97,7 @@ public class Management {
|
||||
sched.runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new AsyncChangeEmail(player, plugin, null, newEmail, newEmailVerify).process();
|
||||
new AsyncChangeEmail(player, plugin, null, newEmail, newEmailVerify, settings).process();
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -103,7 +106,7 @@ public class Management {
|
||||
sched.runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new AsyncChangeEmail(player, plugin, oldEmail, newEmail).process();
|
||||
new AsyncChangeEmail(player, plugin, oldEmail, newEmail, settings).process();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -5,8 +5,10 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
|
||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.output.Messages;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import fr.xephi.authme.util.Utils;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
@ -19,18 +21,21 @@ public class AsyncChangeEmail {
|
||||
private final String newEmail;
|
||||
private final String newEmailVerify;
|
||||
private final Messages m;
|
||||
private final NewSetting settings;
|
||||
|
||||
public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail, String newEmailVerify) {
|
||||
public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail, String newEmailVerify,
|
||||
NewSetting settings) {
|
||||
this.m = plugin.getMessages();
|
||||
this.player = player;
|
||||
this.plugin = plugin;
|
||||
this.oldEmail = oldEmail;
|
||||
this.newEmail = newEmail;
|
||||
this.newEmailVerify = newEmailVerify;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail) {
|
||||
this(player, plugin, oldEmail, newEmail, newEmail);
|
||||
public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail, NewSetting settings) {
|
||||
this(player, plugin, oldEmail, newEmail, newEmail, settings);
|
||||
}
|
||||
|
||||
public void process() {
|
||||
@ -57,7 +62,7 @@ public class AsyncChangeEmail {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!Settings.isEmailCorrect(newEmail)) {
|
||||
if (!Utils.isEmailCorrect(newEmail, settings)) {
|
||||
m.send(player, MessageKey.INVALID_NEW_EMAIL);
|
||||
return;
|
||||
}
|
||||
|
@ -361,7 +361,7 @@ public final class Settings {
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String checkLang(String lang) {
|
||||
private static String checkLang(String lang) {
|
||||
if (new File(PLUGIN_FOLDER, "messages" + File.separator + "messages_" + lang + ".yml").exists()) {
|
||||
ConsoleLogger.info("Set Language to: " + lang);
|
||||
return lang;
|
||||
@ -420,42 +420,6 @@ public final class Settings {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method isEmailCorrect.
|
||||
*
|
||||
* @param email String
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean isEmailCorrect(String email) {
|
||||
if (!email.contains("@"))
|
||||
return false;
|
||||
if (email.equalsIgnoreCase("your@email.com"))
|
||||
return false;
|
||||
String emailDomain = email.split("@")[1];
|
||||
boolean correct = true;
|
||||
if (emailWhitelist != null && !emailWhitelist.isEmpty()) {
|
||||
for (String domain : emailWhitelist) {
|
||||
if (!domain.equalsIgnoreCase(emailDomain)) {
|
||||
correct = false;
|
||||
} else {
|
||||
correct = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return correct;
|
||||
}
|
||||
if (emailBlacklist != null && !emailBlacklist.isEmpty()) {
|
||||
for (String domain : emailBlacklist) {
|
||||
if (domain.equalsIgnoreCase(emailDomain)) {
|
||||
correct = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return correct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves current configuration (plus defaults) to disk.
|
||||
* <p>
|
||||
|
@ -7,8 +7,10 @@ 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.World;
|
||||
@ -19,6 +21,7 @@ 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,6 +257,30 @@ 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 enum GroupType {
|
||||
|
@ -3,14 +3,18 @@ package fr.xephi.authme.util;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
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.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
@ -92,6 +96,95 @@ public class UtilsTest {
|
||||
assertThat(players, hasSize(2));
|
||||
}
|
||||
|
||||
// ----------------
|
||||
// Tests for Utils#isEmailCorrect()
|
||||
// ----------------
|
||||
@Test
|
||||
public void shouldAcceptEmailWithEmptyLists() {
|
||||
// given
|
||||
NewSetting settings = mock(NewSetting.class);
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.EMPTY_LIST);
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.EMPTY_LIST);
|
||||
|
||||
// when
|
||||
boolean result = Utils.isEmailCorrect("test@example.org", settings);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAcceptEmailWithWhitelist() {
|
||||
// given
|
||||
NewSetting settings = mock(NewSetting.class);
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST))
|
||||
.willReturn(Arrays.asList("domain.tld", "example.com"));
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.EMPTY_LIST);
|
||||
|
||||
// when
|
||||
boolean result = Utils.isEmailCorrect("TesT@Example.com", settings);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectEmailNotInWhitelist() {
|
||||
// given
|
||||
NewSetting settings = mock(NewSetting.class);
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST))
|
||||
.willReturn(Arrays.asList("domain.tld", "example.com"));
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST)).willReturn(Collections.EMPTY_LIST);
|
||||
|
||||
// when
|
||||
boolean result = Utils.isEmailCorrect("email@other-domain.abc", settings);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldAcceptEmailNotInBlacklist() {
|
||||
// given
|
||||
NewSetting settings = mock(NewSetting.class);
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.EMPTY_LIST);
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST))
|
||||
.willReturn(Arrays.asList("Example.org", "a-test-name.tld"));
|
||||
|
||||
// when
|
||||
boolean result = Utils.isEmailCorrect("sample@valid-name.tld", settings);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectEmailInBlacklist() {
|
||||
// given
|
||||
NewSetting settings = mock(NewSetting.class);
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_WHITELIST)).willReturn(Collections.EMPTY_LIST);
|
||||
given(settings.getProperty(EmailSettings.DOMAIN_BLACKLIST))
|
||||
.willReturn(Arrays.asList("Example.org", "a-test-name.tld"));
|
||||
|
||||
// when
|
||||
boolean result = Utils.isEmailCorrect("sample@a-Test-name.tld", settings);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectInvalidEmail() {
|
||||
// given/when/then
|
||||
assertThat(Utils.isEmailCorrect("invalidinput", mock(NewSetting.class)), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectDefaultEmail() {
|
||||
// given/when/then
|
||||
assertThat(Utils.isEmailCorrect("your@email.com", mock(NewSetting.class)), equalTo(false));
|
||||
}
|
||||
|
||||
// Note: This method is used through reflections
|
||||
@SuppressWarnings("unused")
|
||||
public static Player[] onlinePlayersImpl() {
|
||||
|
Loading…
Reference in New Issue
Block a user