"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.checkDeviceIntegrity = void 0; const functions = __importStar(require("firebase-functions")); const google_auth_library_1 = require("google-auth-library"); const PACKAGE_NAME = 'app.closer'; const PLAY_INTEGRITY_URL = `https://playintegrity.googleapis.com/v1/${PACKAGE_NAME}:decodeIntegrityToken`; /** * Verifies a Play Integrity API token server-side and returns whether the * device meets basic integrity requirements. * * Called by [PlayIntegrityChecker] on app startup. Requires the caller to be * authenticated — the Firebase Auth token is verified automatically by the * Functions runtime. * * Setup required before this function works in production: * 1. Enable the Play Integrity API in your Google Cloud project. * 2. Link the Cloud project to your Play Console app (Play Console → * Setup → API access → Link to Cloud project). * 3. Grant the Cloud Functions service account the "Play Integrity API User" * role in IAM, or use a dedicated service account key via * GOOGLE_APPLICATION_CREDENTIALS. * * If the Play Integrity API is not configured the function fails-closed * (returns passed: false). Configure the API and grant the service account * the "Play Integrity API User" IAM role before deploying to production. */ exports.checkDeviceIntegrity = functions.https.onCall(async (data, context) => { if (!context.auth) { throw new functions.https.HttpsError('unauthenticated', 'Caller must be authenticated.'); } if (!context.app) { throw new functions.https.HttpsError('failed-precondition', 'App Check verification required.'); } const token = data === null || data === void 0 ? void 0 : data.token; if (!token || typeof token !== 'string') { throw new functions.https.HttpsError('invalid-argument', 'token is required.'); } try { const verdicts = await decodeIntegrityToken(token); const passed = verdicts.includes('MEETS_DEVICE_INTEGRITY') || verdicts.includes('MEETS_STRONG_INTEGRITY'); return { passed, verdicts }; } catch (err) { console.error('[checkDeviceIntegrity] verification failed:', err); // Fail-closed: an unverifiable request is treated as failed, not passed. // Ensure the Play Integrity API is enabled and the service account has // "Play Integrity API User" role before deploying to production. return { passed: false, verdicts: [], error: 'verification_unavailable' }; } }); async function decodeIntegrityToken(token) { var _a, _b, _c; const auth = new google_auth_library_1.GoogleAuth({ scopes: ['https://www.googleapis.com/auth/playintegrity'], }); const client = await auth.getClient(); const response = await client.request({ url: PLAY_INTEGRITY_URL, method: 'POST', data: { integrity_token: token }, }); return ((_c = (_b = (_a = response.data.tokenPayloadExternal) === null || _a === void 0 ? void 0 : _a.deviceIntegrity) === null || _b === void 0 ? void 0 : _b.deviceRecognitionVerdict) !== null && _c !== void 0 ? _c : []); } //# sourceMappingURL=checkDeviceIntegrity.js.map