Skip to content

Commit

Permalink
Merge pull request #17 from wyhaya/master
Browse files Browse the repository at this point in the history
Support generating and checking token from the current system time
  • Loading branch information
constantoine authored May 6, 2022
2 parents 64225f5 + 1e54605 commit 2c75d04
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use core::fmt;
use {base64, image::Luma, qrcodegen};

use hmac::Mac;
use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH};

type HmacSha1 = hmac::Hmac<sha1::Sha1>;
type HmacSha256 = hmac::Hmac<sha2::Sha256>;
Expand Down Expand Up @@ -103,6 +104,13 @@ impl Algorithm {
}
}

fn system_time() -> Result<u64, SystemTimeError> {
let t = SystemTime::now()
.duration_since(UNIX_EPOCH)?
.as_secs();
Ok(t)
}

/// TOTP holds informations as to how to generate an auth code and validate it. Its [secret](struct.TOTP.html#structfield.secret) field is sensitive data, treat it accordingly
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -169,6 +177,12 @@ impl<T: AsRef<[u8]>> TOTP<T> {
)
}

/// Generate a token from the current system time
pub fn generate_current(&self) -> Result<String, SystemTimeError> {
let t = system_time()?;
Ok(self.generate(t))
}

/// Will check if token is valid by current time, accounting [skew](struct.TOTP.html#structfield.skew)
pub fn check(&self, token: &str, time: u64) -> bool {
let basestep = time / self.step - (self.skew as u64);
Expand All @@ -182,6 +196,12 @@ impl<T: AsRef<[u8]>> TOTP<T> {
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<bool, SystemTimeError> {
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(
Expand Down

0 comments on commit 2c75d04

Please sign in to comment.