Catch up on highlights from Firebase at Google I/O 2023. Learn more

安全規則和 Firebase 身份驗證

Firebase 安全規則以支持多層次復雜性的格式提供訪問控制和數據驗證。要構建基於用戶和基於角色的訪問系統以確保用戶數據安全,請將Firebase 身份驗證與 Firebase 安全規則結合使用。

識別用戶

身份驗證識別請求訪問您的數據的用戶,並將該信息作為您可以在規則中利用的變量提供。 auth變量包含以下信息:

  • uid分配給請求用戶的唯一用戶 ID。
  • token身份驗證收集的值映射。

auth.token變量包含以下值:

場地描述
email與帳戶關聯的電子郵件地址(如果存在)。
email_verified如果用戶已驗證他們有權訪問email地址, true 。一些提供商會自動驗證他們擁有的電子郵件地址。
phone_number與帳戶關聯的電話號碼(如果存在)。
name用戶的顯示名稱(如果已設置)。
sub用戶的 Firebase UID。這在項目中是唯一的。
firebase.identities與此用戶帳戶關聯的所有身份的字典。字典的鍵可以是以下任何一個: emailphonegoogle.comfacebook.comgithub.comtwitter.com 。字典的值是與帳戶關聯的每個身份提供者的唯一標識符數組。例如, auth.token.firebase.identities["google.com"][0]包含與帳戶關聯的第一個 Google 用戶 ID。
firebase.sign_in_provider用於獲取此令牌的登錄提供程序。可以是以下字符串之一: custompasswordphoneanonymousgoogle.comfacebook.comgithub.comtwitter.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;
  }
}

要查看更多利用身份驗證的基本規則示例,請參閱基本安全規則