Search in sources :

Example 16 with HashedPassword

use of fr.xephi.authme.security.crypts.HashedPassword in project AuthMeReloaded by AuthMe.

the class PasswordRegisterExecutorTest method shouldCreatePlayerAuth.

@Test
public void shouldCreatePlayerAuth() {
    // given
    given(passwordSecurity.computeHash(anyString(), anyString())).willAnswer(invocation -> new HashedPassword(invocation.getArgument(0)));
    Player player = mockPlayerWithName("S1m0N");
    TestHelper.mockPlayerIp(player, "123.45.67.89");
    PasswordRegisterParams params = PasswordRegisterParams.of(player, "pass", "mail@example.org");
    // when
    PlayerAuth auth = executor.buildPlayerAuth(params);
    // then
    assertThat(auth, hasAuthBasicData("s1m0n", "S1m0N", "mail@example.org", null));
    assertThat(auth.getRegistrationIp(), equalTo("123.45.67.89"));
    assertIsCloseTo(auth.getRegistrationDate(), System.currentTimeMillis(), 500);
    assertThat(auth.getPassword(), equalToHash("pass"));
}
Also used : Player(org.bukkit.entity.Player) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) HashedPassword(fr.xephi.authme.security.crypts.HashedPassword) Test(org.junit.Test)

Example 17 with HashedPassword

use of fr.xephi.authme.security.crypts.HashedPassword in project AuthMeReloaded by AuthMe.

the class AsynchronousUnregisterTest method shouldPerformUnregister.

@Test
public void shouldPerformUnregister() {
    // given
    Player player = mock(Player.class);
    String name = "Frank21";
    given(player.getName()).willReturn(name);
    given(player.isOnline()).willReturn(true);
    PlayerAuth auth = mock(PlayerAuth.class);
    given(playerCache.getAuth(name)).willReturn(auth);
    HashedPassword password = new HashedPassword("password", "in_auth_obj");
    given(auth.getPassword()).willReturn(password);
    String userPassword = "pass";
    given(passwordSecurity.comparePassword(userPassword, password, name)).willReturn(true);
    given(dataSource.removeAuth(name)).willReturn(true);
    given(service.getProperty(RegistrationSettings.FORCE)).willReturn(true);
    given(service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)).willReturn(true);
    given(service.getProperty(RestrictionSettings.TIMEOUT)).willReturn(21);
    setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
    // when
    asynchronousUnregister.unregister(player, userPassword);
    // then
    verify(service).send(player, MessageKey.UNREGISTERED_SUCCESS);
    verify(passwordSecurity).comparePassword(userPassword, password, name);
    verify(dataSource).removeAuth(name);
    verify(playerCache).removePlayer(name);
    verify(teleportationService).teleportOnJoin(player);
    verifyCalledUnregisterEventFor(player);
    verify(commandManager).runCommandsOnUnregister(player);
    verify(bungeeSender).sendAuthMeBungeecordMessage(MessageType.UNREGISTER, name);
    verify(player).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 21 * 20, 2));
}
Also used : Player(org.bukkit.entity.Player) PotionEffect(org.bukkit.potion.PotionEffect) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) HashedPassword(fr.xephi.authme.security.crypts.HashedPassword) Test(org.junit.Test)

Example 18 with HashedPassword

use of fr.xephi.authme.security.crypts.HashedPassword in project AuthMeReloaded by AuthMe.

the class AsynchronousUnregisterTest method shouldPerformUnregisterAndNotApplyBlindEffect.

@Test
public void shouldPerformUnregisterAndNotApplyBlindEffect() {
    // given
    Player player = mock(Player.class);
    String name = "Frank21";
    given(player.getName()).willReturn(name);
    given(player.isOnline()).willReturn(true);
    PlayerAuth auth = mock(PlayerAuth.class);
    given(playerCache.getAuth(name)).willReturn(auth);
    HashedPassword password = new HashedPassword("password", "in_auth_obj");
    given(auth.getPassword()).willReturn(password);
    String userPassword = "pass";
    given(passwordSecurity.comparePassword(userPassword, password, name)).willReturn(true);
    given(dataSource.removeAuth(name)).willReturn(true);
    given(service.getProperty(RegistrationSettings.FORCE)).willReturn(true);
    given(service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)).willReturn(false);
    setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
    // when
    asynchronousUnregister.unregister(player, userPassword);
    // then
    verify(service).send(player, MessageKey.UNREGISTERED_SUCCESS);
    verify(passwordSecurity).comparePassword(userPassword, password, name);
    verify(dataSource).removeAuth(name);
    verify(playerCache).removePlayer(name);
    verify(teleportationService).teleportOnJoin(player);
    verifyCalledUnregisterEventFor(player);
    verify(commandManager).runCommandsOnUnregister(player);
    verify(bungeeSender).sendAuthMeBungeecordMessage(MessageType.UNREGISTER, name);
    verify(player, never()).addPotionEffect(any(PotionEffect.class));
}
Also used : Player(org.bukkit.entity.Player) PotionEffect(org.bukkit.potion.PotionEffect) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) HashedPassword(fr.xephi.authme.security.crypts.HashedPassword) Test(org.junit.Test)

