Skip to content

Commit

Permalink
changed the sub as cache key into token_string as sub might be the sa…
Browse files Browse the repository at this point in the history
…me for multiple users
  • Loading branch information
Josephine.Rutten committed Dec 20, 2024
1 parent 6c3b298 commit 34b63b2
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/cnaas_nms/tools/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_token_info_from_cache(token: Token) -> Optional[dict]:
"""Check if the userinfo is in the cache to avoid multiple calls to the OIDC server"""
try:
with redis_session() as redis: # type: ignore
cached_token_info = redis.hget(REDIS_OAUTH_TOKEN_INFO_KEY, token.decoded_token["sub"])
cached_token_info = redis.hget(REDIS_OAUTH_TOKEN_INFO_KEY, token.token_string)
if cached_token_info:
return json.loads(cached_token_info)
except RedisError as e:
Expand All @@ -33,7 +33,7 @@ def put_token_info_in_cache(token: Token, token_info) -> bool:
try:
with redis_session() as redis: # type: ignore
if "exp" in token.decoded_token:
redis.hsetnx(REDIS_OAUTH_TOKEN_INFO_KEY, token.decoded_token["sub"], token_info)
redis.hsetnx(REDIS_OAUTH_TOKEN_INFO_KEY, token.token_string, token_info)
# expire hash at access_token expiry time or 1 hour from now (whichever is sooner)
# Entire hash is expired, since redis does not support expiry on individual keys
expire_at = min(int(token.decoded_token["exp"]), int(time.time()) + 3600)
Expand Down

0 comments on commit 34b63b2

Please sign in to comment.