use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
time::Duration,
};
pub mod memory;
pub use memory::InMemoryCache;
pub trait JwksCache: Send + Sync {
fn get(&self, key: &str) -> Option<String>;
fn set(&self, key: &str, value: String, ttl: Duration);
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TokenCacheKey(String);
impl TokenCacheKey {
#[must_use]
pub fn from_token(token: &str) -> Self {
let mut hasher = DefaultHasher::new();
token.hash(&mut hasher);
Self(format!("token:{}", hasher.finish()))
}
#[must_use]
pub fn from_token_with_context(
token: &str,
issuer: Option<&str>,
audience: Option<&[String]>,
) -> Self {
let mut hasher = DefaultHasher::new();
token.hash(&mut hasher);
issuer.hash(&mut hasher);
if let Some(audience) = audience {
for value in audience {
value.hash(&mut hasher);
}
}
Self(format!("token:{}", hasher.finish()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConfigCacheKey(String);
impl ConfigCacheKey {
#[must_use]
pub fn from_url(url: &str) -> Self {
Self(format!("config:{url}"))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct JwkCacheKey(String);
impl JwkCacheKey {
#[must_use]
pub fn from_jwks_uri_and_kid(jwks_uri: &str, kid: &str) -> Self {
Self(format!("jwk:{jwks_uri}:{kid}"))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}