diff --git a/src/config.rs b/src/config.rs index 81d7044..b0de9d7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,7 +19,7 @@ impl PublicConfig { /// Construct a PublicConfig with default values, in case there is nothing saved on disk fn default() -> PublicConfig { let mut db_salt = [0u8; 32]; - getrandom::fill(&mut db_salt); + let _ = getrandom::fill(&mut db_salt); PublicConfig { db_salt } } diff --git a/src/error.rs b/src/error.rs index aebb2e9..a6c39f4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -17,4 +17,7 @@ pub enum CreddyError { #[error("Unable to hash password: {0}")] PasswordHash(#[from] argon2::Error), + + #[error("Unable to get random data: {0}")] + Entropy(#[from] getrandom::Error), } diff --git a/src/main.rs b/src/main.rs index f2d2354..fcda3c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod session; use config::AppConfig; use session::AppSession; -use screens::{HomeScreen, WelcomeScreen}; +use screens::{HomeScreen, UnlockScreen, WelcomeScreen}; struct CreddyApp {} @@ -25,8 +25,9 @@ impl Render for CreddyApp { WelcomeScreen::new(window, cx) }) .into_any_element(), - // todo: unlock screen - AppSession::Locked => div().child("Creddy is locked").into_any_element(), + AppSession::Locked => window + .use_keyed_state("unlock", cx, |window, cx| UnlockScreen::new(window, cx)) + .into_any_element(), AppSession::Unlocked { .. } => window .use_keyed_state("home", cx, |_, _| HomeScreen {}) .into_any_element(), diff --git a/src/screens/mod.rs b/src/screens/mod.rs index 8054deb..e02fdcd 100644 --- a/src/screens/mod.rs +++ b/src/screens/mod.rs @@ -1,5 +1,7 @@ mod home; +mod unlock; mod welcome; pub use home::HomeScreen; +pub use unlock::UnlockScreen; pub use welcome::WelcomeScreen; diff --git a/src/screens/unlock.rs b/src/screens/unlock.rs new file mode 100644 index 0000000..cfa651d --- /dev/null +++ b/src/screens/unlock.rs @@ -0,0 +1,128 @@ +use gpui_kit::{ + AppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString, + Styled, Subscription, Task, Window, div, rems, +}; +use gpui_omarchy::{ButtonVariant, Status, alert, button, focus_scope}; + +use crate::components::{PassphraseInput, PassphraseInputEvent}; +use crate::config::AppConfig; +use crate::db::Db; +use crate::error::CreddyError; +use crate::session::AppSession; + + +/// Shown at startup when a database exists: enter the passphrase to open it. +pub struct UnlockScreen { + passphrase: Entity, + error: Option, + /// Set while the KDF is running; dropping it cancels the task. + pending: Option>, + _subscriptions: Vec, +} + +impl UnlockScreen { + pub fn new(window: &mut Window, cx: &mut Context) -> Self { + // nothing else shares the show/hide flag, so it's private to this input + let show = cx.new(|_| false); + let passphrase = cx.new(|cx| PassphraseInput::new(show, window, cx)); + passphrase.update(cx, |input, cx| input.focus(window, cx)); + + let submit = cx.subscribe_in(&passphrase, window, |this, _, event, window, cx| { + if let PassphraseInputEvent::Submit = event { + this.submit(window, cx); + } + }); + + Self { + passphrase, + error: None, + pending: None, + _subscriptions: vec![submit], + } + } + + fn submit(&mut self, _: &mut Window, cx: &mut Context) { + if self.pending.is_some() { + return; + } + + let passphrase = self.passphrase.read(cx).value(cx); + if passphrase.is_empty() { + self.error = Some("Passphrase is empty.".into()); + cx.notify(); + return; + } + + let config = cx.global::().clone(); + self.error = None; + self.pending = Some(cx.spawn(async move |this, cx| { + // argon2 is deliberately slow, so keep it off the UI thread + let result = cx + .background_spawn(async move { Db::connect(&config, passphrase.as_str()) }) + .await; + + let _ = this.update(cx, |this, cx| { + this.pending = None; + match result { + Ok(db) => cx.set_global(AppSession::Unlocked(db)), + Err(e) => this.error = Some(error_message(&e)), + } + cx.notify(); + }); + })); + cx.notify(); + } +} + +/// Turn a failed unlock into something fit to show the user. +fn error_message(e: &CreddyError) -> SharedString { + // todo: have Db::connect return a dedicated error variant for this instead + if let CreddyError::Db(duckdb::Error::DuckDBFailure(_, Some(desc))) = e + && desc.contains("Wrong encryption key") + { + return "Incorrect passphrase.".into(); + } + format!("Failed to unlock: {e}").into() +} + +impl Render for UnlockScreen { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + focus_scope("unlock") + .size_full() + .flex() + .flex_col() + .items_center() + .p(rems(0.5)) + .child( + div() + .text_size(rems(1.875)) + .font_weight(FontWeight::BOLD) + .child("Creddy is locked"), + ) + .child( + // fill the rest of the window so the form sits in the middle of it + div() + .flex_1() + .w_full() + .max_w(rems(24.)) + .flex() + .flex_col() + .justify_center() + .gap(rems(1.)) + .child( + div() + .text_center() + .text_size(rems(1.25)) + .font_weight(FontWeight::BOLD) + .child("Please enter your passphrase"), + ) + .children(self.error.clone().map(|e| alert(e, Status::Error, cx))) + .child(self.passphrase.clone()) + .child( + button("submit", "Submit", ButtonVariant::Primary, cx) + .disabled(self.pending.is_some()) + .on_click(cx.listener(|this, _, window, cx| this.submit(window, cx))), + ), + ) + } +}