【问题标题】:Swagger specific security for a pathSwagger 路径的特定安全性
【发布时间】:2016-11-06 08:28:31
【问题描述】:

我有一个 Node.js API,我想在其中添加 swagger 文档。客户端通过 JWT 授权,所以我将其添加到安全性:

securityDefinitions:
  UserSecurity:
    type: apiKey
    description: User is logged in
    in: header
    name: Authorization

我可以将其添加到不同的路径以告诉客户端,要执行此操作,您需要登录。

/user/{userId}
  get:
    security:
      - UserSecurity: []

但是如何添加更具体的安全约束?就像,用户只能以该用户身份登录才能编辑配置文件。或者,如果用户具有超级管理员状态,或者如果他是董事会的管理员,则用户可以编辑评论,评论发布在或记录为创建此评论的用户。

【问题讨论】:

标签: node.js api documentation swagger swagger-2.0


【解决方案1】:

AFAIK,没有直接的方法可以将“角色”添加到招摇文档中。

我所做的是,我正在向 swagger 文件 x-scope 添加一个自定义部分:

get:
    operationId: getToken
    x-scope:
      - merchant
    security:
      - token: []

然后在代码中,我根据路径中提供的角色检查用户的角色:

authorize: (req, def, token, callback) => {
  let scopes = req.swagger.operation["x-scope"];
  //scopes will contain ["merchant"] array

  return verifyUserAndRoles(token, scopes);
}

【讨论】:

  • 像这样工作得很好,谢谢。如果有非常具体的安全要求,我会尝试以更好的方式定义我的范围。如果不可能,我将这些要求作为描述添加到 401 响应中。
  • 是的,在 swagger 定义文件中很难做到真正具体的要求
猜你喜欢
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 2020-02-29
  • 2019-03-13
  • 2018-08-15
  • 1970-01-01
  • 2018-06-11
  • 2021-07-22
相关资源
最近更新 更多