Update tweaker login to use authlib.
This commit is contained in:
parent
d4d562c1ca
commit
7cd70b3f26
1 changed files with 21 additions and 99 deletions
|
@ -6,127 +6,49 @@ import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.Proxy;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
|
||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
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.
|
* Basic implementation of Mojang's 'Yggdrasil' login system, purely intended as a dev time bare bones login.
|
||||||
* Login errors are not handled.
|
* Login errors are not handled.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
public class Yggdrasil
|
||||||
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<Property> 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 static void login(Map<String, String> args)
|
public static void login(Map<String, String> args)
|
||||||
{
|
{
|
||||||
if (!args.containsKey("--username") || !args.containsKey("--password")) return;
|
if (!args.containsKey("--username") || !args.containsKey("--password")) return;
|
||||||
|
YggdrasilUserAuthentication auth = (YggdrasilUserAuthentication) new YggdrasilAuthenticationService(Proxy.NO_PROXY, "1").createUserAuthentication(Agent.MINECRAFT);
|
||||||
String username = args.get("--username");
|
auth.setUsername(args.get("--username"));
|
||||||
String password = args.remove("--password");
|
auth.setPassword(args.remove("--password"));
|
||||||
|
|
||||||
Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
|
||||||
String request = GSON.toJson(new Request(username, password));
|
|
||||||
|
|
||||||
OutputStream out = null;
|
|
||||||
InputStream in = null;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
HttpURLConnection con = (HttpURLConnection)(new URL("https://authserver.mojang.com/authenticate")).openConnection();
|
auth.logIn();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (MalformedURLException e)
|
catch (AuthenticationException e)
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
LogManager.getLogger("FMLTWEAK").error("-- Login failed! " + e.getMessage());
|
||||||
}
|
Throwables.propagate(e);
|
||||||
catch (IOException e)
|
return; // dont set other variables
|
||||||
{
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
close(out);
|
|
||||||
close(in);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue