mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-16 08:05:34 -04:00
[MSA new] Throw exception when response error; Progress bar re-impl
This commit is contained in:
parent
3aa5621c97
commit
83d6f64958
@ -19,12 +19,14 @@ import net.kdt.pojavlaunch.value.launcherprofiles.*;
|
|||||||
import net.kdt.pojavlaunch.value.*;
|
import net.kdt.pojavlaunch.value.*;
|
||||||
|
|
||||||
public class MicrosoftAuthTask extends AsyncTask<String, Void, Object> {
|
public class MicrosoftAuthTask extends AsyncTask<String, Void, Object> {
|
||||||
|
/*
|
||||||
private static final String authTokenUrl = "https://login.live.com/oauth20_token.srf";
|
private static final String authTokenUrl = "https://login.live.com/oauth20_token.srf";
|
||||||
private static final String xblAuthUrl = "https://user.auth.xboxlive.com/user/authenticate";
|
private static final String xblAuthUrl = "https://user.auth.xboxlive.com/user/authenticate";
|
||||||
private static final String xstsAuthUrl = "https://xsts.auth.xboxlive.com/xsts/authorize";
|
private static final String xstsAuthUrl = "https://xsts.auth.xboxlive.com/xsts/authorize";
|
||||||
private static final String mcLoginUrl = "https://api.minecraftservices.com/authentication/login_with_xbox";
|
private static final String mcLoginUrl = "https://api.minecraftservices.com/authentication/login_with_xbox";
|
||||||
// private static final String mcStoreUrl = "https://api.minecraftservices.com/entitlements/mcstore";
|
private static final String mcStoreUrl = "https://api.minecraftservices.com/entitlements/mcstore";
|
||||||
private static final String mcProfileUrl = "https://api.minecraftservices.com/minecraft/profile";
|
private static final String mcProfileUrl = "https://api.minecraftservices.com/minecraft/profile";
|
||||||
|
*/
|
||||||
|
|
||||||
//private Gson gson = new Gson();
|
//private Gson gson = new Gson();
|
||||||
private RefreshListener listener;
|
private RefreshListener listener;
|
||||||
@ -43,14 +45,13 @@ public class MicrosoftAuthTask extends AsyncTask<String, Void, Object> {
|
|||||||
build.setMessage(ctx.getString(R.string.global_waiting));
|
build.setMessage(ctx.getString(R.string.global_waiting));
|
||||||
build.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
build.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
||||||
build.setCancelable(false);
|
build.setCancelable(false);
|
||||||
build.setMax(5);
|
build.setMax(6);
|
||||||
build.show();
|
build.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object doInBackground(String... args) {
|
public Object doInBackground(String... args) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
String authCode = args[0];
|
String authCode = args[0];
|
||||||
/*
|
/*
|
||||||
publishProgress();
|
publishProgress();
|
||||||
@ -68,13 +69,13 @@ public class MicrosoftAuthTask extends AsyncTask<String, Void, Object> {
|
|||||||
publishProgress();
|
publishProgress();
|
||||||
|
|
||||||
*/
|
*/
|
||||||
Msa msa = new Msa(authCode);
|
Msa msa = new Msa(this, authCode);
|
||||||
|
|
||||||
// TODO migrate account format to json
|
// TODO migrate account format to json
|
||||||
//MinecraftAccount acc = new MinecraftAccount();
|
//MinecraftAccount acc = new MinecraftAccount();
|
||||||
|
|
||||||
MCProfile.Builder profilePath = new MCProfile.Builder();
|
MCProfile.Builder profilePath = new MCProfile.Builder();
|
||||||
if(msa.doesOwnGame) {
|
if (msa.doesOwnGame) {
|
||||||
profilePath.setClientID("0" /* FIXME */);
|
profilePath.setClientID("0" /* FIXME */);
|
||||||
profilePath.setAccessToken(msa.mcToken);
|
profilePath.setAccessToken(msa.mcToken);
|
||||||
profilePath.setUsername(msa.mcName);
|
profilePath.setUsername(msa.mcName);
|
||||||
@ -82,7 +83,6 @@ public class MicrosoftAuthTask extends AsyncTask<String, Void, Object> {
|
|||||||
profilePath.setIsMojangAccount(false);
|
profilePath.setIsMojangAccount(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MCProfile.build(profilePath);
|
MCProfile.build(profilePath);
|
||||||
|
|
||||||
return profilePath;
|
return profilePath;
|
||||||
@ -91,6 +91,10 @@ public class MicrosoftAuthTask extends AsyncTask<String, Void, Object> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void publishProgressPublic() {
|
||||||
|
super.publishProgress();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onProgressUpdate(Void[] p1) {
|
protected void onProgressUpdate(Void[] p1) {
|
||||||
super.onProgressUpdate(p1);
|
super.onProgressUpdate(p1);
|
||||||
|
@ -29,294 +29,308 @@ import java.util.UUID;
|
|||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import android.os.*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class Msa {
|
public class Msa {
|
||||||
|
/*
|
||||||
|
private static final String loginUrl = "https://login.live.com/oauth20_authorize.srf" +
|
||||||
|
"?client_id=00000000402b5328" +
|
||||||
|
"&response_type=code" +
|
||||||
|
"&scope=service%3A%3Auser.auth.xboxlive.com%3A%3AMBI_SSL" +
|
||||||
|
"&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf";
|
||||||
|
|
||||||
private static final String loginUrl = "https://login.live.com/oauth20_authorize.srf" +
|
private static final String redirectUrlSuffix = "https://login.live.com/oauth20_desktop.srf?code=";
|
||||||
"?client_id=00000000402b5328" +
|
*/
|
||||||
"&response_type=code" +
|
|
||||||
"&scope=service%3A%3Auser.auth.xboxlive.com%3A%3AMBI_SSL" +
|
|
||||||
"&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf";
|
|
||||||
|
|
||||||
private static final String redirectUrlSuffix = "https://login.live.com/oauth20_desktop.srf?code=";
|
|
||||||
|
|
||||||
private static final String authTokenUrl = "https://login.live.com/oauth20_token.srf";
|
private static final String authTokenUrl = "https://login.live.com/oauth20_token.srf";
|
||||||
|
|
||||||
private static final String xblAuthUrl = "https://user.auth.xboxlive.com/user/authenticate";
|
private static final String xblAuthUrl = "https://user.auth.xboxlive.com/user/authenticate";
|
||||||
|
|
||||||
private static final String xstsAuthUrl = "https://xsts.auth.xboxlive.com/xsts/authorize";
|
private static final String xstsAuthUrl = "https://xsts.auth.xboxlive.com/xsts/authorize";
|
||||||
|
|
||||||
private static final String mcLoginUrl = "https://api.minecraftservices.com/authentication/login_with_xbox";
|
private static final String mcLoginUrl = "https://api.minecraftservices.com/authentication/login_with_xbox";
|
||||||
|
|
||||||
private static final String mcStoreUrl = "https://api.minecraftservices.com/entitlements/mcstore";
|
private static final String mcStoreUrl = "https://api.minecraftservices.com/entitlements/mcstore";
|
||||||
|
|
||||||
private static final String mcProfileUrl = "https://api.minecraftservices.com/minecraft/profile";
|
private static final String mcProfileUrl = "https://api.minecraftservices.com/minecraft/profile";
|
||||||
|
|
||||||
|
private MicrosoftAuthTask task;
|
||||||
|
|
||||||
public String mcName;
|
public String mcName;
|
||||||
public String mcToken;
|
public String mcToken;
|
||||||
public String mcUuid;
|
public String mcUuid;
|
||||||
public boolean doesOwnGame;
|
public boolean doesOwnGame;
|
||||||
public Msa(String authCode) throws IOException, JSONException {
|
|
||||||
|
public Msa(MicrosoftAuthTask task, String authCode) throws IOException, JSONException {
|
||||||
|
this.task = task;
|
||||||
acquireAccessToken(authCode);
|
acquireAccessToken(authCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void acquireAccessToken(String authcode) throws IOException, JSONException {
|
public void acquireAccessToken(String authcode) throws IOException, JSONException {
|
||||||
|
task.publishProgressPublic();
|
||||||
|
|
||||||
URL url = new URL(authTokenUrl);
|
URL url = new URL(authTokenUrl);
|
||||||
Log.i("MicroAuth","authCode= "+authcode);
|
Log.i("MicroAuth","authCode= "+authcode);
|
||||||
Map<Object, Object> data = new HashMap();/*Map.of(
|
Map<Object, Object> data = new HashMap<>();/*Map.of(
|
||||||
"client_id", "00000000402b5328",
|
"client_id", "00000000402b5328",
|
||||||
"code", authcode,
|
"code", authcode,
|
||||||
"grant_type", "authorization_code",
|
"grant_type", "authorization_code",
|
||||||
"redirect_uri", "https://login.live.com/oauth20_desktop.srf",
|
"redirect_uri", "https://login.live.com/oauth20_desktop.srf",
|
||||||
"scope", "service::user.auth.xboxlive.com::MBI_SSL"
|
"scope", "service::user.auth.xboxlive.com::MBI_SSL"
|
||||||
);*/
|
);*/
|
||||||
data.put("client_id", "00000000402b5328");
|
data.put("client_id", "00000000402b5328");
|
||||||
data.put("code", authcode);
|
data.put("code", authcode);
|
||||||
data.put("grant_type", "authorization_code");
|
data.put("grant_type", "authorization_code");
|
||||||
data.put("redirect_uri", "https://login.live.com/oauth20_desktop.srf");
|
data.put("redirect_uri", "https://login.live.com/oauth20_desktop.srf");
|
||||||
data.put("scope", "service::user.auth.xboxlive.com::MBI_SSL");
|
data.put("scope", "service::user.auth.xboxlive.com::MBI_SSL");
|
||||||
|
|
||||||
//да пошла yf[eq1 она ваша джава 11
|
//да пошла yf[eq1 она ваша джава 11
|
||||||
String req = ofFormData(data);
|
String req = ofFormData(data);
|
||||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||||
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||||
conn.setRequestProperty("charset", "utf-8");
|
conn.setRequestProperty("charset", "utf-8");
|
||||||
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
|
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
|
||||||
conn.setRequestMethod("POST");
|
conn.setRequestMethod("POST");
|
||||||
conn.setUseCaches(false);
|
conn.setUseCaches(false);
|
||||||
conn.setDoInput(true);
|
conn.setDoInput(true);
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
conn.connect();
|
conn.connect();
|
||||||
try(OutputStream wr = conn.getOutputStream()) {
|
try(OutputStream wr = conn.getOutputStream()) {
|
||||||
wr.write(req.getBytes("UTF-8"));
|
wr.write(req.getBytes("UTF-8"));
|
||||||
}
|
}
|
||||||
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
||||||
String s = ""; int len = 0; byte[] buf = new byte[256];
|
String s = ""; int len = 0; byte[] buf = new byte[256];
|
||||||
InputStream is = conn.getInputStream();
|
InputStream is = conn.getInputStream();
|
||||||
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
||||||
s += new String(buf,0,len);
|
s += new String(buf,0,len);
|
||||||
}
|
|
||||||
JSONObject jo = new JSONObject(s);
|
|
||||||
Log.i("MicroAuth","Acess Token = "+jo.getString("access_token"));
|
|
||||||
acquireXBLToken(jo.getString("access_token"));
|
|
||||||
}else{
|
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
|
||||||
}
|
}
|
||||||
|
JSONObject jo = new JSONObject(s);
|
||||||
|
Log.i("MicroAuth","Acess Token = "+jo.getString("access_token"));
|
||||||
|
acquireXBLToken(jo.getString("access_token"));
|
||||||
|
}else{
|
||||||
|
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
||||||
|
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void acquireXBLToken(String accessToken) throws IOException, JSONException {
|
private void acquireXBLToken(String accessToken) throws IOException, JSONException {
|
||||||
|
task.publishProgressPublic();
|
||||||
|
|
||||||
URL url = new URL(xblAuthUrl);
|
URL url = new URL(xblAuthUrl);
|
||||||
|
|
||||||
Map<Object, Object> data = new HashMap();
|
Map<Object, Object> data = new HashMap<>();
|
||||||
Map<Object, Object> properties = new HashMap();
|
Map<Object, Object> properties = new HashMap<>();
|
||||||
properties.put("AuthMethod", "RPS");
|
properties.put("AuthMethod", "RPS");
|
||||||
properties.put("SiteName", "user.auth.xboxlive.com");
|
properties.put("SiteName", "user.auth.xboxlive.com");
|
||||||
properties.put("RpsTicket", accessToken);
|
properties.put("RpsTicket", accessToken);
|
||||||
data.put("Properties",properties);
|
data.put("Properties",properties);
|
||||||
data.put("RelyingParty", "http://auth.xboxlive.com");
|
data.put("RelyingParty", "http://auth.xboxlive.com");
|
||||||
data.put("TokenType", "JWT");
|
data.put("TokenType", "JWT");
|
||||||
/*Map.of(
|
/*Map.of(
|
||||||
|
|
||||||
"Properties", Map.of(
|
"Properties", Map.of(
|
||||||
"AuthMethod", "RPS",
|
"AuthMethod", "RPS",
|
||||||
"SiteName", "user.auth.xboxlive.com",
|
"SiteName", "user.auth.xboxlive.com",
|
||||||
"RpsTicket", accessToken
|
"RpsTicket", accessToken
|
||||||
),
|
),
|
||||||
"RelyingParty", "http://auth.xboxlive.com",
|
"RelyingParty", "http://auth.xboxlive.com",
|
||||||
"TokenType", "JWT"
|
"TokenType", "JWT"
|
||||||
);*/
|
);*/
|
||||||
String req = ofJSONData(data);
|
String req = ofJSONData(data);
|
||||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||||
conn.setRequestProperty("Content-Type", "application/json");
|
conn.setRequestProperty("Content-Type", "application/json");
|
||||||
conn.setRequestProperty("Accept", "application/json");
|
conn.setRequestProperty("Accept", "application/json");
|
||||||
conn.setRequestProperty("charset", "utf-8");
|
conn.setRequestProperty("charset", "utf-8");
|
||||||
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
|
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
|
||||||
conn.setRequestMethod("POST");
|
conn.setRequestMethod("POST");
|
||||||
conn.setUseCaches(false);
|
conn.setUseCaches(false);
|
||||||
conn.setDoInput(true);
|
conn.setDoInput(true);
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
conn.connect();
|
conn.connect();
|
||||||
try(OutputStream wr = conn.getOutputStream()) {
|
try(OutputStream wr = conn.getOutputStream()) {
|
||||||
wr.write(req.getBytes("UTF-8"));
|
wr.write(req.getBytes("UTF-8"));
|
||||||
}
|
}
|
||||||
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
||||||
String s = ""; int len = 0; byte[] buf = new byte[256];
|
String s = ""; int len = 0; byte[] buf = new byte[256];
|
||||||
InputStream is = conn.getInputStream();
|
InputStream is = conn.getInputStream();
|
||||||
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
||||||
s += new String(buf,0,len);
|
s += new String(buf,0,len);
|
||||||
}
|
|
||||||
JSONObject jo = new JSONObject(s);
|
|
||||||
Log.i("MicroAuth","Xbl Token = "+jo.getString("Token"));
|
|
||||||
acquireXsts(jo.getString("Token"));
|
|
||||||
}else{
|
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
|
||||||
}
|
}
|
||||||
|
JSONObject jo = new JSONObject(s);
|
||||||
|
Log.i("MicroAuth","Xbl Token = "+jo.getString("Token"));
|
||||||
|
acquireXsts(jo.getString("Token"));
|
||||||
|
}else{
|
||||||
|
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
||||||
|
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void acquireXsts(String xblToken) throws IOException, JSONException {
|
private void acquireXsts(String xblToken) throws IOException, JSONException {
|
||||||
URL url = new URL(xstsAuthUrl);
|
task.publishProgressPublic();
|
||||||
Map<Object, Object> data = new HashMap();
|
|
||||||
Map<Object, Object> properties = new HashMap();
|
|
||||||
properties.put("SandboxId", "RETAIL");
|
|
||||||
properties.put("UserTokens",Collections.singleton(xblToken));
|
|
||||||
data.put("Properties",properties);
|
|
||||||
data.put("RelyingParty", "rp://api.minecraftservices.com/");
|
|
||||||
data.put("TokenType", "JWT");
|
|
||||||
/*Map<Object, Object> data = Map.of(
|
|
||||||
"Properties", Map.of(
|
|
||||||
"SandboxId", "RETAIL",
|
|
||||||
"UserTokens", List.of(xblToken)
|
|
||||||
),
|
|
||||||
"RelyingParty", "rp://api.minecraftservices.com/",
|
|
||||||
"TokenType", "JWT"
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
String req = ofJSONData(data);
|
|
||||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
||||||
conn.setRequestProperty("Content-Type", "application/json");
|
|
||||||
conn.setRequestProperty("Accept", "application/json");
|
|
||||||
conn.setRequestProperty("charset", "utf-8");
|
|
||||||
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
|
|
||||||
conn.setRequestMethod("POST");
|
|
||||||
conn.setUseCaches(false);
|
|
||||||
conn.setDoInput(true);
|
|
||||||
conn.setDoOutput(true);
|
|
||||||
conn.connect();
|
|
||||||
try(OutputStream wr = conn.getOutputStream()) {
|
|
||||||
wr.write(req.getBytes("UTF-8"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
URL url = new URL(xstsAuthUrl);
|
||||||
String s = ""; int len = 0; byte[] buf = new byte[256];
|
Map<Object, Object> data = new HashMap<>();
|
||||||
InputStream is = conn.getInputStream();
|
Map<Object, Object> properties = new HashMap<>();
|
||||||
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
properties.put("SandboxId", "RETAIL");
|
||||||
s += new String(buf,0,len);
|
properties.put("UserTokens",Collections.singleton(xblToken));
|
||||||
}
|
data.put("Properties",properties);
|
||||||
JSONObject jo = new JSONObject(s);
|
data.put("RelyingParty", "rp://api.minecraftservices.com/");
|
||||||
String uhs = jo.getJSONObject("DisplayClaims").getJSONArray("xui").getJSONObject(0).getString("uhs");
|
data.put("TokenType", "JWT");
|
||||||
Log.i("MicroAuth","Xbl Xsts = "+jo.getString("Token")+"; Uhs = " + uhs);
|
/*Map<Object, Object> data = Map.of(
|
||||||
acquireMinecraftToken(uhs,jo.getString("Token"));
|
"Properties", Map.of(
|
||||||
}else{
|
"SandboxId", "RETAIL",
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
"UserTokens", List.of(xblToken)
|
||||||
|
),
|
||||||
|
"RelyingParty", "rp://api.minecraftservices.com/",
|
||||||
|
"TokenType", "JWT"
|
||||||
|
);
|
||||||
|
*/
|
||||||
|
String req = ofJSONData(data);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||||
|
conn.setRequestProperty("Content-Type", "application/json");
|
||||||
|
conn.setRequestProperty("Accept", "application/json");
|
||||||
|
conn.setRequestProperty("charset", "utf-8");
|
||||||
|
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
conn.setUseCaches(false);
|
||||||
|
conn.setDoInput(true);
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.connect();
|
||||||
|
try(OutputStream wr = conn.getOutputStream()) {
|
||||||
|
wr.write(req.getBytes("UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
||||||
|
String s = ""; int len = 0; byte[] buf = new byte[256];
|
||||||
|
InputStream is = conn.getInputStream();
|
||||||
|
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
||||||
|
s += new String(buf,0,len);
|
||||||
}
|
}
|
||||||
|
JSONObject jo = new JSONObject(s);
|
||||||
|
String uhs = jo.getJSONObject("DisplayClaims").getJSONArray("xui").getJSONObject(0).getString("uhs");
|
||||||
|
Log.i("MicroAuth","Xbl Xsts = "+jo.getString("Token")+"; Uhs = " + uhs);
|
||||||
|
acquireMinecraftToken(uhs,jo.getString("Token"));
|
||||||
|
}else{
|
||||||
|
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
||||||
|
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void acquireMinecraftToken(String xblUhs, String xblXsts) throws IOException, JSONException {
|
private void acquireMinecraftToken(String xblUhs, String xblXsts) throws IOException, JSONException {
|
||||||
URL url = new URL(mcLoginUrl);
|
task.publishProgressPublic();
|
||||||
|
|
||||||
Map<Object, Object> data = new HashMap();
|
URL url = new URL(mcLoginUrl);
|
||||||
data.put( "identityToken", "XBL3.0 x=" + xblUhs + ";" + xblXsts);
|
|
||||||
|
|
||||||
String req = ofJSONData(data);
|
Map<Object, Object> data = new HashMap<>();
|
||||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
data.put("identityToken", "XBL3.0 x=" + xblUhs + ";" + xblXsts);
|
||||||
conn.setRequestProperty("Content-Type", "application/json");
|
|
||||||
conn.setRequestProperty("Accept", "application/json");
|
String req = ofJSONData(data);
|
||||||
conn.setRequestProperty("charset", "utf-8");
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||||
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
|
conn.setRequestProperty("Content-Type", "application/json");
|
||||||
conn.setRequestMethod("POST");
|
conn.setRequestProperty("Accept", "application/json");
|
||||||
conn.setUseCaches(false);
|
conn.setRequestProperty("charset", "utf-8");
|
||||||
conn.setDoInput(true);
|
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
|
||||||
conn.setDoOutput(true);
|
conn.setRequestMethod("POST");
|
||||||
conn.connect();
|
conn.setUseCaches(false);
|
||||||
try(OutputStream wr = conn.getOutputStream()) {
|
conn.setDoInput(true);
|
||||||
wr.write(req.getBytes("UTF-8"));
|
conn.setDoOutput(true);
|
||||||
|
conn.connect();
|
||||||
|
try(OutputStream wr = conn.getOutputStream()) {
|
||||||
|
wr.write(req.getBytes("UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
||||||
|
String s = ""; int len = 0; byte[] buf = new byte[256];
|
||||||
|
InputStream is = conn.getInputStream();
|
||||||
|
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
||||||
|
s += new String(buf,0,len);
|
||||||
}
|
}
|
||||||
|
JSONObject jo = new JSONObject(s);
|
||||||
|
Log.i("MicroAuth","MC token: "+jo.getString("access_token"));
|
||||||
|
mcToken = jo.getString("access_token");
|
||||||
|
checkMcProfile(jo.getString("access_token"));
|
||||||
|
checkMcStore(jo.getString("access_token"));
|
||||||
|
|
||||||
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
}else{
|
||||||
String s = ""; int len = 0; byte[] buf = new byte[256];
|
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
||||||
InputStream is = conn.getInputStream();
|
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
||||||
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
}
|
||||||
s += new String(buf,0,len);
|
|
||||||
}
|
|
||||||
JSONObject jo = new JSONObject(s);
|
|
||||||
Log.i("MicroAuth","MC token: "+jo.getString("access_token"));
|
|
||||||
mcToken = jo.getString("access_token");
|
|
||||||
checkMcProfile(jo.getString("access_token"));
|
|
||||||
checkMcStore(jo.getString("access_token"));
|
|
||||||
|
|
||||||
}else{
|
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private void checkMcStore(String mcAccessToken) throws IOException, JSONException {
|
private void checkMcStore(String mcAccessToken) throws IOException, JSONException {
|
||||||
URL url = new URL(mcStoreUrl);
|
task.publishProgressPublic();
|
||||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
||||||
conn.setRequestProperty("Authorization", "Bearer " + mcAccessToken);
|
|
||||||
conn.setRequestMethod("GET");
|
|
||||||
conn.setUseCaches(false);
|
|
||||||
conn.connect();
|
|
||||||
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
|
||||||
String s = ""; int len = 0; byte[] buf = new byte[256];
|
|
||||||
InputStream is = conn.getInputStream();
|
|
||||||
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
|
||||||
s += new String(buf,0,len);
|
|
||||||
}
|
|
||||||
JSONObject jo = new JSONObject(s);
|
|
||||||
JSONArray ja = jo.getJSONArray("items");
|
|
||||||
Log.i("MicroAuth","Store Len = " + ja.length());
|
|
||||||
for(int i = 0; i < ja.length(); i++) {
|
|
||||||
String prod = ja.getJSONObject(i).getString("name");
|
|
||||||
Log.i("MicroAuth","Product " + i +": " +prod);
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
HttpRequest request = HttpRequest.newBuilder(uri)
|
|
||||||
.header("Authorization", "Bearer " + mcAccessToken)
|
|
||||||
.GET().build();
|
|
||||||
|
|
||||||
HttpClient.newBuilder().build().sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(resp -> {
|
URL url = new URL(mcStoreUrl);
|
||||||
if (resp.statusCode() >= 200 && resp.statusCode() < 300) {
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||||
String body = resp.body();
|
conn.setRequestProperty("Authorization", "Bearer " + mcAccessToken);
|
||||||
Log.i("MicroAuth","store: " + body);
|
conn.setRequestMethod("GET");
|
||||||
}
|
conn.setUseCaches(false);
|
||||||
});
|
conn.connect();
|
||||||
*/
|
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
||||||
|
String s = ""; int len = 0; byte[] buf = new byte[256];
|
||||||
|
InputStream is = conn.getInputStream();
|
||||||
|
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
||||||
|
s += new String(buf,0,len);
|
||||||
|
}
|
||||||
|
JSONObject jo = new JSONObject(s);
|
||||||
|
JSONArray ja = jo.getJSONArray("items");
|
||||||
|
Log.i("MicroAuth","Store Len = " + ja.length());
|
||||||
|
for(int i = 0; i < ja.length(); i++) {
|
||||||
|
String prod = ja.getJSONObject(i).getString("name");
|
||||||
|
Log.i("MicroAuth","Product " + i +": " +prod);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
||||||
|
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
HttpRequest request = HttpRequest.newBuilder(uri)
|
||||||
|
.header("Authorization", "Bearer " + mcAccessToken)
|
||||||
|
.GET().build();
|
||||||
|
|
||||||
|
HttpClient.newBuilder().build().sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(resp -> {
|
||||||
|
if (resp.statusCode() >= 200 && resp.statusCode() < 300) {
|
||||||
|
String body = resp.body();
|
||||||
|
Log.i("MicroAuth","store: " + body);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkMcProfile(String mcAccessToken) throws IOException, JSONException {
|
private void checkMcProfile(String mcAccessToken) throws IOException, JSONException {
|
||||||
URL url = new URL(mcProfileUrl);
|
task.publishProgressPublic();
|
||||||
|
|
||||||
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
URL url = new URL(mcProfileUrl);
|
||||||
conn.setRequestProperty("Authorization", "Bearer " + mcAccessToken);
|
|
||||||
conn.setUseCaches(false);
|
|
||||||
conn.connect();
|
|
||||||
|
|
||||||
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
||||||
String s = ""; int len = 0; byte[] buf = new byte[256];
|
conn.setRequestProperty("Authorization", "Bearer " + mcAccessToken);
|
||||||
InputStream is = conn.getInputStream();
|
conn.setUseCaches(false);
|
||||||
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
conn.connect();
|
||||||
s += new String(buf,0,len);
|
|
||||||
}
|
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
||||||
Log.i("MicroAuth","profile:" + s);
|
String s = ""; int len = 0; byte[] buf = new byte[256];
|
||||||
JSONObject jsonObject = new JSONObject(s);
|
InputStream is = conn.getInputStream();
|
||||||
String name = (String) jsonObject.get("name");
|
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
||||||
String uuid = (String) jsonObject.get("id");
|
s += new String(buf,0,len);
|
||||||
String uuidDashes = uuid .replaceFirst(
|
|
||||||
"(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5"
|
|
||||||
);
|
|
||||||
doesOwnGame = true;
|
|
||||||
Log.i("MicroAuth","UserName = " + name);
|
|
||||||
Log.i("MicroAuth","Uuid Minecraft = " + uuidDashes);
|
|
||||||
mcName=name;
|
|
||||||
mcUuid=uuidDashes;
|
|
||||||
}else{
|
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
|
||||||
Log.i("MicroAuth","It seems that this Microshit Account does not own the game.");
|
|
||||||
doesOwnGame = false;
|
|
||||||
}
|
}
|
||||||
|
Log.i("MicroAuth","profile:" + s);
|
||||||
|
JSONObject jsonObject = new JSONObject(s);
|
||||||
|
String name = (String) jsonObject.get("name");
|
||||||
|
String uuid = (String) jsonObject.get("id");
|
||||||
|
String uuidDashes = uuid .replaceFirst(
|
||||||
|
"(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5"
|
||||||
|
);
|
||||||
|
doesOwnGame = true;
|
||||||
|
Log.i("MicroAuth","UserName = " + name);
|
||||||
|
Log.i("MicroAuth","Uuid Minecraft = " + uuidDashes);
|
||||||
|
mcName=name;
|
||||||
|
mcUuid=uuidDashes;
|
||||||
|
}else{
|
||||||
|
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
||||||
|
Log.i("MicroAuth","It seems that this Microshit Account does not own the game.");
|
||||||
|
doesOwnGame = false;
|
||||||
|
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String ofJSONData(Map<Object, Object> data) {
|
public static String ofJSONData(Map<Object, Object> data) {
|
||||||
return new JSONObject(data).toString();
|
return new JSONObject(data).toString();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String ofFormData(Map<Object, Object> data) {
|
public static String ofFormData(Map<Object, Object> data) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user