Skip to content

Commit

Permalink
delete unused methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvr committed Oct 1, 2024
1 parent bc3fbac commit 4543168
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 173 deletions.
14 changes: 0 additions & 14 deletions src/main/java/cc/unknown/ui/auth/MicrosoftAccount.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package cc.unknown.ui.auth;

import java.util.function.Consumer;

import cc.unknown.ui.auth.MicrosoftLogin.LoginData;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -16,17 +13,6 @@ public MicrosoftAccount(String name, String uuid, String accessToken, String ref
this.refreshToken = refreshToken;
}

public static MicrosoftAccount create() {
MicrosoftAccount account = new MicrosoftAccount("", "", "", "");
Consumer<String> consumer = (String refreshToken) -> {
account.setRefreshToken(refreshToken);
account.login();
};

MicrosoftLogin.getRefreshToken(consumer);
return account;
}

@Override
public boolean login() {
if (refreshToken.isEmpty()) return super.login();
Expand Down
159 changes: 0 additions & 159 deletions src/main/java/cc/unknown/ui/auth/MicrosoftLogin.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
package cc.unknown.ui.auth;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

import com.google.gson.Gson;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -29,8 +11,6 @@

public class MicrosoftLogin {

private static final ExecutorService executor = Executors.newSingleThreadExecutor();

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand All @@ -48,46 +28,6 @@ public boolean isGood() {
private static final String CLIENT_ID = "ba89e6e0-8490-4a26-8746-f389a0d3ccc7", CLIENT_SECRET = "hlQ8Q~33jTRilP4yE-UtuOt9wG.ZFLqq6pErIa2B";
private static final int PORT = 8247;

private static HttpServer server;
private static Consumer<String> callback;

private static void browse(final String url) {
String[] browsers = {
"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe " + url + " -incognito",
"C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe " + url + " -incognito",
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe " + url + " -incognito",
"open -na \"Google Chrome\" --args --incognito \"" + url + "\""
};

for (String browser : browsers) {
try {
Runtime.getRuntime().exec(browser);
return;
} catch (Exception ignored) {
}
}
}

private static void copy(final String url) {
// set clipboard to url
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(url), null);
}

public static void getRefreshToken(final Consumer<String> callback) {
MicrosoftLogin.callback = callback;

startServer();
copy("https://login.live.com/oauth20_authorize.srf?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&response_type=code&redirect_uri=http://localhost:" + PORT + "&scope=XboxLive.signin%20offline_access&prompt=select_account");
}

public static void getRefreshTokenAndOpenBrowser(final Consumer<String> callback) {
MicrosoftLogin.callback = callback;

startServer();
browse("https://login.live.com/oauth20_authorize.srf?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&response_type=code&redirect_uri=http://localhost:" + PORT + "&scope=XboxLive.signin%20offline_access&prompt=select_account");
}

private static final Gson gson = new Gson();

public static LoginData login(String refreshToken) {
Expand Down Expand Up @@ -126,13 +66,6 @@ public static LoginData login(String refreshToken) {

if (mcRes == null) return new LoginData();

// Check game ownership
// final GameOwnershipResponse gameOwnershipRes = gson.fromJson(
// Browser.getBearerResponse("https://api.minecraftservices.com/entitlements/mcstore", mcRes.access_token),
// GameOwnershipResponse.class);
//
// if (gameOwnershipRes == null || !gameOwnershipRes.hasGameOwnership()) return new LoginData();

// Profile
final ProfileResponse profileRes = gson.fromJson(
Browser.getBearerResponse("https://api.minecraftservices.com/minecraft/profile", mcRes.access_token),
Expand All @@ -143,77 +76,8 @@ public static LoginData login(String refreshToken) {
return new LoginData(mcRes.access_token, refreshToken, profileRes.id, profileRes.name);
}

private static void startServer() {
if (server != null) return;

try {
server = HttpServer.create(new InetSocketAddress("localhost", PORT), 0);

server.createContext("/", new Handler());
server.setExecutor(executor);
server.start();
} catch (final IOException e) {
e.printStackTrace();
}
}

private static void stopServer() {
if (server == null) return;

server.stop(0);
server = null;

callback = null;
}

private static class Handler implements HttpHandler {
@Override
public void handle(final HttpExchange req) throws IOException {
if (req.getRequestMethod().equals("GET")) {
// Login
final List<NameValuePair> query = URLEncodedUtils.parse(req.getRequestURI(), StandardCharsets.UTF_8.name());

boolean ok = false;

for (final NameValuePair pair : query) {
if (pair.getName().equals("code")) {
handleCode(pair.getValue());

ok = true;
break;
}
}

if (!ok) writeText(req, "Cannot authenticate.");
else writeText(req, "<html>You may now close this page, if you didn't specify an account then clear your cookies to select an individual one.</html>");
}

stopServer();
}

private void handleCode(final String code) {
//System.out.println(code);
final String response = Browser.postExternal("https://login.live.com/oauth20_token.srf",
"client_id=" + CLIENT_ID + "&code=" + code + "&client_secret=" + CLIENT_SECRET + "&grant_type=authorization_code&redirect_uri=http://localhost:" + PORT, false);
final AuthTokenResponse res = gson.fromJson(
response,
AuthTokenResponse.class);

if (res == null) callback.accept(null);
else callback.accept(res.refresh_token);
}

private void writeText(final HttpExchange req, final String text) throws IOException {
final OutputStream out = req.getResponseBody();

req.getResponseHeaders().add("Content-Type", "text/html; charset=utf-8");
req.sendResponseHeaders(200, text.length());

out.write(text.getBytes(StandardCharsets.UTF_8));
out.flush();
out.close();
}
}

private static class AuthTokenResponse {
@Expose
Expand Down Expand Up @@ -251,29 +115,6 @@ public static class McResponse {
public String access_token;
}

private static class GameOwnershipResponse {
@Expose
@SerializedName("items")
private Item[] items;

private static class Item {
@Expose
@SerializedName("name")
private String name;
}

private boolean hasGameOwnership() {
boolean hasProduct = false;
boolean hasGame = false;

for (final Item item : items) {
if (item.name.equals("product_minecraft")) hasProduct = true;
else if (item.name.equals("game_minecraft")) hasGame = true;
}

return hasProduct && hasGame;
}
}

public static class ProfileResponse {
@Expose
Expand Down

0 comments on commit 4543168

Please sign in to comment.