Search in sources :

Example 11 with CommandSource

use of com.velocitypowered.api.command.CommandSource in project LimboAuth by Elytrium.

the class ForceChangePasswordCommand method execute.

@Override
public void execute(SimpleCommand.Invocation invocation) {
    CommandSource source = invocation.source();
    String[] args = invocation.arguments();
    if (args.length == 2) {
        String nickname = args[0];
        String newPassword = args[1];
        try {
            UpdateBuilder<RegisteredPlayer, String> updateBuilder = this.playerDao.updateBuilder();
            updateBuilder.where().eq("LOWERCASENICKNAME", nickname.toLowerCase(Locale.ROOT));
            updateBuilder.updateColumnValue("HASH", AuthSessionHandler.genHash(newPassword));
            updateBuilder.update();
            this.server.getPlayer(nickname).ifPresent(player -> player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.message, newPassword))));
            source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.successful, nickname)));
        } catch (SQLException e) {
            source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.notSuccessful, nickname)));
            e.printStackTrace();
        }
        return;
    }
    source.sendMessage(this.usage);
}
Also used : SQLException(java.sql.SQLException) CommandSource(com.velocitypowered.api.command.CommandSource) RegisteredPlayer(net.elytrium.limboauth.model.RegisteredPlayer)

Example 12 with CommandSource

use of com.velocitypowered.api.command.CommandSource in project LimboAuth by Elytrium.

the class ForceUnregisterCommand method execute.

@Override
public void execute(SimpleCommand.Invocation invocation) {
    CommandSource source = invocation.source();
    String[] args = invocation.arguments();
    if (args.length == 1) {
        String playerNick = args[0];
        try {
            this.playerDao.deleteById(playerNick.toLowerCase(Locale.ROOT));
            this.plugin.removePlayerFromCache(playerNick);
            this.server.getPlayer(playerNick).ifPresent(player -> player.disconnect(this.kick));
            source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.successful, playerNick)));
        } catch (SQLException e) {
            source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.notSuccessful, playerNick)));
            e.printStackTrace();
        }
        return;
    }
    source.sendMessage(this.usage);
}
Also used : SQLException(java.sql.SQLException) CommandSource(com.velocitypowered.api.command.CommandSource)

Example 13 with CommandSource

use of com.velocitypowered.api.command.CommandSource in project LimboAuth by Elytrium.

the class LimboAuthCommand method execute.

@Override
public void execute(SimpleCommand.Invocation invocation) {
    CommandSource source = invocation.source();
    String[] args = invocation.arguments();
    if (args.length == 1) {
        if (args[0].equalsIgnoreCase("reload") && source.hasPermission("limboauth.admin.reload")) {
            try {
                this.plugin.reload();
                source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.RELOAD));
            } catch (Exception e) {
                source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Settings.IMP.MAIN.STRINGS.RELOAD_FAILED));
                e.printStackTrace();
            }
            return;
        }
    }
    this.showHelp(source);
}
Also used : CommandSource(com.velocitypowered.api.command.CommandSource)

Example 14 with CommandSource

use of com.velocitypowered.api.command.CommandSource in project LimboAuth by Elytrium.

the class PremiumCommand method execute.

@Override
public void execute(SimpleCommand.Invocation invocation) {
    CommandSource source = invocation.source();
    String[] args = invocation.arguments();
    if (!(source instanceof Player)) {
        source.sendMessage(this.notPlayer);
        return;
    }
    if (args.length == 2) {
        if (args[1].equalsIgnoreCase("confirm")) {
            String username = ((Player) source).getUsername();
            RegisteredPlayer player = AuthSessionHandler.fetchInfo(this.playerDao, username);
            if (player == null) {
                source.sendMessage(this.notRegistered);
            } else if (player.getHash().isEmpty()) {
                source.sendMessage(this.alreadyPremium);
            } else if (AuthSessionHandler.checkPassword(args[0], player, this.playerDao)) {
                if (this.plugin.isPremiumExternal(username)) {
                    try {
                        player.setHash("");
                        this.playerDao.update(player);
                        this.plugin.removePlayerFromCache(username);
                        ((Player) source).disconnect(this.successful);
                    } catch (SQLException e) {
                        source.sendMessage(this.errorOccurred);
                        e.printStackTrace();
                    }
                } else {
                    source.sendMessage(this.notPremium);
                }
            } else {
                source.sendMessage(this.wrongPassword);
            }
            return;
        }
    }
    source.sendMessage(this.usage);
}
Also used : Player(com.velocitypowered.api.proxy.Player) RegisteredPlayer(net.elytrium.limboauth.model.RegisteredPlayer) SQLException(java.sql.SQLException) CommandSource(com.velocitypowered.api.command.CommandSource) RegisteredPlayer(net.elytrium.limboauth.model.RegisteredPlayer)

Example 15 with CommandSource

use of com.velocitypowered.api.command.CommandSource in project LimboAuth by Elytrium.

the class TotpCommand method execute.

