From edd82f7978fdb9097beaa1424ee19df5342816e9 Mon Sep 17 00:00:00 2001 From: Enginecrafter77 Date: Fri, 10 Jun 2022 11:28:59 +0200 Subject: [PATCH] style: ProxyAgnosticMojangResolver: Extracted the URL into a constant The URL of the mojang session server call in ProxyAgnosticMojangResolver was extracted into a constant in order to mitigate the "magic constants" problem. Additionally, the 204 in the response code check was replaced by a constant from HttpURLConnection, again, in order to not use any magic numbers. --- .../mojang/ProxyAgnosticMojangResolver.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/github/games647/fastlogin/core/mojang/ProxyAgnosticMojangResolver.java b/core/src/main/java/com/github/games647/fastlogin/core/mojang/ProxyAgnosticMojangResolver.java index 18c75b8c..aabba993 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/mojang/ProxyAgnosticMojangResolver.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/mojang/ProxyAgnosticMojangResolver.java @@ -42,21 +42,34 @@ import java.util.Optional; * @author games647, Enginecrafter77 */ public class ProxyAgnosticMojangResolver extends MojangResolver { + /** + * A formatting string containing an URL used to call the {@code hasJoined} method on mojang session servers. + * + * Formatting parameters: + * 1. The username of the player in question + * 2. The serverId of this server + */ + public static final String MOJANG_SESSIONSERVER_HASJOINED_CALL_URLFMT = "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=%s&serverId=%s"; + @Override public Optional hasJoined(String username, String serverHash, InetAddress hostIp) throws IOException { - String url = String.format("https://sessionserver.mojang.com/session/minecraft/hasJoined?username=%s&serverId=%s", username, serverHash); + String url = String.format(MOJANG_SESSIONSERVER_HASJOINED_CALL_URLFMT, username, serverHash); HttpURLConnection conn = this.getConnection(url); int responseCode = conn.getResponseCode(); Verification verification = null; - if(responseCode != 204) + + // Mojang session servers send HTTP 204 (NO CONTENT) when the authentication seems invalid + // If that's not our case, the authentication is valid, and so we can parse the response. + if(responseCode != HttpURLConnection.HTTP_NO_CONTENT) verification = this.parseRequest(conn, this::parseVerification); + return Optional.ofNullable(verification); } - // Functional implementation of InputStreamAction + // Functional implementation of InputStreamAction, used in hasJoined method in parseRequest call protected Verification parseVerification(InputStream input) throws IOException { return this.readJson(input, Verification.class);