เมื่อคุณ เข้าใจว่า App Check จะส่งผลต่อผู้ใช้ของคุณอย่างไร และพร้อมที่จะดำเนินการต่อแล้ว คุณสามารถเปิดใช้การบังคับใช้ App Check สำหรับ ฟังก์ชันที่เรียกใช้ได้
เปิดใช้การบังคับใช้
หากต้องการเริ่มบังคับใช้ข้อกำหนดโทเค็น App Check ในฟังก์ชันที่เรียกใช้ได้ ให้แก้ไขฟังก์ชันเพื่อตรวจสอบโทเค็น App Check ที่ถูกต้อง ดังที่แสดงด้านล่าง เมื่อเปิดใช้การบังคับใช้แล้ว ระบบจะปฏิเสธคำขอที่ไม่ได้ยืนยันทั้งหมด
ติดตั้ง Cloud Functions SDK
Node.js (รุ่นที่ 2)
อัปเดตทรัพยากร Dependency
firebase-functionsของโปรเจ็กต์เป็นเวอร์ชัน 4.0.0 ขึ้นไปnpm install firebase-functions@">=4.0.0"Node.js (รุ่นที่ 1)
อัปเดตทรัพยากร Dependency
firebase-functionsของโปรเจ็กต์เป็นเวอร์ชัน 4.0.0 ขึ้นไปnpm install firebase-functions@">=4.0.0"Python (เวอร์ชันตัวอย่าง)
เพิ่ม
firebase-functionsลงในfunctions/requirements.txtfirebase-functions >= 0.1.0จากนั้นอัปเดตทรัพยากร Dependency ในสภาพแวดล้อมเสมือนของโปรเจ็กต์
./venv/bin/pip install -r requirements.txtเปิดใช้ตัวเลือกการรันไทม์การบังคับใช้ App Check สำหรับฟังก์ชัน
Node.js (รุ่นที่ 2)
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)
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 (เวอร์ชันตัวอย่าง)
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. ...ทำให้ฟังก์ชันใช้งานได้อีกครั้ง
firebase deploy --only functions
เมื่อทำให้การเปลี่ยนแปลงเหล่านี้ใช้งานได้แล้ว ฟังก์ชันที่เรียกใช้ได้จะต้องใช้ โทเค็น App Check ที่ถูกต้อง Client SDK Cloud Functions จะแนบโทเค็น App Check โดยอัตโนมัติ เมื่อคุณเรียกใช้ฟังก์ชันที่เรียกใช้ได้
การป้องกันการเล่นซ้ำ (เบต้า)
หากต้องการป้องกันฟังก์ชันที่เรียกใช้ได้จากการโจมตีแบบเล่นซ้ำ คุณสามารถใช้โทเค็น App Check หลังจากยืนยันแล้ว เมื่อใช้โทเค็นแล้ว จะใช้โทเค็นนั้นอีกไม่ได้
โปรดทราบว่าการใช้การป้องกันการเล่นซ้ำจะเพิ่มการเดินทางไปกลับของเครือข่ายในการยืนยันโทเค็น จึงทำให้การเรียกฟังก์ชันมีเวลาในการตอบสนองเพิ่มขึ้น ด้วยเหตุนี้ แอปส่วนใหญ่จึงมักจะเปิดใช้การป้องกันการเล่นซ้ำเฉพาะในปลายทางที่มีความละเอียดอ่อนเป็นพิเศษ
วิธีใช้โทเค็น
ใน Google Cloudคอนโซล, ให้มอบบทบาท "ผู้ยืนยันโทเค็น Firebase App Check" ให้กับบัญชีบริการ ที่ฟังก์ชันใช้
- หากคุณเริ่มต้นใช้งาน Admin SDK อย่างชัดเจนและระบุข้อมูลเข้าสู่ระบบบัญชีบริการ Admin SDK ของโปรเจ็กต์ บทบาทที่จำเป็นจะได้รับมอบหมายแล้ว
- หากคุณใช้ Cloud Functions รุ่นที่ 1 กับการกำหนดค่า Admin SDK เริ่มต้น ให้มอบบทบาทให้กับบัญชีบริการเริ่มต้นของ App Engine ดูการเปลี่ยนสิทธิ์ของบัญชีบริการ
- หากคุณใช้ Cloud Functions รุ่นที่ 2 กับการกำหนดค่า Admin SDK เริ่มต้น ให้มอบบทบาทให้กับบัญชีบริการเริ่มต้นของ Compute
ตั้งค่า
consumeAppCheckTokenเป็นtrueในคำจำกัดความฟังก์ชันNode.js (รุ่นที่ 2)
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)
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. ... });อัปเดตโค้ดฝั่งไคลเอ็นต์ของแอปเพื่อรับโทเค็นแบบใช้ครั้งเดียวที่ใช้ได้เมื่อคุณเรียกฟังก์ชัน
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();