Search in sources :

Example 1 with JSONException

use of org.to2mbn.authlibinjector.internal.org.json.JSONException in project authlib-injector by to2mbn.

the class YggdrasilConfiguration method parse.

public static YggdrasilConfiguration parse(String apiRoot, String metadataResponse) throws IOException {
    if (!apiRoot.endsWith("/"))
        apiRoot += "/";
    try {
        JSONObject response = new JSONObject(metadataResponse);
        List<String> skinDomains = new ArrayList<>();
        ofNullable(response.optJSONArray("skinDomains")).ifPresent(it -> it.forEach(domain -> {
            if (domain instanceof String)
                skinDomains.add((String) domain);
        }));
        Optional<PublicKey> decodedPublickey;
        String publickeyString = response.optString("signaturePublickey");
        if (publickeyString == null) {
            decodedPublickey = empty();
        } else {
            try {
                decodedPublickey = of(loadX509PublicKey(decodePublicKey(publickeyString)));
            } catch (IllegalArgumentException | GeneralSecurityException e) {
                throw new IOException("Bad signature publickey", e);
            }
        }
        Map<String, String> meta = new TreeMap<>();
        ofNullable(response.optJSONObject("meta")).map(JSONObject::toMap).ifPresent(it -> it.forEach((k, v) -> meta.put(k, String.valueOf(v))));
        return new YggdrasilConfiguration(apiRoot, unmodifiableList(skinDomains), unmodifiableMap(meta), decodedPublickey);
    } catch (JSONException e) {
        throw new IOException("Invalid json", e);
    }
}
Also used : Optional.empty(java.util.Optional.empty) Collections.unmodifiableList(java.util.Collections.unmodifiableList) Optional.ofNullable(java.util.Optional.ofNullable) JSONException(org.to2mbn.authlibinjector.internal.org.json.JSONException) Optional.of(java.util.Optional.of) IOException(java.io.IOException) PublicKey(java.security.PublicKey) ArrayList(java.util.ArrayList) JSONObject(org.to2mbn.authlibinjector.internal.org.json.JSONObject) MessageFormat.format(java.text.MessageFormat.format) List(java.util.List) GeneralSecurityException(java.security.GeneralSecurityException) TreeMap(java.util.TreeMap) Objects.requireNonNull(java.util.Objects.requireNonNull) KeyUtils.loadX509PublicKey(org.to2mbn.authlibinjector.util.KeyUtils.loadX509PublicKey) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) Optional(java.util.Optional) KeyUtils.decodePublicKey(org.to2mbn.authlibinjector.util.KeyUtils.decodePublicKey) PublicKey(java.security.PublicKey) KeyUtils.loadX509PublicKey(org.to2mbn.authlibinjector.util.KeyUtils.loadX509PublicKey) KeyUtils.decodePublicKey(org.to2mbn.authlibinjector.util.KeyUtils.decodePublicKey) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) JSONException(org.to2mbn.authlibinjector.internal.org.json.JSONException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) JSONObject(org.to2mbn.authlibinjector.internal.org.json.JSONObject)

Example 2 with JSONException

use of org.to2mbn.authlibinjector.internal.org.json.JSONException in project authlib-injector by to2mbn.

the class DeprecatedApiHttpd method queryCharacterUUID.