@Override
public void execute(SimpleCommand.Invocation invocation) {
    CommandSource source = invocation.source();
    String[] args = invocation.arguments();
    if (!(source instanceof Player)) {
        source.sendMessage(this.notPlayer);
        return;
    }
    if (args.length == 0) {
        source.sendMessage(this.usage);
    } else {
        String username = ((Player) source).getUsername();
        RegisteredPlayer playerInfo;
        UpdateBuilder<RegisteredPlayer, String> updateBuilder;
        switch(args[0]) {
            case "enable":
                {
                    if (this.needPassword ? args.length == 2 : args.length == 1) {
                        playerInfo = AuthSessionHandler.fetchInfo(this.playerDao, username);
                        if (playerInfo == null) {
                            source.sendMessage(this.notRegistered);
                            return;
                        } else if (playerInfo.getHash().isEmpty()) {
                            source.sendMessage(this.crackedCommand);
                        } else if (this.needPassword && !AuthSessionHandler.checkPassword(args[1], playerInfo, this.playerDao)) {
                            source.sendMessage(this.wrongPassword);
                            return;
                        }
                        if (!playerInfo.getTotpToken().isEmpty()) {
                            source.sendMessage(this.alreadyEnabled);
                            return;
                        }
                        String secret = this.secretGenerator.generate();
                        try {
                            updateBuilder = this.playerDao.updateBuilder();
                            updateBuilder.where().eq("NICKNAME", username);
                            updateBuilder.updateColumnValue("TOTPTOKEN", secret);
                            updateBuilder.update();
                        } catch (SQLException e) {
                            source.sendMessage(this.errorOccurred);
                            e.printStackTrace();
                        }
                        source.sendMessage(this.successful);
                        QrData data = new QrData.Builder().label(username).secret(secret).issuer(this.issuer).build();
                        String qrUrl = this.qrGeneratorUrl.replace("{data}", URLEncoder.encode(data.getUri(), StandardCharsets.UTF_8));
                        source.sendMessage(this.qr.clickEvent(ClickEvent.openUrl(qrUrl)));
                        source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.token, secret)).clickEvent(ClickEvent.copyToClipboard(secret)));
                        String codes = String.join(", ", this.codesGenerator.generateCodes(this.recoveryCodesAmount));
                        source.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(MessageFormat.format(this.recovery, codes)).clickEvent(ClickEvent.copyToClipboard(codes)));
                    } else {
                        source.sendMessage(this.usage);
                    }
                    break;
                }
            case "disable":
                {
                    if (args.length != 2) {
                        source.sendMessage(this.usage);
                        return;
                    }
                    playerInfo = AuthSessionHandler.fetchInfo(this.playerDao, username);
                    if (playerInfo == null) {
                        source.sendMessage(this.notRegistered);
                        return;
                    }
                    if (AuthSessionHandler.getVerifier().isValidCode(playerInfo.getTotpToken(), args[1])) {
                        try {
                            updateBuilder = this.playerDao.updateBuilder();
                            updateBuilder.where().eq("NICKNAME", username);
                            updateBuilder.updateColumnValue("TOTPTOKEN", "");
                            updateBuilder.update();
                            source.sendMessage(this.disabled);
                        } catch (SQLException e) {
                            source.sendMessage(this.errorOccurred);
                            e.printStackTrace();
                        }
                    } else {
                        source.sendMessage(this.wrong);
                    }
                    break;
                }
            default:
                {
                    source.sendMessage(this.usage);
                    break;
                }
        }
    }
}
Also used : Player(com.velocitypowered.api.proxy.Player) RegisteredPlayer(net.elytrium.limboauth.model.RegisteredPlayer) QrData(dev.samstevens.totp.qr.QrData) SQLException(java.sql.SQLException) CommandSource(com.velocitypowered.api.command.CommandSource) RegisteredPlayer(net.elytrium.limboauth.model.RegisteredPlayer)

Aggregations

CommandSource (com.velocitypowered.api.command.CommandSource)43 Player (com.velocitypowered.api.proxy.Player)23 Component (net.kyori.adventure.text.Component)10 SimpleCommand (com.velocitypowered.api.command.SimpleCommand)7 SQLException (java.sql.SQLException)6 List (java.util.List)6 LegacyComponentSerializer (net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer)6 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)5 Subscribe (com.velocitypowered.api.event.Subscribe)4 ConsoleCommandSource (com.velocitypowered.api.proxy.ConsoleCommandSource)4 ProxyServer (com.velocitypowered.api.proxy.ProxyServer)4 RegisteredServer (com.velocitypowered.api.proxy.server.RegisteredServer)4 ArrayList (java.util.ArrayList)4 Collectors (java.util.stream.Collectors)4 RegisteredPlayer (net.elytrium.limboauth.model.RegisteredPlayer)4 Gson (com.google.gson.Gson)3 LiteralArgumentBuilder (com.mojang.brigadier.builder.LiteralArgumentBuilder)3 BrigadierCommand (com.velocitypowered.api.command.BrigadierCommand)3 RawCommand (com.velocitypowered.api.command.RawCommand)3 ServerConnection (com.velocitypowered.api.proxy.ServerConnection)3