Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,137 changes: 483 additions & 654 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ totp-rs = { version = "6.0.0", features = [
] } # TOTP Generation ...
rfd = "0.17.2" # File System Dialogs
secrecy = "0.10.3" # Provides us with SecretString
uuid = "1.26.1" # Allows us to identify entries
uuid = "1.27.0" # Allows us to identify entries
serde = { version = "1.0.229", features = [
"derive",
] } # Needed for config serializing and deserializing
Expand All @@ -47,6 +47,11 @@ raw-window-handle = "0.6.2" # also needed for clipboard
tracing = "0.1.44"
tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }

serde_json = "1.0.151" # needed for aegis import (vault parsing)
aes-gcm = "0.11.1" # aegis vault decryption
scrypt = { version = "0.12.0", default-features = false } # aegis key derivation
base64 = "0.23.1" # aegis vault payload

[target.'cfg(unix)'.dependencies] # gstreamer dependencies for qr detection with camera (Unix-like systems)
gstreamer = "0.25.4"
gstreamer-app = "0.25.2"
Expand Down
1,215 changes: 497 additions & 718 deletions cargo-sources.json

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::sync::{Arc, Mutex};

use iced::{
Element, Length, Subscription, Task, Theme,
Length, Subscription, Task, Theme, Widget,
time::Instant,
widget::{container, text},
};
Expand Down Expand Up @@ -143,19 +143,21 @@ impl Clockode {
}
}

pub fn view(&self) -> Element<'_, Message> {
pub fn view(&self) -> impl Widget<Message> {
let content = match &self.screen {
Screen::Error(error) => container(text(error)).center(Length::Fill).into(),
Screen::CreateDatabase(create_database) => {
create_database.view(self.now).map(Message::CreateDatabase)
}
Screen::UnlockDatabase(unlock_database) => {
unlock_database.view(self.now).map(Message::UnlockDatabase)
}
Screen::HomePage(homepage) => homepage.view(self.now).map(Message::HomePage),
Screen::Error(error) => container(text(error)).center(Length::Fill).boxed(),
Screen::CreateDatabase(create_database) => create_database
.view(self.now)
.map(Message::CreateDatabase)
.boxed(),
Screen::UnlockDatabase(unlock_database) => unlock_database
.view(self.now)
.map(Message::UnlockDatabase)
.boxed(),
Screen::HomePage(homepage) => homepage.view(self.now).map(Message::HomePage).boxed(),
};

widgets::toast::Manager::new(content, &self.toasts, Message::CloseToast).into()
widgets::toast::Manager::new(content, &self.toasts, Message::CloseToast)
}

pub fn subscription(&self) -> Subscription<Message> {
Expand Down
1 change: 1 addition & 0 deletions src/app/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod database;
mod entry;
pub mod specific_impl;

pub use database::ClockodeDatabase;
pub use database::check_database;
Expand Down
83 changes: 38 additions & 45 deletions src/app/core/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
sync::{Arc, Mutex},
time::SystemTime,
};
use tracing::{info, warn};
use tracing::info;

use crate::{
APP_ID,
Expand Down Expand Up @@ -80,7 +80,7 @@ fn save_database_atomic(

// capture the mtime the destination file will have after the rename, tthis is what lets callers recognize (and ignore) filesystem-watcher events caused by this very save.
f.metadata().and_then(|m| m.modified()).ok()
}; // drop file handle
}; // drop file handle

