כדי להתחיל לאכוף את דרישות ה-App Check token בפונקציות הענן הניתנות להתקשרות, שנה את הפונקציות שלך כדי לחפש אסימוני App Check חוקיים.
לפני שאתה מתחיל
אפשר בדיקת אפליקציות בלקוחות Apple , Android ו- Web שלך.
הוסף תמיכת App Check לפונקציה
עדכן את התלות ב-firebase
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
לאחר פריסת השינויים הללו, פונקציות הענן הניתנות להתקשרות ידרשו אסימוני App Check חוקיים. ערכות ה-SDK של לקוח Cloud Functions מצרף אוטומטית אסימון App Check כאשר אתה מפעיל פונקציה הניתנת להתקשרות.