Skip to content

Commit

Permalink
Do not delete the app on fail
Browse files Browse the repository at this point in the history
  • Loading branch information
The Judge committed Sep 16, 2024
1 parent 86f2ff7 commit 7797f17
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
7 changes: 4 additions & 3 deletions src/main/java/pojlib/account/LoginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ public static void beginLogin(Activity activity) {
while(res.account() == null);
try {
API.currentAcc = MinecraftAccount.login(activity.getFilesDir() + "/accounts", new String[]{res.accessToken(), String.valueOf(res.expiresOnDate().getTime())});
} catch (IOException | JSONException e) {
e.printStackTrace();
} catch (IOException | JSONException | MSAException e) {
Logger.getInstance().appendToLog("Unable to load account! | " + e);
}
API.profileImage = MinecraftAccount.getSkinFaceUrl(API.currentAcc);
API.profileName = API.currentAcc.username;
} catch (ExecutionException | InterruptedException e) {
throw new MSAException("MicrosoftLogin | Something went wrong! Couldn't reach the Microsoft Auth servers.", e);
Logger.getInstance().appendToLog("MicrosoftLogin | Something went wrong! Couldn't reach the Microsoft Auth servers.");
API.msaMessage = "MicrosoftLogin | Something went wrong! Couldn't reach the Microsoft Auth servers.";
}
});

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/pojlib/account/MinecraftAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pojlib.util.Constants;
import pojlib.util.GsonUtils;
import pojlib.util.Logger;
import pojlib.util.MSAException;

public class MinecraftAccount {
public String accessToken;
Expand All @@ -24,7 +25,7 @@ public class MinecraftAccount {
public final String userType = "msa";


public static MinecraftAccount login(String gameDir, String[] response) throws IOException, JSONException {
public static MinecraftAccount login(String gameDir, String[] response) throws MSAException, IOException, JSONException {
String mcToken = Msa.acquireXBLToken(response[0]);
Msa instance = new Msa();
MinecraftAccount account = instance.performLogin(mcToken);
Expand Down Expand Up @@ -54,7 +55,7 @@ public static MinecraftAccount load(String path, @Nullable String newToken, @Nul
}
GsonUtils.objectToJsonFile(path + "/account.json", acc);
return acc;
} catch (IOException | JSONException e) {
} catch (IOException | JSONException | MSAException e) {
Logger.getInstance().appendToLog("Unable to load account! | " + e);
return null;
}
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/pojlib/account/Msa.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public class Msa {
public long expiresAt;

/** Performs a full login, calling back listeners appropriately */
public MinecraftAccount performLogin(String xblToken) {
public MinecraftAccount performLogin(String xblToken) throws MSAException {
try {
String[] xsts = acquireXsts(xblToken);
if(xsts == null) {
return null;
}
String mcToken = acquireMinecraftToken(xsts[0], xsts[1]);
acquireMinecraftToken(xsts[0], xsts[1]);
fetchOwnedItems(mcToken);
checkMcProfile(mcToken);

Expand All @@ -69,17 +69,17 @@ public MinecraftAccount performLogin(String xblToken) {
acc.expiresIn = expiresAt;
} else {
Logger.getInstance().appendToLog("MicrosoftLogin | Unknown Error occurred.");
throw new MSAException("MicrosoftLogin | Unknown Error occurred.", null);
throw new MSAException("MicrosoftLogin | Unknown Error occurred.");
}

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

static String acquireXBLToken(String accessToken) throws IOException, JSONException {
static String acquireXBLToken(String accessToken) throws IOException, MSAException, JSONException {
URL url = new URL(Constants.XBL_AUTH_URL);

JSONObject data = new JSONObject();
Expand Down Expand Up @@ -109,7 +109,7 @@ static String acquireXBLToken(String accessToken) throws IOException, JSONExcept
}

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

JSONObject data = new JSONObject();
Expand Down Expand Up @@ -144,16 +144,16 @@ private String[] acquireXsts(String xblToken) throws IOException, JSONException
String locale_id = XSTS_ERRORS.get(xerr);
if(locale_id != null) {
Logger.getInstance().appendToLog(responseContents);
throw new MSAException(responseContents, null);
throw new MSAException(responseContents);
}
// Logger.getInstance().appendToLog("Unknown error returned from Xbox Live\n" + responseContents);
throw new MSAException("Unknown error returned from Xbox Live", null);
throw new MSAException("Unknown error returned from Xbox Live");
} else{
throw getResponseThrowable(conn);
}
}

private String acquireMinecraftToken(String xblUhs, String xblXsts) throws IOException, JSONException {
private void acquireMinecraftToken(String xblUhs, String xblXsts) throws IOException, MSAException, JSONException {
URL url = new URL(Constants.MC_LOGIN_URL);

JSONObject data = new JSONObject();
Expand All @@ -173,13 +173,12 @@ private String acquireMinecraftToken(String xblUhs, String xblXsts) throws IOExc
conn.disconnect();
expiresAt = System.currentTimeMillis() + (jo.getInt("expires_in") * 1000L);
mcToken = jo.getString("access_token");
return jo.getString("access_token");
}else{
throw getResponseThrowable(conn);
}
}

private void fetchOwnedItems(String mcAccessToken) throws IOException {
private void fetchOwnedItems(String mcAccessToken) throws MSAException, IOException {
URL url = new URL(Constants.MC_STORE_URL);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
Expand All @@ -195,7 +194,7 @@ private void fetchOwnedItems(String mcAccessToken) throws IOException {
}

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

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
Expand All @@ -222,7 +221,7 @@ public static boolean checkMcProfile(String mcAccessToken) throws IOException, J
} else {
Logger.getInstance().appendToLog("MicrosoftLogin | It seems that this Microsoft Account does not own the game.");
doesOwnGame = false;
throw new MSAException("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.", null);
throw new MSAException("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.");
}
}

Expand Down Expand Up @@ -257,11 +256,11 @@ private static String convertToFormData(String... data) throws UnsupportedEncodi
return builder.toString();
}

private static RuntimeException getResponseThrowable(HttpURLConnection conn) throws IOException {
private static MSAException getResponseThrowable(HttpURLConnection conn) throws IOException, MSAException {
Logger.getInstance().appendToLog("MicrosoftLogin | Error code: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
if(conn.getResponseCode() == 429) {
throw new MSAException("Too many requests, please try again later.", null);
throw new MSAException("Too many requests, please try again later.");
}
throw new MSAException(conn.getResponseMessage(), null);
throw new MSAException(conn.getResponseMessage());
}
}
6 changes: 2 additions & 4 deletions src/main/java/pojlib/util/MSAException.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package pojlib.util;

import androidx.annotation.Nullable;
import pojlib.API;

public class MSAException extends RuntimeException {
public MSAException(String msaMessage, @Nullable Throwable cause) {
super(msaMessage, cause);
public class MSAException extends Exception {
public MSAException(String msaMessage) {
API.msaMessage = msaMessage;
}
}

0 comments on commit 7797f17

Please sign in to comment.