diff --git a/src/main/java/com/cringe_studios/cringe_authenticator_library/OTP.java b/src/main/java/com/cringe_studios/cringe_authenticator_library/OTP.java index 47381e4..21363e4 100644 --- a/src/main/java/com/cringe_studios/cringe_authenticator_library/OTP.java +++ b/src/main/java/com/cringe_studios/cringe_authenticator_library/OTP.java @@ -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); } diff --git a/src/main/java/com/cringe_studios/cringe_authenticator_library/OTPException.java b/src/main/java/com/cringe_studios/cringe_authenticator_library/OTPException.java index c84162c..f596f1d 100644 --- a/src/main/java/com/cringe_studios/cringe_authenticator_library/OTPException.java +++ b/src/main/java/com/cringe_studios/cringe_authenticator_library/OTPException.java @@ -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; diff --git a/src/main/java/com/cringe_studios/cringe_authenticator_library/OTPType.java b/src/main/java/com/cringe_studios/cringe_authenticator_library/OTPType.java index d636d64..96cc7cd 100644 --- a/src/main/java/com/cringe_studios/cringe_authenticator_library/OTPType.java +++ b/src/main/java/com/cringe_studios/cringe_authenticator_library/OTPType.java @@ -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 } } } diff --git a/src/test/java/test/OTPTest.java b/src/test/java/test/OTPTest.java index 46e8b63..51e8e8a 100644 --- a/src/test/java/test/OTPTest.java +++ b/src/test/java/test/OTPTest.java @@ -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) {