如何指定用于使用 Flask-connexion 和 swagger-2.0 验证基本身份验证的函数 API

How to specify the function used to validate the Basic auth with Flask-connexion and swagger-2.0 API

我正在使用带有 Flask-connexion 的 swagger 2.0 API。

在 swagger.yml 文件中,我将安全定义设置为基本:

  BasicAuth:
    type: basic

然后,我将此安全性添加到我想要保护的路径中。

# Tenant paths
  /tenant:
    get:
      operationId: tenant.read_all
      tags:
        - Tenant
      summary: Read the entire set of tenants, sorted by name
      description: Read the entire set of tenants, sorted by name
      security:
        - basicAuth: []
      responses:
        200:
          description: Successfully read tenant set operation
          schema:
            type: array
            items:
              $ref: '#/definitions/Tenant'

但我不明白如何指定验证登录名和密码的函数。我需要收集这些参数并在调用路径函数之前进行验证。

如果这使用 Flask-Login 或 Flask-BasicAuth 隐式定义?

或者是否应该通过在我的 tenant.py 文件中添加代码来在没有 Flask-connexion 的情况下明确完成,例如:

@auth_basic.login_required
def read_all():
...

我希望 Flask-connexion 重定向到验证登录名和密码的 auth 函数,然后重定向到路径 method/function.

https://connexion.readthedocs.io/en/latest/security.html#basic-authentication

您必须在 Swagger 文件中定义:

securityDefinitions: basic: type: basic x-basicInfoFunc: app.basic_auth

x-basicInfoFunc 将映射到验证函数,在此示例中,函数 basic_auth应用文件.

使用 Swagger 的完整示例:https://github.com/zalando/connexion/tree/master/examples/swagger2/basicauth

使用 OpenApi 的完整示例:https://github.com/zalando/connexion/tree/master/examples/openapi3/basicauth