Class: OmniauthOidc::JwksCache
- Inherits:
-
Object
- Object
- OmniauthOidc::JwksCache
- Defined in:
- lib/omniauth/oidc/jwks_cache.rb
Overview
Thread-safe JWKS cache with configurable TTL and force refresh capability
Defined Under Namespace
Classes: CacheEntry
Constant Summary collapse
- DEFAULT_TTL =
1 hour in seconds
3600
Instance Attribute Summary collapse
-
#ttl ⇒ Object
Get current TTL.
Class Method Summary collapse
-
.clear! ⇒ Object
Clear all cached keys (useful for testing).
- .instance ⇒ Object
-
.invalidate(jwks_uri) ⇒ Object
Force refresh a specific JWKS URI on next access.
Instance Method Summary collapse
-
#clear! ⇒ Object
Clear entire cache.
-
#fetch(jwks_uri, force_refresh: false, &block) ⇒ Object
Fetch JWKS, using cache if valid.
-
#initialize(ttl: DEFAULT_TTL) ⇒ JwksCache
constructor
A new instance of JwksCache.
-
#invalidate(jwks_uri) ⇒ Object
Invalidate a specific URI's cache.
-
#valid?(jwks_uri) ⇒ Boolean
Check if a specific URI's cache is valid.
Constructor Details
#initialize(ttl: DEFAULT_TTL) ⇒ JwksCache
Returns a new instance of JwksCache.
26 27 28 29 30 |
# File 'lib/omniauth/oidc/jwks_cache.rb', line 26 def initialize(ttl: DEFAULT_TTL) @ttl = ttl @cache = {} @mutex = Mutex.new end |
Instance Attribute Details
#ttl ⇒ Object
Get current TTL
74 75 76 |
# File 'lib/omniauth/oidc/jwks_cache.rb', line 74 def ttl @ttl end |
Class Method Details
.clear! ⇒ Object
Clear all cached keys (useful for testing)
16 17 18 |
# File 'lib/omniauth/oidc/jwks_cache.rb', line 16 def clear! instance.clear! end |
.instance ⇒ Object
11 12 13 |
# File 'lib/omniauth/oidc/jwks_cache.rb', line 11 def instance @instance ||= new end |
.invalidate(jwks_uri) ⇒ Object
Force refresh a specific JWKS URI on next access
21 22 23 |
# File 'lib/omniauth/oidc/jwks_cache.rb', line 21 def invalidate(jwks_uri) instance.invalidate(jwks_uri) end |
Instance Method Details
#clear! ⇒ Object
Clear entire cache
66 67 68 69 70 71 |
# File 'lib/omniauth/oidc/jwks_cache.rb', line 66 def clear! @mutex.synchronize do @cache.clear Logging.debug("JWKS cache cleared") end end |
#fetch(jwks_uri, force_refresh: false, &block) ⇒ Object
Fetch JWKS, using cache if valid
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/omniauth/oidc/jwks_cache.rb', line 33 def fetch(jwks_uri, force_refresh: false, &block) @mutex.synchronize do entry = @cache[jwks_uri] if !force_refresh && entry && !expired?(entry) Logging.debug("JWKS cache hit", jwks_uri: jwks_uri) return entry.keys end Logging.info("JWKS cache miss, fetching", jwks_uri: jwks_uri, force_refresh: force_refresh) keys = block.call @cache[jwks_uri] = CacheEntry.new(keys: keys, fetched_at: Time.now) keys end end |
#invalidate(jwks_uri) ⇒ Object
Invalidate a specific URI's cache
58 59 60 61 62 63 |
# File 'lib/omniauth/oidc/jwks_cache.rb', line 58 def invalidate(jwks_uri) @mutex.synchronize do @cache.delete(jwks_uri) Logging.debug("JWKS cache invalidated", jwks_uri: jwks_uri) end end |
#valid?(jwks_uri) ⇒ Boolean
Check if a specific URI's cache is valid
50 51 52 53 54 55 |
# File 'lib/omniauth/oidc/jwks_cache.rb', line 50 def valid?(jwks_uri) @mutex.synchronize do entry = @cache[jwks_uri] entry && !expired?(entry) end end |