From d483e460c37ee470afe25aa5133b4606497cdde3 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 26 Jul 2018 15:42:12 +0200 Subject: [PATCH] Ensure skin servers have a valid address (or gateway in the case of legacy) --- .../voxelmodpack/hdskins/HDSkinManager.java | 2 ++ .../hdskins/skins/AbstractSkinServer.java | 24 +++++++++++++++++++ .../hdskins/skins/BethlehemSkinServer.java | 8 ++----- .../hdskins/skins/LegacySkinServer.java | 18 +++++--------- .../hdskins/skins/SkinServer.java | 3 +++ .../hdskins/skins/SkinServerSerializer.java | 6 ++++- .../hdskins/skins/ValhallaSkinServer.java | 14 +---------- 7 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java index 7f0b378d..ccc34157 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/HDSkinManager.java @@ -241,6 +241,8 @@ public final class HDSkinManager implements IResourceManagerReloadListener { } public void addSkinServer(SkinServer skinServer) { + skinServer.validate(); + skinServers.add(skinServer); } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/AbstractSkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/AbstractSkinServer.java index fcf5238f..f75f9774 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/AbstractSkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/AbstractSkinServer.java @@ -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 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 metadata) throws AuthenticationException, IOException; + @Override + public String toString() { + return new ToStringBuilder(this, IndentedToStringStyle.INSTANCE) + .append("address", address) + .build(); + } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java index b8fd762e..fa93d0f5 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java @@ -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 diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java index f98e1675..e87af0b4 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java @@ -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 getPreviewTextures(GameProfile profile) { - if (Strings.isNullOrEmpty(gateway)) { - return Collections.emptyMap(); - } - Map 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); } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServer.java index 90ba0420..0428671a 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServer.java @@ -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 uploadSkin(Session session, @Nullable URI image, MinecraftProfileTexture.Type type, Map 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); diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServerSerializer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServerSerializer.java index f4992bed..6500e8f4 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServerSerializer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServerSerializer.java @@ -32,6 +32,10 @@ public class SkinServerSerializer implements JsonSerializer, 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; } } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java index d07b7fa1..7bd21324 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java @@ -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 {