在需要通过 REST 公开部分 Meteor 集合时,我偶然发现了Collection API Meteorite 包。
虽然这确实可以通过 REST 公开 Meteor 集合,但它没有考虑任何安全性。有什么方法可以将 Collection API 与中引入的 Meteor 身份验证系统集成在一起0.5.2?
在需要通过 REST 公开部分 Meteor 集合时,我偶然发现了Collection API Meteorite 包。
虽然这确实可以通过 REST 公开 Meteor 集合,但它没有考虑任何安全性。有什么方法可以将 Collection API 与中引入的 Meteor 身份验证系统集成在一起0.5.2?
是的,有点。使用 REST 陨石包,您可以声明您的集合,然后在集合上使用允许规则,但有几个警告(注意:这是需要更多工作的伪代码!):
Players = new Meteor.Collection("players");
//a collection of associated userids and auth token headers
APIUsers = new Meteor.Collection("apiusers");
Players.allow({
insert: function (userId, doc) {
//invoking this from a RESTful context means the userId is NOT
//available, so you'll need to do three things things:
// (1) a way to get the current http request's X-Auth-Token header
// (2) a collection to look up the user(s) associated with
// that token
// (3) and an owner field on the Players collection to join back
// to the found userids.
return (_.indexOf(APIUsers.findOne(
{XAuthToken: __CURRENT_X_AUTH_TOKEN__}).users
, doc.owner) > -1;
},
update: function (userId, docs, fields, modifier) {
/* similar logic */
},
remove: function (userId, docs) {
/* similar logic */
},
fetch: ['owner']
});
但是,虽然我认为 RESTful 方法将证明在将遗留应用程序集成到 Meteor 上下文中很有用,但我强烈建议研究DDP 协议以集成新项目。
正如您在上面看到的,允许规则不公开 GET 回调,大概是因为期望 GET 是在服务器公开的发布中定义的。DDP 客户端在较低级别连接以订阅这些发布,因此在此上下文中的 GET 将比 RESTful 方法更加精细。
对于现在遇到此问题的任何人,Restivus允许您在集合上生成 REST 端点,并在这些端点上配置用户身份验证和角色权限。默认身份验证使用 Meteor 的内置登录令牌机制来对 a 进行身份验证,并Meteor.user提供对经过身份验证的端点的访问。如果默认值对您来说太多或不够,您还可以提供自定义身份验证方法。这是一个简单的例子(希望你能阅读 CoffeeScript):this.userthis.userId
# Generates: GET, POST on /api/users and GET, DELETE on /api/users/:id for
# Meteor.users collection
Restivus.addCollection Meteor.users
excludedEndpoints: ['deleteAll', 'put']
routeOptions:
authRequired: true
endpoints:
post:
authRequired: false
delete:
roleRequired: 'admin'