JWKSCache#
- class gafaelfawr.cache.JWKSCache(lifetime, min_refresh)#
Bases:
BaseCacheA cache of the signing keys of a single JWT issuer.
This holds the parsed keys from an issuer’s JSON Web Key Set (JWKS), indexed by key ID, along with the JWKS URI discovered from the issuer’s OpenID Connect metadata, for a fixed lifetime so that each token verification does not require retrieving and parsing the keys again. All of the retrieval logic is handled by the verifier that uses it, currently
GitHubActionsTokenVerifier.The caller should first call
getwithout a lock and use the result if it contains the needed key. Otherwise, the caller should take the lock, callgetagain, and only if the key is still missing retrieve a fresh set andstoreit, so that concurrent requests do not all fetch the keys at once. Since anyone can present a token naming an unknown key ID, the caller should also checkrecently_refreshedbefore retrieving the keys again, so that a stream of such tokens cannot force a request to the issuer for each one.- Parameters:
Methods Summary
clear()Invalidate the cache.
get()Retrieve the cached keys, if available and not expired.
Retrieve the cached JWKS URI, if available and not expired.
lock()Return the cache lock for use in a context manager.
Whether the keys were stored within the minimum refresh interval.
store(jwks_uri, keys)Store a freshly retrieved key set in the cache.
Methods Documentation
- async clear()#
Invalidate the cache.
Used primarily for testing.
- Return type:
- Overrides:
- get()#
Retrieve the cached keys, if available and not expired.
- get_jwks_uri()#
Retrieve the cached JWKS URI, if available and not expired.
The URI is cached for the same lifetime as the keys, so that a refresh prompted by an unknown key ID does not have to retrieve the issuer’s OpenID Connect metadata again, but a change to that metadata is picked up once the cache expires.
- lock()#
Return the cache lock for use in a context manager.
- Returns:
The lock for the cache.
- Return type:
- recently_refreshed()#
Whether the keys were stored within the minimum refresh interval.