Merge pull request #1518 from AuthMe/fixing-session-expired-message-spam

Fixing session expired message spam
This commit is contained in:
HexelDev 2018-03-05 20:42:42 +01:00 committed by GitHub
commit c9b2881a28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 12 deletions

View File

@ -45,11 +45,13 @@ public class SessionService implements Reloadable {
database.setUnlogged(name);
database.revokeSession(name);
PlayerAuth auth = database.getAuth(name);
if (hasValidSessionData(auth, player)) {
SessionState state = fetchSessionStatus(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);
}
}
@ -62,19 +64,26 @@ 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 boolean 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 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) {

View File

@ -0,0 +1,13 @@
package fr.xephi.authme.service;
public enum SessionState {
VALID,
NOT_VALID,
OUTDATED,
IP_CHANGED
}

View File

@ -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);