Para comenzar a hacer cumplir los requisitos del token de verificación de aplicaciones en sus funciones en la nube a las que se puede llamar, modifique sus funciones para verificar si hay tokens de verificación de aplicaciones válidos.
Antes de que empieces
Habilite App Check en sus clientes Apple , Android y Web .
Agregar compatibilidad con App Check a una función
Actualice la
firebase-functions
de base de fuego de su proyecto a la versión 3.14.0 o posterior:npm install firebase-functions@">=3.14.0"
Y actualice la dependencia
firebase-admin
de su proyecto a la versión 9.8.0 o posterior:npm install firebase-admin@">=9.8.0"
Agregue un cheque para
context.app
a su función. Su función debería fallar sicontext.app
no está definido.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. });
(Opcional) Si desea manejar tokens de verificación de aplicaciones no válidos con su propia lógica (por ejemplo, si desea registrar temporalmente, en lugar de rechazar, solicitudes no válidas antes de habilitar la aplicación total), establezca
allowInvalidAppCheckToken
entrue
: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. });
Para habilitar la protección completa de App Check, establezca
allowInvalidAppCheckToken
enfalse
.Redistribuya sus funciones:
firebase deploy --only functions
Una vez que se implementen estos cambios, sus Cloud Functions a las que se puede llamar requerirán tokens de verificación de aplicaciones válidos. Los SDK de cliente de Cloud Functions adjuntan automáticamente un token de App Check cuando invocas una función invocable.