use crate::{
component::AnyExtension,
types::{Configuration, Error, ErrorResponse, GatewayHeaders, PublicMetadataEndpoint, RequestContext, Token},
};
pub trait AuthenticationExtension: Sized + 'static {
fn new(config: Configuration) -> Result<Self, Error>;
fn authenticate(&mut self, ctx: &RequestContext, headers: &GatewayHeaders) -> Result<Token, ErrorResponse>;
fn public_metadata(&mut self) -> Result<Vec<PublicMetadataEndpoint>, Error> {
Ok(vec![])
}
}
#[doc(hidden)]
pub fn register<T: AuthenticationExtension>() {
pub(super) struct Proxy<T: AuthenticationExtension>(T);
impl<T: AuthenticationExtension> AnyExtension for Proxy<T> {
fn authenticate(&mut self, ctx: &RequestContext, headers: &GatewayHeaders) -> Result<Token, ErrorResponse> {
self.0.authenticate(ctx, headers)
}
fn public_metadata(&mut self) -> Result<Vec<PublicMetadataEndpoint>, Error> {
AuthenticationExtension::public_metadata(&mut self.0)
}
}
crate::component::register_extension(Box::new(|_, config| {
<T as AuthenticationExtension>::new(config).map(|extension| Box::new(Proxy(extension)) as Box<dyn AnyExtension>)
}))
}