tree-wide: run cargo fmt

Signed-off-by: Christoph Heiss <christoph@c8h4.io>
This commit is contained in:
Christoph Heiss 2024-08-16 15:50:19 +02:00
parent b5d479b135
commit 7b9f6480d2
Signed by: c8h4
GPG key ID: 1538094429952F86
2 changed files with 60 additions and 32 deletions

View file

@ -21,7 +21,9 @@ pub fn create(siv: &mut Cursive) {
let password_edit = EditView::new().secret().with_name("master_password");
let password_view = OnEventView::new(password_edit).on_event(Event::CtrlChar('u'), |siv| {
siv.find_name::<EditView>("master_password").unwrap().set_content("")(siv);
siv.find_name::<EditView>("master_password")
.unwrap()
.set_content("")(siv);
});
let layout = LinearLayout::new(Orientation::Vertical)
@ -38,15 +40,28 @@ pub fn create(siv: &mut Cursive) {
siv.clear();
siv.add_layer(dialog);
if let Some(email) = siv.user_data().map(|data: &mut VaultData| data.sync.profile.email.clone()) {
siv.find_name::<EditView>("email").unwrap().set_content(email)(siv);
if let Some(email) = siv
.user_data()
.map(|data: &mut VaultData| data.sync.profile.email.clone())
{
siv.find_name::<EditView>("email")
.unwrap()
.set_content(email)(siv);
siv.focus_name("master_password").unwrap();
}
}
fn on_login(siv: &mut Cursive) {
let email = siv.find_name::<EditView>("email").unwrap().get_content().to_string();
let password = siv.find_name::<EditView>("master_password").unwrap().get_content().to_string();
let email = siv
.find_name::<EditView>("email")
.unwrap()
.get_content()
.to_string();
let password = siv
.find_name::<EditView>("master_password")
.unwrap()
.get_content()
.to_string();
siv.set_autorefresh(true);
let progress_text = TextContent::new("authenticating ...");
@ -65,9 +80,9 @@ fn on_login(siv: &mut Cursive) {
sink.send(Box::new(|siv| {
siv.set_autorefresh(false);
})).unwrap();
}))
.unwrap();
});
}
fn decrypt_cached(
@ -75,12 +90,17 @@ fn decrypt_cached(
progress: TextContent,
mut vault: VaultData,
email: &str,
master_password: &str
master_password: &str,
) {
progress.set_content("decrypting ...");
vault.auth.cipher = CipherSuite::from(email, master_password, vault.auth.kdf_iterations);
if vault.auth.cipher.set_decrypt_key(&vault.sync.profile.key).is_err() {
if vault
.auth
.cipher
.set_decrypt_key(&vault.sync.profile.key)
.is_err()
{
handle_login_error(sink, Some(vault), ApiError::LoginFailed);
} else {
vault::decrypt(&mut vault);
@ -88,7 +108,8 @@ fn decrypt_cached(
sink.send(Box::new(|siv| {
siv.set_user_data(vault);
vault::create(siv);
})).unwrap();
}))
.unwrap();
}
}
@ -99,7 +120,7 @@ fn sync_and_decrypt(sink: CursiveSink, progress: TextContent, email: &str, maste
let vault = sync_vault_data(auth).unwrap();
decrypt_cached(sink, progress, vault, email, master_password);
},
}
Err(err) => handle_login_error(sink, None, err),
}
}
@ -107,19 +128,26 @@ fn sync_and_decrypt(sink: CursiveSink, progress: TextContent, email: &str, maste
fn handle_login_error(sink: CursiveSink, vault: Option<VaultData>, error: ApiError) {
sink.send(Box::new(move |siv| {
siv.pop_layer();
siv.find_name::<EditView>("master_password").unwrap().set_content("")(siv);
siv.find_name::<EditView>("master_password")
.unwrap()
.set_content("")(siv);
if let Some(vault) = vault {
siv.set_user_data(vault);
}
siv.add_layer(Dialog::info(error.to_string()));
})).unwrap();
}))
.unwrap();
}
fn sync_vault_data(auth: AuthData) -> Result<VaultData, String> {
bitwarden::sync(&auth)
.map(|sync| VaultData { auth, sync, decrypted: Vec::new() })
.map(|sync| VaultData {
auth,
sync,
decrypted: Vec::new(),
})
.map_err(|e| e.to_string())
.and_then(|vault| vault::save_local_data(&vault).and(Ok(vault)))
}

View file

@ -202,7 +202,8 @@ pub fn create(siv: &mut Cursive) {
}
pub fn decrypt(vault: &mut VaultData) {
vault.decrypted = vault.sync
vault.decrypted = vault
.sync
.ciphers
.iter()
.map(|c| VaultEntry::from_cipher_entry(c, &vault.auth.cipher).unwrap())
@ -222,10 +223,9 @@ fn fuzzy_match_on_edit(siv: &mut Cursive, content: &str) {
}
let matcher = SkimMatcherV2::default();
let mut items: Vec<(i64, VaultEntry)> = items.iter()
.map(|entry| {
(matcher.fuzzy_match(&entry.name, content), entry.clone())
})
let mut items: Vec<(i64, VaultEntry)> = items
.iter()
.map(|entry| (matcher.fuzzy_match(&entry.name, content), entry.clone()))
.filter(|(score, _)| score.is_some())
.map(|(score, entry)| (score.unwrap(), entry))
.collect();
@ -255,41 +255,41 @@ fn save_data_to<T>(filename: &str, data: &T) -> Result<(), String>
where
T: Serialize,
{
let mut path = get_app_data_path()
.map_err(|err| format!("failed to write sync data: {}", err))?;
let mut path =
get_app_data_path().map_err(|err| format!("failed to write sync data: {}", err))?;
path.push(filename);
let file = File::create(path)
.map_err(|err| format!("failed to write sync data: {}", err))?;
let file = File::create(path).map_err(|err| format!("failed to write sync data: {}", err))?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, data)
.map_err(|err| format!("failed to write sync data: {}", err))
serde_json::to_writer(writer, data).map_err(|err| format!("failed to write sync data: {}", err))
}
fn read_data_from<T>(filename: &str) -> Result<T, String>
where
T: DeserializeOwned,
{
let mut path = get_app_data_path()
.map_err(|err| format!("failed to read sync data: {}", err))?;
let mut path =
get_app_data_path().map_err(|err| format!("failed to read sync data: {}", err))?;
path.push(filename);
let file = File::open(path)
.map_err(|err| format!("failed to read sync data: {}", err))?;
let file = File::open(path).map_err(|err| format!("failed to read sync data: {}", err))?;
let reader = BufReader::new(file);
serde_json::from_reader(reader)
.map_err(|err| format!("failed to read sync data: {}", err))
serde_json::from_reader(reader).map_err(|err| format!("failed to read sync data: {}", err))
}
pub fn read_local_data() -> Result<VaultData, String> {
let auth = read_data_from("auth.json")?;
let sync = read_data_from("vault.json")?;
Ok(VaultData { auth, sync, decrypted: Vec::new() })
Ok(VaultData {
auth,
sync,
decrypted: Vec::new(),
})
}
pub fn save_local_data(data: &VaultData) -> Result<(), String> {