برای شروع اجرای الزامات نشانه بررسی برنامه در توابع Cloud قابل فراخوانی، عملکردهای خود را برای بررسی نشانه های معتبر بررسی برنامه تغییر دهید.
قبل از اینکه شروع کنی
در کلاینت های اپل ، اندروید و وب خود، بررسی برنامه را فعال کنید.
افزودن پشتیبانی بررسی برنامه به یک تابع
وابستگی
firebase-functions
پروژه خود را به نسخه 3.14.0 یا جدیدتر به روز کنید:npm install firebase-functions@">=3.14.0"
و وابستگی
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. });
(اختیاری) اگر میخواهید نشانههای نامعتبر App Check را با منطق خود مدیریت کنید (به عنوان مثال، اگر میخواهید بهجای رد درخواستهای نامعتبر قبل از فعال کردن اجرای کامل، موقتاً ثبت نام کنید)،
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. });
برای فعال کردن حفاظت کامل App Check،
allowInvalidAppCheckToken
را رویfalse
تنظیم کنید.توابع خود را مجدداً مستقر کنید:
firebase deploy --only functions
پس از اعمال این تغییرات، توابع Cloud قابل فراخوانی به کدهای معتبر App Check نیاز دارند. هنگامی که یک تابع قابل فراخوانی را فراخوانی می کنید، کیت توسعه نرم افزاری سرویس گیرنده توابع Cloud به طور خودکار یک نشانه بررسی برنامه را متصل می کند.