stephen-bracken commented on code in PR #70783:
URL: https://github.com/apache/airflow/pull/70783#discussion_r3768016762


##########
airflow-core/src/airflow/api_fastapi/auth/middlewares/refresh_token.py:
##########
@@ -45,58 +50,114 @@ class JWTRefreshMiddleware(BaseHTTPMiddleware):
 
     async def dispatch(self, request: Request, call_next):
         new_token = None
-        current_token = request.cookies.get(COOKIE_NAME_JWT_TOKEN)
+        current_user = None
+        new_user = None
         try:
-            if current_token is not None:
-                try:
-                    new_user, current_user = await 
self._refresh_user(current_token)
-                    if user := (new_user or current_user):
-                        # Stamp the trust sentinel alongside the user so 
`get_user()`
-                        # can distinguish this trusted assignment from a stray 
write
-                        # by unrelated middleware.
-                        request.state.user = user
-                        request.state.user_authenticated_via = 
USER_INJECTED_BY_TRUSTED_MIDDLEWARE
-                    if new_user:
-                        # If we created a new user, serialize it and set it as 
a cookie
-                        new_token = get_auth_manager().generate_jwt(new_user)
-                except (HTTPException, 
AuthManagerRefreshTokenExpiredException):
-                    # Receive a HTTPException when the Airflow token is expired
-                    # Receive a AuthManagerRefreshTokenExpiredException when 
the potential underlying refresh
-                    # token used by the auth manager is expired
-                    new_token = ""
+            try:
+                new_user, current_user = await self._refresh_user(request)
+            except (HTTPException, AuthManagerRefreshTokenExpiredException):
+                # Receive a HTTPException when the Airflow token is expired
+                # Receive a AuthManagerRefreshTokenExpiredException when the 
potential underlying refresh
+                # token used by the auth manager is expired
+                new_token = ""
+
+            if user := (new_user or current_user):
+                # Stamp the trust sentinel alongside the user so `get_user()`
+                # can distinguish this trusted assignment from a stray write
+                # by unrelated middleware.
+                request.state.user = user
+                request.state.user_authenticated_via = 
USER_INJECTED_BY_TRUSTED_MIDDLEWARE
 
             response = await call_next(request)
 
-            if new_token is not None:
+            if new_user or new_token is not None:
+                secure = request_cookie_is_secure(request)
                 cookie_path = get_cookie_path()
-                secure = request.base_url.scheme == "https" or 
bool(conf.get("api", "ssl_cert", fallback=""))
-                response.set_cookie(
-                    COOKIE_NAME_JWT_TOKEN,
-                    new_token,
-                    path=cookie_path,
-                    httponly=True,
-                    secure=secure,
-                    samesite="lax",
-                    max_age=0 if new_token == "" else None,
-                )
-                # Clear any stale _token cookie at root path "/".
-                # Older Airflow instances may have set the cookie there;
-                # without this, the root-path cookie keeps being sent on
-                # every request, causing an infinite redirect loop.
-                if cookie_path != "/":
-                    response.delete_cookie(

Review Comment:
   under the hood starlette is calling `set_cookie` with `max_age=0`, but I 
agree its an unnecessary change



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