Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js swagger api express中的用户和角色授权_Node.js_Express_Swagger Ui_Swagger 2.0 - Fatal编程技术网

Node.js swagger api express中的用户和角色授权

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

伙计们

在我现有的api上,我已经使用承载安全性进行了用户身份验证。使用http头api_密钥和更高版本的令牌

我的问题似乎是我有不同的终点,只需要根据角色来消费

例如,要发布新用户,请执行以下操作:

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定义中

以下是我所做的:

  • 向需要RBAC(基于角色的访问控制)的每个端点添加x-swagger-roles

  • 在向应用程序注册swagger node runner之前放置中间件。在本例中,我们使用的是expressjs,因此使用了connect中间件

    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);
    
  • 注:

  • 此代码尚未在生产中进行测试,应由安全专业人员进行审查

  • x-swagger-roles不会出现在您的swagger ui中,除非对其进行更改,这超出了本答案的范围


  • 你能找到关于这个的文档,或者解决这个问题吗?我自己也被困在这里,在我的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);