implements authentication/token retrieval/keyring
This commit is contained in:
@@ -2,6 +2,7 @@ use crate::daemon::SettingsKey;
|
||||
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use keyring::{Entry, Result};
|
||||
|
||||
use kordophone::api::{AuthenticationStore, http_client::Credentials};
|
||||
use kordophone::model::JwtToken;
|
||||
@@ -22,6 +23,8 @@ impl DatabaseAuthenticationStore {
|
||||
#[async_trait]
|
||||
impl AuthenticationStore for DatabaseAuthenticationStore {
|
||||
async fn get_credentials(&mut self) -> Option<Credentials> {
|
||||
use keyring::secret_service::SsCredential;
|
||||
|
||||
self.database.lock().await.with_settings(|settings| {
|
||||
let username: Option<String> = settings.get::<String>(SettingsKey::USERNAME)
|
||||
.unwrap_or_else(|e| {
|
||||
@@ -29,31 +32,39 @@ impl AuthenticationStore for DatabaseAuthenticationStore {
|
||||
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()
|
||||
});
|
||||
match username {
|
||||
Some(username) => {
|
||||
let credential = SsCredential::new_with_target(None, "net.buzzert.kordophonecd", &username).unwrap();
|
||||
|
||||
if username.is_none() {
|
||||
log::warn!("Username not present in database");
|
||||
}
|
||||
let password: Result<String> = Entry::new_with_credential(Box::new(credential))
|
||||
.get_password();
|
||||
|
||||
match (username, password) {
|
||||
(Some(username), password) => Some(Credentials { username, password }),
|
||||
_ => None,
|
||||
log::debug!("password: {:?}", password);
|
||||
|
||||
match password {
|
||||
Ok(password) => Some(Credentials { username, password }),
|
||||
Err(e) => {
|
||||
log::error!("error getting password from keyring: {}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
None => 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
|
||||
.with_settings(|settings| {
|
||||
match settings.get::<JwtToken>(SettingsKey::TOKEN) {
|
||||
Ok(token) => token,
|
||||
Err(e) => {
|
||||
log::warn!("Failed to get token from settings: {}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}).await
|
||||
}
|
||||
|
||||
async fn set_token(&mut self, token: JwtToken) {
|
||||
|
||||
@@ -51,6 +51,7 @@ pub mod target {
|
||||
pub static SETTINGS: &str = "settings";
|
||||
pub static UPDATES: &str = "updates";
|
||||
}
|
||||
|
||||
pub struct Daemon {
|
||||
pub event_sender: Sender<Event>,
|
||||
event_receiver: Receiver<Event>,
|
||||
|
||||
@@ -4,7 +4,6 @@ use anyhow::Result;
|
||||
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";
|
||||
}
|
||||
|
||||
@@ -13,7 +12,6 @@ pub mod keys {
|
||||
pub struct Settings {
|
||||
pub server_url: Option<String>,
|
||||
pub username: Option<String>,
|
||||
pub credential_item: Option<String>,
|
||||
pub token: Option<String>,
|
||||
}
|
||||
|
||||
@@ -21,12 +19,10 @@ impl Settings {
|
||||
pub fn from_db(db_settings: &mut DbSettings) -> Result<Self> {
|
||||
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,9 +34,6 @@ impl Settings {
|
||||
if let Some(username) = &self.username {
|
||||
db_settings.put(keys::USERNAME, &username)?;
|
||||
}
|
||||
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)?;
|
||||
}
|
||||
|
||||
@@ -115,7 +115,6 @@ impl DbusSettings for ServerImpl {
|
||||
Event::UpdateSettings(Settings {
|
||||
server_url: Some(url),
|
||||
username: Some(user),
|
||||
credential_item: None,
|
||||
token: None,
|
||||
}, r)
|
||||
)
|
||||
@@ -131,7 +130,6 @@ impl DbusSettings for ServerImpl {
|
||||
Event::UpdateSettings(Settings {
|
||||
server_url: Some(value),
|
||||
username: None,
|
||||
credential_item: None,
|
||||
token: None,
|
||||
}, r)
|
||||
)
|
||||
@@ -147,28 +145,10 @@ impl DbusSettings for ServerImpl {
|
||||
Event::UpdateSettings(Settings {
|
||||
server_url: None,
|
||||
username: Some(value),
|
||||
credential_item: None,
|
||||
token: None,
|
||||
}, r)
|
||||
)
|
||||
}
|
||||
|
||||
fn credential_item(&self) -> Result<dbus::Path<'static>, dbus::MethodErr> {
|
||||
self.send_event_sync(Event::GetAllSettings)
|
||||
.map(|settings| settings.credential_item.unwrap_or_default()).map(|item| dbus::Path::new(item).unwrap_or_default())
|
||||
}
|
||||
|
||||
fn set_credential_item(&self, value: dbus::Path<'static>) -> Result<(), dbus::MethodErr> {
|
||||
self.send_event_sync(|r|
|
||||
Event::UpdateSettings(Settings {
|
||||
server_url: None,
|
||||
username: None,
|
||||
credential_item: Some(value.to_string()),
|
||||
token: None,
|
||||
}, r)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn run_sync_future<F, T>(f: F) -> Result<T, MethodErr>
|
||||
|
||||
Reference in New Issue
Block a user