Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phybros/auth providers #260

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/io/servertap/ServerTapMain.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,13 +23,15 @@
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

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<ConsoleLine> consoleBuffer = new ArrayList<>();
private ExternalPluginWrapperRepo externalPluginWrapperRepo;
private WebhookEventListener webhookEventListener;
Expand All @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -124,4 +149,12 @@ public List<ConsoleLine> getConsoleBuffer() {
public WebServer getWebServer() {
return this.app;
}

public RequestAuthProvider getAuthProvider() {
return authProvider;
}

public void setAuthProvider(RequestAuthProvider authProvider) {
this.authProvider = authProvider;
}
}
2 changes: 2 additions & 0 deletions src/main/java/io/servertap/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends RouteRole> routeRoles) throws Exception {
ServerTapMain.instance.getAuthProvider().authenticateRequest(handler, ctx);

// If auth is not enabled just serve it all
if (!this.isAuthEnabled) {
handler.handle(ctx);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/servertap/auth/RequestAuthProvider.java
Original file line number Diff line number Diff line change
@@ -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;

}
18 changes: 18 additions & 0 deletions src/main/java/io/servertap/auth/UselessAuthProvider.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down