Search in sources :

Example 16 with ValidationResult

use of fr.xephi.authme.service.ValidationService.ValidationResult in project AuthMeReloaded by AuthMe.

the class SetPasswordCommand method runCommand.

@Override
protected void runCommand(Player player, List<String> arguments) {
    if (recoveryService.canChangePassword(player)) {
        String name = player.getName();
        String password = arguments.get(0);
        ValidationResult result = validationService.validatePassword(password, name);
        if (!result.hasError()) {
            HashedPassword hashedPassword = passwordSecurity.computeHash(password, name);
            dataSource.updatePassword(name, hashedPassword);
            ConsoleLogger.info("Player '" + name + "' has changed their password from recovery");
            commonService.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS);
        } else {
            commonService.send(player, result.getMessageKey(), result.getArgs());
        }
    }
}
Also used : ValidationResult(fr.xephi.authme.service.ValidationService.ValidationResult) HashedPassword(fr.xephi.authme.security.crypts.HashedPassword)

Example 17 with ValidationResult

use of fr.xephi.authme.service.ValidationService.ValidationResult in project AuthMeReloaded by AuthMe.

the class ChangePasswordAdminCommand method executeCommand.

@Override
public void executeCommand(final CommandSender sender, List<String> arguments) {
    // Get the player and password
    final String playerName = arguments.get(0);
    final String playerPass = arguments.get(1);
    // Validate the password
    ValidationResult validationResult = validationService.validatePassword(playerPass, playerName);
    if (validationResult.hasError()) {
        commonService.send(sender, validationResult.getMessageKey(), validationResult.getArgs());
        return;
    }
    // Set the password
    bukkitService.runTaskOptionallyAsync(() -> changePassword(playerName.toLowerCase(), playerPass, sender));
}
Also used : ValidationResult(fr.xephi.authme.service.ValidationService.ValidationResult)

Example 18 with ValidationResult

use of fr.xephi.authme.service.ValidationService.ValidationResult in project AuthMeReloaded by AuthMe.

the class RegisterAdminCommand method executeCommand.

@Override
public void executeCommand(final CommandSender sender, List<String> arguments) {
    // Get the player name and password
    final String playerName = arguments.get(0);
    final String playerPass = arguments.get(1);
    final String playerNameLowerCase = playerName.toLowerCase();
    // Command logic
    ValidationResult passwordValidation = validationService.validatePassword(playerPass, playerName);
    if (passwordValidation.hasError()) {
        commonService.send(sender, passwordValidation.getMessageKey(), passwordValidation.getArgs());
        return;
    }
    bukkitService.runTaskOptionallyAsync(new Runnable() {

        @Override
        public void run() {
            if (dataSource.isAuthAvailable(playerNameLowerCase)) {
                commonService.send(sender, MessageKey.NAME_ALREADY_REGISTERED);
                return;
            }
            HashedPassword hashedPassword = passwordSecurity.computeHash(playerPass, playerNameLowerCase);
            PlayerAuth auth = PlayerAuth.builder().name(playerNameLowerCase).realName(playerName).password(hashedPassword).build();
            if (!dataSource.saveAuth(auth)) {
                commonService.send(sender, MessageKey.ERROR);
                return;
            }
            dataSource.setUnlogged(playerNameLowerCase);
            commonService.send(sender, MessageKey.REGISTER_SUCCESS);
            ConsoleLogger.info(sender.getName() + " registered " + playerName);
            final Player player = bukkitService.getPlayerExact(playerName);
            if (player != null) {
                bukkitService.scheduleSyncTaskFromOptionallyAsyncTask(new Runnable() {

                    @Override
                    public void run() {
                        player.kickPlayer(commonService.retrieveSingleMessage(MessageKey.KICK_FOR_ADMIN_REGISTER));
                    }
                });
            }
        }
    });
}
Also used : Player(org.bukkit.entity.Player) ValidationResult(fr.xephi.authme.service.ValidationService.ValidationResult) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) HashedPassword(fr.xephi.authme.security.crypts.HashedPassword)

Example 19 with ValidationResult

use of fr.xephi.authme.service.ValidationService.ValidationResult in project AuthMeReloaded by AuthMe.

the class RegisterAdminCommandTest method shouldRejectInvalidPassword.

@Test
public void shouldRejectInvalidPassword() {
    // given
    String user = "tester";
    String password = "myPassword";
    given(validationService.validatePassword(password, user)).willReturn(new ValidationResult(MessageKey.INVALID_PASSWORD_LENGTH));
    CommandSender sender = mock(CommandSender.class);
    // when
    command.executeCommand(sender, Arrays.asList(user, password));
    // then
    verify(validationService).validatePassword(password, user);
    verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH, new String[0]);
    verify(bukkitService, never()).runTaskAsynchronously(any(Runnable.class));
}
Also used : CommandSender(org.bukkit.command.CommandSender) ValidationResult(fr.xephi.authme.service.ValidationService.ValidationResult) Test(org.junit.Test)

Example 20 with ValidationResult

use of fr.xephi.authme.service.ValidationService.ValidationResult in project AuthMeReloaded by AuthMe.

the class ChangePasswordAdminCommandTest method shouldUpdatePasswordOfLoggedInUser.

@Test
public void shouldUpdatePasswordOfLoggedInUser() {
    // given
    CommandSender sender = mock(CommandSender.class);
    String player = "my_user12";
    String password = "passPass";
    given(playerCache.isAuthenticated(player)).willReturn(true);
    HashedPassword hashedPassword = mock(HashedPassword.class);
    given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword);
    given(dataSource.updatePassword(player, hashedPassword)).willReturn(true);
    given(validationService.validatePassword(password, player)).willReturn(new ValidationResult());
    // when
    command.executeCommand(sender, Arrays.asList(player, password));
    runOptionallyAsyncTask(bukkitService);
    // then
    verify(validationService).validatePassword(password, player);
    verify(service).send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS);
    verify(passwordSecurity).computeHash(password, player);
    verify(dataSource).updatePassword(player, hashedPassword);
}
Also used : CommandSender(org.bukkit.command.CommandSender) ValidationResult(fr.xephi.authme.service.ValidationService.ValidationResult) HashedPassword(fr.xephi.authme.security.crypts.HashedPassword) Test(org.junit.Test)

Aggregations

ValidationResult (fr.xephi.authme.service.ValidationService.ValidationResult)25 Test (org.junit.Test)20 CommandSender (org.bukkit.command.CommandSender)11 HashedPassword (fr.xephi.authme.security.crypts.HashedPassword)8 PlayerAuth (fr.xephi.authme.data.auth.PlayerAuth)6 Player (org.bukkit.entity.Player)5 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 MessageKey (fr.xephi.authme.message.MessageKey)1 BlockCommandSender (org.bukkit.command.BlockCommandSender)1