From f508645496b01a6d5fa54cf9901b3ff179239242 Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Wed, 11 Nov 2015 22:07:01 +0800 Subject: [PATCH] cleaning codes. --- .../utils/auth/OfflineAuthenticator.java | 1 - .../utils/auth/YggdrasilAuthenticator.java | 9 +- ...tion.java => YggdrasilAuthentication.java} | 103 ++++++++++-------- .../YggdrasilAuthenticationService.java | 61 ----------- .../auth/yggdrasil/properties/Property.java | 19 ---- .../request/AuthenticationRequest.java | 5 +- .../yggdrasil/request/RefreshRequest.java | 12 +- 7 files changed, 70 insertions(+), 140 deletions(-) rename HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/{YggdrasilUserAuthentication.java => YggdrasilAuthentication.java} (78%) delete mode 100644 HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthenticationService.java diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/OfflineAuthenticator.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/OfflineAuthenticator.java index 861875efc..77a572f2a 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/OfflineAuthenticator.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/OfflineAuthenticator.java @@ -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 diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/YggdrasilAuthenticator.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/YggdrasilAuthenticator.java index 74e9b1216..6a7fcfd11 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/YggdrasilAuthenticator.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/YggdrasilAuthenticator.java @@ -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 diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilUserAuthentication.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthentication.java similarity index 78% rename from HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilUserAuthentication.java rename to HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthentication.java index c42a2ce8e..4f377b2c9 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilUserAuthentication.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthentication.java @@ -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 credentials) { logOut(); @@ -243,14 +260,12 @@ public class YggdrasilUserAuthentication { } public Map saveForStorage() { - Map result = new HashMap(); + Map 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; } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthenticationService.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthenticationService.java deleted file mode 100644 index 33a7a2d07..000000000 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthenticationService.java +++ /dev/null @@ -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 makeRequest(URL url, Object input, Class 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; - } -} diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/Property.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/Property.java index 7b31d26d2..856f44c92 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/Property.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/Property.java @@ -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; - } } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/request/AuthenticationRequest.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/request/AuthenticationRequest.java index 6713a46d7..92071e0de 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/request/AuthenticationRequest.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/request/AuthenticationRequest.java @@ -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; } } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/request/RefreshRequest.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/request/RefreshRequest.java index 31d437fdd..16eda3018 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/request/RefreshRequest.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/request/RefreshRequest.java @@ -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; } }