安全規則和 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 FirestoreRealtime DatabaseCloud 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變數來定義指派給應用程式使用者的自訂欄位。

例如,假設您要建立一個「admin」角色,以啟用對某些路徑的寫入存取權限。您可以將該屬性指派給用戶,然後在授予路徑存取權限的規則中利用它。

在 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;
  }
}

在身份驗證中建立自訂聲明後,您可以在規則中存取自訂聲明。然後,您可以使用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;
  }
}

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