Compare commits

...

3 Commits

Author SHA1 Message Date
Quentin Gliech
4ab90c0fe8 Fix the typing of the cache context 2025-04-14 19:19:49 +02:00
Quentin Gliech
c6e3de6ed6 Newsfile 2025-04-14 19:17:01 +02:00
Quentin Gliech
6ce626a1c4 Don't cache introspection failures 2025-04-14 19:16:07 +02:00
2 changed files with 10 additions and 3 deletions

1
changelog.d/18339.bugfix Normal file
View File

@@ -0,0 +1 @@
Stop caching introspection failures when delegating auth to MAS.

View File

@@ -48,7 +48,7 @@ from synapse.logging.context import make_deferred_yieldable
from synapse.types import Requester, UserID, create_requester
from synapse.util import json_decoder
from synapse.util.caches.cached_call import RetryOnExceptionCachedCall
from synapse.util.caches.response_cache import ResponseCache
from synapse.util.caches.response_cache import ResponseCache, ResponseCacheContext
if TYPE_CHECKING:
from synapse.rest.admin.experimental_features import ExperimentalFeature
@@ -275,7 +275,9 @@ class MSC3861DelegatedAuth(BaseAuth):
metadata = await self._issuer_metadata.get()
return metadata.get("introspection_endpoint")
async def _introspect_token(self, token: str) -> IntrospectionResult:
async def _introspect_token(
self, token: str, cache_context: ResponseCacheContext[str]
) -> IntrospectionResult:
"""
Send a token to the introspection endpoint and returns the introspection response
@@ -291,6 +293,8 @@ class MSC3861DelegatedAuth(BaseAuth):
Returns:
The introspection response
"""
# By default, we shouldn't cache the result unless we know it's valid
cache_context.should_cache = False
introspection_endpoint = await self._introspection_endpoint()
raw_headers: Dict[str, str] = {
"Content-Type": "application/x-www-form-urlencoded",
@@ -348,6 +352,8 @@ class MSC3861DelegatedAuth(BaseAuth):
"The introspection endpoint returned an invalid JSON response."
)
# We had a valid response, so we can cache it
cache_context.should_cache = True
return IntrospectionResult(
IntrospectionToken(**resp), retrieved_at_ms=self._clock.time_msec()
)
@@ -429,7 +435,7 @@ class MSC3861DelegatedAuth(BaseAuth):
try:
introspection_result = await self._introspection_cache.wrap(
token, self._introspect_token, token
token, self._introspect_token, token, cache_context=True
)
except Exception:
logger.exception("Failed to introspect token")