use super::JwtPlugin;
use crate::plugins::endpoint_context::{EndpointContext, WithCallbacks};
use better_auth_core::{AuthPlugin, AuthResult, AuthSchema, CreateJwk, Jwk};
use std::{future::Future, pin::Pin, sync::Arc};
pub type JwtAdapterFuture<'a, T> = Pin<Box<dyn Future<Output = AuthResult<T>> + Send + 'a>>;
type Get<S> = dyn for<'a> Fn(&'a EndpointContext<'_, S>) -> JwtAdapterFuture<'a, Option<Vec<Jwk>>>
+ Send
+ Sync;
type Create<S> = dyn for<'a> Fn(CreateJwk, &'a EndpointContext<'_, S>) -> JwtAdapterFuture<'a, Jwk>
+ Send
+ Sync;
pub struct JwtCallbacks<S: AuthSchema> {
pub(super) get: Option<Arc<Get<S>>>,
pub(super) create: Option<Arc<Create<S>>>,
}
impl<S: AuthSchema> Default for JwtCallbacks<S> {
fn default() -> Self {
Self {
get: None,
create: None,
}
}
}
impl<S: AuthSchema> JwtCallbacks<S> {
pub fn get_jwks<F>(mut self, callback: F) -> Self
where
F: for<'a> Fn(&'a EndpointContext<'_, S>) -> JwtAdapterFuture<'a, Option<Vec<Jwk>>>
+ Send
+ Sync
+ 'static,
{
self.get = Some(Arc::new(callback));
self
}
pub fn create_jwk<F>(mut self, callback: F) -> Self
where
F: for<'a> Fn(CreateJwk, &'a EndpointContext<'_, S>) -> JwtAdapterFuture<'a, Jwk>
+ Send
+ Sync
+ 'static,
{
self.create = Some(Arc::new(callback));
self
}
}
impl JwtPlugin {
pub fn callbacks<S: AuthSchema>(self, callbacks: JwtCallbacks<S>) -> impl AuthPlugin<S> {
WithCallbacks {
plugin: self,
callbacks: Arc::new(callbacks),
}
}
}