114 lines
3.0 KiB
Java
114 lines
3.0 KiB
Java
package eu.oskar3123.spigot2fa.handler;
|
|
|
|
import eu.oskar3123.spigot2fa.Main;
|
|
import eu.oskar3123.spigot2fa.map.QRMapRenderer;
|
|
import eu.oskar3123.spigot2fa.tfa.TFA;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.configuration.Configuration;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.meta.MapMeta;
|
|
import org.bukkit.map.MapView;
|
|
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
public class TFAHandler
|
|
{
|
|
|
|
private Main plugin;
|
|
private Map<UUID, String> isInProcess = new HashMap<>();
|
|
|
|
public TFAHandler(Main plugin)
|
|
{
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
public String formatSecret(String secret)
|
|
{
|
|
return secret.toLowerCase().replaceAll("(.{4})(?=.{4})", "$1 ");
|
|
}
|
|
|
|
public boolean hasEnabled2FA(UUID uuid)
|
|
{
|
|
return plugin.configHandler.getConfig(plugin.players).isString(uuid.toString());
|
|
}
|
|
|
|
public void creatingSuccess(Player player)
|
|
{
|
|
String secret = remove(player.getUniqueId());
|
|
plugin.configHandler.getConfig(plugin.players).set(player.getUniqueId().toString(), secret);
|
|
plugin.configHandler.saveConfig(plugin.players);
|
|
}
|
|
|
|
public void creatingFailed(Player player)
|
|
{
|
|
remove(player.getUniqueId());
|
|
}
|
|
|
|
public boolean matchCode(String code1, String code2)
|
|
{
|
|
return code1.equalsIgnoreCase(code2);
|
|
}
|
|
|
|
public boolean matchCode(Player player, String code)
|
|
{
|
|
String actualCode = TFA.getTOTPCode(getKey(player.getUniqueId()));
|
|
return matchCode(actualCode, code);
|
|
}
|
|
|
|
private String remove(UUID uuid)
|
|
{
|
|
return isInProcess.remove(uuid);
|
|
}
|
|
|
|
public String startCreating(Player player)
|
|
{
|
|
String key = TFA.getRandomSecretKey();
|
|
isInProcess.put(player.getUniqueId(), key);
|
|
showQRCode(player, key);
|
|
return key;
|
|
}
|
|
|
|
private void showQRCode(Player player, String secret)
|
|
{
|
|
try
|
|
{
|
|
ItemStack map = new ItemStack(Material.FILLED_MAP);
|
|
MapView view = Bukkit.createMap(player.getWorld());
|
|
view.getRenderers().clear();
|
|
view.addRenderer(new QRMapRenderer(secret, player));
|
|
MapMeta mapMeta = (MapMeta) map.getItemMeta();
|
|
mapMeta.setMapId(view.getId());
|
|
map.setItemMeta(mapMeta);
|
|
player.getInventory().setItemInMainHand(map);
|
|
player.sendMap(view);
|
|
player.sendMessage("Secret Key: " + secret);
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
e.printStackTrace();
|
|
player.sendMessage("Failed to generate qr code");
|
|
}
|
|
}
|
|
|
|
public boolean isInProcess(UUID uuid)
|
|
{
|
|
return isInProcess.containsKey(uuid);
|
|
}
|
|
|
|
public String getKey(UUID uuid)
|
|
{
|
|
if (isInProcess.containsKey(uuid))
|
|
{
|
|
return isInProcess.get(uuid);
|
|
}
|
|
Configuration players = plugin.configHandler.getConfig(plugin.players);
|
|
return players.getString(uuid.toString());
|
|
}
|
|
|
|
}
|