49
49
from synapse .types import Requester , UserID , create_requester
50
50
from synapse .util import json_decoder
51
51
from synapse .util .caches .cached_call import RetryOnExceptionCachedCall
52
- from synapse .util .caches .response_cache import ResponseCache
52
+ from synapse .util .caches .response_cache import ResponseCache , ResponseCacheContext
53
53
54
54
if TYPE_CHECKING :
55
55
from synapse .rest .admin .experimental_features import ExperimentalFeature
@@ -279,7 +279,9 @@ async def _introspection_endpoint(self) -> str:
279
279
metadata = await self ._issuer_metadata .get ()
280
280
return metadata .get ("introspection_endpoint" )
281
281
282
- async def _introspect_token (self , token : str ) -> IntrospectionResult :
282
+ async def _introspect_token (
283
+ self , token : str , cache_context : ResponseCacheContext [str ]
284
+ ) -> IntrospectionResult :
283
285
"""
284
286
Send a token to the introspection endpoint and returns the introspection response
285
287
@@ -295,6 +297,8 @@ async def _introspect_token(self, token: str) -> IntrospectionResult:
295
297
Returns:
296
298
The introspection response
297
299
"""
300
+ # By default, we shouldn't cache the result unless we know it's valid
301
+ cache_context .should_cache = False
298
302
introspection_endpoint = await self ._introspection_endpoint ()
299
303
raw_headers : Dict [str , str ] = {
300
304
"Content-Type" : "application/x-www-form-urlencoded" ,
@@ -352,6 +356,8 @@ async def _introspect_token(self, token: str) -> IntrospectionResult:
352
356
"The introspection endpoint returned an invalid JSON response."
353
357
)
354
358
359
+ # We had a valid response, so we can cache it
360
+ cache_context .should_cache = True
355
361
return IntrospectionResult (
356
362
IntrospectionToken (** resp ), retrieved_at_ms = self ._clock .time_msec ()
357
363
)
@@ -482,7 +488,7 @@ async def get_user_by_access_token(
482
488
483
489
try :
484
490
introspection_result = await self ._introspection_cache .wrap (
485
- token , self ._introspect_token , token
491
+ token , self ._introspect_token , token , cache_context = True
486
492
)
487
493
except Exception :
488
494
logger .exception ("Failed to introspect token" )
0 commit comments