Don't use metadata for two-factor auth

This commit is contained in:
cnr 2017-01-11 16:17:03 -06:00
parent 6f2705f0e4
commit 91773de97e

View File

@ -1,5 +1,9 @@
package mineplex.core.twofactor;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -21,7 +25,6 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.bukkit.metadata.FixedMetadataValue;
import com.warrenstrange.googleauth.GoogleAuthenticator;
@ -39,8 +42,8 @@ import mineplex.serverdata.database.DBPool;
@ReflectivelyCreateMiniPlugin
public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
{
private static final String TFA_SETUP = "twofactor:setup";
private static final String TFA_AUTHENTICATING = "twofactor:authenticating";
private final Map<UUID, String> setupData = new HashMap<>();
private final Set<UUID> authenticating = new HashSet<>();
private static final GoogleAuthenticator authenticator = new GoogleAuthenticator();
private final CoreClientManager _clientManager = Managers.require(CoreClientManager.class);
@ -106,7 +109,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
player.getInventory().setHeldItemSlot(slot);
player.getInventory().setItemInHand(stack);
player.setMetadata(TFA_SETUP, new FixedMetadataValue(_plugin, secret));
setupData.put(player.getUniqueId(), secret);
player.sendMessage(F.main("2FA", "Setting up two-factor authentication."));
}
@ -127,7 +130,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
{
// Hooray 2FA
player.sendMessage(F.main("2FA", "Please enter your two-factor auth code"));
player.setMetadata(TFA_AUTHENTICATING, new FixedMetadataValue(_plugin, true));
authenticating.add(player.getUniqueId());
}
else
{
@ -139,12 +142,11 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
public void onQuit(PlayerQuitEvent event)
{
Player player = event.getPlayer();
if (player.hasMetadata(TFA_SETUP))
if (setupData.remove(player.getUniqueId()) != null)
{
player.setItemInHand(null);
player.removeMetadata(TFA_SETUP, _plugin);
}
player.removeMetadata(TFA_AUTHENTICATING, _plugin);
authenticating.remove(player.getUniqueId());
}
@EventHandler(priority = EventPriority.LOWEST)
@ -152,12 +154,13 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
{
Player player = event.getPlayer();
String secret = null;
if (player.hasMetadata(TFA_SETUP))
String secret = null; // Check setup data first
if (setupData.containsKey(player.getUniqueId()))
{
secret = player.getMetadata(TFA_SETUP).get(0).asString();
secret = setupData.get(player.getUniqueId());
}
else if (player.hasMetadata(TFA_AUTHENTICATING))
else if (authenticating.contains(player.getUniqueId()))
{
secret = Get(player).getSecretKey().get();
}
@ -192,7 +195,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
player.sendMessage(F.main("2FA", "Authorized for 24 hours."));
if (player.hasMetadata(TFA_SETUP))
if (setupData.containsKey(player.getUniqueId()))
{
// Remove setup map + save secret
player.setItemInHand(null);
@ -220,8 +223,8 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
_repository.saveLogin(player, player.getAddress().getAddress().toString().substring(1));
player.removeMetadata(TFA_SETUP, _plugin);
player.removeMetadata(TFA_AUTHENTICATING, _plugin);
setupData.remove(player.getUniqueId());
authenticating.remove(player.getUniqueId());
}
// Cancel relevant events
@ -230,7 +233,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
public void onChangeHeldItem(PlayerItemHeldEvent event)
{
Player player = event.getPlayer();
if (player.hasMetadata(TFA_SETUP))
if (setupData.containsKey(player.getUniqueId()))
{
event.setCancelled(true);
}
@ -240,7 +243,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
public void onClick(InventoryClickEvent event)
{
Player player = (Player) event.getWhoClicked();
if (player.hasMetadata(TFA_SETUP) || player.hasMetadata(TFA_AUTHENTICATING))
if (setupData.containsKey(player.getUniqueId()) || authenticating.contains(player.getUniqueId()))
{
event.setCancelled(true);
}
@ -250,7 +253,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
public void onDrag(InventoryDragEvent event)
{
Player player = (Player) event.getWhoClicked();
if (player.hasMetadata(TFA_SETUP) || player.hasMetadata(TFA_AUTHENTICATING))
if (setupData.containsKey(player.getUniqueId()) || authenticating.contains(player.getUniqueId()))
{
event.setCancelled(true);
}
@ -260,7 +263,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
public void onDrop(PlayerDropItemEvent event)
{
Player player = event.getPlayer();
if (player.hasMetadata(TFA_SETUP) || player.hasMetadata(TFA_AUTHENTICATING))
if (setupData.containsKey(player.getUniqueId()) || authenticating.contains(player.getUniqueId()))
{
event.setCancelled(true);
}
@ -270,7 +273,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
public void onInteract(PlayerInteractEvent event)
{
Player player = event.getPlayer();
if (player.hasMetadata(TFA_SETUP) || player.hasMetadata(TFA_AUTHENTICATING))
if (setupData.containsKey(player.getUniqueId()) || authenticating.contains(player.getUniqueId()))
{
event.setCancelled(true);
}
@ -280,7 +283,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
public void onInteractWithEntity(PlayerInteractEntityEvent event)
{
Player player = event.getPlayer();
if (player.hasMetadata(TFA_SETUP) || player.hasMetadata(TFA_AUTHENTICATING))
if (setupData.containsKey(player.getUniqueId()) || authenticating.contains(player.getUniqueId()))
{
event.setCancelled(true);
}
@ -290,7 +293,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
public void onCommand(PlayerCommandPreprocessEvent event)
{
Player player = event.getPlayer();
if (player.hasMetadata(TFA_SETUP) || player.hasMetadata(TFA_AUTHENTICATING))
if (setupData.containsKey(player.getUniqueId()) || authenticating.contains(player.getUniqueId()))
{
event.setMessage("/");
event.setCancelled(true);
@ -301,7 +304,7 @@ public class TwoFactorAuth extends MiniClientPlugin<TwoFactorData>
public void onMove(PlayerMoveEvent event)
{
Player player = event.getPlayer();
if (player.hasMetadata(TFA_SETUP) || player.hasMetadata(TFA_AUTHENTICATING))
if (setupData.containsKey(player.getUniqueId()) || authenticating.contains(player.getUniqueId()))
{
if (Recharge.Instance.use(player, "two-factor message cooldown", 3000L, false, false))
{