Skip to content

Commit e7edc61

Browse files
committed
Implement FabAuthManager.refresh_user for sliding JWT refresh
JWTRefreshMiddleware (added in 3.1.4) calls auth_manager.refresh_user on every request; if a user is returned it reissues the _token cookie so long-open UI tabs don't burst 401s at token expiry. BaseAuthManager.refresh_user is a no-op by default. KeycloakAuthManager overrides it; FAB does not, so on FAB deployments the silent-refresh path does nothing and UI tabs 401 every jwt_expiration_time interval. Return the current user unconditionally for authenticated FAB sessions. deserialize_user already re-fetches the user from DB by token['sub'], so the object is fresh and returning it is safe. Related: apache#57065
1 parent 40ced09 commit e7edc61

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ def _fetch_user() -> User:
315315
def serialize_user(self, user: User) -> dict[str, Any]:
316316
return {"sub": str(user.id)}
317317

318+
def refresh_user(self, *, user: User) -> User | None:
319+
if not user or user.is_anonymous:
320+
return None
321+
return user
322+
318323
def is_logged_in(self) -> bool:
319324
"""Return whether the user is logged in."""
320325
user = self.get_user()

‎providers/fab/tests/unit/fab/auth_manager/test_fab_auth_manager.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,19 @@ def test_serialize_user(self, flask_app, auth_manager_with_appbuilder):
246246
result = auth_manager_with_appbuilder.serialize_user(user)
247247
assert result == {"sub": str(user.id)}
248248

249+
def test_refresh_user_returns_user_for_authenticated(self, auth_manager_with_appbuilder):
250+
user = Mock()
251+
user.is_anonymous = False
252+
assert auth_manager_with_appbuilder.refresh_user(user=user) is user
253+
254+
def test_refresh_user_returns_none_for_anonymous(self, auth_manager_with_appbuilder):
255+
user = Mock()
256+
user.is_anonymous = True
257+
assert auth_manager_with_appbuilder.refresh_user(user=user) is None
258+
259+
def test_refresh_user_returns_none_for_no_user(self, auth_manager_with_appbuilder):
260+
assert auth_manager_with_appbuilder.refresh_user(user=None) is None
261+
249262
@mock.patch.object(FabAuthManager, "get_user")
250263
def test_is_logged_in(self, mock_get_user, auth_manager_with_appbuilder):
251264
user = Mock()

0 commit comments

Comments
 (0)