Auth0 JWT validation¶
EarthRanger validates Auth0 access tokens with Authlib’s Django ResourceProtector and
Auth0JWTBearerTokenValidator (das/utils/auth0/auth0_validators.py). Keep direct authlib.jose and joserfc
imports inside that module; its tests in das/utils/tests/test_auth0_validators.py are also exempt from the import ban
so they can sign real tokens and exercise the library boundary.
Key rotation¶
Auth0 rotates signing keys, so a token may carry a kid that the cached JWKS has not seen yet. Authlib 1.8 and later
(the repo requires 1.9.2 or later) catches every Joserfc error and raises its OAuth InvalidTokenError, with the
original error on __cause__. An unknown kid therefore surfaces as an InvalidTokenError whose cause is Joserfc’s
InvalidKeyIdError.
authenticate_token relies on exactly that: when it sees such an error it tries to claim the refresh cooldown, forces a
JWKS refresh and retries once. Any other error, or an already-claimed cooldown, re-raises Authlib’s exception and
ResourceProtector returns HTTP 401. The real-token tests fail if a library upgrade changes the exception shape, so do
not replace them with tests that mock authenticate_token.
Only a token whose header kid is missing from the cached JWKS triggers a refresh. Key lookup happens before signature
verification, so such a token is unauthenticated input: anyone can send one. The cooldown bounds the cost, and the
retried decode verifies signature and claims in full against the refreshed keys.
Cooldown¶
The cooldown is one atomic cache
addof<jwks cache key>_refresh_cooldownforJWKS_FORCED_REFRESH_COOLDOWN_S(60 seconds), claimed before the fetch. This allows at most one unknown-kidforced refresh per window per issuer. A cache-miss fill also claims the cooldown, because those keys are fresh, so a cold-cache request with an unknownkidfetches once, not twice. The scheduled refresh task bypasses the cooldown.The one-fetch-per-window guarantee covers forced (unknown-
kid) refreshes and a single cold request. Cache fills are not single-flight: simultaneous requests on a cold cache may each fetch once, bounded by the number of concurrent requests at cold start or TTL expiry. The scheduled refresh task keeps the cache warm.The key embeds a SHA-256 digest of the issuer, so every worker computes the same key. It lives in the shared cache alias, which has no tenant key function: workers and tenants configured with the same Auth0 issuer share the cooldown. Do not add a tenant id to the key.
A JWKS payload is cached only if Joserfc can import it as a non-empty key set, so an upstream error object or
{}is never cached.
Failure behaviour and trade-offs¶
Refreshing is best effort. An error while claiming the cooldown, or a failed or invalid forced fetch, is logged as a warning and treated as “no refresh”: Authlib rejects the token normally with 401, not 500, and previously cached keys stay in place. This does not cover everything: an error while reading the cached JWKS in the first place (for example a cache outage on a cold start) is not swallowed and surfaces as a 500, as before.
The cooldown is spent even if the fetch fails, which protects Auth0 during an outage.
A garbage
kidsent just before a genuine rotation can use up the window; tokens signed with the new key then get 401 until the cooldown ends and another unknown-kidrequest refreshes, or until the cached JWKS expires.A genuine rotation within the window after a cache fill is not refreshed until the window ends.
A worker that claims the cooldown and then crashes suppresses refreshes for up to 60 seconds.
Requests that arrive while another worker’s refresh is still in flight validate against the old keys and may see a transient 401.