Merge factors type for authentication
This commit is contained in:
@@ -23,13 +23,15 @@ impl TotpKey {
|
||||
pub fn new_random() -> Self {
|
||||
let random_bytes = rand::thread_rng().gen::<[u8; 10]>();
|
||||
Self {
|
||||
encoded: base32::encode(BASE32_ALPHABET, &random_bytes)
|
||||
encoded: base32::encode(BASE32_ALPHABET, &random_bytes),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a key from an encoded secret
|
||||
pub fn from_encoded_secret(s: &str) -> Self {
|
||||
Self { encoded: s.to_string() }
|
||||
Self {
|
||||
encoded: s.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get QrCode URL for user
|
||||
@@ -74,15 +76,19 @@ impl TotpKey {
|
||||
/// Get the code at a specific time
|
||||
fn get_code_at<F: Fn() -> u64>(&self, get_time: F) -> Res<String> {
|
||||
let gen = TotpGenerator::new()
|
||||
.set_digit(NUM_DIGITS).unwrap()
|
||||
.set_step(PERIOD).unwrap()
|
||||
.set_digit(NUM_DIGITS)
|
||||
.unwrap()
|
||||
.set_step(PERIOD)
|
||||
.unwrap()
|
||||
.set_hash_algorithm(HashAlgorithm::SHA1)
|
||||
.build();
|
||||
|
||||
let key = match base32::decode(BASE32_ALPHABET, &self.encoded) {
|
||||
None => {
|
||||
return Err(Box::new(
|
||||
std::io::Error::new(ErrorKind::Other, "Failed to decode base32 secret!")));
|
||||
return Err(Box::new(std::io::Error::new(
|
||||
ErrorKind::Other,
|
||||
"Failed to decode base32 secret!",
|
||||
)));
|
||||
}
|
||||
Some(k) => k,
|
||||
};
|
||||
@@ -113,4 +119,4 @@ mod test {
|
||||
let key = TotpKey::from_encoded_secret("JBSWY3DPEHPK3PXP");
|
||||
assert_eq!("124851", key.get_code_at(|| 1650470683).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user