Implement MSA device code step

This commit is contained in:
HoosierTransfer 2024-05-16 21:36:32 -04:00
parent 0d91493cc7
commit 49d0b603ba
8 changed files with 9988 additions and 9331 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,13 @@
package net.hoosiertransfer.EaglerXMicrosoft;
public class ApplicationDetails {
public final String CLIENT_ID;
public final String SCOPE;
public final OAuthEnvironment OAUTH_ENVIRONMENT;
public ApplicationDetails(String client_id, String scope, OAuthEnvironment oauth_environment) {
this.CLIENT_ID = client_id;
this.SCOPE = scope;
this.OAUTH_ENVIRONMENT = oauth_environment;
}
}

View File

@ -0,0 +1,25 @@
package net.hoosiertransfer.EaglerXMicrosoft;
public class MinecraftConstants {
public static final String JAVA_TITLE_ID = "00000000402b5328"; // Win32
public static final String BEDROCK_NINTENDO_TITLE_ID = "00000000441cc96b"; // Nintendo
public static final String BEDROCK_ANDROID_TITLE_ID = "0000000048183522"; // Android
public static final String BEDROCK_IOS_TITLE_ID = "000000004c17c01a"; // iOS
public static final String EDU_CLIENT_ID = "b36b1432-1a1c-4c82-9b76-24de1cab42f2"; // Win32
public static final String BEDROCK_PLAY_FAB_TITLE_ID = "20CA2";
public static final String EDU_PLAY_FAB_TITLE_ID = "6955F";
public static final String SCOPE1 = "XboxLive.signin XboxLive.offline_access";
public static final String SCOPE2 = "XboxLive.signin offline_access";
public static final String SCOPE3 = "offline_access XboxLive.signin XboxLive.offline_access";
public static final String SCOPE_TITLE_AUTH = "service::user.auth.xboxlive.com::MBI_SSL";
public static final String XBL_XSTS_RELYING_PARTY = "http://xboxlive.com";
public static final String JAVA_XSTS_RELYING_PARTY = "rp://api.minecraftservices.com/";
public static final String BEDROCK_XSTS_RELYING_PARTY = "https://multiplayer.minecraft.net/";
public static final String BEDROCK_PLAY_FAB_XSTS_RELYING_PARTY = "https://b980a380.minecraft.playfabapi.com/";
public static final String BEDROCK_REALMS_XSTS_RELYING_PARTY = "https://pocket.realms.minecraft.net/";
}

View File

@ -0,0 +1,44 @@
package net.hoosiertransfer.EaglerXMicrosoft;
public enum OAuthEnvironment {
LIVE("https://login.live.com/", "oauth20_connect.srf", "oauth20_authorize.srf", "oauth20_token.srf",
"oauth20_desktop.srf"),
MICROSOFT_ONLINE_COMMON("https://login.microsoftonline.com/common/oauth2/", "v2.0/devicecode", "v2.0/authorize",
"v2.0/token", "nativeclient"),
MICROSOFT_ONLINE_CONSUMERS("https://login.microsoftonline.com/consumers/oauth2/", "v2.0/devicecode",
"v2.0/authorize", "v2.0/token", "nativeclient"),
;
private final String baseUrl;
private final String deviceCodePath;
private final String authorizePath;
private final String tokenPath;
private final String nativeClientPath;
OAuthEnvironment(String baseUrl, String deviceCodePath, String authorizePath, String tokenPath,
String nativeClientPath) {
this.baseUrl = baseUrl;
this.deviceCodePath = deviceCodePath;
this.authorizePath = authorizePath;
this.tokenPath = tokenPath;
this.nativeClientPath = nativeClientPath;
}
public String getDeviceCodeUrl() {
return this.baseUrl + this.deviceCodePath;
}
public String getAuthorizeUrl() {
return this.baseUrl + this.authorizePath;
}
public String getTokenUrl() {
return this.baseUrl + this.tokenPath;
}
public String getNativeClientUrl() {
return this.baseUrl + this.nativeClientPath;
}
}

