From 9b0c8c23e85930ef1128e73e06713c8a36708625 Mon Sep 17 00:00:00 2001 From: rtkay123 Date: Sun, 8 Feb 2026 13:35:04 +0200 Subject: feat: merge config --- lib/auth-service/Cargo.toml | 3 +++ lib/auth-service/src/client/mod.rs | 49 ++++++++++++++++++++++++++++++++++++++ lib/auth-service/src/lib.rs | 23 +++++++++--------- 3 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 lib/auth-service/src/client/mod.rs (limited to 'lib') diff --git a/lib/auth-service/Cargo.toml b/lib/auth-service/Cargo.toml index 147c2fa..8efdc57 100644 --- a/lib/auth-service/Cargo.toml +++ b/lib/auth-service/Cargo.toml @@ -7,5 +7,8 @@ readme.workspace = true documentation.workspace = true [dependencies] +secrecy = "0.10.3" oauth2 = "5.0.0" thiserror.workspace = true +tracing.workspace = true +url = { workspace = true, features = ["serde"] } diff --git a/lib/auth-service/src/client/mod.rs b/lib/auth-service/src/client/mod.rs new file mode 100644 index 0000000..25cf16c --- /dev/null +++ b/lib/auth-service/src/client/mod.rs @@ -0,0 +1,49 @@ +use oauth2::{AuthUrl, ClientId, ClientSecret, EndpointNotSet, EndpointSet, RedirectUrl, TokenUrl}; +use secrecy::{ExposeSecret, SecretString}; +use tracing::debug; +use url::Url; + +use crate::AuthServiceError; + +pub struct OauthClient( + oauth2::basic::BasicClient< + EndpointSet, + EndpointNotSet, + EndpointNotSet, + EndpointNotSet, + EndpointSet, + >, +); + +pub struct ClientConfig { + client_id: String, + client_secret: SecretString, + token_url: Url, + auth_url: Url, +} + +impl TryFrom for OauthClient { + type Error = AuthServiceError; + + fn try_from(value: ClientConfig) -> Result { + debug!("creating oauth client"); + Ok(Self( + oauth2::basic::BasicClient::new(ClientId::new(value.client_id)) + .set_client_secret(ClientSecret::new( + value.client_secret.expose_secret().to_string(), + )) + .set_auth_uri(AuthUrl::from_url(value.auth_url)) + .set_token_uri(TokenUrl::from_url(value.token_url)), + )) + } +} + +impl OauthClient { + #[must_use] + pub fn with_redirect_url(self, url: &Url) -> Self { + Self( + self.0 + .set_redirect_uri(RedirectUrl::from_url(url.to_owned())), + ) + } +} diff --git a/lib/auth-service/src/lib.rs b/lib/auth-service/src/lib.rs index b93cf3f..f7b9e80 100644 --- a/lib/auth-service/src/lib.rs +++ b/lib/auth-service/src/lib.rs @@ -1,14 +1,15 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right -} +pub mod client; -#[cfg(test)] -mod tests { - use super::*; +use thiserror::Error; - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } +#[derive(Error, Debug)] +pub enum AuthServiceError { + #[error("invalid url provided")] + InvalidUrl(#[from] url::ParseError), + #[error("the data for key `{0}` is not available")] + Redaction(String), + #[error("invalid header (expected {expected:?}, found {found:?})")] + InvalidHeader { expected: String, found: String }, + #[error("unknown data store error")] + Unknown, } -- cgit v1.2.3