Wenn Sie verstanden haben, wie sich App Check auf Ihre Nutzer auswirkt und Sie bereit sind, fortzufahren, können Sie die App Check-Durchsetzung für aufrufbare Funktionen aktivieren.
Erzwingung aktivieren
Wenn Sie die Anforderungen für App Check-Tokens in Ihren aufrufbaren Funktionen durchsetzen möchten, ändern Sie Ihre Funktionen so, dass sie wie unten gezeigt auf gültige App Check-Tokens prüfen. Wenn Sie die Erzwingung aktivieren, werden alle nicht überprüften Anfragen abgelehnt.
Installieren Sie das Cloud Functions SDK.
Node.js (2. Generation)
Aktualisieren Sie die
firebase-functions
-Abhängigkeit Ihres Projekts auf Version 4.0.0 oder höher:npm install firebase-functions@">=4.0.0"
Node.js (1. Generation)
Aktualisieren Sie die
firebase-functions
-Abhängigkeit Ihres Projekts auf Version 4.0.0 oder höher:npm install firebase-functions@">=4.0.0"
Python (Vorschau)
firebase-functions
zufunctions/requirements.txt
hinzufügen:firebase-functions >= 0.1.0
Aktualisieren Sie dann die Abhängigkeiten in der virtuellen Umgebung Ihres Projekts:
./venv/bin/pip install -r requirements.txt
Aktivieren Sie die Laufzeitoption „App Check-Erzwingung“ für Ihre Funktion:
Node.js (2. Generation)
const { onCall } = require("firebase-functions/v2/https"); exports.yourV2CallableFunction = onCall( { enforceAppCheck: true, // Reject requests with missing or invalid App Check tokens. }, (request) => { // request.app contains data from App Check, including the app ID. // Your function logic follows. ... } );
Node.js (1. Generation)
const functions = require("firebase-functions/v1"); exports.yourV1CallableFunction = functions .runWith({ enforceAppCheck: true, // Reject requests with missing or invalid App Check tokens. }) .https.onCall((data, context) => { // context.app contains data from App Check, including the app ID. // Your function logic follows. ... });
Python (Vorschau)
from firebase_functions import https_fn @https_fn.on_call( enforce_app_check=True # Reject requests with missing or invalid App Check tokens. ) def your_callable_function(req: https_fn.CallableRequest) -> https_fn.Response: # req.app contains data from App Check, including the app ID. # Your function logic follows. ...
Stellen Sie Ihre Funktionen noch einmal bereit:
firebase deploy --only functions
Nachdem diese Änderungen bereitgestellt wurden, benötigen Ihre aufrufbaren Funktionen gültige App Check-Tokens. Die Cloud Functions-Client-SDKs hängen automatisch ein App Check-Token an, wenn Sie eine aufrufbare Funktion aufrufen.
Schutz vor Replay-Angriffen (Beta)
Um eine aufrufbare Funktion vor Replay-Angriffen zu schützen, können Sie das App Check-Token nach der Bestätigung verwenden. Nachdem das Token verwendet wurde, kann es nicht noch einmal verwendet werden.
Wenn Sie den Replay-Schutz verwenden, wird der Tokenbestätigung ein Netzwerk-Roundtrip hinzugefügt. Dadurch wird die Latenz des Funktionsaufrufs erhöht. Aus diesem Grund aktivieren die meisten Apps den Replay-Schutz in der Regel nur für besonders sensible Endpunkte.
So verwenden Sie Tokens:
Gewähren Sie in der Google Cloud-Konsole dem von der Funktion verwendeten Dienstkonto die Rolle „Firebase App Check Token Verifier“.
- Wenn Sie das Admin SDK explizit initialisieren und die Anmeldedaten des Admin SDK-Dienstkontos Ihres Projekts angegeben haben, ist die erforderliche Rolle bereits zugewiesen.
- Wenn Sie Cloud Functions der 1. Generation mit der Standardkonfiguration des Admin SDK verwenden, weisen Sie die Rolle dem App Engine-Standarddienstkonto zu. Weitere Informationen finden Sie unter Dienstkontoberechtigungen ändern.
- Wenn Sie Cloud Functions der 2. Generation mit der Standardkonfiguration des Admin SDK verwenden, weisen Sie die Rolle dem Compute-Standarddienstkonto zu.
Legen Sie in Ihrer Funktionsdefinition
consumeAppCheckToken
auftrue
fest:Node.js (2. Generation)
const { onCall } = require("firebase-functions/v2/https"); exports.yourV2CallableFunction = onCall( { enforceAppCheck: true, // Reject requests with missing or invalid App Check tokens. consumeAppCheckToken: true // Consume the token after verification. }, (request) => { // request.app contains data from App Check, including the app ID. // Your function logic follows. ... } );
Node.js (1. Generation)
const functions = require("firebase-functions/v1"); exports.yourV1CallableFunction = functions .runWith({ enforceAppCheck: true, // Reject requests with missing or invalid App Check tokens. consumeAppCheckToken: true // Consume the token after verification. }) .https.onCall((data, context) => { // context.app contains data from App Check, including the app ID. // Your function logic follows. ... });
Aktualisieren Sie den Clientcode Ihrer App, um Einmal-Tokens zu erhalten, wenn Sie die Funktion aufrufen:
Swift
let options = HTTPSCallableOptions(requireLimitedUseAppCheckTokens: true) let yourCallableFunction = Functions.functions().httpsCallable("yourCallableFunction", options: options) do { let result = try await yourCallableFunction.call() } catch { // ... }
Kotlin
val yourCallableFunction = Firebase.functions.getHttpsCallable("yourCallableFunction") { limitedUseAppCheckTokens = true } val result = yourCallableFunction.call().await()
Java
HttpsCallableReference yourCallableFunction = FirebaseFunctions.getInstance().getHttpsCallable( "yourCallableFunction", new HttpsCallableOptions.Builder() .setLimitedUseAppCheckTokens(true) .build() ); Task<HttpsCallableResult> result = yourCallableFunction.call();
Web
import { getFunctions, httpsCallable } from "firebase/functions"; const yourCallableFunction = httpsCallable( getFunctions(), "yourCallableFunction", { limitedUseAppCheckTokens: true }, ); await yourCallableFunction();