如何在 swager securityDefinitions 中指定 x-apikeyInfoFunc?

How do I specify x-apikeyInfoFunc in swager securityDefinitions?

我有定义 API 的 openapi,其 securityDefinitions:

securityDefinitions:
  APIKeyHeader:
    type: apiKey
    in: header
    name: Authorization
security:
  - APIKeyHeader: []

当我开始这个项目时,我收到这个警告:

WARNING    2022-01-27 13:24:41,001 connexion.operations.secure    security_decorator                   142 : ... x-apikeyInfoFunc missing

当我尝试使用 API 方法时出现这样的错误:

INFO       2022-01-27 13:56:15,256 connexion.api.security         get_authorization_info               131 : ... No auth provided. Aborting with 401.

我发现我需要在 securityDefinitions 中指定 x-apikeyInfoFunc。我指定了一个我认为可以进行身份​​验证的函数:

securityDefinitions:
  APIKeyHeader:
    type: apiKey
    in: header
    name: Authorization
    x-apikeyInfoFunc: util.authentication_decorator.authenticate
security:
  - APIKeyHeader: []

函数本身:

def authenticate(arg: Optional[Sequence[str]] = DEFAULT_SCOPE):
    """ decorator to handle api key authentication """
    def decorator(fun):
        """ decorator that gets applied to the function """
        def wrapper(*a, **kw):
            """ function wrapper """
            # pylint: disable=unused-argument
            api_key = request.headers.get('Authorization')
            if validate_scope(api_key, scopes):
                # return fun(*a, **kw)
                return fun()
            LOGGER.debug('Invalid or missing API key in request')
            return {'msg': 'Make sure you supply your API key with sufficient scope in the Authorization header'}, 403

        return wrapper

    if callable(arg):
        scopes = DEFAULT_SCOPE
        return decorator(arg)

    scopes = arg
    return decorator

该函数用作装饰器来验证每个 API 方法。当我开始这个项目时,我没有收到警告。但是当我实际尝试使用 API 方法之一时出现另一个错误:

ERROR      2022-01-28 13:50:03,330 openapi_helper.app_helper      log_exception                        1891: Exception on /v1/jira/search_issues_by_tags [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.6/site-packages/connexion/decorators/decorator.py", line 48, in wrapper
    response = function(request)
  File "/usr/local/lib/python3.6/site-packages/connexion/decorators/security.py", line 322, in wrapper
    token_info = get_authorization_info(auth_funcs, request, required_scopes)
  File "/usr/local/lib/python3.6/site-packages/connexion/decorators/security.py", line 127, in get_authorization_info
    token_info = func(request, required_scopes)
  File "/usr/local/lib/python3.6/site-packages/connexion/decorators/security.py", line 284, in wrapper
    token_info = apikey_info_func(apikey, required_scopes=required_scopes)
TypeError: authenticate() got an unexpected keyword argument 'required_scopes'

我被困在这一点上,不知道如何进行。在这种情况下使用 connexion 2.6.0。

根据Connexion docsx-apikeyInfoFunc函数必须有两个参数:apikeyrequired_scopes

Example 1
Example 2