Skip to content

Commit

Permalink
feat: remove auth lib and call external rust-token-server-client crate
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Oct 23, 2023
1 parent 856aef2 commit e9c2ddf
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 387 deletions.
8 changes: 4 additions & 4 deletions dap-bin/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pub fn init_logger() {
.compact();

// This limits the logger to emits only proxy crate
let pkg_name = env!("CARGO_PKG_NAME").replace('-', "_");
let level_string = std::env::var(EnvFilter::DEFAULT_ENV).unwrap_or_else(|_| "info".to_string());
let filter_layer = EnvFilter::new(format!("{}={}", pkg_name, level_string));
// let filter_layer = EnvFilter::from_default_env();
// let pkg_name = env!("CARGO_PKG_NAME").replace('-', "_");
// let level_string = std::env::var(EnvFilter::DEFAULT_ENV).unwrap_or_else(|_| "info".to_string());
// let filter_layer = EnvFilter::new(format!("{}={}", pkg_name, level_string));
let filter_layer = EnvFilter::from_default_env();

tracing_subscriber::registry()
.with(format_layer)
Expand Down
1 change: 1 addition & 0 deletions dap-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ trust-dns-resolver = { version = "0.23.1", default-features = false, features =


# authentication
auth-client = { git = "https://github.com/junkurihara/rust-token-server", package = "rust-token-server-client", branch = "main" }
jwt-simple = "0.11.7"
chrono = "0.4.31"
serde = { version = "1.0.189", features = ["derive"] }
Expand Down
223 changes: 0 additions & 223 deletions dap-lib/src/auth/authenticator.rs

This file was deleted.

29 changes: 0 additions & 29 deletions dap-lib/src/auth/message.rs

This file was deleted.

66 changes: 62 additions & 4 deletions dap-lib/src/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
mod authenticator;
mod message;
mod token;
use crate::{
error::{bail, DapError},
http::HttpClient,
log::*,
};
use async_trait::async_trait;
use auth_client::{AuthenticationConfig, TokenClient, TokenHttpClient};
use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;
use tokio::sync::RwLock;
use url::Url;

pub use authenticator::Authenticator;
#[async_trait]
impl TokenHttpClient for HttpClient {
async fn post_json<S, R>(&self, url: &Url, json_body: &S) -> anyhow::Result<R>
where
S: Serialize + Send + Sync,
R: DeserializeOwned + Send + Sync,
{
let res = self.post(url.to_owned()).await.json(json_body).send().await?;
if !res.status().is_success() {
let err_res = res.error_for_status_ref();
bail!(DapError::HttpClientError(err_res.unwrap_err()));
}
let json_res = res.json::<R>().await?;
Ok(json_res)
}

async fn get_json<R>(&self, url: &Url) -> anyhow::Result<R>
where
R: DeserializeOwned + Send + Sync,
{
let res = self.get(url.to_owned()).await.send().await?;
if !res.status().is_success() {
let err_res = res.error_for_status_ref();
bail!(DapError::HttpClientError(err_res.unwrap_err()));
}
let json_res = res.json::<R>().await?;

Ok(json_res)
}
}

pub struct Authenticator {
inner: TokenClient<HttpClient>,
}
impl Authenticator {
pub async fn new(auth_config: &AuthenticationConfig, http_client: Arc<RwLock<HttpClient>>) -> Result<Self, DapError> {
let inner = TokenClient::new(auth_config, http_client).await?;
Ok(Self { inner })
}
pub async fn login(&self) -> Result<(), DapError> {
self.inner.login().await?;
info!("Login success");
Ok(())
}
pub async fn refresh(&self) -> Result<(), DapError> {
// TODO: expiration check logic
self.inner.refresh().await?;
info!("Refresh success");
Ok(())
}
}
Loading

0 comments on commit e9c2ddf

Please sign in to comment.