Remove startup session swapping from unknit
parent
ca66d553bd
commit
ea9d543d32
|
@ -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<User> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<String> 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<String> arguments = new ArrayList<>(Arrays.asList(args));
|
||||
replaceVersion(arguments);
|
||||
|
||||
Session session = GameAuthenticationFlow.createSessionFromEnvironment();
|
||||
if (session != null) {
|
||||
replaceSession(arguments, session);
|
||||
}
|
||||
|
||||
return arguments.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue