具有自定义令牌的Firebase存储通配符规则

时间:2019-03-15 15:57:49

标签: firebase firebase-storage firebase-security-rules

由于某种原因,我无法使通配符在具有自定义标记的路径中工作。 用户已分配了自定义令牌,例如admin: trueySWLb8NSTj9sur6n2CbS: true

service firebase.storage {
  match /b/{bucket}/o {
        match /conferences/{confId}/sponsors/{sponsId=**} {
        allow read: if request.auth.uid != null
        allow write: if request.auth.token.confId == true
    }
  }
}

我正尝试从客户端写到/conferences/ySWLb8NSTj9sur6n2CbS/sponsors/whatever.jpeg,但是访问被拒绝。

如果我现在更改为以下内容,它将正常工作。

service firebase.storage {
  match /b/{bucket}/o {
        match /conferences/{confId}/sponsors/{sponsId=**} {
        allow read: if request.auth.uid != null
        allow write: if request.auth.token.admin == true
    }
  }
}

我甚至通过将自定义令牌更改为ySWLb8NSTj9sur6n2CbS: "ySWLb8NSTj9sur6n2CbS",然后尝试以下操作均未成功并拒绝访问来对它进行测试!

service firebase.storage {
  match /b/{bucket}/o {
        match /conferences/{confId}/sponsors/{sponsId=**} {
        allow read: if request.auth.uid != null
        allow write: if request.auth.token.confId == confId
    }
  }
}

我感到通配符由于某种原因未被使用,或者我在这里忽略了什么? 在文档中,我发现:https://firebase.google.com/docs/storage/security/user-security?authuser=0

1 个答案:

答案 0 :(得分:0)

所以我发现围绕它的另一种方法是出于某种奇怪的原因。 我已经以数组形式向用户分配了自定义声明。在该数组中,您有confId。所以我检查相关的confId是否在该数组中。

service firebase.storage {
  match /b/{bucket}/o {
        match /conferences {
        match /{confId}/sponsors/{sponsorId} {
        allow read: if confId in request.auth.token.userEvents
        allow write: if confId in request.auth.token.adminEvents
      }
    }
  }
}