如何使用 Firestore 安全规则解密加密的 UID?

Muh*_*mad 4 javascript firebase firebase-security google-cloud-firestore

  1. 我得到的uid,当用户登录到火力使用第一次phone number。
  2. 然后我加密了uid.
  3. 然后,我创建了一个集合作为users与文件ID uid在像公司的FireStore: users/uid。

  4. 现在用户希望写进去users/userId,如果 condition是true象下面这样:

  match /users/{userId} {
     allow write: if request.auth.uid == userId;
  }
Run Code Online (Sandbox Code Playgroud)

这里正如我所提到的number 2,userId是加密的,但未request.auth.uid加密。

那么我们如何在这里解密呢(userId)?

如果我使用散列 sha256,那么如何在客户端解码 sha256?

我在用 crypto-js

Dou*_*son 5

根据文档,您可以在 security rules 中使用散列值。如果您没有使用所描述的散列算法之一,那么它将不起作用。您可以在发行说明中了解它的工作原理:

新的散列和散列相邻方法是:

hashing.crc32()
hashing.crc32c()
hashing.sha256()
hashing.md5()
<ByteValue>.toBase64()
<ByteValue>.toHexString()
<String>.toUtf8()
Run Code Online (Sandbox Code Playgroud)

例如,以前,如果 Firestore 中的电子邮件版本使用 SHA-256 进行哈希处理,您将无法将该电子邮件与使用 auth 对象发送的纯文本电子邮件进行比较。现在你可以:

hashing.sha256(request.auth.email.utf8()) == resource.data.ownerEmailHash
Run Code Online (Sandbox Code Playgroud)

或者,如果您在文档中有一个字段供用户存储他们的中篇小说,您可能希望为这个很长的字符串使用一个较短的标识符:

match /novellas/{hash} {
 allow write: if hash == hashing.sha256(request.resource.data.
           novella.utf8()) && resource == null
}
Run Code Online (Sandbox Code Playgroud)

字符串被视为 UTF-8 编码的字节,返回值为 Bytes 类型:

hashing.md5("Tag".utf8()) => b"wQEFjn6iG7vypayJMIjpCw=="
Run Code Online (Sandbox Code Playgroud)