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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user