clean code.

This commit is contained in:
huanghongxun 2015-10-05 21:55:30 +08:00
parent 1d252ea71c
commit b9c03d6ec3
10 changed files with 244 additions and 322 deletions

View File

@ -27,22 +27,22 @@ import org.jackhuang.hellominecraft.launcher.settings.Settings;
*/
public abstract class IAuthenticator {
public static final YggdrasilAuthenticator yggdrasilLogin;
public static final OfflineAuthenticator offlineLogin;
public static final SkinmeAuthenticator skinmeLogin;
public static final YggdrasilAuthenticator YGGDRASIL_LOGIN;
public static final OfflineAuthenticator OFFLINE_LOGIN;
public static final SkinmeAuthenticator SKINME_LOGIN;
public static final List<IAuthenticator> logins;
public static final List<IAuthenticator> LOGINS;
static {
String clientToken = Settings.getInstance().getClientToken();
logins = new ArrayList<>();
logins.add(offlineLogin = new OfflineAuthenticator(clientToken));
logins.add(yggdrasilLogin = new YggdrasilAuthenticator(clientToken));
logins.add(skinmeLogin = new SkinmeAuthenticator(clientToken));
yggdrasilLogin.onLoadSettings(Settings.getInstance().getYggdrasilConfig());
LOGINS = new ArrayList<>();
LOGINS.add(OFFLINE_LOGIN = new OfflineAuthenticator(clientToken));
LOGINS.add(YGGDRASIL_LOGIN = new YggdrasilAuthenticator(clientToken));
LOGINS.add(SKINME_LOGIN = new SkinmeAuthenticator(clientToken));
YGGDRASIL_LOGIN.onLoadSettings(Settings.getInstance().getYggdrasilConfig());
Runtime.getRuntime().addShutdownHook(new Thread(()
-> Settings.getInstance().setYggdrasilConfig(yggdrasilLogin.onSaveSettings())
-> Settings.getInstance().setYggdrasilConfig(YGGDRASIL_LOGIN.onSaveSettings())
));
}

View File

@ -1,5 +1,13 @@
package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap;
import java.util.UUID;
import org.jackhuang.hellominecraft.utils.StrUtils;
@ -11,7 +19,7 @@ public class GameProfile {
public final PropertyMap properties = new PropertyMap();
public GameProfile(UUID id, String name) {
if ((id == null) && (StrUtils.isBlank(name)))
if (id == null && StrUtils.isBlank(name))
throw new IllegalArgumentException("Name and ID cannot both be blank");
this.id = id;
@ -19,14 +27,14 @@ public class GameProfile {
}
public boolean isComplete() {
return (id != null) && (StrUtils.isNotBlank(name));
return id != null && StrUtils.isNotBlank(name);
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if ((o == null) || (getClass() != o.getClass()))
if (o == null || getClass() != o.getClass())
return false;
GameProfile that = (GameProfile) o;
@ -47,4 +55,25 @@ public class GameProfile {
public String toString() {
return "GameProfile{" + "id=" + id + ", name=" + name + ", properties=" + properties + '}';
}
public static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile> {
@Override
public GameProfile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = (JsonObject) json;
UUID id = object.has("id") ? (UUID) context.deserialize(object.get("id"), UUID.class) : null;
String name = object.has("name") ? object.getAsJsonPrimitive("name").getAsString() : null;
return new GameProfile(id, name);
}
@Override
public JsonElement serialize(GameProfile src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject result = new JsonObject();
if (src.id != null)
result.add("id", context.serialize(src.id));
if (src.name != null)
result.addProperty("name", src.name);
return result;
}
}
}

View File

@ -10,7 +10,7 @@ public class UUIDTypeAdapter extends TypeAdapter<UUID> {
@Override
public void write(JsonWriter out, UUID value)
throws IOException {
throws IOException {
out.value(fromUUID(value));
}

View File

@ -2,19 +2,12 @@ package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
@ -22,6 +15,7 @@ import java.net.URLEncoder;
import java.util.Map;
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.logging.logger.Logger;
import org.jackhuang.hellominecraft.utils.NetUtils;
@ -195,25 +189,4 @@ public class YggdrasilAuthenticationService {
public String getClientToken() {
return this.clientToken;
}
private static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile> {
@Override
public GameProfile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = (JsonObject) json;
UUID id = object.has("id") ? (UUID) context.deserialize(object.get("id"), UUID.class) : null;
String name = object.has("name") ? object.getAsJsonPrimitive("name").getAsString() : null;
return new GameProfile(id, name);
}
@Override
public JsonElement serialize(GameProfile src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject result = new JsonObject();
if (src.id != null)
result.add("id", context.serialize(src.id));
if (src.name != null)
result.addProperty("name", src.name);
return result;
}
}
}

View File

@ -19,7 +19,7 @@ import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.Authe
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.RefreshResponse;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.User;
public class YggdrasilUserAuthentication {
public class YggdrasilUserAuthentication {
protected static final String STORAGE_KEY_PROFILE_NAME = "displayName";
protected static final String STORAGE_KEY_PROFILE_ID = "uuid";
@ -35,60 +35,58 @@ public class YggdrasilUserAuthentication {
private GameProfile selectedProfile;
public void setUsername(String username) {
if ((isLoggedIn()) && (canPlayOnline())) {
throw new IllegalStateException("Cannot change username whilst logged in & online");
}
if ((isLoggedIn()) && (canPlayOnline()))
throw new IllegalStateException("Cannot change username whilst logged in & online");
this.username = username;
this.username = username;
}
public void setPassword(String password) {
if ((isLoggedIn()) && (canPlayOnline()) && (StrUtils.isNotBlank(password))) {
throw new IllegalStateException("Cannot set password whilst logged in & online");
}
if ((isLoggedIn()) && (canPlayOnline()) && (StrUtils.isNotBlank(password)))
throw new IllegalStateException("Cannot set password whilst logged in & online");
this.password = password;
this.password = password;
}
protected String getUsername() {
return this.username;
return this.username;
}
protected String getPassword() {
return this.password;
return this.password;
}
protected void setSelectedProfile(GameProfile selectedProfile) {
this.selectedProfile = selectedProfile;
this.selectedProfile = selectedProfile;
}
public GameProfile getSelectedProfile() {
return this.selectedProfile;
return this.selectedProfile;
}
public YggdrasilAuthenticationService getAuthenticationService() {
return this.authenticationService;
return this.authenticationService;
}
public String getUserID() {
return this.userid;
return this.userid;
}
public PropertyMap getUserProperties() {
if (isLoggedIn()) {
PropertyMap result = new PropertyMap();
result.putAll(getModifiableUserProperties());
return result;
}
return new PropertyMap();
if (isLoggedIn()) {
PropertyMap result = new PropertyMap();
result.putAll(getModifiableUserProperties());
return result;
}
return new PropertyMap();
}
protected PropertyMap getModifiableUserProperties() {
return this.userProperties;
return this.userProperties;
}
protected void setUserid(String userid) {
this.userid = userid;
this.userid = userid;
}
private static final Logger LOGGER = new Logger("YggdrasilUserAuthentication");
@ -101,275 +99,236 @@ public class YggdrasilUserAuthentication {
private boolean isOnline;
public YggdrasilUserAuthentication(YggdrasilAuthenticationService authenticationService) {
this.authenticationService = authenticationService;
this.authenticationService = authenticationService;
}
public boolean canLogIn() {
return (!canPlayOnline()) && (StrUtils.isNotBlank(getUsername())) && ((StrUtils.isNotBlank(getPassword())) || (StrUtils.isNotBlank(getAuthenticatedToken())));
return (!canPlayOnline()) && (StrUtils.isNotBlank(getUsername())) && ((StrUtils.isNotBlank(getPassword())) || (StrUtils.isNotBlank(getAuthenticatedToken())));
}
public void logIn() throws AuthenticationException {
if (StrUtils.isBlank(getUsername())) {
throw new AuthenticationException(C.i18n("login.invalid_username"));
}
if (StrUtils.isBlank(getUsername()))
throw new AuthenticationException(C.i18n("login.invalid_username"));
if (StrUtils.isNotBlank(getAuthenticatedToken())) {
logInWithToken();
} else if (StrUtils.isNotBlank(getPassword())) {
logInWithPassword();
} else {
throw new AuthenticationException(C.i18n("login.invalid_password"));
}
if (StrUtils.isNotBlank(getAuthenticatedToken()))
logInWithToken();
else if (StrUtils.isNotBlank(getPassword()))
logInWithPassword();
else
throw new AuthenticationException(C.i18n("login.invalid_password"));
}
protected void logInWithPassword() throws AuthenticationException {
if (StrUtils.isBlank(getUsername())) {
throw new AuthenticationException(C.i18n("login.invalid_username"));
}
if (StrUtils.isBlank(getPassword())) {
throw new AuthenticationException(C.i18n("login.invalid_password"));
}
if (StrUtils.isBlank(getUsername()))
throw new AuthenticationException(C.i18n("login.invalid_username"));
if (StrUtils.isBlank(getPassword()))
throw new AuthenticationException(C.i18n("login.invalid_password"));
LOGGER.info("Logging in with username & password");
LOGGER.info("Logging in with username & password");
AuthenticationRequest request = new AuthenticationRequest(this, getUsername(), getPassword());
AuthenticationResponse response = (AuthenticationResponse) getAuthenticationService().makeRequest(ROUTE_AUTHENTICATE, request, AuthenticationResponse.class);
AuthenticationRequest request = new AuthenticationRequest(this, getUsername(), getPassword());
AuthenticationResponse response = (AuthenticationResponse) getAuthenticationService().makeRequest(ROUTE_AUTHENTICATE, request, AuthenticationResponse.class);
if (!response.clientToken.equals(getAuthenticationService().getClientToken())) {
throw new AuthenticationException(C.i18n("login.changed_client_token"));
}
if (!response.clientToken.equals(getAuthenticationService().getClientToken()))
throw new AuthenticationException(C.i18n("login.changed_client_token"));
User user = response.user;
User user = response.user;
if ((user != null) && (user.getId() != null)) {
setUserid(user.getId());
} else {
setUserid(getUsername());
}
setUserid(user != null && user.id != null ? user.id : getUsername());
this.isOnline = true;
this.accessToken = response.accessToken;
this.isOnline = true;
this.accessToken = response.accessToken;
this.profiles = response.availableProfiles;
setSelectedProfile(response.selectedProfile);
getModifiableUserProperties().clear();
this.profiles = response.availableProfiles;
setSelectedProfile(response.selectedProfile);
getModifiableUserProperties().clear();
updateUserProperties(user);
updateUserProperties(user);
}
protected void updateUserProperties(User user) {
if (user == null) {
return;
}
if (user == null)
return;
if (user.getProperties() != null) {
getModifiableUserProperties().putAll(user.getProperties());
}
if (user.properties != null)
getModifiableUserProperties().putAll(user.properties);
}
protected void logInWithToken() throws AuthenticationException {
if (StrUtils.isBlank(getUserID())) {
if (StrUtils.isBlank(getUsername())) {
setUserid(getUsername());
} else {
throw new AuthenticationException(C.i18n("login.invalid_uuid_and_username"));
}
}
if (StrUtils.isBlank(getAuthenticatedToken())) {
throw new AuthenticationException(C.i18n("login.invalid_access_token"));
}
if (StrUtils.isBlank(getUserID()))
if (StrUtils.isBlank(getUsername()))
setUserid(getUsername());
else
throw new AuthenticationException(C.i18n("login.invalid_uuid_and_username"));
if (StrUtils.isBlank(getAuthenticatedToken()))
throw new AuthenticationException(C.i18n("login.invalid_access_token"));
LOGGER.info("Logging in with access token");
LOGGER.info("Logging in with access token");
RefreshRequest request = new RefreshRequest(this);
RefreshResponse response = (RefreshResponse) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, RefreshResponse.class);
RefreshRequest request = new RefreshRequest(this);
RefreshResponse response = (RefreshResponse) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, RefreshResponse.class);
if (!response.clientToken.equals(getAuthenticationService().getClientToken())) {
throw new AuthenticationException(C.i18n("login.changed_client_token"));
}
if (!response.clientToken.equals(getAuthenticationService().getClientToken()))
throw new AuthenticationException(C.i18n("login.changed_client_token"));
if ((response.user != null) && (response.user.getId() != null)) {
setUserid(response.user.getId());
} else {
setUserid(getUsername());
}
setUserid(response.user != null && response.user.id != null ? response.user.id : getUsername());
this.isOnline = true;
this.accessToken = response.accessToken;
this.profiles = response.availableProfiles;
setSelectedProfile(response.selectedProfile);
getModifiableUserProperties().clear();
this.isOnline = true;
this.accessToken = response.accessToken;
this.profiles = response.availableProfiles;
setSelectedProfile(response.selectedProfile);
getModifiableUserProperties().clear();
updateUserProperties(response.user);
updateUserProperties(response.user);
}
public void logOut() {
this.password = null;
this.userid = null;
setSelectedProfile(null);
getModifiableUserProperties().clear();
this.password = null;
this.userid = null;
setSelectedProfile(null);
getModifiableUserProperties().clear();
this.accessToken = null;
this.profiles = null;
this.isOnline = false;
this.accessToken = null;
this.profiles = null;
this.isOnline = false;
}
public GameProfile[] getAvailableProfiles() {
return this.profiles;
return this.profiles;
}
public boolean isLoggedIn() {
return StrUtils.isNotBlank(this.accessToken);
return StrUtils.isNotBlank(this.accessToken);
}
public boolean canPlayOnline() {
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 + "'");
}
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);
RefreshResponse response = (RefreshResponse) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, RefreshResponse.class);
RefreshRequest request = new RefreshRequest(this, profile);
RefreshResponse response = (RefreshResponse) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, RefreshResponse.class);
if (!response.clientToken.equals(getAuthenticationService().getClientToken())) {
throw new AuthenticationException(C.i18n("login.changed_client_token"));
}
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);
this.isOnline = true;
this.accessToken = response.accessToken;
setSelectedProfile(response.selectedProfile);
}
public void loadFromStorage(Map<String, Object> credentials) {
logOut();
logOut();
setUsername((String)credentials.get("username"));
setUsername((String) credentials.get("username"));
if (credentials.containsKey("userid")) {
this.userid = (String)credentials.get("userid");
} else {
this.userid = this.username;
}
if (credentials.containsKey("userid"))
this.userid = (String) credentials.get("userid");
else
this.userid = this.username;
if (credentials.containsKey("userProperties")) {
try {
List<Map> list = (List<Map>) credentials.get("userProperties");
if (credentials.containsKey("userProperties"))
try {
List<Map> list = (List<Map>) credentials.get("userProperties");
for (Map propertyMap : list) {
String name = (String) propertyMap.get("name");
String value = (String) propertyMap.get("value");
String signature = (String) propertyMap.get("signature");
for (Map propertyMap : list) {
String name = (String) propertyMap.get("name");
String value = (String) propertyMap.get("value");
String signature = (String) propertyMap.get("signature");
if (signature == null) {
getModifiableUserProperties().put(name, new Property(name, value));
} else {
getModifiableUserProperties().put(name, new Property(name, value, signature));
}
}
} catch (Throwable t) {
LOGGER.warn("Couldn't deserialize user properties", t);
}
}
getModifiableUserProperties().put(name, new Property(name, value, signature));
}
} catch (Throwable t) {
LOGGER.warn("Couldn't deserialize user properties", t);
}
if ((credentials.containsKey("displayName")) && (credentials.containsKey("uuid"))) {
GameProfile profile = new GameProfile(UUIDTypeAdapter.fromString((String)credentials.get("uuid")), (String)credentials.get("displayName"));
if (credentials.containsKey("profileProperties")) {
try {
List<Map> list = (List<Map>) credentials.get("profileProperties");
for (Map propertyMap : list) {
String name = (String) propertyMap.get("name");
String value = (String) propertyMap.get("value");
String signature = (String) propertyMap.get("signature");
if ((credentials.containsKey("displayName")) && (credentials.containsKey("uuid"))) {
GameProfile profile = new GameProfile(UUIDTypeAdapter.fromString((String) credentials.get("uuid")), (String) credentials.get("displayName"));
if (credentials.containsKey("profileProperties"))
try {
List<Map<String, String>> list = (List<Map<String, String>>) credentials.get("profileProperties");
for (Map<String, String> propertyMap : list) {
String name = propertyMap.get("name");
String value = propertyMap.get("value");
String signature = propertyMap.get("signature");
if (signature == null) {
profile.properties.put(name, new Property(name, value));
} else {
profile.properties.put(name, new Property(name, value, signature));
}
}
} catch (Throwable t) {
LOGGER.warn("Couldn't deserialize profile properties", t);
}
}
setSelectedProfile(profile);
}
profile.properties.put(name, new Property(name, value, signature));
}
} catch (Throwable t) {
LOGGER.warn("Couldn't deserialize profile properties", t);
}
setSelectedProfile(profile);
}
this.accessToken = (String)credentials.get(STORAGE_KEY_ACCESS_TOKEN);
this.accessToken = (String) credentials.get(STORAGE_KEY_ACCESS_TOKEN);
}
public Map<String, Object> saveForStorage() {
Map result = new HashMap();
Map result = new HashMap();
if (getUsername() != null) {
result.put("username", getUsername());
}
if (getUserID() != null) {
result.put("userid", getUserID());
} else if (getUsername() != null) {
result.put("username", getUsername());
}
if (getUsername() != null)
result.put("username", getUsername());
if (getUserID() != null)
result.put("userid", getUserID());
else if (getUsername() != null)
result.put("username", getUsername());
if (!getUserProperties().isEmpty()) {
List properties = new ArrayList();
for (Property userProperty : getUserProperties().values()) {
Map property = new HashMap();
property.put("name", userProperty.getName());
property.put("value", userProperty.getValue());
property.put("signature", userProperty.getSignature());
properties.add(property);
}
result.put("userProperties", properties);
}
if (!getUserProperties().isEmpty()) {
List properties = new ArrayList();
for (Property userProperty : getUserProperties().values()) {
Map<String, String> property = new HashMap<>();
property.put("name", userProperty.name);
property.put("value", userProperty.value);
property.put("signature", userProperty.signature);
properties.add(property);
}
result.put("userProperties", properties);
}
GameProfile sel = getSelectedProfile();
if (sel != null) {
result.put("displayName", sel.name);
result.put("uuid", sel.id);
GameProfile sel = getSelectedProfile();
if (sel != null) {
result.put("displayName", sel.name);
result.put("uuid", sel.id);
List properties = new ArrayList();
for (Property profileProperty : sel.properties.values()) {
Map property = new HashMap();
property.put("name", profileProperty.getName());
property.put("value", profileProperty.getValue());
property.put("signature", profileProperty.getSignature());
properties.add(property);
}
List properties = new ArrayList();
for (Property profileProperty : sel.properties.values()) {
Map<String, String> property = new HashMap<>();
property.put("name", profileProperty.name);
property.put("value", profileProperty.value);
property.put("signature", profileProperty.signature);
properties.add(property);
}
if (!properties.isEmpty()) {
result.put("profileProperties", properties);
}
}
if (!properties.isEmpty())
result.put("profileProperties", properties);
}
if (StrUtils.isNotBlank(getAuthenticatedToken())) {
result.put("accessToken", getAuthenticatedToken());
}
if (StrUtils.isNotBlank(getAuthenticatedToken()))
result.put("accessToken", getAuthenticatedToken());
return result;
return result;
}
@Deprecated
public String getSessionToken() {
if ((isLoggedIn()) && (getSelectedProfile() != null) && (canPlayOnline())) {
return String.format("token:%s:%s", new Object[]{getAuthenticatedToken(), getSelectedProfile().id});
}
return null;
if (isLoggedIn() && getSelectedProfile() != null && canPlayOnline())
return String.format("token:%s:%s", new Object[]{getAuthenticatedToken(), getSelectedProfile().id});
return null;
}
public String getAuthenticatedToken() {
return this.accessToken;
return this.accessToken;
}
@Override
public String toString() {
return "YggdrasilAuthenticationService{profiles=" + Arrays.toString(this.profiles) + ", selectedProfile=" + getSelectedProfile() + ", username='" + getUsername() + '\'' + ", isLoggedIn=" + isLoggedIn() + ", canPlayOnline=" + canPlayOnline() + ", accessToken='" + this.accessToken + '\'' + ", clientToken='" + getAuthenticationService().getClientToken() + '\'' + '}';
return "YggdrasilAuthenticationService{profiles=" + Arrays.toString(this.profiles) + ", selectedProfile=" + getSelectedProfile() + ", username='" + getUsername() + '\'' + ", isLoggedIn=" + isLoggedIn() + ", canPlayOnline=" + canPlayOnline() + ", accessToken='" + this.accessToken + '\'' + ", clientToken='" + getAuthenticationService().getClientToken() + '\'' + '}';
}
}
}

