Firebase Security Rules ให้การควบคุมการเข้าถึงและการตรวจสอบข้อมูลในรูปแบบที่รองรับ ความซับซ้อนหลายระดับ หากต้องการสร้างระบบการเข้าถึงตามผู้ใช้และตามบทบาท ที่ช่วยรักษาข้อมูลของผู้ใช้ให้ปลอดภัย ให้ใช้ Firebase Authentication กับ Firebase Security Rules
ระบุผู้ใช้
Authentication ระบุผู้ใช้ที่ขอสิทธิ์เข้าถึงข้อมูลของคุณ และให้ข้อมูลดังกล่าวเป็นตัวแปรที่คุณใช้ประโยชน์ในกฎได้ auth
ตัวแปร
มีข้อมูลต่อไปนี้
uid
: รหัสผู้ใช้ที่ไม่ซ้ำกันซึ่งกำหนดให้กับผู้ใช้ที่ส่งคำขอtoken
: แผนที่ค่าที่รวบรวมโดย Authentication
ตัวแปร auth.token
มีค่าต่อไปนี้
ช่อง | คำอธิบาย |
---|---|
email |
อีเมลที่เชื่อมโยงกับบัญชี (หากมี) |
email_verified |
true หากผู้ใช้ยืนยันว่าตนมีสิทธิ์เข้าถึงอีเมล email ผู้ให้บริการบางรายจะยืนยันอีเมลของตนโดยอัตโนมัติ |
phone_number |
หมายเลขโทรศัพท์ที่เชื่อมโยงกับบัญชี (หากมี) |
name |
ชื่อที่แสดงของผู้ใช้ หากตั้งค่าไว้ |
sub |
UID ของ Firebase ของผู้ใช้ ซึ่งจะไม่ซ้ำกันภายในโปรเจ็กต์ |
firebase.identities |
พจนานุกรมของข้อมูลระบุตัวตนทั้งหมดที่เชื่อมโยงกับบัญชีของผู้ใช้รายนี้ คีย์ของพจนานุกรมอาจเป็นค่าใดค่าหนึ่งต่อไปนี้ email , phone , google.com , facebook.com , github.com , twitter.com ค่าของพจนานุกรมคืออาร์เรย์ของตัวระบุที่ไม่ซ้ำกันสำหรับผู้ให้บริการข้อมูลประจำตัวแต่ละรายที่เชื่อมโยงกับบัญชี เช่น auth.token.firebase.identities["google.com"][0] มีรหัสผู้ใช้ Google รายแรกที่เชื่อมโยงกับบัญชี |
firebase.sign_in_provider |
ผู้ให้บริการลงชื่อเข้าใช้ที่ใช้รับโทเค็นนี้ อาจเป็นสตริงใดก็ได้ต่อไปนี้ custom , password , phone , anonymous , google.com , facebook.com , github.com , twitter.com |
firebase.tenant |
รหัสผู้เช่าที่เชื่อมโยงกับบัญชี หากมี เช่น tenant2-m6tyz |
หากต้องการเพิ่มแอตทริบิวต์การตรวจสอบสิทธิ์ที่กำหนดเอง auth.token
ตัวแปรจะมีการอ้างสิทธิ์ที่กำหนดเอง
ที่คุณระบุด้วย
เมื่อผู้ใช้ที่ขอสิทธิ์เข้าถึงไม่ได้ลงชื่อเข้าใช้ ตัวแปร auth
จะเป็น null
คุณใช้ประโยชน์จากสิ่งนี้ในกฎได้หากต้องการจำกัดสิทธิ์การอ่าน
สำหรับผู้ใช้ที่ตรวจสอบสิทธิ์แล้ว เช่น auth != null
อย่างไรก็ตาม โดยทั่วไปเราขอแนะนำ
ให้จำกัดสิทธิ์การเขียนเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับตัวแปร auth
ได้ที่เอกสารอ้างอิง
สำหรับ
Cloud Firestore
Realtime Database และ
Cloud Storage
ใช้ประโยชน์จากข้อมูลผู้ใช้ในกฎ
ในทางปฏิบัติ การใช้ข้อมูลที่ได้รับการตรวจสอบสิทธิ์ในกฎจะทำให้กฎมีประสิทธิภาพและยืดหยุ่นมากขึ้น คุณควบคุมการเข้าถึงข้อมูลตามข้อมูลประจำตัวของผู้ใช้ได้
ในกฎ ให้กําหนดวิธีที่ข้อมูลในตัวแปร auth
ซึ่งเป็นข้อมูลผู้ใช้ของผู้ขอ ตรงกับข้อมูลผู้ใช้ที่เชื่อมโยงกับข้อมูลที่ขอ
เช่น แอปอาจต้องการตรวจสอบว่าผู้ใช้สามารถอ่านและเขียนข้อมูลของตนเองเท่านั้น ในกรณีนี้ คุณอาจต้องการให้ตัวแปร auth.uid
ตรงกับรหัสผู้ใช้ในข้อมูลที่ขอ
Cloud 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;
}
}
}
Realtime Database
{
"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"
}
}
}
}
Cloud Storage
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 คุณสามารถเพิ่มช่องที่กำหนดเองลงในเอกสารของผู้ใช้และเรียกค่าของช่องนั้นได้ด้วยการอ่านแบบฝังในกฎ ดังนั้น กฎที่อิงตามผู้ดูแลระบบ จะมีลักษณะดังตัวอย่างต่อไปนี้
Cloud 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;
}
}
คุณเข้าถึงการอ้างสิทธิ์ที่กำหนดเองได้ใน Rules หลังจากสร้างการอ้างสิทธิ์ที่กำหนดเองใน Authentication จากนั้นคุณจะอ้างอิงเคลมที่กำหนดเองเหล่านั้นได้โดยใช้ตัวแปร auth.token
Cloud 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";
}
}
}
Realtime Database
{
"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"
}
}
}
Cloud Storage
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;
}
}
ดูตัวอย่างเพิ่มเติมของRulesการใช้ประโยชน์Authenticationพื้นฐานได้ที่ กฎความปลอดภัยพื้นฐาน