简体   繁体   English

如何指定用于使用 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

I'am using swagger 2.0 API with Flask-connexion.我正在使用带有 Flask-connexion 的 swagger 2.0 API。

In the swagger.yml file, I set the security definition to basic:在 swagger.yml 文件中,我将安全定义设置为基本:

  BasicAuth:
    type: basic

And then, I added this security to the path I want to secure.然后,我将此安全性添加到我想要保护的路径中。

# 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'

But I don't understand how to specify the function which will validate the login, password.但我不明白如何指定验证登录名和密码的功能。 I need to collect these parameters and valide then before the path function get called.在调用路径函数之前,我需要收集这些参数并进行验证。

If this implicitly defined using Flask-Login or Flask-BasicAuth for instance?例如,如果这使用 Flask-Login 或 Flask-BasicAuth 隐式定义?

Or should it be done explicitly as without Flask-connexion by adding code in my tenant.py file such as:或者应该在没有 Flask-connexion 的情况下通过在我的 tenant.py 文件中添加代码来明确完成,例如:

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

I would expect to have Flask-connexion redirect to an auth function which would validate the login and password, and then redirect to path method/function.我希望 Flask-connexion 重定向到验证登录名和密码的 auth 函数,然后重定向到路径方法/函数。

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

You must define in your Swagger file:您必须在 Swagger 文件中定义:

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

The x-basicInfoFunc will map to the validation function , in this example the function basic_auth is in the app file. x-basicInfoFunc将映射到验证函数,在此示例中,函数basic_auth位于应用程序文件中。

Complete example with Swagger: https://github.com/zalando/connexion/tree/master/examples/swagger2/basicauth Swagger 的完整示例: https : //github.com/zalando/connexion/tree/master/examples/swagger2/basicauth

Complete example with OpenApi: https://github.com/zalando/connexion/tree/master/examples/openapi3/basicauth OpenApi 的完整示例: https : //github.com/zalando/connexion/tree/master/examples/openapi3/basicauth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM