grafbase_sdk/extension/
authentication.rs1use crate::{
2 component::AnyExtension,
3 host::Headers,
4 types::{Configuration, ErrorResponse, Token},
5 Error,
6};
7
8pub trait AuthenticationExtension: Sized + 'static {
10 fn new(config: Configuration) -> Result<Self, Error>;
20
21 fn authenticate(&mut self, headers: Headers) -> Result<Token, ErrorResponse>;
30}
31
32#[doc(hidden)]
33pub fn register<T: AuthenticationExtension>() {
34 pub(super) struct Proxy<T: AuthenticationExtension>(T);
35
36 impl<T: AuthenticationExtension> AnyExtension for Proxy<T> {
37 fn authenticate(&mut self, headers: Headers) -> Result<Token, ErrorResponse> {
38 AuthenticationExtension::authenticate(&mut self.0, headers)
39 }
40 }
41
42 crate::component::register_extension(Box::new(|_, config| {
43 <T as AuthenticationExtension>::new(config).map(|extension| Box::new(Proxy(extension)) as Box<dyn AnyExtension>)
44 }))
45}