Added custom OTPException and InvalidBase32 Exception; removed a few

warnings
This commit is contained in:
TheArrayser 2023-06-18 23:05:15 +02:00
parent 30a9ae1632
commit e0d0d6bc08
5 changed files with 51 additions and 24 deletions

View File

@ -50,7 +50,9 @@ public abstract class OTP {
public abstract long getNextDueTime(); public abstract long getNextDueTime();
public abstract String getPin(); public abstract String getPin() throws OTPException;
public abstract void incrementCounter();
public String getSecret() { public String getSecret() {
return this.base32Secret; return this.base32Secret;

View File

@ -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);
}
}

View File

@ -1,16 +1,11 @@
package com.cringe_studios.cringe_authenticator_library.impl; package com.cringe_studios.cringe_authenticator_library.impl;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; 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.OTP;
import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm; import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm;
import com.cringe_studios.cringe_authenticator_library.OTPException;
import com.cringe_studios.cringe_authenticator_library.OTPType; import com.cringe_studios.cringe_authenticator_library.OTPType;
//https://datatracker.ietf.org/doc/html/rfc4226 //https://datatracker.ietf.org/doc/html/rfc4226
@ -30,7 +25,7 @@ public class HOTP extends OTP {
} }
@Override @Override
public String getPin() { public String getPin() throws OTPException{
try { try {
long movingFactor = this.getCounter(); long movingFactor = this.getCounter();
@ -41,14 +36,12 @@ public class HOTP extends OTP {
} }
return generateOTP(-1, text); return generateOTP(-1, text);
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
// TODO Auto-generated catch block throw new OTPException("Your secret is invalid! Please rescan the code!", e);
e.printStackTrace();
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block throw new OTPException("Your platform does not support the " + this.getAlgorithm().name() + "algorithm!", e);
e.printStackTrace();
} }
return null;
} }
public String generateOTP(int truncationOffset, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException { public String generateOTP(int truncationOffset, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException {
@ -84,4 +77,9 @@ public class HOTP extends OTP {
return result; return result;
} }
@Override
public void incrementCounter() {
this.counter++;
}
} }

View File

@ -0,0 +1,5 @@
package com.cringe_studios.cringe_authenticator_library.impl;
public class InvalidBase32Exception extends IllegalArgumentException {
private static final long serialVersionUID = 8747632785152793756L;
}

View File

@ -1,15 +1,12 @@
package com.cringe_studios.cringe_authenticator_library.impl; package com.cringe_studios.cringe_authenticator_library.impl;
import java.math.BigInteger;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.time.Instant; 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.OTPAlgorithm;
import com.cringe_studios.cringe_authenticator_library.OTPException;
import com.cringe_studios.cringe_authenticator_library.OTPType; import com.cringe_studios.cringe_authenticator_library.OTPType;
//https://datatracker.ietf.org/doc/html/rfc6238 //https://datatracker.ietf.org/doc/html/rfc6238
@ -22,24 +19,20 @@ public class TOTP extends HOTP {
@Override @Override
public long getNextDueTime() { public long getNextDueTime() {
// TODO Auto-generated method stub
// e.g. current time: 20 seconds & current period: 30 seconds -> next due time: // e.g. current time: 20 seconds & current period: 30 seconds -> next due time:
// 30 seconds // 30 seconds
return (this.getCounter() + 1) * this.getPeriod(); return (this.getCounter() + 1) * this.getPeriod();
} }
@Override @Override
public String getPin() { public String getPin() throws OTPException{
// TODO Auto-generated method stub
try { try {
return getPinAt(Instant.now().getEpochSecond()); return getPinAt(Instant.now().getEpochSecond());
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
e.printStackTrace(); throw new OTPException("Your secret is invalid! Please rescan the code!", e);
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); throw new OTPException("Your platform does not support the " + this.getAlgorithm().name() + "algorithm!", e);
} }
return null;
} }
@Override @Override
@ -75,4 +68,7 @@ public class TOTP extends HOTP {
return this.generateOTP(-1, msg); return this.generateOTP(-1, msg);
} }
@Override
public void incrementCounter() {}
} }