Private
Public Access
1
0

daemon: Token store

This commit is contained in:
2025-04-27 14:01:19 -07:00
parent 84f782cc03
commit 49f8b81b9c
8 changed files with 51 additions and 26 deletions

1
Cargo.lock generated
View File

@@ -857,6 +857,7 @@ name = "kordophoned"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait",
"dbus", "dbus",
"dbus-codegen", "dbus-codegen",
"dbus-crossroads", "dbus-crossroads",

View File

@@ -8,7 +8,7 @@ pub use tokio::sync::Mutex;
use crate::repository::Repository; use crate::repository::Repository;
use crate::settings::Settings; use crate::settings::Settings;
pub use kordophone::api::TokenManagement; pub use kordophone::api::TokenStore;
use kordophone::model::JwtToken; use kordophone::model::JwtToken;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
@@ -89,7 +89,8 @@ impl DatabaseAccess for Arc<Mutex<Database>> {
static TOKEN_KEY: &str = "token"; static TOKEN_KEY: &str = "token";
impl TokenManagement for Database { #[async_trait]
impl TokenStore for Database {
async fn get_token(&mut self) -> Option<JwtToken> { async fn get_token(&mut self) -> Option<JwtToken> {
self.with_settings(|settings| { self.with_settings(|settings| {
let token: Result<Option<JwtToken>> = settings.get(TOKEN_KEY); let token: Result<Option<JwtToken>> = settings.get(TOKEN_KEY);

View File

@@ -3,6 +3,7 @@ extern crate serde;
use std::{path::PathBuf, str}; use std::{path::PathBuf, str};
use crate::api::{TokenStore, InMemoryTokenStore};
use hyper::{Body, Client, Method, Request, Uri}; use hyper::{Body, Client, Method, Request, Uri};
use async_trait::async_trait; use async_trait::async_trait;
@@ -15,10 +16,10 @@ use crate::{
type HttpClient = Client<hyper::client::HttpConnector>; type HttpClient = Client<hyper::client::HttpConnector>;
pub struct HTTPAPIClient { pub struct HTTPAPIClient<K: TokenStore + Send + Sync> {
pub base_url: Uri, pub base_url: Uri,
pub token_store: K,
credentials: Option<Credentials>, credentials: Option<Credentials>,
auth_token: Option<JwtToken>,
client: HttpClient, client: HttpClient,
} }
@@ -91,7 +92,7 @@ impl<B> AuthSetting for hyper::http::Request<B> {
} }
#[async_trait] #[async_trait]
impl APIInterface for HTTPAPIClient { impl<K: TokenStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
type Error = Error; type Error = Error;
async fn get_version(&mut self) -> Result<String, Self::Error> { async fn get_version(&mut self) -> Result<String, Self::Error> {
@@ -113,7 +114,7 @@ impl APIInterface for HTTPAPIClient {
let body = || -> Body { serde_json::to_string(&credentials).unwrap().into() }; let body = || -> Body { serde_json::to_string(&credentials).unwrap().into() };
let token: AuthResponse = self.request_with_body_retry("authenticate", Method::POST, body, false).await?; let token: AuthResponse = self.request_with_body_retry("authenticate", Method::POST, body, false).await?;
let token = JwtToken::new(&token.jwt).map_err(|_| Error::DecodeError)?; let token = JwtToken::new(&token.jwt).map_err(|_| Error::DecodeError)?;
self.auth_token = Some(token.clone()); self.token_store.set_token(token.clone()).await;
Ok(token) Ok(token)
} }
@@ -124,12 +125,12 @@ impl APIInterface for HTTPAPIClient {
} }
} }
impl HTTPAPIClient { impl<K: TokenStore + Send + Sync> HTTPAPIClient<K> {
pub fn new(base_url: Uri, credentials: Option<Credentials>) -> HTTPAPIClient { pub fn new(base_url: Uri, credentials: Option<Credentials>, token_store: K) -> HTTPAPIClient<K> {
HTTPAPIClient { HTTPAPIClient {
base_url, base_url,
credentials, credentials,
auth_token: Option::None, token_store,
client: Client::new(), client: Client::new(),
} }
} }
@@ -178,7 +179,8 @@ impl HTTPAPIClient {
.expect("Unable to build request") .expect("Unable to build request")
}; };
let request = build_request(&self.auth_token); let token = self.token_store.get_token().await;
let request = build_request(&token);
let mut response = self.client.request(request).await?; let mut response = self.client.request(request).await?;
log::debug!("-> Response: {:}", response.status()); log::debug!("-> Response: {:}", response.status());
@@ -195,7 +197,7 @@ impl HTTPAPIClient {
if let Some(credentials) = &self.credentials { if let Some(credentials) = &self.credentials {
self.authenticate(credentials.clone()).await?; self.authenticate(credentials.clone()).await?;
let request = build_request(&self.auth_token); let request = build_request(&token);
response = self.client.request(request).await?; response = self.client.request(request).await?;
} }
}, },
@@ -228,14 +230,14 @@ mod test {
use super::*; use super::*;
#[cfg(test)] #[cfg(test)]
fn local_mock_client() -> HTTPAPIClient { fn local_mock_client() -> HTTPAPIClient<InMemoryTokenStore> {
let base_url = "http://localhost:5738".parse().unwrap(); let base_url = "http://localhost:5738".parse().unwrap();
let credentials = Credentials { let credentials = Credentials {
username: "test".to_string(), username: "test".to_string(),
password: "test".to_string(), password: "test".to_string(),
}; };
HTTPAPIClient::new(base_url, credentials.into()) HTTPAPIClient::new(base_url, credentials.into(), InMemoryTokenStore::new())
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -26,22 +26,24 @@ pub trait APIInterface {
async fn authenticate(&mut self, credentials: Credentials) -> Result<JwtToken, Self::Error>; async fn authenticate(&mut self, credentials: Credentials) -> Result<JwtToken, Self::Error>;
} }
pub trait TokenManagement { #[async_trait]
pub trait TokenStore {
async fn get_token(&mut self) -> Option<JwtToken>; async fn get_token(&mut self) -> Option<JwtToken>;
async fn set_token(&mut self, token: JwtToken); async fn set_token(&mut self, token: JwtToken);
} }
pub struct InMemoryTokenManagement { pub struct InMemoryTokenStore {
token: Option<JwtToken>, token: Option<JwtToken>,
} }
impl InMemoryTokenManagement { impl InMemoryTokenStore {
pub fn new() -> Self { pub fn new() -> Self {
Self { token: None } Self { token: None }
} }
} }
impl TokenManagement for InMemoryTokenManagement { #[async_trait]
impl TokenStore for InMemoryTokenStore {
async fn get_token(&mut self) -> Option<JwtToken> { async fn get_token(&mut self) -> Option<JwtToken> {
self.token.clone() self.token.clone()
} }

View File

@@ -5,6 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.98" anyhow = "1.0.98"
async-trait = "0.1.88"
dbus = "0.9.7" dbus = "0.9.7"
dbus-crossroads = "0.5.2" dbus-crossroads = "0.5.2"
dbus-tokio = "0.7.6" dbus-tokio = "0.7.6"

View File

@@ -12,7 +12,7 @@ use thiserror::Error;
use tokio::sync::mpsc::{Sender, Receiver}; use tokio::sync::mpsc::{Sender, Receiver};
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use futures_util::FutureExt; use async_trait::async_trait;
use kordophone_db::{ use kordophone_db::{
database::{Database, DatabaseAccess}, database::{Database, DatabaseAccess},
@@ -24,7 +24,7 @@ use kordophone::model::JwtToken;
use kordophone::api::{ use kordophone::api::{
http_client::{Credentials, HTTPAPIClient}, http_client::{Credentials, HTTPAPIClient},
APIInterface, APIInterface,
TokenManagement, TokenStore,
}; };
#[derive(Debug, Error)] #[derive(Debug, Error)]
@@ -35,6 +35,21 @@ pub enum DaemonError {
pub type DaemonResult<T> = Result<T, Box<dyn Error + Send + Sync>>; pub type DaemonResult<T> = Result<T, Box<dyn Error + Send + Sync>>;
struct DatabaseTokenStore {
database: Arc<Mutex<Database>>,
}
#[async_trait]
impl TokenStore for DatabaseTokenStore {
async fn get_token(&mut self) -> Option<JwtToken> {
self.database.lock().await.get_token().await
}
async fn set_token(&mut self, token: JwtToken) {
self.database.lock().await.set_token(token).await;
}
}
pub struct Daemon { pub struct Daemon {
pub event_sender: Sender<Event>, pub event_sender: Sender<Event>,
event_receiver: Receiver<Event>, event_receiver: Receiver<Event>,
@@ -122,7 +137,8 @@ impl Daemon {
} }
), ),
_ => None, _ => None,
} },
DatabaseTokenStore { database: database.clone() }
); );
// This function needed to implement TokenManagement // This function needed to implement TokenManagement
@@ -169,7 +185,7 @@ impl Daemon {
Ok(settings) Ok(settings)
} }
async fn get_client(&mut self) -> Result<HTTPAPIClient> { async fn get_client(&mut self) -> Result<HTTPAPIClient<DatabaseTokenStore>> {
let settings = self.database.with_settings(|s| let settings = self.database.with_settings(|s|
Settings::from_db(s) Settings::from_db(s)
).await?; ).await?;
@@ -188,7 +204,8 @@ impl Daemon {
} }
), ),
_ => None, _ => None,
} },
DatabaseTokenStore { database: self.database.clone() }
); );
Ok(client) Ok(client)

View File

@@ -7,6 +7,7 @@ mod keys {
pub static CREDENTIAL_ITEM: &str = "CredentialItem"; pub static CREDENTIAL_ITEM: &str = "CredentialItem";
} }
#[derive(Debug)]
pub struct Settings { pub struct Settings {
pub server_url: Option<String>, pub server_url: Option<String>,
pub username: Option<String>, pub username: Option<String>,

View File

@@ -1,14 +1,14 @@
use kordophone::APIInterface; use kordophone::APIInterface;
use kordophone::api::http_client::HTTPAPIClient; use kordophone::api::http_client::HTTPAPIClient;
use kordophone::api::http_client::Credentials; use kordophone::api::http_client::Credentials;
use kordophone::api::InMemoryTokenManagement; use kordophone::api::InMemoryTokenStore;
use dotenv; use dotenv;
use anyhow::Result; use anyhow::Result;
use clap::Subcommand; use clap::Subcommand;
use crate::printers::{ConversationPrinter, MessagePrinter}; use crate::printers::{ConversationPrinter, MessagePrinter};
pub fn make_api_client_from_env() -> HTTPAPIClient { pub fn make_api_client_from_env() -> HTTPAPIClient<InMemoryTokenStore> {
dotenv::dotenv().ok(); dotenv::dotenv().ok();
// read from env // read from env
@@ -23,7 +23,7 @@ pub fn make_api_client_from_env() -> HTTPAPIClient {
.expect("KORDOPHONE_PASSWORD must be set"), .expect("KORDOPHONE_PASSWORD must be set"),
}; };
HTTPAPIClient::new(base_url.parse().unwrap(), credentials.into()) HTTPAPIClient::new(base_url.parse().unwrap(), credentials.into(), InMemoryTokenStore::new())
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@@ -52,7 +52,7 @@ impl Commands {
} }
struct ClientCli { struct ClientCli {
api: HTTPAPIClient, api: HTTPAPIClient<InMemoryTokenStore>,
} }
impl ClientCli { impl ClientCli {