Thermos supports GSON so we could share the json parsing

This commit is contained in:
games647 committed 2017-09-22 18:17:35 +02:00
1 parent 109508dae6
commit bb80521ab6
12 files changed
+103 -101

No files matched your search

@@ -9,7 +9,7 @@ import com.github.games647.fastlogin.bukkit.listener.protocollib.ProtocolLibList
import com.github.games647.fastlogin.bukkit.listener.protocolsupport.ProtocolSupportListener;
import com.github.games647.fastlogin.bukkit.tasks.DelayedAuthHook;
import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.github.games647.fastlogin.core.shared.MojangApiConnector;
import com.github.games647.fastlogin.core.mojang.MojangApiConnector;
import com.github.games647.fastlogin.core.shared.PlatformPlugin;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteArrayDataOutput;
@@ -1,8 +1,10 @@
package com.github.games647.fastlogin.bukkit;
import com.github.games647.fastlogin.core.mojang.MojangApiConnector;
import com.github.games647.fastlogin.core.mojang.SkinProperties;
import com.github.games647.fastlogin.core.mojang.VerificationReply;
import com.github.games647.fastlogin.core.shared.FastLoginCore;
import com.github.games647.fastlogin.core.shared.LoginSession;
import com.github.games647.fastlogin.core.shared.MojangApiConnector;
import java.io.BufferedReader;
import java.io.InputStreamReader;
@@ -14,10 +16,6 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class MojangApiBukkit extends MojangApiConnector {
//mojang api check to prove a player is logged in minecraft and made a join server request
@@ -42,26 +40,23 @@ public class MojangApiBukkit extends MojangApiConnector {
HttpURLConnection conn = getConnection(url);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
String line = reader.readLine();
if (line != null && !"null".equals(line)) {
//validate parsing
//http://wiki.vg/Protocol_Encryption#Server
JSONObject userData = (JSONObject) JSONValue.parseWithException(line);
String uuid = (String) userData.get("id");
playerSession.setUuid(FastLoginCore.parseId(uuid));
//validate parsing
//http://wiki.vg/Protocol_Encryption#Server
VerificationReply verification = gson.fromJson(reader, VerificationReply.class);
JSONArray properties = (JSONArray) userData.get("properties");
JSONObject skinProperty = (JSONObject) properties.get(0);
String uuid = verification.getId();
playerSession.setUuid(FastLoginCore.parseId(uuid));
String propertyName = (String) skinProperty.get("name");
if ("textures".equals(propertyName)) {
String skinValue = (String) skinProperty.get("value");
String signature = (String) skinProperty.get("signature");
playerSession.setSkin(skinValue, signature);
}
SkinProperties[] properties = verification.getProperties();
if (properties != null && properties.length > 0) {
SkinProperties skinProperty = properties[0];
return true;
String skinValue = skinProperty.getValue();
String signature = skinProperty.getSignature();
playerSession.setSkin(skinValue, signature);
}
return true;
}
} catch (Exception ex) {
//catch not only io-exceptions also parse and NPE on unexpected json format
@@ -71,24 +66,4 @@ public class MojangApiBukkit extends MojangApiConnector {
//this connection doesn't need to be closed. So can make use of keep alive in java
return false;
}
@Override
protected String getUUIDFromJson(String json) {
boolean isArray = json.startsWith("[");
JSONObject mojangPlayer;
if (isArray) {
JSONArray array = (JSONArray) JSONValue.parse(json);
mojangPlayer = (JSONObject) array.get(0);
} else {
mojangPlayer = (JSONObject) JSONValue.parse(json);
}
String uuid = (String) mojangPlayer.get("id");
if ("null".equals(uuid)) {
return null;
}
return uuid;
}
}
@@ -15,7 +15,7 @@ public class EncryptionUtilTest {
byte[] token = EncryptionUtil.generateVerifyToken(random);
assertNotNull(token);
assertEquals(token.length, 4);
assertEquals(4, token.length);
}
// @Test