Ensure skin servers have a valid address (or gateway in the case of legacy)

This commit is contained in:
Sollace 2018-07-26 15:42:12 +02:00
parent b0d675a70c
commit d483e460c3
7 changed files with 43 additions and 32 deletions

View file

@ -241,6 +241,8 @@ public final class HDSkinManager implements IResourceManagerReloadListener {
}
public void addSkinServer(SkinServer skinServer) {
skinServer.validate();
skinServers.add(skinServer);
}

View file

@ -9,6 +9,11 @@ import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.logging.log4j.util.Strings;
import com.google.gson.JsonParseException;
import com.google.gson.annotations.Expose;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
@ -20,6 +25,12 @@ import net.minecraft.util.Session;
public abstract class AbstractSkinServer implements SkinServer {
@Expose
protected final String address;
public AbstractSkinServer(String address) {
this.address = address;
}
@Override
public final Optional<MinecraftTexturesPayload> loadProfileData(GameProfile profile) {
@ -40,8 +51,21 @@ public abstract class AbstractSkinServer implements SkinServer {
}, HDSkinManager.skinUploadExecutor);
}
@Override
public void validate() throws JsonParseException {
if (Strings.isBlank(address)) {
throw new JsonParseException("Address was not specified.");
}
}
protected abstract MinecraftTexturesPayload getProfileData(GameProfile profile);
protected abstract SkinUploadResponse doUpload(Session session, URI image, Type type, Map<String, String> metadata) throws AuthenticationException, IOException;
@Override
public String toString() {
return new ToStringBuilder(this, IndentedToStringStyle.INSTANCE)
.append("address", address)
.build();
}
}

View file

@ -9,7 +9,6 @@ import org.apache.http.HttpStatus;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.gson.annotations.Expose;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type;
@ -23,11 +22,8 @@ public class BethlehemSkinServer extends AbstractSkinServer {
private static final String SERVER_ID = "7853dfddc358333843ad55a2c7485c4aa0380a51";
@Expose
private final String address;
private BethlehemSkinServer(String address) {
this.address = address;
public BethlehemSkinServer(String address) {
super(address);
}
@Override

View file

@ -1,6 +1,5 @@
package com.voxelmodpack.hdskins.skins;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.gson.annotations.Expose;
@ -16,10 +15,10 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.util.Strings;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Locale;
import java.util.Map;
@ -32,23 +31,16 @@ public class LegacySkinServer extends AbstractSkinServer {
private static final Logger logger = LogManager.getLogger();
@Expose
private final String address;
@Expose
private final String gateway;
public LegacySkinServer(String address, @Nullable String gateway) {
this.address = address;
this.gateway = gateway;
super(address);
this.gateway = Strings.isBlank(gateway) ? address : gateway;
}
@Override
public Map<Type, MinecraftProfileTexture> getPreviewTextures(GameProfile profile) {
if (Strings.isNullOrEmpty(gateway)) {
return Collections.emptyMap();
}
Map<Type, MinecraftProfileTexture> map = new EnumMap<>(Type.class);
for (Type type : Type.values()) {
@ -105,8 +97,10 @@ public class LegacySkinServer extends AbstractSkinServer {
response = response.substring(7);
}
if (!response.equalsIgnoreCase("OK") && !response.endsWith("OK"))
if (!response.equalsIgnoreCase("OK") && !response.endsWith("OK")) {
throw new IOException(response);
}
return new SkinUploadResponse(response);
}
}

View file

@ -3,6 +3,7 @@ package com.voxelmodpack.hdskins.skins;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
@ -39,6 +40,8 @@ public interface SkinServer extends Exposable {
CompletableFuture<SkinUploadResponse> uploadSkin(Session session, @Nullable URI image, MinecraftProfileTexture.Type type, Map<String, String> metadata);
void validate() throws JsonParseException;
public static void verifyServerConnection(Session session, String serverId) throws AuthenticationException {
MinecraftSessionService service = Minecraft.getMinecraft().getSessionService();
service.joinServer(session.getProfile(), session.getToken(), serverId);

View file

@ -32,6 +32,10 @@ public class SkinServerSerializer implements JsonSerializer<SkinServer>, JsonDes
public SkinServer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String type = json.getAsJsonObject().get("type").getAsString();
return context.deserialize(json, HDSkinManager.INSTANCE.getSkinServerClass(type));
SkinServer server = context.deserialize(json, HDSkinManager.INSTANCE.getSkinServerClass(type));
server.validate();
return server;
}
}

View file

@ -2,7 +2,6 @@ package com.voxelmodpack.hdskins.skins;
import com.google.common.base.Preconditions;
import com.google.gson.JsonObject;
import com.google.gson.annotations.Expose;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
@ -14,7 +13,6 @@ import javax.annotation.Nullable;
import net.minecraft.client.Minecraft;
import net.minecraft.util.Session;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
@ -42,13 +40,10 @@ import java.util.UUID;
@ServerType("valhalla")
public class ValhallaSkinServer extends AbstractSkinServer {
@Expose
private final String address;
private transient String accessToken;
public ValhallaSkinServer(String address) {
this.address = address;
super(address);
}
@Override
@ -222,13 +217,6 @@ public class ValhallaSkinServer extends AbstractSkinServer {
return URI.create(String.format("%s/auth/response", address));
}
@Override
public String toString() {
return new ToStringBuilder(this, IndentedToStringStyle.INSTANCE)
.append("address", address)
.build();
}
@SuppressWarnings("WeakerAccess")
private static class AuthHandshake {