mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-18 06:38:28 +01:00
Minor code householding
This commit is contained in:
parent
80dce1a92f
commit
8e4288f911
@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
import fr.xephi.authme.command.ExecutableCommand;
|
||||
import fr.xephi.authme.task.purge.PurgeService;
|
||||
import org.bukkit.ChatColor;
|
||||
@ -26,10 +27,8 @@ public class PurgeCommand implements ExecutableCommand {
|
||||
String daysStr = arguments.get(0);
|
||||
|
||||
// Convert the days string to an integer value, and make sure it's valid
|
||||
int days;
|
||||
try {
|
||||
days = Integer.parseInt(daysStr);
|
||||
} catch (NumberFormatException ex) {
|
||||
Integer days = Ints.tryParse(daysStr);
|
||||
if (days == null) {
|
||||
sender.sendMessage(ChatColor.RED + "The value you've entered is invalid!");
|
||||
return;
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package fr.xephi.authme.data.limbo;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.SpawnLoader;
|
||||
import fr.xephi.authme.settings.properties.LimboSettings;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
import org.bukkit.Location;
|
||||
@ -20,9 +19,6 @@ import static fr.xephi.authme.util.Utils.isCollectionEmpty;
|
||||
*/
|
||||
class LimboServiceHelper {
|
||||
|
||||
@Inject
|
||||
private SpawnLoader spawnLoader;
|
||||
|
||||
@Inject
|
||||
private PermissionsManager permissionsManager;
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.security.crypts;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
import de.rtner.misc.BinTools;
|
||||
import de.rtner.security.auth.spi.PBKDF2Engine;
|
||||
import de.rtner.security.auth.spi.PBKDF2Parameters;
|
||||
@ -38,13 +39,12 @@ public class Pbkdf2 extends HexSaltedMethod {
|
||||
if (line.length != 4) {
|
||||
return false;
|
||||
}
|
||||
int iterations;
|
||||
try {
|
||||
iterations = Integer.parseInt(line[1]);
|
||||
} catch (NumberFormatException e) {
|
||||
ConsoleLogger.logException("Cannot read number of rounds for Pbkdf2", e);
|
||||
Integer iterations = Ints.tryParse(line[1]);
|
||||
if (iterations == null) {
|
||||
ConsoleLogger.warning("Cannot read number of rounds for Pbkdf2: '" + line[1] + "'");
|
||||
return false;
|
||||
}
|
||||
|
||||
String salt = line[2];
|
||||
byte[] derivedKey = BinTools.hex2bin(line[3]);
|
||||
PBKDF2Parameters params = new PBKDF2Parameters("HmacSHA256", "UTF-8", salt.getBytes(), iterations, derivedKey);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.security.crypts;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
import de.rtner.security.auth.spi.PBKDF2Engine;
|
||||
import de.rtner.security.auth.spi.PBKDF2Parameters;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
@ -27,13 +28,12 @@ public class Pbkdf2Django extends HexSaltedMethod {
|
||||
if (line.length != 4) {
|
||||
return false;
|
||||
}
|
||||
int iterations;
|
||||
try {
|
||||
iterations = Integer.parseInt(line[1]);
|
||||
} catch (NumberFormatException e) {
|
||||
ConsoleLogger.logException("Could not read number of rounds for Pbkdf2Django:", e);
|
||||
Integer iterations = Ints.tryParse(line[1]);
|
||||
if (iterations == null) {
|
||||
ConsoleLogger.warning("Cannot read number of rounds for Pbkdf2Django: '" + line[1] + "'");
|
||||
return false;
|
||||
}
|
||||
|
||||
String salt = line[2];
|
||||
byte[] derivedKey = Base64.getDecoder().decode(line[3]);
|
||||
PBKDF2Parameters params = new PBKDF2Parameters("HmacSHA256", "ASCII", salt.getBytes(), iterations, derivedKey);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.security.totp;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
import com.warrenstrange.googleauth.GoogleAuthenticator;
|
||||
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
|
||||
import com.warrenstrange.googleauth.GoogleAuthenticatorQRGenerator;
|
||||
@ -43,13 +44,8 @@ public class TotpAuthenticator {
|
||||
* @return true if code is valid, false otherwise
|
||||
*/
|
||||
public boolean checkCode(String totpKey, String inputCode) {
|
||||
try {
|
||||
Integer totpCode = Integer.valueOf(inputCode);
|
||||
return authenticator.authorize(totpKey, totpCode);
|
||||
} catch (NumberFormatException e) {
|
||||
// ignore
|
||||
}
|
||||
return false;
|
||||
Integer totpCode = Ints.tryParse(inputCode);
|
||||
return totpCode != null && authenticator.authorize(totpKey, totpCode);
|
||||
}
|
||||
|
||||
public TotpGenerationResult generateTotpKey(Player player) {
|
||||
|
@ -30,10 +30,10 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.returnGivenOnlinePlayers;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
@ -123,7 +123,7 @@ public class OnJoinVerifierTest {
|
||||
List<Player> onlinePlayers = Arrays.asList(mock(Player.class), mock(Player.class));
|
||||
given(permissionsManager.hasPermission(onlinePlayers.get(0), PlayerStatePermission.IS_VIP)).willReturn(true);
|
||||
given(permissionsManager.hasPermission(onlinePlayers.get(1), PlayerStatePermission.IS_VIP)).willReturn(false);
|
||||
returnOnlineListFromBukkitServer(onlinePlayers);
|
||||
returnGivenOnlinePlayers(bukkitService, onlinePlayers);
|
||||
given(server.getMaxPlayers()).willReturn(onlinePlayers.size());
|
||||
given(messages.retrieveSingle(player, MessageKey.KICK_FOR_VIP)).willReturn("kick for vip");
|
||||
|
||||
@ -147,7 +147,7 @@ public class OnJoinVerifierTest {
|
||||
given(permissionsManager.hasPermission(player, PlayerStatePermission.IS_VIP)).willReturn(true);
|
||||
List<Player> onlinePlayers = Collections.singletonList(mock(Player.class));
|
||||
given(permissionsManager.hasPermission(onlinePlayers.get(0), PlayerStatePermission.IS_VIP)).willReturn(true);
|
||||
returnOnlineListFromBukkitServer(onlinePlayers);
|
||||
returnGivenOnlinePlayers(bukkitService, onlinePlayers);
|
||||
given(server.getMaxPlayers()).willReturn(onlinePlayers.size());
|
||||
given(messages.retrieveSingle(player, MessageKey.KICK_FULL_SERVER)).willReturn("kick full server");
|
||||
|
||||
@ -501,13 +501,6 @@ public class OnJoinVerifierTest {
|
||||
onJoinVerifier.checkPlayerCountry(joiningPlayer, ip, false);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void returnOnlineListFromBukkitServer(Collection<Player> onlineList) {
|
||||
// Note ljacqu 20160529: The compiler gets lost in generics because Collection<? extends Player> is returned
|
||||
// from getOnlinePlayers(). We need to uncheck onlineList to a simple Collection or it will refuse to compile.
|
||||
given(bukkitService.getOnlinePlayers()).willReturn((Collection) onlineList);
|
||||
}
|
||||
|
||||
private void expectValidationExceptionWith(MessageKey messageKey, String... args) {
|
||||
expectedException.expect(exceptionWithData(messageKey, args));
|
||||
}
|
||||
|
@ -21,13 +21,13 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.returnGivenOnlinePlayers;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@ -241,7 +241,6 @@ public class AsynchronousLoginTest {
|
||||
return player;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void mockOnlinePlayersInBukkitService() {
|
||||
// 127.0.0.4: albania (online), brazil (offline)
|
||||
Player playerA = mockPlayer("albania");
|
||||
@ -266,8 +265,8 @@ public class AsynchronousLoginTest {
|
||||
Player playerF = mockPlayer("france");
|
||||
TestHelper.mockPlayerIp(playerF, "192.168.0.0");
|
||||
|
||||
Collection onlinePlayers = Arrays.asList(playerA, playerB, playerC, playerD, playerE, playerF);
|
||||
given(bukkitService.getOnlinePlayers()).willReturn(onlinePlayers);
|
||||
List<Player> onlinePlayers = Arrays.asList(playerA, playerB, playerC, playerD, playerE, playerF);
|
||||
returnGivenOnlinePlayers(bukkitService, onlinePlayers);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ import org.mockito.Mock;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.returnGivenOnlinePlayers;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@ -154,11 +155,10 @@ public class AntiBotServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public void shouldInformPlayersOnActivation() {
|
||||
// given - listening antibot
|
||||
List<Player> players = Arrays.asList(mock(Player.class), mock(Player.class));
|
||||
given(bukkitService.getOnlinePlayers()).willReturn((List) players);
|
||||
returnGivenOnlinePlayers(bukkitService, players);
|
||||
given(permissionsManager.hasPermission(players.get(0), AdminPermission.ANTIBOT_MESSAGES)).willReturn(false);
|
||||
given(permissionsManager.hasPermission(players.get(1), AdminPermission.ANTIBOT_MESSAGES)).willReturn(true);
|
||||
|
||||
|
@ -1,7 +1,12 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
|
||||
/**
|
||||
@ -81,4 +86,17 @@ public final class BukkitServiceTestHelper {
|
||||
return null;
|
||||
}).when(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), anyLong());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to return the given players when its method
|
||||
* {@link BukkitService#getOnlinePlayers()} is invoked.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
* @param players the players to return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void returnGivenOnlinePlayers(BukkitService bukkitService, Collection<Player> players) {
|
||||
// The compiler gets lost in generics because Collection<? extends Player> is returned from getOnlinePlayers()
|
||||
given(bukkitService.getOnlinePlayers()).willReturn((Collection) players);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.returnGivenOnlinePlayers;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
@ -68,7 +69,7 @@ public class WelcomeMessageConfigurationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadWelcomeMessage() throws IOException {
|
||||
public void shouldLoadWelcomeMessage() {
|
||||
// given
|
||||
String welcomeMessage = "This is my welcome message for testing\nBye!";
|
||||
setWelcomeMessageAndReload(welcomeMessage);
|
||||
@ -84,7 +85,7 @@ public class WelcomeMessageConfigurationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReplaceNameAndIpAndCountry() throws IOException {
|
||||
public void shouldReplaceNameAndIpAndCountry() {
|
||||
// given
|
||||
String welcomeMessage = "Hello {PLAYER}, your IP is {IP}\nYour country is {COUNTRY}.\nWelcome to {SERVER}!";
|
||||
setWelcomeMessageAndReload(welcomeMessage);
|
||||
@ -108,11 +109,11 @@ public class WelcomeMessageConfigurationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldApplyOtherReplacements() throws IOException {
|
||||
public void shouldApplyOtherReplacements() {
|
||||
// given
|
||||
String welcomeMessage = "{ONLINE}/{MAXPLAYERS} online\n{LOGINS} logged in\nYour world is {WORLD}\nServer: {VERSION}";
|
||||
setWelcomeMessageAndReload(welcomeMessage);
|
||||
given(bukkitService.getOnlinePlayers()).willReturn((List) Arrays.asList(mock(Player.class), mock(Player.class)));
|
||||
returnGivenOnlinePlayers(bukkitService, Arrays.asList(mock(Player.class), mock(Player.class)));
|
||||
given(server.getMaxPlayers()).willReturn(20);
|
||||
given(playerCache.getLogged()).willReturn(1);
|
||||
given(server.getBukkitVersion()).willReturn("Bukkit-456.77.8");
|
||||
|
Loading…
Reference in New Issue
Block a user