// replace the old database with the new one
if let Err(e) = std::fs::rename(&tmp_path, path) {
Expand Down Expand Up @@ -263,6 +263,42 @@ impl ClockodeDatabase {
.await
}

pub async fn add_entries(&self, entries: Vec<ClockodeEntry>) -> Result<(), anywho::Error> {
info!("Adding {} database entries", entries.len());

let lock = self.lock.clone();
let path = self.path.clone();
let password = self.password.clone();
let known_mtime = self.known_mtime.clone();

smol::unblock(move || {
let _guard = lock
.lock()
.map_err(|e| anywho!("Database lock poisoned: {}", e))?;

let mut file = std::fs::File::open(&*path)?;
let key = DatabaseKey::new().with_password(password.expose_secret());
let mut db = Database::open(&mut file, key)?;
drop(file);

let mut root = db.root_mut();
let mut target_group = root
.group_by_name_mut("Default Group")
.ok_or_else(|| anywho!("Default Group not found"))?;

for entry in entries {
let mut keepass_entry = target_group.add_entry();
update_clockode_entry_in_keepass(entry, &mut keepass_entry);
}

let mtime = save_database_atomic(&mut db, &path, &password)?;
record_known_mtime(&known_mtime, mtime);

Ok(())
})
.await
}

pub async fn update_entry(&self, entry: ClockodeEntry) -> Result<(), anywho::Error> {
info!("Updating database entry");

Expand Down Expand Up @@ -352,49 +388,6 @@ impl ClockodeDatabase {
.await
}

// Import the content given in standard totp
pub async fn import_content(&self, file_path: PathBuf) -> Result<(), anywho::Error> {
info!("Importing content to database");

// Read the import file
let content = std::fs::read_to_string(&file_path)
.map_err(|e| anywho!("Failed to read import file: {}", e))?;

for line in content.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}

// we use from_url unchecked because of the same reason we can't use TOTP::new
// Don't use TOTP::new() because it enforces validation and some secrets (ej: microsoft)
// that are xxxx xxxx xxxx xxxx will fail here if we use ::new() with error:
// Failed to construct TOTP object: The length of the shared secret MUST be at least 128 bits. 80 bits is not enough
match totp_rs::Totp::from_url_unchecked(line) {
Ok(totp) => {
let name = if totp.account_name().trim().is_empty() {
"Default".to_string()
} else {
totp.account_name().to_string()
};

let entry = ClockodeEntry {
id: None,
name,
totp,
};

self.add_entry(entry).await?;
}
Err(e) => {
warn!("Warning: Failed to parse TOTP URL '{}': {}", line, e);
}
}
}

Ok(())
}

// Export content to standard
pub async fn export_content(&self, file_path: PathBuf) -> Result<(), anywho::Error> {
info!("Exporting database content");
Expand Down
33 changes: 27 additions & 6 deletions src/app/core/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ pub struct ClockodeEntry {
pub totp: Totp,
}

/// Display name for an imported entry: "Issuer (account)", falling back to
/// whichever part is present, or "Default" if neither is.
pub fn import_display_name(issuer: &str, account: &str) -> String {
match (issuer.trim(), account.trim()) {
("", "") => "Default".to_string(),
("", account) => account.to_string(),
(issuer, "") => issuer.to_string(),
(issuer, account) => format!("{issuer} ({account})"),
}
}

impl TryFrom<Entry> for ClockodeEntry {
type Error = anywho::Error;

Expand Down Expand Up @@ -68,7 +79,15 @@ impl TryFrom<Entry> for ClockodeEntry {
.to_string();

// Don't use build() because it enforces validation and some secrets (ej: microsoft)
let totp_result = totp_rs::Builder::new().with_algorithm(algorithm).with_digits(digits).with_skew(0).with_step_duration(period).with_secret(secret_bytes).with_issuer(issuer).with_account_name(account_name).build_noncompliant();
let totp_result = totp_rs::Builder::new()
.with_algorithm(algorithm)
.with_digits(digits)
.with_skew(0)
.with_step_duration(period)
.with_secret(secret_bytes)
.with_issuer(issuer)
.with_account_name(account_name)
.build_noncompliant();

Ok(ClockodeEntry {
id: Some(id),
Expand Down Expand Up @@ -106,11 +125,13 @@ pub fn update_clockode_entry_in_keepass(value: ClockodeEntry, entry: &mut EntryM
);
entry.fields.insert(
CUSTOM_ISSUER_KEY.to_string(),
Value::Unprotected(value
.totp
.issuer()
.unwrap_or(value.name.as_str())
.to_string()),
Value::Unprotected(
value
.totp
.issuer()
.unwrap_or(value.name.as_str())
.to_string(),
),
);

entry.fields.insert(
Expand Down
Loading