Add check_current
This commit is contained in:
parent
d54f86592c
commit
1e54605808
19
src/lib.rs
19
src/lib.rs
|
@ -104,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
|
/// 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)]
|
#[derive(Debug, Clone)]
|
||||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||||
|
@ -172,10 +179,8 @@ impl<T: AsRef<[u8]>> TOTP<T> {
|
||||||
|
|
||||||
/// Generate a token from the current system time
|
/// Generate a token from the current system time
|
||||||
pub fn generate_current(&self) -> Result<String, SystemTimeError> {
|
pub fn generate_current(&self) -> Result<String, SystemTimeError> {
|
||||||
let time = SystemTime::now()
|
let t = system_time()?;
|
||||||
.duration_since(UNIX_EPOCH)?
|
Ok(self.generate(t))
|
||||||
.as_secs();
|
|
||||||
Ok(self.generate(time))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Will check if token is valid by current time, accounting [skew](struct.TOTP.html#structfield.skew)
|
/// Will check if token is valid by current time, accounting [skew](struct.TOTP.html#structfield.skew)
|
||||||
|
@ -191,6 +196,12 @@ impl<T: AsRef<[u8]>> TOTP<T> {
|
||||||
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(
|
||||||
|
|
Loading…
Reference in New Issue