ashb commented on a change in pull request #14219:
URL: https://github.com/apache/airflow/pull/14219#discussion_r588303765



##########
File path: airflow/api_connexion/webserver_auth.py
##########
@@ -0,0 +1,71 @@
+# 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.
+
+import logging
+from functools import wraps
+from typing import Callable, TypeVar, cast
+
+from flask import Response, current_app, request
+from flask_appbuilder.const import AUTH_LDAP
+from flask_jwt_extended import verify_jwt_in_request
+from flask_login import login_user
+
+from airflow.configuration import conf
+
+SECRET = conf.get("webserver", "secret_key")
+T = TypeVar("T", bound=Callable)  # pylint: disable=invalid-name
+
+log = logging.getLogger(__name__)
+
+
+def auth_current_user():
+    """Checks the authentication and return a value"""
+    db_login_disabled = conf.getboolean("api", "disable_db_login")
+    auth_header = request.headers.get("Authorization", None)
+    if auth_header and not db_login_disabled:
+        if auth_header.startswith("Basic"):
+            user = None
+            auth = request.authorization
+            if auth is None or not (auth.username and auth.password):
+                return None
+            ab_security_manager = current_app.appbuilder.sm
+            if ab_security_manager.auth_type == AUTH_LDAP:
+                user = ab_security_manager.auth_user_ldap(auth.username, 
auth.password)
+            if user is None:
+                user = ab_security_manager.auth_user_db(auth.username, 
auth.password)
+            if user is not None:
+                login_user(user, remember=False)
+                return user
+    try:
+        verify_jwt_in_request()
+        return 1
+    except Exception as err:  # pylint: disable=broad-except
+        log.debug("Can't verify jwt: %s", str(err))
+    return None
+
+
+def requires_authentication(function: T):
+    """Decorator for functions that require authentication"""
+
+    @wraps(function)
+    def decorated(*args, **kwargs):
+        if auth_current_user() is not None:
+            return function(*args, **kwargs)
+        else:
+            return Response("Unauthorized", 401, {"WWW-Authenticate": 
"Bearer"})
+
+    return cast(T, decorated)

Review comment:
       This one doesn't seem be be used as a decorator, so could be removed 
entirely, and 
   ```python
       if auth_current_user():
   ```
   
   could be used in it's place (see next comment)

##########
File path: airflow/api_connexion/security.py
##########
@@ -14,24 +14,30 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-
 from functools import wraps
 from typing import Callable, Optional, Sequence, Tuple, TypeVar, cast
 
 from flask import Response, current_app
 
 from airflow.api_connexion.exceptions import PermissionDenied, Unauthenticated
+from airflow.api_connexion.webserver_auth import requires_authentication
 
 T = TypeVar("T", bound=Callable)  # pylint: disable=invalid-name
 
 
 def check_authentication() -> None:
     """Checks that the request has valid authorization information."""
     response = current_app.api_auth.requires_authentication(Response)()
-    if response.status_code != 200:
-        # since this handler only checks authentication, not authorization,
-        # we should always return 401
-        raise Unauthenticated(headers=response.headers)
+    if response.status_code == 200:
+        return
+    jwt_response = requires_authentication(Response)()
+    if jwt_response.status_code == 200:
+        return
+    # since this handler only checks authentication, not authorization,
+    # we should always return 401
+    if jwt_response.status_code != 200:
+        raise Unauthenticated(headers=jwt_response.headers)

Review comment:
       ```suggestion
       if auth_current_user():
           return
       raise Unauthenticated(headers={"WWW-Authenticate": "Bearer"})
   ```




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to