diff --git a/como_auth/src/lib.rs b/como_auth/src/lib.rs index 3547edc..f406014 100644 --- a/como_auth/src/lib.rs +++ b/como_auth/src/lib.rs @@ -1,4 +1,5 @@ pub use introspection::IntrospectionConfigClap; +use oauth::{OAuth, ZitadelConfig}; mod introspection; mod oauth; @@ -47,7 +48,16 @@ pub struct ZitadelClap { pub token_url: Option, } -impl AuthClap {} +impl TryFrom for OAuth { + type Error = anyhow::Error; + + fn try_from(value: AuthClap) -> Result { + match value.engine { + AuthEngine::Noop => Ok(OAuth::new_noop()), + AuthEngine::Zitadel => Ok(OAuth::from(ZitadelConfig::try_from(value.zitadel)?)), + } + } +} #[cfg(test)] mod test { diff --git a/como_auth/src/oauth.rs b/como_auth/src/oauth.rs index b790d3d..a3babf3 100644 --- a/como_auth/src/oauth.rs +++ b/como_auth/src/oauth.rs @@ -3,6 +3,8 @@ use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, T use std::ops::Deref; use std::sync::Arc; +use crate::ZitadelClap; + #[async_trait] pub trait OAuthClient { async fn get_token(&self) -> anyhow::Result<()>; @@ -104,6 +106,33 @@ impl From for ZitadelOAuthClient { } } +impl TryFrom for ZitadelConfig { + type Error = anyhow::Error; + + fn try_from(value: ZitadelClap) -> Result { + Ok(Self { + auth_url: value + .auth_url + .ok_or(anyhow::anyhow!("auth_url was not set"))?, + client_id: value + .client_id + .ok_or(anyhow::anyhow!("client_id was not set"))?, + client_secret: value + .client_secret + .ok_or(anyhow::anyhow!("client_secret was not set"))?, + redirect_url: value + .redirect_url + .ok_or(anyhow::anyhow!("redirect_url was not set"))?, + token_url: value + .token_url + .ok_or(anyhow::anyhow!("token_url was not set"))?, + authority_url: value + .authority_url + .ok_or(anyhow::anyhow!("authority_url was not set"))?, + }) + } +} + #[async_trait] impl OAuthClient for ZitadelOAuthClient { async fn get_token(&self) -> anyhow::Result<()> { @@ -113,10 +142,7 @@ impl OAuthClient for ZitadelOAuthClient { #[cfg(test)] mod tests { - use crate::{ - oauth::{OAuth, ZitadelConfig}, - ZitadelClap, - }; + use crate::ZitadelClap; use clap::Parser; use sealed_test::prelude::*;