From 669e3f53fa5cfe1e65f5ad2509069e7e0f481e3e Mon Sep 17 00:00:00 2001 From: Henry Le Grys Date: Sat, 26 Oct 2024 03:57:03 +0100 Subject: [PATCH] Fix launcher not prompting for re-login if refresh token expired --- .../launcher/auth/MicrosoftLoginService.java | 2 +- .../launcher/dialog/AccountSelectDialog.java | 33 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/launcher/src/main/java/com/skcraft/launcher/auth/MicrosoftLoginService.java b/launcher/src/main/java/com/skcraft/launcher/auth/MicrosoftLoginService.java index 5244b2a..0c46177 100644 --- a/launcher/src/main/java/com/skcraft/launcher/auth/MicrosoftLoginService.java +++ b/launcher/src/main/java/com/skcraft/launcher/auth/MicrosoftLoginService.java @@ -87,7 +87,7 @@ public class MicrosoftLoginService implements LoginService { .expectResponseCodeOr(200, (req) -> { TokenError error = req.returnContent().asJson(TokenError.class); - return new AuthenticationException(error.errorDescription); + return new AuthenticationException(error.errorDescription, true); }) .returnContent() .asJson(TokenResponse.class); diff --git a/launcher/src/main/java/com/skcraft/launcher/dialog/AccountSelectDialog.java b/launcher/src/main/java/com/skcraft/launcher/dialog/AccountSelectDialog.java index 6015f02..89d0785 100644 --- a/launcher/src/main/java/com/skcraft/launcher/dialog/AccountSelectDialog.java +++ b/launcher/src/main/java/com/skcraft/launcher/dialog/AccountSelectDialog.java @@ -102,7 +102,7 @@ public class AccountSelectDialog extends JDialog { } }); - addMicrosoftButton.addActionListener(ev -> attemptMicrosoftLogin()); + addMicrosoftButton.addActionListener(ev -> attemptMicrosoftLogin(SharedLocale.tr("login.microsoft.seeBrowser"))); offlineButton.addActionListener(ev -> setResult(new OfflineSession(launcher.getProperties().getProperty("offlinePlayerName")))); @@ -145,8 +145,7 @@ public class AccountSelectDialog extends JDialog { dispose(); } - private void attemptMicrosoftLogin() { - String status = SharedLocale.tr("login.microsoft.seeBrowser"); + private void attemptMicrosoftLogin(String status) { SettableProgress progress = new SettableProgress(status, -1); ListenableFuture future = launcher.getExecutor().submit(() -> { @@ -181,15 +180,9 @@ public class AccountSelectDialog extends JDialog { @Override public void onFailure(Throwable t) { - if (t instanceof AuthenticationException) { - if (((AuthenticationException) t).isInvalidatedSession()) { - // Just need to log in again - LoginDialog.ReloginDetails details = new LoginDialog.ReloginDetails(session.getUsername(), - SharedLocale.tr("login.relogin", t.getLocalizedMessage())); - Session newSession = LoginDialog.showLoginRequest(AccountSelectDialog.this, launcher, details); - - setResult(newSession); - } + if (t instanceof AuthenticationException && ((AuthenticationException) t).isInvalidatedSession()) { + // Just need to log in again + relogin(session, t.getLocalizedMessage()); } else { SwingHelper.showErrorDialog(AccountSelectDialog.this, t.getLocalizedMessage(), SharedLocale.tr("errorTitle"), t); } @@ -200,6 +193,22 @@ public class AccountSelectDialog extends JDialog { SharedLocale.tr("login.loggingInStatus")); } + /** + * Re-login to an expired session + */ + private void relogin(SavedSession session, String message) { + if (session.getType() == UserType.MICROSOFT) { + this.attemptMicrosoftLogin(message); + } else { + LoginDialog.ReloginDetails details = new LoginDialog.ReloginDetails(session.getUsername(), + SharedLocale.tr("login.relogin", message)); + Session newSession = LoginDialog.showLoginRequest(AccountSelectDialog.this, launcher, details); + + launcher.getAccounts().update(newSession.toSavedSession()); + setResult(newSession); + } + } + @RequiredArgsConstructor private static class RestoreSessionCallable implements Callable, ProgressObservable { private final LoginService service;