Add next_step and next_step_current methods Signed-off-by: constantoine <cleo.rebert-ext@treezor.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
diff --git a/src/lib.rs b/src/lib.rs
index 92fce39..f29533d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -216,6 +216,21 @@ impl<T: AsRef<[u8]>> TOTP<T> {
)
}
+ /// Returns the timestamp of the first second for the next step
+ /// given the provided timestamp in seconds
+ pub fn next_step(&self, time: u64) -> u64 {
+ let step = time / self.step;
+
+ (step + 1) * self.step
+ }
+
+ /// Returns the timestamp of the first second of the next step
+ /// According to system time
+ pub fn next_step_current(&self)-> Result<u64, SystemTimeError> {
+ let t = system_time()?;
+ Ok(self.next_step(t))
+ }
+
/// Generate a token from the current system time
pub fn generate_current(&self) -> Result<String, SystemTimeError> {
let t = system_time()?;
@@ -574,6 +589,21 @@ mod tests {
}
#[test]
+ fn next_step() {
+ let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, "TestSecret", Some("Github".to_string()), "constantoine@github.com".to_string()).unwrap();
+ assert!(totp.next_step(0) == 30);
+ assert!(totp.next_step(29) == 30);
+ assert!(totp.next_step(30) == 60);
+ }
+
+ #[test]
+ fn next_step_current() {
+ let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, "TestSecret", Some("Github".to_string()), "constantoine@github.com".to_string()).unwrap();
+ let t = system_time().unwrap();
+ assert!(totp.next_step_current().unwrap() == totp.next_step(t));
+ }
+
+ #[test]
#[cfg(feature = "otpauth")]
fn from_url_err() {
assert!(TOTP::<Vec<u8>>::from_url("otpauth://hotp/123").is_err());