mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-22 11:54:39 +01:00
Update HttpServer & AuthenticationSystem to use Javalin
This commit is contained in:
@@ -2,16 +2,13 @@ package emu.grasscutter.server.http.dispatch;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.auth.AuthenticationSystem;
|
||||
import emu.grasscutter.auth.OAuthAuthenticator;
|
||||
import emu.grasscutter.auth.OAuthAuthenticator.ClientType;
|
||||
import emu.grasscutter.server.http.Router;
|
||||
import emu.grasscutter.server.http.objects.*;
|
||||
import emu.grasscutter.server.http.objects.ComboTokenReqJson.LoginTokenData;
|
||||
import emu.grasscutter.utils.JsonUtils;
|
||||
import express.Express;
|
||||
import express.http.Request;
|
||||
import express.http.Response;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.Context;
|
||||
|
||||
import static emu.grasscutter.utils.Language.translate;
|
||||
|
||||
@@ -19,40 +16,40 @@ import static emu.grasscutter.utils.Language.translate;
|
||||
* Handles requests related to authentication. (aka dispatch)
|
||||
*/
|
||||
public final class DispatchHandler implements Router {
|
||||
@Override public void applyRoutes(Express express, Javalin handle) {
|
||||
@Override public void applyRoutes(Javalin javalin) {
|
||||
// Username & Password login (from client).
|
||||
express.post("/hk4e_global/mdk/shield/api/login", DispatchHandler::clientLogin);
|
||||
javalin.post("/hk4e_global/mdk/shield/api/login", DispatchHandler::clientLogin);
|
||||
// Cached token login (from registry).
|
||||
express.post("/hk4e_global/mdk/shield/api/verify", DispatchHandler::tokenLogin);
|
||||
javalin.post("/hk4e_global/mdk/shield/api/verify", DispatchHandler::tokenLogin);
|
||||
// Combo token login (from session key).
|
||||
express.post("/hk4e_global/combo/granter/login/v2/login", DispatchHandler::sessionKeyLogin);
|
||||
javalin.post("/hk4e_global/combo/granter/login/v2/login", DispatchHandler::sessionKeyLogin);
|
||||
|
||||
// External login (from other clients).
|
||||
express.get("/authentication/type", (request, response) -> response.send(Grasscutter.getAuthenticationSystem().getClass().getSimpleName()));
|
||||
express.post("/authentication/login", (request, response) -> Grasscutter.getAuthenticationSystem().getExternalAuthenticator()
|
||||
.handleLogin(AuthenticationSystem.fromExternalRequest(request, response)));
|
||||
express.post("/authentication/register", (request, response) -> Grasscutter.getAuthenticationSystem().getExternalAuthenticator()
|
||||
.handleAccountCreation(AuthenticationSystem.fromExternalRequest(request, response)));
|
||||
express.post("/authentication/change_password", (request, response) -> Grasscutter.getAuthenticationSystem().getExternalAuthenticator()
|
||||
.handlePasswordReset(AuthenticationSystem.fromExternalRequest(request, response)));
|
||||
javalin.get("/authentication/type", ctx -> ctx.result(Grasscutter.getAuthenticationSystem().getClass().getSimpleName()));
|
||||
javalin.post("/authentication/login", ctx -> Grasscutter.getAuthenticationSystem().getExternalAuthenticator()
|
||||
.handleLogin(AuthenticationSystem.fromExternalRequest(ctx)));
|
||||
javalin.post("/authentication/register", ctx -> Grasscutter.getAuthenticationSystem().getExternalAuthenticator()
|
||||
.handleAccountCreation(AuthenticationSystem.fromExternalRequest(ctx)));
|
||||
javalin.post("/authentication/change_password", ctx -> Grasscutter.getAuthenticationSystem().getExternalAuthenticator()
|
||||
.handlePasswordReset(AuthenticationSystem.fromExternalRequest(ctx)));
|
||||
|
||||
// External login (from OAuth2).
|
||||
express.post("/hk4e_global/mdk/shield/api/loginByThirdparty", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator()
|
||||
.handleLogin(AuthenticationSystem.fromExternalRequest(request, response)));
|
||||
express.get("/authentication/openid/redirect", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator()
|
||||
.handleTokenProcess(AuthenticationSystem.fromExternalRequest(request, response)));
|
||||
express.get("/Api/twitter_login", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator()
|
||||
.handleRedirection(AuthenticationSystem.fromExternalRequest(request, response), ClientType.DESKTOP));
|
||||
express.get("/sdkTwitterLogin.html", (request, response) -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator()
|
||||
.handleRedirection(AuthenticationSystem.fromExternalRequest(request, response), ClientType.MOBILE));
|
||||
javalin.post("/hk4e_global/mdk/shield/api/loginByThirdparty", ctx -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator()
|
||||
.handleLogin(AuthenticationSystem.fromExternalRequest(ctx)));
|
||||
javalin.get("/authentication/openid/redirect", ctx -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator()
|
||||
.handleTokenProcess(AuthenticationSystem.fromExternalRequest(ctx)));
|
||||
javalin.get("/Api/twitter_login", ctx -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator()
|
||||
.handleRedirection(AuthenticationSystem.fromExternalRequest(ctx), ClientType.DESKTOP));
|
||||
javalin.get("/sdkTwitterLogin.html", ctx -> Grasscutter.getAuthenticationSystem().getOAuthAuthenticator()
|
||||
.handleRedirection(AuthenticationSystem.fromExternalRequest(ctx), ClientType.MOBILE));
|
||||
}
|
||||
|
||||
/**
|
||||
* @route /hk4e_global/mdk/shield/api/login
|
||||
*/
|
||||
private static void clientLogin(Request request, Response response) {
|
||||
private static void clientLogin(Context ctx) {
|
||||
// Parse body data.
|
||||
String rawBodyData = request.ctx().body();
|
||||
String rawBodyData = ctx.body();
|
||||
var bodyData = JsonUtils.decode(rawBodyData, LoginAccountRequestJson.class);
|
||||
|
||||
// Validate body data.
|
||||
@@ -62,20 +59,20 @@ public final class DispatchHandler implements Router {
|
||||
// Pass data to authentication handler.
|
||||
var responseData = Grasscutter.getAuthenticationSystem()
|
||||
.getPasswordAuthenticator()
|
||||
.authenticate(AuthenticationSystem.fromPasswordRequest(request, bodyData));
|
||||
.authenticate(AuthenticationSystem.fromPasswordRequest(ctx, bodyData));
|
||||
// Send response.
|
||||
response.send(responseData);
|
||||
ctx.json(responseData);
|
||||
|
||||
// Log to console.
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.account.login_attempt", request.ip()));
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.account.login_attempt", ctx.ip()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @route /hk4e_global/mdk/shield/api/verify
|
||||
*/
|
||||
private static void tokenLogin(Request request, Response response) {
|
||||
private static void tokenLogin(Context ctx) {
|
||||
// Parse body data.
|
||||
String rawBodyData = request.ctx().body();
|
||||
String rawBodyData = ctx.body();
|
||||
var bodyData = JsonUtils.decode(rawBodyData, LoginTokenRequestJson.class);
|
||||
|
||||
// Validate body data.
|
||||
@@ -85,20 +82,20 @@ public final class DispatchHandler implements Router {
|
||||
// Pass data to authentication handler.
|
||||
var responseData = Grasscutter.getAuthenticationSystem()
|
||||
.getTokenAuthenticator()
|
||||
.authenticate(AuthenticationSystem.fromTokenRequest(request, bodyData));
|
||||
.authenticate(AuthenticationSystem.fromTokenRequest(ctx, bodyData));
|
||||
// Send response.
|
||||
response.send(responseData);
|
||||
ctx.json(responseData);
|
||||
|
||||
// Log to console.
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.account.login_attempt", request.ip()));
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.account.login_attempt", ctx.ip()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @route /hk4e_global/combo/granter/login/v2/login
|
||||
*/
|
||||
private static void sessionKeyLogin(Request request, Response response) {
|
||||
private static void sessionKeyLogin(Context ctx) {
|
||||
// Parse body data.
|
||||
String rawBodyData = request.ctx().body();
|
||||
String rawBodyData = ctx.body();
|
||||
var bodyData = JsonUtils.decode(rawBodyData, ComboTokenReqJson.class);
|
||||
|
||||
// Validate body data.
|
||||
@@ -111,11 +108,11 @@ public final class DispatchHandler implements Router {
|
||||
// Pass data to authentication handler.
|
||||
var responseData = Grasscutter.getAuthenticationSystem()
|
||||
.getSessionKeyAuthenticator()
|
||||
.authenticate(AuthenticationSystem.fromComboTokenRequest(request, bodyData, tokenData));
|
||||
.authenticate(AuthenticationSystem.fromComboTokenRequest(ctx, bodyData, tokenData));
|
||||
// Send response.
|
||||
response.send(responseData);
|
||||
ctx.json(responseData);
|
||||
|
||||
// Log to console.
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.account.login_attempt", request.ip()));
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.account.login_attempt", ctx.ip()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,20 +11,12 @@ import emu.grasscutter.server.event.dispatch.QueryCurrentRegionEvent;
|
||||
import emu.grasscutter.server.http.Router;
|
||||
import emu.grasscutter.server.http.objects.QueryCurRegionRspJson;
|
||||
import emu.grasscutter.utils.Crypto;
|
||||
import emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.Utils;
|
||||
import express.Express;
|
||||
import express.http.Request;
|
||||
import express.http.Response;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.Context;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.security.Signature;
|
||||
@@ -107,36 +99,36 @@ public final class RegionHandler implements Router {
|
||||
regionListResponse = Utils.base64Encode(updatedRegionList.toByteString().toByteArray());
|
||||
}
|
||||
|
||||
@Override public void applyRoutes(Express express, Javalin handle) {
|
||||
express.get("/query_region_list", RegionHandler::queryRegionList);
|
||||
express.get("/query_cur_region/:region", RegionHandler::queryCurrentRegion );
|
||||
@Override public void applyRoutes(Javalin javalin) {
|
||||
javalin.get("/query_region_list", RegionHandler::queryRegionList);
|
||||
javalin.get("/query_cur_region/{region}", RegionHandler::queryCurrentRegion );
|
||||
}
|
||||
|
||||
/**
|
||||
* @route /query_region_list
|
||||
*/
|
||||
private static void queryRegionList(Request request, Response response) {
|
||||
private static void queryRegionList(Context ctx) {
|
||||
// Invoke event.
|
||||
QueryAllRegionsEvent event = new QueryAllRegionsEvent(regionListResponse); event.call();
|
||||
// Respond with event result.
|
||||
response.send(event.getRegionList());
|
||||
ctx.result(event.getRegionList());
|
||||
|
||||
// Log to console.
|
||||
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s request: query_region_list", request.ip()));
|
||||
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s request: query_region_list", ctx.ip()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @route /query_cur_region/:region
|
||||
* @route /query_cur_region/{region}
|
||||
*/
|
||||
private static void queryCurrentRegion(Request request, Response response) {
|
||||
private static void queryCurrentRegion(Context ctx) {
|
||||
// Get region to query.
|
||||
String regionName = request.params("region");
|
||||
String versionName = request.query("version");
|
||||
String regionName = ctx.pathParam("region");
|
||||
String versionName = ctx.queryParam("version");
|
||||
var region = regions.get(regionName);
|
||||
|
||||
// Get region data.
|
||||
String regionData = "CAESGE5vdCBGb3VuZCB2ZXJzaW9uIGNvbmZpZw==";
|
||||
if (request.query().values().size() > 0) {
|
||||
if (ctx.queryParamMap().values().size() > 0) {
|
||||
if (region != null)
|
||||
regionData = region.getBase64();
|
||||
}
|
||||
@@ -150,18 +142,18 @@ public final class RegionHandler implements Router {
|
||||
try {
|
||||
QueryCurrentRegionEvent event = new QueryCurrentRegionEvent(regionData); event.call();
|
||||
|
||||
if (request.query("dispatchSeed") == null) {
|
||||
if (ctx.queryParam("dispatchSeed") == null) {
|
||||
// More love for UA Patch players
|
||||
var rsp = new QueryCurRegionRspJson();
|
||||
|
||||
rsp.content = event.getRegionInfo();
|
||||
rsp.sign = "TW9yZSBsb3ZlIGZvciBVQSBQYXRjaCBwbGF5ZXJz";
|
||||
|
||||
response.send(rsp);
|
||||
ctx.json(rsp);
|
||||
return;
|
||||
}
|
||||
|
||||
String key_id = request.query("key_id");
|
||||
String key_id = ctx.queryParam("key_id");
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key_id.equals("3") ? Crypto.CUR_OS_ENCRYPT_KEY : Crypto.CUR_CN_ENCRYPT_KEY);
|
||||
var regionInfo = Utils.base64Decode(event.getRegionInfo());
|
||||
@@ -189,7 +181,7 @@ public final class RegionHandler implements Router {
|
||||
rsp.content = Utils.base64Encode(encryptedRegionInfoStream.toByteArray());
|
||||
rsp.sign = Utils.base64Encode(privateSignature.sign());
|
||||
|
||||
response.send(rsp);
|
||||
ctx.json(rsp);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Grasscutter.getLogger().error("An error occurred while handling query_cur_region.", e);
|
||||
@@ -199,10 +191,10 @@ public final class RegionHandler implements Router {
|
||||
// Invoke event.
|
||||
QueryCurrentRegionEvent event = new QueryCurrentRegionEvent(regionData); event.call();
|
||||
// Respond with event result.
|
||||
response.send(event.getRegionInfo());
|
||||
ctx.result(event.getRegionInfo());
|
||||
}
|
||||
// Log to console.
|
||||
Grasscutter.getLogger().info(String.format("Client %s request: query_cur_region/%s", request.ip(), regionName));
|
||||
Grasscutter.getLogger().info(String.format("Client %s request: query_cur_region/%s", ctx.ip(), regionName));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user