pub struct JwksCache { /* private fields */ }Expand description
Caches JWKS decoding keys with a configurable TTL.
Thread-safe: all access goes through a parking_lot::RwLock.
When the cache expires, the next call to get_key
triggers a background-safe refresh. A tokio::sync::Mutex serialises
concurrent refresh attempts to prevent thundering-herd behaviour.
Implementations§
Source§impl JwksCache
impl JwksCache
Sourcepub async fn from_oidc_issuer(
issuer: &str,
ttl: Duration,
) -> Result<Self, AuthError>
pub async fn from_oidc_issuer( issuer: &str, ttl: Duration, ) -> Result<Self, AuthError>
Create a cache by first performing OIDC discovery on the given issuer URL, then eagerly fetching the JWKS.
Sourcepub fn from_keys(keys: HashMap<String, CachedKey>) -> Self
pub fn from_keys(keys: HashMap<String, CachedKey>) -> Self
Create a cache pre-populated with the given keys (useful for testing).
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Whether the cached keys have expired (or were never fetched).
Sourcepub async fn refresh(&self) -> Result<(), AuthError>
pub async fn refresh(&self) -> Result<(), AuthError>
Fetch the JWKS from the configured URL and update the cache.
On failure, previously cached keys are preserved (stale-on-error) so that transient network failures don’t immediately break JWT validation for all requests.
Sourcepub async fn get_key(&self, kid: &str) -> Result<CachedKey, AuthError>
pub async fn get_key(&self, kid: &str) -> Result<CachedKey, AuthError>
Get a cached key by its kid (key ID).
If the cache has expired, it is refreshed first. Concurrent callers share a single refresh request (thundering-herd protection).
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for JwksCache
impl !UnwindSafe for JwksCache
impl Freeze for JwksCache
impl Send for JwksCache
impl Sync for JwksCache
impl Unpin for JwksCache
impl UnsafeUnpin for JwksCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more