axum_oidc_layer/cache/
mod.rs1use std::{
7 collections::hash_map::DefaultHasher,
8 hash::{Hash, Hasher},
9 time::Duration,
10};
11
12pub mod memory;
13
14pub use memory::InMemoryCache;
15
16pub trait JwksCache: Send + Sync {
21 fn get(&self, key: &str) -> Option<String>;
24
25 fn set(&self, key: &str, value: String, ttl: Duration);
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Hash)]
33pub struct TokenCacheKey(String);
34
35impl TokenCacheKey {
36 #[must_use]
38 pub fn from_token(token: &str) -> Self {
39 let mut hasher = DefaultHasher::new();
40 token.hash(&mut hasher);
41 Self(format!("token:{}", hasher.finish()))
42 }
43
44 #[must_use]
46 pub fn from_token_with_context(
47 token: &str,
48 issuer: Option<&str>,
49 audience: Option<&[String]>,
50 ) -> Self {
51 let mut hasher = DefaultHasher::new();
52 token.hash(&mut hasher);
53 issuer.hash(&mut hasher);
54 if let Some(audience) = audience {
55 for value in audience {
56 value.hash(&mut hasher);
57 }
58 }
59 Self(format!("token:{}", hasher.finish()))
60 }
61
62 #[must_use]
64 pub fn as_str(&self) -> &str {
65 &self.0
66 }
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub struct ConfigCacheKey(String);
74
75impl ConfigCacheKey {
76 #[must_use]
78 pub fn from_url(url: &str) -> Self {
79 Self(format!("config:{url}"))
80 }
81
82 #[must_use]
84 pub fn as_str(&self) -> &str {
85 &self.0
86 }
87}
88
89#[derive(Debug, Clone, PartialEq, Eq, Hash)]
93pub struct JwkCacheKey(String);
94
95impl JwkCacheKey {
96 #[must_use]
98 pub fn from_jwks_uri_and_kid(jwks_uri: &str, kid: &str) -> Self {
99 Self(format!("jwk:{jwks_uri}:{kid}"))
100 }
101
102 #[must_use]
104 pub fn as_str(&self) -> &str {
105 &self.0
106 }
107}