Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion HMCL/src/main/java/org/jackhuang/hmcl/game/OAuthServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -172,6 +175,7 @@ public void close() {

public static class Factory implements OAuth.Callback {
public final EventManager<GrantDeviceCodeEvent> onGrantDeviceCode = new EventManager<>();
public final EventManager<LoginCompletedDeviceCodeEvent> onLoginCompletedDeviceCode = new EventManager<>();
public final EventManager<OpenBrowserEvent> onOpenBrowserAuthorizationCode = new EventManager<>();
public final EventManager<OpenBrowserEvent> onOpenBrowserDevice = new EventManager<>();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public MicrosoftAccountLoginPane(Account account, Consumer<AuthInfo> 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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -312,6 +324,10 @@ record WaitForScanQrCode(String userCode, String verificationUri) implements Ste

}

record DeviceLoginCompleted() implements Step {

}

record LoginFailed(String message) implements Step {
}
}
Expand Down
Loading