Skip to content

Commit

Permalink
Rework acc refresh and initial impl for multi-acc
Browse files Browse the repository at this point in the history
  • Loading branch information
The Judge committed Nov 8, 2024
1 parent 229950f commit c9b0abf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
11 changes: 7 additions & 4 deletions src/main/java/pojlib/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static boolean removeAccount(Activity activity, String uuid) {
*
* @param activity Android activity object
*/
public static void login(Activity activity, @Nullable String accountUUID)
public static void login(Activity activity, String accountUUID)
{
ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkCapabilities capabilities = connManager.getNetworkCapabilities(connManager.getActiveNetwork());
Expand All @@ -210,6 +210,11 @@ public static void login(Activity activity, @Nullable String accountUUID)
hasWifi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
}

if(accountUUID == null) {
LoginHelper.login(activity);
return;
}

MinecraftAccount acc = MinecraftAccount.load(activity.getFilesDir() + "/accounts", accountUUID);
if(acc != null && (acc.expiresOn >= System.currentTimeMillis() || !hasWifi || acc.isDemoMode)) {
currentAcc = acc;
Expand All @@ -219,9 +224,7 @@ public static void login(Activity activity, @Nullable String accountUUID)
return;
} else if(acc != null && acc.expiresOn < System.currentTimeMillis()) {
currentAcc = LoginHelper.refreshAccount(activity, accountUUID);
if(currentAcc == null) {
LoginHelper.login(activity);
} else {
if(currentAcc != null) {
API.profileImage = MinecraftAccount.getSkinFaceUrl(API.currentAcc);
API.profileName = API.currentAcc.username;
API.profileUUID = API.currentAcc.uuid;
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/pojlib/account/LoginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,24 @@ public class LoginHelper {

public static MinecraftAccount refreshAccount(Activity activity, String uuid) {
Set<IAccount> accountsInCache = pca.getAccounts().join();
IAccount account = accountsInCache.iterator().next();

IAuthenticationResult result;
try {
SilentParameters silentParameters =
SilentParameters
.builder(SCOPES, account)
.build();

result = pca.acquireTokenSilently(silentParameters).join();
MinecraftAccount acc = new Msa(activity).performLogin(result.accessToken());
GsonUtils.objectToJsonFile(activity.getFilesDir() + "/accounts/" + uuid + ".json", acc);
return acc;
for (IAccount account : accountsInCache) {
SilentParameters silentParameters =
SilentParameters
.builder(SCOPES, account)
.build();

result = pca.acquireTokenSilently(silentParameters).join();
MinecraftAccount acc = new Msa(activity).performLogin(result.accessToken());
GsonUtils.objectToJsonFile(activity.getFilesDir() + "/accounts/" + acc.uuid + ".json", acc);
if (!acc.uuid.equals(uuid)) {
// Refresh was for the wrong acc, try again
continue;
}
return acc;
}
return null;
} catch (Exception ex) {
Logger.getInstance().appendToLog("Couldn't refresh token! " + ex);
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pojlib/account/MinecraftAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static MinecraftAccount load(String path, String uuid) {
}

public static String getSkinFaceUrl(MinecraftAccount account) {
if (!Msa.doesOwnGame) {
if (account.isDemoMode) {
return Constants.MINOTAR_URL + "/helm/MHF_Steve";
} else {
try {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/pojlib/account/Msa.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ public class Msa {
XSTS_ERRORS.put(2148916238L, "Your account is a child account, and needs to be added into a Family in order to log in.");
}

private final Activity activity;

/* Fields used to fill the account */
public String mcName;
public String mcToken;
public String mcUuid;
public static boolean doesOwnGame;
private Activity activity;
public boolean doesOwnGame;
private long mcExpiresOn;

public Msa(Activity currentActivity) {
Expand Down

0 comments on commit c9b0abf

Please sign in to comment.