我确信我在Firebase存储规则方面遗漏了一些东西,但我做了以下几点:
第1步
首先,我设置了以下Firebase存储规则:
service firebase.storage {
match /b/{bucket}/o {
match /items/{dev_key}/{perm_id}/{file_name} {
allow write: if request.auth.uid == dev_id;
allow read: if request.auth.token.permId == perm_id;
}
}
}
我希望只有具有与相关位置匹配的自定义声明许可证的登录用户才能下载文件,
allow read: if request.auth.token.permId == perm_id;
.
然后,我设置了一个
custom claim in Cloud Functions
对用户执行以下操作:
第2步
admin.auth().setCustomUserClaims(uid, {permId: '1'}).then(() => {
// send off some triggers to let user know the download is coming
admin.database().ref(`collection/${uid}/${itemId}`).update({
downloadReady: true
});
});
然后我注销了用户并再次登录。。。设置自定义声明。
我检查了它们在云函数中的设置,如下所示:
步骤3
admin.auth().verifyIdToken(idToken).then((claims) => {
console.log("--------------claims -------------");
console.log(JSON.stringify(claims));
});
我在索赔字符串中看到。。。
permID: "1"
在客户端,我请求了一个下载URL(希望这里是我出错的地方)。。。我希望这不是公共下载url,而是Firebase存储安全规则将检查的下载url:
第4步
var pathReference = storage.ref('items/<some-key>/1/Item-1');
pathReference.getDownloadURL()
.then((url)=>{
console.log("url: ", url);
})
我从这个电话中收到的url给了我这个链接
https://firebasestorage.googleapis.com/v0/b/emiru84-games.appspot.com/o/games%2FcfaoVuEdJqOWDi9oeaLLphXl0E82%2F1%2FGame-1?alt=media&token=45653143-924a-4a7e-b51d-00774d8986a0
(我用来测试的一个小图像)
到目前为止,很好,拥有正确声明的用户能够查看此图像
然后我重复了第2步,再次注销/登录,但这次的权限为“0”。我希望之前生成的url不再工作,因为我的用户不再有正确的自定义声明。。。bucket位置仍然在同一位置(bucket/dev_key/1/filename),但仍然有效。
如果我重复步骤4,我会得到一个新的url,然后给出相应的403错误响应。然而,旧的url仍然有效(我想只要附加了令牌参数)。这是意料之中的吗?如果是这样,我不知道如果下载url是公共的,存储安全规则会有什么不同?
如果有人能帮我清理一下迷糊的大脑,我将不胜感激。