View File

@ -27,7 +27,7 @@ public class LegacyPropertyMapSerializer
for (String key : src.keySet()) {
JsonArray values = new JsonArray();
values.add(new JsonPrimitive(src.get(key).getValue()));
values.add(new JsonPrimitive(src.get(key).value));
result.add(key, values);
}

View File

@ -9,9 +9,9 @@ import org.jackhuang.hellominecraft.utils.code.Base64;
public class Property {
private final String name;
private final String value;
private final String signature;
public final String name;
public final String value;
public final String signature;
public Property(String value, String name) {
this(value, name, null);
@ -23,33 +23,13 @@ public class Property {
this.signature = signature;
}
public String getName() {
return this.name;
}
public String getValue() {
return this.value;
}
public String getSignature() {
return this.signature;
}
public boolean hasSignature() {
return this.signature != null;
}
public boolean isSignatureValid(PublicKey publicKey) {
try {
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initVerify(publicKey);
signature.update(this.value.getBytes());
return signature.verify(Base64.decode(this.signature.toCharArray()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (SignatureException e) {
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
e.printStackTrace();
}
return false;

View File

@ -14,57 +14,47 @@ import java.util.Map;
public class PropertyMap extends HashMap<String, Property> {
public PropertyMap() {
}
public static class Serializer implements JsonSerializer<PropertyMap>, JsonDeserializer<PropertyMap> {
@Override
@Override
public PropertyMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
PropertyMap result = new PropertyMap();
if ((json instanceof JsonObject)) {
JsonObject object = (JsonObject) json;
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
if ((entry.getValue() instanceof JsonArray)) {
for (JsonElement element : (JsonArray) entry.getValue()) {
for (Map.Entry<String, JsonElement> entry : object.entrySet())
if ((entry.getValue() instanceof JsonArray))
for (JsonElement element : (JsonArray) entry.getValue())
result.put(entry.getKey(),
new Property((String) entry.getKey(), element.getAsString()));
}
}
}
} else if ((json instanceof JsonArray)) {
for (JsonElement element : (JsonArray) json) {
new Property((String) entry.getKey(), element.getAsString()));
} else if ((json instanceof JsonArray))
for (JsonElement element : (JsonArray) json)
if ((element instanceof JsonObject)) {
JsonObject object = (JsonObject) element;
String name = object.getAsJsonPrimitive("name").getAsString();
String value = object.getAsJsonPrimitive("value").getAsString();
if (object.has("signature")) {
if (object.has("signature"))
result.put(name, new Property(name, value, object.getAsJsonPrimitive("signature").getAsString()));
} else {
else
result.put(name, new Property(name, value));
}
}
}
}
return result;
}
@Override
@Override
public JsonElement serialize(PropertyMap src, Type typeOfSrc, JsonSerializationContext context) {
JsonArray result = new JsonArray();
for (Property property : src.values()) {
JsonObject object = new JsonObject();
object.addProperty("name", property.getName());
object.addProperty("value", property.getValue());
object.addProperty("name", property.name);
object.addProperty("value", property.value);
if (property.hasSignature()) {
object.addProperty("signature", property.getSignature());
}
if (property.signature != null)
object.addProperty("signature", property.signature);
result.add(object);
}
@ -72,4 +62,4 @@ public class PropertyMap extends HashMap<String, Property> {
return result;
}
}
}
}

View File

@ -4,14 +4,6 @@ import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.Pro
public class User {
private String id;
private PropertyMap properties;
public String getId() {
return this.id;
}
public PropertyMap getProperties() {
return this.properties;
}
public String id;
public PropertyMap properties;
}

View File

@ -279,7 +279,7 @@ public class MainPagePanel extends javax.swing.JPanel {
int index = cboLoginMode.getSelectedIndex();
if (index < 0) return;
IAuthenticator l = IAuthenticator.logins.get(index);
IAuthenticator l = IAuthenticator.LOGINS.get(index);
if (l.isHidePasswordBox()) {
pnlPassword.setVisible(false);
lblUserName.setText(C.i18n("login.username"));
@ -324,7 +324,7 @@ public class MainPagePanel extends javax.swing.JPanel {
if (preaparingAuth) return;
int index = cboLoginMode.getSelectedIndex();
IAuthenticator l = IAuthenticator.logins.get(index);
IAuthenticator l = IAuthenticator.LOGINS.get(index);
CardLayout cl = (CardLayout) pnlPassword.getLayout();
if (l.isLoggedIn()) l.logout();
cl.first(pnlPassword);
@ -335,7 +335,7 @@ public class MainPagePanel extends javax.swing.JPanel {
Settings.getInstance().setUsername(txtPlayerName.getText());
int index = cboLoginMode.getSelectedIndex();
if (index < 0) return;
IAuthenticator l = IAuthenticator.logins.get(index);
IAuthenticator l = IAuthenticator.LOGINS.get(index);
if (l.isHidePasswordBox()) btnRunActionPerformed();
else if (!l.isLoggedIn()) txtPassword.requestFocus();
}
@ -366,12 +366,12 @@ public class MainPagePanel extends javax.swing.JPanel {
}
final int index = cboLoginMode.getSelectedIndex();
if (index < 0 || index >= IAuthenticator.logins.size()) {
if (index < 0 || index >= IAuthenticator.LOGINS.size()) {
HMCLog.warn("There's no login method.");
MessageBox.Show(C.i18n("login.methods.no_method"));
return;
}
final IAuthenticator l = IAuthenticator.logins.get(index);
final IAuthenticator l = IAuthenticator.LOGINS.get(index);
final LoginInfo li = new LoginInfo(Settings.getInstance().getUsername(), l.isLoggedIn() || l.isHidePasswordBox() ? null : new String(txtPassword.getPassword()));
Thread t = new Thread() {
@Override
@ -399,9 +399,8 @@ public class MainPagePanel extends javax.swing.JPanel {
// <editor-fold defaultstate="collapsed" desc="Loads">
private void prepareAuths() {
preaparingAuth = true;
List<IAuthenticator> list = IAuthenticator.logins;
cboLoginMode.removeAllItems();
for (IAuthenticator str : list)
for (IAuthenticator str : IAuthenticator.LOGINS)
try {
cboLoginMode.addItem(str.getName());
} catch (Exception ex) {
@ -506,7 +505,7 @@ public class MainPagePanel extends javax.swing.JPanel {
if (showLeft)
SwingUtilities.invokeLater(() -> MainFrame.instance.showMessage(C.i18n("ui.message.first_load")));
if (cboLoginMode.getSelectedIndex() >= 0 && cboLoginMode.getSelectedIndex() < cboLoginMode.getItemCount()) {
IAuthenticator l = IAuthenticator.logins.get(cboLoginMode.getSelectedIndex());
IAuthenticator l = IAuthenticator.LOGINS.get(cboLoginMode.getSelectedIndex());
if (!l.isHidePasswordBox() && !l.isLoggedIn())
SwingUtilities.invokeLater(() -> MainFrame.instance.showMessage(C.i18n("ui.message.enter_password")));
}