From b7be91bc9cc9421075806d3cecf28b00e4ec92ca Mon Sep 17 00:00:00 2001 From: Henry Le Grys Date: Mon, 24 May 2021 13:17:11 +0100 Subject: [PATCH] Automatically show login dialog when session expires Previously it just gave an unhelpful error message; now it pops up the login dialog again. Also, the duplicated accounts bug should be fixed! --- .../auth/AuthenticationException.java | 8 +++++++ .../launcher/auth/YggdrasilLoginService.java | 4 +++- .../launcher/dialog/AccountSelectDialog.java | 22 ++++++++++++------- .../skcraft/launcher/dialog/LoginDialog.java | 19 ++++++++++++++-- .../skcraft/launcher/lang/Launcher.properties | 1 + 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/launcher/src/main/java/com/skcraft/launcher/auth/AuthenticationException.java b/launcher/src/main/java/com/skcraft/launcher/auth/AuthenticationException.java index 675d32e..6290d74 100644 --- a/launcher/src/main/java/com/skcraft/launcher/auth/AuthenticationException.java +++ b/launcher/src/main/java/com/skcraft/launcher/auth/AuthenticationException.java @@ -7,11 +7,14 @@ package com.skcraft.launcher.auth; import com.skcraft.launcher.LauncherException; +import lombok.Getter; /** * Thrown on authentication error. */ public class AuthenticationException extends LauncherException { + @Getter + private boolean invalidatedSession = false; public AuthenticationException(String message, String localizedMessage) { super(message, localizedMessage); @@ -21,6 +24,11 @@ public class AuthenticationException extends LauncherException { super(message, message); } + public AuthenticationException(String message, boolean invalidatedSession) { + super(message, message); + this.invalidatedSession = invalidatedSession; + } + public AuthenticationException(Throwable cause, String localizedMessage) { super(cause, localizedMessage); } diff --git a/launcher/src/main/java/com/skcraft/launcher/auth/YggdrasilLoginService.java b/launcher/src/main/java/com/skcraft/launcher/auth/YggdrasilLoginService.java index 8d39800..1e79934 100644 --- a/launcher/src/main/java/com/skcraft/launcher/auth/YggdrasilLoginService.java +++ b/launcher/src/main/java/com/skcraft/launcher/auth/YggdrasilLoginService.java @@ -54,11 +54,13 @@ public class YggdrasilLoginService implements LoginService { if (req.getResponseCode() != 200) { ErrorResponse error = req.returnContent().asJson(ErrorResponse.class); - throw new AuthenticationException(error.getErrorMessage()); + throw new AuthenticationException(error.getErrorMessage(), true); } else { AuthenticateResponse response = req.returnContent().asJson(AuthenticateResponse.class); Profile profile = response.getSelectedProfile(); + if (profile == null) return null; // Minecraft not owned + if (previous != null && previous.getAvatarImage() != null) { profile.setAvatarImage(previous.getAvatarImage()); } else { 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 83ee469..9221d79 100644 --- a/launcher/src/main/java/com/skcraft/launcher/dialog/AccountSelectDialog.java +++ b/launcher/src/main/java/com/skcraft/launcher/dialog/AccountSelectDialog.java @@ -7,10 +7,7 @@ import com.skcraft.concurrency.ObservableFuture; import com.skcraft.concurrency.ProgressObservable; import com.skcraft.concurrency.SettableProgress; import com.skcraft.launcher.Launcher; -import com.skcraft.launcher.auth.LoginService; -import com.skcraft.launcher.auth.OfflineSession; -import com.skcraft.launcher.auth.SavedSession; -import com.skcraft.launcher.auth.Session; +import com.skcraft.launcher.auth.*; import com.skcraft.launcher.persistence.Persistence; import com.skcraft.launcher.swing.LinedBoxPanel; import com.skcraft.launcher.swing.SwingHelper; @@ -100,7 +97,7 @@ public class AccountSelectDialog extends JDialog { Session newSession = LoginDialog.showLoginRequest(this, launcher); if (newSession != null) { - launcher.getAccounts().add(newSession.toSavedSession()); + launcher.getAccounts().update(newSession.toSavedSession()); setResult(newSession); } }); @@ -157,7 +154,7 @@ public class AccountSelectDialog extends JDialog { progress.set(SharedLocale.tr("login.loggingInStatus"), -1)); if (newSession != null) { - launcher.getAccounts().add(newSession.toSavedSession()); + launcher.getAccounts().update(newSession.toSavedSession()); setResult(newSession); } @@ -184,13 +181,22 @@ public class AccountSelectDialog extends JDialog { @Override public void onFailure(Throwable t) { - t.printStackTrace(); + if (t instanceof AuthenticationException) { + if (((AuthenticationException) t).isInvalidatedSession()) { + // Just need to log in again + LoginDialog.ReloginDetails details = new LoginDialog.ReloginDetails(session.getUsername(), t.getLocalizedMessage()); + Session newSession = LoginDialog.showLoginRequest(AccountSelectDialog.this, launcher, details); + + setResult(newSession); + } + } else { + SwingHelper.showErrorDialog(AccountSelectDialog.this, t.getLocalizedMessage(), SharedLocale.tr("errorTitle"), t); + } } }, SwingExecutor.INSTANCE); ProgressDialog.showProgress(this, future, SharedLocale.tr("login.loggingInTitle"), SharedLocale.tr("login.loggingInStatus")); - SwingHelper.addErrorDialogCallback(this, future); } @RequiredArgsConstructor diff --git a/launcher/src/main/java/com/skcraft/launcher/dialog/LoginDialog.java b/launcher/src/main/java/com/skcraft/launcher/dialog/LoginDialog.java index e93abe4..e99e050 100644 --- a/launcher/src/main/java/com/skcraft/launcher/dialog/LoginDialog.java +++ b/launcher/src/main/java/com/skcraft/launcher/dialog/LoginDialog.java @@ -19,6 +19,7 @@ import com.skcraft.launcher.persistence.Persistence; import com.skcraft.launcher.swing.*; import com.skcraft.launcher.util.SharedLocale; import com.skcraft.launcher.util.SwingExecutor; +import lombok.Data; import lombok.Getter; import lombok.NonNull; import lombok.RequiredArgsConstructor; @@ -28,6 +29,7 @@ import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; +import java.util.Optional; import java.util.concurrent.Callable; /** @@ -38,6 +40,7 @@ public class LoginDialog extends JDialog { private final Launcher launcher; @Getter private Session session; + private final JLabel message = new JLabel(SharedLocale.tr("login.defaultMessage")); private final JTextField usernameText = new JTextField(); private final JPasswordField passwordText = new JPasswordField(); private final JButton loginButton = new JButton(SharedLocale.tr("login.login")); @@ -52,7 +55,7 @@ public class LoginDialog extends JDialog { * @param owner the owner * @param launcher the launcher */ - public LoginDialog(Window owner, @NonNull Launcher launcher) { + public LoginDialog(Window owner, @NonNull Launcher launcher, Optional reloginDetails) { super(owner, ModalityType.DOCUMENT_MODAL); this.launcher = launcher; @@ -72,6 +75,8 @@ public class LoginDialog extends JDialog { dispose(); } }); + + reloginDetails.ifPresent(details -> message.setText(details.message)); } @SuppressWarnings("unchecked") @@ -80,6 +85,7 @@ public class LoginDialog extends JDialog { loginButton.setFont(loginButton.getFont().deriveFont(Font.BOLD)); + formPanel.addRow(message); formPanel.addRow(new JLabel(SharedLocale.tr("login.idEmail")), usernameText); formPanel.addRow(new JLabel(SharedLocale.tr("login.password")), passwordText); buttonsPanel.setBorder(BorderFactory.createEmptyBorder(26, 13, 13, 13)); @@ -144,7 +150,11 @@ public class LoginDialog extends JDialog { } public static Session showLoginRequest(Window owner, Launcher launcher) { - LoginDialog dialog = new LoginDialog(owner, launcher); + return showLoginRequest(owner, launcher, null); + } + + public static Session showLoginRequest(Window owner, Launcher launcher, ReloginDetails reloginDetails) { + LoginDialog dialog = new LoginDialog(owner, launcher, Optional.ofNullable(reloginDetails)); dialog.setVisible(true); return dialog.getSession(); } @@ -186,4 +196,9 @@ public class LoginDialog extends JDialog { } } + @Data + public static class ReloginDetails { + private final String username; + private final String message; + } } diff --git a/launcher/src/main/resources/com/skcraft/launcher/lang/Launcher.properties b/launcher/src/main/resources/com/skcraft/launcher/lang/Launcher.properties index 085d65e..f4d52a7 100644 --- a/launcher/src/main/resources/com/skcraft/launcher/lang/Launcher.properties +++ b/launcher/src/main/resources/com/skcraft/launcher/lang/Launcher.properties @@ -100,6 +100,7 @@ login.login=Login... login.recoverAccount=Forgot your login? login.playOffline=Play offline login.title=Minecraft Login +login.defaultMessage=Log in with your Mojang account login.idEmail=ID/Email\: login.password=Password\: login.noPasswordError=Please enter a password.