From 201b6826ed88771e7aa1ce911e7d3b80f5be2ed1 Mon Sep 17 00:00:00 2001 From: Gravita Date: Tue, 15 Jun 2021 18:24:29 +0700 Subject: [PATCH] [FEATURE] PasswordVerifier encrypt support --- .../auth/core/JsonCoreProvider.java | 7 ++-- .../auth/password/AcceptPasswordVerifier.java | 8 +++++ .../auth/password/DigestPasswordVerifier.java | 18 ++++++++-- .../DoubleDigestPasswordVerifier.java | 22 +++++++++--- .../auth/password/JsonPasswordVerifier.java | 34 +++++++++++++++++++ .../auth/password/PasswordVerifier.java | 7 ++++ .../auth/password/PlainPasswordVerifier.java | 5 +++ .../auth/password/RejectPasswordVerifier.java | 8 +++++ 8 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/AcceptPasswordVerifier.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/JsonPasswordVerifier.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/RejectPasswordVerifier.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/JsonCoreProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/JsonCoreProvider.java index bdb2fc63..f6852be1 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/JsonCoreProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/JsonCoreProvider.java @@ -27,7 +27,7 @@ import java.util.UUID; public class JsonCoreProvider extends AuthCoreProvider { - private transient final Logger logger = LogManager.getLogger(); + private static transient final Logger logger = LogManager.getLogger(); public String getUserByUsernameUrl; public String getUserByLoginUrl; public String getUserByUUIDUrl; @@ -302,8 +302,11 @@ public long getExpireIn() { } } - public R jsonRequest(T request, String url, Class clazz) { + return jsonRequest(request, url, bearerToken, clazz, client); + } + + public static R jsonRequest(T request, String url, String bearerToken, Class clazz, HttpClient client) { HttpRequest.BodyPublisher publisher; if (request != null) { publisher = HttpRequest.BodyPublishers.ofString(request.toString()); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/AcceptPasswordVerifier.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/AcceptPasswordVerifier.java new file mode 100644 index 00000000..18e56713 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/AcceptPasswordVerifier.java @@ -0,0 +1,8 @@ +package pro.gravit.launchserver.auth.password; + +public class AcceptPasswordVerifier extends PasswordVerifier { + @Override + public boolean check(String encryptedPassword, String password) { + return true; + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/DigestPasswordVerifier.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/DigestPasswordVerifier.java index abca8a42..a5546763 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/DigestPasswordVerifier.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/DigestPasswordVerifier.java @@ -13,15 +13,29 @@ public class DigestPasswordVerifier extends PasswordVerifier { private transient final Logger logger = LogManager.getLogger(); public String algo; + private byte[] digest(String text) throws NoSuchAlgorithmException { + MessageDigest digest = MessageDigest.getInstance(algo); + return digest.digest(text.getBytes(StandardCharsets.UTF_8)); + } + @Override public boolean check(String encryptedPassword, String password) { try { - MessageDigest digest = MessageDigest.getInstance(algo); byte[] bytes = SecurityHelper.fromHex(encryptedPassword); - return Arrays.equals(bytes, digest.digest(password.getBytes(StandardCharsets.UTF_8))); + return Arrays.equals(bytes, digest(password)); } catch (NoSuchAlgorithmException e) { logger.error("Digest algorithm {} not supported", algo); return false; } } + + @Override + public String encrypt(String password) { + try { + return SecurityHelper.toHex(digest(password)); + } catch (NoSuchAlgorithmException e) { + logger.error("Digest algorithm {} not supported", algo); + return null; + } + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/DoubleDigestPasswordVerifier.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/DoubleDigestPasswordVerifier.java index 071664fd..60262a67 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/DoubleDigestPasswordVerifier.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/DoubleDigestPasswordVerifier.java @@ -14,16 +14,30 @@ public class DoubleDigestPasswordVerifier extends PasswordVerifier { public String algo; public boolean toHexMode; + private byte[] digest(String text) throws NoSuchAlgorithmException { + MessageDigest digest = MessageDigest.getInstance(algo); + byte[] firstDigest = digest.digest(); + return toHexMode ? digest.digest(SecurityHelper.toHex(firstDigest).getBytes(StandardCharsets.UTF_8)) : digest.digest(firstDigest); + } + @Override public boolean check(String encryptedPassword, String password) { try { - MessageDigest digest = MessageDigest.getInstance(algo); - byte[] bytes = SecurityHelper.fromHex(password); - byte[] firstDigest = digest.digest(bytes); - return Arrays.equals(encryptedPassword.getBytes(StandardCharsets.UTF_8), toHexMode ? digest.digest(SecurityHelper.toHex(firstDigest).getBytes(StandardCharsets.UTF_8)) : digest.digest(firstDigest)); + byte[] bytes = SecurityHelper.fromHex(encryptedPassword); + return Arrays.equals(bytes, digest(password)); } catch (NoSuchAlgorithmException e) { logger.error("Digest algorithm {} not supported", algo); return false; } } + + @Override + public String encrypt(String password) { + try { + return SecurityHelper.toHex(digest(password)); + } catch (NoSuchAlgorithmException e) { + logger.error("Digest algorithm {} not supported", algo); + return null; + } + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/JsonPasswordVerifier.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/JsonPasswordVerifier.java new file mode 100644 index 00000000..8bab6e45 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/JsonPasswordVerifier.java @@ -0,0 +1,34 @@ +package pro.gravit.launchserver.auth.password; + +import pro.gravit.launchserver.auth.core.JsonCoreProvider; + +import java.net.http.HttpClient; + +public class JsonPasswordVerifier extends PasswordVerifier { + public String url; + public String bearerToken; + private transient HttpClient client = HttpClient.newBuilder().build(); + + public static class JsonPasswordRequest { + public String encryptedPassword; + public String password; + + public JsonPasswordRequest(String encryptedPassword, String password) { + this.encryptedPassword = encryptedPassword; + this.password = password; + } + } + + public static class JsonPasswordResponse { + public boolean success; + } + + @Override + public boolean check(String encryptedPassword, String password) { + JsonPasswordResponse response = JsonCoreProvider.jsonRequest(new JsonPasswordRequest(encryptedPassword, password), url, bearerToken, JsonPasswordResponse.class, client); + if (response != null) { + return response.success; + } + return false; + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/PasswordVerifier.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/PasswordVerifier.java index 08f4ac5a..30c352e9 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/PasswordVerifier.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/PasswordVerifier.java @@ -11,9 +11,16 @@ public static void registerProviders() { providers.register("plain", PlainPasswordVerifier.class); providers.register("digest", DigestPasswordVerifier.class); providers.register("doubleDigest", DoubleDigestPasswordVerifier.class); + providers.register("json", JsonPasswordVerifier.class); + providers.register("accept", AcceptPasswordVerifier.class); + providers.register("reject", RejectPasswordVerifier.class); registeredProviders = true; } } public abstract boolean check(String encryptedPassword, String password); + + public String encrypt(String password) { + throw new UnsupportedOperationException(); + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/PlainPasswordVerifier.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/PlainPasswordVerifier.java index 5983c75e..5eabd765 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/PlainPasswordVerifier.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/PlainPasswordVerifier.java @@ -5,4 +5,9 @@ public class PlainPasswordVerifier extends PasswordVerifier { public boolean check(String encryptedPassword, String password) { return encryptedPassword.equals(password); } + + @Override + public String encrypt(String password) { + return super.encrypt(password); + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/RejectPasswordVerifier.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/RejectPasswordVerifier.java new file mode 100644 index 00000000..5058259c --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/password/RejectPasswordVerifier.java @@ -0,0 +1,8 @@ +package pro.gravit.launchserver.auth.password; + +public class RejectPasswordVerifier extends PasswordVerifier { + @Override + public boolean check(String encryptedPassword, String password) { + return false; + } +}