Cloud Storage के लिए Firebase Security Rules, Firebase Authentication के साथ इंटिग्रेट हो जाता है, ताकि Cloud Storage के लिए शक्तिशाली उपयोगकर्ता आधारित प्रमाणीकरण. इससे आपको Firebase Authentication टोकन के दावों के आधार पर विस्तृत ऐक्सेस कंट्रोल.
उपयोगकर्ता की पुष्टि करना
जब कोई पुष्टि किया गया उपयोगकर्ता, Cloud Storage के लिए अनुरोध करता है,
request.auth
वैरिएबल में, उपयोगकर्ता के uid
की जानकारी अपने-आप भर जाती है
(request.auth.uid
) और Firebase Authentication JWT के दावे
(request.auth.token
).
इसके अलावा, पसंद के मुताबिक पुष्टि करने की सुविधा का इस्तेमाल करने पर, अतिरिक्त दावे दिखते हैं
request.auth.token
फ़ील्ड में.
जब कोई ऐसा उपयोगकर्ता अनुरोध करता है जिसकी पुष्टि नहीं हुई है, तो request.auth
वैरिएबल
null
.
इस डेटा का इस्तेमाल करके, आम तौर पर पुष्टि करने के तरीके को सुरक्षित रखने के लिए कई तरीके अपनाए जाते हैं फ़ाइलें:
- सार्वजनिक:
request.auth
को अनदेखा करें - पुष्टि की गई निजी जानकारी: पक्का करें कि
request.auth
,null
न हो - उपयोगकर्ता निजी: जाँचें कि
request.auth.uid
पथuid
के बराबर है - निजी ग्रुप: किसी चुने गए दावे से मैच करने के लिए, कस्टम टोकन के दावों की जांच करें या यह देखने के लिए फ़ाइल मेटाडेटा पढ़ें कि मेटाडेटा फ़ील्ड मौजूद है या नहीं
सार्वजनिक
जो नियम request.auth
कॉन्टेक्स्ट को लागू नहीं करता उसे
public
नियम का इस्तेमाल करता है, क्योंकि यह उपयोगकर्ता की पुष्टि करने के संदर्भ पर विचार नहीं करता.
ये नियम, गेम ऐसेट, साउंड जैसे सार्वजनिक डेटा को दिखाने में मददगार हो सकते हैं
फ़ाइलें या अन्य स्टैटिक कॉन्टेंट.
// Anyone to read a public image if the file is less than 100kB // Anyone can upload a public file ending in '.txt' match /public/{imageId} { allow read: if resource.size < 100 * 1024; allow write: if imageId.matches(".*\\.txt"); }
निजी तौर पर पुष्टि की गई
कुछ मामलों में, हो सकता है कि आप डेटा को इसके सभी प्रमाणित उपयोगकर्ताओं को दिखाना चाहें
ले सकते हैं, लेकिन गैर-पुष्टि नहीं किए गए उपयोगकर्ताओं से नहीं. request.auth
से
बिना पुष्टि वाले उपयोगकर्ताओं के लिए वैरिएबल null
है. आपको बस इसकी जांच करनी है
request.auth
वैरिएबल, पुष्टि करने के लिए मौजूद होता है:
// Require authentication on all internal image reads match /internal/{imageId} { allow read: if request.auth != null; }
निजी उपयोगकर्ता
अब तक, request.auth
का सबसे सामान्य इस्तेमाल का उदाहरण
ऐसे उपयोगकर्ता जिनके पास अपनी फ़ाइलों पर पूरी अनुमतियां हैं: प्रोफ़ाइल फ़ोटो अपलोड करने से
निजी दस्तावेज़ों को पढ़ने के लिए डिज़ाइन किया गया है.
चूंकि Cloud Storage में फ़ाइलों का फ़ाइल का पूरा पथ है, इसलिए इसे
किसी फ़ाइल को उपयोगकर्ता कंट्रोल करने के लिए इस्तेमाल किया जाता है.
फ़ाइल पाथ में मौजूद जानकारी (जैसे कि उपयोगकर्ता की uid
) की जांच कब की जा सकती है
नियम का मूल्यांकन किया जाता है:
// Only a user can upload their profile picture, but anyone can view it match /users/{userId}/profilePicture.png { allow read; allow write: if request.auth != null && request.auth.uid == userId; }
समूह निजी
किसी ऑब्जेक्ट पर ग्रुप की अनुमतियों को अनुमति देना भी समान रूप से सामान्य इस्तेमाल का उदाहरण होगा, जैसे, शेयर किए गए किसी दस्तावेज़ पर टीम के कई सदस्यों को साथ मिलकर काम करने की अनुमति देना. यह लीजिए ऐसा करने के कई तरीके हैं:
- Firebase Authentication कस्टम टोकन मिंट करें जिसमें ग्रुप के किसी सदस्य के बारे में ज़्यादा जानकारी शामिल हो. जैसे, ग्रुप आईडी
- इसमें ग्रुप की जानकारी (जैसे कि ग्रुप आईडी या अनुमति वाले
uid
की सूची) शामिल करें फ़ाइल मेटाडेटा
इस डेटा के टोकन या फ़ाइल मेटाडेटा में सेव होने के बाद, इसका रेफ़रंस दिया जा सकता है एक नियम के अंदर से:
// Allow reads if the group ID in your token matches the file metadata's `owner` property // Allow writes if the group ID is in the user's custom token match /files/{groupId}/{fileName} { allow read: if resource.metadata.owner == request.auth.token.groupId; allow write: if request.auth.token.groupId == groupId; }
पूरा उदाहरण
पुष्टि करने से जुड़ी चार सामान्य तरह की पाबंदियों के सामान्य मामलों को दिखाया गया है नीचे दिए गए उदाहरण में:
service firebase.storage { match /b/{bucket}/o { match /images { // Anyone can view any image (no auth, publicly readable) match /{allImages=**} { allow read; } // Only authenticated users can write to "public" images match /public/{imageId} { allow write: if request.auth != null; } // Only an individual user can write to "their" images match /{userId}/{imageId} { allow write: if request.auth.uid == userId; } // Allow a "group" of users to read/write to shared images // An owner metadata property on the object contains the groupId for reads // A custom token has been minted with a groupId property for writes match /{groupId}/{imageId} { allow read: if resource.metadata.owner == request.auth.token.groupId; allow write: if request.auth.token.groupId == groupId; } } } }