From f65e85dbe1efc0e2517a4285dc9b35934ff16dfe Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 20 Aug 2023 01:44:54 +0200 Subject: [PATCH] feat(auth): add file merge as base Signed-off-by: kjuulh --- como_auth/src/lib.rs | 50 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/como_auth/src/lib.rs b/como_auth/src/lib.rs index f406014..0928256 100644 --- a/como_auth/src/lib.rs +++ b/como_auth/src/lib.rs @@ -1,5 +1,6 @@ pub use introspection::IntrospectionConfigClap; use oauth::{OAuth, ZitadelConfig}; +use serde::{Deserialize, Serialize}; mod introspection; mod oauth; @@ -26,7 +27,12 @@ pub struct AuthClap { pub zitadel: ZitadelClap, } -#[derive(clap::Args, Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct AuthConfigFile { + zitadel: Option, +} + +#[derive(clap::Args, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[group(requires_all = ["auth_url", "client_id", "client_secret", "redirect_url", "token_url", "authority_url"])] pub struct ZitadelClap { #[arg(env = "ZITADEL_AUTH_URL", long = "zitadel-auth-url")] @@ -59,6 +65,45 @@ impl TryFrom for OAuth { } } +impl AuthClap { + pub(crate) fn merge(&mut self, config: AuthConfigFile) -> &mut Self { + if let Some(zitadel) = config.zitadel { + if let Some(auth_url) = zitadel.auth_url { + if let Some(_) = self.zitadel.auth_url { + _ = self.zitadel.auth_url.replace(auth_url); + } + } + if let Some(client_id) = zitadel.client_id { + if let Some(_) = self.zitadel.client_id { + _ = self.zitadel.client_id.replace(client_id); + } + } + if let Some(client_secret) = zitadel.client_secret { + if let Some(_) = self.zitadel.client_secret { + _ = self.zitadel.client_secret.replace(client_secret); + } + } + if let Some(redirect_url) = zitadel.redirect_url { + if let Some(_) = self.zitadel.redirect_url { + _ = self.zitadel.redirect_url.replace(redirect_url); + } + } + if let Some(authority_url) = zitadel.authority_url { + if let Some(_) = self.zitadel.authority_url { + _ = self.zitadel.authority_url.replace(authority_url); + } + } + if let Some(token_url) = zitadel.token_url { + if let Some(_) = self.zitadel.token_url { + _ = self.zitadel.token_url.replace(token_url); + } + } + } + + self + } +} + #[cfg(test)] mod test { use crate::{AuthClap, AuthEngine, ZitadelClap}; @@ -129,8 +174,7 @@ mod test { let cli: Cli = Cli::parse_from(&[ "base", "one", - "--auth-engine", - "zitadel", + "--auth-engine=zitadel", "--zitadel-client-id=something", "--zitadel-client-secret=something", "--zitadel-auth-url=https://something",