diff --git a/src/main/java/io/servertap/ServerTapMain.java b/src/main/java/io/servertap/ServerTapMain.java index b72e8d83..9af858b7 100644 --- a/src/main/java/io/servertap/ServerTapMain.java +++ b/src/main/java/io/servertap/ServerTapMain.java @@ -1,6 +1,8 @@ package io.servertap; import io.servertap.api.v1.models.ConsoleLine; +import io.servertap.auth.RequestAuthProvider; +import io.servertap.auth.UselessAuthProvider; import io.servertap.commands.ServerTapCommand; import io.servertap.metrics.Metrics; import io.servertap.plugin.api.ServerTapWebserverService; @@ -21,6 +23,7 @@ import org.jetbrains.annotations.NotNull; import java.io.File; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; @@ -28,6 +31,7 @@ public class ServerTapMain extends JavaPlugin { private static final java.util.logging.Logger log = Bukkit.getLogger(); private final Logger rootLogger = (Logger) LogManager.getRootLogger(); + private static final String defaultAuthProviderName = "io.servertap.auth.UselessAuthProvider"; private final List consoleBuffer = new ArrayList<>(); private ExternalPluginWrapperRepo externalPluginWrapperRepo; private WebhookEventListener webhookEventListener; @@ -37,6 +41,7 @@ public class ServerTapMain extends JavaPlugin { private final LagDetector lagDetector; private final Server server; private WebServer app; + private RequestAuthProvider authProvider; public ServerTapMain() { super(); @@ -85,6 +90,26 @@ private void setupWebServer(FileConfiguration bukkitConfig) { app = new WebServer(this, bukkitConfig, log); app.start(bukkitConfig.getInt("port", 4567)); WebServerRoutes.addV1Routes(this, log, lagDetector, app, consoleListener, externalPluginWrapperRepo); + + try { + String providerName = bukkitConfig.getString("auth.provider", defaultAuthProviderName); + Class provider = Class.forName(providerName); + RequestAuthProvider requestAuthProvider = (RequestAuthProvider) provider.getDeclaredConstructor().newInstance(); + setAuthProvider(requestAuthProvider); + + log.info(String.format("Using auth provider: %s", providerName)); + } + catch (ClassNotFoundException cex) { + throw new RuntimeException(cex); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } } public void reload() { @@ -124,4 +149,12 @@ public List getConsoleBuffer() { public WebServer getWebServer() { return this.app; } + + public RequestAuthProvider getAuthProvider() { + return authProvider; + } + + public void setAuthProvider(RequestAuthProvider authProvider) { + this.authProvider = authProvider; + } } diff --git a/src/main/java/io/servertap/WebServer.java b/src/main/java/io/servertap/WebServer.java index 867e2efa..0a611158 100644 --- a/src/main/java/io/servertap/WebServer.java +++ b/src/main/java/io/servertap/WebServer.java @@ -92,6 +92,8 @@ private void configureJavalin(JavalinConfig config, ServerTapMain main) { * Verifies the Path is a wagger call or has the correct authentication */ private void manageAccess(Handler handler, Context ctx, Set routeRoles) throws Exception { + ServerTapMain.instance.getAuthProvider().authenticateRequest(handler, ctx); + // If auth is not enabled just serve it all if (!this.isAuthEnabled) { handler.handle(ctx); diff --git a/src/main/java/io/servertap/auth/RequestAuthProvider.java b/src/main/java/io/servertap/auth/RequestAuthProvider.java new file mode 100644 index 00000000..c9471bc0 --- /dev/null +++ b/src/main/java/io/servertap/auth/RequestAuthProvider.java @@ -0,0 +1,10 @@ +package io.servertap.auth; + +import io.javalin.http.Context; +import io.javalin.http.Handler; + +public interface RequestAuthProvider { + + void authenticateRequest(Handler handler, Context ctx) throws Exception; + +} diff --git a/src/main/java/io/servertap/auth/UselessAuthProvider.java b/src/main/java/io/servertap/auth/UselessAuthProvider.java new file mode 100644 index 00000000..3cd96082 --- /dev/null +++ b/src/main/java/io/servertap/auth/UselessAuthProvider.java @@ -0,0 +1,18 @@ +package io.servertap.auth; + +import io.javalin.http.Context; +import io.javalin.http.Handler; +import org.bukkit.Bukkit; + +public class UselessAuthProvider implements RequestAuthProvider { + + private static final java.util.logging.Logger log = Bukkit.getLogger(); + + @Override + public void authenticateRequest(Handler handler, Context ctx) throws Exception { + // do nothing, just allow the request lol! + log.info("USELESSLY HANDLING A REQUEST!"); + handler.handle(ctx); + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1848dcd4..6be30e37 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -4,6 +4,9 @@ useKeyAuth: true key: 'change_me' normalizeMessages: true +auth: + provider: io.servertap.auth.UselessAuthProvider + # TLS (a.k.a. SSL) options tls: # Set to true to enable TLS