如何在swager securityDefinitions中指定x-apikeyInfoFunc



我有一个openapi,它定义了具有此安全性的API定义:

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'

我被这一点卡住了,不知道该怎么办。在这种情况下使用连接2.6.0。

根据Connexion文档,x-apikeyInfoFunc函数必须有两个参数:apikeyrequired_scopes

示例1
示例2

相关内容

  • 没有找到相关文章

最新更新