From 48d09c8ae30a3c2c679edb7a6c279160e2c45b92 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 20 Aug 2023 12:08:14 +0200 Subject: [PATCH] refactor(auth): dyn Introspection Signed-off-by: kjuulh --- como_auth/src/introspection.rs | 79 +++++++++++++++++++--------------- como_auth/src/lib.rs | 3 +- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/como_auth/src/introspection.rs b/como_auth/src/introspection.rs index 590484e..de91ecc 100644 --- a/como_auth/src/introspection.rs +++ b/como_auth/src/introspection.rs @@ -1,4 +1,6 @@ -use anyhow::Context; +use std::sync::Arc; + +use async_trait::async_trait; use axum::extract::FromRef; use openidconnect::IntrospectionUrl; use zitadel::{ @@ -7,44 +9,53 @@ use zitadel::{ oidc::{discovery::discover, introspection::AuthorityAuthentication}, }; +use crate::AuthClap; + +#[async_trait] +pub trait Introspection { + async fn get_user(&self) -> anyhow::Result<()>; +} + +pub struct IntrospectionService(Arc); + +impl IntrospectionService { + pub async fn new_zitadel(config: &AuthClap) -> anyhow::Result { + let res = IntrospectionStateBuilder::new(&config.zitadel.authority_url.clone().unwrap()) + .with_basic_auth( + &config.zitadel.client_id.clone().unwrap(), + &config.zitadel.client_secret.clone().unwrap(), + ) + .build() + .await?; + + Ok(IntrospectionService(Arc::new(ZitadelIntrospection::new( + res, + )))) + } +} + +pub struct ZitadelIntrospection { + state: IntrospectionState, +} + +impl ZitadelIntrospection { + pub fn new(state: IntrospectionState) -> Self { + Self { state } + } +} + +#[async_trait] +impl Introspection for ZitadelIntrospection { + async fn get_user(&self) -> anyhow::Result<()> { + Ok(()) + } +} + #[derive(Clone, Debug)] pub struct IntrospectionState { pub(crate) config: IntrospectionConfig, } -#[derive(clap::Args, Clone, Debug, PartialEq, Eq)] -pub struct IntrospectionConfigClap { - // #[arg( - // env = "ZITADEL_AUTHORITY", - // long = "zitadel-authority", - // group = "zitadel" - // )] - pub authority: Option, - - // #[arg( - // env = "ZITADEL_CLIENT_ID", - // long = "zitadel-client-id", - // group = "zitadel" - // )] - pub client_id: Option, - // #[arg( - // env = "ZITADEL_CLIENT_SECRET", - // long = "zitadel-client-secret", - // group = "zitadel" - // )] - pub client_secret: Option, -} - -impl IntrospectionConfigClap { - async fn try_into(self) -> anyhow::Result { - IntrospectionStateBuilder::new(&self.authority.unwrap()) - .with_basic_auth(&self.client_id.unwrap(), &self.client_secret.unwrap()) - .build() - .await - .context("failed to generate an introspection builder") - } -} - /// Configuration that must be inject into the axum application state. Used by the /// [IntrospectionStateBuilder](super::IntrospectionStateBuilder). This struct is also used to create the [IntrospectionState](IntrospectionState) #[derive(Debug, Clone)] diff --git a/como_auth/src/lib.rs b/como_auth/src/lib.rs index 0928256..439903f 100644 --- a/como_auth/src/lib.rs +++ b/como_auth/src/lib.rs @@ -1,4 +1,3 @@ -pub use introspection::IntrospectionConfigClap; use oauth::{OAuth, ZitadelConfig}; use serde::{Deserialize, Serialize}; @@ -66,7 +65,7 @@ impl TryFrom for OAuth { } impl AuthClap { - pub(crate) fn merge(&mut self, config: AuthConfigFile) -> &mut Self { + pub 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 {