daemon: reorg
This commit is contained in:
65
kordophoned/src/daemon/auth_store.rs
Normal file
65
kordophoned/src/daemon/auth_store.rs
Normal file
@@ -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<Mutex<Database>>,
|
||||
}
|
||||
|
||||
impl DatabaseAuthenticationStore {
|
||||
pub fn new(database: Arc<Mutex<Database>>) -> Self {
|
||||
Self { database }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AuthenticationStore for DatabaseAuthenticationStore {
|
||||
async fn get_credentials(&mut self) -> Option<Credentials> {
|
||||
self.database.lock().await.with_settings(|settings| {
|
||||
let username: Option<String> = settings.get::<String>(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::<String>(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<JwtToken> {
|
||||
self.database.lock().await
|
||||
.with_settings(|settings| settings.get::<JwtToken>(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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<T> = Result<T, Box<dyn Error + Send + Sync>>;
|
||||
|
||||
struct DatabaseAuthenticationStore {
|
||||
database: Arc<Mutex<Database>>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AuthenticationStore for DatabaseAuthenticationStore {
|
||||
async fn get_credentials(&mut self) -> Option<Credentials> {
|
||||
self.database.lock().await.with_settings(|settings| {
|
||||
let username: Option<String> = settings.get::<String>(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::<String>(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<JwtToken> {
|
||||
self.database.lock().await
|
||||
.with_settings(|settings| settings.get::<JwtToken>(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)
|
||||
|
||||
Reference in New Issue
Block a user