diff options
Diffstat (limited to 'lib/auth-service/src/client')
| -rw-r--r-- | lib/auth-service/src/client/http.rs | 56 | ||||
| -rw-r--r-- | lib/auth-service/src/client/mod.rs | 29 |
2 files changed, 76 insertions, 9 deletions
diff --git a/lib/auth-service/src/client/http.rs b/lib/auth-service/src/client/http.rs new file mode 100644 index 0000000..5621fb0 --- /dev/null +++ b/lib/auth-service/src/client/http.rs @@ -0,0 +1,56 @@ +use std::ops::Deref; + +use oauth2::http; + +#[derive(Clone, Debug)] +pub struct HttpAuthClient(reqwest::Client); + +impl From<reqwest::Client> for HttpAuthClient { + fn from(value: reqwest::Client) -> Self { + Self(value) + } +} + +impl Deref for HttpAuthClient { + type Target = reqwest::Client; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl<'c> oauth2::AsyncHttpClient<'c> for HttpAuthClient { + type Error = oauth2::HttpClientError<reqwest::Error>; + + #[cfg(target_arch = "wasm32")] + type Future = Pin<Box<dyn Future<Output = Result<HttpResponse, Self::Error>> + 'c>>; + #[cfg(not(target_arch = "wasm32"))] + type Future = std::pin::Pin< + Box<dyn Future<Output = Result<oauth2::HttpResponse, Self::Error>> + Send + Sync + 'c>, + >; + + fn call(&'c self, request: oauth2::HttpRequest) -> Self::Future { + Box::pin(async move { + let response = self + .0 + .execute(request.try_into().map_err(Box::new)?) + .await + .map_err(Box::new)?; + + let mut builder = http::Response::builder().status(response.status()); + + #[cfg(not(target_arch = "wasm32"))] + { + builder = builder.version(response.version()); + } + + for (name, value) in response.headers().iter() { + builder = builder.header(name, value); + } + + builder + .body(response.bytes().await.map_err(Box::new)?.to_vec()) + .map_err(oauth2::HttpClientError::Http) + }) + } +} diff --git a/lib/auth-service/src/client/mod.rs b/lib/auth-service/src/client/mod.rs index 45260fb..e02672b 100644 --- a/lib/auth-service/src/client/mod.rs +++ b/lib/auth-service/src/client/mod.rs @@ -1,3 +1,6 @@ +pub(crate) mod http; +use std::ops::Deref; + use oauth2::{ AuthUrl, ClientId, ClientSecret, CsrfToken, EndpointNotSet, EndpointSet, RedirectUrl, Scope, TokenUrl, @@ -8,16 +11,24 @@ use url::Url; use crate::{AuthServiceError, Provider}; +type Inner = oauth2::basic::BasicClient< + EndpointSet, + EndpointNotSet, + EndpointNotSet, + EndpointNotSet, + EndpointSet, +>; + #[derive(Debug, Clone)] -pub struct OauthClient( - oauth2::basic::BasicClient< - EndpointSet, - EndpointNotSet, - EndpointNotSet, - EndpointNotSet, - EndpointSet, - >, -); +pub struct OauthClient(Inner); + +impl Deref for OauthClient { + type Target = Inner; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} #[derive(Debug)] pub struct ClientConfig { |
