Make subclasses of TotpInfo override only getOtp(long time)

This fixes an issue where Steam OTP's were displayed in the wrong
format. The underlying issue has been present for a while, but it first
became apparent in e4c9a584f4.
This commit is contained in:
Alexander Bakker 2024-11-17 10:39:33 +01:00
parent 6d8eec0e21
commit 843e5f1ab5
6 changed files with 9 additions and 30 deletions

View file

@ -25,10 +25,10 @@ public class YAOTP {
public static YAOTP generateOTP(byte[] secret, String pin, int digits, String otpAlgo, long period) public static YAOTP generateOTP(byte[] secret, String pin, int digits, String otpAlgo, long period)
throws NoSuchAlgorithmException, InvalidKeyException, IOException { throws NoSuchAlgorithmException, InvalidKeyException, IOException {
long seconds = System.currentTimeMillis() / 1000; long seconds = System.currentTimeMillis() / 1000;
return generateOTP(secret, pin, digits, otpAlgo, seconds, period); return generateOTP(secret, pin, digits, otpAlgo, period, seconds);
} }
public static YAOTP generateOTP(byte[] secret, String pin, int digits, String otpAlgo, long seconds, long period) public static YAOTP generateOTP(byte[] secret, String pin, int digits, String otpAlgo, long period, long seconds)
throws NoSuchAlgorithmException, InvalidKeyException, IOException { throws NoSuchAlgorithmException, InvalidKeyException, IOException {
byte[] pinWithHash; byte[] pinWithHash;
byte[] pinBytes = pin.getBytes(StandardCharsets.UTF_8); byte[] pinBytes = pin.getBytes(StandardCharsets.UTF_8);

View file

@ -30,20 +30,6 @@ public class MotpInfo extends TotpInfo {
setPin(pin); setPin(pin);
} }
@Override
public String getOtp() {
if (_pin == null) {
throw new IllegalStateException("PIN must be set before generating an OTP");
}
try {
MOTP otp = MOTP.generateOTP(getSecret(), getAlgorithm(false), getDigits(), getPeriod(), getPin());
return otp.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
@Override @Override
public String getOtp(long time) { public String getOtp(long time) {
if (_pin == null) { if (_pin == null) {

View file

@ -20,11 +20,11 @@ public class SteamInfo extends TotpInfo {
} }
@Override @Override
public String getOtp() throws OtpInfoException { public String getOtp(long time) throws OtpInfoException {
checkSecret(); checkSecret();
try { try {
OTP otp = TOTP.generateOTP(getSecret(), getAlgorithm(true), getDigits(), getPeriod()); OTP otp = TOTP.generateOTP(getSecret(), getAlgorithm(true), getDigits(), getPeriod(), time);
return otp.toSteamString(); return otp.toSteamString();
} catch (InvalidKeyException | NoSuchAlgorithmException e) { } catch (InvalidKeyException | NoSuchAlgorithmException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View file

@ -27,14 +27,7 @@ public class TotpInfo extends OtpInfo {
@Override @Override
public String getOtp() throws OtpInfoException { public String getOtp() throws OtpInfoException {
checkSecret(); return getOtp(System.currentTimeMillis() / 1000);
try {
OTP otp = TOTP.generateOTP(getSecret(), getAlgorithm(true), getDigits(), getPeriod());
return otp.toString();
} catch (InvalidKeyException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
} }
public String getOtp(long time) throws OtpInfoException { public String getOtp(long time) throws OtpInfoException {

View file

@ -38,13 +38,13 @@ public class YandexInfo extends TotpInfo {
} }
@Override @Override
public String getOtp() { public String getOtp(long time) {
if (_pin == null) { if (_pin == null) {
throw new IllegalStateException("PIN must be set before generating an OTP"); throw new IllegalStateException("PIN must be set before generating an OTP");
} }
try { try {
YAOTP otp = YAOTP.generateOTP(getSecret(), getPin(), getDigits(), getAlgorithm(true), getPeriod()); YAOTP otp = YAOTP.generateOTP(getSecret(), getPin(), getDigits(), getAlgorithm(true), getPeriod(), time);
return otp.toString(); return otp.toString();
} catch (InvalidKeyException | NoSuchAlgorithmException | IOException e) { } catch (InvalidKeyException | NoSuchAlgorithmException | IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View file

@ -32,8 +32,8 @@ public class YAOTPTest {
testCase.pin, testCase.pin,
8, 8,
"HmacSHA256", "HmacSHA256",
testCase.timestamp, 30,
30 testCase.timestamp
); );
assertEquals(testCase.expected, otp.toString()); assertEquals(testCase.expected, otp.toString());
} }