daemon: Token store
This commit is contained in:
@@ -3,6 +3,7 @@ extern crate serde;
|
||||
|
||||
use std::{path::PathBuf, str};
|
||||
|
||||
use crate::api::{TokenStore, InMemoryTokenStore};
|
||||
use hyper::{Body, Client, Method, Request, Uri};
|
||||
|
||||
use async_trait::async_trait;
|
||||
@@ -15,10 +16,10 @@ use crate::{
|
||||
|
||||
type HttpClient = Client<hyper::client::HttpConnector>;
|
||||
|
||||
pub struct HTTPAPIClient {
|
||||
pub struct HTTPAPIClient<K: TokenStore + Send + Sync> {
|
||||
pub base_url: Uri,
|
||||
pub token_store: K,
|
||||
credentials: Option<Credentials>,
|
||||
auth_token: Option<JwtToken>,
|
||||
client: HttpClient,
|
||||
}
|
||||
|
||||
@@ -91,7 +92,7 @@ impl<B> AuthSetting for hyper::http::Request<B> {
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl APIInterface for HTTPAPIClient {
|
||||
impl<K: TokenStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
|
||||
type Error = 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 token: AuthResponse = self.request_with_body_retry("authenticate", Method::POST, body, false).await?;
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -124,12 +125,12 @@ impl APIInterface for HTTPAPIClient {
|
||||
}
|
||||
}
|
||||
|
||||
impl HTTPAPIClient {
|
||||
pub fn new(base_url: Uri, credentials: Option<Credentials>) -> HTTPAPIClient {
|
||||
impl<K: TokenStore + Send + Sync> HTTPAPIClient<K> {
|
||||
pub fn new(base_url: Uri, credentials: Option<Credentials>, token_store: K) -> HTTPAPIClient<K> {
|
||||
HTTPAPIClient {
|
||||
base_url,
|
||||
credentials,
|
||||
auth_token: Option::None,
|
||||
token_store,
|
||||
client: Client::new(),
|
||||
}
|
||||
}
|
||||
@@ -178,7 +179,8 @@ impl HTTPAPIClient {
|
||||
.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?;
|
||||
|
||||
log::debug!("-> Response: {:}", response.status());
|
||||
@@ -195,7 +197,7 @@ impl HTTPAPIClient {
|
||||
if let Some(credentials) = &self.credentials {
|
||||
self.authenticate(credentials.clone()).await?;
|
||||
|
||||
let request = build_request(&self.auth_token);
|
||||
let request = build_request(&token);
|
||||
response = self.client.request(request).await?;
|
||||
}
|
||||
},
|
||||
@@ -228,14 +230,14 @@ mod test {
|
||||
use super::*;
|
||||
|
||||
#[cfg(test)]
|
||||
fn local_mock_client() -> HTTPAPIClient {
|
||||
fn local_mock_client() -> HTTPAPIClient<InMemoryTokenStore> {
|
||||
let base_url = "http://localhost:5738".parse().unwrap();
|
||||
let credentials = Credentials {
|
||||
username: "test".to_string(),
|
||||
password: "test".to_string(),
|
||||
};
|
||||
|
||||
HTTPAPIClient::new(base_url, credentials.into())
|
||||
HTTPAPIClient::new(base_url, credentials.into(), InMemoryTokenStore::new())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -26,22 +26,24 @@ pub trait APIInterface {
|
||||
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 set_token(&mut self, token: JwtToken);
|
||||
}
|
||||
|
||||
pub struct InMemoryTokenManagement {
|
||||
pub struct InMemoryTokenStore {
|
||||
token: Option<JwtToken>,
|
||||
}
|
||||
|
||||
impl InMemoryTokenManagement {
|
||||
impl InMemoryTokenStore {
|
||||
pub fn new() -> Self {
|
||||
Self { token: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl TokenManagement for InMemoryTokenManagement {
|
||||
#[async_trait]
|
||||
impl TokenStore for InMemoryTokenStore {
|
||||
async fn get_token(&mut self) -> Option<JwtToken> {
|
||||
self.token.clone()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user