Improve MS login

This commit is contained in:
Johannes Frohnmeyer 2021-12-11 15:37:31 +01:00
parent 2d47f2e45f
commit c53b60bf18
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 61 additions and 68 deletions

View File

@ -47,7 +47,6 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'org.slf4j:slf4j-api:1.7.32'
implementation 'ch.qos.logback:logback-classic:1.2.7'
implementation 'net.freeutils:jlhttp:2.6'
implementation 'org.eclipse.jgit:org.eclipse.jgit:6.0.0.202111291000-r'
implementation project(":wrapper")

View File

@ -1,26 +1,28 @@
package io.gitlab.jfronny.inceptum.windows;
import com.sun.net.httpserver.HttpServer;
import imgui.ImGui;
import io.gitlab.jfronny.inceptum.Inceptum;
import io.gitlab.jfronny.inceptum.InceptumGui;
import io.gitlab.jfronny.inceptum.model.microsoft.*;
import io.gitlab.jfronny.inceptum.util.Utils;
import io.gitlab.jfronny.inceptum.util.api.account.AccountManager;
import io.gitlab.jfronny.inceptum.util.api.account.MicrosoftAccount;
import io.gitlab.jfronny.inceptum.util.api.account.MicrosoftAuthAPI;
import io.gitlab.jfronny.inceptum.util.Utils;
import net.freeutils.httpserver.HTTPServer;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
public class MicrosoftLoginWindow extends Window {
private static HTTPServer server = new HTTPServer(28562);
private static HTTPServer.VirtualHost host = server.getVirtualHost(null);
private static HttpServer server;
private static final String MICROSOFT_LOGIN_URL = "https://login.live.com/oauth20_authorize.srf?client_id=90890812-00d1-48a8-8d3f-38465ef43b58&prompt=select_account&cobrandid=8058f65d-ce06-4c30-9559-473c9275a65d&response_type=code&scope=XboxLive.signin%20XboxLive.offline_access&redirect_uri=http%3A%2F%2F127.0.0.1%3A28562";
private MicrosoftAccount account;
private final MicrosoftAccount account;
public MicrosoftLoginWindow() {
this(null);
@ -39,7 +41,7 @@ public class MicrosoftLoginWindow extends Window {
@Override
public void draw() {
ImGui.text("This feature uses modified ATLauncher code");
ImGui.text("This feature uses modified ATLauncher code, so the login prompt will ask you to log in to ATLauncher");
ImGui.text("Click the button below to begin");
if (ImGui.button("Open in Browser")) {
try {
@ -51,43 +53,61 @@ public class MicrosoftLoginWindow extends Window {
}
private void startServer() throws Exception {
host.addContext("/", (req, res) -> {
if (req.getParams().containsKey("error")) {
res.getHeaders().add("Content-Type", "text/plain");
res.send(500, "Error logging in. Check console for more information");
Inceptum.LOGGER.error("Error logging into Microsoft account: " + URLDecoder
.decode(req.getParams().get("error_description"), StandardCharsets.UTF_8.toString()));
close();
return 0;
}
if (server != null) return;
server = HttpServer.create(new InetSocketAddress("127.0.0.1", MicrosoftAuthAPI.MICROSOFT_LOGIN_REDIRECT_PORT), 0);
if (!req.getParams().containsKey("code")) {
res.getHeaders().add("Content-Type", "text/plain");
res.send(400, "Code is missing");
close();
return 0;
}
server.createContext("/", req -> {
if (req.getRequestMethod().equals("GET")) {
Map<String, String> query = new LinkedHashMap<>();
for (String pair : req.getRequestURI().getQuery().split("&")) {
int index = pair.indexOf('=');
query.put(URLDecoder.decode(pair.substring(0, index), StandardCharsets.UTF_8), URLDecoder.decode(pair.substring(index + 1), StandardCharsets.UTF_8));
}
try {
acquireAccessToken(req.getParams().get("code"));
} catch (Exception e) {
Inceptum.LOGGER.error("Error acquiring accessToken", e);
res.getHeaders().add("Content-Type", "text/html");
res.send(500, "Error logging in. Check console for more information");
close();
return 0;
}
int respCode;
String respStr;
res.getHeaders().add("Content-Type", "text/plain");
// #. {0} is the name of the launcher
res.send(200, "Login complete. You can now close this window and go back to GLaunch");
close();
return 0;
}, "GET");
if (query.containsKey("error")) {
respCode = 500;
respStr = "Error logging in. Check console for more information";
Inceptum.LOGGER.error("Error logging into Microsoft account: " + URLDecoder
.decode(query.get("error_description"), StandardCharsets.UTF_8.toString()));
}
else if (query.containsKey("code")) {
try {
acquireAccessToken(query.get("code"));
respCode = 200;
respStr = "Login complete. You can now close this window and go back to Inceptum";
} catch (Exception e) {
Inceptum.LOGGER.error("Error acquiring accessToken", e);
respCode = 500;
respStr = "Error logging in. Check console for more information";
}
}
else {
respCode = 400;
respStr = "Code is missing";
}
OutputStream os = req.getResponseBody();
req.sendResponseHeaders(respCode, respStr.length());
os.write(respStr.getBytes(StandardCharsets.UTF_8));
os.flush();
os.close();
stopServer();
}
});
server.start();
}
private void stopServer() {
if (server == null) return;
server.stop(0);
server = null;
}
private void acquireAccessToken(String authcode) throws Exception {
OauthTokenResponse oauthTokenResponse = MicrosoftAuthAPI.tradeCodeForAccessToken(authcode);
@ -101,35 +121,7 @@ public class MicrosoftLoginWindow extends Window {
}
private void acquireXsts(OauthTokenResponse oauthTokenResponse, String xblToken) throws Exception {
XboxLiveAuthResponse xstsAuthResponse = null;
xstsAuthResponse = MicrosoftAuthAPI.getXstsToken(xblToken);
/*try {
xstsAuthResponse = MicrosoftAuthAPI.getXstsToken(xblToken);
} catch (DownloadException e) {
if (e.response != null) {
GLaunch.LOGGER.debug(Gsons.DEFAULT.toJson(e.response));
XboxLiveAuthErrorResponse xboxLiveAuthErrorResponse = Gsons.DEFAULT.fromJson(e.response,
XboxLiveAuthErrorResponse.class);
String error = xboxLiveAuthErrorResponse.getErrorMessageForCode();
if (error != null) {
GLaunch.LOGGER.warn(error);
DialogManager.okDialog().setTitle("Error logging into Xbox Live")
.setContent(new HTMLBuilder().center().text(error).build()).setType(DialogManager.ERROR)
.show();
String link = xboxLiveAuthErrorResponse.getBrowserLinkForCode();
if (link != null) {
Utils.openWebBrowser(new URI(link));
}
}
throw e;
}
}*/
XboxLiveAuthResponse xstsAuthResponse = MicrosoftAuthAPI.getXstsToken(xblToken);
if (xstsAuthResponse != null) {
acquireMinecraftToken(oauthTokenResponse, xstsAuthResponse);
@ -159,8 +151,10 @@ public class MicrosoftLoginWindow extends Window {
}
// add the account
addAccount(oauthTokenResponse, xstsAuthResponse, loginResponse, profile);
close();
Inceptum.showInfo("The account \"" + profile.name + "\" was added successfully", "Account added");
}
private void addAccount(OauthTokenResponse oauthTokenResponse, XboxLiveAuthResponse xstsAuthResponse,