Improved API, added checks, added tests; doesn't currently run tho

This commit is contained in:
TheArrayser 2023-06-30 23:04:05 +02:00
parent a2af6825f0
commit d64ab8e17f
4 changed files with 13 additions and 7 deletions

View File

@ -45,10 +45,10 @@ public abstract class OTP {
* @param counter REQUIRED if HOTP, otherwise meaningless. The initial counter for HOTP (eg 21, if user reloaded counter 21 times)
* @param periodInSeconds OPTIONAL (default 30) for TOTP, the refresh rate of the TOTP
* @param checksum OPTIONAL: appends a checksum digit to the end of the string
* @return returns a valid OTP object or null, which can be used to create a new OTP pin
* @return returns a valid OTP object, which can be used to create a new OTP pin
*/
public static OTP createNewOTP(OTPType type, String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) throws OTPException{
if(type == null) return null;
if(type == null) throw new OTPException("No OTP Type given!");
return type.instance(secret, algorithm, digits, counter, periodInSeconds, checksum);
}

View File

@ -1,6 +1,6 @@
package com.cringe_studios.cringe_authenticator_library;
public class OTPException extends RuntimeException {
public class OTPException extends Exception {
private static final long serialVersionUID = 4635395862998258574L;

View File

@ -16,15 +16,15 @@ public enum OTPType {
return friendlyName;
}
public OTP instance(String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) {
public OTP instance(String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) throws OTPException {
if(/*type == null ||*/ secret == null || secret.length() == 0/*|| counter < 0*/) {
return null;
throw new OTPException("Invalid Secret!");
}
try {
Base32.decode(secret);
}catch (IllegalArgumentException e) {
throw new OTPException("Your secret is invalid! Please rescan the code!", e);
throw new OTPException("Invalid Secret!", e);
}
switch(this) {
@ -32,7 +32,7 @@ public enum OTPType {
return new com.cringe_studios.cringe_authenticator_library.impl.HOTP(secret, algorithm, digits, counter, periodInSeconds, checksum);
case TOTP:
return new com.cringe_studios.cringe_authenticator_library.impl.TOTP(secret, algorithm, digits, counter, periodInSeconds, checksum);
default: return null;
default: throw new OTPException("No OTP Type given!"); //Respect, if you run this code
}
}
}

View File

@ -2,6 +2,7 @@ package test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.math.BigInteger;
@ -64,6 +65,11 @@ public class OTPTest {
OTP testOTP4 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA512, 10, 0, 0, false);
assertDoesNotThrow(() -> testOTP4.getPin());
assertDoesNotThrow(() -> {
OTP testOTP5 = OTP.createNewOTP(OTPType.HOTP, "", OTPAlgorithm.SHA1, 8, 0, 0, false);
assertNotNull(testOTP5);
});
}
private static byte[] hexStr2Bytes(String hex) {