Node.js swagger api express中的用户和角色授权
伙计们 在我现有的api上,我已经使用承载安全性进行了用户身份验证。使用http头api_密钥和更高版本的令牌 我的问题似乎是我有不同的终点,只需要根据角色来消费 例如,要发布新用户,请执行以下操作:Node.js swagger api express中的用户和角色授权,node.js,express,swagger-ui,swagger-2.0,Node.js,Express,Swagger Ui,Swagger 2.0,伙计们 在我现有的api上,我已经使用承载安全性进行了用户身份验证。使用http头api_密钥和更高版本的令牌 我的问题似乎是我有不同的终点,只需要根据角色来消费 例如,要发布新用户,请执行以下操作: POST user should only be authenticated to user with admin role. 我已经看过了,但是我在他们的文档和谷歌上也找不到任何东西 请给我一些脑力激荡的主意好吗?下面是我在nodejs和express中的访问验证代码。 swaggerTool
POST user should only be authenticated to user with admin role.
我已经看过了,但是我在他们的文档和谷歌上也找不到任何东西
请给我一些脑力激荡的主意好吗?下面是我在nodejs和express中的访问验证代码。
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
app.use(middleware.swaggerSecurity({
Bearer: function(req,def,apiKey,next){
apiKey= apiKey.slice(7)
debug("token check",def,apiKey)
var ok=checkToken(apiKey)
if(ok) {
req.user=ok
debug('Token is ok')
return next()
}
debug("Invalid token",apiKey)
var err=Error("Invalid token")
err.statusCode=403
next(err)
}
}));
在撰写本文时,解决方案仍在酝酿之中。通过oAuth作用域保存或使用“hacky”api密钥安全定义(),Swagger没有内置的RBAC机制 幸运的是,我们可以创建一些非常基本的中间件来处理这个问题,因为swagger确实允许我们将x-swagger-*成员添加到swagger定义中 以下是我所做的:
var findOne = function (haystack, arr) {
return arr.some(function (v) {
return haystack.indexOf(v) >= 0;
});
};
app.use(function(req, res, next) {
var operation = runner.getOperation(req);
if(operation && operation.definition) {
var definition = operation.definition;
var requiredRoles = definition['x-swagger-roles'];
// if the endpoint has no required roles then go to the next route
if(!requiredRoles) return next();
// get the users roles
var userRoles = req.session.roles; // this may differ for you
// if any roles match then go to the next route
if(findOne(userRoles, requiredRoles)) return next();
// if no roles match then assert that this endpoint is forbidden
else return res.sendStatus(403);
}
next();
})
// it's important to register the middleware after the role check
runner.expressMiddleware().register(app);
你能找到关于这个的文档,或者解决这个问题吗?我自己也被困在这里,在我的API公开的每个服务中向每个方法添加安全检查似乎非常麻烦。我很惊讶在斯威格没有人解决这个问题。我已经做到了!!!
var findOne = function (haystack, arr) {
return arr.some(function (v) {
return haystack.indexOf(v) >= 0;
});
};
app.use(function(req, res, next) {
var operation = runner.getOperation(req);
if(operation && operation.definition) {
var definition = operation.definition;
var requiredRoles = definition['x-swagger-roles'];
// if the endpoint has no required roles then go to the next route
if(!requiredRoles) return next();
// get the users roles
var userRoles = req.session.roles; // this may differ for you
// if any roles match then go to the next route
if(findOne(userRoles, requiredRoles)) return next();
// if no roles match then assert that this endpoint is forbidden
else return res.sendStatus(403);
}
next();
})
// it's important to register the middleware after the role check
runner.expressMiddleware().register(app);