路径的 Swagger 特定安全性
Swagger specific security for a path
我有一个 Node.js API,我想在其中添加 swagger 文档。客户端通过 JWT 授权,所以我将此添加到安全性中:
securityDefinitions:
UserSecurity:
type: apiKey
description: User is logged in
in: header
name: Authorization
然后我可以将它添加到不同的路径以告诉客户,为此你需要登录。
/user/{userId}
get:
security:
- UserSecurity: []
但是如何添加更具体的安全限制?例如,用户只有以该用户身份登录才能编辑配置文件。或者,如果用户具有超级管理员身份,或者如果他是论坛的管理员,则用户可以编辑评论,评论发布在 OR 被记录为创建此评论的用户。
AFAIK,没有直接的方法可以将 'roles' 添加到 swagger 文档中。
我所做的是向 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);
}
我有一个 Node.js API,我想在其中添加 swagger 文档。客户端通过 JWT 授权,所以我将此添加到安全性中:
securityDefinitions:
UserSecurity:
type: apiKey
description: User is logged in
in: header
name: Authorization
然后我可以将它添加到不同的路径以告诉客户,为此你需要登录。
/user/{userId}
get:
security:
- UserSecurity: []
但是如何添加更具体的安全限制?例如,用户只有以该用户身份登录才能编辑配置文件。或者,如果用户具有超级管理员身份,或者如果他是论坛的管理员,则用户可以编辑评论,评论发布在 OR 被记录为创建此评论的用户。
AFAIK,没有直接的方法可以将 'roles' 添加到 swagger 文档中。
我所做的是向 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);
}