private Optional<String> queryCharacterUUID(String username) throws UncheckedIOException, JSONException {
    String responseText;
    try {
        responseText = asString(postURL(configuration.getApiRoot() + "api/profiles/minecraft", CONTENT_TYPE_JSON, new JSONArray(new String[] { username }).toString().getBytes(UTF_8)));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    debug("[httpd] query uuid of username {0}, response: {1}", username, responseText);
    JSONArray response = new JSONArray(responseText);
    if (response.length() == 0) {
        return empty();
    } else if (response.length() == 1) {
        return of(response.getJSONObject(0).getString("id"));
    } else {
        throw new JSONException("Unexpected response length");
    }
}
Also used : JSONArray(org.to2mbn.authlibinjector.internal.org.json.JSONArray) JSONException(org.to2mbn.authlibinjector.internal.org.json.JSONException) UncheckedIOException(java.io.UncheckedIOException) IOUtils.asString(org.to2mbn.authlibinjector.util.IOUtils.asString) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 3 with JSONException

use of org.to2mbn.authlibinjector.internal.org.json.JSONException in project authlib-injector by to2mbn.

the class DeprecatedApiHttpd method processAsSkin.

private Optional<Response> processAsSkin(IHTTPSession session) {
    Matcher matcher = URL_SKINS.matcher(session.getUri());
    if (!matcher.find())
        return empty();
    String username = matcher.group("username");
    Optional<String> skinUrl;
    try {
        skinUrl = queryCharacterUUID(username).flatMap(uuid -> queryCharacterProperty(uuid, "textures")).map(encoded -> asString(Base64.getDecoder().decode(encoded))).flatMap(texturesPayload -> obtainTextureUrl(texturesPayload, "SKIN"));
    } catch (UncheckedIOException | JSONException e) {
        info("[httpd] unable to fetch skin for {0}: {1}", username, e);
        return of(newFixedLengthResponse(Status.INTERNAL_ERROR, null, null));
    }
    if (skinUrl.isPresent()) {
        String url = skinUrl.get();
        debug("[httpd] retrieving skin for {0} from {1}", username, url);
        byte[] data;
        try {
            data = getURL(url);
        } catch (IOException e) {
            info("[httpd] unable to retrieve skin from {0}: {1}", url, e);
            return of(newFixedLengthResponse(Status.NOT_FOUND, null, null));
        }
        info("[httpd] retrieved skin for {0} from {1}, {2} bytes", username, url, data.length);
        return of(newFixedLengthResponse(Status.OK, "image/png", new ByteArrayInputStream(data), data.length));
    } else {
        info("[httpd] no skin found for {0}", username);
        return of(newFixedLengthResponse(Status.NOT_FOUND, null, null));
    }
}
Also used : Optional.empty(java.util.Optional.empty) JSONArray(org.to2mbn.authlibinjector.internal.org.json.JSONArray) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOUtils.asString(org.to2mbn.authlibinjector.util.IOUtils.asString) JSONException(org.to2mbn.authlibinjector.internal.org.json.JSONException) Optional.of(java.util.Optional.of) IOException(java.io.IOException) YggdrasilConfiguration(org.to2mbn.authlibinjector.YggdrasilConfiguration) IOUtils.getURL(org.to2mbn.authlibinjector.util.IOUtils.getURL) IOUtils.postURL(org.to2mbn.authlibinjector.util.IOUtils.postURL) UncheckedIOException(java.io.UncheckedIOException) JSONObject(org.to2mbn.authlibinjector.internal.org.json.JSONObject) Base64(java.util.Base64) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) NanoHTTPD(fi.iki.elonen.NanoHTTPD) LoggingUtils.info(org.to2mbn.authlibinjector.util.LoggingUtils.info) LoggingUtils.debug(org.to2mbn.authlibinjector.util.LoggingUtils.debug) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Status(fi.iki.elonen.NanoHTTPD.Response.Status) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONException(org.to2mbn.authlibinjector.internal.org.json.JSONException) UncheckedIOException(java.io.UncheckedIOException) IOUtils.asString(org.to2mbn.authlibinjector.util.IOUtils.asString) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

IOException (java.io.IOException)3 JSONException (org.to2mbn.authlibinjector.internal.org.json.JSONException)3 UncheckedIOException (java.io.UncheckedIOException)2 Optional (java.util.Optional)2 Optional.empty (java.util.Optional.empty)2 Optional.of (java.util.Optional.of)2 JSONArray (org.to2mbn.authlibinjector.internal.org.json.JSONArray)2 JSONObject (org.to2mbn.authlibinjector.internal.org.json.JSONObject)2 IOUtils.asString (org.to2mbn.authlibinjector.util.IOUtils.asString)2 NanoHTTPD (fi.iki.elonen.NanoHTTPD)1 Status (fi.iki.elonen.NanoHTTPD.Response.Status)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 GeneralSecurityException (java.security.GeneralSecurityException)1 PublicKey (java.security.PublicKey)1 MessageFormat.format (java.text.MessageFormat.format)1 ArrayList (java.util.ArrayList)1 Base64 (java.util.Base64)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1