refactor(auth): setup convenience for OAuth
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 01:25:46 +02:00
parent 0bb7074334
commit acde8b17e1
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
2 changed files with 41 additions and 5 deletions

View File

@ -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<String>,
}
impl AuthClap {}
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)?)),
}
}
}
#[cfg(test)]
mod test {

View File

@ -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<ZitadelConfig> for ZitadelOAuthClient {
}
}
impl TryFrom<ZitadelClap> for ZitadelConfig {
type Error = anyhow::Error;
fn try_from(value: ZitadelClap) -> Result<Self, Self::Error> {
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::*;