호출 가능한 Cloud Functions에서 App Check 토큰 요구 사항을 시행하려면 유효한 App Check 토큰을 확인하도록 함수를 수정하십시오.
시작하기 전에
Apple , Android 및 웹 클라이언트에서 앱 확인을 활성화합니다.
기능에 앱 확인 지원 추가
프로젝트의 firebase
firebase-functions
종속성을 버전 3.14.0 이상으로 업데이트합니다.npm install firebase-functions@">=3.14.0"
그리고 프로젝트의 firebase
firebase-admin
종속성을 버전 9.8.0 이상으로 업데이트합니다.npm install firebase-admin@">=9.8.0"
함수에
context.app
에 대한 검사를 추가합니다.context.app
이 정의되어 있지 않으면 함수가 실패해야 합니다.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. });
(선택 사항) 자체 논리로 잘못된 앱 확인 토큰을 처리하려면(예: 전체 시행을 활성화하기 전에 잘못된 요청을 거부하지 않고 일시적으로 기록하려는 경우)
allowInvalidAppCheckToken
을true
로 설정합니다.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. });
전체 앱 검사 보호를 활성화하려면
allowInvalidAppCheckToken
을false
로 설정하십시오.기능 재배포:
firebase deploy --only functions
이러한 변경 사항이 배포되면 호출 가능한 Cloud Functions에 유효한 App Check 토큰이 필요합니다. Cloud Functions 클라이언트 SDK는 호출 가능한 함수를 호출할 때 App Check 토큰을 자동으로 연결합니다.