Skip to content

Commit

Permalink
Finish refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
The Judge committed Aug 11, 2024
1 parent ba8439a commit 60482a5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/main/java/pojlib/account/MinecraftAccount.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package pojlib.account;

import static pojlib.account.Msa.checkMcProfile;

import android.app.Activity;

import com.google.gson.Gson;
Expand All @@ -27,9 +25,11 @@ public class MinecraftAccount {

public final String userType = "msa";


public static MinecraftAccount login(String gameDir, String[] response) throws IOException, JSONException {
String mcToken = Msa.acquireXBLToken(response[0]);
MinecraftAccount account = checkMcProfile(mcToken);
Msa instance = new Msa(false, mcToken);
MinecraftAccount account = instance.performLogin();
account.expiresIn = Long.parseLong(response[1]);

GsonUtils.objectToJsonFile(gameDir + "/account.json", account);
Expand Down
115 changes: 52 additions & 63 deletions src/main/java/pojlib/account/Msa.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import pojlib.API;
import pojlib.util.Constants;
import pojlib.util.FileUtil;
import pojlib.util.Logger;
Expand Down Expand Up @@ -57,48 +58,40 @@ public Msa(boolean isRefresh, String authCode){
}

/** Performs a full login, calling back listeners appropriately */
public void performLogin(@Nullable final FileUtils.ProgressListener progressListener,
@Nullable final DoneListener doneListener,
@Nullable final ErrorListener errorListener,
Activity activity) {
sExecutorService.execute(() -> {
try {
notifyProgress(progressListener, 1, activity);
String accessToken = acquireAccessToken(mIsRefresh, mAuthCode);
notifyProgress(progressListener, 2, activity);
String xboxLiveToken = acquireXBLToken(accessToken);
notifyProgress(progressListener, 3, activity);
String[] xsts = acquireXsts(xboxLiveToken);
notifyProgress(progressListener, 4, activity);
String mcToken = acquireMinecraftToken(xsts[0], xsts[1]);
notifyProgress(progressListener, 5, activity);
fetchOwnedItems(mcToken);
checkMcProfile(mcToken);

MinecraftAccount acc = MinecraftAccount.load(mcName);
if(acc == null) acc = new MinecraftAccount();
if (doesOwnGame) {
/*acc.xuid = xsts[0];*/
/*acc.clientToken = "0"; *//* FIXME */
acc.accessToken = mcToken;
acc.username = mcName;
acc.uuid = mcUuid;
acc.expiresIn = expiresAt;
}
acc.save();

if(doneListener != null) {
MinecraftAccount finalAcc = acc;
activity.runOnUiThread(() -> doneListener.onLoginDone(finalAcc));
}

}catch (Exception e){
Logger.getInstance().appendToLog("MicroAuth | Exception thrown during authentication" + e);
if(errorListener != null);
activity.runOnUiThread(() -> errorListener.onLoginError(e));
public MinecraftAccount performLogin() {
try {
String accessToken = acquireAccessToken(mIsRefresh, mAuthCode);
String xboxLiveToken = acquireXBLToken(accessToken);
String[] xsts = acquireXsts(xboxLiveToken);
if(xsts == null) {
return null;
}
String mcToken = acquireMinecraftToken(xsts[0], xsts[1]);
fetchOwnedItems(mcToken);
if(!checkMcProfile(mcToken)) {
Logger.getInstance().appendToLog("MicrosoftLogin | checkMcProfile failed.");
return null;
}

MinecraftAccount acc = MinecraftAccount.load(mcName, null, null);
if (acc == null) acc = new MinecraftAccount();
if (doesOwnGame) {
/*acc.xuid = xsts[0];*/
/*acc.clientToken = "0"; *//* FIXME */
acc.accessToken = mcToken;
acc.username = mcName;
acc.uuid = mcUuid;
acc.expiresIn = expiresAt;
} else {
Logger.getInstance().appendToLog("MicrosoftLogin | Unknown Error occurred.");
return null;
}
ProgressLayout.clearProgress(ProgressLayout.AUTHENTICATE_MICROSOFT);
});

return acc;
} catch (Exception e) {
Logger.getInstance().appendToLog("MicrosoftLogin | Exception thrown during authentication" + e);
return null;
}
}

public String acquireAccessToken(boolean isRefresh, String authcode) throws IOException, JSONException {
Expand Down Expand Up @@ -145,7 +138,7 @@ static String acquireXBLToken(String accessToken) throws IOException, JSONExcept
properties.put("AuthMethod", "RPS");
properties.put("SiteName", "user.auth.xboxlive.com");
properties.put("RpsTicket", accessToken);
data.put("Properties",properties);
data.put("Properties", properties);
data.put("RelyingParty", "http://auth.xboxlive.com");
data.put("TokenType", "JWT");

Expand All @@ -167,7 +160,7 @@ static String acquireXBLToken(String accessToken) throws IOException, JSONExcept
}

/** @return [uhs, token]*/
private @NonNull String[] acquireXsts(String xblToken) throws IOException, JSONException {
private String[] acquireXsts(String xblToken) throws IOException, JSONException {
URL url = new URL(Constants.XSTS_AUTH_URL);

JSONObject data = new JSONObject();
Expand All @@ -179,10 +172,10 @@ static String acquireXBLToken(String accessToken) throws IOException, JSONExcept
data.put("TokenType", "JWT");

String req = data.toString();
Logger.getInstance().appendToLog("MicroAuth | " + req);
Logger.getInstance().appendToLog("MicrosoftLogin | " + req);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
setCommonProperties(conn, req);
Logger.getInstance().appendToLog("MicroAuth | " + conn.getRequestMethod());
Logger.getInstance().appendToLog("MicrosoftLogin | " + conn.getRequestMethod());
conn.connect();

try(OutputStream wr = conn.getOutputStream()) {
Expand All @@ -195,15 +188,17 @@ static String acquireXBLToken(String accessToken) throws IOException, JSONExcept
String token = jo.getString("Token");
conn.disconnect();
return new String[]{uhs, token};
}else if(conn.getResponseCode() == 401) {
} else if(conn.getResponseCode() == 401) {
String responseContents = FileUtil.read(conn.getErrorStream());
JSONObject jo = new JSONObject(responseContents);
long xerr = jo.optLong("XErr", -1);
String locale_id = XSTS_ERRORS.get(xerr);
if(locale_id != null) {
throw new PresentedException(new RuntimeException(responseContents), locale_id);
Logger.getInstance().appendToLog(responseContents);
return null;
}
throw new PresentedException(new RuntimeException(responseContents), "Unknown Xbox Live API error ", xerr);
Logger.getInstance().appendToLog("Unknown error returned from Xbox Live\n" + responseContents);
return null;
}else{
throw getResponseThrowable(conn);
}
Expand Down Expand Up @@ -250,7 +245,8 @@ private void fetchOwnedItems(String mcAccessToken) throws IOException {
// as it does not indicate whether the user owns the game through Game Pass.
}

public static void checkMcProfile(String mcAccessToken) throws IOException, JSONException {
// Returns false for failure //
public static boolean checkMcProfile(String mcAccessToken) throws IOException, JSONException {
URL url = new URL(Constants.MC_PROFILE_URL);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
Expand All @@ -271,24 +267,17 @@ public static void checkMcProfile(String mcAccessToken) throws IOException, JSON
doesOwnGame = true;
Logger.getInstance().appendToLog("MicrosoftLogin | UserName = " + name);
Logger.getInstance().appendToLog("MicrosoftLogin | Uuid Minecraft = " + uuidDashes);
mcName=name;
mcUuid=uuidDashes;
}else{
mcName = name;
mcUuid = uuidDashes;
return true;
} else {
Logger.getInstance().appendToLog("MicrosoftLogin | It seems that this Microsoft Account does not own the game.");
doesOwnGame = false;
throw new PresentedException(new RuntimeException(conn.getResponseMessage()), "It seems like this account does not have a Minecraft profile. If you have Xbox Game Pass, please log in on https://minecraft.net/ and set it up.");
}
}

/** Wrapper to ease notifying the listener */
private void notifyProgress(@Nullable FileUtils.ProgressListener listener, int step, Activity activity){
if(listener != null){
activity.runOnUiThread(() -> listener.onLoginProgress(step));
API.msaMessage = "It seems like this account does not have a Minecraft profile. If you have Xbox Game Pass, please log in on https://minecraft.net/ and set it up.";
return false;
}
ProgressLayout.setProgress(ProgressLayout.AUTHENTICATE_MICROSOFT, step*20);
}


/** Set common properties for the connection. Given that all requests are POST, interactivity is always enabled */
private static void setCommonProperties(HttpURLConnection conn, String formData) {
conn.setRequestProperty("Content-Type", "application/json");
Expand Down Expand Up @@ -323,7 +312,7 @@ private static String convertToFormData(String... data) throws UnsupportedEncodi
private static RuntimeException getResponseThrowable(HttpURLConnection conn) throws IOException {
Logger.getInstance().appendToLog("MicrosoftLogin | Error code: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
if(conn.getResponseCode() == 429) {
return new PresentedException("Too many requests, please try again later.");
return new RuntimeException("Too many requests, please try again later.");
}
return new RuntimeException(conn.getResponseMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion wrapper

0 comments on commit 60482a5

Please sign in to comment.