[MSA new] Throw exception when response error; Progress bar re-impl

This commit is contained in:
khanhduytran0 2020-12-16 05:23:54 +07:00
parent 3aa5621c97
commit 83d6f64958
2 changed files with 266 additions and 248 deletions

View File

@ -19,13 +19,15 @@ 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,20 +69,19 @@ 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);
profilePath.setProfileID(msa.mcUuid); profilePath.setProfileID(msa.mcUuid);
profilePath.setIsMojangAccount(false); profilePath.setIsMojangAccount(false);
} }
MCProfile.build(profilePath); MCProfile.build(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);

View File

@ -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 {
URL url = new URL(authTokenUrl);
Log.i("MicroAuth","authCode= "+authcode);
Map<Object, Object> data = new HashMap();/*Map.of(
"client_id", "00000000402b5328",
"code", authcode,
"grant_type", "authorization_code",
"redirect_uri", "https://login.live.com/oauth20_desktop.srf",
"scope", "service::user.auth.xboxlive.com::MBI_SSL"
);*/
data.put("client_id", "00000000402b5328");
data.put("code", authcode);
data.put("grant_type", "authorization_code");
data.put("redirect_uri", "https://login.live.com/oauth20_desktop.srf");
data.put("scope", "service::user.auth.xboxlive.com::MBI_SSL");
//да пошла yf[eq1 она ваша джава 11 public void acquireAccessToken(String authcode) throws IOException, JSONException {
String req = ofFormData(data); task.publishProgressPublic();
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); URL url = new URL(authTokenUrl);
conn.setRequestProperty("charset", "utf-8"); Log.i("MicroAuth","authCode= "+authcode);
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length)); Map<Object, Object> data = new HashMap<>();/*Map.of(
conn.setRequestMethod("POST"); "client_id", "00000000402b5328",
conn.setUseCaches(false); "code", authcode,
conn.setDoInput(true); "grant_type", "authorization_code",
conn.setDoOutput(true); "redirect_uri", "https://login.live.com/oauth20_desktop.srf",
conn.connect(); "scope", "service::user.auth.xboxlive.com::MBI_SSL"
try(OutputStream wr = conn.getOutputStream()) { );*/
wr.write(req.getBytes("UTF-8")); data.put("client_id", "00000000402b5328");
data.put("code", authcode);
data.put("grant_type", "authorization_code");
data.put("redirect_uri", "https://login.live.com/oauth20_desktop.srf");
data.put("scope", "service::user.auth.xboxlive.com::MBI_SSL");
//да пошла yf[eq1 она ваша джава 11
String req = ofFormData(data);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
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);
} }
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) { JSONObject jo = new JSONObject(s);
String s = ""; int len = 0; byte[] buf = new byte[256]; Log.i("MicroAuth","Acess Token = "+jo.getString("access_token"));
InputStream is = conn.getInputStream(); acquireXBLToken(jo.getString("access_token"));
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё }else{
s += new String(buf,0,len); Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
} throw new RuntimeException("MSA Error: " + 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());
}
} }
private void acquireXBLToken(String accessToken) throws IOException, JSONException { private void acquireXBLToken(String accessToken) throws IOException, JSONException {
task.publishProgressPublic();
URL url = new URL(xblAuthUrl);
Map<Object, Object> data = new HashMap(); URL url = new URL(xblAuthUrl);
Map<Object, Object> properties = new HashMap();
properties.put("AuthMethod", "RPS");
properties.put("SiteName", "user.auth.xboxlive.com");
properties.put("RpsTicket", accessToken);
data.put("Properties",properties);
data.put("RelyingParty", "http://auth.xboxlive.com");
data.put("TokenType", "JWT");
/*Map.of(
"Properties", Map.of( Map<Object, Object> data = new HashMap<>();
"AuthMethod", "RPS", Map<Object, Object> properties = new HashMap<>();
"SiteName", "user.auth.xboxlive.com", properties.put("AuthMethod", "RPS");
"RpsTicket", accessToken properties.put("SiteName", "user.auth.xboxlive.com");
), properties.put("RpsTicket", accessToken);
"RelyingParty", "http://auth.xboxlive.com", data.put("Properties",properties);
"TokenType", "JWT" data.put("RelyingParty", "http://auth.xboxlive.com");
);*/ data.put("TokenType", "JWT");
String req = ofJSONData(data); /*Map.of(
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Content-Type", "application/json"); "Properties", Map.of(
conn.setRequestProperty("Accept", "application/json"); "AuthMethod", "RPS",
conn.setRequestProperty("charset", "utf-8"); "SiteName", "user.auth.xboxlive.com",
conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length)); "RpsTicket", accessToken
conn.setRequestMethod("POST"); ),
conn.setUseCaches(false); "RelyingParty", "http://auth.xboxlive.com",
conn.setDoInput(true); "TokenType", "JWT"
conn.setDoOutput(true); );*/
conn.connect(); String req = ofJSONData(data);
try(OutputStream wr = conn.getOutputStream()) { HttpURLConnection conn = (HttpURLConnection)url.openConnection();
wr.write(req.getBytes("UTF-8")); conn.setRequestProperty("Content-Type", "application/json");
} conn.setRequestProperty("Accept", "application/json");
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) { conn.setRequestProperty("charset", "utf-8");
String s = ""; int len = 0; byte[] buf = new byte[256]; conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
InputStream is = conn.getInputStream(); conn.setRequestMethod("POST");
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё conn.setUseCaches(false);
s += new String(buf,0,len); conn.setDoInput(true);
} conn.setDoOutput(true);
JSONObject jo = new JSONObject(s); conn.connect();
Log.i("MicroAuth","Xbl Token = "+jo.getString("Token")); try(OutputStream wr = conn.getOutputStream()) {
acquireXsts(jo.getString("Token")); wr.write(req.getBytes("UTF-8"));
}else{ }
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage()); 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","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);
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) { Map<Object, Object> data = new HashMap<>();
String s = ""; int len = 0; byte[] buf = new byte[256]; data.put("identityToken", "XBL3.0 x=" + xblUhs + ";" + xblXsts);
InputStream is = conn.getInputStream();
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё String req = ofJSONData(data);
s += new String(buf,0,len); HttpURLConnection conn = (HttpURLConnection)url.openConnection();
} conn.setRequestProperty("Content-Type", "application/json");
JSONObject jo = new JSONObject(s); conn.setRequestProperty("Accept", "application/json");
Log.i("MicroAuth","MC token: "+jo.getString("access_token")); conn.setRequestProperty("charset", "utf-8");
mcToken = jo.getString("access_token"); conn.setRequestProperty("Content-Length", Integer.toString(req.getBytes("UTF-8").length));
checkMcProfile(jo.getString("access_token")); conn.setRequestMethod("POST");
checkMcStore(jo.getString("access_token")); conn.setUseCaches(false);
conn.setDoInput(true);
}else{ conn.setDoOutput(true);
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage()); 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"));
}else{
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
throw new RuntimeException("MSA Error: " + 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); HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect(); conn.setRequestProperty("Authorization", "Bearer " + mcAccessToken);
conn.setUseCaches(false);
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) { conn.connect();
String s = ""; int len = 0; byte[] buf = new byte[256];
InputStream is = conn.getInputStream(); if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё String s = ""; int len = 0; byte[] buf = new byte[256];
s += new String(buf,0,len); InputStream is = conn.getInputStream();
} while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
Log.i("MicroAuth","profile:" + s); s += new String(buf,0,len);
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;
} }
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) {