Per iniziare ad applicare i requisiti del token App Check nelle tue funzioni cloud richiamabili, modifica le tue funzioni per verificare la presenza di token App Check validi.
Prima di iniziare
Abilita App Check nei tuoi client Apple , Android e Web .
Aggiungi il supporto di App Check a una funzione
Aggiorna la dipendenza
firebase-functions
del tuo progetto alla versione 3.14.0 o successiva:npm install firebase-functions@">=3.14.0"
E aggiorna la dipendenza
firebase-admin
del tuo progetto alla versione 9.8.0 o successiva:npm install firebase-admin@">=9.8.0"
Aggiungi un controllo per
context.app
alla tua funzione. La tua funzione dovrebbe fallire secontext.app
non è definito.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. });
(Facoltativo) Se desideri gestire token App Check non validi con la tua logica (ad esempio, se desideri registrare temporaneamente, anziché rifiutare, richieste non valide prima di abilitare l'applicazione completa), imposta
allowInvalidAppCheckToken
sutrue
: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. });
Per abilitare la protezione completa di App Check, imposta
allowInvalidAppCheckToken
sufalse
.Ridistribuisci le tue funzioni:
firebase deploy --only functions
Una volta distribuite queste modifiche, le tue funzioni cloud richiamabili richiederanno token App Check validi. Gli SDK client di Cloud Functions allegano automaticamente un token App Check quando si richiama una funzione richiamabile.