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

aba*_*ker 26 security authentication authorization firebase firepad

是否有针对firebase应用程序中受保护内容的适当授权规则的最佳实践方法

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

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

方法1:秘密URL

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

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

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

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

取自: Firebase中的受保护内容可能吗?

{

"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

   }

}

}
Run Code Online (Sandbox Code Playgroud)

优点:

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

缺点:

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

方法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.
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

优点:

  • 适当的身份验证/授权

缺点:

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

方法4:组

每次授予对一组用户授予对Firebase位置的访问权限的组

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

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

有一个更好的方法吗?

Mic*_*uer 13

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

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

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


归档时间:

查看次数:

9898 次

最近记录:

12 年,2 月 前