Firebase 安全规则以支持多层次复杂性的格式提供访问控制和数据验证。要构建基于用户和基于角色的访问系统以确保用户数据安全,请将Firebase 身份验证与 Firebase 安全规则结合使用。
识别用户
身份验证识别请求访问您的数据的用户,并将该信息作为您可以在规则中利用的变量提供。 auth
变量包含以下信息:
-
uid
:分配给请求用户的唯一用户 ID。 -
token
:身份验证收集的值映射。
auth.token
变量包含以下值:
场地 | 描述 |
---|---|
email | 与帐户关联的电子邮件地址(如果存在)。 |
email_verified | 如果用户已验证他们有权访问email 地址,则为true 。一些提供商会自动验证他们拥有的电子邮件地址。 |
phone_number | 与帐户关联的电话号码(如果存在)。 |
name | 用户的显示名称(如果已设置)。 |
sub | 用户的 Firebase UID。这在项目中是唯一的。 |
firebase.identities | 与此用户帐户关联的所有身份的字典。字典的键可以是以下任何一个: email 、 phone 、 google.com 、 facebook.com 、 github.com 、 twitter.com 。字典的值是与帐户关联的每个身份提供者的唯一标识符数组。例如, auth.token.firebase.identities["google.com"][0] 包含与帐户关联的第一个 Google 用户 ID。 |
firebase.sign_in_provider | 用于获取此令牌的登录提供程序。可以是以下字符串之一: custom 、 password 、 phone 、 anonymous 、 google.com 、 facebook.com 、 github.com 、 twitter.com 。 |
firebase.tenant | 与帐户关联的 tenantId(如果存在)。例如tenant2-m6tyz |
如果要添加自定义身份验证属性, auth.token
变量还包含您指定的任何自定义声明。
当请求访问的用户未登录时, auth
变量为null
。例如,如果您想限制经过身份验证的用户的读取访问权限,则可以在您的规则中利用它 — auth != null
。但是,我们通常建议进一步限制写访问。
有关auth
变量的更多信息,请参阅Cloud Firestore 、实时数据库和Cloud Storage的参考文档。
在规则中利用用户信息
实际上,在您的规则中使用经过身份验证的信息会使您的规则更加强大和灵活。您可以根据用户身份控制对数据的访问。
在您的规则中,定义auth
变量中的信息(请求者的用户信息)如何匹配与所请求数据关联的用户信息。
例如,您的应用可能希望确保用户只能读取和写入他们自己的数据。在这种情况下,您可能希望auth.uid
变量与所请求数据的用户 ID 匹配:
云端 Firestore
service cloud.firestore {
match /databases/{database}/documents {
// Make sure the uid of the requesting user matches name of the user
// document. The wildcard expression {userId} makes the userId variable
// available in rules.
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}
实时数据库
{
"rules": {
"users": {
"$userId": {
// grants write access to the owner of this user account
// whose uid must exactly match the key ($userId)
".write": "$userId === auth.uid"
}
}
}
}
云储存
service firebase.storage {
// Only a user can upload their file, but anyone can view it
match /users/{userId}/{fileName} {
allow read;
allow write: if request.auth != null && request.auth.uid == userId;
}
}
定义自定义用户信息
您可以进一步利用auth
变量来定义分配给应用程序用户的自定义字段。
例如,假设您要创建一个“管理员”角色,以启用对特定路径的写访问权限。您可以将该属性分配给用户,然后在授予路径访问权限的规则中利用它。
在 Cloud Firestore 中,您可以向用户的文档添加自定义字段,并通过规则中的嵌入式读取检索该字段的值。因此,您的基于管理员的规则将类似于以下示例:
云端 Firestore
service cloud.firestore {
match /databases/{database}/documents/some_collection: {
// Remember that, in Cloud Firestore, reads embedded in your rules are billed operations
write: if request.auth != null && get(/databases/(database)/documents/users/$(request.auth.uid)).data.admin) == true;
read: if request.auth != null;
}
}
在 Authentication 中创建自定义声明后,您可以在 Rules 中访问自定义声明。然后,您可以使用auth.token
变量引用这些自定义声明。
云端 Firestore
service cloud.firestore {
match /databases/{database}/documents {
// For attribute-based access control, check for an admin claim
allow write: if request.auth.token.admin == true;
allow read: true;
// Alterntatively, for role-based access, assign specific roles to users
match /some_collection/{document} {
allow read: if request.auth.token.reader == "true";
allow write: if request.auth.token.writer == "true";
}
}
}
实时数据库
{
"rules": {
"some_path/$sub_path": {
// Create a custom claim for the admin role
".write": "auth.uid !== null && auth.token.writer === true"
".read": "auth.uid !== null"
}
}
}
云储存
service firebase.storage {
// Create a custom claim for the admin role
match /files/{fileName} {
allow read: if request.auth.uid != null;
allow write: if request.auth.token.admin == true;
}
}
要查看更多利用身份验证的基本规则示例,请参阅基本安全规则。