Skip to content

Commit 3bed03c

Browse files
CiiLu3gf8jv4dv
andauthored
设备代码流在完成授权后隐藏二维码 (#6087)
Co-authored-by: 3gf8jv4dv <3gf8jv4dv@gmail.com>
1 parent d295e60 commit 3bed03c

6 files changed

Lines changed: 40 additions & 2 deletions

File tree

HMCL/src/main/java/org/jackhuang/hmcl/game/OAuthServer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030

3131
import java.io.IOException;
3232
import java.security.SecureRandom;
33-
import java.util.*;
33+
import java.util.Base64;
34+
import java.util.HashMap;
35+
import java.util.Locale;
36+
import java.util.Map;
3437
import java.util.concurrent.CompletableFuture;
3538
import java.util.concurrent.ExecutionException;
3639

@@ -172,6 +175,7 @@ public void close() {
172175

173176
public static class Factory implements OAuth.Callback {
174177
public final EventManager<GrantDeviceCodeEvent> onGrantDeviceCode = new EventManager<>();
178+
public final EventManager<LoginCompletedDeviceCodeEvent> onLoginCompletedDeviceCode = new EventManager<>();
175179
public final EventManager<OpenBrowserEvent> onOpenBrowserAuthorizationCode = new EventManager<>();
176180
public final EventManager<OpenBrowserEvent> onOpenBrowserDevice = new EventManager<>();
177181

@@ -199,6 +203,11 @@ public void grantDeviceCode(String userCode, String verificationURI) {
199203
onGrantDeviceCode.fireEvent(new GrantDeviceCodeEvent(this, userCode, verificationURI));
200204
}
201205

206+
@Override
207+
public void loginCompletedDeviceCode() {
208+
onLoginCompletedDeviceCode.fireEvent(new LoginCompletedDeviceCodeEvent(this));
209+
}
210+
202211
@Override
203212
public void openBrowser(OAuth.GrantFlow grantFlow, String url) throws IOException {
204213
lastlyOpenedURL = url;
@@ -235,6 +244,12 @@ public String getVerificationUri() {
235244
}
236245
}
237246

247+
public static class LoginCompletedDeviceCodeEvent extends Event {
248+
public LoginCompletedDeviceCodeEvent(Object source) {
249+
super(source);
250+
}
251+
}
252+
238253
public static class OpenBrowserEvent extends Event {
239254
private final String url;
240255

HMCL/src/main/java/org/jackhuang/hmcl/ui/account/MicrosoftAccountLoginPane.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public MicrosoftAccountLoginPane(Account account, Consumer<AuthInfo> callback, R
124124
step.set(new Step.WaitForScanQrCode(event.getUserCode(), event.getVerificationUri()));
125125
}));
126126

127+
holder.registerWeak(Accounts.OAUTH_CALLBACK.onLoginCompletedDeviceCode, event -> Platform.runLater(() -> {
128+
if (step.get() instanceof Step.WaitForScanQrCode)
129+
step.set(new Step.DeviceLoginCompleted());
130+
}));
131+
127132
this.step.set(Accounts.OAUTH_CALLBACK.getClientId().isEmpty()
128133
? new Step.Init()
129134
: new Step.StartAuthorizationCodeLogin());
@@ -220,6 +225,13 @@ private void onStep(Step currentStep) {
220225
codeBox.setMaxWidth(USE_PREF_SIZE);
221226

222227
rootContainer.getChildren().addAll(deviceHint, new Group(qrCode), codeBox);
228+
} else if (currentStep instanceof Step.DeviceLoginCompleted) {
229+
loginButtonSpinner.setLoading(true);
230+
231+
var hintPane = new HintPane(MessageDialogPane.MessageType.INFO);
232+
hintPane.setSegment(i18n("account.methods.microsoft.methods.device.hint.completed"));
233+
234+
rootContainer.getChildren().addAll(hintPane);
223235
} else if (currentStep instanceof Step.LoginFailed failed) {
224236
btnLogin.setOnAction(e -> this.step.set(new Step.StartAuthorizationCodeLogin()));
225237
loginButtonSpinner.setLoading(false);
@@ -312,6 +324,10 @@ record WaitForScanQrCode(String userCode, String verificationUri) implements Ste
312324

313325
}
314326

327+
record DeviceLoginCompleted() implements Step {
328+
329+
}
330+
315331
record LoginFailed(String message) implements Step {
316332
}
317333
}

HMCL/src/main/resources/assets/lang/I18N.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ account.methods.microsoft.birth=How to Change Your Account Birth Date
106106
account.methods.microsoft.code=Code (Copied to Clipboard)
107107
account.methods.microsoft.close_page=Microsoft account authorization is now completed.\n\
108108
\n\
109-
There are some extra works for us, but you can safely close this tab for now.
109+
There are some extra work for us, but you can safely close this tab for now.
110110
account.methods.microsoft.deauthorize=Deauthorize
111111
account.methods.microsoft.error.add_family=Please click <a href="https://support.microsoft.com/account-billing/837badbc-999e-54d2-2617-d19206b9540a">here</a> to change your account birth date to be over 18 years old, or add your account to a family.
112112
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
122122
account.methods.microsoft.hint=Click the "Log in" button to start adding your Microsoft account.
123123
account.methods.microsoft.methods.device=Log In with QR Code
124124
account.methods.microsoft.methods.device.hint=Scan QR code or visit <a href="%s">%s</a> to complete login, enter <b>%s</b> in the opened page.
125+
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.
125126
account.methods.microsoft.methods.browser=Log In via Browser
126127
account.methods.microsoft.methods.browser.hint=Click the "Log in" button or <a href="%s">copy the link</a> and paste it into the browser to log in.
127128
account.methods.microsoft.manual=<b>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.</b>

HMCL/src/main/resources/assets/lang/I18N_zh.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ account.methods.microsoft.makegameidsettings=建立檔案 / 編輯檔案名稱
122122
account.methods.microsoft.hint=點擊「登入」按鈕開始新增 Microsoft 帳戶。
123123
account.methods.microsoft.methods.device=掃描 QR Code 登入
124124
account.methods.microsoft.methods.device.hint=掃描 QR Code 或訪問 <a href="%s">%s</a>,在開啟的頁面中輸入 <b>%s</b> 完成登入。
125+
account.methods.microsoft.methods.device.hint.completed=已完成 Microsoft 帳戶授權。其餘登入步驟將由啟動器自動執行,請稍候。
125126
account.methods.microsoft.methods.browser=在瀏覽器中登入
126127
account.methods.microsoft.methods.browser.hint=點擊「登入」按鈕或者<a href="%s">複製連結</a>並在瀏覽器中貼上以登入。
127128
account.methods.microsoft.manual=<b>若網路環境不佳,可能會導致網頁載入緩慢甚至無法載入,請稍後再試或更換網路環境後再試。</b>

HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ account.methods.microsoft.makegameidsettings=创建档案 / 编辑档案名称
123123
account.methods.microsoft.hint=点击“登录”按钮开始添加微软账户。
124124
account.methods.microsoft.methods.device=扫描二维码登录
125125
account.methods.microsoft.methods.device.hint=扫描二维码或访问 <a href="%s">%s</a>,在打开的页面中输入 <b>%s</b> 完成登录。
126+
account.methods.microsoft.methods.device.hint.completed=已完成微软账户授权。其余登录步骤将由启动器自动执行,请稍候。
126127
account.methods.microsoft.methods.browser=在浏览器登录
127128
account.methods.microsoft.methods.browser.hint=点击“登录”按钮或者<a href="%s">复制链接</a>并在浏览器中粘贴以登录。
128129
account.methods.microsoft.manual=<b>若网络环境不佳,可能会导致网页加载缓慢甚至无法加载,请使用网络代理并重试。</b>\n\

HMCLCore/src/main/java/org/jackhuang/hmcl/auth/OAuth.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ private Result authenticateDevice(Options options) throws IOException, Interrupt
160160
continue;
161161
}
162162

163+
options.callback.loginCompletedDeviceCode();
164+
163165
return new Result(tokenResponse.accessToken, tokenResponse.refreshToken);
164166
}
165167
}
@@ -268,6 +270,8 @@ public interface Callback {
268270

269271
void grantDeviceCode(String userCode, String verificationURI);
270272

273+
void loginCompletedDeviceCode();
274+
271275
/**
272276
* Open browser
273277
*

0 commit comments

Comments
 (0)