Remove _current methods
This commit is contained in:
parent
542ed7c97f
commit
43c4eec067
75
src/lib.rs
75
src/lib.rs
|
@ -69,7 +69,6 @@ use image::Luma;
|
||||||
use url::{Host, Url};
|
use url::{Host, Url};
|
||||||
|
|
||||||
use hmac::Mac;
|
use hmac::Mac;
|
||||||
use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH};
|
|
||||||
|
|
||||||
type HmacSha1 = hmac::Hmac<sha1::Sha1>;
|
type HmacSha1 = hmac::Hmac<sha1::Sha1>;
|
||||||
type HmacSha256 = hmac::Hmac<sha2::Sha256>;
|
type HmacSha256 = hmac::Hmac<sha2::Sha256>;
|
||||||
|
@ -129,11 +128,6 @@ impl Algorithm {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn system_time() -> Result<u64, SystemTimeError> {
|
|
||||||
let t = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
|
|
||||||
Ok(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, Eq, Hash)]
|
||||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||||
pub struct TOTPRef<'a> {
|
pub struct TOTPRef<'a> {
|
||||||
|
@ -282,23 +276,9 @@ impl<'a> TOTPRef<'a> {
|
||||||
(step + 1) * self.step
|
(step + 1) * self.step
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the timestamp of the first second of the next step
|
/// Give the ttl (in seconds) of the token taken at the provided timestamp (also in seconds).
|
||||||
/// According to system time
|
pub fn ttl(&self, time: u64) -> u64 {
|
||||||
pub fn next_step_current(&self) -> Result<u64, SystemTimeError> {
|
self.step - (time % self.step)
|
||||||
let t = system_time()?;
|
|
||||||
Ok(self.next_step(t))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Give the ttl (in seconds) of the current token
|
|
||||||
pub fn ttl(&self) -> Result<u64, SystemTimeError> {
|
|
||||||
let t = system_time()?;
|
|
||||||
Ok(self.step - (t % self.step))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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 given the provided timestamp in seconds, accounting [skew](struct.TOTP.html#structfield.skew)
|
/// 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
|
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
|
/// 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 {
|
pub fn get_secret_base32(&self) -> String {
|
||||||
base32::encode(
|
base32::encode(
|
||||||
|
@ -508,23 +482,10 @@ impl TOTP {
|
||||||
self.as_ref().next_step(time)
|
self.as_ref().next_step(time)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the timestamp of the first second of the next step
|
/// Give the ttl (in seconds) of the token taken at the provided timestamp (also in seconds).
|
||||||
/// According to system time
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn next_step_current(&self) -> Result<u64, SystemTimeError> {
|
pub fn ttl(&self, time: u64) -> u64 {
|
||||||
self.as_ref().next_step_current()
|
self.as_ref().ttl(time)
|
||||||
}
|
|
||||||
|
|
||||||
/// Give the ttl (in seconds) of the current token
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn ttl(&self) -> Result<u64, SystemTimeError> {
|
|
||||||
self.as_ref().ttl()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate a token from the current system time
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn generate_current(&self) -> Result<String, SystemTimeError> {
|
|
||||||
self.as_ref().generate_current()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Will check if token is valid given the provided timestamp in seconds, accounting [skew](struct.TOTP.html#structfield.skew)
|
/// 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)
|
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<bool, SystemTimeError> {
|
|
||||||
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
|
/// 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)]
|
#[inline(always)]
|
||||||
pub fn get_secret_base32(&self) -> String {
|
pub fn get_secret_base32(&self) -> String {
|
||||||
|
@ -848,8 +803,15 @@ impl LabeledTOTP {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
fn system_time() -> Result<u64, SystemTimeError> {
|
||||||
|
let t = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs();
|
||||||
|
Ok(t)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "gen_secret")]
|
#[cfg(feature = "gen_secret")]
|
||||||
fn default_values() {
|
fn default_values() {
|
||||||
|
@ -1092,7 +1054,7 @@ mod tests {
|
||||||
"constantoine@github.com".to_string(),
|
"constantoine@github.com".to_string(),
|
||||||
))
|
))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(totp.totp.ttl().is_ok());
|
totp.totp.ttl(system_time().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1119,7 +1081,7 @@ mod tests {
|
||||||
.as_secs();
|
.as_secs();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
totp.generate(time).as_str(),
|
totp.generate(time).as_str(),
|
||||||
totp.generate_current().unwrap()
|
totp.generate(system_time().unwrap())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1145,9 +1107,8 @@ mod tests {
|
||||||
fn checks_token_current() {
|
fn checks_token_current() {
|
||||||
let totp = TOTP::new(Algorithm::SHA1, 6, 0, 1, "TestSecretSuperSecret".into()).unwrap();
|
let totp = TOTP::new(Algorithm::SHA1, 6, 0, 1, "TestSecretSuperSecret".into()).unwrap();
|
||||||
assert!(totp
|
assert!(totp
|
||||||
.check_current(&totp.generate_current().unwrap())
|
.check(&totp.generate(system_time().unwrap()), system_time().unwrap()));
|
||||||
.unwrap());
|
assert!(!totp.check("bogus", system_time().unwrap()));
|
||||||
assert!(!totp.check_current("bogus").unwrap());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1170,7 +1131,7 @@ mod tests {
|
||||||
fn next_step_current() {
|
fn next_step_current() {
|
||||||
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, "TestSecretSuperSecret".into()).unwrap();
|
let totp = TOTP::new(Algorithm::SHA1, 6, 1, 30, "TestSecretSuperSecret".into()).unwrap();
|
||||||
let t = system_time().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]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue