Skip to content

Commit

Permalink
Rewrite chunks of MSAL and MinecraftAccount handling
Browse files Browse the repository at this point in the history
  • Loading branch information
The Judge committed Sep 22, 2024
1 parent 90adbd7 commit a9ad411
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 62 deletions.
13 changes: 7 additions & 6 deletions src/main/java/pojlib/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,23 @@ public static void login(Activity activity)
hasWifi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
}

MinecraftAccount acc = MinecraftAccount.load(activity.getFilesDir() + "/accounts", null, null);
if(acc != null && (acc.expiresIn > System.currentTimeMillis() || !hasWifi)) {
MinecraftAccount acc = MinecraftAccount.load(activity.getFilesDir() + "/accounts");
if(acc != null && (acc.expiresOn >= System.currentTimeMillis() || !hasWifi)) {
currentAcc = acc;
API.profileImage = MinecraftAccount.getSkinFaceUrl(API.currentAcc);
API.profileName = API.currentAcc.username;
return;
} else if(acc != null && acc.expiresIn <= System.currentTimeMillis()) {
currentAcc = LoginHelper.getNewToken(activity);
} else if(acc != null && acc.expiresOn > System.currentTimeMillis()) {
currentAcc = LoginHelper.refreshAccount(acc);
if(currentAcc == null) {
LoginHelper.beginLogin(activity);
LoginHelper.login(activity);
} else {
API.profileImage = MinecraftAccount.getSkinFaceUrl(API.currentAcc);
API.profileName = API.currentAcc.username;
return;
}
}

LoginHelper.beginLogin(activity);
LoginHelper.login(activity);
}
}
62 changes: 30 additions & 32 deletions src/main/java/pojlib/account/LoginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand All @@ -28,8 +29,8 @@
import pojlib.util.MSAException;

