Skip to content

Commit

Permalink
Merge pull request #22 from QuestCraftPlusPlus/MSAL
Browse files Browse the repository at this point in the history
MSAL integration
  • Loading branch information
thejudge156 authored Aug 14, 2023
2 parents f42a1e5 + 85c0a9c commit 81911ed
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 142 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
implementation("androidx.legacy:legacy-preference-v14:1.0.0")
implementation("com.google.android.material:material:1.8.0")
implementation("androidx.annotation:annotation:1.6.0")
implementation("com.microsoft.azure:msal4j:1.13.9")
implementation("androidx.browser:browser:1.5.0")
implementation("blank:unity-classes")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
Expand Down
59 changes: 59 additions & 0 deletions lib/src/main/assets/modernfix-mixins.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This is the configuration file for ModernFix.
# In general, prefer using the config screen to editing this file. It can be accessed
# via the standard mod menu on your respective mod loader. Changes will, however,
# require restarting the game to take effect.
#
# The following options can be enabled or disabled if there is a compatibility issue.
# Add a line with your option name and =true or =false at the bottom of the file to enable
# or disable a rule. For example:
# mixin.perf.dynamic_resources=true
# Do not include the #. You may reset to defaults by deleting this file.
#
# Available options:
# mixin.bugfix.chunk_deadlock=false # (overridden for mod compat)
# mixin.bugfix.concurrency=true # (default)
# mixin.bugfix.ender_dragon_leak=true # (default)
# mixin.bugfix.packet_leak=false # (default)
# mixin.bugfix.paper_chunk_patches=false # (overridden for mod compat)
# mixin.devenv=false # (default)
# mixin.feature.branding=true # (default)
# mixin.feature.direct_stack_trace=false # (default)
# mixin.feature.disable_unihex_font=false # (default)
# mixin.feature.integrated_server_watchdog=true # (default)
# mixin.feature.measure_time=true # (default)
# mixin.feature.snapshot_easter_egg=true # (default)
# mixin.feature.spam_thread_dump=false # (default)
# mixin.feature.spark_profile_launch=false # (default)
# mixin.feature.warn_missing_perf_mods=true # (default)
# mixin.launch.class_search_cache=true # (default)
# mixin.perf.blast_search_trees=true # (default)
# mixin.perf.cache_blockstate_cache_arrays=true # (default)
# mixin.perf.cache_model_materials=true # (default)
# mixin.perf.cache_strongholds=true # (default)
# mixin.perf.cache_upgraded_structures=true # (default)
# mixin.perf.clear_fabric_mapping_tables=false # (default)
# mixin.perf.clear_mixin_classinfo=false # (default)
# mixin.perf.compact_bit_storage=true # (default)
# mixin.perf.dedicated_reload_executor=true # (default)
# mixin.perf.deduplicate_climate_parameters=false # (default)
# mixin.perf.deduplicate_location=false # (default)
# mixin.perf.deduplicate_wall_shapes=true # (default)
# mixin.perf.dynamic_dfu=true # (default)
# mixin.perf.dynamic_entity_renderers=false # (default)
# mixin.perf.dynamic_resources=false # (default)
# mixin.perf.dynamic_resources.diagonalfences=true # (default)
# mixin.perf.dynamic_structure_manager=true # (default)
# mixin.perf.faster_item_rendering=false # (default)
# mixin.perf.faster_texture_stitching=true # (default)
# mixin.perf.model_optimizations=true # (default)
# mixin.perf.nbt_memory_usage=false # (overridden for mod compat)
# mixin.perf.reduce_blockstate_cache_rebuilds=true # (default)
# mixin.perf.remove_biome_temperature_cache=true # (default)
# mixin.perf.remove_spawn_chunks=false # (default)
# mixin.perf.resourcepacks=true # (default)
# mixin.perf.state_definition_construct=true # (default)
# mixin.perf.thread_priorities=true # (default)
# mixin.safety=true # (default)
#
# User overrides go here.
mixin.perf.dynamic_resources=true
53 changes: 21 additions & 32 deletions lib/src/main/java/pojlib/account/MinecraftAccount.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package pojlib.account;

import static pojlib.account.Msa.checkMcProfile;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.json.JSONException;
import pojlib.util.Constants;
import pojlib.util.GsonUtils;
import pojlib.util.LoginHelper;

import java.io.File;
import java.io.FileReader;
Expand All @@ -19,21 +24,23 @@
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Set;

import javax.annotation.Nullable;

