fix: not compatible with ely.by

Ely.by's Yggdrasil response does not include 'user' field.
This commit makes the 'user' field optional.
This commit is contained in:
Haowei Wen
2021-02-15 03:54:32 +08:00
committed by Yuhui Huang
parent 46869ad5f7
commit 734c1e14f0
2 changed files with 11 additions and 15 deletions

View File

@@ -218,7 +218,7 @@ public class YggdrasilService {
response.accessToken, response.accessToken,
response.selectedProfile, response.selectedProfile,
response.availableProfiles == null ? null : unmodifiableList(response.availableProfiles), response.availableProfiles == null ? null : unmodifiableList(response.availableProfiles),
response.user); response.user == null ? null : response.user.getProperties());
} }
private static void requireEmpty(String response) throws AuthenticationException { private static void requireEmpty(String response) throws AuthenticationException {

View File

@@ -21,6 +21,7 @@ import com.google.gson.Gson;
import org.jackhuang.hmcl.auth.AuthInfo; import org.jackhuang.hmcl.auth.AuthInfo;
import org.jackhuang.hmcl.util.Immutable; import org.jackhuang.hmcl.util.Immutable;
import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter; import org.jackhuang.hmcl.util.gson.UUIDTypeAdapter;
import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -36,14 +37,15 @@ public class YggdrasilSession {
private final String accessToken; private final String accessToken;
private final GameProfile selectedProfile; private final GameProfile selectedProfile;
private final List<GameProfile> availableProfiles; private final List<GameProfile> availableProfiles;
private final User user; @Nullable
private final Map<String, String> userProperties;
public YggdrasilSession(String clientToken, String accessToken, GameProfile selectedProfile, List<GameProfile> availableProfiles, User user) { public YggdrasilSession(String clientToken, String accessToken, GameProfile selectedProfile, List<GameProfile> availableProfiles, Map<String, String> userProperties) {
this.clientToken = clientToken; this.clientToken = clientToken;
this.accessToken = accessToken; this.accessToken = accessToken;
this.selectedProfile = selectedProfile; this.selectedProfile = selectedProfile;
this.availableProfiles = availableProfiles; this.availableProfiles = availableProfiles;
this.user = user; this.userProperties = userProperties;
} }
public String getClientToken() { public String getClientToken() {
@@ -68,8 +70,8 @@ public class YggdrasilSession {
return availableProfiles; return availableProfiles;
} }
public User getUser() { public Map<String, String> getUserProperties() {
return user; return userProperties;
} }
public static YggdrasilSession fromStorage(Map<?, ?> storage) { public static YggdrasilSession fromStorage(Map<?, ?> storage) {
@@ -77,34 +79,28 @@ public class YggdrasilSession {
String name = tryCast(storage.get("displayName"), String.class).orElseThrow(() -> new IllegalArgumentException("displayName is missing")); String name = tryCast(storage.get("displayName"), String.class).orElseThrow(() -> new IllegalArgumentException("displayName is missing"));
String clientToken = tryCast(storage.get("clientToken"), String.class).orElseThrow(() -> new IllegalArgumentException("clientToken is missing")); String clientToken = tryCast(storage.get("clientToken"), String.class).orElseThrow(() -> new IllegalArgumentException("clientToken is missing"));
String accessToken = tryCast(storage.get("accessToken"), String.class).orElseThrow(() -> new IllegalArgumentException("accessToken is missing")); String accessToken = tryCast(storage.get("accessToken"), String.class).orElseThrow(() -> new IllegalArgumentException("accessToken is missing"));
String userId = tryCast(storage.get("userid"), String.class).orElseThrow(() -> new IllegalArgumentException("userid is missing"));
Map<String, String> userProperties = tryCast(storage.get("userProperties"), Map.class).orElse(null); Map<String, String> userProperties = tryCast(storage.get("userProperties"), Map.class).orElse(null);
return new YggdrasilSession(clientToken, accessToken, new GameProfile(uuid, name), null, new User(userId, userProperties)); return new YggdrasilSession(clientToken, accessToken, new GameProfile(uuid, name), null, userProperties);
} }
public Map<Object, Object> toStorage() { public Map<Object, Object> toStorage() {
if (selectedProfile == null) if (selectedProfile == null)
throw new IllegalStateException("No character is selected"); throw new IllegalStateException("No character is selected");
if (user == null)
throw new IllegalStateException("No user is specified");
return mapOf( return mapOf(
pair("clientToken", clientToken), pair("clientToken", clientToken),
pair("accessToken", accessToken), pair("accessToken", accessToken),
pair("uuid", UUIDTypeAdapter.fromUUID(selectedProfile.getId())), pair("uuid", UUIDTypeAdapter.fromUUID(selectedProfile.getId())),
pair("displayName", selectedProfile.getName()), pair("displayName", selectedProfile.getName()),
pair("userid", user.getId()), pair("userProperties", userProperties));
pair("userProperties", user.getProperties()));
} }
public AuthInfo toAuthInfo() { public AuthInfo toAuthInfo() {
if (selectedProfile == null) if (selectedProfile == null)
throw new IllegalStateException("No character is selected"); throw new IllegalStateException("No character is selected");
if (user == null)
throw new IllegalStateException("No user is specified");
return new AuthInfo(selectedProfile.getName(), selectedProfile.getId(), accessToken, return new AuthInfo(selectedProfile.getName(), selectedProfile.getId(), accessToken,
Optional.ofNullable(user.getProperties()) Optional.ofNullable(userProperties)
.map(properties -> properties.entrySet().stream() .map(properties -> properties.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, .collect(Collectors.toMap(Map.Entry::getKey,
e -> Collections.singleton(e.getValue())))) e -> Collections.singleton(e.getValue()))))