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


##########
providers/fab/src/airflow/providers/fab/www/extensions/init_session.py:
##########
@@ -62,3 +70,32 @@ def make_session_permanent():
             f"[fab] session_backend: '{selected_backend}'. Please set "
             "this to either 'database' or 'securecookie'."
         )
+
+
+def init_session_max_lifetime(app):
+    """Expire sessions ``[fab] session_max_lifetime_minutes`` after login, 
regardless of activity."""
+    max_lifetime_minutes = conf.getint("fab", "session_max_lifetime_minutes", 
fallback=0)
+    if max_lifetime_minutes <= 0:
+        return
+
+    max_lifetime_seconds = max_lifetime_minutes * 60
+
+    # ``weak=False``: the receiver is a local function, so a weak subscription 
could be collected.
+    @user_logged_in.connect_via(app, weak=False)
+    def stamp_login_time(sender, user, **kwargs):
+        # Wall clock rather than ``time.monotonic()``: the stamp is persisted 
in the session and
+        # read back by other API server processes, which share no monotonic 
clock origin.
+        builtin_flask_session[SESSION_LOGIN_TIME_KEY] = time.time()
+
+    @app.before_request
+    def expire_session_past_max_lifetime():
+        login_time = builtin_flask_session.get(SESSION_LOGIN_TIME_KEY)
+        if login_time is None:
+            # Sessions that predate this setting have no stamp; cap them from 
now on rather than
+            # leaving them exempt forever.
+            if current_user.is_authenticated:
+                builtin_flask_session[SESSION_LOGIN_TIME_KEY] = time.time()
+            return
+        if time.time() - login_time > max_lifetime_seconds:
+            log.debug("Session reached [fab] session_max_lifetime_minutes, 
expiring it.")
+            builtin_flask_session.clear()

Review Comment:
   +1



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