Skip to content

Commit

Permalink
Merge pull request #368 from mircokroon/msauth
Browse files Browse the repository at this point in the history
Microsoft authentication
  • Loading branch information
mircokroon authored Jun 5, 2022
2 parents 3a7fb8c + 7de6704 commit bfb283a
Show file tree
Hide file tree
Showing 26 changed files with 842 additions and 295 deletions.
41 changes: 24 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,37 +152,44 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
<version>2.11.0</version>
</dependency>

<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>18.0.1</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
<version>2.9.0</version>
</dependency>

<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.7.02</version>
<version>3.13.8</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<version>1.7.36</version>
</dependency>


Expand All @@ -202,73 +209,73 @@
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
<version>18.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>11-ea+24</version>
<version>18.0.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/dnsjava/dnsjava -->
<dependency>
<groupId>dnsjava</groupId>
<artifactId>dnsjava</artifactId>
<version>3.3.0</version>
<version>3.5.1</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.1</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.19.0</version>
<version>3.22.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.11.0</version>
<version>4.5.1</version>
<scope>test</scope>
</dependency>

<!-- Allow JavaFx to load without installing it, seems to fix issues running on Linux -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
<version>18.0.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
<version>18.0.1</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
<version>18.0.1</version>
<classifier>linux</classifier>
</dependency>
<dependency>
Expand Down
97 changes: 74 additions & 23 deletions src/main/java/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
package config;

import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import game.data.WorldManager;
import game.data.registries.RegistryLoader;
import game.data.registries.RegistryManager;
import game.protocol.Protocol;
import game.protocol.ProtocolVersionHandler;
import gui.GuiManager;
import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.function.Consumer;

import game.data.registries.RegistryManager;
import javafx.application.Platform;
import org.apache.commons.lang3.SystemUtils;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;

import game.data.WorldManager;
import game.data.registries.RegistryLoader;
import game.protocol.Protocol;
import game.protocol.ProtocolVersionHandler;
import gui.GuiManager;
import packets.builder.PacketBuilder;
import proxy.ConnectionDetails;
import proxy.ConnectionManager;
import proxy.auth.AuthDetails;
import proxy.auth.AuthenticationMethod;
import proxy.auth.MicrosoftAuthHandler;
import util.LocalDateTimeAdapter;
import util.PathUtils;

