sjyangkevin commented on code in PR #77138:
URL: https://github.com/apache/airflow/pull/77138#discussion_r4644144533


##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -310,23 +314,39 @@ def get_user(self) -> User:
     def session(self):
         return self.appbuilder.session
 
-    @cachedmethod(lambda self: self.cache, key=lambda _, token: 
int(token["sub"]))
     def deserialize_user(self, token: dict[str, Any]) -> User:
         user_id = int(token["sub"])
+        # Tokens issued before the claim existed count as the initial 
generation.
+        token_generation = token.get(TOKEN_GENERATION_CLAIM, 0)
+        if type(token_generation) is not int:
+            raise ValueError(f"Token of user with id {user_id} has an invalid 
{TOKEN_GENERATION_CLAIM} claim")
+        user = self._get_active_user(user_id)
+        if token_generation > user.token_generation:
+            # The token was issued after a password change that the cached 
user predates.
+            self.cache.pop(user_id, None)
+            user = self._get_active_user(user_id)
+        if token_generation != user.token_generation:
+            raise ValueError(f"Token of user with id {user_id} was issued 
before their last password change")
+        return user
+
+    @cachedmethod(lambda self: self.cache, key=lambda _, user_id: user_id)

Review Comment:
   Thanks for pointing this out. After rebasing on #76748, the cache is also 
used from the event loop, when tokens are checked on the sync and async paths, 
and from threadpool threads, when `update_user` removes a user. Passing the 
lock to `cachedmethod` would only have covered the sync path, because the async 
path reads and writes the cache directly. So I removed `cachedmethod`. Both 
paths now go through `_get_cached_user`, and every read, write and removal uses 
a single `cache_lock`. The lock is held only around the cache access itself, 
not while querying the database, so it doesn't hold up the event loop. 
`test_cache_is_accessed_only_under_cache_lock` checks this on both paths.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to