Some checks are pending
ci/woodpecker/pr/test Pipeline is pending
Signed-off-by: kjuulh <contact@kjuulh.io>
73 lines
2.3 KiB
Rust
73 lines
2.3 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::oauth::{zitadel::ZitadelConfig, OAuth};
|
|
|
|
use super::AuthClap;
|
|
|
|
#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug)]
|
|
pub enum AuthEngine {
|
|
Noop,
|
|
Zitadel,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct AuthConfigFile {
|
|
zitadel: Option<ZitadelClap>,
|
|
}
|
|
|
|
#[derive(clap::Args, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
|
|
#[group(requires_all = ["client_id", "client_secret", "redirect_url", "authority_url"])]
|
|
pub struct ZitadelClap {
|
|
#[arg(env = "ZITADEL_CLIENT_ID", long = "zitadel-client-id")]
|
|
pub client_id: Option<String>,
|
|
|
|
#[arg(env = "ZITADEL_CLIENT_SECRET", long = "zitadel-client-secret")]
|
|
pub client_secret: Option<String>,
|
|
|
|
#[arg(env = "ZITADEL_REDIRECT_URL", long = "zitadel-redirect-url")]
|
|
pub redirect_url: Option<String>,
|
|
|
|
#[arg(env = "ZITADEL_AUTHORITY_URL", long = "zitadel-authority-url")]
|
|
pub authority_url: Option<String>,
|
|
}
|
|
|
|
impl TryFrom<AuthClap> for OAuth {
|
|
type Error = anyhow::Error;
|
|
|
|
fn try_from(value: AuthClap) -> Result<Self, Self::Error> {
|
|
match value.engine {
|
|
AuthEngine::Noop => Ok(OAuth::new_noop()),
|
|
AuthEngine::Zitadel => Ok(OAuth::from(ZitadelConfig::try_from(value.zitadel)?)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AuthClap {
|
|
pub fn merge(&mut self, config: AuthConfigFile) -> &mut Self {
|
|
if let Some(zitadel) = config.zitadel {
|
|
if let Some(client_id) = zitadel.client_id {
|
|
if self.zitadel.client_id.is_some() {
|
|
_ = self.zitadel.client_id.replace(client_id);
|
|
}
|
|
}
|
|
if let Some(client_secret) = zitadel.client_secret {
|
|
if self.zitadel.client_secret.is_some() {
|
|
_ = self.zitadel.client_secret.replace(client_secret);
|
|
}
|
|
}
|
|
if let Some(redirect_url) = zitadel.redirect_url {
|
|
if self.zitadel.redirect_url.is_some() {
|
|
_ = self.zitadel.redirect_url.replace(redirect_url);
|
|
}
|
|
}
|
|
if let Some(authority_url) = zitadel.authority_url {
|
|
if self.zitadel.authority_url.is_some() {
|
|
_ = self.zitadel.authority_url.replace(authority_url);
|
|
}
|
|
}
|
|
}
|
|
|
|
self
|
|
}
|
|
}
|