From 5bb6842ba86a2b8eb42ccd5fd821f7375c07b198 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Thu, 18 May 2023 12:36:33 +0800 Subject: [PATCH] =?UTF-8?q?build:=20otp=20=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/framework/sdk/pom.xml | 7 +- .../framework/sdk/src/test/java/TestTotp.java | 90 +++++++++++++++++++ pom.xml | 1 + 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 backend/framework/sdk/src/test/java/TestTotp.java diff --git a/backend/framework/sdk/pom.xml b/backend/framework/sdk/pom.xml index 51f9c3180f..b1b225e032 100644 --- a/backend/framework/sdk/pom.xml +++ b/backend/framework/sdk/pom.xml @@ -418,8 +418,13 @@ org.eclipse.jgit ${jgit.version} + + + com.github.bastiaanjansen + otp-java + ${otp-java.version} + - diff --git a/backend/framework/sdk/src/test/java/TestTotp.java b/backend/framework/sdk/src/test/java/TestTotp.java new file mode 100644 index 0000000000..eac62cd31f --- /dev/null +++ b/backend/framework/sdk/src/test/java/TestTotp.java @@ -0,0 +1,90 @@ +import com.bastiaanjansen.otp.HMACAlgorithm; +import com.bastiaanjansen.otp.HOTPGenerator; +import com.bastiaanjansen.otp.SecretGenerator; +import com.bastiaanjansen.otp.TOTPGenerator; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import java.time.Duration; +import java.util.UUID; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class TestTotp { + public static String code; + // Generate a secret (or use your own secret) +// private static final byte[] secret = SecretGenerator.generate(256); + private static final byte[] secret = UUID.randomUUID().toString().getBytes(); + + @Test + @Order(1) + public void testGenerateTotp() { + + + TOTPGenerator totpGenerator = new TOTPGenerator.Builder(secret) + .withHOTPGenerator(builder -> { + builder.withPasswordLength(6); + builder.withAlgorithm(HMACAlgorithm.SHA256); // SHA256 and SHA512 are also supported + }) + .withPeriod(Duration.ofSeconds(60)) + .build(); + + + try { + code = totpGenerator.now(); + System.out.println(code); + + } catch (IllegalStateException e) { + // Handle error + } + } + + @Test + @Order(2) + public void testVerifyTotp() { + + TOTPGenerator totpGenerator = new TOTPGenerator.Builder(secret) + .withHOTPGenerator(builder -> { + builder.withPasswordLength(6); + builder.withAlgorithm(HMACAlgorithm.SHA256); // SHA256 and SHA512 are also supported + }) + .withPeriod(Duration.ofSeconds(60)) + .build(); + + + try { + + // To verify a token: + boolean isValid = totpGenerator.verify(code); + System.out.println(isValid); + } catch (IllegalStateException e) { + // Handle error + } + } + + @Test + @Order(3) + public void testGenerateHotp() { + + HOTPGenerator hotp = new HOTPGenerator.Builder(secret) + .withAlgorithm(HMACAlgorithm.SHA256) // SHA256 and SHA512 are also supported + .withPasswordLength(6) + .build(); + code = hotp.generate(1);// 1 is the counter value + System.out.println(code); + } + + @Test + @Order(4) + public void testVerifyHotp() { + + HOTPGenerator hotp = new HOTPGenerator.Builder(secret) + .withAlgorithm(HMACAlgorithm.SHA256) // SHA256 and SHA512 are also supported + .withPasswordLength(6) + .build(); + + boolean verify = hotp.verify(code, 1); + System.out.println(verify); + } +} diff --git a/pom.xml b/pom.xml index c38aa0d08a..523a272f7a 100644 --- a/pom.xml +++ b/pom.xml @@ -86,6 +86,7 @@ 5.4.0 22.3.1 3.0.0-RC5 + 2.0.1 1.12.1 v16.10.0