Skip to content

Commit

Permalink
Make subclasses of TotpInfo override only getOtp(long time)
Browse files Browse the repository at this point in the history
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 e4c9a58.
  • Loading branch information
alexbakker committed Nov 17, 2024
1 parent 6d8eec0 commit 843e5f1
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ private YAOTP(long code, int digits) {
public static YAOTP generateOTP(byte[] secret, String pin, int digits, String otpAlgo, long period)
throws NoSuchAlgorithmException, InvalidKeyException, IOException {
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 {
byte[] pinWithHash;
byte[] pinBytes = pin.getBytes(StandardCharsets.UTF_8);
Expand Down
14 changes: 0 additions & 14 deletions app/src/main/java/com/beemdevelopment/aegis/otp/MotpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ public MotpInfo(byte[] secret, String pin) throws OtpInfoException {
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
public String getOtp(long time) {
if (_pin == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public SteamInfo(byte[] secret, String algorithm, int digits, int period) throws
}

@Override
public String getOtp() throws OtpInfoException {
public String getOtp(long time) throws OtpInfoException {
checkSecret();

try {
OTP otp = TOTP.generateOTP(getSecret(), getAlgorithm(true), getDigits(), getPeriod());
OTP otp = TOTP.generateOTP(getSecret(), getAlgorithm(true), getDigits(), getPeriod(), time);
return otp.toSteamString();
} catch (InvalidKeyException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ public TotpInfo(byte[] secret, String algorithm, int digits, int period) throws

@Override
public String getOtp() throws OtpInfoException {
checkSecret();

try {
OTP otp = TOTP.generateOTP(getSecret(), getAlgorithm(true), getDigits(), getPeriod());
return otp.toString();
} catch (InvalidKeyException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return getOtp(System.currentTimeMillis() / 1000);
}

public String getOtp(long time) throws OtpInfoException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public YandexInfo(@NonNull byte[] secret, @Nullable String pin) throws OtpInfoEx
}

@Override
public String getOtp() {
public String getOtp(long time) {
if (_pin == null) {
throw new IllegalStateException("PIN must be set before generating an OTP");
}

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();
} catch (InvalidKeyException | NoSuchAlgorithmException | IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public void validateYaOtp()
testCase.pin,
8,
"HmacSHA256",
testCase.timestamp,
30
30,
testCase.timestamp
);
assertEquals(testCase.expected, otp.toString());
}
Expand Down

0 comments on commit 843e5f1

Please sign in to comment.