build: otp 测试

This commit is contained in:
CaptainB 2023-05-18 12:36:33 +08:00 committed by 刘瑞斌
parent 3000e68c4f
commit 5bb6842ba8
3 changed files with 97 additions and 1 deletions

View File

@ -418,8 +418,13 @@
<artifactId>org.eclipse.jgit</artifactId>
<version>${jgit.version}</version>
</dependency>
<!-- otp -->
<dependency>
<groupId>com.github.bastiaanjansen</groupId>
<artifactId>otp-java</artifactId>
<version>${otp-java.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}

View File

@ -86,6 +86,7 @@
<curator.version>5.4.0</curator.version>
<graalvmjs.version>22.3.1</graalvmjs.version>
<embedded.version>3.0.0-RC5</embedded.version>
<otp-java.version>2.0.1</otp-java.version>
<!-- frontend -->
<frontend-maven-plugin.version>1.12.1</frontend-maven-plugin.version>
<node.version>v16.10.0</node.version>