لبدء فرض متطلبات رمز التحقق من التطبيق في وظائف السحابة القابلة للاستدعاء ، قم بتعديل وظائفك للتحقق من وجود رموز التحقق من التطبيق الصالحة.
قبل ان تبدأ
تمكين التطبيق تحقق في عملاء Apple و Android والويب .
إضافة دعم التحقق من التطبيق إلى وظيفة
قم بتحديث تبعية
firebase-functions
الخاصة بمشروعك إلى الإصدار 3.14.0 أو أحدث:npm install firebase-functions@">=3.14.0"
وقم بتحديث تبعية مسؤول Firebase
firebase-admin
لمشروعك إلى الإصدار 9.8.0 أو أحدث:npm install firebase-admin@">=9.8.0"
أضف فحصًا لـ
context.app
إلى وظيفتك. يجب أن تفشل وظيفتك إذا لم يتم تعريفcontext.app
.exports.yourCallableFunction = functions.https.onCall((data, context) => { // context.app will be undefined if the request doesn't include an // App Check token. (If the request includes an invalid App Check // token, the request will be rejected with HTTP error 401.) if (context.app == undefined) { throw new functions.https.HttpsError( 'failed-precondition', 'The function must be called from an App Check verified app.') } // Your function logic follows. });
(اختياري) إذا كنت تريد التعامل مع الرموز المميزة للتحقق من التطبيق غير الصالحة باستخدام منطقك الخاص (على سبيل المثال ، إذا كنت تريد تسجيل الطلبات غير الصالحة مؤقتًا بدلاً من رفضها قبل تمكين التنفيذ الكامل) ، فقم بتعيين
allowInvalidAppCheckToken
إلىtrue
:exports.yourCallableFunction = functions. .runWith({ allowInvalidAppCheckToken: true // Opt-out: Requests with invalid App // Check tokens continue to your code. }) .https.onCall((data, context) => { // Now, requests with an invalid App Check token are not rejected. // // context.app will be undefined if the request: // 1) Does not include an App Check token // 2) Includes an invalid App Check token if (context.app == undefined) { // You can inspect the raw request header to check whether an App // Check token was provided in the request. If you're not ready to // fully enable App Check yet, you could log these conditions instead // of throwing errors. const rawToken = context.rawRequest.header['X-Firebase-AppCheck']; if (rawToken == undefined) { throw new functions.https.HttpsError( 'failed-precondition', 'The function must be called from an App Check verified app.' ); } else { throw new functions.https.HttpsError( 'unauthenticated', 'Provided App Check token failed to validate.' ); } } // Your function logic follows. });
لتمكين الحماية الكاملة للتحقق من التطبيق ، اضبط
allowInvalidAppCheckToken
علىfalse
.أعد نشر وظائفك:
firebase deploy --only functions
بمجرد نشر هذه التغييرات ، ستتطلب وظائف السحابة القابلة للاستدعاء رموز التحقق من التطبيق الصالحة. تُرفق حزم SDK لعميل Cloud Functions تلقائيًا رمز التحقق من التطبيق عند استدعاء وظيفة قابلة للاستدعاء.