From fb2923f761d4cb6155597450a54957ac30c2854f Mon Sep 17 00:00:00 2001 From: TheArrayser Date: Wed, 28 Jun 2023 17:01:41 +0200 Subject: [PATCH] Added requirements checks --- .../cringe_authenticator_library/OTP.java | 5 ++++- .../cringe_authenticator_library/OTPType.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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 9182489..a211953 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 @@ -1,5 +1,7 @@ package com.cringe_studios.cringe_authenticator_library; +import java.security.InvalidKeyException; + import com.cringe_studios.cringe_authenticator_library.impl.Base32; public abstract class OTP { @@ -45,7 +47,8 @@ public abstract class OTP { * @param checksum OPTIONAL: appends a checksum digit to the end of the string * @return returns a String containing the OTP Digits and the optional checksum at the end */ - public static OTP createNewOTP(OTPType type, String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) { + 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; return type.instance(secret, algorithm, digits, counter, periodInSeconds, checksum); } 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 7100ed5..6c59f1f 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 @@ -1,5 +1,7 @@ package com.cringe_studios.cringe_authenticator_library; +import com.cringe_studios.cringe_authenticator_library.impl.Base32; + public enum OTPType { HOTP("HMAC-based One-Time Password"), TOTP("Time-based One-Time Password"); @@ -15,6 +17,16 @@ public enum OTPType { } public OTP instance(String secret, OTPAlgorithm algorithm, int digits, long counter, long periodInSeconds, boolean checksum) { + if(/*type == null ||*/ secret == null /*|| counter < 0*/) { + return null; + } + + try { + Base32.decode(secret); + }catch (IllegalArgumentException e) { + throw new OTPException("Your secret is invalid! Please rescan the code!", e); + } + switch(this) { case HOTP: return new com.cringe_studios.cringe_authenticator_library.impl.HOTP(secret, algorithm, digits, counter, periodInSeconds, checksum);