【问题标题】:Flutter Firestore - PERMISSION_DENIED: Missing or insufficient permissionsFlutter Firestore - PERMISSION_DENIED:权限缺失或不足
【发布时间】:2019-01-06 11:04:49
【问题描述】:

我在我的 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"
  }
}

上面的声明部分对应于我的数据库安全规则中的request.auth.claims.organisation_uuid == orgID 规则。 在 Firestore Web 控制台中为单个文档模拟的请求工作正常,但使用 Flutter 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. -

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

【问题讨论】:

  • 我想知道我是否遇到了每个请求的评估次数的限制``每个请求的exists()、get()和getAfter()调用的最大数量10个单文档请求和查询请求。 20 用于多文档读取、事务和批量写入。之前的 10 个限制也适用于每个操作。 ```

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


【解决方案1】:

所以我认为这个问题是我的自定义令牌的 .claims 部分在请求对象上不可用,我将我的规则更新为以下内容,一切都按预期工作

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);
    }        
  }
}

在我看来,好像自定义 JWT 令牌声明的评估方式存在错误,或者我误读了文档,请澄清一下。

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 2019-05-05
    • 2018-09-12
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多