Add the ability to define multiple authentication services

This commit is contained in:
sebivh
2025-10-17 13:56:15 +02:00
parent 1fc848287e
commit a0157292cc
6 changed files with 97 additions and 12 deletions

View File

@@ -1,5 +1,8 @@
package de.sebastianvonhelmersen;
import de.sebastianvonhelmersen.authentication.Authenticator;
import de.sebastianvonhelmersen.authentication.FetSite;
import de.sebastianvonhelmersen.authentication.Ldap;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
@@ -23,11 +26,13 @@ import java.util.logging.Level;
public class fetmcplugin extends JavaPlugin implements Listener {
private Database db;
private Map<UUID, AuthInfos> allInfos = HashMap.newHashMap(0);
private Authenticator authenticator;
@Override
public void onEnable() {
// Make sure the plugin's data folder exists
File dataFolder = this.getDataFolder();
this.authenticator = new FetSite("MyokpBxpqxw9Eo8IduwH9IFSnBy4qII6");
if (!dataFolder.exists()) {
dataFolder.mkdirs();
@@ -145,10 +150,10 @@ public class fetmcplugin extends JavaPlugin implements Listener {
Component.text("§eBitte gib das Password deines FET Accounts ein!"));
break;
case AUTH_WAITING_PASSWORD:
LdapUser user = checkLdap(info.getUsername(), message.trim());
boolean valid = validate(info.getUsername(), message.trim());
event.setCancelled(true);
event.getRecipients().clear();
if(user != null) {
if(valid) {
allInfos.remove(uuid);
this.db.addPlayer(uuid.toString(), event.getPlayer().getName(), user);
Bukkit.getScheduler().runTask(this, () -> {
@@ -177,11 +182,16 @@ public class fetmcplugin extends JavaPlugin implements Listener {
return;
}
private LdapUser checkLdap(String username, String password) {
/*
* This function uses the defined Authenticator instance to authenticate the User against their
* provided username - token combination
* @param username the username used to authenticate the player with the provider
* @param token some sort of authentication methon. Can be Password, AppPassword or some other form of token the
* provider takes to authenticate the user
*/
private boolean validate(String username, String token) {
getLogger().log(Level.INFO, "Checking LDAP for user " + username);
return Ldap.authenticate(username, password);
//return true;
return authenticator.authenticate(username, token);
}
}