-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove auth lib and call external rust-token-server-client crate
- Loading branch information
1 parent
856aef2
commit e9c2ddf
Showing
10 changed files
with
78 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
Oops, something went wrong.