diff --git a/src/main/java/pojlib/API.java b/src/main/java/pojlib/API.java index 00f5373a..258d649a 100644 --- a/src/main/java/pojlib/API.java +++ b/src/main/java/pojlib/API.java @@ -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); } } diff --git a/src/main/java/pojlib/account/LoginHelper.java b/src/main/java/pojlib/account/LoginHelper.java index a8c75177..d58a87ba 100644 --- a/src/main/java/pojlib/account/LoginHelper.java +++ b/src/main/java/pojlib/account/LoginHelper.java @@ -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; @@ -28,8 +29,8 @@ import pojlib.util.MSAException; public class LoginHelper { - public static Thread loginThread; - + public static final Set SCOPES; + private static Thread loginThread; private static PublicClientApplication pca; static { @@ -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 future; - try { - Set 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 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 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 deviceCodeConsumer = (DeviceCode deviceCode) -> API.msaMessage = deviceCode.message(); - HashSet params = new HashSet<>(); - params.add("XboxLive.SignIn"); - params.add("XboxLive.offline_access"); CompletableFuture 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); } diff --git a/src/main/java/pojlib/account/MinecraftAccount.java b/src/main/java/pojlib/account/MinecraftAccount.java index ea1805c9..4aaf2d2c 100644 --- a/src/main/java/pojlib/account/MinecraftAccount.java +++ b/src/main/java/pojlib/account/MinecraftAccount.java @@ -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; @@ -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) { diff --git a/src/main/java/pojlib/account/Msa.java b/src/main/java/pojlib/account/Msa.java index 593a20be..0a625605 100644 --- a/src/main/java/pojlib/account/Msa.java +++ b/src/main/java/pojlib/account/Msa.java @@ -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 */ @@ -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.");