client: actually do authentication properly
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
pub mod settings;
|
||||
use settings::Settings;
|
||||
use settings::keys as SettingsKey;
|
||||
|
||||
pub mod events;
|
||||
use events::*;
|
||||
@@ -26,7 +27,7 @@ use kordophone::model::JwtToken;
|
||||
use kordophone::api::{
|
||||
http_client::{Credentials, HTTPAPIClient},
|
||||
APIInterface,
|
||||
TokenStore,
|
||||
AuthenticationStore,
|
||||
};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@@ -37,18 +38,52 @@ pub enum DaemonError {
|
||||
|
||||
pub type DaemonResult<T> = Result<T, Box<dyn Error + Send + Sync>>;
|
||||
|
||||
struct DatabaseTokenStore {
|
||||
struct DatabaseAuthenticationStore {
|
||||
database: Arc<Mutex<Database>>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl TokenStore for DatabaseTokenStore {
|
||||
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.get_token().await
|
||||
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.set_token(token).await;
|
||||
self.database.lock().await
|
||||
.with_settings(|settings| settings.put(SettingsKey::TOKEN, &token)).await.unwrap_or_else(|e| {
|
||||
log::error!("Failed to set token: {}", e);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,9 +287,7 @@ impl Daemon {
|
||||
}
|
||||
|
||||
async fn get_settings(&mut self) -> Result<Settings> {
|
||||
let settings = self.database.with_settings(Settings::from_db
|
||||
).await?;
|
||||
|
||||
let settings = self.database.with_settings(Settings::from_db).await?;
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
@@ -262,30 +295,19 @@ impl Daemon {
|
||||
self.database.with_settings(|s| settings.save(s)).await
|
||||
}
|
||||
|
||||
async fn get_client(&mut self) -> Result<HTTPAPIClient<DatabaseTokenStore>> {
|
||||
async fn get_client(&mut self) -> Result<HTTPAPIClient<DatabaseAuthenticationStore>> {
|
||||
Self::get_client_impl(&mut self.database).await
|
||||
}
|
||||
|
||||
async fn get_client_impl(database: &mut Arc<Mutex<Database>>) -> Result<HTTPAPIClient<DatabaseTokenStore>> {
|
||||
let settings = database.with_settings(Settings::from_db
|
||||
).await?;
|
||||
async fn get_client_impl(database: &mut Arc<Mutex<Database>>) -> Result<HTTPAPIClient<DatabaseAuthenticationStore>> {
|
||||
let settings = database.with_settings(Settings::from_db).await?;
|
||||
|
||||
let server_url = settings.server_url
|
||||
.ok_or(DaemonError::ClientNotConfigured)?;
|
||||
|
||||
let client = HTTPAPIClient::new(
|
||||
server_url.parse().unwrap(),
|
||||
|
||||
match (settings.username, settings.credential_item) {
|
||||
(Some(username), Some(password)) => Some(
|
||||
Credentials {
|
||||
username,
|
||||
password,
|
||||
}
|
||||
),
|
||||
_ => None,
|
||||
},
|
||||
DatabaseTokenStore { database: database.clone() }
|
||||
DatabaseAuthenticationStore { database: database.clone() }
|
||||
);
|
||||
|
||||
Ok(client)
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use kordophone_db::settings::Settings as DbSettings;
|
||||
use anyhow::Result;
|
||||
|
||||
mod keys {
|
||||
pub mod keys {
|
||||
pub static SERVER_URL: &str = "ServerURL";
|
||||
pub static USERNAME: &str = "Username";
|
||||
pub static CREDENTIAL_ITEM: &str = "CredentialItem";
|
||||
pub static TOKEN: &str = "Token";
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -13,6 +14,7 @@ pub struct Settings {
|
||||
pub server_url: Option<String>,
|
||||
pub username: Option<String>,
|
||||
pub credential_item: Option<String>,
|
||||
pub token: Option<String>,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
@@ -20,11 +22,12 @@ impl Settings {
|
||||
let server_url: Option<String> = db_settings.get(keys::SERVER_URL)?;
|
||||
let username: Option<String> = db_settings.get(keys::USERNAME)?;
|
||||
let credential_item: Option<String> = db_settings.get(keys::CREDENTIAL_ITEM)?;
|
||||
|
||||
let token: Option<String> = db_settings.get(keys::TOKEN)?;
|
||||
Ok(Self {
|
||||
server_url,
|
||||
username,
|
||||
credential_item,
|
||||
token,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -38,6 +41,9 @@ impl Settings {
|
||||
if let Some(credential_item) = &self.credential_item {
|
||||
db_settings.put(keys::CREDENTIAL_ITEM, &credential_item)?;
|
||||
}
|
||||
if let Some(token) = &self.token {
|
||||
db_settings.put(keys::TOKEN, &token)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,6 +108,7 @@ impl DbusSettings for ServerImpl {
|
||||
server_url: Some(url),
|
||||
username: Some(user),
|
||||
credential_item: None,
|
||||
token: None,
|
||||
}, r)
|
||||
)
|
||||
}
|
||||
@@ -123,6 +124,7 @@ impl DbusSettings for ServerImpl {
|
||||
server_url: Some(value),
|
||||
username: None,
|
||||
credential_item: None,
|
||||
token: None,
|
||||
}, r)
|
||||
)
|
||||
}
|
||||
@@ -138,6 +140,7 @@ impl DbusSettings for ServerImpl {
|
||||
server_url: None,
|
||||
username: Some(value),
|
||||
credential_item: None,
|
||||
token: None,
|
||||
}, r)
|
||||
)
|
||||
}
|
||||
@@ -153,6 +156,7 @@ impl DbusSettings for ServerImpl {
|
||||
server_url: None,
|
||||
username: None,
|
||||
credential_item: Some(value.to_string()),
|
||||
token: None,
|
||||
}, r)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user