Skip to content

Commit

Permalink
Added possibility to logout the session
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsik68 committed Dec 30, 2013
1 parent 404577e commit 58fb64b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/sk/tomsik68/mclauncher/api/common/MCLauncherAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static final class URLS {

public static final String PASSWORD_LOGIN_URL = "https://authserver.mojang.com/authenticate";
public static final String SESSION_LOGIN_URL = "https://authserver.mojang.com/refresh";
public static final String SESSION_LOGOUT_URL = "https://authserver.mojang.com/invalidate";

public static final String LWJGL_DOWNLOAD_URL = "http://kent.dl.sourceforge.net/project/java-game-lib/Official%20Releases/LWJGL%202.9.0/lwjgl-2.9.0.zip";
public static final String RESOURCES_DOWNLOAD_URL = "http://s3.amazonaws.com/MinecraftResources/";
Expand Down
2 changes: 2 additions & 0 deletions src/sk/tomsik68/mclauncher/api/login/ILoginService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

public interface ILoginService extends IOnlineService {
public ISession login(IProfile profile) throws Exception;

public void logout(ISession session) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public ISession login(IProfile profile) throws Exception {
return factory.createSession(loginResponse.split(":"));
}

@Override
public void logout(ISession session) throws Exception {
// nothing happens, can't log out.
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,24 @@

public class YDLoginService implements ILoginService {
private static UUID clientToken = UUID.randomUUID();


public YDLoginService() {
}

@Override
public ISession login(IProfile profile) throws Exception {
YDLoginResponse response;
if (profile instanceof LegacyProfile) {
if (profile instanceof LegacyProfile)
response = doPasswordLogin(profile);
} else
else {
response = doSessionLogin(profile);
return new YDSession(response);
if (profile instanceof YDAuthProfile)
((YDAuthProfile) profile).setPassword(response.getSessionID());
}

YDSession result = new YDSession(response);

return result;
}

private YDLoginResponse doSessionLogin(IProfile profile) throws Exception {
Expand Down Expand Up @@ -72,7 +77,19 @@ public void load(File mcInstance) throws Exception {
if (file.exists()) {
JSONObject obj = (JSONObject) JSONValue.parse(new FileReader(file));
clientToken = UUID.fromString(obj.get("clientToken").toString());
System.out.println("Loaded client token: " + clientToken.toString());
MCLauncherAPI.log.info("Loaded client token: " + clientToken.toString());
}
}

@Override
public void logout(ISession session) throws Exception {
YDLogoutRequest request = new YDLogoutRequest(session, clientToken);
String result = HttpUtils.doJSONPost(MCLauncherAPI.URLS.SESSION_LOGOUT_URL, request);
if (result.length() > 0) {
YDResponse response = new YDResponse((JSONObject) JSONValue.parse(result));
if (response.getError() != null) {
throw new RuntimeException(response.getError() + " " + response.getMessage());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sk.tomsik68.mclauncher.impl.login.yggdrasil;

import java.util.UUID;

import net.minidev.json.JSONObject;
import sk.tomsik68.mclauncher.api.json.IJSONSerializable;
import sk.tomsik68.mclauncher.api.login.ISession;

public class YDLogoutRequest implements IJSONSerializable {
private final ISession session;
private final UUID clientToken;

public YDLogoutRequest(ISession session, UUID clientToken) {
this.session = session;
this.clientToken = clientToken;
}

@Override
public JSONObject toJSON() {
JSONObject jsonObj = new JSONObject();
jsonObj.put("accessToken", session.getSessionID());
jsonObj.put("clientToken", clientToken.toString());
return jsonObj;
}

}
26 changes: 17 additions & 9 deletions test/TestLogin.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import javax.swing.JOptionPane;

import org.junit.Test;

import sk.tomsik68.mclauncher.api.login.IProfile;
import sk.tomsik68.mclauncher.api.login.ISession;
import sk.tomsik68.mclauncher.impl.login.legacy.LegacyLoginService;
import sk.tomsik68.mclauncher.impl.login.legacy.LegacyProfile;
import sk.tomsik68.mclauncher.impl.common.Platform;
import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDLoginService;
import sk.tomsik68.mclauncher.impl.login.yggdrasil.io.YDProfileIO;

public class TestLogin {

@Test
public void test() {
String password = JOptionPane.showInputDialog("Enter your password to try the login system");
/*String password = JOptionPane.showInputDialog("Enter your password to try the login system");
IProfile profile = new LegacyProfile("tomsik68@gmail.com", password);
LegacyLoginService lls = new LegacyLoginService();
try {
ISession session = lls.login(profile);
System.out.println("Legacy Login: " + session.getSessionID());
} catch (Exception e) {
e.printStackTrace();
}
YDLoginService yls = new YDLoginService();
ISession session;
}*/

try {
YDLoginService yls = new YDLoginService();
yls.load(Platform.getCurrentPlatform().getWorkingDirectory());

YDProfileIO profileIO = new YDProfileIO(Platform.getCurrentPlatform().getWorkingDirectory());
IProfile[] profiles = profileIO.read();
IProfile profile = profiles[0];

ISession session;
session = yls.login(profile);
System.out.println("YD Login: " + session.getSessionID());

profiles[0] = profile;
profileIO.write(profiles);

} catch (Exception e) {
e.printStackTrace();
}
Expand Down
8 changes: 6 additions & 2 deletions test/TestMCDownloadLaunch.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import sk.tomsik68.mclauncher.api.common.IObservable;
import sk.tomsik68.mclauncher.api.common.IObserver;
import sk.tomsik68.mclauncher.api.common.mc.IMinecraftInstance;
import sk.tomsik68.mclauncher.api.login.IProfile;
import sk.tomsik68.mclauncher.api.login.ISession;
import sk.tomsik68.mclauncher.api.versions.IVersion;
import sk.tomsik68.mclauncher.impl.common.Platform;
import sk.tomsik68.mclauncher.impl.common.mc.MinecraftInstance;
import sk.tomsik68.mclauncher.impl.login.legacy.LegacyProfile;
import sk.tomsik68.mclauncher.impl.login.yggdrasil.YDLoginService;
import sk.tomsik68.mclauncher.impl.login.yggdrasil.io.YDProfileIO;
import sk.tomsik68.mclauncher.impl.versions.mcdownload.MCDownloadVersionList;

public class TestMCDownloadLaunch {
Expand All @@ -36,7 +37,10 @@ public void onUpdate(IObservable<IVersion> observable, IVersion changed) {
// finally use my minecraft credentials
YDLoginService service = new YDLoginService();
service.load(Platform.getCurrentPlatform().getWorkingDirectory());
ISession session = service.login(new LegacyProfile("Tomsik68@gmail.com", "mypassword"));
YDProfileIO profileIO = new YDProfileIO(Platform.getCurrentPlatform().getWorkingDirectory());
IProfile[] profiles = profileIO.read();
ISession session = service.login(profiles[0]);


Process proc = changed.getLauncher().launch(session, mc, null, changed, new ILaunchSettings() {

Expand Down

0 comments on commit 58fb64b

Please sign in to comment.