From 00ce8b385c090dab076ce72cfc327cf09d800026 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Wed, 19 Jun 2024 21:20:37 +0100 Subject: [PATCH] More old code removal --- .../bootstrap/standalone/StandaloneMain.java | 1 - .../mcxboxbroadcast/core/AuthManager.java | 29 +++++++---- .../mcxboxbroadcast/core/Constants.java | 8 --- .../rtm516/mcxboxbroadcast/core/Utils.java | 50 ------------------- .../LiveAuthenticationException.java | 7 --- .../models/session/JoinSessionRequest.java | 1 - .../session/SessionCustomProperties.java | 3 -- 7 files changed, 20 insertions(+), 79 deletions(-) delete mode 100644 core/src/main/java/com/rtm516/mcxboxbroadcast/core/Utils.java delete mode 100644 core/src/main/java/com/rtm516/mcxboxbroadcast/core/exceptions/LiveAuthenticationException.java diff --git a/bootstrap/standalone/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/standalone/StandaloneMain.java b/bootstrap/standalone/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/standalone/StandaloneMain.java index 1ac125c..cbe3092 100644 --- a/bootstrap/standalone/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/standalone/StandaloneMain.java +++ b/bootstrap/standalone/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/standalone/StandaloneMain.java @@ -6,7 +6,6 @@ import com.nukkitx.protocol.bedrock.BedrockPong; import com.rtm516.mcxboxbroadcast.core.SessionInfo; import com.rtm516.mcxboxbroadcast.core.SessionManager; -import com.rtm516.mcxboxbroadcast.core.SubSessionManager; import com.rtm516.mcxboxbroadcast.core.configs.StandaloneConfig; import com.rtm516.mcxboxbroadcast.core.exceptions.SessionCreationException; import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException; diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/AuthManager.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/AuthManager.java index 564d873..1512601 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/AuthManager.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/AuthManager.java @@ -14,7 +14,6 @@ import net.raphimc.minecraftauth.util.JsonUtil; import net.raphimc.minecraftauth.util.MicrosoftConstants; import net.raphimc.minecraftauth.util.OAuthEnvironment; -import net.raphimc.minecraftauth.util.logging.ILogger; import java.io.IOException; import java.nio.file.Files; @@ -48,19 +47,21 @@ public AuthManager(String cache, Logger logger) { this.oldXboxAuth = Paths.get(cache, "xbox_token.json"); this.logger = logger; + // Replace the default logger with one we control MinecraftAuth.LOGGER = logger.prefixed("Auth"); - httpClient = MinecraftAuth.createHttpClient(); + this.httpClient = MinecraftAuth.createHttpClient(); - appDetails = new MsaCodeStep.ApplicationDetails(MicrosoftConstants.BEDROCK_ANDROID_TITLE_ID, MicrosoftConstants.SCOPE_TITLE_AUTH, null, null, OAuthEnvironment.LIVE); - initialAuth = new StepMsaToken(new StepMsaDeviceCodeMsaCode(new StepMsaDeviceCode(appDetails), 120 * 1000)); - stepDeviceToken = new StepXblDeviceToken("Android"); + // Setup the authentication steps + this.appDetails = new MsaCodeStep.ApplicationDetails(MicrosoftConstants.BEDROCK_ANDROID_TITLE_ID, MicrosoftConstants.SCOPE_TITLE_AUTH, null, null, OAuthEnvironment.LIVE); + this.initialAuth = new StepMsaToken(new StepMsaDeviceCodeMsaCode(new StepMsaDeviceCode(this.appDetails), 120 * 1000)); + this.stepDeviceToken = new StepXblDeviceToken("Android"); - StepInitialXblSession xblAuth = new StepInitialXblSession(initialAuth, new StepXblDeviceToken("Android")); + StepInitialXblSession xblAuth = new StepInitialXblSession(this.initialAuth, new StepXblDeviceToken("Android")); - xstsAuth = new StepXblSisuAuthentication(xblAuth, MicrosoftConstants.XBL_XSTS_RELYING_PARTY); + this.xstsAuth = new StepXblSisuAuthentication(xblAuth, MicrosoftConstants.XBL_XSTS_RELYING_PARTY); - xstsToken = null; + this.xstsToken = null; } /** @@ -109,6 +110,9 @@ private void importLiveTokens() { } } + /** + * Follow the auth flow to get the Xbox token and store it + */ private void initialise() { importLiveTokens(); @@ -120,6 +124,7 @@ private void initialise() { } try { + // Get the XSTS token or refresh it if it's expired if (xstsToken == null) { xstsToken = xstsAuth.getFromInput(httpClient, new StepMsaDeviceCode.MsaDeviceCodeCallback(msaDeviceCode -> { logger.info("To sign in, use a web browser to open the page " + msaDeviceCode.getVerificationUri() + " and enter the code " + msaDeviceCode.getUserCode() + " to authenticate."); @@ -131,13 +136,19 @@ private void initialise() { // Save to cache.json Files.writeString(cache, JsonUtil.GSON.toJson(xstsAuth.toJson(xstsToken))); + // Construct and store the Xbox token info xboxTokenInfo = new XboxTokenInfo(xstsToken.getDisplayClaims().get("xid"), xstsToken.getUserHash(), xstsToken.getDisplayClaims().get("gtg"), xstsToken.getToken(), String.valueOf(xstsToken.getExpireTimeMs())); - } catch (Exception e) { throw new RuntimeException(e); } } + /** + * Get the Xbox token info + * If the token is expired or missing then refresh it + * + * @return The Xbox token info + */ public XboxTokenInfo getXboxToken() { if (xstsToken == null || xboxTokenInfo == null || xstsToken.isExpired()) { initialise(); diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/Constants.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/Constants.java index e128e9b..59e02f2 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/Constants.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/Constants.java @@ -9,18 +9,10 @@ public class Constants { public static final Gson GSON = new GsonBuilder().registerTypeAdapter(Instant.class, new InstantConverter()).create(); - public static final String AUTH_TITLE = "0000000048183522"; // 00000000441cc96b Nintendo Switch, 0000000048183522 Android - public static final String SCOPE = "service::user.auth.xboxlive.com::MBI_SSL"; - public static final String RELAYING_PARTY = "http://xboxlive.com"; - public static final String SERVICE_CONFIG_ID = "4fc10100-5f7a-4470-899b-280835760c07"; // The service config ID for Minecraft public static final String CREATE_SESSION = "https://sessiondirectory.xboxlive.com/serviceconfigs/" + SERVICE_CONFIG_ID + "/sessionTemplates/MinecraftLobby/sessions/%s"; public static final String JOIN_SESSION = "https://sessiondirectory.xboxlive.com/handles/%s/session"; - public static final URI LIVE_DEVICE_CODE_REQUEST = URI.create("https://login.live.com/oauth20_connect.srf"); - public static final URI LIVE_TOKEN_REQUEST = URI.create("https://login.live.com/oauth20_token.srf"); - public static final URI DEVICE_AUTHENTICATE_REQUEST = URI.create("https://device.auth.xboxlive.com/device/authenticate"); - public static final URI XSTS_AUTHENTICATE_REQUEST = URI.create("https://xsts.auth.xboxlive.com/xsts/authorize"); public static final URI RTA_WEBSOCKET = URI.create("wss://rta.xboxlive.com/connect"); public static final URI CREATE_HANDLE = URI.create("https://sessiondirectory.xboxlive.com/handles"); diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/Utils.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/Utils.java deleted file mode 100644 index d521414..0000000 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/Utils.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.rtm516.mcxboxbroadcast.core; - -import java.io.PrintWriter; -import java.io.StringWriter; - -public class Utils { - public static String getStackTrace(Throwable ex) { - // Create a PrintWriter to write the stack trace to - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - - // Write the stack trace to the PrintWriter - ex.printStackTrace(pw); - - // Close the PrintWriter - pw.close(); - - // Return the stack trace as a string - return sw.toString(); - } - - /** - * Get the error message for the Xbox error code - *

- * Ported from HashimTheArab/gophertunnel - * - * @param code The Xbox error code - * @return The error message for the Xbox error code - */ - public static String getXboxErrorCode(String code) { - return switch (code) { - case "2148916227" -> - "Your account was banned by Xbox for violating one or more Community Standards for Xbox and is unable to be used."; - case "2148916229" -> - "Your account is currently restricted and your guardian has not given you permission to play online. Login to https://account.microsoft.com/family/ and have your guardian change your permissions."; - case "2148916233" -> - "Your account currently does not have an Xbox profile. Please create one at https://www.xbox.com/live"; - case "2148916234" -> "Your account has not accepted Xbox's Terms of Service. Please login and accept them."; - case "2148916235" -> - "Your account resides in a region that Xbox has not authorized use from. Xbox has blocked your attempt at logging in."; - case "2148916236" -> - "Your account requires proof of age. Please login to https://login.live.com/login.srf and provide proof of age."; - case "2148916237" -> - "Your account has reached its limit for playtime. Your account has been blocked from logging in."; - case "2148916238" -> - "The account date of birth is under 18 years and cannot proceed unless the account is added to a family by an adult."; - default -> ""; - }; - } -} diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/exceptions/LiveAuthenticationException.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/exceptions/LiveAuthenticationException.java deleted file mode 100644 index 2e121c1..0000000 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/exceptions/LiveAuthenticationException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.rtm516.mcxboxbroadcast.core.exceptions; - -public class LiveAuthenticationException extends Exception { - public LiveAuthenticationException(String message) { - super(message); - } -} diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/models/session/JoinSessionRequest.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/models/session/JoinSessionRequest.java index 7c637b6..ce71bba 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/models/session/JoinSessionRequest.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/models/session/JoinSessionRequest.java @@ -8,7 +8,6 @@ import java.util.HashMap; import java.util.Map; -import java.util.UUID; public class JoinSessionRequest { public Map members; diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/models/session/SessionCustomProperties.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/models/session/SessionCustomProperties.java index d4ddda8..51bcc40 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/models/session/SessionCustomProperties.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/models/session/SessionCustomProperties.java @@ -1,8 +1,5 @@ package com.rtm516.mcxboxbroadcast.core.models.session; -import com.rtm516.mcxboxbroadcast.core.ExpandedSessionInfo; - -import java.util.Collections; import java.util.List; public record SessionCustomProperties(