use std::collections::HashMap;
pub trait Auth: Send + Sync + 'static {
fn resolve(&self, token: &str) -> Option<String>;
}
#[derive(Debug, Clone, Default)]
pub struct BearerAuth {
tokens: HashMap<String, String>,
}
impl BearerAuth {
pub fn new() -> Self {
Self::default()
}
pub fn from_pairs<I, T, V>(iter: I) -> Self
where
I: IntoIterator<Item = (T, V)>,
T: Into<String>,
V: Into<String>,
{
let mut auth = Self::new();
for (t, v) in iter {
auth.insert(t, v);
}
auth
}
pub fn with(mut self, token: impl Into<String>, vid: impl Into<String>) -> Self {
self.insert(token, vid);
self
}
pub fn insert(&mut self, token: impl Into<String>, vid: impl Into<String>) {
self.tokens.insert(token.into(), vid.into());
}
}
impl Auth for BearerAuth {
fn resolve(&self, token: &str) -> Option<String> {
self.tokens.get(token).cloned()
}
}