mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2025-01-04 15:07:49 +01:00
Merge pull request #1518 from AuthMe/fixing-session-expired-message-spam
Fixing session expired message spam
This commit is contained in:
commit
c9b2881a28
@ -45,11 +45,13 @@ public class SessionService implements Reloadable {
|
|||||||
database.setUnlogged(name);
|
database.setUnlogged(name);
|
||||||
database.revokeSession(name);
|
database.revokeSession(name);
|
||||||
PlayerAuth auth = database.getAuth(name);
|
PlayerAuth auth = database.getAuth(name);
|
||||||
if (hasValidSessionData(auth, player)) {
|
|
||||||
|
SessionState state = fetchSessionStatus(auth, player);
|
||||||
|
if (state.equals(SessionState.VALID)) {
|
||||||
RestoreSessionEvent event = bukkitService.createAndCallEvent(
|
RestoreSessionEvent event = bukkitService.createAndCallEvent(
|
||||||
isAsync -> new RestoreSessionEvent(player, isAsync));
|
isAsync -> new RestoreSessionEvent(player, isAsync));
|
||||||
return !event.isCancelled();
|
return !event.isCancelled();
|
||||||
} else {
|
} else if (state.equals(SessionState.IP_CHANGED)) {
|
||||||
service.send(player, MessageKey.SESSION_EXPIRED);
|
service.send(player, MessageKey.SESSION_EXPIRED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,19 +64,26 @@ public class SessionService implements Reloadable {
|
|||||||
*
|
*
|
||||||
* @param auth the player auth
|
* @param auth the player auth
|
||||||
* @param player the associated player
|
* @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) {
|
if (auth == null) {
|
||||||
ConsoleLogger.warning("No PlayerAuth in database for '" + player.getName() + "' during session check");
|
ConsoleLogger.warning("No PlayerAuth in database for '" + player.getName() + "' during session check");
|
||||||
return false;
|
return SessionState.NOT_VALID;
|
||||||
} else if (auth.getLastLogin() == null) {
|
} else if (auth.getLastLogin() == null) {
|
||||||
return false;
|
return SessionState.NOT_VALID;
|
||||||
}
|
}
|
||||||
long timeSinceLastLogin = System.currentTimeMillis() - auth.getLastLogin();
|
long timeSinceLastLogin = System.currentTimeMillis() - auth.getLastLogin();
|
||||||
return PlayerUtils.getPlayerIp(player).equals(auth.getLastIp())
|
|
||||||
&& timeSinceLastLogin > 0
|
if (timeSinceLastLogin > 0
|
||||||
&& timeSinceLastLogin < service.getProperty(PluginSettings.SESSIONS_TIMEOUT) * MILLIS_PER_MINUTE;
|
&& 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) {
|
public void grantSession(String name) {
|
||||||
|
13
src/main/java/fr/xephi/authme/service/SessionState.java
Normal file
13
src/main/java/fr/xephi/authme/service/SessionState.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package fr.xephi.authme.service;
|
||||||
|
|
||||||
|
public enum SessionState {
|
||||||
|
|
||||||
|
VALID,
|
||||||
|
|
||||||
|
NOT_VALID,
|
||||||
|
|
||||||
|
OUTDATED,
|
||||||
|
|
||||||
|
IP_CHANGED
|
||||||
|
|
||||||
|
}
|
@ -106,7 +106,6 @@ public class SessionServiceTest {
|
|||||||
// then
|
// then
|
||||||
assertThat(result, equalTo(false));
|
assertThat(result, equalTo(false));
|
||||||
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
||||||
verify(commonService).send(player, MessageKey.SESSION_EXPIRED);
|
|
||||||
verify(dataSource).hasSession(name);
|
verify(dataSource).hasSession(name);
|
||||||
verify(dataSource).setUnlogged(name);
|
verify(dataSource).setUnlogged(name);
|
||||||
verify(dataSource).revokeSession(name);
|
verify(dataSource).revokeSession(name);
|
||||||
@ -132,7 +131,6 @@ public class SessionServiceTest {
|
|||||||
// then
|
// then
|
||||||
assertThat(result, equalTo(false));
|
assertThat(result, equalTo(false));
|
||||||
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
verify(commonService).getProperty(PluginSettings.SESSIONS_ENABLED);
|
||||||
verify(commonService).send(player, MessageKey.SESSION_EXPIRED);
|
|
||||||
verify(dataSource).hasSession(name);
|
verify(dataSource).hasSession(name);
|
||||||
verify(dataSource).setUnlogged(name);
|
verify(dataSource).setUnlogged(name);
|
||||||
verify(dataSource).revokeSession(name);
|
verify(dataSource).revokeSession(name);
|
||||||
@ -145,9 +143,10 @@ public class SessionServiceTest {
|
|||||||
String ip = "127.3.12.15";
|
String ip = "127.3.12.15";
|
||||||
Player player = mockPlayerWithNameAndIp(name, ip);
|
Player player = mockPlayerWithNameAndIp(name, ip);
|
||||||
given(dataSource.hasSession(name)).willReturn(true);
|
given(dataSource.hasSession(name)).willReturn(true);
|
||||||
|
given(commonService.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(8);
|
||||||
PlayerAuth auth = PlayerAuth.builder()
|
PlayerAuth auth = PlayerAuth.builder()
|
||||||
.name(name)
|
.name(name)
|
||||||
.lastLogin(System.currentTimeMillis())
|
.lastLogin(System.currentTimeMillis() - 7 * 60 * 1000)
|
||||||
.lastIp("8.8.8.8").build();
|
.lastIp("8.8.8.8").build();
|
||||||
given(dataSource.getAuth(name)).willReturn(auth);
|
given(dataSource.getAuth(name)).willReturn(auth);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user