diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/OAuthServer.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/OAuthServer.java index daa1fc90fc..a96de02b72 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/OAuthServer.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/OAuthServer.java @@ -30,7 +30,10 @@ import java.io.IOException; import java.security.SecureRandom; -import java.util.*; +import java.util.Base64; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -172,6 +175,7 @@ public void close() { public static class Factory implements OAuth.Callback { public final EventManager onGrantDeviceCode = new EventManager<>(); + public final EventManager onLoginCompletedDeviceCode = new EventManager<>(); public final EventManager onOpenBrowserAuthorizationCode = new EventManager<>(); public final EventManager onOpenBrowserDevice = new EventManager<>(); @@ -199,6 +203,11 @@ public void grantDeviceCode(String userCode, String verificationURI) { onGrantDeviceCode.fireEvent(new GrantDeviceCodeEvent(this, userCode, verificationURI)); } + @Override + public void loginCompletedDeviceCode() { + onLoginCompletedDeviceCode.fireEvent(new LoginCompletedDeviceCodeEvent(this)); + } + @Override public void openBrowser(OAuth.GrantFlow grantFlow, String url) throws IOException { lastlyOpenedURL = url; @@ -235,6 +244,12 @@ public String getVerificationUri() { } } + public static class LoginCompletedDeviceCodeEvent extends Event { + public LoginCompletedDeviceCodeEvent(Object source) { + super(source); + } + } + public static class OpenBrowserEvent extends Event { private final String url; diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/MicrosoftAccountLoginPane.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/MicrosoftAccountLoginPane.java index ce6f9c2e4d..72f57393a9 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/MicrosoftAccountLoginPane.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/MicrosoftAccountLoginPane.java @@ -124,6 +124,11 @@ public MicrosoftAccountLoginPane(Account account, Consumer callback, R step.set(new Step.WaitForScanQrCode(event.getUserCode(), event.getVerificationUri())); })); + holder.registerWeak(Accounts.OAUTH_CALLBACK.onLoginCompletedDeviceCode, event -> Platform.runLater(() -> { + if (step.get() instanceof Step.WaitForScanQrCode) + step.set(new Step.DeviceLoginCompleted()); + })); + this.step.set(Accounts.OAUTH_CALLBACK.getClientId().isEmpty() ? new Step.Init() : new Step.StartAuthorizationCodeLogin()); @@ -220,6 +225,13 @@ private void onStep(Step currentStep) { codeBox.setMaxWidth(USE_PREF_SIZE); rootContainer.getChildren().addAll(deviceHint, new Group(qrCode), codeBox); + } else if (currentStep instanceof Step.DeviceLoginCompleted) { + loginButtonSpinner.setLoading(true); + + var hintPane = new HintPane(MessageDialogPane.MessageType.INFO); + hintPane.setSegment(i18n("account.methods.microsoft.methods.device.hint.completed")); + + rootContainer.getChildren().addAll(hintPane); } else if (currentStep instanceof Step.LoginFailed failed) { btnLogin.setOnAction(e -> this.step.set(new Step.StartAuthorizationCodeLogin())); loginButtonSpinner.setLoading(false); @@ -312,6 +324,10 @@ record WaitForScanQrCode(String userCode, String verificationUri) implements Ste } + record DeviceLoginCompleted() implements Step { + + } + record LoginFailed(String message) implements Step { } } diff --git a/HMCL/src/main/resources/assets/lang/I18N.properties b/HMCL/src/main/resources/assets/lang/I18N.properties index 731eaee788..105e31707e 100644 --- a/HMCL/src/main/resources/assets/lang/I18N.properties +++ b/HMCL/src/main/resources/assets/lang/I18N.properties @@ -106,7 +106,7 @@ account.methods.microsoft.birth=How to Change Your Account Birth Date account.methods.microsoft.code=Code (Copied to Clipboard) account.methods.microsoft.close_page=Microsoft account authorization is now completed.\n\ \n\ - There are some extra works for us, but you can safely close this tab for now. + There are some extra work for us, but you can safely close this tab for now. account.methods.microsoft.deauthorize=Deauthorize account.methods.microsoft.error.add_family=Please click here to change your account birth date to be over 18 years old, or add your account to a family. account.methods.microsoft.error.country_unavailable=Xbox Live is not available in your current country/region. @@ -122,6 +122,7 @@ account.methods.microsoft.makegameidsettings=Create Profile / Edit Profile Name account.methods.microsoft.hint=Click the "Log in" button to start adding your Microsoft account. account.methods.microsoft.methods.device=Log In with QR Code account.methods.microsoft.methods.device.hint=Scan QR code or visit %s to complete login, enter %s in the opened page. +account.methods.microsoft.methods.device.hint.completed=Microsoft account authorization is now completed. There are some extra work for us, just wait for a while. account.methods.microsoft.methods.browser=Log In via Browser account.methods.microsoft.methods.browser.hint=Click the "Log in" button or copy the link and paste it into the browser to log in. account.methods.microsoft.manual=If your internet connection is bad, it may cause web pages to load slowly or fail to load altogether.\nYou may try again later or switch to a different internet connection. diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh.properties b/HMCL/src/main/resources/assets/lang/I18N_zh.properties index 90f2157ece..efb24f963e 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh.properties @@ -122,6 +122,7 @@ account.methods.microsoft.makegameidsettings=建立檔案 / 編輯檔案名稱 account.methods.microsoft.hint=點擊「登入」按鈕開始新增 Microsoft 帳戶。 account.methods.microsoft.methods.device=掃描 QR Code 登入 account.methods.microsoft.methods.device.hint=掃描 QR Code 或訪問 %s,在開啟的頁面中輸入 %s 完成登入。 +account.methods.microsoft.methods.device.hint.completed=已完成 Microsoft 帳戶授權。其餘登入步驟將由啟動器自動執行,請稍候。 account.methods.microsoft.methods.browser=在瀏覽器中登入 account.methods.microsoft.methods.browser.hint=點擊「登入」按鈕或者複製連結並在瀏覽器中貼上以登入。 account.methods.microsoft.manual=若網路環境不佳,可能會導致網頁載入緩慢甚至無法載入,請稍後再試或更換網路環境後再試。 diff --git a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties index 976a48046c..e7983b2286 100644 --- a/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties +++ b/HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties @@ -123,6 +123,7 @@ account.methods.microsoft.makegameidsettings=创建档案 / 编辑档案名称 account.methods.microsoft.hint=点击“登录”按钮开始添加微软账户。 account.methods.microsoft.methods.device=扫描二维码登录 account.methods.microsoft.methods.device.hint=扫描二维码或访问 %s,在打开的页面中输入 %s 完成登录。 +account.methods.microsoft.methods.device.hint.completed=已完成微软账户授权。其余登录步骤将由启动器自动执行,请稍候。 account.methods.microsoft.methods.browser=在浏览器登录 account.methods.microsoft.methods.browser.hint=点击“登录”按钮或者复制链接并在浏览器中粘贴以登录。 account.methods.microsoft.manual=若网络环境不佳,可能会导致网页加载缓慢甚至无法加载,请使用网络代理并重试。\n\ diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/OAuth.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/OAuth.java index d47860d617..0393f76437 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/OAuth.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/OAuth.java @@ -160,6 +160,8 @@ private Result authenticateDevice(Options options) throws IOException, Interrupt continue; } + options.callback.loginCompletedDeviceCode(); + return new Result(tokenResponse.accessToken, tokenResponse.refreshToken); } } @@ -268,6 +270,8 @@ public interface Callback { void grantDeviceCode(String userCode, String verificationURI); + void loginCompletedDeviceCode(); + /** * Open browser *