From 7cd70b3f2683bb31392095468afea444966db420 Mon Sep 17 00:00:00 2001 From: Lex Manos Date: Fri, 11 Jul 2014 17:52:43 -0700 Subject: [PATCH] Update tweaker login to use authlib. --- .../mods/fml/common/launcher/Yggdrasil.java | 120 +++--------------- 1 file changed, 21 insertions(+), 99 deletions(-) diff --git a/fml/src/main/java/cpw/mods/fml/common/launcher/Yggdrasil.java b/fml/src/main/java/cpw/mods/fml/common/launcher/Yggdrasil.java index a8975e36f..dc42c143f 100644 --- a/fml/src/main/java/cpw/mods/fml/common/launcher/Yggdrasil.java +++ b/fml/src/main/java/cpw/mods/fml/common/launcher/Yggdrasil.java @@ -6,127 +6,49 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.Proxy; import java.net.URL; import java.util.List; import java.util.Map; import org.apache.commons.io.IOUtils; +import org.apache.logging.log4j.LogManager; import com.google.common.base.Charsets; +import com.google.common.base.Throwables; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.mojang.authlib.Agent; +import com.mojang.authlib.exceptions.*; +import com.mojang.authlib.yggdrasil.*; /** * Basic implementation of Mojang's 'Yggdrasil' login system, purely intended as a dev time bare bones login. * Login errors are not handled. */ -@SuppressWarnings("unused") -public class Yggdrasil { - private static class Request { - Agent agent = new Agent(); - String username; - String password; - String clientToken = null; - boolean requestUser = true; - - public Request(String username, String password){ - this.username = username; - this.password = password; - } - - private static class Agent { - String name = "Minecraft"; - int version = 1; - } - } - - private static class Response { - String error; - String errorMessage; - String cause; - String accessToken; - String clientToken; - Profile selectedProfile; - Profile[] availableProfiles; - User user; - - private static class Profile { - String id; - String name; - boolean legacy; - } - - private static class User { - String id; - List properties; - - private static class Property { - String name; - String value; - } - } - } - - private static void close(Closeable c) - { - if (c != null) { - try { - c.close(); - } catch (IOException e){} - } - } - +public class Yggdrasil +{ public static void login(Map args) { if (!args.containsKey("--username") || !args.containsKey("--password")) return; - - String username = args.get("--username"); - String password = args.remove("--password"); - - Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - String request = GSON.toJson(new Request(username, password)); - - OutputStream out = null; - InputStream in = null; + YggdrasilUserAuthentication auth = (YggdrasilUserAuthentication) new YggdrasilAuthenticationService(Proxy.NO_PROXY, "1").createUserAuthentication(Agent.MINECRAFT); + auth.setUsername(args.get("--username")); + auth.setPassword(args.remove("--password")); try { - HttpURLConnection con = (HttpURLConnection)(new URL("https://authserver.mojang.com/authenticate")).openConnection(); - con.setConnectTimeout(15000); - con.setReadTimeout(15000); - con.setUseCaches(false); - - byte[] data = request.getBytes(Charsets.UTF_8); - - con.setRequestProperty("Content-Type", "application/json; charset=utf-8"); - con.setRequestProperty("Content-Length", Integer.toString(data.length)); - con.setDoOutput(true); - - out = con.getOutputStream(); - out.write(data); - - in = con.getInputStream(); - Response result = GSON.fromJson(IOUtils.toString(in, Charsets.UTF_8), Response.class); - - if (result.selectedProfile != null) - { - args.put("--username", result.selectedProfile.name); - args.put("--uuid", result.selectedProfile.id); - args.put("--accessToken", result.accessToken); - } + auth.logIn(); } - catch (MalformedURLException e) + catch (AuthenticationException e) { - e.printStackTrace(); - } - catch (IOException e) - { - e.printStackTrace(); - } - finally - { - close(out); - close(in); + LogManager.getLogger("FMLTWEAK").error("-- Login failed! " + e.getMessage()); + Throwables.propagate(e); + return; // dont set other variables } + + args.put("--username", auth.getSelectedProfile().getName()); + args.put("--uuid", auth.getSelectedProfile().getId().toString().replace("-", "")); + args.put("--accessToken", auth.getAuthenticatedToken()); + args.put("--userProperties", auth.getUserProperties().toString()); } }