public class LoginHelper {
public static Thread loginThread;

public static final Set<String> SCOPES;
private static Thread loginThread;
private static PublicClientApplication pca;

static {
Expand All @@ -54,50 +55,47 @@ public class LoginHelper {
} catch (IOException e) {
throw new RuntimeException(e);
}

SCOPES = new HashSet<>();
SCOPES.add("XboxLive.SignIn");
SCOPES.add("XboxLive.offline_access");
}

public static MinecraftAccount getNewToken(Activity activity) {
CompletableFuture<IAuthenticationResult> future;
try {
Set<IAccount> accounts = pca.getAccounts().join();
if (accounts.isEmpty()) {
Logger.getInstance().appendToLog("Error!: QuestCraft account not set!");
beginLogin(activity);
throw new RuntimeException("Error!: QuestCraft account not set!");
}
IAccount account = accounts.iterator().next();
HashSet<String> params = new HashSet<>();
params.add("XboxLive.SignIn");
params.add("XboxLive.offline_access");
future = pca.acquireTokenSilently(SilentParameters.builder(params, account).build());
} catch (MalformedURLException e) {
Logger.getInstance().appendToLog(e.toString());
throw new RuntimeException(e);
}
public static MinecraftAccount refreshAccount(MinecraftAccount acc) {
Set<IAccount> accountsInCache = pca.getAccounts().join();
IAccount account = accountsInCache.iterator().next();

IAuthenticationResult result;
try {
IAuthenticationResult res = future.get();
return MinecraftAccount.load(activity.getFilesDir() + "/accounts", res.accessToken(), String.valueOf(res.expiresOnDate().getTime()));
} catch (ExecutionException | InterruptedException e) {
Logger.getInstance().appendToLog(e.toString());
throw new RuntimeException(e);
SilentParameters silentParameters =
SilentParameters
.builder(SCOPES, account)
.build();

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

acc.expiresOn = result.expiresOnDate().getTime();
acc.accessToken = Msa.acquireXBLToken(result.accessToken());
return acc;
} catch (Exception ex) {
return null;
}
}

public static void beginLogin(Activity activity) {
public static void login(Activity activity) {
loginThread = new Thread(() -> {
Consumer<DeviceCode> deviceCodeConsumer = (DeviceCode deviceCode) -> API.msaMessage = deviceCode.message();
HashSet<String> params = new HashSet<>();
params.add("XboxLive.SignIn");
params.add("XboxLive.offline_access");
CompletableFuture<IAuthenticationResult> future = pca.acquireToken(
DeviceCodeFlowParameters.builder(params, deviceCodeConsumer).build());
DeviceCodeFlowParameters.builder(SCOPES, deviceCodeConsumer).build());

try {
IAuthenticationResult res = future.get();
while(res.account() == null);
while(res.account() == null) {
Thread.sleep(20);
}
try {
API.currentAcc = MinecraftAccount.login(activity, activity.getFilesDir() + "/accounts", new String[]{res.accessToken(), String.valueOf(res.expiresOnDate().getTime())});
API.currentAcc = MinecraftAccount.login(activity, activity.getFilesDir() + "/accounts", res.accessToken(), res.expiresOnDate().getTime());
} catch (IOException | JSONException | MSAException e) {
Logger.getInstance().appendToLog("Unable to load account! | " + e);
}
Expand Down
27 changes: 6 additions & 21 deletions src/main/java/pojlib/account/MinecraftAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ public class MinecraftAccount {
public String accessToken;
public String uuid;
public String username;
public long expiresIn;
public long expiresOn;
public final String userType = "msa";


public static MinecraftAccount login(Activity activity, String gameDir, String[] response) throws MSAException, IOException, JSONException {
String mcToken = Msa.acquireXBLToken(response[0]);
public static MinecraftAccount login(Activity activity, String gameDir, String msToken, long expiresOn) throws MSAException, IOException, JSONException {
String mcToken = Msa.acquireXBLToken(msToken);
Msa instance = new Msa(activity);
MinecraftAccount account = instance.performLogin(mcToken);
account.expiresIn = Long.parseLong(response[1]);
account.expiresOn = expiresOn;

GsonUtils.objectToJsonFile(gameDir + "/account.json", account);
return account;
Expand All @@ -43,22 +42,8 @@ public static boolean logout(Activity activity) {
}

//Try this before using login - the account will have been saved to disk if previously logged in
public static MinecraftAccount load(String path, @Nullable String newToken, @Nullable String expire) {
MinecraftAccount acc;
try {
acc = new Gson().fromJson(new FileReader(path + "/account.json"), MinecraftAccount.class);
if(newToken != null) {
acc.accessToken = Msa.acquireXBLToken(newToken);
}
if(expire != null) {
acc.expiresIn = Long.parseLong(expire);
}
GsonUtils.objectToJsonFile(path + "/account.json", acc);
return acc;
} catch (IOException | JSONException | MSAException e) {
Logger.getInstance().appendToLog("Unable to load account! | " + e);
return null;
}
public static MinecraftAccount load(String path) {
return GsonUtils.jsonFileToObject(path + "/account.json", MinecraftAccount.class);
}

public static String getSkinFaceUrl(MinecraftAccount account) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/pojlib/account/Msa.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Msa {
private Activity activity;

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

/** Performs a full login, calling back listeners appropriately */
Expand All @@ -65,13 +65,13 @@ public MinecraftAccount performLogin(String xblToken) throws MSAException {
fetchOwnedItems(mcToken);
checkMcProfile(mcToken);

MinecraftAccount acc = MinecraftAccount.load(activity.getFilesDir() + "/accounts", null, null);
MinecraftAccount acc = MinecraftAccount.load(activity.getFilesDir() + "/accounts");
if (acc == null) acc = new MinecraftAccount();
if (doesOwnGame) {
acc.accessToken = mcToken;
acc.username = mcName;
acc.uuid = mcUuid;
acc.expiresIn = expiresAt;
acc.expiresOn = expiresAt;
} else {
Logger.getInstance().appendToLog("MicrosoftLogin | Unknown Error occurred.");
throw new MSAException("MicrosoftLogin | Unknown Error occurred.");
Expand Down

0 comments on commit a9ad411

Please sign in to comment.