Change password task to async process

- Perform async change password task just like other async processes: via Management
- Remove legacy setting
- Remove now unused service getter (#736)
This commit is contained in:
ljacqu 2016-06-15 20:56:34 +02:00
parent 15886fb517
commit ac484345a2
6 changed files with 65 additions and 72 deletions

View File

@ -736,15 +736,6 @@ public class AuthMe extends JavaPlugin {
// Service getters (deprecated)
// Use @Inject fields instead
// -------------
/**
* @return Plugin's messages.
* @deprecated should be used in API classes only (temporarily)
*/
@Deprecated
public Messages getMessages() {
return messages;
}
/**
* @return NewSetting
* @deprecated should be used in API classes only (temporarily)

View File

@ -1,13 +1,10 @@
package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.AuthMe;
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.security.PasswordSecurity;
import fr.xephi.authme.task.ChangePasswordTask;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.util.ValidationService.ValidationResult;
import org.bukkit.entity.Player;
@ -26,15 +23,11 @@ public class ChangePasswordCommand extends PlayerCommand {
@Inject
private PlayerCache playerCache;
@Inject
private BukkitService bukkitService;
@Inject
private ValidationService validationService;
@Inject
// TODO ljacqu 20160531: Remove this once change password task runs as a process (via Management)
private PasswordSecurity passwordSecurity;
private Management management;
@Override
public void runCommand(Player player, List<String> arguments) {
@ -54,9 +47,7 @@ public class ChangePasswordCommand extends PlayerCommand {
return;
}
AuthMe plugin = AuthMe.getInstance();
// TODO ljacqu 20160117: Call async task via Management
bukkitService.runTaskAsynchronously(
new ChangePasswordTask(plugin, player, oldPassword, newPassword, passwordSecurity));
management.performPasswordChange(player, oldPassword, newPassword);
}
}

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.process;
import fr.xephi.authme.process.changepassword.AsyncChangePassword;
import fr.xephi.authme.process.email.AsyncAddEmail;
import fr.xephi.authme.process.email.AsyncChangeEmail;
import fr.xephi.authme.process.join.AsynchronousJoin;
@ -36,9 +37,12 @@ public class Management {
private AsynchronousLogin asynchronousLogin;
@Inject
private AsynchronousUnregister asynchronousUnregister;
@Inject
private AsyncChangePassword asyncChangePassword;
Management() { }
public void performLogin(final Player player, final String password, final boolean forceLogin) {
runTask(new Runnable() {
@Override
@ -111,6 +115,15 @@ public class Management {
});
}
public void performPasswordChange(final Player player, final String oldPassword, final String newPassword) {
runTask(new Runnable() {
@Override
public void run() {
asyncChangePassword.changePassword(player, oldPassword, newPassword);
}
});
}
private void runTask(Runnable runnable) {
bukkitService.runTaskAsynchronously(runnable);
}

View File

@ -1,4 +1,4 @@
package fr.xephi.authme.task;
package fr.xephi.authme.process.changepassword;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
@ -6,52 +6,60 @@ import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.ProcessService;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.entity.Player;
public class ChangePasswordTask implements Runnable {
import javax.inject.Inject;
private final AuthMe plugin;
private final Player player;
private final String oldPassword;
private final String newPassword;
private final PasswordSecurity passwordSecurity;
public class AsyncChangePassword implements AsynchronousProcess {
public ChangePasswordTask(AuthMe plugin, Player player, String oldPassword, String newPassword,
PasswordSecurity passwordSecurity) {
this.plugin = plugin;
this.player = player;
this.oldPassword = oldPassword;
this.newPassword = newPassword;
this.passwordSecurity = passwordSecurity;
}
@Inject
private AuthMe plugin;
@Override
public void run() {
Messages m = plugin.getMessages();
@Inject
private DataSource dataSource;
@Inject
private ProcessService processService;
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private PlayerCache playerCache;
@Inject
private BukkitService bukkitService;
AsyncChangePassword() { }
public void changePassword(final Player player, String oldPassword, String newPassword) {
final String name = player.getName().toLowerCase();
PlayerAuth auth = PlayerCache.getInstance().getAuth(name);
PlayerAuth auth = playerCache.getAuth(name);
if (passwordSecurity.comparePassword(oldPassword, auth.getPassword(), player.getName())) {
HashedPassword hashedPassword = passwordSecurity.computeHash(newPassword, name);
auth.setPassword(hashedPassword);
if (!plugin.getDataSource().updatePassword(auth)) {
m.send(player, MessageKey.ERROR);
if (!dataSource.updatePassword(auth)) {
processService.send(player, MessageKey.ERROR);
return;
}
PlayerCache.getInstance().updatePlayer(auth);
m.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS);
playerCache.updatePlayer(auth);
processService.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS);
ConsoleLogger.info(player.getName() + " changed his password");
if (Settings.bungee) {
if (processService.getProperty(HooksSettings.BUNGEECORD)) {
final String hash = hashedPassword.getHash();
final String salt = hashedPassword.getSalt();
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
bukkitService.scheduleSyncDelayedTask(new Runnable() {
@Override
public void run() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
@ -64,7 +72,7 @@ public class ChangePasswordTask implements Runnable {
});
}
} else {
m.send(player, MessageKey.WRONG_PASSWORD);
processService.send(player, MessageKey.WRONG_PASSWORD);
}
}
}

View File

@ -2,7 +2,6 @@ package fr.xephi.authme.settings;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -26,7 +25,6 @@ public final class Settings {
public static boolean protectInventoryBeforeLogInEnabled;
public static boolean isStopEnabled;
public static boolean reloadSupport;
public static boolean bungee;
public static boolean forceRegLogin;
public static boolean noTeleport;
public static boolean isRemoveSpeedEnabled;
@ -65,7 +63,6 @@ public final class Settings {
protectInventoryBeforeLogInEnabled = load(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN);
isStopEnabled = configFile.getBoolean("Security.SQLProblem.stopServer", true);
reloadSupport = configFile.getBoolean("Security.ReloadCommand.useReloadCommandSupport", true);
bungee = load(HooksSettings.BUNGEECORD);
defaultWorld = configFile.getString("Purge.defaultWorld", "world");
forceRegLogin = load(RegistrationSettings.FORCE_LOGIN_AFTER_REGISTER);
noTeleport = load(RestrictionSettings.NO_TELEPORT);

View File

@ -1,13 +1,11 @@
package fr.xephi.authme.command.executable.changepassword;
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.process.Management;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.ChangePasswordTask;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.ValidationService;
import fr.xephi.authme.util.ValidationService.ValidationResult;
import org.bukkit.command.BlockCommandSender;
@ -16,7 +14,6 @@ import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@ -26,8 +23,6 @@ import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.any;
@ -46,18 +41,18 @@ public class ChangePasswordCommandTest {
@InjectMocks
private ChangePasswordCommand command;
@Mock
private PlayerCache playerCache;
@Mock
private CommandService commandService;
@Mock
private BukkitService bukkitService;
private PlayerCache playerCache;
@Mock
private ValidationService validationService;
@Mock
private Management management;
@Before
public void setSettings() {
when(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).thenReturn(2);
@ -110,20 +105,18 @@ public class ChangePasswordCommandTest {
@Test
public void shouldForwardTheDataForValidPassword() {
// given
CommandSender sender = initPlayerWithName("parker", true);
String oldPass = "oldpass";
String newPass = "abc123";
Player player = initPlayerWithName("parker", true);
given(validationService.validatePassword("abc123", "parker")).willReturn(new ValidationResult());
// when
command.executeCommand(sender, Arrays.asList("abc123", "abc123"));
command.executeCommand(player, Arrays.asList(oldPass, newPass));
// then
verify(validationService).validatePassword("abc123", "parker");
verify(commandService, never()).send(eq(sender), any(MessageKey.class));
ArgumentCaptor<ChangePasswordTask> taskCaptor = ArgumentCaptor.forClass(ChangePasswordTask.class);
verify(bukkitService).runTaskAsynchronously(taskCaptor.capture());
ChangePasswordTask task = taskCaptor.getValue();
assertThat((String) ReflectionTestUtils.getFieldValue(ChangePasswordTask.class, task, "newPassword"),
equalTo("abc123"));
verify(validationService).validatePassword(newPass, "parker");
verify(commandService, never()).send(eq(player), any(MessageKey.class));
verify(management).performPasswordChange(player, oldPass, newPass);
}
private Player initPlayerWithName(String name, boolean loggedIn) {