From 3411450ff1a6b4c051aa344842c310166363b360 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Tue, 14 Jun 2016 21:03:32 +0200 Subject: [PATCH] #760 Fix single session feature - Move the check from PlayerLoginEvent to AsyncPlayerPreLoginEvent. Single session can only be implemented with PreLoginEvent; it is already to late to check this in the PlayerLoginEvent. Ergo, we cannot offer this for CraftBukkit. - Remove interactions with LimboCache - no interactions with LimboCache expected until after OnJoinVerification checks. (Thanks sgdc3!) --- .../authme/listener/AuthMePlayerListener.java | 4 +++- .../xephi/authme/listener/OnJoinVerifier.java | 18 +++--------------- .../authme/listener/OnJoinVerifierTest.java | 19 +++++++------------ 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index 67e57ee8b..84fff695d 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -217,6 +217,9 @@ public class AuthMePlayerListener implements Listener { onJoinVerifier.checkAntibot(name, isAuthAvailable); onJoinVerifier.checkKickNonRegistered(isAuthAvailable); onJoinVerifier.checkIsValidName(name); + // Note #760: Single session must be checked here - checking with PlayerLoginEvent is too late and + // the first connection will have been kicked. This means this feature doesn't work on CraftBukkit. + onJoinVerifier.checkSingleSession(name); } catch (FailedVerificationException e) { event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs())); event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); @@ -243,7 +246,6 @@ public class AuthMePlayerListener implements Listener { onJoinVerifier.checkKickNonRegistered(isAuthAvailable); onJoinVerifier.checkIsValidName(name); onJoinVerifier.checkNameCasing(player, auth); - onJoinVerifier.checkSingleSession(player); onJoinVerifier.checkPlayerCountry(isAuthAvailable, event); } catch (FailedVerificationException e) { event.setKickMessage(m.retrieveSingle(e.getReason(), e.getArgs())); diff --git a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java index 64f8c3f7c..297e74c6c 100644 --- a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java +++ b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java @@ -3,9 +3,6 @@ package fr.xephi.authme.listener; import fr.xephi.authme.AntiBot; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.cache.limbo.LimboCache; -import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.initialization.Reloadable; import fr.xephi.authme.output.MessageKey; @@ -18,7 +15,6 @@ import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.util.BukkitService; import fr.xephi.authme.util.StringUtils; -import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.ValidationService; import org.bukkit.Server; import org.bukkit.entity.Player; @@ -49,8 +45,6 @@ class OnJoinVerifier implements Reloadable { @Inject private BukkitService bukkitService; @Inject - private LimboCache limboCache; - @Inject private Server server; private Pattern nicknamePattern; @@ -187,21 +181,15 @@ class OnJoinVerifier implements Reloadable { * Checks if a player with the same name (case-insensitive) is already playing and refuses the * connection if so configured. * - * @param player the player to verify + * @param name the player name to check */ - public void checkSingleSession(Player player) throws FailedVerificationException { + public void checkSingleSession(String name) throws FailedVerificationException { if (!settings.getProperty(RestrictionSettings.FORCE_SINGLE_SESSION)) { return; } - Player onlinePlayer = bukkitService.getPlayerExact(player.getName()); + Player onlinePlayer = bukkitService.getPlayerExact(name); if (onlinePlayer != null) { - String name = player.getName().toLowerCase(); - LimboPlayer limbo = limboCache.getLimboPlayer(name); - if (limbo != null && PlayerCache.getInstance().isAuthenticated(name)) { - Utils.addNormal(player, limbo.getGroup()); - limboCache.deleteLimboPlayer(name); - } throw new FailedVerificationException(MessageKey.USERNAME_ALREADY_ONLINE_ERROR); } } diff --git a/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java b/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java index 1cf9d0c1e..6d5d27e44 100644 --- a/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java +++ b/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java @@ -3,7 +3,6 @@ package fr.xephi.authme.listener; import fr.xephi.authme.AntiBot; import fr.xephi.authme.TestHelper; import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; @@ -67,8 +66,6 @@ public class OnJoinVerifierTest { @Mock private BukkitService bukkitService; @Mock - private LimboCache limboCache; - @Mock private Server server; @Rule @@ -341,21 +338,21 @@ public class OnJoinVerifierTest { @Test public void shouldAcceptNameThatIsNotOnline() throws FailedVerificationException { // given - Player player = newPlayerWithName("bobby"); + String name = "bobby"; given(settings.getProperty(RestrictionSettings.FORCE_SINGLE_SESSION)).willReturn(true); given(bukkitService.getPlayerExact("bobby")).willReturn(null); // when - onJoinVerifier.checkSingleSession(player); + onJoinVerifier.checkSingleSession(name); // then - verifyZeroInteractions(limboCache); + verify(bukkitService).getPlayerExact(name); } @Test public void shouldRejectNameAlreadyOnline() throws FailedVerificationException { // given - Player player = newPlayerWithName("Charlie"); + String name = "Charlie"; Player onlinePlayer = newPlayerWithName("charlie"); given(bukkitService.getPlayerExact("Charlie")).willReturn(onlinePlayer); given(settings.getProperty(RestrictionSettings.FORCE_SINGLE_SESSION)).willReturn(true); @@ -364,22 +361,20 @@ public class OnJoinVerifierTest { expectValidationExceptionWith(MessageKey.USERNAME_ALREADY_ONLINE_ERROR); // when / then - onJoinVerifier.checkSingleSession(player); - verify(limboCache).getLimboPlayer("charlie"); + onJoinVerifier.checkSingleSession(name); } @Test public void shouldAcceptAlreadyOnlineNameForDisabledSetting() throws FailedVerificationException { // given - Player player = newPlayerWithName("Felipe"); + String name = "Felipe"; given(settings.getProperty(RestrictionSettings.FORCE_SINGLE_SESSION)).willReturn(false); // when - onJoinVerifier.checkSingleSession(player); + onJoinVerifier.checkSingleSession(name); // then verifyZeroInteractions(bukkitService); - verifyZeroInteractions(limboCache); } private static Player newPlayerWithName(String name) {