polyfill api.minecraftservices.com/privileges

This commit is contained in:
Haowei Wen 2020-12-25 05:50:29 +08:00
parent 961366e012
commit 35b8b58e1d
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4
2 changed files with 59 additions and 0 deletions

View file

@ -44,6 +44,7 @@ import java.util.Set;
import java.util.stream.Stream;
import moe.yushi.authlibinjector.httpd.DefaultURLRedirector;
import moe.yushi.authlibinjector.httpd.LegacySkinAPIFilter;
import moe.yushi.authlibinjector.httpd.PrivilegesFilter;
import moe.yushi.authlibinjector.httpd.QueryProfileFilter;
import moe.yushi.authlibinjector.httpd.QueryUUIDsFilter;
import moe.yushi.authlibinjector.httpd.URLFilter;
@ -239,6 +240,12 @@ public final class AuthlibInjector {
log(INFO, "Disabled Mojang namespace");
}
boolean polyfillPrivilegesApi = !Boolean.TRUE.equals(config.getMeta().get("feature.privileges_api"));
if (polyfillPrivilegesApi) {
log(INFO, "Polyfill privileges API");
filters.add(new PrivilegesFilter());
}
return filters;
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (C) 2020 Haowei Wen <yushijinhun@gmail.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package moe.yushi.authlibinjector.httpd;
import static moe.yushi.authlibinjector.util.IOUtils.CONTENT_TYPE_JSON;
import java.io.IOException;
import java.util.Optional;
import moe.yushi.authlibinjector.internal.fi.iki.elonen.IHTTPSession;
import moe.yushi.authlibinjector.internal.fi.iki.elonen.Response;
import moe.yushi.authlibinjector.internal.fi.iki.elonen.Status;
import moe.yushi.authlibinjector.internal.org.json.simple.JSONObject;
public class PrivilegesFilter implements URLFilter {
private static final String[] PRIVILEGES = { "onlineChat", "multiplayerServer", "multiplayerRealms" };
@Override
public boolean canHandle(String domain) {
return domain.equals("api.minecraftservices.com");
}
@Override
public Optional<Response> handle(String domain, String path, IHTTPSession session) throws IOException {
if (domain.equals("api.minecraftservices.com") && path.equals("/privileges") && session.getMethod().equals("GET")) {
JSONObject response = new JSONObject();
JSONObject privileges = new JSONObject();
for (String privilegeName : PRIVILEGES) {
JSONObject privilege = new JSONObject();
privilege.put("enabled", true);
privileges.put(privilegeName, privilege);
}
response.put("privileges", privileges);
return Optional.of(Response.newFixedLength(Status.OK, CONTENT_TYPE_JSON, response.toJSONString()));
} else {
return Optional.empty();
}
}
}