Added custom OTPException and InvalidBase32 Exception; removed a few
warnings
This commit is contained in:
parent
30a9ae1632
commit
e0d0d6bc08
@ -50,7 +50,9 @@ public abstract class OTP {
|
||||
|
||||
public abstract long getNextDueTime();
|
||||
|
||||
public abstract String getPin();
|
||||
public abstract String getPin() throws OTPException;
|
||||
|
||||
public abstract void incrementCounter();
|
||||
|
||||
public String getSecret() {
|
||||
return this.base32Secret;
|
||||
|
@ -0,0 +1,26 @@
|
||||
package com.cringe_studios.cringe_authenticator_library;
|
||||
|
||||
public class OTPException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 4635395862998258574L;
|
||||
|
||||
public OTPException() {
|
||||
}
|
||||
|
||||
public OTPException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public OTPException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public OTPException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public OTPException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
|
||||
}
|
@ -1,16 +1,11 @@
|
||||
package com.cringe_studios.cringe_authenticator_library.impl;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.MacSpi;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import javax.xml.crypto.dsig.spec.HMACParameterSpec;
|
||||
|
||||
import com.cringe_studios.cringe_authenticator_library.OTP;
|
||||
import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm;
|
||||
import com.cringe_studios.cringe_authenticator_library.OTPException;
|
||||
import com.cringe_studios.cringe_authenticator_library.OTPType;
|
||||
|
||||
//https://datatracker.ietf.org/doc/html/rfc4226
|
||||
@ -30,7 +25,7 @@ public class HOTP extends OTP {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPin() {
|
||||
public String getPin() throws OTPException{
|
||||
try {
|
||||
long movingFactor = this.getCounter();
|
||||
|
||||
@ -41,14 +36,12 @@ public class HOTP extends OTP {
|
||||
}
|
||||
|
||||
return generateOTP(-1, text);
|
||||
|
||||
} catch (InvalidKeyException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
throw new OTPException("Your secret is invalid! Please rescan the code!", e);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
throw new OTPException("Your platform does not support the " + this.getAlgorithm().name() + "algorithm!", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String generateOTP(int truncationOffset, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException {
|
||||
@ -84,4 +77,9 @@ public class HOTP extends OTP {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incrementCounter() {
|
||||
this.counter++;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,5 @@
|
||||
package com.cringe_studios.cringe_authenticator_library.impl;
|
||||
|
||||
public class InvalidBase32Exception extends IllegalArgumentException {
|
||||
private static final long serialVersionUID = 8747632785152793756L;
|
||||
}
|
@ -1,15 +1,12 @@
|
||||
package com.cringe_studios.cringe_authenticator_library.impl;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
|
||||
import com.cringe_studios.cringe_authenticator_library.OTP;
|
||||
import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm;
|
||||
import com.cringe_studios.cringe_authenticator_library.OTPException;
|
||||
import com.cringe_studios.cringe_authenticator_library.OTPType;
|
||||
|
||||
//https://datatracker.ietf.org/doc/html/rfc6238
|
||||
@ -22,24 +19,20 @@ public class TOTP extends HOTP {
|
||||
|
||||
@Override
|
||||
public long getNextDueTime() {
|
||||
// TODO Auto-generated method stub
|
||||
// e.g. current time: 20 seconds & current period: 30 seconds -> next due time:
|
||||
// 30 seconds
|
||||
return (this.getCounter() + 1) * this.getPeriod();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPin() {
|
||||
// TODO Auto-generated method stub
|
||||
public String getPin() throws OTPException{
|
||||
try {
|
||||
return getPinAt(Instant.now().getEpochSecond());
|
||||
} catch (InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
throw new OTPException("Your secret is invalid! Please rescan the code!", e);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
throw new OTPException("Your platform does not support the " + this.getAlgorithm().name() + "algorithm!", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -75,4 +68,7 @@ public class TOTP extends HOTP {
|
||||
|
||||
return this.generateOTP(-1, msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incrementCounter() {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user