public class Config {
Expand All @@ -50,6 +51,10 @@ public class Config {
private transient boolean debugTrackEvents = false;
private transient VersionReporter versionReporter;

private MicrosoftAuthHandler microsoftAuth;
private AuthDetails manualAuth;
private AuthenticationMethod authMethod = AuthenticationMethod.AUTOMATIC;

public Config() {
this.versionReporter = new VersionReporter(0);
}
Expand All @@ -65,7 +70,10 @@ private static Config createConfig() {
try {
File file = configPath.toFile();
if (file.exists() && file.isFile()) {
return new Gson().fromJson(new JsonReader(new FileReader(file)), Config.class);
return new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
.create()
.fromJson(new JsonReader(new FileReader(file)), Config.class);
}
} catch (Exception ex) {
System.out.println("Cannot read " + configPath.toString());
Expand Down Expand Up @@ -147,20 +155,30 @@ public static String getUsername() {
return instance.username;
}

public static void handleErrorOutput() {
instance.handleGuiOnlyMode();
}

public boolean startWithSettings() {
return guiOnlyMode;
}


public void settingsComplete() {
GuiManager.setConfig(this);

if (guiOnlyMode && !GuiManager.isStarted()) {
handleGuiOnlyMode();

GuiManager.loadSceneSettings();
return;
}

// auth
boolean hasAccessToken = this.accessToken != null && !this.accessToken.equals("");
if (hasAccessToken) {
this.manualAuth = AuthDetails.fromAccessToken(accessToken);
this.authMethod = AuthenticationMethod.MANUAL;
}

// round to regions
centerX = (centerX >> 9) << 9;
centerZ = (centerZ >> 9) << 9;
Expand Down Expand Up @@ -188,14 +206,30 @@ public void settingsComplete() {

private void writeSettings() {
try {
String contents = new GsonBuilder().setPrettyPrinting().create().toJson(this);
// clear other auth settings
switch (authMethod) {
case AUTOMATIC -> {
manualAuth = null;
microsoftAuth = null;
}
case MICROSOFT -> manualAuth = null;
case MANUAL -> microsoftAuth = null;
}

String contents = new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
.setPrettyPrinting().create().toJson(this);
Files.createDirectories(configPath.getParent());
Files.write(configPath, Collections.singleton(contents));
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static void save() {
instance.writeSettings();
}

public static void clearSettings() {
try {
if (configPath.toFile().exists()) {
Expand All @@ -216,7 +250,7 @@ private void handleGuiOnlyMode() {
System.out.println("Application seems to be running without console. Redirecting error output to GUI. " +
"If this is not desired, run with --force-console.");

GuiManager.redirectErrorOutput();
Platform.runLater(GuiManager::redirectErrorOutput);
}
}

Expand Down Expand Up @@ -305,11 +339,11 @@ public static Consumer<PacketBuilder> getPacketInjector() {

@Option(name = "--token", aliases = "-t",
usage = "Minecraft access token. Found in launcher_accounts.json by default.")
public String accessToken;
public transient String accessToken;

@Option(name = "--username", aliases = "-u",
usage = "Your Minecraft username.")
public String username;
public transient String username;

@Option(name = "--port", aliases = "-p",
usage = "The port on which the remote server runs.")
Expand Down Expand Up @@ -393,7 +427,6 @@ public static Consumer<PacketBuilder> getPacketInjector() {

@Option(name = "--disable-messages",
usage = "Disable various info messages (e.g. chest saving).")

public boolean disableInfoMessages = false;

// getters
Expand Down Expand Up @@ -435,10 +468,12 @@ public static VersionReporter versionReporter() {
return instance.versionReporter;
}



public static AuthDetails getManualAuthDetails() {
return AuthDetails.fromUsername(instance.username, instance.accessToken);
return instance.manualAuth;
}

public static void setManualAuthDetails(AuthDetails details) {
instance.manualAuth = details;
}

// inverted boolean getters
Expand All @@ -460,5 +495,21 @@ public static boolean handleBlockChanges() {
public static void setZoomLevel(int val) {
instance.zoomLevel = val;
}

public static MicrosoftAuthHandler getMicrosoftAuth() {
return instance.microsoftAuth;
}

public static void setMicrosoftAuth(MicrosoftAuthHandler microsoftAuth) {
instance.microsoftAuth = microsoftAuth;
}

public static AuthenticationMethod getAuthMethod() {
return instance.authMethod;
}

public static void setAuthMethod(AuthenticationMethod authMethod) {
instance.authMethod = authMethod;
}
}

4 changes: 2 additions & 2 deletions src/main/java/game/data/RenderDistanceExtender.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public void setServerReportedRenderDistance(int serverDistance) {
return;
}

if (serverDistance >= 28) {
if (serverDistance >= 28 && Config.getExtendedRenderDistance() > 0) {
System.err.println("Server seems to be running at abnormally high render distance of " + serverDistance
+ ". Run with --measure-render-distance to ignore this value.");
+ ". Enable 'measure render distance' to ignored reported value.");
}

this.setServerDistance(serverDistance);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/game/data/registries/RegistryLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package game.data.registries;

import gui.GuiManager;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -106,7 +107,10 @@ private static int getJavaVersion() {
* helpful messages as well to put the user at ease about the delay.
*/
private void getReportsFromServerJar() throws IOException, InterruptedException {
System.out.println("Looks like we have not run in version " + version + " before.");
GuiManager.setStatusMessage("Running version " + version + " for the first time." +
" Generating reports... (this may take a few minutes)");

System.out.println("Generating reports for version " + version + ".");

String serverUrl = VersionManifestHandler.findServerUrl(version);

Expand All @@ -124,6 +128,8 @@ private void getReportsFromServerJar() throws IOException, InterruptedException
generateReports();
moveReports();
clean();

GuiManager.setStatusMessage("");
}

/**
Expand Down
Loading

0 comments on commit bfb283a

Please sign in to comment.