From faa3313ebb95e028ad7f1da5d72c94bccbc17a4c Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sun, 8 Nov 2026 20:40:56 -0400 Subject: [PATCH] fix: propagate async JWKS fetch cancellation --- .../authentication/async_token_verifier.py | 2 +- .../test_async_token_verifier.py | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/authentication/test_async_token_verifier.py diff --git a/src/auth0/authentication/async_token_verifier.py b/src/auth0/authentication/async_token_verifier.py index dc32d498..2ccb0a5b 100644 --- a/src/auth0/authentication/async_token_verifier.py +++ b/src/auth0/authentication/async_token_verifier.py @@ -91,7 +91,7 @@ async def _fetch_jwks(self, force: bool = False) -> dict[str, RSAPublicKey]: try: jwks = await self._async_client.get(self._jwks_url) self._cache_jwks(jwks) - except: # noqa: E722 + except Exception: return self._cache_value return self._cache_value diff --git a/tests/authentication/test_async_token_verifier.py b/tests/authentication/test_async_token_verifier.py new file mode 100644 index 00000000..dd85bb9a --- /dev/null +++ b/tests/authentication/test_async_token_verifier.py @@ -0,0 +1,36 @@ +import asyncio +import unittest +from unittest.mock import AsyncMock + +from auth0.authentication.async_token_verifier import AsyncJwksFetcher +from auth0.authentication.exceptions import TokenValidationError + + +class TestAsyncJwksFetcher(unittest.IsolatedAsyncioTestCase): + async def test_cancellation_propagates_without_retry(self): + fetcher = AsyncJwksFetcher("https://example.invalid/jwks") + started = asyncio.Event() + + async def get_jwks(url): + started.set() + await asyncio.Future() + + fetcher._async_client.get = AsyncMock(side_effect=get_jwks) + task = asyncio.create_task(fetcher.get_key("test-key")) + try: + await asyncio.wait_for(started.wait(), timeout=1) + task.cancel() + with self.assertRaises(asyncio.CancelledError): + await asyncio.wait_for(task, timeout=1) + self.assertTrue(task.cancelled()) + fetcher._async_client.get.assert_awaited_once() + finally: + task.cancel() + await asyncio.gather(task, return_exceptions=True) + + async def test_request_error_still_reports_missing_key(self): + fetcher = AsyncJwksFetcher("https://example.invalid/jwks") + fetcher._async_client.get = AsyncMock(side_effect=RuntimeError("request failed")) + + with self.assertRaisesRegex(TokenValidationError, "RSA Public Key"): + await fetcher.get_key("test-key")