Search in sources :

Example 1 with MinecraftTexturesPayload

use of com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload in project Krothium-Launcher by DarkLBP.

the class YggdrasilMinecraftSessionService method fetchCustomTextures.

public Map<Type, MinecraftProfileTexture> fetchCustomTextures(GameProfile profile, boolean requireSecure) {
    if (cache.containsKey(profile.getName())) {
        System.out.println("Serving cached textures for: " + profile.getName() + " / " + profile.getId());
        return cache.get(profile.getName());
    } else {
        try {
            System.out.println("Serving textures for: " + profile.getName() + " / " + profile.getId());
            JSONArray users = new JSONArray();
            users.put(profile.getName());
            byte[] data = users.toString().getBytes();
            String profileID = null;
            String response = Utils.sendPost(GET_PROFILESID, data, new HashMap<String, String>());
            JSONArray rdata = new JSONArray(response);
            if (rdata.length() == 1) {
                JSONObject user = rdata.getJSONObject(0);
                if (user.has("id")) {
                    profileID = user.getString("id");
                    System.out.println("Found user " + profile.getName() + " on Krothium server.");
                }
            } else {
                System.out.println("No textures found on Krothium for " + profile.getName() + ". Searching in Mojang server...");
                HashMap<String, String> params = new HashMap<>();
                params.put("Content-Type", "application/json");
                response = Utils.sendPost(GET_PROFILESID_MOJANG, data, params);
                rdata = new JSONArray(response);
                if (rdata.length() == 1) {
                    JSONObject user = rdata.getJSONObject(0);
                    if (user.has("id")) {
                        profileID = user.getString("id");
                        System.out.println("Found user " + profile.getName() + " on Mojang server.");
                    }
                }
            }
            if (profileID != null) {
                JSONObject profileData = new JSONObject(Utils.readURL("https://sessionserver.mojang.com/session/minecraft/profile/" + profileID + "?unsigned=" + !requireSecure));
                if (profileData.has("properties")) {
                    JSONArray properties = profileData.getJSONArray("properties");
                    if (properties.length() == 1) {
                        JSONObject property = properties.getJSONObject(0);
                        if (property.has("name") && property.has("value")) {
                            if (property.getString("name").equals("textures") && !property.getString("value").isEmpty()) {
                                String textures = new String(Base64.decodeBase64(property.getString("value")), Charsets.UTF_8);
                                MinecraftTexturesPayload result = this.gson.fromJson(textures, MinecraftTexturesPayload.class);
                                cache.put(profile.getName(), result.getTextures());
                                System.out.println("Found textures for " + profile.getName() + ".");
                                return result.getTextures();
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            System.err.println("Failed to fetch data from profile " + profile.getId() + " with name " + profile.getName());
        }
    }
    System.out.println("No textures found for " + profile.getName());
    return new HashMap();
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) MinecraftTexturesPayload(com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload) JsonParseException(com.google.gson.JsonParseException) AuthenticationException(com.mojang.authlib.exceptions.AuthenticationException) AuthenticationUnavailableException(com.mojang.authlib.exceptions.AuthenticationUnavailableException)

Example 2 with MinecraftTexturesPayload

use of com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload in project Krothium-Launcher by DarkLBP.

the class YggdrasilMinecraftSessionService method getTextures.

public Map<Type, MinecraftProfileTexture> getTextures(GameProfile profile, boolean requireSecure) {
    Property textureProperty = (Property) Iterables.getFirst(profile.getProperties().get("textures"), (Object) null);
    if (textureProperty == null) {
        return fetchCustomTextures(profile, requireSecure);
    } else {
        MinecraftTexturesPayload result;
        try {
            String json = new String(Base64.decodeBase64(textureProperty.getValue()), Charsets.UTF_8);
            result = (MinecraftTexturesPayload) this.gson.fromJson(json, MinecraftTexturesPayload.class);
        } catch (JsonParseException var7) {
            LOGGER.error("Could not decode textures payload", var7);
            return new HashMap();
        }
        if (result != null && result.getTextures() != null) {
            Iterator var8 = result.getTextures().entrySet().iterator();
            Map.Entry entry;
            do {
                if (!var8.hasNext()) {
                    return result.getTextures();
                }
                entry = (Map.Entry) var8.next();
            } while (isWhitelistedDomain(((MinecraftProfileTexture) entry.getValue()).getUrl()));
            LOGGER.error("Textures payload has been tampered with (non-whitelisted domain)");
            return new HashMap();
        } else {
            return new HashMap();
        }
    }
}
Also used : HashMap(java.util.HashMap) Iterator(java.util.Iterator) MinecraftTexturesPayload(com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload) JSONObject(org.json.JSONObject) JsonParseException(com.google.gson.JsonParseException) Property(com.mojang.authlib.properties.Property) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JsonParseException (com.google.gson.JsonParseException)2 MinecraftTexturesPayload (com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload)2 HashMap (java.util.HashMap)2 JSONObject (org.json.JSONObject)2 AuthenticationException (com.mojang.authlib.exceptions.AuthenticationException)1 AuthenticationUnavailableException (com.mojang.authlib.exceptions.AuthenticationUnavailableException)1 Property (com.mojang.authlib.properties.Property)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1