From 7e9da9fd7c965df344ffb6ac643a3c7a6d4b8aa4 Mon Sep 17 00:00:00 2001 From: games647 Date: Mon, 18 Jul 2022 10:58:29 +0200 Subject: [PATCH 1/5] Migrate to Java 8 (sponsored contribution) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Java >8 includes many helpful features and API additions. Current phoronix benchmarks indicated a very similar performance. Furthermore, the fact that Java 18 even works with 1.8.8 contributed to the decision to move to Java 17 like vanilla Java in 1.19. This also helps us to learn about the newer features added for our personal interests. As this is a free project, this motivated us to make this step. Nevertheless, many server owners were frustrated about this decision. Thanks to financial contribution, we revised this decision until Java 8 is end of life or no longer used actively used according to bstats.org 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 68095f6..81abad1 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 2e21ccb..4ce14ff 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 829d764..dc208ff 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 e375e29..619d41d 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 07ff623..e4ce55a 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 c2a2d1a..29b2f3d 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 e1b8bea..df3888e 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 4361fe9..ac0991e 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 ee62e24..9a75fc5 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 324e240..89dc7ee 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 ee58bde..26492d3 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 f2f3950..e1b26b0 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 + --- .../listener/protocollib/EncryptionUtil.java | 6 +- .../protocollib/ProtocolLibListener.java | 7 ++- .../protocollib/VerifyResponseTask.java | 1 + .../protocollib/packet/ClientPublicKey.java | 56 ++++++++++++++++++- .../fastlogin/bukkit/FastLoginBukkitTest.java | 4 +- .../listener/protocollib/Base64Adapter.java | 2 + .../protocollib/EncryptionUtilTest.java | 1 + .../listener/protocollib/ResourceLoader.java | 15 ++--- .../protocollib/SignatureTestData.java | 2 + .../games647/fastlogin/core/CommonUtil.java | 9 +-- .../fastlogin/core/StoredProfile.java | 3 +- pom.xml | 9 ++- 12 files changed, 92 insertions(+), 23 deletions(-) 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 + From dd2fca2ea1b3c65c2014865d469e5510217f6b55 Mon Sep 17 00:00:00 2001 From: games647 Date: Thu, 21 Jul 2022 11:18:26 +0200 Subject: [PATCH 2/5] Replace var with val to prevent conflicts with newer Java version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 81abad1..d148bc1 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 @@ -59,7 +59,7 @@ import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; -import lombok.var; +import lombok.val; /** * Encryption and decryption minecraft util for connection between servers @@ -179,9 +179,9 @@ final class EncryptionUtil { private static PublicKey loadMojangSessionKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { - var keyUrl = FastLoginBukkit.class.getClassLoader().getResource("yggdrasil_session_pubkey.der"); - var keyData = Resources.toByteArray(keyUrl); - var keySpec = new X509EncodedKeySpec(keyData); + val keyUrl = FastLoginBukkit.class.getClassLoader().getResource("yggdrasil_session_pubkey.der"); + val keyData = Resources.toByteArray(keyUrl); + val keySpec = new X509EncodedKeySpec(keyData); return KeyFactory.getInstance("RSA").generatePublic(keySpec); } 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 dc208ff..1363cb3 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,7 +60,7 @@ import java.util.UUID; import javax.crypto.Cipher; import javax.crypto.SecretKey; -import lombok.var; +import lombok.val; import org.bukkit.entity.Player; import static com.comphenix.protocol.PacketType.Login.Client.START; @@ -269,7 +269,7 @@ public class VerifyResponseTask implements Runnable { startPacket.getStrings().write(0, username); EquivalentConverter converter = BukkitConverters.getWrappedPublicKeyDataConverter(); - var wrappedKey = Optional.ofNullable(clientKey).map(key -> + val wrappedKey = Optional.ofNullable(clientKey).map(key -> new WrappedProfileKeyData(clientKey.expiry(), clientKey.key(), clientKey.signature()) ); 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 e4ce55a..9d6832e 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,7 +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 lombok.val; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -39,12 +39,12 @@ class FastLoginBukkitTest { @Test void testRGB() { - var message = "&x00002a00002b&lText"; - var msg = CommonUtil.translateColorCodes(message); + val message = "&x00002a00002b&lText"; + val msg = CommonUtil.translateColorCodes(message); assertEquals(msg, "§x00002a00002b§lText"); - var components = TextComponent.fromLegacyText(msg); - var expected = "{\"bold\":true,\"color\":\"#00a00b\",\"text\":\"Text\"}"; + val components = TextComponent.fromLegacyText(msg); + val 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 29b2f3d..c2fb34d 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,13 +32,13 @@ import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.util.Base64; -import lombok.var; +import lombok.val; public class Base64Adapter extends TypeAdapter { @Override public void write(JsonWriter out, byte[] value) throws IOException { - var encoded = Base64.getEncoder().encodeToString(value); + val encoded = Base64.getEncoder().encodeToString(value); out.value(encoded); } 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 df3888e..1bbe864 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,7 +50,7 @@ import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; -import lombok.var; +import lombok.val; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -61,7 +61,7 @@ class EncryptionUtilTest { @Test void testVerifyToken() { - var random = ThreadLocalRandom.current(); + val random = ThreadLocalRandom.current(); byte[] token = EncryptionUtil.generateVerifyToken(random); assertAll( @@ -89,10 +89,10 @@ class EncryptionUtilTest { @Test void testExpiredClientKey() throws Exception { - var clientKey = ResourceLoader.loadClientKey("client_keys/valid_public_key.json"); + val clientKey = ResourceLoader.loadClientKey("client_keys/valid_public_key.json"); // Client expires at the exact second mentioned, so use it for verification - var expiredTimestamp = clientKey.expiry(); + val expiredTimestamp = clientKey.expiry(); assertFalse(EncryptionUtil.verifyClientKey(clientKey, expiredTimestamp)); } @@ -106,7 +106,7 @@ class EncryptionUtilTest { "client_keys/invalid_wrong_signature.json" }) void testInvalidClientKey(String clientKeySource) throws Exception { - var clientKey = ResourceLoader.loadClientKey(clientKeySource); + val clientKey = ResourceLoader.loadClientKey(clientKeySource); Instant expireTimestamp = clientKey.expiry().minus(5, ChronoUnit.HOURS); assertFalse(EncryptionUtil.verifyClientKey(clientKey, expireTimestamp)); @@ -114,8 +114,8 @@ class EncryptionUtilTest { @Test void testValidClientKey() throws Exception { - var clientKey = ResourceLoader.loadClientKey("client_keys/valid_public_key.json"); - var verificationTimestamp = clientKey.expiry().minus(5, ChronoUnit.HOURS); + val clientKey = ResourceLoader.loadClientKey("client_keys/valid_public_key.json"); + val verificationTimestamp = clientKey.expiry().minus(5, ChronoUnit.HOURS); assertTrue(EncryptionUtil.verifyClientKey(clientKey, verificationTimestamp)); } @@ -123,7 +123,7 @@ class EncryptionUtilTest { @Test void testDecryptSharedSecret() throws Exception { KeyPair serverPair = EncryptionUtil.generateKeyPair(); - var serverPK = serverPair.getPublic(); + val serverPK = serverPair.getPublic(); SecretKey secretKey = generateSharedKey(); byte[] encryptedSecret = encrypt(serverPK, secretKey.getEncoded()); @@ -135,7 +135,7 @@ class EncryptionUtilTest { private static byte[] encrypt(PublicKey receiverKey, byte... message) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { - var encryptCipher = Cipher.getInstance(receiverKey.getAlgorithm()); + val encryptCipher = Cipher.getInstance(receiverKey.getAlgorithm()); encryptCipher.init(Cipher.ENCRYPT_MODE, receiverKey); return encryptCipher.doFinal(message); } @@ -151,9 +151,9 @@ class EncryptionUtilTest { @Test void testServerIdHash() throws Exception { - var serverId = ""; - var sharedSecret = generateSharedKey(); - var serverPK = ResourceLoader.loadClientKey("client_keys/valid_public_key.json").key(); + val serverId = ""; + val sharedSecret = generateSharedKey(); + val serverPK = ResourceLoader.loadClientKey("client_keys/valid_public_key.json").key(); String sessionHash = getServerHash(serverId, sharedSecret, serverPK); assertEquals(EncryptionUtil.getServerIdHashString(serverId, sharedSecret, serverPK), sessionHash); @@ -167,7 +167,7 @@ class EncryptionUtilTest { // sha1.update(server's encoded public key from Encryption Request) // hash := sha1.hexdigest() # String of hex characters @SuppressWarnings("deprecation") - var hasher = Hashing.sha1().newHasher(); + val hasher = Hashing.sha1().newHasher(); hasher.putString(serverId, StandardCharsets.US_ASCII); hasher.putBytes(sharedSecret.getEncoded()); hasher.putBytes(serverPK.getEncoded()); @@ -180,9 +180,9 @@ class EncryptionUtilTest { @Test void testServerIdHashWrongSecret() throws Exception { - var serverId = ""; - var sharedSecret = generateSharedKey(); - var serverPK = ResourceLoader.loadClientKey("client_keys/valid_public_key.json").key(); + val serverId = ""; + val sharedSecret = generateSharedKey(); + val serverPK = ResourceLoader.loadClientKey("client_keys/valid_public_key.json").key(); String sessionHash = getServerHash(serverId, sharedSecret, serverPK); assertNotEquals(EncryptionUtil.getServerIdHashString("", generateSharedKey(), serverPK), sessionHash); @@ -190,12 +190,12 @@ class EncryptionUtilTest { @Test void testServerIdHashWrongServerKey() { - var serverId = ""; - var sharedSecret = generateSharedKey(); - var serverPK = EncryptionUtil.generateKeyPair().getPublic(); + val serverId = ""; + val sharedSecret = generateSharedKey(); + val serverPK = EncryptionUtil.generateKeyPair().getPublic(); String sessionHash = getServerHash(serverId, sharedSecret, serverPK); - var wrongPK = EncryptionUtil.generateKeyPair().getPublic(); + val wrongPK = EncryptionUtil.generateKeyPair().getPublic(); assertNotEquals(EncryptionUtil.getServerIdHashString("", sharedSecret, wrongPK), sessionHash); } @@ -239,8 +239,8 @@ class EncryptionUtilTest { @Test void testNonce() throws Exception { byte[] expected = {1, 2, 3, 4}; - var serverKey = EncryptionUtil.generateKeyPair(); - var encryptedNonce = encrypt(serverKey.getPublic(), expected); + val serverKey = EncryptionUtil.generateKeyPair(); + val encryptedNonce = encrypt(serverKey.getPublic(), expected); assertTrue(EncryptionUtil.verifyNonce(expected, serverKey.getPrivate(), encryptedNonce)); } @@ -248,19 +248,19 @@ class EncryptionUtilTest { @Test void testNonceIncorrect() throws Exception { byte[] expected = {1, 2, 3, 4}; - var serverKey = EncryptionUtil.generateKeyPair(); + val serverKey = EncryptionUtil.generateKeyPair(); // flipped first character - var encryptedNonce = encrypt(serverKey.getPublic(), new byte[]{0, 2, 3, 4}); + val encryptedNonce = encrypt(serverKey.getPublic(), new byte[]{0, 2, 3, 4}); assertFalse(EncryptionUtil.verifyNonce(expected, serverKey.getPrivate(), encryptedNonce)); } @Test void testNonceFailedDecryption() throws Exception { byte[] expected = {1, 2, 3, 4}; - var serverKey = EncryptionUtil.generateKeyPair(); + val serverKey = EncryptionUtil.generateKeyPair(); // generate a new keypair that is different - var encryptedNonce = encrypt(EncryptionUtil.generateKeyPair().getPublic(), expected); + val encryptedNonce = encrypt(EncryptionUtil.generateKeyPair().getPublic(), expected); assertThrows(GeneralSecurityException.class, () -> EncryptionUtil.verifyNonce(expected, serverKey.getPrivate(), encryptedNonce) @@ -270,7 +270,7 @@ class EncryptionUtilTest { @Test void testNonceIncorrectEmpty() { byte[] expected = {1, 2, 3, 4}; - var serverKey = EncryptionUtil.generateKeyPair(); + val serverKey = EncryptionUtil.generateKeyPair(); byte[] encryptedNonce = {}; assertThrows(GeneralSecurityException.class, 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 9a75fc5..01b734e 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,13 +32,13 @@ import com.google.gson.annotations.JsonAdapter; import java.io.IOException; import java.nio.charset.StandardCharsets; -import lombok.var; +import lombok.val; public class SignatureTestData { public static SignatureTestData fromResource(String resourceName) throws IOException { - var keyUrl = Resources.getResource(resourceName); - var encodedSignature = Resources.toString(keyUrl, StandardCharsets.US_ASCII); + val keyUrl = Resources.getResource(resourceName); + val encodedSignature = Resources.toString(keyUrl, StandardCharsets.US_ASCII); return new Gson().fromJson(encodedSignature, SignatureTestData.class); } diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java index 148282c..c83c14d 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java @@ -57,6 +57,7 @@ import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.Server; +import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.PluginManager; import net.md_5.bungee.api.scheduler.GroupedThreadFactory; @@ -100,7 +101,7 @@ public class FastLoginBungee extends Plugin implements PlatformPlugin converter = BukkitConverters.getWrappedPublicKeyDataConverter(); - var wrappedKey = Optional.ofNullable(clientKey).map(key -> + val wrappedKey = Optional.ofNullable(clientKey).map(key -> new WrappedProfileKeyData(clientKey.expiry(), clientKey.key(), clientKey.signature()) ); 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 e4ce55a4..9d6832e5 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,7 +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 lombok.val; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -39,12 +39,12 @@ class FastLoginBukkitTest { @Test void testRGB() { - var message = "&x00002a00002b&lText"; - var msg = CommonUtil.translateColorCodes(message); + val message = "&x00002a00002b&lText"; + val msg = CommonUtil.translateColorCodes(message); assertEquals(msg, "§x00002a00002b§lText"); - var components = TextComponent.fromLegacyText(msg); - var expected = "{\"bold\":true,\"color\":\"#00a00b\",\"text\":\"Text\"}"; + val components = TextComponent.fromLegacyText(msg); + val 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 29b2f3d6..c2fb34df 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,13 +32,13 @@ import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.util.Base64; -import lombok.var; +import lombok.val; public class Base64Adapter extends TypeAdapter { @Override public void write(JsonWriter out, byte[] value) throws IOException { - var encoded = Base64.getEncoder().encodeToString(value); + val encoded = Base64.getEncoder().encodeToString(value); out.value(encoded); } 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 df3888ef..1bbe8642 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,7 +50,7 @@ import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; -import lombok.var; +import lombok.val; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -61,7 +61,7 @@ class EncryptionUtilTest { @Test void testVerifyToken() { - var random = ThreadLocalRandom.current(); + val random = ThreadLocalRandom.current(); byte[] token = EncryptionUtil.generateVerifyToken(random); assertAll( @@ -89,10 +89,10 @@ class EncryptionUtilTest { @Test void testExpiredClientKey() throws Exception { - var clientKey = ResourceLoader.loadClientKey("client_keys/valid_public_key.json"); + val clientKey = ResourceLoader.loadClientKey("client_keys/valid_public_key.json"); // Client expires at the exact second mentioned, so use it for verification - var expiredTimestamp = clientKey.expiry(); + val expiredTimestamp = clientKey.expiry(); assertFalse(EncryptionUtil.verifyClientKey(clientKey, expiredTimestamp)); } @@ -106,7 +106,7 @@ class EncryptionUtilTest { "client_keys/invalid_wrong_signature.json" }) void testInvalidClientKey(String clientKeySource) throws Exception { - var clientKey = ResourceLoader.loadClientKey(clientKeySource); + val clientKey = ResourceLoader.loadClientKey(clientKeySource); Instant expireTimestamp = clientKey.expiry().minus(5, ChronoUnit.HOURS); assertFalse(EncryptionUtil.verifyClientKey(clientKey, expireTimestamp)); @@ -114,8 +114,8 @@ class EncryptionUtilTest { @Test void testValidClientKey() throws Exception { - var clientKey = ResourceLoader.loadClientKey("client_keys/valid_public_key.json"); - var verificationTimestamp = clientKey.expiry().minus(5, ChronoUnit.HOURS); + val clientKey = ResourceLoader.loadClientKey("client_keys/valid_public_key.json"); + val verificationTimestamp = clientKey.expiry().minus(5, ChronoUnit.HOURS); assertTrue(EncryptionUtil.verifyClientKey(clientKey, verificationTimestamp)); } @@ -123,7 +123,7 @@ class EncryptionUtilTest { @Test void testDecryptSharedSecret() throws Exception { KeyPair serverPair = EncryptionUtil.generateKeyPair(); - var serverPK = serverPair.getPublic(); + val serverPK = serverPair.getPublic(); SecretKey secretKey = generateSharedKey(); byte[] encryptedSecret = encrypt(serverPK, secretKey.getEncoded()); @@ -135,7 +135,7 @@ class EncryptionUtilTest { private static byte[] encrypt(PublicKey receiverKey, byte... message) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { - var encryptCipher = Cipher.getInstance(receiverKey.getAlgorithm()); + val encryptCipher = Cipher.getInstance(receiverKey.getAlgorithm()); encryptCipher.init(Cipher.ENCRYPT_MODE, receiverKey); return encryptCipher.doFinal(message); } @@ -151,9 +151,9 @@ class EncryptionUtilTest { @Test void testServerIdHash() throws Exception { - var serverId = ""; - var sharedSecret = generateSharedKey(); - var serverPK = ResourceLoader.loadClientKey("client_keys/valid_public_key.json").key(); + val serverId = ""; + val sharedSecret = generateSharedKey(); + val serverPK = ResourceLoader.loadClientKey("client_keys/valid_public_key.json").key(); String sessionHash = getServerHash(serverId, sharedSecret, serverPK); assertEquals(EncryptionUtil.getServerIdHashString(serverId, sharedSecret, serverPK), sessionHash); @@ -167,7 +167,7 @@ class EncryptionUtilTest { // sha1.update(server's encoded public key from Encryption Request) // hash := sha1.hexdigest() # String of hex characters @SuppressWarnings("deprecation") - var hasher = Hashing.sha1().newHasher(); + val hasher = Hashing.sha1().newHasher(); hasher.putString(serverId, StandardCharsets.US_ASCII); hasher.putBytes(sharedSecret.getEncoded()); hasher.putBytes(serverPK.getEncoded()); @@ -180,9 +180,9 @@ class EncryptionUtilTest { @Test void testServerIdHashWrongSecret() throws Exception { - var serverId = ""; - var sharedSecret = generateSharedKey(); - var serverPK = ResourceLoader.loadClientKey("client_keys/valid_public_key.json").key(); + val serverId = ""; + val sharedSecret = generateSharedKey(); + val serverPK = ResourceLoader.loadClientKey("client_keys/valid_public_key.json").key(); String sessionHash = getServerHash(serverId, sharedSecret, serverPK); assertNotEquals(EncryptionUtil.getServerIdHashString("", generateSharedKey(), serverPK), sessionHash); @@ -190,12 +190,12 @@ class EncryptionUtilTest { @Test void testServerIdHashWrongServerKey() { - var serverId = ""; - var sharedSecret = generateSharedKey(); - var serverPK = EncryptionUtil.generateKeyPair().getPublic(); + val serverId = ""; + val sharedSecret = generateSharedKey(); + val serverPK = EncryptionUtil.generateKeyPair().getPublic(); String sessionHash = getServerHash(serverId, sharedSecret, serverPK); - var wrongPK = EncryptionUtil.generateKeyPair().getPublic(); + val wrongPK = EncryptionUtil.generateKeyPair().getPublic(); assertNotEquals(EncryptionUtil.getServerIdHashString("", sharedSecret, wrongPK), sessionHash); } @@ -239,8 +239,8 @@ class EncryptionUtilTest { @Test void testNonce() throws Exception { byte[] expected = {1, 2, 3, 4}; - var serverKey = EncryptionUtil.generateKeyPair(); - var encryptedNonce = encrypt(serverKey.getPublic(), expected); + val serverKey = EncryptionUtil.generateKeyPair(); + val encryptedNonce = encrypt(serverKey.getPublic(), expected); assertTrue(EncryptionUtil.verifyNonce(expected, serverKey.getPrivate(), encryptedNonce)); } @@ -248,19 +248,19 @@ class EncryptionUtilTest { @Test void testNonceIncorrect() throws Exception { byte[] expected = {1, 2, 3, 4}; - var serverKey = EncryptionUtil.generateKeyPair(); + val serverKey = EncryptionUtil.generateKeyPair(); // flipped first character - var encryptedNonce = encrypt(serverKey.getPublic(), new byte[]{0, 2, 3, 4}); + val encryptedNonce = encrypt(serverKey.getPublic(), new byte[]{0, 2, 3, 4}); assertFalse(EncryptionUtil.verifyNonce(expected, serverKey.getPrivate(), encryptedNonce)); } @Test void testNonceFailedDecryption() throws Exception { byte[] expected = {1, 2, 3, 4}; - var serverKey = EncryptionUtil.generateKeyPair(); + val serverKey = EncryptionUtil.generateKeyPair(); // generate a new keypair that is different - var encryptedNonce = encrypt(EncryptionUtil.generateKeyPair().getPublic(), expected); + val encryptedNonce = encrypt(EncryptionUtil.generateKeyPair().getPublic(), expected); assertThrows(GeneralSecurityException.class, () -> EncryptionUtil.verifyNonce(expected, serverKey.getPrivate(), encryptedNonce) @@ -270,7 +270,7 @@ class EncryptionUtilTest { @Test void testNonceIncorrectEmpty() { byte[] expected = {1, 2, 3, 4}; - var serverKey = EncryptionUtil.generateKeyPair(); + val serverKey = EncryptionUtil.generateKeyPair(); byte[] encryptedNonce = {}; assertThrows(GeneralSecurityException.class, 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 9a75fc56..01b734ea 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,13 +32,13 @@ import com.google.gson.annotations.JsonAdapter; import java.io.IOException; import java.nio.charset.StandardCharsets; -import lombok.var; +import lombok.val; public class SignatureTestData { public static SignatureTestData fromResource(String resourceName) throws IOException { - var keyUrl = Resources.getResource(resourceName); - var encodedSignature = Resources.toString(keyUrl, StandardCharsets.US_ASCII); + val keyUrl = Resources.getResource(resourceName); + val encodedSignature = Resources.toString(keyUrl, StandardCharsets.US_ASCII); return new Gson().fromJson(encodedSignature, SignatureTestData.class); } diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java index 148282c4..c83c14d0 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/FastLoginBungee.java @@ -57,6 +57,7 @@ import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.Server; +import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.PluginManager; import net.md_5.bungee.api.scheduler.GroupedThreadFactory; @@ -100,7 +101,7 @@ public class FastLoginBungee extends Plugin implements PlatformPlugin Date: Thu, 21 Jul 2022 11:19:43 +0200 Subject: [PATCH 3/5] Use Lombok to create a record-like struct --- .../protocollib/ProtocolLibListener.java | 2 +- .../protocollib/packet/ClientPublicKey.java | 62 +++---------------- .../listener/protocollib/ResourceLoader.java | 2 +- 3 files changed, 10 insertions(+), 56 deletions(-) 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 4ce14ff6..af526580 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 @@ -241,7 +241,7 @@ public class ProtocolLibListener extends PacketAdapter { Instant expires = profileKey.getExpireTime(); PublicKey key = profileKey.getKey(); byte[] signature = profileKey.getSignature(); - ClientPublicKey clientKey = new ClientPublicKey(expires, key, signature); + ClientPublicKey clientKey = ClientPublicKey.of(expires, key, signature); try { if (EncryptionUtil.verifyClientKey(clientKey, Instant.now())) { return Optional.of(clientKey); 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 619d41da..cab804ef 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,62 +27,16 @@ 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 final class ClientPublicKey { - private final Instant expiry; - private final PublicKey key; - private final byte[] signature; +import lombok.Value; +import lombok.experimental.Accessors; - 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(); - } +@Accessors(fluent = true) +@Value(staticConstructor = "of") +public class ClientPublicKey { + Instant expiry; + PublicKey key; + byte[] signature; public boolean isExpired(Instant verifyTimestamp) { return !verifyTimestamp.isBefore(expiry); 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 ac0991ec..2f6d7b3f 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 @@ -77,7 +77,7 @@ public class ResourceLoader { RSAPublicKey publicKey = parsePublicKey(key); byte[] signature = Base64.getDecoder().decode(object.getAsJsonPrimitive("signature").getAsString()); - return new ClientPublicKey(expires, publicKey, signature); + return ClientPublicKey.of(expires, publicKey, signature); } private static RSAPublicKey parsePublicKey(String keySpec) From 79a83ce84d839835d552aed8e7a42a4f044edf51 Mon Sep 17 00:00:00 2001 From: games647 Date: Thu, 21 Jul 2022 11:20:40 +0200 Subject: [PATCH 4/5] Throw exception for utilities --- .../bukkit/listener/protocollib/EncryptionUtil.java | 2 +- .../com/github/games647/fastlogin/core/CommonUtil.java | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) 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 d148bc17..d7b3d727 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 @@ -87,7 +87,7 @@ final class EncryptionUtil { } private EncryptionUtil() { - // utility + throw new RuntimeException("No instantiation of utility classes allowed"); } /** 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 89dc7ee2..d985086b 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 @@ -28,6 +28,7 @@ package com.github.games647.fastlogin.core; import com.google.common.cache.CacheBuilder; import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -71,7 +72,7 @@ public final class CommonUtil { * This creates a SLF4J logger. In the process it initializes the SLF4J service provider. This method looks * for the provider in the plugin jar instead of in the server jar when creating a Logger. The provider is only * initialized once, so this method should be called early. - * + *

* The provider is bound to the service class `SLF4JServiceProvider`. Relocating this class makes it available * for exclusive own usage. Other dependencies will use the relocated service too, and therefore will find the * initialized provider. @@ -95,7 +96,8 @@ public final class CommonUtil { Constructor cons = adapterClass.getDeclaredConstructor(java.util.logging.Logger.class); cons.setAccessible(true); return cons.newInstance(parent); - } catch (ReflectiveOperationException reflectEx) { + } catch (IllegalAccessException | InstantiationException | InvocationTargetException + | NoSuchMethodException reflectEx) { parent.log(Level.WARNING, "Cannot create slf4j logging adapter", reflectEx); parent.log(Level.WARNING, "Creating logger instance manually..."); return LoggerFactory.getLogger(parent.getName()); @@ -106,6 +108,6 @@ public final class CommonUtil { } private CommonUtil() { - //Utility class + throw new RuntimeException("No instantiation of utility classes allowed"); } } From bd65c792d0d80f09146226f10e66ae6ec9cce96b Mon Sep 17 00:00:00 2001 From: games647 Date: Thu, 21 Jul 2022 11:20:49 +0200 Subject: [PATCH 5/5] Use correct generics --- .../java/com/github/games647/fastlogin/core/CommonUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d985086b..b76399bf 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 @@ -53,7 +53,7 @@ public final class CommonUtil { builder.maximumSize(maxSize); } - return (ConcurrentMap) builder.build().asMap(); + return builder.build().asMap(); } public static String translateColorCodes(String rawMessage) {