refactor(auth): dyn Introspection
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
f65e85dbe1
commit
48d09c8ae3
@ -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<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)]
|
||||
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<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
|
||||
/// [IntrospectionStateBuilder](super::IntrospectionStateBuilder). This struct is also used to create the [IntrospectionState](IntrospectionState)
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -1,4 +1,3 @@
|
||||
pub use introspection::IntrospectionConfigClap;
|
||||
use oauth::{OAuth, ZitadelConfig};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@ -66,7 +65,7 @@ impl TryFrom<AuthClap> 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 {
|
||||
|
Loading…
Reference in New Issue
Block a user