Permalink
Browse files

Changed YggdrasilSession to use HttpRequest.

Added getAccessToken() to MinecraftSession.
  • Loading branch information...
sk89q committed Oct 18, 2013
1 parent f3ca920 commit 83bb2deb1ae3350c700e40452570271fc4ea5e2f
@@ -42,17 +42,17 @@
private static final String MINECRAFT_LOGIN_URL = "https://login.minecraft.net/";
private static final String LAUNCHER_VERSION = "13";
-
+
private String username;
private URL loginURL;
private boolean isValid;
private String latestVersion;
private String downloadTicket;
private String sessionId;
-
+
/**
* Construct the session.
- *
+ *
* @param username username
*/
public LegacySession(String username) {
@@ -63,22 +63,22 @@ public LegacySession(String username) {
throw new RuntimeException(e);
}
}
-
+
@Override
- public void login(String password) throws IOException,
+ public void login(String password) throws IOException,
OutdatedLauncherException, LoginException, UserNotPremiumException {
-
+
HttpsURLConnection conn = null;
-
+
String params = String.format("user=%s&password=%s&version=%s",
URLEncoder.encode(username, "UTF-8"),
URLEncoder.encode(password, "UTF-8"),
URLEncoder.encode(LAUNCHER_VERSION, "UTF-8"));
-
+
TrustManager[] trustManagers = new TrustManager[] {
Launcher.getInstance().getKeyRing().getKeyStore(Ring.MINECRAFT_LOGIN)
};
-
+
try {
conn = (HttpsURLConnection) loginURL.openConnection();
SSLContext ctx = SSLContext.getInstance("TLS");
@@ -96,16 +96,16 @@ public void login(String password) throws IOException,
conn.setReadTimeout(1000 * 60 * 10);
conn.connect();
-
+
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
-
+
if (conn.getResponseCode() != 200) {
throw new IOException("Did not get expected 200 code");
}
-
+
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder s = new StringBuilder();
@@ -115,10 +115,10 @@ public void login(String password) throws IOException,
s.append(buf, 0, len);
}
String result = s.toString();
-
+
if (result.contains(":")) {
String[] values = result.split(":");
-
+
try {
latestVersion = values[0].trim();
downloadTicket = values[1].trim();
@@ -128,7 +128,7 @@ public void login(String password) throws IOException,
throw new LoginException(
"Returned login payload had an incorrect number of arguments");
}
-
+
isValid = true;
} else {
if (result.trim().equals("Bad login")) {
@@ -150,12 +150,12 @@ public void login(String password) throws IOException,
conn = null;
}
}
-
+
@Override
public boolean isValid() {
return isValid;
}
-
+
@Override
public String getUsername() {
return username;
@@ -166,9 +166,14 @@ public String getSessionId() {
return sessionId;
}
+ @Override
+ public String getAccessToken() {
+ return null;
+ }
+
/**
* Get the latest version.
- *
+ *
* @return the latest version
*/
public String getLatestVersion() {
@@ -177,20 +182,20 @@ public String getLatestVersion() {
/**
* Get the download ticket, available once logged in.
- *
+ *
* @return download ticket
*/
public String getDownloadTicket() {
return downloadTicket;
}
-
+
/**
* Get the login URL being used.
- *
+ *
* @return url the URL
*/
public URL getLoginURL() {
return loginURL;
}
-
+
}
@@ -27,51 +27,60 @@
/**
* Attempt to perform a login.
- *
+ *
* @param password plain text password
- * @throws IOException throw on an IO error
- * @throws LoginException on a login exception
+ * @throws IOException throw on an IO error
+ * @throws LoginException on a login exception
* @throws OutdatedLauncherException thrown if the launcher is 'out of date'
- * @throws UserNotPremiumException thrown if the user is not premium
+ * @throws UserNotPremiumException thrown if the user is not premium
*/
void login(String password) throws IOException,
LoginException, OutdatedLauncherException, UserNotPremiumException;
/**
* Returns whether the session can be used in its current state.
- *
+ * <p/>
* <p>For authenticated sessions, this would return true if the login is successful.
* For offline sessions, they may always return true.</p>
- *
+ *
* @return true is valid
*/
boolean isValid();
/**
* Get the username. If a login was successful, this will be the correct
* form of the username.
- *
+ *
* @return username
*/
String getUsername();
/**
* Get a session ID.
- *
+ *
* @return session ID, or null
*/
String getSessionId();
+ /**
+ * Get an access token that will identify the user in place of the password.
+ * <p/>
+ * Access tokens may or may not be available.
+ *
+ * @return an access token, or null
+ */
+ String getAccessToken();
+
/**
* Thrown on a login error.
*/
public static class LoginException extends Exception {
private static final long serialVersionUID = 3704469434921739106L;
-
+
public LoginException(String message) {
super(message);
}
-
+
public LoginException(String message, Throwable t) {
super(message, t);
}
@@ -56,4 +56,9 @@ public String getSessionId() {
return null;
}
+ @Override
+ public String getAccessToken() {
+ return null;
+ }
+
}
Oops, something went wrong.

0 comments on commit 83bb2de

Please sign in to comment.