Add next_step and next_step_current methods

Signed-off-by: constantoine <cleo.rebert-ext@treezor.com>
This commit is contained in:
constantoine 2022-05-20 17:43:39 +02:00
parent ff6f562ab7
commit 4f32055d8b
No known key found for this signature in database
GPG Key ID: 0FA097951CF65367
1 changed files with 30 additions and 0 deletions

View File

@ -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()?;
@ -573,6 +588,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() {