mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-17 00:29:50 -04:00
[MSA] Minimize code as much as possible
This commit is contained in:
parent
b53d17a50e
commit
892c76171d
@ -5,31 +5,12 @@
|
|||||||
*/
|
*/
|
||||||
package net.kdt.pojavlaunch.authenticator.microsoft;
|
package net.kdt.pojavlaunch.authenticator.microsoft;
|
||||||
|
|
||||||
import android.util.Log;
|
import android.util.*;
|
||||||
|
import java.io.*;
|
||||||
import java.io.IOException;
|
import java.net.*;
|
||||||
import java.io.InputStream;
|
import java.util.*;
|
||||||
import java.io.OutputStream;
|
import net.kdt.pojavlaunch.*;
|
||||||
import java.io.UnsupportedEncodingException;
|
import org.json.*;
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.ResourceBundle;
|
|
||||||
import java.util.UUID;
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import android.os.*;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -95,17 +76,11 @@ public class Msa {
|
|||||||
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];
|
JSONObject jo = new JSONObject(Tools.read(conn.getInputStream()));
|
||||||
InputStream is = conn.getInputStream();
|
|
||||||
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
|
||||||
s += new String(buf,0,len);
|
|
||||||
}
|
|
||||||
JSONObject jo = new JSONObject(s);
|
|
||||||
Log.i("MicroAuth","Acess Token = "+jo.getString("access_token"));
|
Log.i("MicroAuth","Acess Token = "+jo.getString("access_token"));
|
||||||
acquireXBLToken(jo.getString("access_token"));
|
acquireXBLToken(jo.getString("access_token"));
|
||||||
}else{
|
}else{
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
throwResponseError(conn);
|
||||||
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -148,17 +123,11 @@ public class Msa {
|
|||||||
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];
|
JSONObject jo = new JSONObject(Tools.read(conn.getInputStream()));
|
||||||
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"));
|
Log.i("MicroAuth","Xbl Token = "+jo.getString("Token"));
|
||||||
acquireXsts(jo.getString("Token"));
|
acquireXsts(jo.getString("Token"));
|
||||||
}else{
|
}else{
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
throwResponseError(conn);
|
||||||
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,18 +167,12 @@ public class Msa {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
||||||
String s = ""; int len = 0; byte[] buf = new byte[256];
|
JSONObject jo = new JSONObject(Tools.read(conn.getInputStream()));
|
||||||
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");
|
String uhs = jo.getJSONObject("DisplayClaims").getJSONArray("xui").getJSONObject(0).getString("uhs");
|
||||||
Log.i("MicroAuth","Xbl Xsts = "+jo.getString("Token")+"; Uhs = " + uhs);
|
Log.i("MicroAuth","Xbl Xsts = "+jo.getString("Token")+"; Uhs = " + uhs);
|
||||||
acquireMinecraftToken(uhs,jo.getString("Token"));
|
acquireMinecraftToken(uhs,jo.getString("Token"));
|
||||||
}else{
|
}else{;
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
throwResponseError(conn);
|
||||||
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,20 +200,14 @@ public class Msa {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
||||||
String s = ""; int len = 0; byte[] buf = new byte[256];
|
JSONObject jo = new JSONObject(Tools.read(conn.getInputStream()));
|
||||||
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"));
|
Log.i("MicroAuth","MC token: "+jo.getString("access_token"));
|
||||||
mcToken = jo.getString("access_token");
|
mcToken = jo.getString("access_token");
|
||||||
checkMcProfile(jo.getString("access_token"));
|
checkMcProfile(jo.getString("access_token"));
|
||||||
checkMcStore(jo.getString("access_token"));
|
checkMcStore(jo.getString("access_token"));
|
||||||
|
|
||||||
}else{
|
}else{;
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
throwResponseError(conn);
|
||||||
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 {
|
||||||
@ -263,12 +220,7 @@ public class Msa {
|
|||||||
conn.setUseCaches(false);
|
conn.setUseCaches(false);
|
||||||
conn.connect();
|
conn.connect();
|
||||||
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
|
||||||
String s = ""; int len = 0; byte[] buf = new byte[256];
|
JSONObject jo = new JSONObject(Tools.read(conn.getInputStream()));
|
||||||
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");
|
JSONArray ja = jo.getJSONArray("items");
|
||||||
Log.i("MicroAuth","Store Len = " + ja.length());
|
Log.i("MicroAuth","Store Len = " + ja.length());
|
||||||
for(int i = 0; i < ja.length(); i++) {
|
for(int i = 0; i < ja.length(); i++) {
|
||||||
@ -276,8 +228,7 @@ public class Msa {
|
|||||||
Log.i("MicroAuth","Product " + i +": " +prod);
|
Log.i("MicroAuth","Product " + i +": " +prod);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
throwResponseError(conn);
|
||||||
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
HttpRequest request = HttpRequest.newBuilder(uri)
|
HttpRequest request = HttpRequest.newBuilder(uri)
|
||||||
@ -304,11 +255,7 @@ public class Msa {
|
|||||||
conn.connect();
|
conn.connect();
|
||||||
|
|
||||||
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= Tools.read(conn.getInputStream());
|
||||||
InputStream is = conn.getInputStream();
|
|
||||||
while((len = is.read(buf)) != -1) { //читаем строчку пока не получим всё
|
|
||||||
s += new String(buf,0,len);
|
|
||||||
}
|
|
||||||
Log.i("MicroAuth","profile:" + s);
|
Log.i("MicroAuth","profile:" + s);
|
||||||
JSONObject jsonObject = new JSONObject(s);
|
JSONObject jsonObject = new JSONObject(s);
|
||||||
String name = (String) jsonObject.get("name");
|
String name = (String) jsonObject.get("name");
|
||||||
@ -322,10 +269,9 @@ public class Msa {
|
|||||||
mcName=name;
|
mcName=name;
|
||||||
mcUuid=uuidDashes;
|
mcUuid=uuidDashes;
|
||||||
}else{
|
}else{
|
||||||
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": "+conn.getResponseMessage());
|
Log.i("MicroAuth","It seems that this Microsoft Account does not own the game.");
|
||||||
Log.i("MicroAuth","It seems that this Microshit Account does not own the game.");
|
|
||||||
doesOwnGame = false;
|
doesOwnGame = false;
|
||||||
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
|
throwResponseError(conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,5 +296,10 @@ public class Msa {
|
|||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void throwResponseError(HttpURLConnection conn) throws IOException {
|
||||||
|
String errStr = Tools.read(conn.getErrorStream());
|
||||||
|
Log.i("MicroAuth","Error code: " + conn.getResponseCode() + ": " + conn.getResponseMessage() + "\n" + errStr);
|
||||||
|
throw new RuntimeException("MSA Error: " + conn.getResponseCode() + ": " + conn.getResponseMessage() + "\n, Error stream: " + errStr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user