目前,我有一个使用Connexion并接收OpenAPI规范的工作API:
connexion_app.add_api(
"openapi.yaml",
options={"swagger_ui": False},
validate_responses=True,
strict_validation=True, # Changing this also didn't help
)
响应按以下顺序进行验证:
parameters
。
API-Key的验证是通过OpenAPI规范完成的:
securitySchemes:
apiKeyAuth:
type: apiKey
in: header
name: API-Key
x-apikeyInfoFunc: server.security.check_api_key
security:
- apiKeyAuth: []
验证也是通过OpenAPI规范完成的。
在端点中验证签名:
if not verify_signature(kwargs):
abort(401, "Signature could not be verified")
verify_signature
基本上是这样的:
def verify_signature(request) -> bool:
"""Calculate the signature using the header and data."""
signature = re.findall(r'"([A-Za-z0-9+/=]+)"', connexion.request.headers.get("Message-Signature", ""))
created = re.findall(r"created=(\d+)", connexion.request.headers.get("Message-Signature", ""))
if len(signature) == 0:
abort(401, "No valid Signature found.")
if len(created) == 0:
abort(401, "No valid created timestamp found.")
signature = signature[0]
created = int(created[0])
method, path, host, api_key, content_type = _get_attributes_from_request()
message = create_signature_message(request["body"], created, method, path, host, api_key, content_type)
recreated_signature = _encode_message(message)
return recreated_signature == str(signature)
为了安全起见,我想交换2和3:
如果请求主体包含所有必要的valid
。
问题是,Connexion在到达执行Python代码(如verify_signature
)的端点之前验证了主体。
我尝试在我的OpenAPI.yaml中添加以下内容:
signatureAuth:
type: http
scheme: basic
x-basicInfoFunc: server.security.verify_signature
security:
- apiKeyAuth: []
signatureAuth: []
但我认为这是错误的方法,因为我认为这只是一种简单的验证方法,我得到了以下错误消息:No authorization token provided
。
关于我的问题:
是否有一种方法可以在Connexion验证主体之前执行接收整个请求的函数?
发布于 2022-07-20 12:28:12
是的,您可以使用Connexion before_request
注释,以便在验证主体之前在新请求上运行一个函数。下面是一个记录标题和内容的示例:
import connexion
import logging
from flask import request
logger = logging.getLogger(__name__)
conn_app = connexion.FlaskApp(__name__)
@conn_app.app.before_request
def before_request():
for h in request.headers:
logger.debug('header %s', h)
logger.debug('data %s', request.get_data())
https://stackoverflow.com/questions/71336502
复制相似问题