use affinidi_did_resolver_cache_sdk::DIDCacheClient;
use reqwest::Client;
use std::{future::Future, pin::Pin, sync::Arc};
use crate::{AuthorizationTokens, errors::Result};
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub trait CustomAuthHandler: Send + Sync {
fn authenticate<'a>(
&'a self,
profile_did: &'a str,
endpoint_did: &'a str,
did_resolver: &'a DIDCacheClient,
client: &'a Client,
) -> BoxFuture<'a, Result<AuthorizationTokens>>;
}
pub trait CustomRefreshHandler: Send + Sync {
fn refresh<'a>(
&'a self,
profile_did: &'a str,
endpoint_did: &'a str,
current_tokens: &'a AuthorizationTokens,
did_resolver: &'a DIDCacheClient,
client: &'a Client,
) -> BoxFuture<'a, Result<AuthorizationTokens>>;
}
#[derive(Clone)]
pub struct CustomAuthHandlers {
pub auth_handler: Option<Arc<dyn CustomAuthHandler>>,
pub refresh_handler: Option<Arc<dyn CustomRefreshHandler>>,
}
impl CustomAuthHandlers {
pub fn new() -> Self {
Self {
auth_handler: None,
refresh_handler: None,
}
}
pub fn with_auth_handler(mut self, handler: Arc<dyn CustomAuthHandler>) -> Self {
self.auth_handler = Some(handler);
self
}
pub fn with_refresh_handler(mut self, handler: Arc<dyn CustomRefreshHandler>) -> Self {
self.refresh_handler = Some(handler);
self
}
}
impl Default for CustomAuthHandlers {
fn default() -> Self {
Self::new()
}
}