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


##########
providers/fab/docs/changelog.rst:
##########
@@ -20,6 +20,10 @@
 Changelog
 ---------
 
+.. note::
+    This release includes a database migration that adds a 
``token_generation`` column to the
+    ``ab_user`` table. Run ``airflow db migrate`` after upgrading the provider.

Review Comment:
   Should we also mention that a password change made from another process (CLI 
`users reset-password`, another API server) only applies there once the cached 
user expires, so up to `[fab] cache_ttl`?



##########
providers/fab/src/airflow/providers/fab/auth_manager/security_manager/override.py:
##########
@@ -601,7 +607,10 @@ def create_login_manager(self) -> LoginManager:
         lm = LoginManager(current_app)
         lm.anonymous_user = AnonymousUser
         lm.login_view = "login"
-        lm.user_loader(self.load_user)
+        lm.user_loader(self.load_session_user)
+        # Scoped to this app: ``user_logged_in`` is shared by every Flask app 
in the process.
+        app = current_app._get_current_object()  # type: ignore[attr-defined]
+        user_logged_in.connect(_record_session_token_generation, sender=app)

Review Comment:
   Could we connect this in `init_session.py`, like `stamp_login_time`? A 
custom `SECURITY_MANAGER_CLASS` that overrides `create_login_manager` without 
calling super never stores the key. `load_session_user` then sends any user who 
has changed their password back to the login page on every request.



##########
providers/fab/tests/unit/fab/auth_manager/security_manager/test_override.py:
##########
@@ -423,6 +427,26 @@ def test_load_user_jwt(self, mock_g):
         assert actual_user is mock_user
         assert mock_g.user is mock_user
 
+    @pytest.mark.parametrize(
+        ("user_token_generation", "loaded"),
+        [
+            pytest.param(0, True, id="before-any-password-change"),
+            pytest.param(1, False, id="after-a-password-change"),
+        ],
+    )
+    def test_load_session_user_counts_session_without_token_generation_as_zero(
+        self, user_token_generation, loaded
+    ):
+        """A Flask session without the key was created before it existed."""
+        sm = EmptySecurityManager()
+        user = Mock(spec=User, token_generation=user_token_generation)
+        app = Flask(__name__)
+        app.secret_key = "test-secret-key"
+
+        with app.test_request_context(), mock.patch.object(sm, "load_user", 
return_value=user):

Review Comment:
   nit: `@mock.patch.object(EmptySecurityManager, "load_user", autospec=True)` 
as a decorator. Same in 
`test_login_racing_password_change_issues_token_for_the_generation_it_read`.



-- 
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