public class MinecraftAccount {

public String accessToken;
public String msaRefreshToken;
public String uuid;
public static String username;
public int expiresIn;
public String username;
public long expiresIn;

public final String userType = "msa";

public static MinecraftAccount login(String gameDir, JsonObject response) throws IOException, JSONException {
MinecraftAccount account = Msa.acquireXBLToken(response.get("access_token").getAsString());
account.msaRefreshToken = response.get("refresh_token").getAsString();
account.expiresIn = response.get("expires_in").getAsInt();
public static MinecraftAccount login(String gameDir, String[] response) throws IOException, JSONException {
String mcToken = Msa.acquireXBLToken(response[0]);
MinecraftAccount account = checkMcProfile(mcToken);
account.expiresIn = Long.parseLong(response[1]);

GsonUtils.objectToJsonFile(gameDir + "/account.json", account);
return account;
Expand All @@ -45,34 +52,16 @@ public static boolean logout(String path) {
}

//Try this before using login - the account will have been saved to disk if previously logged in
public static MinecraftAccount load(String path, String client_id) {
MinecraftAccount acc = null;
public static MinecraftAccount load(String path, @Nullable String newToken) {
MinecraftAccount acc;
try {
acc = new Gson().fromJson(new FileReader(path + "/account.json"), MinecraftAccount.class);
URLConnection connection2 = new URL("https://login.microsoftonline.com/consumers/oauth2/v2.0/token").openConnection();
connection2.setDoOutput(true);
connection2.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

String query = String.format("client_id=%s&grant_type=%s&refresh_token=%s",
URLEncoder.encode(client_id, "UTF-8"),
URLEncoder.encode("refresh_token", "UTF-8"),
URLEncoder.encode(acc.msaRefreshToken, "UTF-8"));
try (OutputStream output = connection2.getOutputStream()) {
output.write(query.getBytes(StandardCharsets.UTF_8));
}

InputStream response = connection2.getInputStream();
JsonObject jsonObject = (JsonObject) JsonParser.parseReader(new InputStreamReader(response, StandardCharsets.UTF_8));

acc = Msa.acquireXBLToken(jsonObject.get("access_token").getAsString());
acc.msaRefreshToken = jsonObject.get("refresh_token").getAsString();
acc.expiresIn = jsonObject.get("expires_in").getAsInt();
GsonUtils.objectToJsonFile(path + "/account.json", acc);
return acc;
} catch (JSONException | IOException e) {
if(e instanceof SocketTimeoutException && acc != null) {
return acc;
if(newToken != null) {
acc.accessToken = Msa.acquireXBLToken(newToken);
}
GsonUtils.objectToJsonFile(path + "/account.json", acc);
return acc;
} catch (IOException | JSONException e) {
return null;
}
}
Expand Down
15 changes: 7 additions & 8 deletions lib/src/main/java/pojlib/account/Msa.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static String read(InputStream is) throws IOException {
return out.toString();
}

public static MinecraftAccount acquireXBLToken(String accessToken) throws IOException, JSONException {
public static String acquireXBLToken(String accessToken) throws IOException, JSONException {
URL url = new URL(Constants.XBL_AUTH_URL);

Map<Object, Object> data = new HashMap<>();
Expand Down Expand Up @@ -81,7 +81,7 @@ public static MinecraftAccount acquireXBLToken(String accessToken) throws IOExce
throw new RuntimeException();
}

private static MinecraftAccount acquireXsts(String xblToken) throws IOException, JSONException {
private static String acquireXsts(String xblToken) throws IOException, JSONException {
URL url = new URL(Constants.XSTS_AUTH_URL);

Map<Object, Object> data = new HashMap<>();
Expand Down Expand Up @@ -118,7 +118,7 @@ private static MinecraftAccount acquireXsts(String xblToken) throws IOException,
throw new RuntimeException();
}

private static MinecraftAccount acquireMinecraftToken(String xblUhs, String xblXsts) throws IOException, JSONException {
private static String acquireMinecraftToken(String xblUhs, String xblXsts) throws IOException, JSONException {
URL url = new URL(Constants.MC_LOGIN_URL);

Map<Object, Object> data = new HashMap<>();
Expand All @@ -142,9 +142,7 @@ private static MinecraftAccount acquireMinecraftToken(String xblUhs, String xblX

if(!jo.isNull("access_token")) {
checkMcStore(jo.getString("access_token"));
MinecraftAccount account = checkMcProfile(jo.getString("access_token"));
account.accessToken = jo.getString("access_token");
return account;
return jo.getString("access_token");
}

File errorFile = new File(Constants.USER_HOME + "/errors.txt");
Expand Down Expand Up @@ -172,7 +170,7 @@ private static void checkMcStore(String mcAccessToken) throws IOException {
}
}

private static MinecraftAccount checkMcProfile(String mcAccessToken) throws IOException, JSONException {
public static MinecraftAccount checkMcProfile(String mcAccessToken) throws IOException, JSONException {
URL url = new URL(Constants.MC_PROFILE_URL);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Expand All @@ -190,13 +188,14 @@ private static MinecraftAccount checkMcProfile(String mcAccessToken) throws IOEx
JSONObject jsonObject = new JSONObject(s);
String name = (String) jsonObject.get("name");
String uuid = (String) jsonObject.get("id");
String uuidDashes = uuid .replaceFirst(
String uuidDashes = uuid.replaceFirst(
"(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5"
);

MinecraftAccount account = new MinecraftAccount();
account.username = name;
account.uuid = uuidDashes;
account.accessToken = mcAccessToken;
return account;
}

Expand Down
Loading

0 comments on commit 81911ed

Please sign in to comment.