Skip to content

Commit

Permalink
password prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasGrandl committed Feb 16, 2024
1 parent ad260a2 commit 6558a4c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
30 changes: 10 additions & 20 deletions src/commands/bitwarden/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::{
collections::HashMap,
fs,
sync::mpsc::{Receiver, Sender},
time::Duration,
};
use std::{collections::HashMap, fs, sync::mpsc::Receiver, time::Duration};

use async_std::channel::Sender;
use bonsaidb::core::keyvalue::KeyStatus;
use gpui::*;

Expand All @@ -21,9 +17,9 @@ use crate::{
use super::list::BitwardenAccount;

#[derive(Clone)]
pub struct BitwardenPasswordPromptBuilder {
account: BitwardenAccount,
password: Sender<String>,
pub(super) struct BitwardenPasswordPromptBuilder {
pub(super) account: BitwardenAccount,
pub(super) password: Sender<String>,
}

impl StateViewBuilder for BitwardenPasswordPromptBuilder {
Expand All @@ -34,7 +30,6 @@ impl StateViewBuilder for BitwardenPasswordPromptBuilder {
_: Receiver<bool>,
cx: &mut WindowContext,
) -> AnyView {
let account = self.account.clone();
let password = self.password.clone();
Form::new(
vec![Input::new(
Expand All @@ -47,18 +42,12 @@ impl StateViewBuilder for BitwardenPasswordPromptBuilder {
Some(|v| v.is_empty().then(|| "Password is required")),
cx,
)],
move |values, actions, cx| {
let account = account.clone();
move |values, _, cx| {
let password = password.clone();
let mut actions = actions.clone();
cx.spawn(|mut cx| async move {
let mut account = account.clone();
account.password = Some(values["password"].clone());
if account.unlock(&mut cx).await.is_err() {
actions.toast.error("Failed to unlock account", &mut cx);
return;
if password.send(values["password"].clone()).await.is_err() {
eprintln!("Failed to send password back");
}
let _ = password.send(values["password"].clone());
let _ = cx.update_global::<StateModel, _>(|model, cx| {
model.pop(cx);
});
Expand Down Expand Up @@ -313,10 +302,11 @@ impl StateViewBuilder for BitwardenAccountListBuilder {
.toast
.error("Failed to delete account", cx);
} else {
actions.toast.error(
actions.toast.success(
"Successfully deleted account",
cx,
);
actions.update();
}
});
}
Expand Down
23 changes: 20 additions & 3 deletions src/commands/bitwarden/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{collections::HashMap, path::PathBuf, sync::mpsc::Receiver, time::Duration};

use async_std::process::{Command, Output};
use async_std::{
channel,
process::{Command, Output},
};
use gpui::*;
use log::*;
use serde::{Deserialize, Serialize};
Expand All @@ -19,7 +22,9 @@ use crate::{
window::Window,
};

use super::accounts::{BitwardenAccountFormBuilder, BitwardenAccountListBuilder};
use super::accounts::{
BitwardenAccountFormBuilder, BitwardenAccountListBuilder, BitwardenPasswordPromptBuilder,
};

#[derive(Clone)]
pub struct BitwardenListBuilder {
Expand Down Expand Up @@ -277,8 +282,20 @@ impl BitwardenAccount {
let password = if let Some(password) = &self.password {
password.clone()
} else {
return Err(anyhow::anyhow!("No password saved"));
let (s, r) = channel::unbounded::<String>();
let _ = cx.update_global::<StateModel, _>(|model, cx| {
model.push(
BitwardenPasswordPromptBuilder {
password: s,
account: self.clone(),
},
cx,
);
});
//
r.recv().await?
};
eprintln!("Password: {}", password);
let output = self
.command(vec!["unlock", &password, "--raw", "--nointeraction"], cx)
.await?;
Expand Down

0 comments on commit 6558a4c

Please sign in to comment.