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);
}
}
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");
}
}
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));
}
}
Aggregations