【问题标题】:Custom claims set in cloud functions do not show up in cloud rules of firebase storage云功能中设置的自定义声明不会显示在 Firebase 存储的云规则中
【发布时间】:2022-01-23 18:41:54
【问题描述】:

我正在尝试使用以下设置自定义声明:

admin.auth().setCustomUserClaims(user.uid, {['hguwukrpyrwxerqr679p']: true});

其中hguwukrpyrwxerqr679p 是firestore 数据库中对象的唯一ID。该对象在创建时具有自己的存储桶。此存储桶具有以下安全规则:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    allow read: if request.auth.token[bucket];
    allow write: if false;
  }
}

很遗憾,这不起作用,我收到 403 错误代码。我不知道为什么,我尝试使用上面的用户在firebase storage 的规则游乐场中模拟请求。令牌部分根本不显示任何自定义声明,但它应该,不应该吗?

我已经用了好几个小时了,尝试了各种方法,甚至在云功能中手动设置声明。有人可以指出我正确的方向或提供有关如何正确调试此问题的提示吗?通过执行调试云函数时

console.log(Object.keys(await admin.auth().getUser(uid).customClaims));

它显示了正确的内容。

【问题讨论】:

  • 这能回答你的问题吗? Firebase Custom Claims doesn't propagate
  • @Dharmaraj flutter 正确显示了自定义声明,但仍被拒绝
  • 您设置声明本身的方式看起来很不寻常。你试过了吗:{'hguwukrpyrwxerqr679p': true}
  • @FrankvanPuffelen 我试过使用{[objectId]: true}{['hguwukrpyrwxerqr679p']: true}{'hguwukrpyrwxerqr679p': true},但这些都不起作用:(
  • @FrankvanPuffelen 在我的最新发现之后我感到很困惑。显然,对存储桶的根应用规则只能使用match /{somePath=**} 通配符。我没有意识到这一点。从这个角度来看,规则与Firestore的规则有些不同。

标签: firebase google-cloud-functions firebase-security


【解决方案1】:

存储在存储桶根目录中的规则以某种方式被忽略。

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    allow read: if request.auth.token[bucket];
    allow write: if false;
  }
}

应该使用通配符

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{somePath=**} {
      allow read: if request.auth.token[bucket];
      allow write: if false;
    }
  }
}

我不确定这是否应该被接受为答案,因为它仅与问题部分相关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-25
    • 2020-08-19
    • 2019-07-20
    • 2021-01-12
    • 2021-10-16
    • 1970-01-01
    • 2019-12-18
    • 2021-09-20
    相关资源
    最近更新 更多