Skip to content

Commit

Permalink
Deps (#126)
Browse files Browse the repository at this point in the history
* Deps update

* Deps update

* Deps update - totp downgrade
  • Loading branch information
gr211 authored Oct 15, 2022
1 parent 21d682a commit 5d2cdfe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
20 changes: 17 additions & 3 deletions src/model/account.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::time::SystemTime;

use base32::decode;
use base32::Alphabet::RFC4648;
use gettextrs::*;
use glib::clone;
use gtk::prelude::*;
Expand Down Expand Up @@ -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<String, TotpError> {
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)
}
}
}
14 changes: 4 additions & 10 deletions src/model/account_errors.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -21,12 +21,6 @@ impl TotpError {
}
}

impl From<SecretParseError> for TotpError {
fn from(e: SecretParseError) -> Self {
TotpError::SecretParseError(e)
}
}

impl From<TotpUrlError> for TotpError {
fn from(e: TotpUrlError) -> Self {
TotpError::TotpUrlError(e)
Expand Down

0 comments on commit 5d2cdfe

Please sign in to comment.