mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-21 19:34:42 +01:00
Implement proper handbook authentication (pt. 1)
This commit is contained in:
@@ -5,6 +5,7 @@ import emu.grasscutter.Grasscutter.ServerDebugMode;
|
||||
import emu.grasscutter.utils.FileUtils;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.json.JavalinGson;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||
@@ -57,6 +58,9 @@ public final class HttpServer {
|
||||
if (DISPATCH_INFO.logRequests == ServerDebugMode.ALL)
|
||||
config.plugins.enableDevLogging();
|
||||
|
||||
// Set the JSON mapper.
|
||||
config.jsonMapper(new JavalinGson());
|
||||
|
||||
// Static files should be added like this https://javalin.io/documentation#static-files
|
||||
});
|
||||
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
package emu.grasscutter.server.http.documentation;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.HANDBOOK;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.auth.AuthenticationSystem.AuthenticationRequest;
|
||||
import emu.grasscutter.server.http.Router;
|
||||
import emu.grasscutter.utils.DispatchUtils;
|
||||
import emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.objects.HandbookBody;
|
||||
import emu.grasscutter.utils.objects.HandbookBody.Action;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.http.Context;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.HANDBOOK;
|
||||
|
||||
/** Handles requests for the new GM Handbook. */
|
||||
public final class HandbookHandler implements Router {
|
||||
private final byte[] handbook;
|
||||
@@ -20,7 +23,7 @@ public final class HandbookHandler implements Router {
|
||||
* found.
|
||||
*/
|
||||
public HandbookHandler() {
|
||||
this.handbook = FileUtils.readResource("/handbook.html");
|
||||
this.handbook = FileUtils.readResource("/html/handbook.html");
|
||||
this.serve = HANDBOOK.enable && this.handbook.length > 0;
|
||||
}
|
||||
|
||||
@@ -30,6 +33,9 @@ public final class HandbookHandler implements Router {
|
||||
|
||||
// The handbook content. (built from src/handbook)
|
||||
javalin.get("/handbook", this::serveHandbook);
|
||||
// The handbook authentication page.
|
||||
javalin.get("/handbook/authenticate", this::authenticate);
|
||||
javalin.post("/handbook/authenticate", this::performAuthentication);
|
||||
|
||||
// Handbook control routes.
|
||||
javalin.post("/handbook/avatar", this::grantAvatar);
|
||||
@@ -59,6 +65,49 @@ public final class HandbookHandler implements Router {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serves the handbook authentication page.
|
||||
*
|
||||
* @route GET /handbook/authenticate
|
||||
* @param ctx The Javalin request context.
|
||||
*/
|
||||
private void authenticate(Context ctx) {
|
||||
if (!this.serve) {
|
||||
ctx.status(500).result("Handbook not found.");
|
||||
} else {
|
||||
// Pass the request to the authenticator.
|
||||
Grasscutter.getAuthenticationSystem()
|
||||
.getHandbookAuthenticator().presentPage(
|
||||
AuthenticationRequest.builder().context(ctx).build());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs authentication for the handbook.
|
||||
*
|
||||
* @route POST /handbook/authenticate
|
||||
* @param ctx The Javalin request context.
|
||||
*/
|
||||
private void performAuthentication(Context ctx) {
|
||||
if (!this.serve) {
|
||||
ctx.status(500).result("Handbook not found.");
|
||||
} else {
|
||||
// Pass the request to the authenticator.
|
||||
var result = Grasscutter.getAuthenticationSystem()
|
||||
.getHandbookAuthenticator().authenticate(
|
||||
AuthenticationRequest.builder().context(ctx).build());
|
||||
if (result == null) {
|
||||
ctx.status(500).result("Authentication failed.");
|
||||
} else {
|
||||
ctx
|
||||
.status(result.getStatus())
|
||||
.result(result.getBody())
|
||||
.contentType(result.getBody().contains("html") ?
|
||||
ContentType.TEXT_HTML : ContentType.TEXT_PLAIN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Grants the avatar to the user.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user