From 5abc9b9d452c2e39e407086e2b7931e3a2554889 Mon Sep 17 00:00:00 2001 From: HexelDev Date: Sun, 4 Mar 2018 18:36:21 +0100 Subject: [PATCH 1/5] 'session expired' message spam fix --- .../xephi/authme/service/SessionService.java | 24 ++++++++++++------- .../fr/xephi/authme/service/SessionState.java | 13 ++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/service/SessionState.java diff --git a/src/main/java/fr/xephi/authme/service/SessionService.java b/src/main/java/fr/xephi/authme/service/SessionService.java index dd676cc87..87f1bfc7d 100644 --- a/src/main/java/fr/xephi/authme/service/SessionService.java +++ b/src/main/java/fr/xephi/authme/service/SessionService.java @@ -45,11 +45,12 @@ public class SessionService implements Reloadable { database.setUnlogged(name); database.revokeSession(name); PlayerAuth auth = database.getAuth(name); - if (hasValidSessionData(auth, player)) { + SessionState state = hasValidSessionData(auth, player); + if(state.equals(SessionState.VALID)) { RestoreSessionEvent event = bukkitService.createAndCallEvent( isAsync -> new RestoreSessionEvent(player, isAsync)); return !event.isCancelled(); - } else { + } else if(state.equals(SessionState.IP_CHANGED)) { service.send(player, MessageKey.SESSION_EXPIRED); } } @@ -64,17 +65,24 @@ public class SessionService implements Reloadable { * @param player the associated player * @return true if the player may resume his login session, false otherwise */ - private boolean hasValidSessionData(PlayerAuth auth, Player player) { + private SessionState hasValidSessionData(PlayerAuth auth, Player player) { if (auth == null) { ConsoleLogger.warning("No PlayerAuth in database for '" + player.getName() + "' during session check"); - return false; + return SessionState.NOT_VALID; } else if (auth.getLastLogin() == null) { - return false; + return SessionState.NOT_VALID; } long timeSinceLastLogin = System.currentTimeMillis() - auth.getLastLogin(); - return PlayerUtils.getPlayerIp(player).equals(auth.getLastIp()) - && timeSinceLastLogin > 0 - && timeSinceLastLogin < service.getProperty(PluginSettings.SESSIONS_TIMEOUT) * MILLIS_PER_MINUTE; + + if(timeSinceLastLogin > 0 + && timeSinceLastLogin < service.getProperty(PluginSettings.SESSIONS_TIMEOUT) * MILLIS_PER_MINUTE) { + if(PlayerUtils.getPlayerIp(player).equals(auth.getLastIp())) { + return SessionState.VALID; + } else { + return SessionState.IP_CHANGED; + } + } + return SessionState.OUTDATED; } public void grantSession(String name) { diff --git a/src/main/java/fr/xephi/authme/service/SessionState.java b/src/main/java/fr/xephi/authme/service/SessionState.java new file mode 100644 index 000000000..801f36bc8 --- /dev/null +++ b/src/main/java/fr/xephi/authme/service/SessionState.java @@ -0,0 +1,13 @@ +package fr.xephi.authme.service; + +public enum SessionState { + + VALID, + + NOT_VALID, + + OUTDATED, + + IP_CHANGED + +} From 9db38a3bf4a840b5685015dd8e43fb12bc1194c3 Mon Sep 17 00:00:00 2001 From: HexelDev Date: Sun, 4 Mar 2018 18:38:46 +0100 Subject: [PATCH 2/5] SessionService#hasValidSessionData description update --- src/main/java/fr/xephi/authme/service/SessionService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/fr/xephi/authme/service/SessionService.java b/src/main/java/fr/xephi/authme/service/SessionService.java index 87f1bfc7d..27f31b850 100644 --- a/src/main/java/fr/xephi/authme/service/SessionService.java +++ b/src/main/java/fr/xephi/authme/service/SessionService.java @@ -63,7 +63,7 @@ public class SessionService implements Reloadable { * * @param auth the player auth * @param player the associated player - * @return true if the player may resume his login session, false otherwise + * @return SessionState based on the state of the session (VALID, NOT_VALID, OUTDATED, IP_CHANGED) */ private SessionState hasValidSessionData(PlayerAuth auth, Player player) { if (auth == null) { From be2c6ae1162f209780ffd4b457d9a577aefb7d17 Mon Sep 17 00:00:00 2001 From: HexelDev Date: Sun, 4 Mar 2018 19:23:08 +0100 Subject: [PATCH 3/5] Fixing tests --- src/main/java/fr/xephi/authme/service/SessionService.java | 1 + .../java/fr/xephi/authme/service/SessionServiceTest.java | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/xephi/authme/service/SessionService.java b/src/main/java/fr/xephi/authme/service/SessionService.java index 27f31b850..f9f43948e 100644 --- a/src/main/java/fr/xephi/authme/service/SessionService.java +++ b/src/main/java/fr/xephi/authme/service/SessionService.java @@ -45,6 +45,7 @@ public class SessionService implements Reloadable { database.setUnlogged(name); database.revokeSession(name); PlayerAuth auth = database.getAuth(name); + SessionState state = hasValidSessionData(auth, player); if(state.equals(SessionState.VALID)) { RestoreSessionEvent event = bukkitService.createAndCallEvent( diff --git a/src/test/java/fr/xephi/authme/service/SessionServiceTest.java b/src/test/java/fr/xephi/authme/service/SessionServiceTest.java index 754487c15..441b53561 100644 --- a/src/test/java/fr/xephi/authme/service/SessionServiceTest.java +++ b/src/test/java/fr/xephi/authme/service/SessionServiceTest.java @@ -106,7 +106,6 @@ public class SessionServiceTest { // then assertThat(result, equalTo(false)); verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED); - verify(commonService).send(player, MessageKey.SESSION_EXPIRED); verify(dataSource).hasSession(name); verify(dataSource).setUnlogged(name); verify(dataSource).revokeSession(name); @@ -132,7 +131,6 @@ public class SessionServiceTest { // then assertThat(result, equalTo(false)); verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED); - verify(commonService).send(player, MessageKey.SESSION_EXPIRED); verify(dataSource).hasSession(name); verify(dataSource).setUnlogged(name); verify(dataSource).revokeSession(name); @@ -145,9 +143,10 @@ public class SessionServiceTest { String ip = "127.3.12.15"; Player player = mockPlayerWithNameAndIp(name, ip); given(dataSource.hasSession(name)).willReturn(true); + given(commonService.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(8); PlayerAuth auth = PlayerAuth.builder() .name(name) - .lastLogin(System.currentTimeMillis()) + .lastLogin(System.currentTimeMillis() - 7 * 60 * 1000) .lastIp("8.8.8.8").build(); given(dataSource.getAuth(name)).willReturn(auth); From 57809194f32f0623c26fbd0ceca96d5886851aa8 Mon Sep 17 00:00:00 2001 From: HexelDev Date: Sun, 4 Mar 2018 19:45:05 +0100 Subject: [PATCH 4/5] Renaming function hasValidSessionData -> fetchSessionStatus --- src/main/java/fr/xephi/authme/service/SessionService.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/xephi/authme/service/SessionService.java b/src/main/java/fr/xephi/authme/service/SessionService.java index f9f43948e..d530f5855 100644 --- a/src/main/java/fr/xephi/authme/service/SessionService.java +++ b/src/main/java/fr/xephi/authme/service/SessionService.java @@ -46,7 +46,7 @@ public class SessionService implements Reloadable { database.revokeSession(name); PlayerAuth auth = database.getAuth(name); - SessionState state = hasValidSessionData(auth, player); + SessionState state = fetchSessionStatus(auth, player); if(state.equals(SessionState.VALID)) { RestoreSessionEvent event = bukkitService.createAndCallEvent( isAsync -> new RestoreSessionEvent(player, isAsync)); @@ -66,8 +66,9 @@ public class SessionService implements Reloadable { * @param player the associated player * @return SessionState based on the state of the session (VALID, NOT_VALID, OUTDATED, IP_CHANGED) */ - private SessionState hasValidSessionData(PlayerAuth auth, Player player) { + private SessionState fetchSessionStatus(PlayerAuth auth, Player player) { if (auth == null) { + ConsoleLogger.warning("No PlayerAuth in database for '" + player.getName() + "' during session check"); return SessionState.NOT_VALID; } else if (auth.getLastLogin() == null) { From 11d039c818861d2bb3f83f188d0b464998051e23 Mon Sep 17 00:00:00 2001 From: HexelDev Date: Mon, 5 Mar 2018 20:20:25 +0100 Subject: [PATCH 5/5] space after 'if'(s) --- .../java/fr/xephi/authme/service/SessionService.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/fr/xephi/authme/service/SessionService.java b/src/main/java/fr/xephi/authme/service/SessionService.java index d530f5855..f821a64b3 100644 --- a/src/main/java/fr/xephi/authme/service/SessionService.java +++ b/src/main/java/fr/xephi/authme/service/SessionService.java @@ -47,11 +47,11 @@ public class SessionService implements Reloadable { PlayerAuth auth = database.getAuth(name); SessionState state = fetchSessionStatus(auth, player); - if(state.equals(SessionState.VALID)) { + if (state.equals(SessionState.VALID)) { RestoreSessionEvent event = bukkitService.createAndCallEvent( isAsync -> new RestoreSessionEvent(player, isAsync)); return !event.isCancelled(); - } else if(state.equals(SessionState.IP_CHANGED)) { + } else if (state.equals(SessionState.IP_CHANGED)) { service.send(player, MessageKey.SESSION_EXPIRED); } } @@ -68,7 +68,6 @@ public class SessionService implements Reloadable { */ private SessionState fetchSessionStatus(PlayerAuth auth, Player player) { if (auth == null) { - ConsoleLogger.warning("No PlayerAuth in database for '" + player.getName() + "' during session check"); return SessionState.NOT_VALID; } else if (auth.getLastLogin() == null) { @@ -76,9 +75,9 @@ public class SessionService implements Reloadable { } long timeSinceLastLogin = System.currentTimeMillis() - auth.getLastLogin(); - if(timeSinceLastLogin > 0 + if (timeSinceLastLogin > 0 && timeSinceLastLogin < service.getProperty(PluginSettings.SESSIONS_TIMEOUT) * MILLIS_PER_MINUTE) { - if(PlayerUtils.getPlayerIp(player).equals(auth.getLastIp())) { + if (PlayerUtils.getPlayerIp(player).equals(auth.getLastIp())) { return SessionState.VALID; } else { return SessionState.IP_CHANGED;