Search in sources :

Example 1 with PlayStatusPacket

use of com.nukkitx.protocol.bedrock.packet.PlayStatusPacket in project ProxyPass by CloudburstMC.

the class UpstreamPacketHandler method handle.

@Override
public boolean handle(LoginPacket packet) {
    int protocolVersion = packet.getProtocolVersion();
    if (protocolVersion != ProxyPass.PROTOCOL_VERSION) {
        PlayStatusPacket status = new PlayStatusPacket();
        if (protocolVersion > ProxyPass.PROTOCOL_VERSION) {
            status.setStatus(PlayStatusPacket.Status.LOGIN_FAILED_SERVER_OLD);
        } else {
            status.setStatus(PlayStatusPacket.Status.LOGIN_FAILED_CLIENT_OLD);
        }
    }
    session.setPacketCodec(ProxyPass.CODEC);
    JsonNode certData;
    try {
        certData = ProxyPass.JSON_MAPPER.readTree(packet.getChainData().toByteArray());
    } catch (IOException e) {
        throw new RuntimeException("Certificate JSON can not be read.");
    }
    JsonNode certChainData = certData.get("chain");
    if (certChainData.getNodeType() != JsonNodeType.ARRAY) {
        throw new RuntimeException("Certificate data is not valid");
    }
    chainData = (ArrayNode) certChainData;
    boolean validChain;
    try {
        validChain = validateChainData(certChainData);
        log.debug("Is player data valid? {}", validChain);
        JWSObject jwt = JWSObject.parse(certChainData.get(certChainData.size() - 1).asText());
        JsonNode payload = ProxyPass.JSON_MAPPER.readTree(jwt.getPayload().toBytes());
        if (payload.get("extraData").getNodeType() != JsonNodeType.OBJECT) {
            throw new RuntimeException("AuthData was not found!");
        }
        extraData = (JSONObject) jwt.getPayload().toJSONObject().get("extraData");
        this.authData = new AuthData(extraData.getAsString("displayName"), UUID.fromString(extraData.getAsString("identity")), extraData.getAsString("XUID"));
        if (payload.get("identityPublicKey").getNodeType() != JsonNodeType.STRING) {
            throw new RuntimeException("Identity Public Key was not found!");
        }
        ECPublicKey identityPublicKey = EncryptionUtils.generateKey(payload.get("identityPublicKey").textValue());
        JWSObject clientJwt = JWSObject.parse(packet.getSkinData().toString());
        verifyJwt(clientJwt, identityPublicKey);
        skinData = new JSONObject(clientJwt.getPayload().toJSONObject());
        initializeProxySession();
    } catch (Exception e) {
        session.disconnect("disconnectionScreen.internalError.cantConnect");
        throw new RuntimeException("Unable to complete login", e);
    }
    return true;
}
Also used : PlayStatusPacket(com.nukkitx.protocol.bedrock.packet.PlayStatusPacket) ECPublicKey(java.security.interfaces.ECPublicKey) JSONObject(com.nimbusds.jose.shaded.json.JSONObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JWSObject(com.nimbusds.jose.JWSObject) JOSEException(com.nimbusds.jose.JOSEException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JOSEException (com.nimbusds.jose.JOSEException)1 JWSObject (com.nimbusds.jose.JWSObject)1 JSONObject (com.nimbusds.jose.shaded.json.JSONObject)1 PlayStatusPacket (com.nukkitx.protocol.bedrock.packet.PlayStatusPacket)1 IOException (java.io.IOException)1 ECPublicKey (java.security.interfaces.ECPublicKey)1