Example 19 with HashedPassword

use of fr.xephi.authme.security.crypts.HashedPassword in project AuthMeReloaded by AuthMe.

the class AsynchronousUnregisterTest method shouldRejectWrongPassword.

@Test
public void shouldRejectWrongPassword() {
    // given
    Player player = mock(Player.class);
    String name = "Bobby";
    given(player.getName()).willReturn(name);
    PlayerAuth auth = mock(PlayerAuth.class);
    given(playerCache.getAuth(name)).willReturn(auth);
    HashedPassword password = new HashedPassword("password", "in_auth_obj");
    given(auth.getPassword()).willReturn(password);
    String userPassword = "pass";
    given(passwordSecurity.comparePassword(userPassword, password, name)).willReturn(false);
    // when
    asynchronousUnregister.unregister(player, userPassword);
    // then
    verify(service).send(player, MessageKey.WRONG_PASSWORD);
    verify(passwordSecurity).comparePassword(userPassword, password, name);
    verifyNoInteractions(dataSource, limboService, teleportationService, bukkitService, bungeeSender);
    verify(player, only()).getName();
}
Also used : Player(org.bukkit.entity.Player) PlayerAuth(fr.xephi.authme.data.auth.PlayerAuth) HashedPassword(fr.xephi.authme.security.crypts.HashedPassword) Test(org.junit.Test)

Example 20 with HashedPassword

use of fr.xephi.authme.security.crypts.HashedPassword in project AuthMeReloaded by AuthMe.

the class AsyncChangePassword method changePasswordAsAdmin.

/**
 * Change a user's password as an administrator, without asking for the previous one
 *
 * @param sender who is performing the operation, null if called by other plugins
 * @param playerName the player name
 * @param newPassword the new password chosen for the player
 */
public void changePasswordAsAdmin(CommandSender sender, final String playerName, String newPassword) {
    final String lowerCaseName = playerName.toLowerCase();
    if (!(playerCache.isAuthenticated(lowerCaseName) || dataSource.isAuthAvailable(lowerCaseName))) {
        if (sender == null) {
            logger.warning("Tried to change password for user " + lowerCaseName + " but it doesn't exist!");
        } else {
            commonService.send(sender, MessageKey.UNKNOWN_USER);
        }
        return;
    }
    HashedPassword hashedPassword = passwordSecurity.computeHash(newPassword, lowerCaseName);
    if (dataSource.updatePassword(lowerCaseName, hashedPassword)) {
        bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, lowerCaseName);
        if (sender != null) {
            commonService.send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS);
            logger.info(sender.getName() + " changed password of " + lowerCaseName);
        } else {
            logger.info("Changed password of " + lowerCaseName);
        }
    } else {
        if (sender != null) {
            commonService.send(sender, MessageKey.ERROR);
        }
        logger.warning("An error occurred while changing password for user " + lowerCaseName + "!");
    }
}
Also used : HashedPassword(fr.xephi.authme.security.crypts.HashedPassword)

Aggregations

HashedPassword (fr.xephi.authme.security.crypts.HashedPassword)55 Test (org.junit.Test)35 PlayerAuth (fr.xephi.authme.data.auth.PlayerAuth)22 Player (org.bukkit.entity.Player)14 ValidationResult (fr.xephi.authme.service.ValidationService.ValidationResult)9 CommandSender (org.bukkit.command.CommandSender)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 PasswordEncryptionEvent (fr.xephi.authme.events.PasswordEncryptionEvent)5 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 ValidationService (fr.xephi.authme.service.ValidationService)2 Connection (java.sql.Connection)2 PotionEffect (org.bukkit.potion.PotionEffect)2 AuthMeMatchers.hasAuthLocation (fr.xephi.authme.AuthMeMatchers.hasAuthLocation)1 EncryptionMethod (fr.xephi.authme.security.crypts.EncryptionMethod)1 Joomla (fr.xephi.authme.security.crypts.Joomla)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1