تتيح حزمة تطوير البرامج (SDK) للمشرف في Firebase تحديد سمات مخصّصة في حسابات المستخدمين. ويوفّر ذلك إمكانية تنفيذ استراتيجيات مختلفة للتحكّم في الوصول، بما في ذلك التحكّم في الوصول استنادًا إلى الدور، في تطبيقات Firebase. يمكن أن تمنح هذه السمات المخصّصة المستخدمين مستويات وصول مختلفة (أدوارًا)، ويتم فرضها في قواعد الأمان الخاصة بالتطبيق.
يمكن تحديد أدوار المستخدمين للحالات الشائعة التالية:
- منح المستخدم امتيازات إدارية للوصول إلى البيانات والموارد
- تحديد المجموعات المختلفة التي ينتمي إليها المستخدم
- توفير إمكانية الوصول إلى مستويات متعددة:
- التمييز بين المشتركين المدفوعين وغير المدفوعين
- التمييز بين المشرفين والمستخدمين العاديين
- تطبيق المعلّم/الطالب وما إلى ذلك
- إضافة معرّف إضافي لمستخدم على سبيل المثال، يمكن ربط مستخدم Firebase بمعرّف مستخدم مختلف في نظام آخر.
لنفترض أنّك تريد حصر الوصول إلى عقدة قاعدة البيانات "adminContent". يمكنك إجراء ذلك من خلال البحث في قاعدة البيانات عن قائمة بالمستخدمين المشرفين. ومع ذلك، يمكنك تحقيق الهدف نفسه بشكل أكثر فعالية باستخدام مطالبة مستخدم مخصّصة باسم admin
مع القاعدة التالية Realtime Database:
{
"rules": {
"adminContent": {
".read": "auth.token.admin === true",
".write": "auth.token.admin === true",
}
}
}
يمكن الوصول إلى مطالبات المستخدم المخصّصة من خلال رموز المصادقة المميزة الخاصة بالمستخدم.
في المثال أعلاه، سيتمكّن فقط المستخدمون الذين تم ضبط قيمة admin
على "صحيح" في مطالبة الرمز المميّز من الوصول إلى العقدة adminContent
للقراءة والكتابة. بما أنّ رمز التعريف يتضمّن هذه التأكيدات، لا يلزم إجراء أي معالجة أو بحث إضافي للتحقّق من أذونات المشرف. بالإضافة إلى ذلك، فإنّ رمز التعريف هو آلية موثوقة لتقديم هذه المطالبات المخصّصة. يجب أن تتحقّق جميع عمليات الوصول التي تتطلّب مصادقة من صحة رمز التعريف قبل معالجة الطلب المرتبط به.
تستند أمثلة الرموز البرمجية والحلول الموضّحة في هذه الصفحة إلى واجهات برمجة التطبيقات الخاصة بخدمة Firebase Auth من جهة العميل وواجهات برمجة التطبيقات الخاصة بخدمة Auth من جهة الخادم التي توفّرها حزمة SDK للمشرف.
ضبط مطالبات المستخدم المخصّصة والتحقّق من صحتها من خلال حزمة تطوير البرامج لوحدة تحكّم المشرف
يمكن أن تحتوي المطالبات المخصّصة على بيانات حساسة، لذا يجب ضبطها فقط من بيئة خادم ذات امتيازات باستخدام حزمة تطوير البرامج (SDK) لمشرف Firebase.
Node.js
// Set admin privilege on the user corresponding to uid.
getAuth()
.setCustomUserClaims(uid, { admin: true })
.then(() => {
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
});
Java
// Set admin privilege on the user corresponding to uid.
Map<String, Object> claims = new HashMap<>();
claims.put("admin", true);
FirebaseAuth.getInstance().setCustomUserClaims(uid, claims);
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
Python
# Set admin privilege on the user corresponding to uid.
auth.set_custom_user_claims(uid, {'admin': True})
# The new custom claims will propagate to the user's ID token the
# next time a new one is issued.
Go
// Get an auth client from the firebase.App
client, err := app.Auth(ctx)
if err != nil {
log.Fatalf("error getting Auth client: %v\n", err)
}
// Set admin privilege on the user corresponding to uid.
claims := map[string]interface{}{"admin": true}
err = client.SetCustomUserClaims(ctx, uid, claims)
if err != nil {
log.Fatalf("error setting custom claims %v\n", err)
}
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
#C
// Set admin privileges on the user corresponding to uid.
var claims = new Dictionary<string, object>()
{
{ "admin", true },
};
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(uid, claims);
// The new custom claims will propagate to the user's ID token the
// next time a new one is issued.
يجب ألا يحتوي كائن المطالبات المخصّصة على أي أسماء مفاتيح محجوزة في OIDC أو أسماء محجوزة في Firebase. يجب ألا يتجاوز حجم حمولة المطالبات المخصّصة 1000 بايت.
يمكن لرمز مميّز للهوية يتم إرساله إلى خادم الخلفية تأكيد هوية المستخدم ومستوى الوصول باستخدام حزمة SDK للمشرف على النحو التالي:
Node.js
// Verify the ID token first.
getAuth()
.verifyIdToken(idToken)
.then((claims) => {
if (claims.admin === true) {
// Allow access to requested admin resource.
}
});
Java
// Verify the ID token first.
FirebaseToken decoded = FirebaseAuth.getInstance().verifyIdToken(idToken);
if (Boolean.TRUE.equals(decoded.getClaims().get("admin"))) {
// Allow access to requested admin resource.
}
Python
# Verify the ID token first.
claims = auth.verify_id_token(id_token)
if claims['admin'] is True:
# Allow access to requested admin resource.
pass
Go
// Verify the ID token first.
token, err := client.VerifyIDToken(ctx, idToken)
if err != nil {
log.Fatal(err)
}
claims := token.Claims
if admin, ok := claims["admin"]; ok {
if admin.(bool) {
//Allow access to requested admin resource.
}
}
#C
// Verify the ID token first.
FirebaseToken decoded = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken);
object isAdmin;
if (decoded.Claims.TryGetValue("admin", out isAdmin))
{
if ((bool)isAdmin)
{
// Allow access to requested admin resource.
}
}
يمكنك أيضًا التحقّق من المطالبات المخصّصة الحالية للمستخدم، والتي تتوفّر كسمة في عنصر المستخدم:
Node.js
// Lookup the user associated with the specified uid.
getAuth()
.getUser(uid)
.then((userRecord) => {
// The claims can be accessed on the user record.
console.log(userRecord.customClaims['admin']);
});
Java
// Lookup the user associated with the specified uid.
UserRecord user = FirebaseAuth.getInstance().getUser(uid);
System.out.println(user.getCustomClaims().get("admin"));
Python
# Lookup the user associated with the specified uid.
user = auth.get_user(uid)
# The claims can be accessed on the user record.
print(user.custom_claims.get('admin'))
Go
// Lookup the user associated with the specified uid.
user, err := client.GetUser(ctx, uid)
if err != nil {
log.Fatal(err)
}
// The claims can be accessed on the user record.
if admin, ok := user.CustomClaims["admin"]; ok {
if admin.(bool) {
log.Println(admin)
}
}
#C
// Lookup the user associated with the specified uid.
UserRecord user = await FirebaseAuth.DefaultInstance.GetUserAsync(uid);
Console.WriteLine(user.CustomClaims["admin"]);
يمكنك حذف المطالبات المخصّصة للمستخدم عن طريق تمرير قيمة فارغة (null) إلى customClaims
.
نقل المطالبات المخصّصة إلى العميل
بعد تعديل المطالبات الجديدة لمستخدم من خلال Admin SDK، يتم نشرها إلى مستخدم مصادق عليه من جهة العميل عبر رمز التعريف المميز بالطرق التالية:
- يسجّل المستخدم الدخول أو يعيد المصادقة بعد تعديل المطالبات المخصّصة. سيتضمّن رمز التعريف المميز الذي تم إصداره نتيجة لذلك أحدث المطالبات.
- يتم تجديد رمز التعريف لجلسة مستخدم حالية بعد انتهاء صلاحية رمز قديم.
- تتم إعادة تحميل الرمز المميّز للمعرّف بشكل إلزامي من خلال استدعاء
currentUser.getIdToken(true)
.
الوصول إلى المطالبات المخصّصة على العميل
لا يمكن استرداد المطالبات المخصّصة إلا من خلال رمز التعريف المميز للمستخدم. قد يكون الوصول إلى هذه المطالبات ضروريًا لتعديل واجهة مستخدم العميل استنادًا إلى دور المستخدم أو مستوى الوصول. ومع ذلك، يجب دائمًا فرض إذن الوصول إلى الخلفية من خلال الرمز المميّز لتعريف الهوية بعد التحقّق منه وتحليل مطالباته. يجب عدم إرسال المطالبات المخصّصة مباشرةً إلى الخلفية، لأنّه لا يمكن الوثوق بها خارج الرمز المميّز.
بعد أن يتم نشر أحدث المطالبات إلى رمز تعريف المستخدم، يمكنك الحصول عليها من خلال استرداد رمز التعريف:
JavaScript
firebase.auth().currentUser.getIdTokenResult()
.then((idTokenResult) => {
// Confirm the user is an Admin.
if (!!idTokenResult.claims.admin) {
// Show admin UI.
showAdminUI();
} else {
// Show regular user UI.
showRegularUI();
}
})
.catch((error) => {
console.log(error);
});
Android
user.getIdToken(false).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
@Override
public void onSuccess(GetTokenResult result) {
boolean isAdmin = result.getClaims().get("admin");
if (isAdmin) {
// Show admin UI.
showAdminUI();
} else {
// Show regular user UI.
showRegularUI();
}
}
});
Swift
user.getIDTokenResult(completion: { (result, error) in
guard let admin = result?.claims?["admin"] as? NSNumber else {
// Show regular user UI.
showRegularUI()
return
}
if admin.boolValue {
// Show admin UI.
showAdminUI()
} else {
// Show regular user UI.
showRegularUI()
}
})
Objective-C
user.getIDTokenResultWithCompletion:^(FIRAuthTokenResult *result,
NSError *error) {
if (error != nil) {
BOOL *admin = [result.claims[@"admin"] boolValue];
if (admin) {
// Show admin UI.
[self showAdminUI];
} else {
// Show regular user UI.
[self showRegularUI];
}
}
}];
أفضل الممارسات المتعلقة بالمطالبات المخصّصة
يتم استخدام المطالبات المخصّصة لتوفير إذن الوصول فقط. وهي غير مصمَّمة لتخزين بيانات إضافية (مثل الملف الشخصي والبيانات المخصّصة الأخرى). على الرغم من أنّ هذه الطريقة قد تبدو آلية ملائمة لتنفيذ ذلك، ننصح بشدة بعدم استخدامها لأنّ هذه المطالبات يتم تخزينها في رمز التعريف، وقد تتسبّب في حدوث مشاكل في الأداء لأنّ جميع الطلبات التي تمّت المصادقة عليها تحتوي دائمًا على رمز تعريف Firebase يتوافق مع المستخدم الذي سجّل الدخول.
- استخدِم المطالبات المخصّصة لتخزين البيانات بغرض التحكّم في وصول المستخدمين فقط. يجب تخزين جميع البيانات الأخرى بشكل منفصل من خلال قاعدة البيانات في الوقت الفعلي أو أي مساحة تخزين أخرى من جهة الخادم.
- يجب أن يكون حجم المطالبات المخصّصة محدودًا. سيؤدي تمرير حمولة مطالبات مخصّصة أكبر من 1000 بايت إلى حدوث خطأ.
أمثلة وحالات استخدام
توضّح الأمثلة التالية المطالبات المخصّصة في سياق حالات استخدام محدّدة في Firebase.
تحديد الأدوار من خلال "وظائف Firebase" عند إنشاء المستخدم
في هذا المثال، يتم ضبط المطالبات المخصّصة على مستخدم عند الإنشاء باستخدام Cloud Functions.
يمكن إضافة مطالبات مخصّصة باستخدام Cloud Functions، ويمكن نشرها على الفور باستخدام Realtime Database. يتم استدعاء الدالة عند الاشتراك فقط باستخدام مشغّل onCreate
. بعد ضبط المطالبات المخصّصة، يتم نشرها إلى جميع الجلسات الحالية والمستقبلية. في المرة التالية التي يسجّل فيها المستخدم الدخول باستخدام بيانات الاعتماد، سيحتوي الرمز المميّز على المطالبات المخصّصة.
التنفيذ من جهة العميل (JavaScript)
const provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider)
.catch(error => {
console.log(error);
});
let callback = null;
let metadataRef = null;
firebase.auth().onAuthStateChanged(user => {
// Remove previous listener.
if (callback) {
metadataRef.off('value', callback);
}
// On user login add new listener.
if (user) {
// Check if refresh is required.
metadataRef = firebase.database().ref('metadata/' + user.uid + '/refreshTime');
callback = (snapshot) => {
// Force refresh to pick up the latest custom claims changes.
// Note this is always triggered on first call. Further optimization could be
// added to avoid the initial trigger when the token is issued and already contains
// the latest claims.
user.getIdToken(true);
};
// Subscribe new listener to changes on that node.
metadataRef.on('value', callback);
}
});
منطق Cloud Functions
تتم إضافة عقدة قاعدة بيانات جديدة (metadata/($uid)} مع إذن القراءة/الكتابة محصورًا بالمستخدم الذي تم إثبات هويته.
const functions = require('firebase-functions');
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');
const { getDatabase } = require('firebase-admin/database');
initializeApp();
// On sign up.
exports.processSignUp = functions.auth.user().onCreate(async (user) => {
// Check if user meets role criteria.
if (
user.email &&
user.email.endsWith('@admin.example.com') &&
user.emailVerified
) {
const customClaims = {
admin: true,
accessLevel: 9
};
try {
// Set custom user claims on this newly created user.
await getAuth().setCustomUserClaims(user.uid, customClaims);
// Update real-time database to notify client to force refresh.
const metadataRef = getDatabase().ref('metadata/' + user.uid);
// Set the refresh time to the current UTC timestamp.
// This will be captured on the client to force a token refresh.
await metadataRef.set({refreshTime: new Date().getTime()});
} catch (error) {
console.log(error);
}
}
});
قواعد قاعدة البيانات
{
"rules": {
"metadata": {
"$user_id": {
// Read access only granted to the authenticated user.
".read": "$user_id === auth.uid",
// Write access only via Admin SDK.
".write": false
}
}
}
}
تحديد الأدوار من خلال طلب HTTP
يضبط المثال التالي مطالبات المستخدم المخصّصة على مستخدم تم تسجيل دخوله حديثًا من خلال طلب HTTP.
التنفيذ من جهة العميل (JavaScript)
const provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider)
.then((result) => {
// User is signed in. Get the ID token.
return result.user.getIdToken();
})
.then((idToken) => {
// Pass the ID token to the server.
$.post(
'/setCustomClaims',
{
idToken: idToken
},
(data, status) => {
// This is not required. You could just wait until the token is expired
// and it proactively refreshes.
if (status == 'success' && data) {
const json = JSON.parse(data);
if (json && json.status == 'success') {
// Force token refresh. The token claims will contain the additional claims.
firebase.auth().currentUser.getIdToken(true);
}
}
});
}).catch((error) => {
console.log(error);
});
التنفيذ في الواجهة الخلفية (Admin SDK)
app.post('/setCustomClaims', async (req, res) => {
// Get the ID token passed.
const idToken = req.body.idToken;
// Verify the ID token and decode its payload.
const claims = await getAuth().verifyIdToken(idToken);
// Verify user is eligible for additional privileges.
if (
typeof claims.email !== 'undefined' &&
typeof claims.email_verified !== 'undefined' &&
claims.email_verified &&
claims.email.endsWith('@admin.example.com')
) {
// Add custom claims for additional privileges.
await getAuth().setCustomUserClaims(claims.sub, {
admin: true
});
// Tell client to refresh token on user.
res.end(JSON.stringify({
status: 'success'
}));
} else {
// Return nothing.
res.end(JSON.stringify({ status: 'ineligible' }));
}
});
يمكن استخدام المسار نفسه عند ترقية مستوى وصول مستخدم حالي. لنأخذ على سبيل المثال مستخدمًا مجانيًا يرقّي اشتراكه إلى اشتراك مدفوع. يتم إرسال رمز التعريف المميز للمستخدم مع معلومات الدفع إلى خادم الخلفية من خلال طلب HTTP. عند معالجة الدفعة بنجاح، يتم ضبط المستخدم كمشترك مدفوع من خلال Admin SDK. يتم إرجاع استجابة HTTP ناجحة إلى العميل لإجباره على إعادة تحميل الرمز المميز.
تحديد الأدوار من خلال نص برمجي في الخلفية
يمكن ضبط نص برمجي متكرّر (لم يتم بدء تنفيذه من العميل) ليتم تنفيذه من أجل تعديل المطالبات المخصّصة للمستخدمين:
Node.js
getAuth()
.getUserByEmail('user@admin.example.com')
.then((user) => {
// Confirm user is verified.
if (user.emailVerified) {
// Add custom claims for additional privileges.
// This will be picked up by the user on token refresh or next sign in on new device.
return getAuth().setCustomUserClaims(user.uid, {
admin: true,
});
}
})
.catch((error) => {
console.log(error);
});
Java
UserRecord user = FirebaseAuth.getInstance()
.getUserByEmail("user@admin.example.com");
// Confirm user is verified.
if (user.isEmailVerified()) {
Map<String, Object> claims = new HashMap<>();
claims.put("admin", true);
FirebaseAuth.getInstance().setCustomUserClaims(user.getUid(), claims);
}
Python
user = auth.get_user_by_email('user@admin.example.com')
# Confirm user is verified
if user.email_verified:
# Add custom claims for additional privileges.
# This will be picked up by the user on token refresh or next sign in on new device.
auth.set_custom_user_claims(user.uid, {
'admin': True
})
Go
user, err := client.GetUserByEmail(ctx, "user@admin.example.com")
if err != nil {
log.Fatal(err)
}
// Confirm user is verified
if user.EmailVerified {
// Add custom claims for additional privileges.
// This will be picked up by the user on token refresh or next sign in on new device.
err := client.SetCustomUserClaims(ctx, user.UID, map[string]interface{}{"admin": true})
if err != nil {
log.Fatalf("error setting custom claims %v\n", err)
}
}
#C
UserRecord user = await FirebaseAuth.DefaultInstance
.GetUserByEmailAsync("user@admin.example.com");
// Confirm user is verified.
if (user.EmailVerified)
{
var claims = new Dictionary<string, object>()
{
{ "admin", true },
};
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
}
يمكن أيضًا تعديل المطالبات المخصّصة بشكل تدريجي من خلال حزمة تطوير البرامج (SDK) للمشرف:
Node.js
getAuth()
.getUserByEmail('user@admin.example.com')
.then((user) => {
// Add incremental custom claim without overwriting existing claims.
const currentCustomClaims = user.customClaims;
if (currentCustomClaims['admin']) {
// Add level.
currentCustomClaims['accessLevel'] = 10;
// Add custom claims for additional privileges.
return getAuth().setCustomUserClaims(user.uid, currentCustomClaims);
}
})
.catch((error) => {
console.log(error);
});
Java
UserRecord user = FirebaseAuth.getInstance()
.getUserByEmail("user@admin.example.com");
// Add incremental custom claim without overwriting the existing claims.
Map<String, Object> currentClaims = user.getCustomClaims();
if (Boolean.TRUE.equals(currentClaims.get("admin"))) {
// Add level.
currentClaims.put("level", 10);
// Add custom claims for additional privileges.
FirebaseAuth.getInstance().setCustomUserClaims(user.getUid(), currentClaims);
}
Python
user = auth.get_user_by_email('user@admin.example.com')
# Add incremental custom claim without overwriting existing claims.
current_custom_claims = user.custom_claims
if current_custom_claims.get('admin'):
# Add level.
current_custom_claims['accessLevel'] = 10
# Add custom claims for additional privileges.
auth.set_custom_user_claims(user.uid, current_custom_claims)
Go
user, err := client.GetUserByEmail(ctx, "user@admin.example.com")
if err != nil {
log.Fatal(err)
}
// Add incremental custom claim without overwriting existing claims.
currentCustomClaims := user.CustomClaims
if currentCustomClaims == nil {
currentCustomClaims = map[string]interface{}{}
}
if _, found := currentCustomClaims["admin"]; found {
// Add level.
currentCustomClaims["accessLevel"] = 10
// Add custom claims for additional privileges.
err := client.SetCustomUserClaims(ctx, user.UID, currentCustomClaims)
if err != nil {
log.Fatalf("error setting custom claims %v\n", err)
}
}
#C
UserRecord user = await FirebaseAuth.DefaultInstance
.GetUserByEmailAsync("user@admin.example.com");
// Add incremental custom claims without overwriting the existing claims.
object isAdmin;
if (user.CustomClaims.TryGetValue("admin", out isAdmin) && (bool)isAdmin)
{
var claims = user.CustomClaims.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
// Add level.
var level = 10;
claims["level"] = level;
// Add custom claims for additional privileges.
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(user.Uid, claims);
}