rawwar commented on code in PR #49599:
URL: https://github.com/apache/airflow/pull/49599#discussion_r2058588792


##########
airflow-core/src/airflow/api_fastapi/auth/managers/simple/middleware.py:
##########
@@ -0,0 +1,34 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from __future__ import annotations
+
+from fastapi import Request
+from starlette.middleware.base import BaseHTTPMiddleware
+
+from airflow.api_fastapi.auth.managers.simple.services.login import 
SimpleAuthManagerLogin
+
+
+class SimpleAllAdminMiddleware(BaseHTTPMiddleware):
+    """Middleware that automatically generates and includes auth header for 
simple auth manager."""
+
+    async def dispatch(self, request: Request, call_next):
+        # Starlette Request is expected to be immutable, but we modify it to 
add the auth header
+        # https://github.com/fastapi/fastapi/issues/2727#issuecomment-770202019
+        token = SimpleAuthManagerLogin.create_token_all_admins()
+        request.headers.__dict__["_list"].append((b"authorization", f"Bearer 
{token}".encode()))

Review Comment:
   Another approach I thought of is to extend `OAuth2AuthorizationCodeBearer` 
to allow Authorization header to be optional when 
AIRFLOW__CORE__SIMPLE_AUTH_MANAGER_ALL_ADMINS=True. 
   
   This also needs get_user dep and few other places in SAM to check if 
AIRFLOW__CORE__SIMPLE_AUTH_MANAGER_ALL_ADMINS is set to True and allow the 
request.
   
   Earlier comment - 
https://github.com/apache/airflow/issues/49453#issuecomment-2820103492



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