grafbase_sdk/extension/
authentication.rsuse crate::{
types::{Configuration, Directive, ErrorResponse, Token},
wit::Headers,
Error,
};
use super::Extension;
type InitFn = Box<dyn Fn(Vec<Directive>, Configuration) -> Result<Box<dyn Authenticator>, Box<dyn std::error::Error>>>;
pub(super) static mut EXTENSION: Option<Box<dyn Authenticator>> = None;
pub static mut INIT_FN: Option<InitFn> = None;
pub(super) fn get_extension() -> Result<&'static mut dyn Authenticator, Error> {
unsafe {
EXTENSION.as_deref_mut().ok_or_else(|| Error {
message: "Resolver extension not initialized correctly.".to_string(),
extensions: Vec::new(),
})
}
}
pub(super) fn init(directives: Vec<Directive>, configuration: Configuration) -> Result<(), Box<dyn std::error::Error>> {
unsafe {
let init = INIT_FN.as_ref().expect("Resolver extension not initialized correctly.");
EXTENSION = Some(init(directives, configuration)?);
}
Ok(())
}
#[doc(hidden)]
pub fn register(f: InitFn) {
unsafe {
INIT_FN = Some(f);
}
}
pub trait Authenticator: Extension {
fn authenticate(&mut self, headers: Headers) -> Result<Token, ErrorResponse>;
}