mirror of
https://github.com/SKCraft/Launcher.git
synced 2024-11-27 12:46:22 +01:00
Handle cancelled OAuth requests
This commit is contained in:
parent
1d4e46214b
commit
93c3f04cbb
@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
|||||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||||
import com.skcraft.launcher.auth.microsoft.MicrosoftWebAuthorizer;
|
import com.skcraft.launcher.auth.microsoft.MicrosoftWebAuthorizer;
|
||||||
import com.skcraft.launcher.auth.microsoft.MinecraftServicesAuthorizer;
|
import com.skcraft.launcher.auth.microsoft.MinecraftServicesAuthorizer;
|
||||||
|
import com.skcraft.launcher.auth.microsoft.OauthResult;
|
||||||
import com.skcraft.launcher.auth.microsoft.XboxTokenAuthorizer;
|
import com.skcraft.launcher.auth.microsoft.XboxTokenAuthorizer;
|
||||||
import com.skcraft.launcher.auth.microsoft.model.McAuthResponse;
|
import com.skcraft.launcher.auth.microsoft.model.McAuthResponse;
|
||||||
import com.skcraft.launcher.auth.microsoft.model.McProfileResponse;
|
import com.skcraft.launcher.auth.microsoft.model.McProfileResponse;
|
||||||
@ -30,12 +31,17 @@ public class MicrosoftLoginService implements LoginService {
|
|||||||
|
|
||||||
public Session login() throws IOException, InterruptedException, AuthenticationException {
|
public Session login() throws IOException, InterruptedException, AuthenticationException {
|
||||||
MicrosoftWebAuthorizer authorizer = new MicrosoftWebAuthorizer(clientId);
|
MicrosoftWebAuthorizer authorizer = new MicrosoftWebAuthorizer(clientId);
|
||||||
String code = authorizer.authorize();
|
OauthResult auth = authorizer.authorize();
|
||||||
|
|
||||||
|
if (auth.isError()) {
|
||||||
|
OauthResult.Error error = (OauthResult.Error) auth;
|
||||||
|
throw new AuthenticationException(error.getErrorMessage());
|
||||||
|
}
|
||||||
|
|
||||||
TokenResponse response = exchangeToken(form -> {
|
TokenResponse response = exchangeToken(form -> {
|
||||||
form.add("grant_type", "authorization_code");
|
form.add("grant_type", "authorization_code");
|
||||||
form.add("redirect_uri", authorizer.getRedirectUri());
|
form.add("redirect_uri", authorizer.getRedirectUri());
|
||||||
form.add("code", code);
|
form.add("code", ((OauthResult.Success) auth).getAuthCode());
|
||||||
});
|
});
|
||||||
|
|
||||||
Profile session = performLogin(response.getAccessToken(), null);
|
Profile session = performLogin(response.getAccessToken(), null);
|
||||||
|
@ -18,7 +18,7 @@ public class MicrosoftWebAuthorizer {
|
|||||||
private final String clientId;
|
private final String clientId;
|
||||||
@Getter private String redirectUri;
|
@Getter private String redirectUri;
|
||||||
|
|
||||||
public String authorize() throws IOException, AuthenticationException, InterruptedException {
|
public OauthResult authorize() throws IOException, AuthenticationException, InterruptedException {
|
||||||
if (Desktop.isDesktopSupported()) {
|
if (Desktop.isDesktopSupported()) {
|
||||||
// Interactive auth
|
// Interactive auth
|
||||||
return authorizeInteractive();
|
return authorizeInteractive();
|
||||||
@ -28,7 +28,7 @@ public class MicrosoftWebAuthorizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String authorizeInteractive() throws IOException, AuthenticationException, InterruptedException {
|
private OauthResult authorizeInteractive() throws IOException, AuthenticationException, InterruptedException {
|
||||||
OauthHttpHandler httpHandler = new OauthHttpHandler();
|
OauthHttpHandler httpHandler = new OauthHttpHandler();
|
||||||
Desktop.getDesktop().browse(generateInteractiveUrl(httpHandler.getPort()));
|
Desktop.getDesktop().browse(generateInteractiveUrl(httpHandler.getPort()));
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ import java.util.concurrent.Executors;
|
|||||||
public class OauthHttpHandler {
|
public class OauthHttpHandler {
|
||||||
private Executor executor = Executors.newCachedThreadPool();
|
private Executor executor = Executors.newCachedThreadPool();
|
||||||
private HttpServer server;
|
private HttpServer server;
|
||||||
private String result;
|
private OauthResult result;
|
||||||
|
|
||||||
public OauthHttpHandler() throws IOException {
|
public OauthHttpHandler() throws IOException {
|
||||||
server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
|
server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
|
||||||
@ -31,7 +31,7 @@ public class OauthHttpHandler {
|
|||||||
return server.getAddress().getPort();
|
return server.getAddress().getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String await() throws InterruptedException {
|
public OauthResult await() throws InterruptedException {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
this.wait();
|
this.wait();
|
||||||
}
|
}
|
||||||
@ -46,7 +46,11 @@ public class OauthHttpHandler {
|
|||||||
public void handle(HttpExchange httpExchange) throws IOException {
|
public void handle(HttpExchange httpExchange) throws IOException {
|
||||||
String query = httpExchange.getRequestURI().getQuery();
|
String query = httpExchange.getRequestURI().getQuery();
|
||||||
Map<String, String> qs = Splitter.on('&').withKeyValueSeparator('=').split(query);
|
Map<String, String> qs = Splitter.on('&').withKeyValueSeparator('=').split(query);
|
||||||
result = qs.get("code");
|
if (qs.get("error") != null) {
|
||||||
|
result = new OauthResult.Error(qs.get("error_description"));
|
||||||
|
} else {
|
||||||
|
result = new OauthResult.Success(qs.get("code"));
|
||||||
|
}
|
||||||
|
|
||||||
synchronized (OauthHttpHandler.this) {
|
synchronized (OauthHttpHandler.this) {
|
||||||
OauthHttpHandler.this.notifyAll();
|
OauthHttpHandler.this.notifyAll();
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.skcraft.launcher.auth.microsoft;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
public interface OauthResult {
|
||||||
|
boolean isError();
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
class Success implements OauthResult {
|
||||||
|
@Getter private final String authCode;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isError() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
class Error implements OauthResult {
|
||||||
|
@Getter private final String errorMessage;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isError() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user