Skip to content

Commit

Permalink
Updated MinecraftAuth API usage (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Jul 14, 2024
1 parent 43b4e1a commit d802714
Showing 1 changed file with 5 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@
import com.rtm516.mcxboxbroadcast.core.models.auth.XboxTokenInfo;
import net.lenni0451.commons.httpclient.HttpClient;
import net.raphimc.minecraftauth.MinecraftAuth;
import net.raphimc.minecraftauth.step.msa.MsaCodeStep;
import net.raphimc.minecraftauth.step.msa.StepMsaDeviceCode;
import net.raphimc.minecraftauth.step.msa.StepMsaDeviceCodeMsaCode;
import net.raphimc.minecraftauth.step.msa.StepMsaToken;
import net.raphimc.minecraftauth.step.msa.StepRefreshTokenMsaCode;
import net.raphimc.minecraftauth.step.xbl.StepXblDeviceToken;
import net.raphimc.minecraftauth.step.xbl.StepXblSisuAuthentication;
import net.raphimc.minecraftauth.step.xbl.session.StepInitialXblSession;
import net.raphimc.minecraftauth.util.JsonUtil;
import net.raphimc.minecraftauth.util.MicrosoftConstants;
import net.raphimc.minecraftauth.util.OAuthEnvironment;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -51,25 +44,15 @@ public AuthManager(String cache, Logger logger) {
* Follow the auth flow to get the Xbox token and store it
*/
private void initialise() {
// Setup the authentication steps
HttpClient httpClient = MinecraftAuth.createHttpClient();
MsaCodeStep.ApplicationDetails appDetails = new MsaCodeStep.ApplicationDetails(MicrosoftConstants.BEDROCK_ANDROID_TITLE_ID, MicrosoftConstants.SCOPE_TITLE_AUTH, null, null, OAuthEnvironment.LIVE);
StepMsaToken initialAuth = new StepMsaToken(new StepMsaDeviceCodeMsaCode(new StepMsaDeviceCode(appDetails), 120 * 1000));
StepInitialXblSession xblAuth = new StepInitialXblSession(initialAuth, new StepXblDeviceToken("Android"));
StepXblSisuAuthentication xstsAuth = new StepXblSisuAuthentication(xblAuth, MicrosoftConstants.XBL_XSTS_RELYING_PARTY);

// Check if we have an old live_token.json file and try to import the refresh token from it
if (Files.exists(oldLiveAuth)) {
logger.info("Trying to convert from old live_token.json to new cache.json");
try {
JsonObject liveToken = JsonUtil.parseString(Files.readString(oldLiveAuth)).getAsJsonObject();
JsonObject tokenData = liveToken.getAsJsonObject("token");

StepMsaToken convertInitialAuth = new StepMsaToken(new StepRefreshTokenMsaCode(appDetails));
StepInitialXblSession convertXblAuth = new StepInitialXblSession(convertInitialAuth, new StepXblDeviceToken("Android"));
StepXblSisuAuthentication convertXstsAuth = new StepXblSisuAuthentication(convertXblAuth, MicrosoftConstants.XBL_XSTS_RELYING_PARTY);

xstsToken = convertXstsAuth.getFromInput(httpClient, new StepRefreshTokenMsaCode.RefreshToken(tokenData.get("refresh_token").getAsString()));
xstsToken = MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.getFromInput(httpClient, new StepMsaToken.RefreshToken(tokenData.get("refresh_token").getAsString()));

Files.delete(oldLiveAuth);
if (Files.exists(oldXboxAuth)) Files.delete(oldXboxAuth);
Expand All @@ -80,23 +63,23 @@ private void initialise() {

// Load in cache.json if we haven't loaded one from the old auth token
try {
if (xstsToken == null && Files.exists(cache)) xstsToken = xstsAuth.fromJson(JsonUtil.parseString(Files.readString(cache)).getAsJsonObject());
if (xstsToken == null && Files.exists(cache)) xstsToken = MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.fromJson(JsonUtil.parseString(Files.readString(cache)).getAsJsonObject());
} catch (IOException e) {
logger.error("Failed to load cache.json", e);
}

try {
// Get the XSTS token or refresh it if it's expired
if (xstsToken == null) {
xstsToken = xstsAuth.getFromInput(httpClient, new StepMsaDeviceCode.MsaDeviceCodeCallback(msaDeviceCode -> {
xstsToken = MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.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.");
}));
} else if (xstsToken.isExpired()) {
xstsToken = xstsAuth.refresh(httpClient, xstsToken);
xstsToken = MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.refresh(httpClient, xstsToken);
}

// Save to cache.json
Files.writeString(cache, JsonUtil.GSON.toJson(xstsAuth.toJson(xstsToken)));
Files.writeString(cache, JsonUtil.GSON.toJson(MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.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()));
Expand Down

0 comments on commit d802714

Please sign in to comment.