Flutter Firestore-PERMISSION_DENIED:缺少权限或权限不足

时间:2018-07-30 08:28:39

标签: firebase firebase-authentication google-cloud-firestore flutter

我在Firestore数据库中设置了以下简单规则。

service cloud.firestore {
  match /databases/{database}/documents {
    function isSignedIn() {
      return request.auth != null;
    }

    match /organisations/{orgID}/{document=**} {
      allow list, read, write: if isSignedIn() && request.auth.claims.organisation_uuid == orgID;                  
    }        
  }
}

目的是使任何经过身份验证的用户都可以访问其组织中的所有文档。我希望用户能够访问的示例集合是下面显示的任务子集合

"organisations": [
  "$orgID": {
    "tasks": [
       "taskID1": {
          ...
       },
       "taskID2": {
          ...
       },
    ]
  }

用户正在使用自定义JWT令牌进行身份验证,该令牌使用的服务帐户如下所示

{
  "iss":"accountemail@db-name.iam.gserviceaccount.com",
  "sub":"accountemail@db-name.iam.gserviceaccount.com",
  "aud":"$correct-audience-string",
  "iat":1532937868,
  "uid":"e13e07a8-f7f2-4c7a-a02f-be729601e62e",
  "exp":1532941468,
  "claims":{
    "organisation_uuid":"9cb3ebae-d16e-4898-a784-f0aac513fcb8"
  }
}

上面的Claims部分对应于我的数据库安全规则中的request.auth.claims.organisation_uuid == orgID规则。 在firestore Web控制台中模拟的单个文档请求工作正常,但使用如下所示的cloud_store颤振库构建的查询

Firestore.instance
  .collection('organisations')
  .document(technician.organisation_uuid)
  .collection('tasks')
  .where("assignee_uuid", isEqualTo: technician.uuid);

导致异常com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.-

我无法确定规则存在的问题,因为经过身份验证的用户应该可以访问该组织下的所有文档。非常感谢任何见解。

1 个答案:

答案 0 :(得分:0)

所以我认为问题在于自定义令牌的service cloud.firestore { match /databases/{database}/documents { function isSignedIn() { return request.auth != null; } function isInTheCorrectOrganisation(orgID){ return get(/databases/$(database)/documents/organisations/$(orgID)/technicians/$(request.auth.uid)).data.organisation_uuid == orgID; } match /organisations/{orgID}/{document=**} { allow read, write: if isSignedIn() && isInTheCorrectOrganisation(orgID); } } } 部分在请求对象上不可用,我将规则更新为以下内容,并且一切都按预期工作

 <<echo=FALSE,eval=T,results='asis' >>=
   table %>%
 #kable_styling(latex_options = c("scale_down"), 
 kable_styling(font_size = 7) %>%
 kable_styling(position = "center") %>%
 kable_styling(full_width = F) %>%
 column_spec(1, width = "5em")%>%
 row_spec(0,bold = T, color = "white", background = "black" )%>% 
 row_spec( dim(jeden)[1]: ( dim(jeden)[1]-4), bold = T, hline_after=T, 
 background = "lightgray" )
 @

在我看来,自定义JWT令牌声明的评估方式似乎有错误,或者我对文档的理解有误,因此澄清起来非常好。

相关问题