From 43c4eec067ab1c2f5c92bf91d737111a34f811e0 Mon Sep 17 00:00:00 2001 From: Michael Pfaff Date: Tue, 9 May 2023 23:59:40 -0400 Subject: [PATCH] Remove _current methods --- src/lib.rs | 75 +++++++++++++----------------------------------------- 1 file changed, 18 insertions(+), 57 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9e7cff2..4762864 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,7 +69,6 @@ use image::Luma; use url::{Host, Url}; use hmac::Mac; -use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH}; type HmacSha1 = hmac::Hmac; type HmacSha256 = hmac::Hmac; @@ -129,11 +128,6 @@ impl Algorithm { } } -fn system_time() -> Result { - let t = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(); - Ok(t) -} - #[derive(Debug, Clone, Copy, Eq, Hash)] #[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct TOTPRef<'a> { @@ -282,23 +276,9 @@ impl<'a> TOTPRef<'a> { (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 { - let t = system_time()?; - Ok(self.next_step(t)) - } - - /// Give the ttl (in seconds) of the current token - pub fn ttl(&self) -> Result { - let t = system_time()?; - Ok(self.step - (t % self.step)) - } - - /// Generate a token from the current system time - pub fn generate_current(&self) -> Result { - let t = system_time()?; - Ok(self.generate(t)) + /// Give the ttl (in seconds) of the token taken at the provided timestamp (also in seconds). + pub fn ttl(&self, time: u64) -> u64 { + self.step - (time % self.step) } /// Will check if token is valid given the provided timestamp in seconds, accounting [skew](struct.TOTP.html#structfield.skew) @@ -314,12 +294,6 @@ impl<'a> TOTPRef<'a> { false } - /// Will check if token is valid by current system time, accounting [skew](struct.TOTP.html#structfield.skew) - pub fn check_current(&self, token: &str) -> Result { - let t = system_time()?; - Ok(self.check(token, t)) - } - /// Will return the base32 representation of the secret, which might be useful when users want to manually add the secret to their authenticator pub fn get_secret_base32(&self) -> String { base32::encode( @@ -508,23 +482,10 @@ impl TOTP { self.as_ref().next_step(time) } - /// Returns the timestamp of the first second of the next step - /// According to system time + /// Give the ttl (in seconds) of the token taken at the provided timestamp (also in seconds). #[inline(always)] - pub fn next_step_current(&self) -> Result { - self.as_ref().next_step_current() - } - - /// Give the ttl (in seconds) of the current token - #[inline(always)] - pub fn ttl(&self) -> Result { - self.as_ref().ttl() - } - - /// Generate a token from the current system time - #[inline(always)] - pub fn generate_current(&self) -> Result { - self.as_ref().generate_current() + pub fn ttl(&self, time: u64) -> u64 { + self.as_ref().ttl(time) } /// Will check if token is valid given the provided timestamp in seconds, accounting [skew](struct.TOTP.html#structfield.skew) @@ -533,12 +494,6 @@ impl TOTP { self.as_ref().check(token, time) } - /// Will check if token is valid by current system time, accounting [skew](struct.TOTP.html#structfield.skew) - #[inline(always)] - pub fn check_current(&self, token: &str) -> Result { - self.as_ref().check_current(token) - } - /// Will return the base32 representation of the secret, which might be useful when users want to manually add the secret to their authenticator #[inline(always)] pub fn get_secret_base32(&self) -> String { @@ -848,8 +803,15 @@ impl LabeledTOTP { #[cfg(test)] mod tests { + use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH}; + use super::*; + fn system_time() -> Result { + let t = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(); + Ok(t) + } + #[test] #[cfg(feature = "gen_secret")] fn default_values() { @@ -1092,7 +1054,7 @@ mod tests { "constantoine@github.com".to_string(), )) .unwrap(); - assert!(totp.totp.ttl().is_ok()); + totp.totp.ttl(system_time().unwrap()); } #[test] @@ -1119,7 +1081,7 @@ mod tests { .as_secs(); assert_eq!( totp.generate(time).as_str(), - totp.generate_current().unwrap() + totp.generate(system_time().unwrap()) ); } @@ -1145,9 +1107,8 @@ mod tests { fn checks_token_current() { let totp = TOTP::new(Algorithm::SHA1, 6, 0, 1, "TestSecretSuperSecret".into()).unwrap(); assert!(totp - .check_current(&totp.generate_current().unwrap()) - .unwrap()); - assert!(!totp.check_current("bogus").unwrap()); + .check(&totp.generate(system_time().unwrap()), system_time().unwrap())); + assert!(!totp.check("bogus", system_time().unwrap())); } #[test] @@ -1170,7 +1131,7 @@ mod tests { fn next_step_current() { let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, "TestSecretSuperSecret".into()).unwrap(); let t = system_time().unwrap(); - assert!(totp.next_step_current().unwrap() == totp.next_step(t)); + assert!(totp.next_step(system_time().unwrap()) == totp.next_step(t)); } #[test]