Files
src
build.gradle
manifest.kod
README.md
settings.gradle
Code
12 1,127
src/main/java/com/krowauth/KrowAuthPlugin.java
package com.krowauth;
import com.krowauth.auth.AuthCommands;
import com.krowauth.auth.AuthSessionManager;
import com.krowauth.auth.PasswordHasher;
import com.krowauth.auth.SqliteStorage;
import com.krowauth.listener.AuthListener;
import java.io.File;
import java.util.Objects;
import java.util.concurrent.CompletionException;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
public final class KrowAuthPlugin extends JavaPlugin {
private SqliteStorage storage;
private PasswordHasher passwordHasher;
private AuthSessionManager sessions;
@Override
public void onEnable() {
saveDefaultConfig();
if (!getDataFolder().exists() && !getDataFolder().mkdirs()) {
throw new IllegalStateException("Could not create the KrowAuth data folder.");
}
storage = new SqliteStorage(new File(getDataFolder(), "accounts.db"));
passwordHasher = new PasswordHasher(getHashIterations());
sessions = new AuthSessionManager(this, storage);
AuthCommands commands = new AuthCommands(this, storage, passwordHasher, sessions);
for (String name : new String[]{"register", "login", "changepassword", "logout", "auth"}) {
Objects.requireNonNull(getCommand(name), "Missing command declaration: " + name)
.setExecutor(commands);
}
getServer().getPluginManager().registerEvents(new AuthListener(this, sessions), this);
storage.ready().whenComplete((ignored, error) -> {
if (error != null) {
logFailure(error);
Bukkit.getScheduler().runTask(this, () -> getServer().getPluginManager().disablePlugin(this));
}
});
getLogger().info("KrowAuth enabled.");
}
@Override
public void onDisable() {
if (sessions != null) {
sessions.shutdown();
}
if (passwordHasher != null) {
passwordHasher.close();
}
if (storage != null) {
storage.close();
}
}
public void sendMessage(CommandSender recipient, String messageKey) {
String prefix = getConfig().getString("prefix", "&8[&5KrowAuth&8] &r");
String message = getConfig().getString("messages." + messageKey,
"&cAuthentication is temporarily unavailable.");
recipient.sendMessage(color(prefix + message));
}
public String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}
public int getMinimumPasswordLength() {
return Math.max(1, getConfig().getInt("security.minimum-password-length", 8));
}
public int getMaximumPasswordLength() {
return Math.max(getMinimumPasswordLength(),
getConfig().getInt("security.maximum-password-length", 128));
}
public int getHashIterations() {
return Math.max(100_000, Math.min(1_000_000,
getConfig().getInt("security.pbkdf2-iterations", 210_000)));
}
public SqliteStorage storage() {
return storage;
}
public AuthSessionManager sessions() {
return sessions;
}
public void logFailure(Throwable error) {
Throwable cause = error;
while (cause instanceof CompletionException && cause.getCause() != null) {
cause = cause.getCause();
}
getLogger().log(Level.SEVERE, "An authentication operation failed.", cause);
}
}