Skip to content

Commit

Permalink
Fix refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
The Judge committed Sep 22, 2024
1 parent e4175cc commit 724e13c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/pojlib/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ public static void login(Activity activity)
API.profileImage = MinecraftAccount.getSkinFaceUrl(API.currentAcc);
API.profileName = API.currentAcc.username;
return;
} else if(acc != null && acc.expiresOn > System.currentTimeMillis()) {
currentAcc = LoginHelper.refreshAccount(acc);
} else if(acc != null && acc.expiresOn < System.currentTimeMillis()) {
currentAcc = LoginHelper.refreshAccount(activity);
if(currentAcc == null) {
LoginHelper.login(activity);
} else {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/pojlib/account/LoginHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pojlib.account;

import android.app.Activity;
import android.util.Log;

import com.microsoft.aad.msal4j.DeviceCode;
import com.microsoft.aad.msal4j.DeviceCodeFlowParameters;
Expand All @@ -25,6 +26,7 @@

import pojlib.API;
import pojlib.util.Constants;
import pojlib.util.GsonUtils;
import pojlib.util.Logger;
import pojlib.util.MSAException;

Expand Down Expand Up @@ -61,9 +63,9 @@ public class LoginHelper {
SCOPES.add("XboxLive.offline_access");
}

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

IAuthenticationResult result;
try {
Expand All @@ -73,12 +75,11 @@ public static MinecraftAccount refreshAccount(MinecraftAccount acc) {
.build();

result = pca.acquireTokenSilently(silentParameters).join();
result.expiresOnDate().getTime();

acc.expiresOn = result.expiresOnDate().getTime();
acc.accessToken = Msa.acquireXBLToken(result.accessToken());
MinecraftAccount acc = new Msa(activity).performLogin(result.accessToken());
GsonUtils.objectToJsonFile(activity.getFilesDir() + "/accounts/account.json", acc);
return acc;
} catch (Exception ex) {
Logger.getInstance().appendToLog("Couldn't refresh token! " + ex);
return null;
}
}
Expand All @@ -95,7 +96,7 @@ public static void login(Activity activity) {
Thread.sleep(20);
}
try {
API.currentAcc = MinecraftAccount.login(activity, activity.getFilesDir() + "/accounts", res.accessToken(), res.expiresOnDate().getTime());
API.currentAcc = MinecraftAccount.login(activity, activity.getFilesDir() + "/accounts", res.accessToken());
} catch (IOException | JSONException | MSAException e) {
Logger.getInstance().appendToLog("Unable to load account! | " + e);
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/pojlib/account/MinecraftAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ public class MinecraftAccount {
public long expiresOn;
public final String userType = "msa";

public static MinecraftAccount login(Activity activity, String gameDir, String msToken, long expiresOn) throws MSAException, IOException, JSONException {
String mcToken = Msa.acquireXBLToken(msToken);
public static MinecraftAccount login(Activity activity, String gameDir, String msToken) throws MSAException, IOException, JSONException {
Msa instance = new Msa(activity);
MinecraftAccount account = instance.performLogin(mcToken);
account.expiresOn = expiresOn;
MinecraftAccount account = instance.performLogin(msToken);

GsonUtils.objectToJsonFile(gameDir + "/account.json", account);
return account;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/pojlib/account/Msa.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ public class Msa {
public String mcToken;
public static String mcUuid;
public static boolean doesOwnGame;
public long expiresAt;
private Activity activity;
private long mcExpiresOn;

public Msa(Activity currentActivity) {
this.activity = currentActivity;
}

/** Performs a full login, calling back listeners appropriately */
public MinecraftAccount performLogin(String xblToken) throws MSAException {
public MinecraftAccount performLogin(String msToken) throws MSAException {
try {
String xblToken = acquireXBLToken(msToken);
String[] xsts = acquireXsts(xblToken);
if(xsts == null) {
return null;
Expand All @@ -71,7 +72,7 @@ public MinecraftAccount performLogin(String xblToken) throws MSAException {
acc.accessToken = mcToken;
acc.username = mcName;
acc.uuid = mcUuid;
acc.expiresOn = expiresAt;
acc.expiresOn = mcExpiresOn;
} else {
Logger.getInstance().appendToLog("MicrosoftLogin | Unknown Error occurred.");
throw new MSAException("MicrosoftLogin | Unknown Error occurred.");
Expand Down Expand Up @@ -176,8 +177,8 @@ private void acquireMinecraftToken(String xblUhs, String xblXsts) throws IOExcep
if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
JSONObject jo = new JSONObject(FileUtil.read(conn.getInputStream()));
conn.disconnect();
expiresAt = System.currentTimeMillis() + (jo.getInt("expires_in") * 1000L);
mcToken = jo.getString("access_token");
mcExpiresOn = System.currentTimeMillis() + (jo.getInt("expires_in") * 1000L);
}else{
throw getResponseThrowable(conn);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pojlib/util/json/MinecraftInstances.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static class Instance {

public List<String> generateLaunchArgs(MinecraftAccount account) {
String[] mcArgs = {"--username", account.username, "--version", versionName, "--gameDir", gameDir,
"--assetsDir", assetsDir, "--assetIndex", assetIndex, "--uuid", account.uuid,
"--assetsDir", assetsDir, "--assetIndex", assetIndex, "--uuid", account.uuid.replace("-", ""),
"--accessToken", account.accessToken, "--userType", account.userType, "--versionType", "release"};

List<String> allArgs = new ArrayList<>(Arrays.asList("-cp", classpath));
Expand Down

0 comments on commit 724e13c

Please sign in to comment.