From 75680b2e9320e4d260b6bc4f1f2d7ababed15ef9 Mon Sep 17 00:00:00 2001 From: TheArrayser Date: Sun, 25 Jun 2023 22:14:12 +0200 Subject: [PATCH] Added Time correction setting to OTP.java --- .../cringe_authenticator_library/OTP.java | 11 +++++++++++ .../cringe_authenticator_library/impl/TOTP.java | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/cringe_studios/cringe_authenticator_library/OTP.java b/src/main/java/com/cringe_studios/cringe_authenticator_library/OTP.java index 5c8aadd..9182489 100644 --- a/src/main/java/com/cringe_studios/cringe_authenticator_library/OTP.java +++ b/src/main/java/com/cringe_studios/cringe_authenticator_library/OTP.java @@ -14,6 +14,7 @@ public abstract class OTP { protected long counter; protected long period; protected boolean checksum; + static protected long timeOffset; protected OTP(OTPType nType, String nSecret, OTPAlgorithm nAlgorithm, int nDigits, long nCounter, long nPeriodInSeconds, boolean nChecksum) { this.secret = Base32.decode(nSecret); @@ -81,4 +82,14 @@ public abstract class OTP { public boolean hasChecksum() { return checksum; } + + // Seconds towards positive infinity means a positive correction into the future (seconds = 2 -> 5 becomes 7) + // Seconds towards negative infinity means a negative correction into the past (seconds = -2 -> 5 becomes 3) + public static void setTimeCorrection(long seconds) { + timeOffset = seconds; + } + + public static long getTimeCorrection() { + return timeOffset; + } } \ No newline at end of file diff --git a/src/main/java/com/cringe_studios/cringe_authenticator_library/impl/TOTP.java b/src/main/java/com/cringe_studios/cringe_authenticator_library/impl/TOTP.java index 9669882..e9623a3 100644 --- a/src/main/java/com/cringe_studios/cringe_authenticator_library/impl/TOTP.java +++ b/src/main/java/com/cringe_studios/cringe_authenticator_library/impl/TOTP.java @@ -37,7 +37,7 @@ public class TOTP extends HOTP { @Override public long getCounter() { - return getCounterAt(Instant.now().getEpochSecond()); + return getCounterAt(Instant.now().getEpochSecond() - super.getTimeCorrection()); } private long getCounterAt(long unixSecond) {