From 397f6dd26bfb06ef8d063b0d0b02ec72c06e5a9d Mon Sep 17 00:00:00 2001 From: Will Warren Date: Mon, 25 Sep 2023 10:19:15 -0400 Subject: [PATCH 1/2] working on pluggable auth --- src/main/java/io/servertap/ServerTapMain.java | 10 ++++++++++ src/main/java/io/servertap/WebServer.java | 2 ++ .../io/servertap/auth/RequestAuthProvider.java | 10 ++++++++++ .../io/servertap/auth/UselessAuthProvider.java | 14 ++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 src/main/java/io/servertap/auth/RequestAuthProvider.java create mode 100644 src/main/java/io/servertap/auth/UselessAuthProvider.java diff --git a/src/main/java/io/servertap/ServerTapMain.java b/src/main/java/io/servertap/ServerTapMain.java index b72e8d83..b28944c3 100644 --- a/src/main/java/io/servertap/ServerTapMain.java +++ b/src/main/java/io/servertap/ServerTapMain.java @@ -1,6 +1,7 @@ package io.servertap; import io.servertap.api.v1.models.ConsoleLine; +import io.servertap.auth.RequestAuthProvider; import io.servertap.commands.ServerTapCommand; import io.servertap.metrics.Metrics; import io.servertap.plugin.api.ServerTapWebserverService; @@ -37,6 +38,7 @@ public class ServerTapMain extends JavaPlugin { private final LagDetector lagDetector; private final Server server; private WebServer app; + private RequestAuthProvider authProvider; public ServerTapMain() { super(); @@ -124,4 +126,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..bf07ecae --- /dev/null +++ b/src/main/java/io/servertap/auth/UselessAuthProvider.java @@ -0,0 +1,14 @@ +package io.servertap.auth; + +import io.javalin.http.Context; +import io.javalin.http.Handler; + +public class UselessAuthProvider implements RequestAuthProvider { + + @Override + public void authenticateRequest(Handler handler, Context ctx) throws Exception { + // do nothing, just allow the request lol! + handler.handle(ctx); + } + +} From 1109d50e48158e4fe14273f98e49de434d283dad Mon Sep 17 00:00:00 2001 From: Will Warren Date: Thu, 28 Sep 2023 15:11:44 -0400 Subject: [PATCH 2/2] new auth provider setup --- src/main/java/io/servertap/ServerTapMain.java | 23 +++++++++++++++++++ .../servertap/auth/UselessAuthProvider.java | 4 ++++ src/main/resources/config.yml | 3 +++ 3 files changed, 30 insertions(+) diff --git a/src/main/java/io/servertap/ServerTapMain.java b/src/main/java/io/servertap/ServerTapMain.java index b28944c3..9af858b7 100644 --- a/src/main/java/io/servertap/ServerTapMain.java +++ b/src/main/java/io/servertap/ServerTapMain.java @@ -2,6 +2,7 @@ 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; @@ -22,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; @@ -29,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; @@ -87,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() { diff --git a/src/main/java/io/servertap/auth/UselessAuthProvider.java b/src/main/java/io/servertap/auth/UselessAuthProvider.java index bf07ecae..3cd96082 100644 --- a/src/main/java/io/servertap/auth/UselessAuthProvider.java +++ b/src/main/java/io/servertap/auth/UselessAuthProvider.java @@ -2,12 +2,16 @@ 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