View File

@ -0,0 +1,15 @@
package net.hoosiertransfer.EaglerXMicrosoft.steps;
import java.util.function.Consumer;
import net.hoosiertransfer.EaglerXMicrosoft.ApplicationDetails;
public abstract class AbstractStep<T> {
protected final ApplicationDetails applicationDetails;
public AbstractStep(ApplicationDetails applicationDetails) {
this.applicationDetails = applicationDetails;
}
public abstract T execute(Consumer<T> consumer);
}

View File

@ -0,0 +1,60 @@
package net.hoosiertransfer.EaglerXMicrosoft.steps;
import java.util.function.Consumer;
import org.json.JSONObject;
import net.hoosiertransfer.EaglerXMicrosoft.ApplicationDetails;
import net.hoosiertransfer.EaglerXMicrosoft.OAuthEnvironment;
import net.hoosiertransfer.EaglerXMicrosoft.network.PostRequest;
public class StepMsaDeviceCode extends AbstractStep<StepMsaDeviceCode.MsaDeviceCode> {
public StepMsaDeviceCode(ApplicationDetails applicationDetails) {
super(applicationDetails);
}
public MsaDeviceCode execute(Consumer<MsaDeviceCode> consumer) {
PostRequest postRequest = new PostRequest();
postRequest.put("client_id", this.applicationDetails.CLIENT_ID);
postRequest.put("scope", this.applicationDetails.SCOPE);
if (this.applicationDetails.OAUTH_ENVIRONMENT == OAuthEnvironment.LIVE) {
postRequest.put("response_type", "device_code");
}
JSONObject response = postRequest.send(this.applicationDetails.OAUTH_ENVIRONMENT.getDeviceCodeUrl());
MsaDeviceCode msaDeviceCode = new MsaDeviceCode(
System.currentTimeMillis() + response.getLong("expires_in") * 1000,
response.getLong("interval") * 1000, response.getString("device_code"), response.getString("user_code"),
response.getString("verification_uri"));
consumer.accept(msaDeviceCode);
return msaDeviceCode;
}
public static class MsaDeviceCode {
long expireTimeMs;
long intervalMs;
String deviceCode;
String userCode;
String verificationUri;
public MsaDeviceCode(long expireTimeMs, long intervalMs, String deviceCode, String userCode,
String verificationUri) {
this.expireTimeMs = expireTimeMs;
this.intervalMs = intervalMs;
this.deviceCode = deviceCode;
this.userCode = userCode;
this.verificationUri = verificationUri;
}
public String getUserCode() {
return this.userCode;
}
public String getVerificationUri() {
return this.verificationUri;
}
}
}

View File

@ -1,13 +1,22 @@
package net.hoosiertransfer;
import net.hoosiertransfer.EaglerXMicrosoft.ApplicationDetails;
import net.hoosiertransfer.EaglerXMicrosoft.MinecraftConstants;
import net.hoosiertransfer.EaglerXMicrosoft.OAuthEnvironment;
import net.hoosiertransfer.EaglerXMicrosoft.network.PostRequest;
import net.hoosiertransfer.EaglerXMicrosoft.steps.StepMsaDeviceCode;
import org.json.JSONObject;
public class Main {
public static final ApplicationDetails applicationDetails = new ApplicationDetails(MinecraftConstants.JAVA_TITLE_ID,
MinecraftConstants.SCOPE_TITLE_AUTH, OAuthEnvironment.LIVE);
public static void main(String[] args) {
PostRequest postRequest = new PostRequest();
postRequest.put("key", "value");
JSONObject response = postRequest.send("https://login.live.com/oauth20_token.srf");
System.out.println(response);
StepMsaDeviceCode stepMsaDeviceCode = new StepMsaDeviceCode(applicationDetails);
StepMsaDeviceCode.MsaDeviceCode msaDeviceCode = stepMsaDeviceCode.execute((deviceCode) -> {
System.out.println(
"Please visit " + deviceCode.getVerificationUri() + " and enter code " + deviceCode.getUserCode());
});
}
}