cleaning codes.

This commit is contained in:
huanghongxun
2015-11-11 22:07:01 +08:00
parent b65be375e7
commit f508645496
7 changed files with 70 additions and 140 deletions

View File

@@ -46,7 +46,6 @@ public final class OfflineAuthenticator extends IAuthenticator {
public static String getUUIDFromUserName(String str) { public static String getUUIDFromUserName(String str) {
return DigestUtils.md5Hex(str); return DigestUtils.md5Hex(str);
//return md5.substring(0, 8) + '-' + md5.substring(8, 12) + '-' + md5.substring(12, 16) + '-' + md5.substring(16, 21) + md5.substring(21);
} }
@Override @Override

View File

@@ -26,8 +26,7 @@ import org.jackhuang.hellominecraft.utils.ArrayUtils;
import org.jackhuang.hellominecraft.views.Selector; import org.jackhuang.hellominecraft.views.Selector;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilAuthenticationService; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilAuthentication;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilUserAuthentication;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.UUIDTypeAdapter; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.UUIDTypeAdapter;
/** /**
@@ -36,13 +35,11 @@ import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.UUIDTypeAdapte
*/ */
public final class YggdrasilAuthenticator extends IAuthenticator { public final class YggdrasilAuthenticator extends IAuthenticator {
YggdrasilAuthenticationService service; YggdrasilAuthentication ua;
YggdrasilUserAuthentication ua;
public YggdrasilAuthenticator(String clientToken) { public YggdrasilAuthenticator(String clientToken) {
super(clientToken); super(clientToken);
service = new YggdrasilAuthenticationService(Proxy.NO_PROXY, clientToken); ua = new YggdrasilAuthentication(Proxy.NO_PROXY, clientToken);
ua = (YggdrasilUserAuthentication) service.createUserAuthentication();
} }
@Override @Override

View File

@@ -1,12 +1,17 @@
package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil; package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import java.io.IOException;
import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import org.jackhuang.hellominecraft.C; import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.HMCLog; import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.utils.ArrayUtils;
import org.jackhuang.hellominecraft.utils.NetUtils; import org.jackhuang.hellominecraft.utils.NetUtils;
import org.jackhuang.hellominecraft.utils.StrUtils; import org.jackhuang.hellominecraft.utils.StrUtils;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap;
@@ -15,7 +20,17 @@ import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.request.Refres
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.Response; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.Response;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.User; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.User;
public class YggdrasilUserAuthentication { public class YggdrasilAuthentication {
public static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(GameProfile.class, new GameProfile.GameProfileSerializer())
.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer())
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
protected static final String BASE_URL = "https://authserver.mojang.com/";
protected static final URL ROUTE_AUTHENTICATE = NetUtils.constantURL(BASE_URL + "authenticate");
protected static final URL ROUTE_REFRESH = NetUtils.constantURL(BASE_URL + "refresh");
protected static final String STORAGE_KEY_ACCESS_TOKEN = "accessToken";
protected static final String STORAGE_KEY_PROFILE_NAME = "displayName"; protected static final String STORAGE_KEY_PROFILE_NAME = "displayName";
protected static final String STORAGE_KEY_PROFILE_ID = "uuid"; protected static final String STORAGE_KEY_PROFILE_ID = "uuid";
@@ -23,13 +38,25 @@ public class YggdrasilUserAuthentication {
protected static final String STORAGE_KEY_USER_NAME = "username"; protected static final String STORAGE_KEY_USER_NAME = "username";
protected static final String STORAGE_KEY_USER_ID = "userid"; protected static final String STORAGE_KEY_USER_ID = "userid";
protected static final String STORAGE_KEY_USER_PROPERTIES = "userProperties"; protected static final String STORAGE_KEY_USER_PROPERTIES = "userProperties";
private final YggdrasilAuthenticationService authenticationService;
private final Proxy proxy;
private final String clientToken;
private final PropertyMap userProperties = new PropertyMap(); private final PropertyMap userProperties = new PropertyMap();
private String userid; private String userid;
private String username; private String username;
private String password; private String password;
private GameProfile selectedProfile; private GameProfile selectedProfile;
private GameProfile[] profiles;
private String accessToken;
private boolean isOnline;
public YggdrasilAuthentication(Proxy proxy, String clientToken) {
this.proxy = proxy;
this.clientToken = clientToken;
}
public void setUsername(String username) { public void setUsername(String username) {
if ((isLoggedIn()) && (canPlayOnline())) if ((isLoggedIn()) && (canPlayOnline()))
throw new IllegalStateException("Cannot change username whilst logged in & online"); throw new IllegalStateException("Cannot change username whilst logged in & online");
@@ -60,10 +87,6 @@ public class YggdrasilUserAuthentication {
return this.selectedProfile; return this.selectedProfile;
} }
public YggdrasilAuthenticationService getAuthenticationService() {
return this.authenticationService;
}
public String getUserID() { public String getUserID() {
return this.userid; return this.userid;
} }
@@ -85,16 +108,29 @@ public class YggdrasilUserAuthentication {
this.userid = userid; this.userid = userid;
} }
private static final String BASE_URL = "https://authserver.mojang.com/"; public Proxy getProxy() {
private static final URL ROUTE_AUTHENTICATE = NetUtils.constantURL(BASE_URL + "authenticate"); return this.proxy;
private static final URL ROUTE_REFRESH = NetUtils.constantURL(BASE_URL + "refresh"); }
private static final String STORAGE_KEY_ACCESS_TOKEN = "accessToken";
private GameProfile[] profiles;
private String accessToken;
private boolean isOnline;
public YggdrasilUserAuthentication(YggdrasilAuthenticationService authenticationService) { protected Response makeRequest(URL url, Object input) throws AuthenticationException {
this.authenticationService = authenticationService; try {
String jsonResult = input == null ? NetUtils.doGet(url) : NetUtils.post(url, GSON.toJson(input), "application/json", proxy);
Response result = (Response) GSON.fromJson(jsonResult, Response.class);
if (result == null)
return null;
if (StrUtils.isNotBlank(result.error))
throw new AuthenticationException("InvalidCredentials " + result.errorMessage);
return result;
} catch (IOException | IllegalStateException | JsonParseException e) {
throw new AuthenticationException(C.i18n("login.failed.connect_authentication_server"), e);
}
}
public String getClientToken() {
return this.clientToken;
} }
public boolean canLogIn() { public boolean canLogIn() {
@@ -121,10 +157,10 @@ public class YggdrasilUserAuthentication {
HMCLog.log("Logging in with username & password"); HMCLog.log("Logging in with username & password");
AuthenticationRequest request = new AuthenticationRequest(this, getUsername(), getPassword()); AuthenticationRequest request = new AuthenticationRequest(clientToken, getUsername(), getPassword());
Response response = (Response) getAuthenticationService().makeRequest(ROUTE_AUTHENTICATE, request, Response.class); Response response = makeRequest(ROUTE_AUTHENTICATE, request);
if (!response.clientToken.equals(getAuthenticationService().getClientToken())) if (!response.clientToken.equals(clientToken))
throw new AuthenticationException(C.i18n("login.changed_client_token")); throw new AuthenticationException(C.i18n("login.changed_client_token"));
User user = response.user; User user = response.user;
@@ -161,9 +197,9 @@ public class YggdrasilUserAuthentication {
HMCLog.log("Logging in with access token"); HMCLog.log("Logging in with access token");
RefreshRequest request = new RefreshRequest(this); RefreshRequest request = new RefreshRequest(this);
Response response = (Response) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, Response.class); Response response = makeRequest(ROUTE_REFRESH, request);
if (!response.clientToken.equals(getAuthenticationService().getClientToken())) if (!response.clientToken.equals(clientToken))
throw new AuthenticationException(C.i18n("login.changed_client_token")); throw new AuthenticationException(C.i18n("login.changed_client_token"));
setUserid(response.user != null && response.user.id != null ? response.user.id : getUsername()); setUserid(response.user != null && response.user.id != null ? response.user.id : getUsername());
@@ -200,25 +236,6 @@ public class YggdrasilUserAuthentication {
return isLoggedIn() && getSelectedProfile() != null && this.isOnline; return isLoggedIn() && getSelectedProfile() != null && this.isOnline;
} }
public void selectGameProfile(GameProfile profile) throws AuthenticationException {
if (!isLoggedIn())
throw new AuthenticationException(C.i18n("login.profile.not_logged_in"));
if (getSelectedProfile() != null)
throw new AuthenticationException(C.i18n("login.profile.selected"));
if (profile == null || !ArrayUtils.contains(this.profiles, profile))
throw new IllegalArgumentException("Invalid profile '" + profile + "'");
RefreshRequest request = new RefreshRequest(this, profile);
Response response = (Response) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, Response.class);
if (!response.clientToken.equals(getAuthenticationService().getClientToken()))
throw new AuthenticationException(C.i18n("login.changed_client_token"));
this.isOnline = true;
this.accessToken = response.accessToken;
setSelectedProfile(response.selectedProfile);
}
public void loadFromStorage(Map<String, Object> credentials) { public void loadFromStorage(Map<String, Object> credentials) {
logOut(); logOut();
@@ -243,14 +260,12 @@ public class YggdrasilUserAuthentication {
} }
public Map<String, Object> saveForStorage() { public Map<String, Object> saveForStorage() {
Map result = new HashMap(); Map<String, Object> result = new HashMap<>();
if (getUsername() != null) if (getUsername() != null)
result.put(STORAGE_KEY_USER_NAME, getUsername()); result.put(STORAGE_KEY_USER_NAME, getUsername());
if (getUserID() != null) if (getUserID() != null)
result.put(STORAGE_KEY_USER_ID, getUserID()); result.put(STORAGE_KEY_USER_ID, getUserID());
else if (getUsername() != null)
result.put(STORAGE_KEY_USER_NAME, getUsername());
if (!getUserProperties().isEmpty()) if (!getUserProperties().isEmpty())
result.put(STORAGE_KEY_USER_PROPERTIES, getUserProperties().list()); result.put(STORAGE_KEY_USER_PROPERTIES, getUserProperties().list());
@@ -264,7 +279,7 @@ public class YggdrasilUserAuthentication {
} }
if (StrUtils.isNotBlank(getAuthenticatedToken())) if (StrUtils.isNotBlank(getAuthenticatedToken()))
result.put("accessToken", getAuthenticatedToken()); result.put(STORAGE_KEY_ACCESS_TOKEN, getAuthenticatedToken());
return result; return result;
} }

View File

@@ -1,61 +0,0 @@
package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap;
import java.io.IOException;
import java.net.Proxy;
import java.net.URL;
import java.util.UUID;
import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile.GameProfileSerializer;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.Response;
import org.jackhuang.hellominecraft.utils.NetUtils;
import org.jackhuang.hellominecraft.utils.StrUtils;
public class YggdrasilAuthenticationService {
private final Proxy proxy;
private final String clientToken;
private final Gson gson;
public YggdrasilAuthenticationService(Proxy proxy, String clientToken) {
this.proxy = proxy;
this.clientToken = clientToken;
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(GameProfile.class, new GameProfileSerializer());
builder.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer());
builder.registerTypeAdapter(UUID.class, new UUIDTypeAdapter());
this.gson = builder.create();
}
public YggdrasilUserAuthentication createUserAuthentication() {
return new YggdrasilUserAuthentication(this);
}
public Proxy getProxy() {
return this.proxy;
}
protected <T extends Response> T makeRequest(URL url, Object input, Class<T> classOfT) throws AuthenticationException {
try {
String jsonResult = input == null ? NetUtils.doGet(url) : NetUtils.post(url, this.gson.toJson(input), "application/json", proxy);
Response result = (Response) this.gson.fromJson(jsonResult, classOfT);
if (result == null)
return null;
if (StrUtils.isNotBlank(result.error))
throw new AuthenticationException("InvalidCredentials " + result.errorMessage);
return (T) result;
} catch (IOException | IllegalStateException | JsonParseException e) {
throw new AuthenticationException(C.i18n("login.failed.connect_authentication_server"), e);
}
}
public String getClientToken() {
return this.clientToken;
}
}

View File

@@ -1,12 +1,5 @@
package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties; package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import org.jackhuang.hellominecraft.utils.code.Base64;
public class Property { public class Property {
public final String name; public final String name;
@@ -22,16 +15,4 @@ public class Property {
this.value = value; this.value = value;
this.signature = signature; this.signature = signature;
} }
public boolean isSignatureValid(PublicKey publicKey) {
try {
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initVerify(publicKey);
sign.update(this.value.getBytes());
return sign.verify(Base64.decode(this.signature.toCharArray()));
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
e.printStackTrace();
}
return false;
}
} }

View File

@@ -1,7 +1,6 @@
package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.request; package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.request;
import java.util.HashMap; import java.util.HashMap;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilUserAuthentication;
public class AuthenticationRequest { public class AuthenticationRequest {
@@ -11,13 +10,13 @@ public class AuthenticationRequest {
public String clientToken; public String clientToken;
public boolean requestUser = true; public boolean requestUser = true;
public AuthenticationRequest(YggdrasilUserAuthentication authenticationService, String username, String password) { public AuthenticationRequest(String username, String password, String clientToken) {
agent = new HashMap<>(); agent = new HashMap<>();
agent.put("name", "Minecraft"); agent.put("name", "Minecraft");
agent.put("version", 1); agent.put("version", 1);
this.username = username; this.username = username;
this.clientToken = authenticationService.getAuthenticationService().getClientToken();
this.password = password; this.password = password;
this.clientToken = clientToken;
} }
} }

View File

@@ -1,7 +1,7 @@
package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.request; package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.request;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilUserAuthentication; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilAuthentication;
public class RefreshRequest { public class RefreshRequest {
@@ -10,13 +10,13 @@ public class RefreshRequest {
public GameProfile selectedProfile; public GameProfile selectedProfile;
public boolean requestUser = true; public boolean requestUser = true;
public RefreshRequest(YggdrasilUserAuthentication authenticationService) { public RefreshRequest(YggdrasilAuthentication userAuth) {
this(authenticationService, null); this(userAuth, null);
} }
public RefreshRequest(YggdrasilUserAuthentication authenticationService, GameProfile profile) { public RefreshRequest(YggdrasilAuthentication userAuth, GameProfile profile) {
this.clientToken = authenticationService.getAuthenticationService().getClientToken(); this.clientToken = userAuth.getClientToken();
this.accessToken = authenticationService.getAuthenticatedToken(); this.accessToken = userAuth.getAuthenticatedToken();
this.selectedProfile = profile; this.selectedProfile = profile;
} }
} }