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) {
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

View File

@ -26,8 +26,7 @@ import org.jackhuang.hellominecraft.utils.ArrayUtils;
import org.jackhuang.hellominecraft.views.Selector;
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.YggdrasilAuthenticationService;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilUserAuthentication;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilAuthentication;
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 {
YggdrasilAuthenticationService service;
YggdrasilUserAuthentication ua;
YggdrasilAuthentication ua;
public YggdrasilAuthenticator(String clientToken) {
super(clientToken);
service = new YggdrasilAuthenticationService(Proxy.NO_PROXY, clientToken);
ua = (YggdrasilUserAuthentication) service.createUserAuthentication();
ua = new YggdrasilAuthentication(Proxy.NO_PROXY, clientToken);
}
@Override

View File

@ -1,12 +1,17 @@
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.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.utils.ArrayUtils;
import org.jackhuang.hellominecraft.utils.NetUtils;
import org.jackhuang.hellominecraft.utils.StrUtils;
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.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_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_ID = "userid";
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 String userid;
private String username;
private String password;
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) {
if ((isLoggedIn()) && (canPlayOnline()))
throw new IllegalStateException("Cannot change username whilst logged in & online");
@ -60,10 +87,6 @@ public class YggdrasilUserAuthentication {
return this.selectedProfile;
}
public YggdrasilAuthenticationService getAuthenticationService() {
return this.authenticationService;
}
public String getUserID() {
return this.userid;
}
@ -84,17 +107,30 @@ public class YggdrasilUserAuthentication {
protected void setUserid(String userid) {
this.userid = userid;
}
public Proxy getProxy() {
return this.proxy;
}
protected Response makeRequest(URL url, Object input) throws AuthenticationException {
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);
private static final String BASE_URL = "https://authserver.mojang.com/";
private static final URL ROUTE_AUTHENTICATE = NetUtils.constantURL(BASE_URL + "authenticate");
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;
if (result == null)
return null;
public YggdrasilUserAuthentication(YggdrasilAuthenticationService authenticationService) {
this.authenticationService = authenticationService;
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() {
@ -121,10 +157,10 @@ public class YggdrasilUserAuthentication {
HMCLog.log("Logging in with username & password");
AuthenticationRequest request = new AuthenticationRequest(this, getUsername(), getPassword());
Response response = (Response) getAuthenticationService().makeRequest(ROUTE_AUTHENTICATE, request, Response.class);
AuthenticationRequest request = new AuthenticationRequest(clientToken, getUsername(), getPassword());
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"));
User user = response.user;
@ -161,9 +197,9 @@ public class YggdrasilUserAuthentication {
HMCLog.log("Logging in with access token");
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"));
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;
}
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) {
logOut();
@ -243,14 +260,12 @@ public class YggdrasilUserAuthentication {
}
public Map<String, Object> saveForStorage() {
Map result = new HashMap();
Map<String, Object> result = new HashMap<>();
if (getUsername() != null)
result.put(STORAGE_KEY_USER_NAME, getUsername());
if (getUserID() != null)
result.put(STORAGE_KEY_USER_ID, getUserID());
else if (getUsername() != null)
result.put(STORAGE_KEY_USER_NAME, getUsername());
if (!getUserProperties().isEmpty())
result.put(STORAGE_KEY_USER_PROPERTIES, getUserProperties().list());
@ -264,7 +279,7 @@ public class YggdrasilUserAuthentication {
}
if (StrUtils.isNotBlank(getAuthenticatedToken()))
result.put("accessToken", getAuthenticatedToken());
result.put(STORAGE_KEY_ACCESS_TOKEN, getAuthenticatedToken());
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;
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 final String name;
@ -22,16 +15,4 @@ public class Property {
this.value = value;
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;
import java.util.HashMap;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilUserAuthentication;
public class AuthenticationRequest {
@ -11,13 +10,13 @@ public class AuthenticationRequest {
public String clientToken;
public boolean requestUser = true;
public AuthenticationRequest(YggdrasilUserAuthentication authenticationService, String username, String password) {
public AuthenticationRequest(String username, String password, String clientToken) {
agent = new HashMap<>();
agent.put("name", "Minecraft");
agent.put("version", 1);
this.username = username;
this.clientToken = authenticationService.getAuthenticationService().getClientToken();
this.password = password;
this.clientToken = clientToken;
}
}

View File

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