refactor(auth): dyn Introspection
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-08-20 12:08:14 +02:00
parent f65e85dbe1
commit 48d09c8ae3
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
2 changed files with 46 additions and 36 deletions

View File

@ -1,4 +1,6 @@
use anyhow::Context; use std::sync::Arc;
use async_trait::async_trait;
use axum::extract::FromRef; use axum::extract::FromRef;
use openidconnect::IntrospectionUrl; use openidconnect::IntrospectionUrl;
use zitadel::{ use zitadel::{
@ -7,44 +9,53 @@ use zitadel::{
oidc::{discovery::discover, introspection::AuthorityAuthentication}, oidc::{discovery::discover, introspection::AuthorityAuthentication},
}; };
use crate::AuthClap;
#[async_trait]
pub trait Introspection {
async fn get_user(&self) -> anyhow::Result<()>;
}
pub struct IntrospectionService(Arc<dyn Introspection + Send + Sync + 'static>);
impl IntrospectionService {
pub async fn new_zitadel(config: &AuthClap) -> anyhow::Result<Self> {
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)] #[derive(Clone, Debug)]
pub struct IntrospectionState { pub struct IntrospectionState {
pub(crate) config: IntrospectionConfig, 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<String>,
// #[arg(
// env = "ZITADEL_CLIENT_ID",
// long = "zitadel-client-id",
// group = "zitadel"
// )]
pub client_id: Option<String>,
// #[arg(
// env = "ZITADEL_CLIENT_SECRET",
// long = "zitadel-client-secret",
// group = "zitadel"
// )]
pub client_secret: Option<String>,
}
impl IntrospectionConfigClap {
async fn try_into(self) -> anyhow::Result<IntrospectionState> {
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 /// 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) /// [IntrospectionStateBuilder](super::IntrospectionStateBuilder). This struct is also used to create the [IntrospectionState](IntrospectionState)
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -1,4 +1,3 @@
pub use introspection::IntrospectionConfigClap;
use oauth::{OAuth, ZitadelConfig}; use oauth::{OAuth, ZitadelConfig};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -66,7 +65,7 @@ impl TryFrom<AuthClap> for OAuth {
} }
impl AuthClap { 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(zitadel) = config.zitadel {
if let Some(auth_url) = zitadel.auth_url { if let Some(auth_url) = zitadel.auth_url {
if let Some(_) = self.zitadel.auth_url { if let Some(_) = self.zitadel.auth_url {