191 lines
6.7 KiB
Java
191 lines
6.7 KiB
Java
package eu.oskar3123.spigot2fa.command;
|
|
|
|
import eu.oskar3123.spigot2fa.Main;
|
|
import eu.oskar3123.spigot2fa.handler.TFAHandler;
|
|
import eu.oskar3123.spigot2fa.util.PlayerUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.bukkit.OfflinePlayer;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandExecutor;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.command.TabCompleter;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.util.StringUtil;
|
|
|
|
import java.util.*;
|
|
|
|
public class TFACommand implements CommandExecutor, TabCompleter
|
|
{
|
|
|
|
private Main plugin;
|
|
private TFAHandler th;
|
|
private final Map<String, String[]> TAB_COMPLETE_MAP = new HashMap<>();
|
|
{
|
|
TAB_COMPLETE_MAP.put("", new String[]{"activate", "remove", "<code>"});
|
|
TAB_COMPLETE_MAP.put(":remove", new String[]{"[player]"});
|
|
}
|
|
|
|
public TFACommand(Main plugin)
|
|
{
|
|
this.plugin = plugin;
|
|
this.th = plugin.tfaHandler;
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
|
|
{
|
|
if (!(sender instanceof Player))
|
|
{
|
|
if (args.length >= 2 && args[0].equalsIgnoreCase("remove"))
|
|
{
|
|
OfflinePlayer otherPlayer = PlayerUtils.getOfflinePlayer(args[1]);
|
|
if (otherPlayer == null)
|
|
{
|
|
sender.sendMessage("Could not find player \"" + args[1] + "\".");
|
|
return true;
|
|
}
|
|
if (th.hasEnabled2FA(otherPlayer.getUniqueId()))
|
|
{
|
|
th.remove2FA(otherPlayer.getUniqueId());
|
|
sender.sendMessage("Removed two-factor authentication for " + otherPlayer.getName() + ".");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
sender.sendMessage(otherPlayer.getName() + " does not have two-factor authentication enabled.");
|
|
return true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sender.sendMessage("Usage: /<command> remove <player>".replace("<command>", label));
|
|
return true;
|
|
}
|
|
}
|
|
Player player = (Player) sender;
|
|
if (args.length >= 1 && args[0].equalsIgnoreCase("remove"))
|
|
{
|
|
if (!player.hasPermission("2fa.remove"))
|
|
{
|
|
player.sendMessage("You don't have permission to do that");
|
|
return true;
|
|
}
|
|
if (args.length >= 2 && player.hasPermission("2fa.remove.other"))
|
|
{
|
|
OfflinePlayer otherPlayer = PlayerUtils.getOfflinePlayer(args[1]);
|
|
if (otherPlayer == null)
|
|
{
|
|
player.sendMessage("Could not find player \"" + args[1] + "\".");
|
|
return true;
|
|
}
|
|
if (th.hasEnabled2FA(otherPlayer.getUniqueId()))
|
|
{
|
|
th.remove2FA(otherPlayer.getUniqueId());
|
|
player.sendMessage("Removed two-factor authentication for " + otherPlayer.getName() + ".");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
player.sendMessage(otherPlayer.getName() + " does not have two-factor authentication enabled.");
|
|
return true;
|
|
}
|
|
}
|
|
if (th.hasEnabled2FA(player.getUniqueId()))
|
|
{
|
|
th.remove2FA(player.getUniqueId());
|
|
player.sendMessage("Removed two-factor authentication.");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
player.sendMessage("You don't have two-factor authentication enabled.");
|
|
return true;
|
|
}
|
|
}
|
|
else if (args.length >= 1 && args[0].equalsIgnoreCase("activate"))
|
|
{
|
|
if (!player.hasPermission("2fa.activate"))
|
|
{
|
|
player.sendMessage("You don't have permission to do that");
|
|
return true;
|
|
}
|
|
if (th.hasEnabled2FA(player.getUniqueId()))
|
|
{
|
|
player.sendMessage("You have already enabled two-factor authentication.");
|
|
return true;
|
|
}
|
|
if (!th.isInProcess(player.getUniqueId()))
|
|
{
|
|
boolean canStart = th.startCreating(player);
|
|
if (!canStart)
|
|
{
|
|
player.sendMessage("You can't hold anything when starting the process.");
|
|
return true;
|
|
}
|
|
player.sendMessage("Scan the barcode or enter the secret manually in your 2FA app.");
|
|
player.sendMessage("Use /2fa <code> to verify the authenticator.");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
player.sendMessage("Confirm the activation by typing /2fa <code>.");
|
|
return true;
|
|
}
|
|
}
|
|
else if (args.length >= 1)
|
|
{
|
|
String secret = th.getKey(player.getUniqueId());
|
|
String pCode = StringUtils.join(args);
|
|
if (th.matchCode(secret, pCode))
|
|
{
|
|
th.creatingSuccess(player);
|
|
player.sendMessage("Successfully activated two-factor authentication.");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
th.creatingFailed(player);
|
|
player.sendMessage("That code is incorrect, aborting.");
|
|
return true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (th.isInProcess(player.getUniqueId()))
|
|
{
|
|
player.sendMessage("Confirm the activation by typing /2fa <code>.");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
player.sendMessage("Usage: " + command.getUsage().replace("<command>", label));
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args)
|
|
{
|
|
if (args.length < 1)
|
|
{
|
|
return new ArrayList<>();
|
|
}
|
|
StringBuilder search = new StringBuilder();
|
|
for (int i = 0; i < args.length - 1; i++)
|
|
{
|
|
search.append(":").append(args[i]);
|
|
}
|
|
if (!TAB_COMPLETE_MAP.containsKey(search.toString()))
|
|
{
|
|
return new ArrayList<>();
|
|
}
|
|
String[] commands = TAB_COMPLETE_MAP.get(search.toString());
|
|
final List<String> completions = new ArrayList<>();
|
|
StringUtil.copyPartialMatches(args[args.length - 1], Arrays.asList(commands), completions);
|
|
Collections.sort(completions);
|
|
return completions;
|
|
}
|
|
|
|
}
|