From e5e815a885e94aef20797d1bc39c8dcd9374f1ee Mon Sep 17 00:00:00 2001 From: games647 Date: Mon, 5 Feb 2018 15:01:28 +0100 Subject: [PATCH] Cancel autologin for AuthMe sessions (Fixes #189, #148, #103) --- .../fastlogin/bukkit/hooks/AuthMeHook.java | 34 +++++++++++++++++-- .../bukkit/hooks/CrazyLoginHook.java | 13 +++++-- .../fastlogin/bukkit/hooks/UltraAuthHook.java | 10 ++++-- .../fastlogin/bukkit/hooks/xAuthHook.java | 12 +++++-- 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/AuthMeHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/AuthMeHook.java index d2a9405e..374ac8e3 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/AuthMeHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/AuthMeHook.java @@ -1,10 +1,20 @@ package com.github.games647.fastlogin.bukkit.hooks; +import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.core.hooks.AuthPlugin; +import com.google.common.collect.Sets; import fr.xephi.authme.api.v3.AuthMeApi; +import fr.xephi.authme.events.RestoreSessionEvent; + +import java.util.Set; +import java.util.UUID; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; /** * Github: https://github.com/Xephi/AuthMeReloaded/ @@ -15,12 +25,31 @@ import org.bukkit.entity.Player; *

* Spigot: https://www.spigotmc.org/resources/authme-reloaded.6269/ */ -public class AuthMeHook implements AuthPlugin { +public class AuthMeHook implements AuthPlugin, Listener { + + private final Set sessionLogins = Sets.newConcurrentHashSet(); + private final FastLoginBukkit plugin; + + public AuthMeHook(FastLoginBukkit plugin) { + this.plugin = plugin; + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) + public void onRestoreSession(RestoreSessionEvent restoreSessionEvent) { + UUID uniqueId = restoreSessionEvent.getPlayer().getUniqueId(); + sessionLogins.add(uniqueId); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent quitEvent) { + UUID uniqueId = quitEvent.getPlayer().getUniqueId(); + sessionLogins.remove(uniqueId); + } @Override public boolean forceLogin(Player player) { //skips registration and login - if (AuthMeApi.getInstance().isAuthenticated(player)) { + if (AuthMeApi.getInstance().isAuthenticated(player) || sessionLogins.contains(player.getUniqueId())) { return false; } else { AuthMeApi.getInstance().forceLogin(player); @@ -38,7 +67,6 @@ public class AuthMeHook implements AuthPlugin { public boolean forceRegister(Player player, String password) { //this automatically registers the player too AuthMeApi.getInstance().forceRegister(player, password); - return true; } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/CrazyLoginHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/CrazyLoginHook.java index 5e227f6b..5867e37e 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/CrazyLoginHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/CrazyLoginHook.java @@ -1,5 +1,6 @@ package com.github.games647.fastlogin.bukkit.hooks; +import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.core.hooks.AuthPlugin; import de.st_ddt.crazylogin.CrazyLogin; @@ -26,13 +27,19 @@ import org.bukkit.entity.Player; */ public class CrazyLoginHook implements AuthPlugin { + private final FastLoginBukkit plugin; + private final CrazyLogin crazyLoginPlugin = CrazyLogin.getPlugin(); private final PlayerListener playerListener = getListener(); + public CrazyLoginHook(FastLoginBukkit plugin) { + this.plugin = plugin; + } + @Override public boolean forceLogin(Player player) { //not thread-safe operation - Future> future = Bukkit.getScheduler().callSyncMethod(crazyLoginPlugin, () -> { + Future> future = Bukkit.getScheduler().callSyncMethod(plugin, () -> { LoginPlayerData playerData = crazyLoginPlugin.getPlayerData(player); if (playerData != null) { //mark the account as logged in @@ -71,7 +78,7 @@ public class CrazyLoginHook implements AuthPlugin { return true; } } catch (InterruptedException | ExecutionException ex) { - crazyLoginPlugin.getLogger().log(Level.SEVERE, "Failed to forceLogin", ex); + plugin.getLogger().log(Level.SEVERE, "Failed to forceLogin", ex); return false; } @@ -105,7 +112,7 @@ public class CrazyLoginHook implements AuthPlugin { try { listener = (PlayerListener) FieldUtils.readField(crazyLoginPlugin, "playerListener", true); } catch (IllegalAccessException ex) { - crazyLoginPlugin.getLogger().log(Level.SEVERE, "Failed to get the listener instance for auto login", ex); + plugin.getLogger().log(Level.SEVERE, "Failed to get the listener instance for auto login", ex); listener = null; } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/UltraAuthHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/UltraAuthHook.java index 2734b743..7f5f1987 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/UltraAuthHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/UltraAuthHook.java @@ -1,5 +1,6 @@ package com.github.games647.fastlogin.bukkit.hooks; +import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.core.hooks.AuthPlugin; import java.util.concurrent.ExecutionException; @@ -23,11 +24,16 @@ import ultraauth.managers.PlayerManager; public class UltraAuthHook implements AuthPlugin { private final Plugin ultraAuthPlugin = Main.main; + private final FastLoginBukkit plugin; + + public UltraAuthHook(FastLoginBukkit plugin) { + this.plugin = plugin; + } @Override public boolean forceLogin(Player player) { //not thread-safe - Future future = Bukkit.getScheduler().callSyncMethod(ultraAuthPlugin, () -> { + Future future = Bukkit.getScheduler().callSyncMethod(plugin, () -> { if (UltraAuthAPI.isAuthenticated(player)) { return true; } @@ -39,7 +45,7 @@ public class UltraAuthHook implements AuthPlugin { try { return future.get(); } catch (InterruptedException | ExecutionException ex) { - ultraAuthPlugin.getLogger().log(Level.SEVERE, "Failed to forceLogin", ex); + plugin.getLogger().log(Level.SEVERE, "Failed to forceLogin", ex); return false; } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/xAuthHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/xAuthHook.java index 0184d21b..29aef6ec 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/xAuthHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/xAuthHook.java @@ -1,5 +1,6 @@ package com.github.games647.fastlogin.bukkit.hooks; +import com.github.games647.fastlogin.bukkit.FastLoginBukkit; import com.github.games647.fastlogin.core.hooks.AuthPlugin; import de.luricos.bukkit.xAuth.xAuth; @@ -22,11 +23,16 @@ import org.bukkit.entity.Player; public class xAuthHook implements AuthPlugin { private final xAuth xAuthPlugin = xAuth.getPlugin(); + private final FastLoginBukkit plugin; + + public xAuthHook(FastLoginBukkit plugin) { + this.plugin = plugin; + } @Override public boolean forceLogin(Player player) { //not thread-safe - Future future = Bukkit.getScheduler().callSyncMethod(xAuthPlugin, () -> { + Future future = Bukkit.getScheduler().callSyncMethod(plugin, () -> { xAuthPlayer xAuthPlayer = xAuthPlugin.getPlayerManager().getPlayer(player); if (xAuthPlayer != null) { if (xAuthPlayer.isAuthenticated()) { @@ -46,7 +52,7 @@ public class xAuthHook implements AuthPlugin { try { return future.get(); } catch (InterruptedException | ExecutionException ex) { - xAuthPlugin.getLogger().log(Level.SEVERE, "Failed to forceLogin", ex); + plugin.getLogger().log(Level.SEVERE, "Failed to forceLogin", ex); return false; } } @@ -74,7 +80,7 @@ public class xAuthHook implements AuthPlugin { //login in the player after registration return future.get() && forceLogin(player); } catch (InterruptedException | ExecutionException ex) { - xAuthPlugin.getLogger().log(Level.SEVERE, "Failed to forceLogin", ex); + plugin.getLogger().log(Level.SEVERE, "Failed to forceLogin", ex); return false; } }