【问题标题】:Authorize FeathersJs service based on Auth0 scope基于 Auth0 范围授权 FeathersJs 服务
【发布时间】:2019-03-04 17:01:18
【问题描述】:

我的 feathers.js API 中有一个用户服务。我想让 find 方法仅适用于已阅读的经过身份验证的用户:用户范围集。

我使用express-jwt 作为身份验证的中间件。 我试过使用express-jwt-authz 进行授权,但我不能,因为req 在钩子中不可用。 express-jwt 库在 req.user 中设置范围

用法 JWT 身份验证中间件使用 智威汤逊。如果令牌有效,req.user 将被设置为 JSON 对象 解码后的中间件用于授权和访问 控制。

有没有办法可以访问钩子内 access_token 中的范围?

我希望能够做这样的事情:

module.exports = {
  before: {
    all: [],
    find: [ authorize(['read:users']) ],
    get: [ authorize(['read:users'])],
    create: [ authorize(['write:users']) ],
    update: [ authorize(['write:users'])   ],
    patch: [ authorize(['write:users'])  ],
    remove: [ authorize(['write:users'])  ]
  }
};

【问题讨论】:

    标签: express auth0 feathersjs


    【解决方案1】:

    通过添加一个将 req.user 数据添加到 req.feathers.scope 的中间件来解决它。

    我的src/middleware/index.js 文件

    const addUserToReq = function(req, res, next) {
      req.feathers.user = req.user; next();
    };
    
    module.exports = function (app) {
      app.use(checkJwt); // express-jwt logic
      app.use(addUserToReq);
    };
    

    然后在钩子里面我可以像这样访问这个信息

    module.exports = function(expectedScopes) {
      return context => {
          let scopes = context.params.user.scope;
        }
      };
    };
    

    【讨论】:

      猜你喜欢
      • 2020-10-12
      • 2020-06-23
      • 2017-02-06
      • 1970-01-01
      • 2016-12-17
      • 2021-11-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      相关资源
      最近更新 更多