diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtil.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtil.java index 68095f66..81abad1d 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtil.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtil.java @@ -50,7 +50,7 @@ import java.time.Instant; import java.util.Arrays; import java.util.Base64; import java.util.Base64.Encoder; -import java.util.random.RandomGenerator; +import java.util.Random; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; @@ -59,6 +59,8 @@ import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; +import lombok.var; + /** * Encryption and decryption minecraft util for connection between servers * and paid Minecraft account clients. @@ -112,7 +114,7 @@ final class EncryptionUtil { * @param random random generator * @return a token with 4 bytes long */ - public static byte[] generateVerifyToken(RandomGenerator random) { + public static byte[] generateVerifyToken(Random random) { byte[] token = new byte[VERIFY_TOKEN_LENGTH]; random.nextBytes(token); return token; diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ProtocolLibListener.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ProtocolLibListener.java index 2e21ccba..4ce14ff6 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ProtocolLibListener.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ProtocolLibListener.java @@ -56,6 +56,7 @@ import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; +import lombok.var; import org.bukkit.entity.Player; import static com.comphenix.protocol.PacketType.Login.Client.ENCRYPTION_BEGIN; @@ -171,7 +172,7 @@ public class ProtocolLibListener extends PacketAdapter { Either either = packet.getSpecificModifier(Either.class).read(0); if (clientPublicKey == null) { Optional left = either.left(); - if (left.isEmpty()) { + if (!left.isPresent()) { plugin.getLog().error("No verify token sent if requested without player signed key {}", sender); return false; } @@ -179,7 +180,7 @@ public class ProtocolLibListener extends PacketAdapter { return EncryptionUtil.verifyNonce(expectedToken, keyPair.getPrivate(), left.get()); } else { Optional optSignatureData = either.right(); - if (optSignatureData.isEmpty()) { + if (!optSignatureData.isPresent()) { plugin.getLog().error("No signature given to sent player signing key {}", sender); return false; } @@ -219,7 +220,7 @@ public class ProtocolLibListener extends PacketAdapter { .optionRead(0); var clientKey = profileKey.flatMap(opt -> opt).flatMap(this::verifyPublicKey); - if (verifyClientKeys && clientKey.isEmpty()) { + if (verifyClientKeys && !clientKey.isPresent()) { // missing or incorrect // expired always not allowed player.kickPlayer(plugin.getCore().getMessage("invalid-public-key")); diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTask.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTask.java index 829d764c..dc208ff7 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTask.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/VerifyResponseTask.java @@ -60,6 +60,7 @@ import java.util.UUID; import javax.crypto.Cipher; import javax.crypto.SecretKey; +import lombok.var; import org.bukkit.entity.Player; import static com.comphenix.protocol.PacketType.Login.Client.START; diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/packet/ClientPublicKey.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/packet/ClientPublicKey.java index e375e294..619d41da 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/packet/ClientPublicKey.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/packet/ClientPublicKey.java @@ -27,8 +27,62 @@ package com.github.games647.fastlogin.bukkit.listener.protocollib.packet; import java.security.PublicKey; import java.time.Instant; +import java.util.Arrays; +import java.util.Objects; +import java.util.StringJoiner; -public record ClientPublicKey(Instant expiry, PublicKey key, byte[] signature) { +public final class ClientPublicKey { + private final Instant expiry; + private final PublicKey key; + private final byte[] signature; + + public ClientPublicKey(Instant expiry, PublicKey key, byte[] signature) { + this.expiry = expiry; + this.key = key; + this.signature = signature; + } + + public Instant expiry() { + return expiry; + } + + public PublicKey key() { + return key; + } + + public byte[] signature() { + return signature; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + + ClientPublicKey that = (ClientPublicKey) obj; + return Objects.equals(this.expiry, that.expiry) + && Objects.equals(this.key, that.key) + && Arrays.equals(this.signature, that.signature); + } + + @Override + public int hashCode() { + return Objects.hash(expiry, key, signature); + } + + @Override + public String toString() { + return new StringJoiner(", ", ClientPublicKey.class.getSimpleName() + "[", "]") + .add("expiry=" + expiry) + .add("key=" + key) + .add("signature=" + Arrays.toString(signature)) + .toString(); + } public boolean isExpired(Instant verifyTimestamp) { return !verifyTimestamp.isBefore(expiry); diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/FastLoginBukkitTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/FastLoginBukkitTest.java index 07ff6232..e4ce55a4 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/FastLoginBukkitTest.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/FastLoginBukkitTest.java @@ -30,6 +30,7 @@ import com.github.games647.fastlogin.core.CommonUtil; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.chat.ComponentSerializer; +import lombok.var; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -43,8 +44,7 @@ class FastLoginBukkitTest { assertEquals(msg, "§x00002a00002b§lText"); var components = TextComponent.fromLegacyText(msg); - var expected = """ - {"bold":true,"color":"#00a00b","text":"Text"}"""; + var expected = "{\"bold\":true,\"color\":\"#00a00b\",\"text\":\"Text\"}"; assertEquals(ComponentSerializer.toString(components), expected); } } diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/Base64Adapter.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/Base64Adapter.java index c2a2d1a4..29b2f3d6 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/Base64Adapter.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/Base64Adapter.java @@ -32,6 +32,8 @@ import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.util.Base64; +import lombok.var; + public class Base64Adapter extends TypeAdapter { @Override diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtilTest.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtilTest.java index e1b8bea2..df3888ef 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtilTest.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtilTest.java @@ -50,6 +50,7 @@ import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; +import lombok.var; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ResourceLoader.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ResourceLoader.java index 4361fe9b..ac0991ec 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ResourceLoader.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/ResourceLoader.java @@ -33,6 +33,7 @@ import com.google.gson.JsonObject; import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; @@ -57,19 +58,19 @@ public class ResourceLoader { ) { PemObject pemObject = pemReader.readPemObject(); byte[] content = pemObject.getContent(); - var privateKeySpec = new PKCS8EncodedKeySpec(content); + PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(content); - var factory = KeyFactory.getInstance("RSA"); + KeyFactory factory = KeyFactory.getInstance("RSA"); return (RSAPrivateKey) factory.generatePrivate(privateKeySpec); } } protected static ClientPublicKey loadClientKey(String path) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { - var keyUrl = Resources.getResource(path); + URL keyUrl = Resources.getResource(path); - var lines = Resources.toString(keyUrl, StandardCharsets.US_ASCII); - var object = new Gson().fromJson(lines, JsonObject.class); + String lines = Resources.toString(keyUrl, StandardCharsets.US_ASCII); + JsonObject object = new Gson().fromJson(lines, JsonObject.class); Instant expires = Instant.parse(object.getAsJsonPrimitive("expires_at").getAsString()); String key = object.getAsJsonPrimitive("key").getAsString(); @@ -87,9 +88,9 @@ public class ResourceLoader { ) { PemObject pemObject = pemReader.readPemObject(); byte[] content = pemObject.getContent(); - var pubKeySpec = new X509EncodedKeySpec(content); + X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(content); - var factory = KeyFactory.getInstance("RSA"); + KeyFactory factory = KeyFactory.getInstance("RSA"); return (RSAPublicKey) factory.generatePublic(pubKeySpec); } } diff --git a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/SignatureTestData.java b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/SignatureTestData.java index ee62e242..9a75fc56 100644 --- a/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/SignatureTestData.java +++ b/bukkit/src/test/java/com/github/games647/fastlogin/bukkit/listener/protocollib/SignatureTestData.java @@ -32,6 +32,8 @@ import com.google.gson.annotations.JsonAdapter; import java.io.IOException; import java.nio.charset.StandardCharsets; +import lombok.var; + public class SignatureTestData { public static SignatureTestData fromResource(String resourceName) throws IOException { diff --git a/core/src/main/java/com/github/games647/fastlogin/core/CommonUtil.java b/core/src/main/java/com/github/games647/fastlogin/core/CommonUtil.java index 324e240e..89dc7ee2 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/CommonUtil.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/CommonUtil.java @@ -25,8 +25,7 @@ */ package com.github.games647.fastlogin.core; -import com.github.games647.craftapi.cache.SafeCacheBuilder; -import com.google.common.cache.CacheLoader; +import com.google.common.cache.CacheBuilder; import java.lang.reflect.Constructor; import java.util.concurrent.ConcurrentMap; @@ -43,7 +42,7 @@ public final class CommonUtil { private static final char TRANSLATED_CHAR = '§'; public static ConcurrentMap buildCache(int expireAfterWrite, int maxSize) { - SafeCacheBuilder builder = SafeCacheBuilder.newBuilder(); + CacheBuilder builder = CacheBuilder.newBuilder(); if (expireAfterWrite > 0) { builder.expireAfterWrite(expireAfterWrite, TimeUnit.MINUTES); @@ -53,9 +52,7 @@ public final class CommonUtil { builder.maximumSize(maxSize); } - return builder.build(CacheLoader.from(() -> { - throw new UnsupportedOperationException(); - })); + return (ConcurrentMap) builder.build().asMap(); } public static String translateColorCodes(String rawMessage) { diff --git a/core/src/main/java/com/github/games647/fastlogin/core/StoredProfile.java b/core/src/main/java/com/github/games647/fastlogin/core/StoredProfile.java index ee58bdeb..26492d31 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/StoredProfile.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/StoredProfile.java @@ -118,10 +118,11 @@ public class StoredProfile extends Profile { return true; } - if (!(o instanceof StoredProfile that)) { + if (!(o instanceof StoredProfile)) { return false; } + StoredProfile that = (StoredProfile) o; if (!super.equals(o)) { return false; } diff --git a/pom.xml b/pom.xml index f2f3950b..e1b26b06 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,7 @@ Unknown - 17 + 8 ${java.version} ${java.version} @@ -189,5 +189,12 @@ 5.8.2 test + + + org.projectlombok + lombok + 1.18.24 + provided +