pythonpython-3.xswaggeropenapiconnexion

How do I specify x-apikeyInfoFunc in swager securityDefinitions?


I have openapi that defines API with this securityDefinitions:

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

When I start the project I get this warning:

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

And such error when I try to use API methods:

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

I found that I need to specify x-apikeyInfoFunc in securityDefinitions. I specified a function that I believe does authentication:

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

The function itself:

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

The function is used as a decorator to authenticate every API method. When I start the project I don't get warning. But I get another error when I actually trying to use one of API method:

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'

I'm stuck on this point, don't have idea how to proceed. connexion 2.6.0 is used in this case.


Solution

  • According to Connexion docs, the x-apikeyInfoFunc function must have two parameters: apikey and required_scopes.

    Example 1
    Example 2