UNPKG

@mathrunet/masamune

Version:

Manages packages for the server portion (NodeJS) of the Masamune framework.

227 lines 10.7 kB
"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; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.verifyIOS = verifyIOS; const functions = __importStar(require("firebase-functions/v2")); const api_1 = require("../api"); const jwt = __importStar(require("jsonwebtoken")); /** * Perform IOS receipt verification. * * IOSの受信確認を実行します。 * * @param {String} receiptData * Receipt data for purchases (for StoreKit1) or JWT token (for StoreKit2). * * 購入の際のレシートデータ(StoreKit1の場合)またはJWTトークン(StoreKit2の場合)。 * * @param {String} password * SharedSecret for AppStore (for StoreKit1), obtained from [Apps]->[App Info]->[Shared Secret for App] in the AppStore. * For StoreKit2, this can be an empty string. * * AppStoreのSharedSecret(StoreKit1の場合)。AppStoreの[アプリ]->[App情報]->[App用共有シークレット]から取得します。 * StoreKit2の場合は空文字列を渡すことができます。 * * @param {Number} storeKitVersion * StoreKit version (1 or 2). Defaults to 1. * * StoreKitのバージョン(1または2)。デフォルトは1。 * * @param {String} transactionId * Transaction ID (required for StoreKit2). * * トランザクションID(StoreKit2の場合は必須)。 * * @return {Promise<{ [key: string]: any; }} * Receipt information for the item. * * アイテムの受領情報。 */ function verifyIOS(_a) { return __awaiter(this, arguments, void 0, function* ({ receiptData, password, storeKitVersion = 1, transactionId }) { if (storeKitVersion === 2) { console.log(`StoreKitVersion2: ${receiptData} ${transactionId}`); if (!transactionId) { throw new functions.https.HttpsError("invalid-argument", "Transaction ID is required for StoreKit2 verification."); } return yield verifyIOSStoreKit2({ jwtToken: receiptData, transactionId }); } console.log(`StoreKitVersion1: ${receiptData}`); if (!password) { throw new functions.https.HttpsError("invalid-argument", "Password is required for StoreKit1 verification."); } let res = yield api_1.Api.post("https://buy.itunes.apple.com/verifyReceipt", { timeout: 30 * 1000, headers: { "Content-Type": "application/json", "Accept": "application/json", }, data: JSON.stringify({ "receipt-data": receiptData, "password": password, "exclude-old-transactions": true, }), }); if (!res) { throw new functions.https.HttpsError("not-found", "The validation data is empty."); } let json = (yield res.json()); let status = json["status"]; if (status === 21007 || status === 21008) { res = yield api_1.Api.post("https://sandbox.itunes.apple.com/verifyReceipt", { timeout: 30 * 1000, headers: { "Content-Type": "application/json", "Accept": "application/json", }, data: JSON.stringify({ "receipt-data": receiptData, "password": password, "exclude-old-transactions": true, }), }); if (!res) { throw new functions.https.HttpsError("not-found", "The validation data is empty."); } json = (yield res.json()); status = json["status"]; if (status !== 0) { throw new functions.https.HttpsError("not-found", "Illegal receipt."); } } else { if (status !== 0) { throw new functions.https.HttpsError("not-found", "Illegal receipt."); } } return json; }); } /** * Verify iOS StoreKit2 transaction. * * iOS StoreKit2のトランザクションを検証します。 * * @param {String} jwtToken * JWT token from StoreKit2. * * StoreKit2からのJWTトークン。 * * * @return {Promise<{ [key: string]: any; }} * Transaction information. * * トランザクション情報。 */ function verifyIOSStoreKit2(_a) { return __awaiter(this, arguments, void 0, function* ({ jwtToken, transactionId }) { var _b, _c, _d, _e, _f, _g; if (!jwtToken) { throw new functions.https.HttpsError("invalid-argument", "JWT token is required for StoreKit2 verification."); } try { const decodedHeader = jwt.decode(jwtToken, { complete: true }); if (!decodedHeader) { throw new functions.https.HttpsError("invalid-argument", "Invalid JWT token."); } const algorithm = decodedHeader.header.alg; // const keyId = decodedHeader.header.kid; // May be used for future certificate validation const x5c = decodedHeader.header.x5c; if (!x5c || !Array.isArray(x5c) || x5c.length === 0) { throw new functions.https.HttpsError("invalid-argument", "Missing x5c certificate chain in JWT header."); } const certificate = `-----BEGIN CERTIFICATE-----\n${x5c[0]}\n-----END CERTIFICATE-----`; const verifiedPayload = jwt.verify(jwtToken, certificate, { algorithms: [algorithm] }); if (transactionId && verifiedPayload.transactionId !== transactionId) { throw new functions.https.HttpsError("permission-denied", "Transaction ID mismatch."); } const result = { status: 0, environment: verifiedPayload.environment || "Production", receipt: { bundle_id: verifiedPayload.bundleId, application_version: verifiedPayload.appVersion, in_app: [{ quantity: verifiedPayload.quantity || "1", product_id: verifiedPayload.productId, transaction_id: verifiedPayload.transactionId, original_transaction_id: verifiedPayload.originalTransactionId, purchase_date_ms: (_b = verifiedPayload.purchaseDate) === null || _b === void 0 ? void 0 : _b.toString(), original_purchase_date_ms: (_c = verifiedPayload.originalPurchaseDate) === null || _c === void 0 ? void 0 : _c.toString(), expires_date_ms: (_d = verifiedPayload.expiresDate) === null || _d === void 0 ? void 0 : _d.toString(), web_order_line_item_id: verifiedPayload.webOrderLineItemId, is_trial_period: verifiedPayload.isTrialPeriod || "false", is_in_intro_offer_period: verifiedPayload.isInIntroOfferPeriod || "false" }] }, latest_receipt_info: [{ quantity: verifiedPayload.quantity || "1", product_id: verifiedPayload.productId, transaction_id: verifiedPayload.transactionId, original_transaction_id: verifiedPayload.originalTransactionId, purchase_date_ms: (_e = verifiedPayload.purchaseDate) === null || _e === void 0 ? void 0 : _e.toString(), original_purchase_date_ms: (_f = verifiedPayload.originalPurchaseDate) === null || _f === void 0 ? void 0 : _f.toString(), expires_date_ms: (_g = verifiedPayload.expiresDate) === null || _g === void 0 ? void 0 : _g.toString(), web_order_line_item_id: verifiedPayload.webOrderLineItemId, is_trial_period: verifiedPayload.isTrialPeriod || "false", is_in_intro_offer_period: verifiedPayload.isInIntroOfferPeriod || "false" }], pending_renewal_info: [], decoded_payload: verifiedPayload }; console.log("StoreKit2 verification successful:", result); return result; } catch (error) { console.error("StoreKit2 verification error:", error); if (error instanceof jwt.JsonWebTokenError) { throw new functions.https.HttpsError("invalid-argument", `Invalid JWT token: ${error.message}`); } throw new functions.https.HttpsError("internal", "Failed to verify StoreKit2 transaction."); } }); } //# sourceMappingURL=verify_ios.js.map