diff --git a/Cargo.lock b/Cargo.lock index cb873dd3..13278e4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3037,9 +3037,9 @@ dependencies = [ [[package]] name = "totp-rs" -version = "3.0.1" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c44da613dc546a6a3db824c5f1d6672d06e304a588e3816446abe13a68c3f72f" +checksum = "b9254defd2c9202c8e5a03e4120faa0c1e0cb8ed365fb5d7305a33d0b4cf571c" dependencies = [ "base32", "constant_time_eq", diff --git a/Cargo.toml b/Cargo.toml index 676b8935..90c5290d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ strum = "0" strum_macros = "0" tempfile-fast = "0" thiserror = "1" -totp-rs = "3" +totp-rs = "2" uuid = { version = "1", features = ["v4"] } [dependencies.gtk] diff --git a/src/model/account.rs b/src/model/account.rs index 05e619d3..c4de0349 100644 --- a/src/model/account.rs +++ b/src/model/account.rs @@ -1,3 +1,7 @@ +use std::time::SystemTime; + +use base32::decode; +use base32::Alphabet::RFC4648; use gettextrs::*; use glib::clone; use gtk::prelude::*; @@ -156,8 +160,18 @@ impl Account { return Err(TotpError::Empty); } - let secret = totp_rs::Secret::Raw(key.as_bytes().to_vec()).to_bytes()?; - let totp = totp_rs::TOTP::new(totp_rs::Algorithm::SHA1, 6, 1, 30, secret)?; - totp.generate_current().map_err(TotpError::SystemTimeError) + let time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); + + Self::generate_time_based_password_with_time(time, key) + } + + fn generate_time_based_password_with_time(time: u64, key: &str) -> Result { + if let Some(b32) = decode(RFC4648 { padding: false }, key) { + let totp_sha1 = totp_rs::TOTP::new(totp_rs::Algorithm::SHA1, 6, 1, 30, b32, None, "none".to_string())?; + totp_sha1.generate(time); + Ok(totp_sha1.generate(time)) + } else { + Err(TotpError::SecretParseError) + } } } diff --git a/src/model/account_errors.rs b/src/model/account_errors.rs index 17622d91..00f806b6 100644 --- a/src/model/account_errors.rs +++ b/src/model/account_errors.rs @@ -1,15 +1,15 @@ use std::time::SystemTimeError; use thiserror::Error; -use totp_rs::{SecretParseError, TotpUrlError}; +use totp_rs::TotpUrlError; #[derive(Debug, Error)] #[allow(clippy::enum_variant_names)] pub enum TotpError { #[error("")] Empty, - #[error("{0:?}")] - SecretParseError(SecretParseError), - #[error("{0}")] + #[error("")] + SecretParseError, + #[error{""}] TotpUrlError(TotpUrlError), #[error("{0}")] SystemTimeError(SystemTimeError), @@ -21,12 +21,6 @@ impl TotpError { } } -impl From for TotpError { - fn from(e: SecretParseError) -> Self { - TotpError::SecretParseError(e) - } -} - impl From for TotpError { fn from(e: TotpUrlError) -> Self { TotpError::TotpUrlError(e)