Added requirements checks
This commit is contained in:
parent
806726d109
commit
fb2923f761
@ -1,5 +1,7 @@
|
|||||||
package com.cringe_studios.cringe_authenticator_library;
|
package com.cringe_studios.cringe_authenticator_library;
|
||||||
|
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
|
||||||
import com.cringe_studios.cringe_authenticator_library.impl.Base32;
|
import com.cringe_studios.cringe_authenticator_library.impl.Base32;
|
||||||
|
|
||||||
public abstract class OTP {
|
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
|
* @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
|
* @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);
|
return type.instance(secret, algorithm, digits, counter, periodInSeconds, checksum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.cringe_studios.cringe_authenticator_library;
|
package com.cringe_studios.cringe_authenticator_library;
|
||||||
|
|
||||||
|
import com.cringe_studios.cringe_authenticator_library.impl.Base32;
|
||||||
|
|
||||||
public enum OTPType {
|
public enum OTPType {
|
||||||
HOTP("HMAC-based One-Time Password"),
|
HOTP("HMAC-based One-Time Password"),
|
||||||
TOTP("Time-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) {
|
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) {
|
switch(this) {
|
||||||
case HOTP:
|
case HOTP:
|
||||||
return new com.cringe_studios.cringe_authenticator_library.impl.HOTP(secret, algorithm, digits, counter, periodInSeconds, checksum);
|
return new com.cringe_studios.cringe_authenticator_library.impl.HOTP(secret, algorithm, digits, counter, periodInSeconds, checksum);
|
||||||
|
Loading…
Reference in New Issue
Block a user