From ea9d543d32575a4ded6eb83a92e65a21250714b9 Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Tue, 8 Mar 2022 01:03:49 +0000 Subject: [PATCH] Remove startup session swapping from unknit --- .../unknit/auth/GameAuthenticationFlow.java | 105 ------------------ .../unknit/mixin/MixinMinecraftMain.java | 17 --- 2 files changed, 122 deletions(-) delete mode 100644 src/main/java/site/hackery/unknit/auth/GameAuthenticationFlow.java diff --git a/src/main/java/site/hackery/unknit/auth/GameAuthenticationFlow.java b/src/main/java/site/hackery/unknit/auth/GameAuthenticationFlow.java deleted file mode 100644 index 19b9184..0000000 --- a/src/main/java/site/hackery/unknit/auth/GameAuthenticationFlow.java +++ /dev/null @@ -1,105 +0,0 @@ -package site.hackery.unknit.auth; - -import net.hycrafthd.minecraft_authenticator.login.AuthenticationException; -import net.hycrafthd.minecraft_authenticator.login.Authenticator; -import net.hycrafthd.minecraft_authenticator.login.User; -import net.hycrafthd.minecraft_authenticator.login.file.AuthenticationFile; -import net.minecraft.client.util.Session; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Optional; - -public class GameAuthenticationFlow { - /** - * Creates an authenticated Minecraft session from given environment variables. - * AUTH_TYPE may be "mojang", "microsoft", or "refresh" (default). - * Under "mojang" authentication, you must supply the "AUTH_USERNAME" and "AUTH_PASSWORD" variables. - * Under "microsoft" authentication, you must supply the "AUTH_TOKEN" variable. - * - * @return a valid Session, or null - */ - public static Session createSessionFromEnvironment() { - try { - Authenticator.Builder readyAuth = createAuthenticator(); - if (readyAuth != null) { - Authenticator authenticator = readyAuth.run(); - Optional userOpt = authenticator.getUser(); - if (userOpt.isEmpty()) { - return null; - } - saveAuthData(authenticator.getResultFile()); - - User user = userOpt.get(); - return new Session(user.getName(), user.getUuid(), user.getAccessToken(), Optional.empty(), Optional.empty(), Session.AccountType.byName(user.getType())); - } - } catch (AuthenticationException | IOException ignored) { - } - - return null; - } - - private static Path getAuthDataFile() { - String authFilePath = System.getenv("AUTH_DATA_FILE"); - authFilePath = authFilePath == null ? "mc_auth.dat" : authFilePath; - return Paths.get(authFilePath); - } - - private static Authenticator.Builder createAuthenticator() throws IOException { - String authType = System.getenv("AUTH_TYPE"); - authType = authType == null ? "refresh" : authType; - - switch (authType) { - case "mojang": - return createMojangAuthenticator(); - case "microsoft": - return createMicrosoftAuthenticator(); - case "refresh": - return createRefreshAuthenticator(); - } - - return null; - } - - private static Authenticator.Builder createMojangAuthenticator() { - String username = System.getenv("AUTH_USERNAME"); - String password = System.getenv("AUTH_PASSWORD"); - - if (username == null || password == null) { - throw new RuntimeException("Attempted to perform Mojang authentication without AUTH_USERNAME or AUTH_PASSWORD set."); - } - - return Authenticator.ofYggdrasil("-", username, password).shouldAuthenticate(); - } - - private static Authenticator.Builder createMicrosoftAuthenticator() { - String token = System.getenv("AUTH_TOKEN"); - if (token == null) { - throw new RuntimeException("Attempted to perform Microsoft authentication without AUTH_TOKEN set. You may want to visit " + Authenticator.microsoftLogin()); - } - - return Authenticator.ofMicrosoft(token).shouldAuthenticate(); - } - - private static Authenticator.Builder createRefreshAuthenticator() throws IOException { - Path authData = getAuthDataFile(); - if (!Files.exists(authData)) { - return null; - } - - try (InputStream inputStream = Files.newInputStream(authData)) { - AuthenticationFile file = AuthenticationFile.read(inputStream); - return Authenticator.of(file).shouldAuthenticate(); - } - } - - private static void saveAuthData(AuthenticationFile file) throws IOException { - try (OutputStream stream = Files.newOutputStream(getAuthDataFile())) { - file.write(stream); - } - } -} diff --git a/src/main/java/site/hackery/unknit/mixin/MixinMinecraftMain.java b/src/main/java/site/hackery/unknit/mixin/MixinMinecraftMain.java index 7351bf9..bb9375a 100644 --- a/src/main/java/site/hackery/unknit/mixin/MixinMinecraftMain.java +++ b/src/main/java/site/hackery/unknit/mixin/MixinMinecraftMain.java @@ -2,11 +2,9 @@ package site.hackery.unknit.mixin; import net.minecraft.client.main.Main; -import net.minecraft.client.util.Session; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.ModifyVariable; -import site.hackery.unknit.auth.GameAuthenticationFlow; import java.util.ArrayList; import java.util.Arrays; @@ -26,26 +24,11 @@ public abstract class MixinMinecraftMain { replace(arguments, "--versionType", "release"); } - private static void replaceSession(List arguments, Session session) { - replace(arguments, "--accessToken", session.getAccessToken()); - - arguments.add("--username"); - arguments.add(session.getUsername()); - - arguments.add("--uuid"); - arguments.add(session.getUuid()); - } - @ModifyVariable(method = "main", at = @At("HEAD"), argsOnly = true, remap = false) private static String[] setArgs(String[] args) { List arguments = new ArrayList<>(Arrays.asList(args)); replaceVersion(arguments); - Session session = GameAuthenticationFlow.createSessionFromEnvironment(); - if (session != null) { - replaceSession(arguments, session); - } - return arguments.toArray(new String[0]); } }