Skip to content

Commit 00745e5

Browse files
committed
Move to StepRefreshTokenMsaCode instead of hacky json rebuilding
1 parent e34631a commit 00745e5

1 file changed

Lines changed: 19 additions & 57 deletions

File tree

core/src/main/java/com/rtm516/mcxboxbroadcast/core/AuthManager.java

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.raphimc.minecraftauth.step.msa.StepMsaDeviceCode;
99
import net.raphimc.minecraftauth.step.msa.StepMsaDeviceCodeMsaCode;
1010
import net.raphimc.minecraftauth.step.msa.StepMsaToken;
11+
import net.raphimc.minecraftauth.step.msa.StepRefreshTokenMsaCode;
1112
import net.raphimc.minecraftauth.step.xbl.StepXblDeviceToken;
1213
import net.raphimc.minecraftauth.step.xbl.StepXblSisuAuthentication;
1314
import net.raphimc.minecraftauth.step.xbl.session.StepInitialXblSession;
@@ -26,12 +27,6 @@ public class AuthManager {
2627
private final Path oldXboxAuth;
2728
private final Logger logger;
2829

29-
private final HttpClient httpClient;
30-
private final MsaCodeStep.ApplicationDetails appDetails;
31-
private final StepMsaToken initialAuth;
32-
private final StepXblDeviceToken stepDeviceToken;
33-
private final StepXblSisuAuthentication xstsAuth;
34-
3530
private StepXblSisuAuthentication.XblSisuTokens xstsToken;
3631
private XboxTokenInfo xboxTokenInfo;
3732

@@ -50,75 +45,42 @@ public AuthManager(String cache, Logger logger) {
5045
// Replace the default logger with one we control
5146
MinecraftAuth.LOGGER = logger.prefixed("Auth");
5247

53-
this.httpClient = MinecraftAuth.createHttpClient();
54-
55-
// Setup the authentication steps
56-
this.appDetails = new MsaCodeStep.ApplicationDetails(MicrosoftConstants.BEDROCK_ANDROID_TITLE_ID, MicrosoftConstants.SCOPE_TITLE_AUTH, null, null, OAuthEnvironment.LIVE);
57-
this.initialAuth = new StepMsaToken(new StepMsaDeviceCodeMsaCode(new StepMsaDeviceCode(this.appDetails), 120 * 1000));
58-
this.stepDeviceToken = new StepXblDeviceToken("Android");
59-
60-
StepInitialXblSession xblAuth = new StepInitialXblSession(this.initialAuth, new StepXblDeviceToken("Android"));
61-
62-
this.xstsAuth = new StepXblSisuAuthentication(xblAuth, MicrosoftConstants.XBL_XSTS_RELYING_PARTY);
63-
6448
this.xstsToken = null;
6549
}
66-
6750
/**
68-
* Convert old auth token to new format if it exists
51+
* Follow the auth flow to get the Xbox token and store it
6952
*/
70-
private void importLiveTokens() {
53+
private void initialise() {
54+
// Setup the authentication steps
55+
HttpClient httpClient = MinecraftAuth.createHttpClient();
56+
MsaCodeStep.ApplicationDetails appDetails = new MsaCodeStep.ApplicationDetails(MicrosoftConstants.BEDROCK_ANDROID_TITLE_ID, MicrosoftConstants.SCOPE_TITLE_AUTH, null, null, OAuthEnvironment.LIVE);
57+
StepMsaToken initialAuth = new StepMsaToken(new StepMsaDeviceCodeMsaCode(new StepMsaDeviceCode(appDetails), 120 * 1000));
58+
StepInitialXblSession xblAuth = new StepInitialXblSession(initialAuth, new StepXblDeviceToken("Android"));
59+
StepXblSisuAuthentication xstsAuth = new StepXblSisuAuthentication(xblAuth, MicrosoftConstants.XBL_XSTS_RELYING_PARTY);
60+
61+
// Check if we have an old live_token.json file and try to import the refresh token from it
7162
if (Files.exists(oldLiveAuth)) {
7263
logger.info("Trying to convert from old live_token.json to new cache.json");
7364
try {
7465
JsonObject liveToken = JsonUtil.parseString(Files.readString(oldLiveAuth)).getAsJsonObject();
7566
JsonObject tokenData = liveToken.getAsJsonObject("token");
7667

77-
JsonObject empty = new JsonObject();
78-
empty.addProperty("expireTimeMs", 0);
79-
empty.addProperty("token", "");
80-
empty.addProperty("titleId", "");
81-
empty.addProperty("userHash", "");
82-
83-
JsonObject msaToken = new JsonObject();
84-
msaToken.addProperty("expireTimeMs", 0);
85-
msaToken.addProperty("accessToken", tokenData.get("access_token").getAsString());
86-
msaToken.addProperty("refreshToken", tokenData.get("refresh_token").getAsString());
87-
88-
JsonObject msaCodeTemp = new JsonObject();
89-
msaCodeTemp.addProperty("clientId", appDetails.getClientId());
90-
msaCodeTemp.addProperty("scope", appDetails.getScope());
91-
msaCodeTemp.addProperty("oAuthEnvironment", appDetails.getOAuthEnvironment().name());
92-
msaToken.add("msaCode", msaCodeTemp);
93-
94-
JsonObject initialXblSession = new JsonObject();
95-
initialXblSession.add("msaToken", initialAuth.toJson(initialAuth.refresh(httpClient, initialAuth.fromJson(msaToken))));
96-
initialXblSession.add("xblDeviceToken", stepDeviceToken.toJson(stepDeviceToken.applyStep(httpClient, null)));
97-
98-
JsonObject convertedLiveToken = new JsonObject();
99-
convertedLiveToken.add("initialXblSession", initialXblSession);
100-
convertedLiveToken.add("titleToken", empty);
101-
convertedLiveToken.add("userToken", empty);
102-
convertedLiveToken.add("xstsToken", empty);
103-
104-
Files.writeString(cache, JsonUtil.GSON.toJson(convertedLiveToken));
68+
StepMsaToken convertInitialAuth = new StepMsaToken(new StepRefreshTokenMsaCode(appDetails));
69+
StepInitialXblSession convertXblAuth = new StepInitialXblSession(convertInitialAuth, new StepXblDeviceToken("Android"));
70+
StepXblSisuAuthentication convertXstsAuth = new StepXblSisuAuthentication(convertXblAuth, MicrosoftConstants.XBL_XSTS_RELYING_PARTY);
71+
72+
xstsToken = convertXstsAuth.getFromInput(httpClient, new StepRefreshTokenMsaCode.RefreshToken(tokenData.get("refresh_token").getAsString()));
73+
10574
Files.delete(oldLiveAuth);
10675
if (Files.exists(oldXboxAuth)) Files.delete(oldXboxAuth);
10776
} catch (Exception e) {
10877
logger.error("Failed to convert old auth token, if this keeps happening please remove " + oldLiveAuth + " and reauthenticate", e);
10978
}
11079
}
111-
}
112-
113-
/**
114-
* Follow the auth flow to get the Xbox token and store it
115-
*/
116-
private void initialise() {
117-
importLiveTokens();
11880

119-
// Load in cache.json
81+
// Load in cache.json if we haven't loaded one from the old auth token
12082
try {
121-
if (Files.exists(cache)) xstsToken = xstsAuth.fromJson(JsonUtil.parseString(Files.readString(cache)).getAsJsonObject());
83+
if (xstsToken == null && Files.exists(cache)) xstsToken = xstsAuth.fromJson(JsonUtil.parseString(Files.readString(cache)).getAsJsonObject());
12284
} catch (IOException e) {
12385
logger.error("Failed to load cache.json", e);
12486
}

0 commit comments

Comments
 (0)