【问题标题】:role based token authentication with apikey security in openapi swagger connexion在 openapi swagger connexion 中使用 apikey 安全性进行基于角色的令牌身份验证
【发布时间】:2021-01-06 16:18:38
【问题描述】:

描述问题

过去几天我一直在努力弄清楚如何在 openapi、swagger、connexion 中使用 apikey 安全性来进行基于角色的令牌身份验证。以下 OpenAPI 3.0 端点定义:

/lab/samples/list:
    get:
      tags:
      - lab
      summary: get a list of all registered samples
      operationId: list_samples
      responses:
        "200":
          description: successfully returned all available samples and their notification status
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Sample-For-Lab'
                x-content-type: application/json
        "400":
          description: invalid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/inline_response'
      security:
      - bearerAuth: ['labuser']

具有相应的安全定义

securitySchemes:
    bearerAuth:
      type: apiKey
      name: Authorization
      in: header
      x-apikeyInfoFunc: swagger_server.controllers.authorization_controller.check_bearerAuth

到目前为止一切顺利。我使用 swagger-codegen 构建了相应的服务器存根,它遵循connexion security model 并提供两个字段api_key,即不记名令牌和“required_scopes”,即应该包含“labuser”。访问端点时,调用控制器函数:

def check_adminuserAuth(api_key, required_scopes):
    return {'sample_key' : 'sample_value}

当正确传递不记名令牌时,required_scopesNone。因此,无法实际验证提供的令牌中显示的凭据和权限是否与授权控制器中端点所需的labuser 范围相匹配。我考虑在被调用的端点list_systemusers() 中处理验证,但连接没有传递令牌。

OpenAPI 3.0 不支持

经过一番挖掘,我发现 OpenAPI 3.0 在全局 API 级别(即是否经过身份验证)提供 apiKey 验证,但不支持每个端点的单个范围。如果您想要单独的范围,则需要切换到 OAuth 安全性。然而,通过 apiKey 安全性对安全范围的支持将在 OpenAPI 3.1 中提供

【问题讨论】:

    标签: swagger openapi api-key bearer-token connexion


    【解决方案1】:

    解决方法

    因此,目前使单个作用域的不记名令牌安全工作的唯一方法是为每个作用域实际定义一个安全方案,例如

    securitySchemes:
        adminuserAuth:
          type: apiKey
          description: Provide your bearer token in the format **Bearer <token>**
          name: Authorization
          in: header
          x-apikeyInfoFunc: swagger_server.controllers.authorization_controller.check_adminuserAuth
        statsuserAuth:
          type: apiKey
          description: Provide your bearer token in the format **Bearer <token>**
          name: Authorization
          in: header
          x-apikeyInfoFunc: swagger_server.controllers.authorization_controller.check_statsuserAuth
        labuserAuth:
          type: apiKey
          description: Provide your bearer token in the format **Bearer <token>** 
          name: Authorization
          in: header
          x-apikeyInfoFunc: swagger_server.controllers.authorization_controller.check_labuserAuth
    

    然后在路径定义上添加您所需的安全身份验证方案

    security:
    - labuserAuth: []
    - adminuserAuth: []
    x-openapi-router-controller: swagger_server.controllers.lab_controller
    

    现在我知道哪个授权控制器方法被称为用户需要显示的所需范围,因此可以根据令牌中显示的方法对其进行验证。

    【讨论】:

      猜你喜欢
      • 2011-06-08
      • 1970-01-01
      • 2016-02-23
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 2018-08-29
      • 2016-08-24
      相关资源
      最近更新 更多