From 2106bce7554d054ca7101bde22d92154063151d7 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Thu, 1 May 2025 20:45:20 -0700 Subject: [PATCH] daemon: reorg --- kordophoned/src/daemon/auth_store.rs | 65 +++++++++++++++++++++++ kordophoned/src/daemon/mod.rs | 77 +++++----------------------- 2 files changed, 78 insertions(+), 64 deletions(-) create mode 100644 kordophoned/src/daemon/auth_store.rs diff --git a/kordophoned/src/daemon/auth_store.rs b/kordophoned/src/daemon/auth_store.rs new file mode 100644 index 0000000..5956b66 --- /dev/null +++ b/kordophoned/src/daemon/auth_store.rs @@ -0,0 +1,65 @@ +use crate::daemon::SettingsKey; + +use std::sync::Arc; +use tokio::sync::Mutex; + +use kordophone::api::{AuthenticationStore, http_client::Credentials}; +use kordophone::model::JwtToken; +use kordophone_db::database::{Database, DatabaseAccess}; + +use async_trait::async_trait; + +pub struct DatabaseAuthenticationStore { + database: Arc>, +} + +impl DatabaseAuthenticationStore { + pub fn new(database: Arc>) -> Self { + Self { database } + } +} + +#[async_trait] +impl AuthenticationStore for DatabaseAuthenticationStore { + async fn get_credentials(&mut self) -> Option { + self.database.lock().await.with_settings(|settings| { + let username: Option = settings.get::(SettingsKey::USERNAME) + .unwrap_or_else(|e| { + log::warn!("error getting username from database: {}", e); + None + }); + + // TODO: This would be the point where we map from credential item to password. + let password: String = settings.get::(SettingsKey::CREDENTIAL_ITEM) + .unwrap_or_else(|e| { + log::warn!("error getting password from database: {}", e); + None + }) + .unwrap_or_else(|| { + log::warn!("warning: no password in database, [DEBUG] using default password"); + "test".to_string() + }); + + if username.is_none() { + log::warn!("Username not present in database"); + } + + match (username, password) { + (Some(username), password) => Some(Credentials { username, password }), + _ => None, + } + }).await + } + + async fn get_token(&mut self) -> Option { + self.database.lock().await + .with_settings(|settings| settings.get::(SettingsKey::TOKEN).unwrap_or_default()).await + } + + async fn set_token(&mut self, token: JwtToken) { + self.database.lock().await + .with_settings(|settings| settings.put(SettingsKey::TOKEN, &token)).await.unwrap_or_else(|e| { + log::error!("Failed to set token: {}", e); + }); + } +} \ No newline at end of file diff --git a/kordophoned/src/daemon/mod.rs b/kordophoned/src/daemon/mod.rs index 30e4266..4af1875 100644 --- a/kordophoned/src/daemon/mod.rs +++ b/kordophoned/src/daemon/mod.rs @@ -16,23 +16,21 @@ use thiserror::Error; use tokio::sync::mpsc::{Sender, Receiver}; use std::sync::Arc; use tokio::sync::Mutex; -use async_trait::async_trait; use kordophone_db::{ database::{Database, DatabaseAccess}, models::{Conversation, Message}, }; -use kordophone::model::JwtToken; -use kordophone::api::{ - http_client::{Credentials, HTTPAPIClient}, - APIInterface, - AuthenticationStore, -}; +use kordophone::api::APIInterface; +use kordophone::api::http_client::HTTPAPIClient; mod update_monitor; use update_monitor::UpdateMonitor; +mod auth_store; +use auth_store::DatabaseAuthenticationStore; + #[derive(Debug, Error)] pub enum DaemonError { #[error("Client Not Configured")] @@ -41,58 +39,10 @@ pub enum DaemonError { pub type DaemonResult = Result>; -struct DatabaseAuthenticationStore { - database: Arc>, -} - -#[async_trait] -impl AuthenticationStore for DatabaseAuthenticationStore { - async fn get_credentials(&mut self) -> Option { - self.database.lock().await.with_settings(|settings| { - let username: Option = settings.get::(SettingsKey::USERNAME) - .unwrap_or_else(|e| { - log::warn!("error getting username from database: {}", e); - None - }); - - // TODO: This would be the point where we map from credential item to password. - let password: String = settings.get::(SettingsKey::CREDENTIAL_ITEM) - .unwrap_or_else(|e| { - log::warn!("error getting password from database: {}", e); - None - }) - .unwrap_or_else(|| { - log::warn!("warning: no password in database, [DEBUG] using default password"); - "test".to_string() - }); - - if username.is_none() { - log::warn!("Username not present in database"); - } - - match (username, password) { - (Some(username), password) => Some(Credentials { username, password }), - _ => None, - } - }).await - } - - async fn get_token(&mut self) -> Option { - self.database.lock().await - .with_settings(|settings| settings.get::(SettingsKey::TOKEN).unwrap_or_default()).await - } - - async fn set_token(&mut self, token: JwtToken) { - self.database.lock().await - .with_settings(|settings| settings.put(SettingsKey::TOKEN, &token)).await.unwrap_or_else(|e| { - log::error!("Failed to set token: {}", e); - }); - } -} - pub mod target { pub static SYNC: &str = "sync"; pub static EVENT: &str = "event"; + pub static SETTINGS: &str = "settings"; pub static UPDATES: &str = "updates"; } pub struct Daemon { @@ -145,7 +95,6 @@ impl Daemon { let mut update_monitor = UpdateMonitor::new(self.database.clone(), self.event_sender.clone()); tokio::spawn(async move { - log::info!(target: target::UPDATES, "Starting update monitor"); update_monitor.run().await; // should run indefinitely }); @@ -167,7 +116,7 @@ impl Daemon { self.runtime.spawn(async move { let result = Self::sync_conversation_list(&mut db_clone, &signal_sender).await; if let Err(e) = result { - log::error!("Error handling sync event: {}", e); + log::error!(target: target::SYNC, "Error handling sync event: {}", e); } }); @@ -181,7 +130,7 @@ impl Daemon { self.runtime.spawn(async move { let result = Self::sync_all_conversations_impl(&mut db_clone, &signal_sender).await; if let Err(e) = result { - log::error!("Error handling sync event: {}", e); + log::error!(target: target::SYNC, "Error handling sync event: {}", e); } }); @@ -195,7 +144,7 @@ impl Daemon { self.runtime.spawn(async move { let result = Self::sync_conversation_impl(&mut db_clone, &signal_sender, conversation_id).await; if let Err(e) = result { - log::error!("Error handling sync event: {}", e); + log::error!(target: target::SYNC, "Error handling sync event: {}", e); } }); @@ -210,7 +159,7 @@ impl Daemon { Event::GetAllSettings(reply) => { let settings = self.get_settings().await .unwrap_or_else(|e| { - log::error!("Failed to get settings: {:#?}", e); + log::error!(target: target::SETTINGS, "Failed to get settings: {:#?}", e); Settings::default() }); @@ -220,7 +169,7 @@ impl Daemon { Event::UpdateSettings(settings, reply) => { self.update_settings(&settings).await .unwrap_or_else(|e| { - log::error!("Failed to update settings: {}", e); + log::error!(target: target::SETTINGS, "Failed to update settings: {}", e); }); reply.send(()).unwrap(); @@ -234,7 +183,7 @@ impl Daemon { Event::DeleteAllConversations(reply) => { self.delete_all_conversations().await .unwrap_or_else(|e| { - log::error!("Failed to delete all conversations: {}", e); + log::error!(target: target::SYNC, "Failed to delete all conversations: {}", e); }); reply.send(()).unwrap(); @@ -371,7 +320,7 @@ impl Daemon { let client = HTTPAPIClient::new( server_url.parse().unwrap(), - DatabaseAuthenticationStore { database: database.clone() } + DatabaseAuthenticationStore::new(database.clone()) ); Ok(client)