vincbeck commented on code in PR #77138:
URL: https://github.com/apache/airflow/pull/77138#discussion_r4615678979
##########
providers/fab/src/airflow/providers/fab/auth_manager/security_manager/override.py:
##########
@@ -614,6 +623,17 @@ def _hash_password(self, password: str) -> str:
method = current_app.config.get("FAB_PASSWORD_HASH_METHOD", "scrypt")
return generate_password_hash(password, method=method)
+ def set_user_password(self, user: User, password: str) -> None:
+ """
+ Set a new password and invalidate the tokens and sessions issued under
the old one.
+
+ Nothing is committed: the caller persists both changes together,
normally with
+ ``update_user``.
+ """
+ user.password = self._hash_password(password)
+ # Incremented in SQL so that concurrent password changes cannot write
the same value.
+ user.token_generation = self.user_model.token_generation + 1
Review Comment:
Nothing evicts the user from `FabAuthManager.cache` here, so even the API
server that made the change keeps accepting old tokens until its cached user
expires (`[fab] cache_ttl`). Can we pop the user from the cache once the change
is committed (in `reset_password` and `FABAuthManagerUsers.update_user`)? Other
processes still have the TTL window, but at least this one rejects old tokens
right away.
##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -310,23 +312,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.
+ user = self._fetch_active_user(user_id)
+ self.cache[user_id] = user
Review Comment:
nit: this way the cache key is only defined in `@cachedmethod`
```suggestion
self.cache.pop(user_id, None)
user = self._get_active_user(user_id)
```
--
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]