add unlock screen
This commit is contained in:
1 parent
a1e5daba59
commit
2371486f39
5 files changed
+138
-4
No files matched your search
+1
-1
@@ -19,7 +19,7 @@ impl PublicConfig {
|
|||||||
/// Construct a PublicConfig with default values, in case there is nothing saved on disk
|
/// Construct a PublicConfig with default values, in case there is nothing saved on disk
|
||||||
fn default() -> PublicConfig {
|
fn default() -> PublicConfig {
|
||||||
let mut db_salt = [0u8; 32];
|
let mut db_salt = [0u8; 32];
|
||||||
getrandom::fill(&mut db_salt);
|
let _ = getrandom::fill(&mut db_salt);
|
||||||
PublicConfig { db_salt }
|
PublicConfig { db_salt }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,4 +17,7 @@ pub enum CreddyError {
|
|||||||
|
|
||||||
#[error("Unable to hash password: {0}")]
|
#[error("Unable to hash password: {0}")]
|
||||||
PasswordHash(#[from] argon2::Error),
|
PasswordHash(#[from] argon2::Error),
|
||||||
|
|
||||||
|
#[error("Unable to get random data: {0}")]
|
||||||
|
Entropy(#[from] getrandom::Error),
|
||||||
}
|
}
|
||||||
+4
-3
@@ -12,7 +12,7 @@ mod session;
|
|||||||
|
|
||||||
use config::AppConfig;
|
use config::AppConfig;
|
||||||
use session::AppSession;
|
use session::AppSession;
|
||||||
use screens::{HomeScreen, WelcomeScreen};
|
use screens::{HomeScreen, UnlockScreen, WelcomeScreen};
|
||||||
|
|
||||||
struct CreddyApp {}
|
struct CreddyApp {}
|
||||||
|
|
||||||
@@ -25,8 +25,9 @@ impl Render for CreddyApp {
|
|||||||
WelcomeScreen::new(window, cx)
|
WelcomeScreen::new(window, cx)
|
||||||
})
|
})
|
||||||
.into_any_element(),
|
.into_any_element(),
|
||||||
// todo: unlock screen
|
AppSession::Locked => window
|
||||||
AppSession::Locked => div().child("Creddy is locked").into_any_element(),
|
.use_keyed_state("unlock", cx, |window, cx| UnlockScreen::new(window, cx))
|
||||||
|
.into_any_element(),
|
||||||
AppSession::Unlocked { .. } => window
|
AppSession::Unlocked { .. } => window
|
||||||
.use_keyed_state("home", cx, |_, _| HomeScreen {})
|
.use_keyed_state("home", cx, |_, _| HomeScreen {})
|
||||||
.into_any_element(),
|
.into_any_element(),
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
mod home;
|
mod home;
|
||||||
|
mod unlock;
|
||||||
mod welcome;
|
mod welcome;
|
||||||
|
|
||||||
pub use home::HomeScreen;
|
pub use home::HomeScreen;
|
||||||
|
pub use unlock::UnlockScreen;
|
||||||
pub use welcome::WelcomeScreen;
|
pub use welcome::WelcomeScreen;
|
||||||
@@ -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<PassphraseInput>,
|
||||||
|
error: Option<SharedString>,
|
||||||
|
/// Set while the KDF is running; dropping it cancels the task.
|
||||||
|
pending: Option<Task<()>>,
|
||||||
|
_subscriptions: Vec<Subscription>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UnlockScreen {
|
||||||
|
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> 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<Self>) {
|
||||||
|
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::<AppConfig>().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<Self>) -> 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))),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user