potiuk commented on code in PR #44311:
URL: https://github.com/apache/airflow/pull/44311#discussion_r1863135186


##########
providers/src/airflow/providers/edge/worker_api/auth.py:
##########
@@ -0,0 +1,98 @@
+# 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 functools import cache
+
+from itsdangerous import BadSignature
+from jwt import (
+    ExpiredSignatureError,
+    ImmatureSignatureError,
+    InvalidAudienceError,
+    InvalidIssuedAtError,
+    InvalidSignatureError,
+)
+
+from airflow.configuration import conf
+from airflow.providers.edge.worker_api.datamodels import JsonRpcRequestBase  # 
noqa: TCH001
+from airflow.providers.edge.worker_api.routes._v2_compat import (
+    Header,
+    HTTPException,
+    Request,
+    status,
+)
+from airflow.utils.jwt_signer import JWTSigner
+
+
+@cache
+def jwt_signer() -> JWTSigner:
+    clock_grace = conf.getint("core", "internal_api_clock_grace", fallback=30)
+    return JWTSigner(
+        secret_key=conf.get("core", "internal_api_secret_key"),
+        expiration_time_in_seconds=clock_grace,
+        leeway_in_seconds=clock_grace,
+        audience="api",
+    )
+
+
+def jwt_token_authorization(method: str, authorization: str):
+    """Check if the JWT token is correct."""
+    try:
+        payload = jwt_signer().verify_token(authorization)
+        signed_method = payload.get("method")
+        if not signed_method or signed_method != method:
+            raise BadSignature("Invalid method in token authorization.")
+    except BadSignature:
+        raise HTTPException(
+            status.HTTP_403_FORBIDDEN, "Bad Signature. Please use only the 
tokens provided by the API."

Review Comment:
   Generally, when there is an authorisation error, from security point of view 
you should not tell what happens, just send "forbidden" and log the actual 
cause in the server log. 
   
   Yes, it is difficult to debug, but "secure by default" means that you should 
not reveal potential attacker any information that can make it easier for them 
to attack.



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