เมื่อเข้าใจว่า App Check จะส่งผลต่อผู้ใช้อย่างไร และพร้อมที่จะดำเนินการต่อแล้ว คุณสามารถเปิดใช้การบังคับใช้ App Check สำหรับ ฟังก์ชันที่เรียกใช้ได้
เปิดใช้การบังคับใช้
หากต้องการเริ่มบังคับใช้App Checkข้อกำหนดเกี่ยวกับโทเค็นในฟังก์ชันที่เรียกใช้ได้ ให้แก้ไขฟังก์ชันเพื่อตรวจสอบโทเค็น App Check ที่ถูกต้องตามที่แสดงด้านล่าง เมื่อเปิดใช้การบังคับใช้แล้ว ระบบจะปฏิเสธคำขอที่ยังไม่ได้รับการยืนยันทั้งหมด
ติดตั้ง Cloud Functions SDK
Node.js (รุ่นที่ 2)
อัปเดต
firebase-functions
ทรัพยากร Dependency ของโปรเจ็กต์เป็นเวอร์ชัน 4.0.0 หรือใหม่กว่าnpm install firebase-functions@">=4.0.0"
Node.js (รุ่นที่ 1)
อัปเดต
firebase-functions
ทรัพยากร Dependency ของโปรเจ็กต์เป็นเวอร์ชัน 4.0.0 หรือใหม่กว่าnpm install firebase-functions@">=4.0.0"
Python (เวอร์ชันตัวอย่าง)
วิธีเพิ่ม
firebase-functions
ลงในfunctions/requirements.txt
firebase-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 ที่ถูกต้อง Cloud Functions SDK ของไคลเอ็นต์จะแนบโทเค็น App Check โดยอัตโนมัติ เมื่อคุณเรียกใช้ฟังก์ชันที่เรียกใช้ได้
การป้องกันการเล่นซ้ำ (เบต้า)
หากต้องการปกป้องฟังก์ชันที่เรียกใช้ได้จากการโจมตีแบบรีเพลย์ คุณสามารถใช้โทเค็น 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();