From 40196c5ed9715c17a53dcf347b265c946016fdca Mon Sep 17 00:00:00 2001 From: Steven Salaun Date: Sat, 6 Aug 2022 17:49:40 +0200 Subject: [PATCH] add `ttl` to TOTP --- examples/ttl.rs | 23 +++++++++++++++++++++++ src/lib.rs | 32 +++++++++++++++++++------------- 2 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 examples/ttl.rs diff --git a/examples/ttl.rs b/examples/ttl.rs new file mode 100644 index 0000000..7554303 --- /dev/null +++ b/examples/ttl.rs @@ -0,0 +1,23 @@ +use totp_rs::{Algorithm, TOTP}; + +fn main() { + let totp = TOTP::new( + Algorithm::SHA1, + 6, + 1, + 30, + "my-secret".to_string(), + None, + "account".to_string(), + ).unwrap(); + + loop { + println!( + "code {}\t ttl {}\t valid until: {}", + totp.generate_current().unwrap(), + totp.ttl().unwrap(), + totp.next_step_current().unwrap() + ); + std::thread::sleep(std::time::Duration::from_secs(1)); + } +} diff --git a/src/lib.rs b/src/lib.rs index be92b46..28d35b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ //! This library permits the creation of 2FA authentification tokens per TOTP, the verification of said tokens, with configurable time skew, validity time of each token, algorithm and number of digits! Default features are kept as low-dependency as possible to ensure small binaries and short compilation time //! -//! Be aware that some authenticator apps will accept the `SHA256` -//! and `SHA512` algorithms but silently fallback to `SHA1` which will +//! Be aware that some authenticator apps will accept the `SHA256` +//! and `SHA512` algorithms but silently fallback to `SHA1` which will //! make the `check()` function fail due to mismatched algorithms. //! //! Use the `SHA1` algorithm to avoid this problem. @@ -178,7 +178,7 @@ impl> TOTP { /// Will create a new instance of TOTP with given parameters. See [the doc](struct.TOTP.html#fields) for reference as to how to choose those values /// /// # Errors - /// + /// /// Will return an error in case issuer or label contain the character ':' pub fn new(algorithm: Algorithm, digits: usize, skew: u8, step: u64, secret: T, issuer: Option, account_name: String) -> Result, TotpUrlError> { if issuer.is_some() && issuer.as_ref().unwrap().contains(':') { @@ -233,6 +233,12 @@ impl> TOTP { 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()?; @@ -244,7 +250,7 @@ impl> TOTP { let basestep = time / self.step - (self.skew as u64); for i in 0..self.skew * 2 + 1 { let step_time = (basestep + (i as u64)) * (self.step as u64); - + if constant_time_eq(self.generate(step_time).as_bytes(), token.as_bytes()) { return true; } @@ -265,7 +271,7 @@ impl> TOTP { self.secret.as_ref(), ) } - + /// Generate a TOTP from the standard otpauth URL #[cfg(feature = "otpauth")] pub fn from_url>(url: S) -> Result>, TotpUrlError> { @@ -276,7 +282,7 @@ impl> TOTP { if url.host() != Some(Host::Domain("totp")) { return Err(TotpUrlError::Host); } - + let mut algorithm = Algorithm::SHA1; let mut digits = 6; let mut step = 30; @@ -292,7 +298,7 @@ impl> TOTP { } else { account_name = path.to_owned(); } - + account_name = urlencoding::decode(account_name.as_str()).map_err(|_| TotpUrlError::AccountName)?.to_string(); for (key, value) in url.query_pairs() { @@ -341,7 +347,7 @@ impl> TOTP { } /// Will generate a standard URL used to automatically add TOTP auths. Usually used with qr codes - /// + /// /// Label and issuer will be URL-encoded if needed be /// Secret will be base 32'd without padding, as per RFC. #[cfg(feature = "otpauth")] @@ -383,12 +389,12 @@ impl> TOTP { let mut vec = Vec::new(); let qr = qrcodegen::QrCode::encode_text(&url, qrcodegen::QrCodeEcc::Medium)?; let size = qr.size() as u32; - + // "+ 8 * 8" is here to add padding (the white border around the QRCode) - // As some QRCode readers don't work without padding + // As some QRCode readers don't work without padding let image_size = size * 8 + 8 * 8; let mut canvas = image::GrayImage::new(image_size, image_size); - + // Draw the border for x in 0..image_size { for y in 0..image_size { @@ -405,12 +411,12 @@ impl> TOTP { // This clever trick to one-line the value was achieved with advanced mathematics // And deep understanding of Boolean algebra. let val = !qr.get_module(x_qr as i32, y_qr as i32) as u8 * 255; - + // Multiply coordinates by width of pixels // And take into account the 8*4 padding on top and left side let x_start = x_qr * 8 + 8*4; let y_start = y_qr * 8 + 8*4; - + // Draw a 8-pixels-wide square for x_img in x_start..x_start + 8 { for y_img in y_start..y_start + 8 {