适用于firebase中受保护内容的授权规则

时间:2013-07-22 22:03:51

标签: security authentication authorization firebase firepad

是否有针对firebase应用中受保护内容的正确授权规则的最佳做法方法

  • 专门使用firepad
  • 受保护的内容我的意思是用户创建文档的位置,只与某些其他用户共享。
  • 此外,我需要能够查询firebase以查找我有权访问的所有文档(我创建的文档和其他用户与我共享的文档)

到目前为止我的一些研究:

方法1:秘密网址

  • 我需要知道能够查看/编辑文档的网址

  • 不是真正的授权,因为任何有权访问该URL的登录用户都可以编辑/修改它。

  • 无法索引我有权访问的所有文档

方法2:使用firebase授权规则将用户添加到文档中,并在读/写之前检查用户是否为document.users。

取自: Protected content in Firebase possible?

{

"documents": {

   "$documents_id": {

       // any friend can read my post

       ".read":  "auth.id === data.child('owner').val() || root.child('users/'+data.child.owner.val()+'/users/'+auth.id).exists()",

       // any friend can edit my post
       ".write": "auth.id === data.child('owner').val() || root.child('users/'+data.child.owner.val()+'/users/'+auth.id).exists()"

   },

   users:{

   // List of user.ids that have access to this document

   }

}

}

优点:

  • 正确的授权/身份验证。只有已获得访问权限的经过身份验证的用户才能查看/编辑。

缺点:

  • 无法查询允许用户编辑的所有文档(我拥有或已与我共享的文档)(这个假设是否正确?)

方法3 :Firebase授权规则(方法2),以及每个用户都有权访问的document_ids数组的冗余用户存储。此用户存储仅用于查询用户有权访问的所有文档。 即:

{
"documents": {
   "$documents_id": {
       // any friend can read my post
       ".read":  "auth.id === data.child('owner').val() || root.child('users/'+data.child.owner.val()+'/users/'+auth.id).exists()",
       // any friend can edit my post
       ".write": "auth.id === data.child('owner').val() || root.child('users/'+data.child.owner.val()+'/users/'+auth.id).exists()"
   }
},
"users":{
    "$user":{
        ".read": "auth.id=$user.id",
        ".write": "auth.id=$user.id"
        "$documents":{
            // All the documents i have access to. This list gets ammended whenever I am granted/stripped access to a document.
        }
    }
}
}

优点:

  • 正确的身份验证/授权

缺点:

  • 重复数据,必须处理两个数据存储之间的同步问题。这似乎不是一个好主意。

方法4:群组

Granting access to Firebase locations to a group of users

使用群组
  • 我们为数据存储中的每个文档都有一个组

  • 无法轻松查询firebase以获取用户可以访问的所有文档

有更好的方法吗?

1 个答案:

答案 0 :(得分:13)

你已经很好地枚举了选项,而且你肯定是在正确的轨道上。正如您所发现的那样,无法根据安全规则进行查询。这是故意完成的,因为(取决于您的安全规则)这可能非常昂贵(Firebase通常会因此而避免复杂查询)。

所以你的方法3是完全正确的方法。为这些情况复制数据实际上是一种非常常见的做法。有关此内容更详细的博客文章,请参阅Denormalizing Your Data is Normal

您还可以使用重复的文档列表执行方法1。如果您希望能够仅使用URL(包含密钥ID)将某人“邀请”到文档,这将非常有用。或者你可以将两者结合起来(有些文件是“公开但不公开”,有些文件是“私人邀请朋友”或其他什么。)

相关问题