Inceptum/src/main/java/io/gitlab/jfronny/inceptum/util/account/MicrosoftAuthAPI.java

103 lines
4.9 KiB
Java

package io.gitlab.jfronny.inceptum.util.account;
import io.gitlab.jfronny.inceptum.model.microsoft.*;
import io.gitlab.jfronny.inceptum.util.HttpUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MicrosoftAuthAPI {
public static final String MICROSOFT_LOGIN_CLIENT_ID = "90890812-00d1-48a8-8d3f-38465ef43b58";
public static final int MICROSOFT_LOGIN_REDIRECT_PORT = 28562;
public static final String MICROSOFT_LOGIN_REDIRECT_URL = "http://127.0.0.1:" + MICROSOFT_LOGIN_REDIRECT_PORT;
public static final String MICROSOFT_LOGIN_REDIRECT_URL_ENCODED = "http%3A%2F%2F127.0.0.1%3A"
+ MICROSOFT_LOGIN_REDIRECT_PORT;
public static final String[] MICROSOFT_LOGIN_SCOPES = { "XboxLive.signin", "XboxLive.offline_access" };
// General Microsoft login constants
public static final String MICROSOFT_LOGIN_URL = "https://login.live.com/oauth20_authorize.srf" + "?client_id="
+ MICROSOFT_LOGIN_CLIENT_ID
+ "&prompt=select_account&cobrandid=8058f65d-ce06-4c30-9559-473c9275a65d&response_type=code" + "&scope="
+ String.join("%20", MICROSOFT_LOGIN_SCOPES) + "&redirect_uri=" + MICROSOFT_LOGIN_REDIRECT_URL_ENCODED;
public static final String MICROSOFT_AUTH_TOKEN_URL = "https://login.live.com/oauth20_token.srf";
public static final String MICROSOFT_XBL_AUTH_TOKEN_URL = "https://user.auth.xboxlive.com/user/authenticate";
public static final String MICROSOFT_XSTS_AUTH_TOKEN_URL = "https://xsts.auth.xboxlive.com/xsts/authorize";
public static final String MICROSOFT_MINECRAFT_LOGIN_URL = "https://api.minecraftservices.com/authentication/login_with_xbox";
public static final String MICROSOFT_MINECRAFT_STORE_URL = "https://api.minecraftservices.com/entitlements/mcstore";
public static final String MICROSOFT_MINECRAFT_PROFILE_URL = "https://api.minecraftservices.com/minecraft/profile";
public static OauthTokenResponse tradeCodeForAccessToken(String code) {
return HttpUtils.post(MICROSOFT_AUTH_TOKEN_URL)
.bodyForm(Map.of("client_id", MICROSOFT_LOGIN_CLIENT_ID,
"code", code,
"grant_type", "authorization_code",
"redirect_uri", MICROSOFT_LOGIN_REDIRECT_URL,
"scope", String.join(" ", MICROSOFT_LOGIN_SCOPES)))
.sendJson(OauthTokenResponse.class);
}
public static OauthTokenResponse refreshAccessToken(String refreshToken) {
return HttpUtils.post(MICROSOFT_AUTH_TOKEN_URL)
.bodyForm(Map.of("client_id", MICROSOFT_LOGIN_CLIENT_ID,
"refresh_token", refreshToken,
"grant_type", "refresh_token",
"redirect_uri", MICROSOFT_LOGIN_REDIRECT_URL))
.sendJson(OauthTokenResponse.class);
}
public static XboxLiveAuthResponse getXBLToken(String accessToken) {
Map<Object, Object> properties = new HashMap<>();
properties.put("AuthMethod", "RPS");
properties.put("SiteName", "user.auth.xboxlive.com");
properties.put("RpsTicket", "d=" + accessToken);
Map<Object, Object> data = new HashMap<>();
data.put("Properties", properties);
data.put("RelyingParty", "http://auth.xboxlive.com");
data.put("TokenType", "JWT");
return HttpUtils.post(MICROSOFT_XBL_AUTH_TOKEN_URL)
.header("x-xbl-contract-version", "1")
.bodyJson(data)
.sendJson(XboxLiveAuthResponse.class);
}
public static XboxLiveAuthResponse getXstsToken(String xblToken) {
Map<Object, Object> properties = new HashMap<>();
properties.put("SandboxId", "RETAIL");
List<String> userTokens = new ArrayList<>();
userTokens.add(xblToken);
properties.put("UserTokens", userTokens);
Map<Object, Object> data = new HashMap<>();
data.put("Properties", properties);
data.put("RelyingParty", "rp://api.minecraftservices.com/");
data.put("TokenType", "JWT");
return HttpUtils.post(MICROSOFT_XSTS_AUTH_TOKEN_URL)
.header("x-xbl-contract-version", "1")
.bodyJson(data)
.sendJson(XboxLiveAuthResponse.class);
}
public static LoginResponse loginToMinecraft(String xstsToken) {
Map<Object, Object> data = new HashMap<Object, Object>();
data.put("identityToken", xstsToken);
return HttpUtils.post(MICROSOFT_MINECRAFT_LOGIN_URL)
.bodyJson(data)
.sendJson(LoginResponse.class);
}
public static Store getMcEntitlements(String accessToken) {
return HttpUtils.get(MICROSOFT_MINECRAFT_STORE_URL).bearer(accessToken).sendJson(Store.class);
}
public static Profile getMcProfile(String accessToken) {
return HttpUtils.get(MICROSOFT_MINECRAFT_PROFILE_URL).bearer(accessToken).sendJson(Profile.class);
}
}