Search in sources :

Example 1 with JSONObject

use of org.to2mbn.authlibinjector.internal.org.json.JSONObject 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 JSONObject

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

the class DeprecatedApiHttpd method queryCharacterProperty.

private Optional<String> queryCharacterProperty(String uuid, String property) throws UncheckedIOException, JSONException {
    String responseText;
    try {
        responseText = asString(getURL(configuration.getApiRoot() + "sessionserver/session/minecraft/profile/" + uuid));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    if (responseText.isEmpty()) {
        debug("[httpd] query profile of {0}, not found", uuid);
        return empty();
    }
    debug("[httpd] query profile of {0}, response: {1}", uuid, responseText);
    JSONObject response = new JSONObject(responseText);
    for (Object element_ : response.getJSONArray("properties")) {
        JSONObject element = (JSONObject) element_;
        if (property.equals(element.getString("name"))) {
            return of(element.getString("value"));
        }
    }
    return empty();
}
Also used : JSONObject(org.to2mbn.authlibinjector.internal.org.json.JSONObject) UncheckedIOException(java.io.UncheckedIOException) JSONObject(org.to2mbn.authlibinjector.internal.org.json.JSONObject) IOUtils.asString(org.to2mbn.authlibinjector.util.IOUtils.asString) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

IOException (java.io.IOException)2 JSONObject (org.to2mbn.authlibinjector.internal.org.json.JSONObject)2 UncheckedIOException (java.io.UncheckedIOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 PublicKey (java.security.PublicKey)1 MessageFormat.format (java.text.MessageFormat.format)1 ArrayList (java.util.ArrayList)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 Optional.of (java.util.Optional.of)1 Optional.ofNullable (java.util.Optional.ofNullable)1 TreeMap (java.util.TreeMap)1 JSONException (org.to2mbn.authlibinjector.internal.org.json.JSONException)1 IOUtils.asString (org.to2mbn.authlibinjector.util.IOUtils.asString)1 KeyUtils.decodePublicKey (org.to2mbn.authlibinjector.util.KeyUtils.decodePublicKey)1