UNPKG

@mathrunet/masamune

Version:

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

195 lines 9.04 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 }); const functions = __importStar(require("firebase-functions/v2")); const verifier = __importStar(require("../lib/functions/verify_android")); const subscriber = __importStar(require("../lib/functions/update_subscription")); const firebase_loader_1 = require("../lib/src/firebase_loader"); /** * Verify subscriptions and add data. * * サブスクリプションの検証とデータの追加を行います。 * * @param process.env.PURCHASE_ANDROID_SERVICEACCOUNT_EMAIL * The email address of your Google service account. * Create an OAuth consent screen from the URL below. * https://console.cloud.google.com/apis/credentials/consent * It is then created from the service account. * https://console.cloud.google.com/iam-admin/serviceaccounts * * Googleのサービスアカウントのメールアドレス。 * 下記のURLからOAuthの同意画面を作成します。 * https://console.cloud.google.com/apis/credentials/consent * その後、サービスアカウントから作成します。 * https://console.cloud.google.com/iam-admin/serviceaccounts * * @param process.env.PURCHASE_ANDROID_SERVICEACCOUNT_PRIVATE_KEY * A private key for your Google service account. * Create an OAuth consent screen from the URL below. * https://console.cloud.google.com/apis/credentials/consent * It is then created from the service account. * https://console.cloud.google.com/iam-admin/serviceaccounts * After creating a service account, create a key in Json format from the Key tab. * The private key is described there. * * Googleのサービスアカウントのプライベートキー。 * 下記のURLからOAuthの同意画面を作成します。 * https://console.cloud.google.com/apis/credentials/consent * その後、サービスアカウントから作成します。 * https://console.cloud.google.com/iam-admin/serviceaccounts * サービスアカウント作成後、キーのタブからJson形式でキーを作成します。 * プライベートキーはそこに記述されています。 * * @param process.env.PURCHASE_SUBSCRIPTIONPATH * Describes the path to the collection of subscriptions. * * サブスクリプションのコレクションのパスを記述します。 * * @param purchaseId * Subscription purchase ID. * * サブスクリプションの購入ID。 * * @param packageName * Application package name. * * アプリケーションのパッケージ名。 * * @param productId * Item ID issued by Google Play. * * GooglePlayで発行されたアイテムID。 * * @param purchaseToken * The purchase token issued at the time of purchase. * * 購入したときに発行された購入トークン。 * * @param data * Document data to be updated. * * 更新するドキュメントデータ。 * * @param userId * ID of the user who purchased the subscription. * * サブスクリプションを購入したユーザーのID。 */ module.exports = (regions, options, data) => { var _a, _b, _c, _d; return functions.https.onCall({ region: (_a = options.region) !== null && _a !== void 0 ? _a : regions, timeoutSeconds: options.timeoutSeconds, memory: options.memory, minInstances: options.minInstances, concurrency: options.concurrency, maxInstances: options.maxInstances, serviceAccount: (_b = options === null || options === void 0 ? void 0 : options.serviceAccount) !== null && _b !== void 0 ? _b : undefined, enforceAppCheck: (_c = options.enforceAppCheck) !== null && _c !== void 0 ? _c : undefined, consumeAppCheckToken: (_d = options.consumeAppCheckToken) !== null && _d !== void 0 ? _d : undefined, }, (query) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d; try { /* ==== Android検証ここから ==== */ if (!query.data.userId) { throw new functions.https.HttpsError("invalid-argument", "User is empty."); } const res = yield verifier.verifyAndroid({ type: "subscriptions", serviceAccountEmail: (_a = process.env.PURCHASE_ANDROID_SERVICEACCOUNT_EMAIL) !== null && _a !== void 0 ? _a : "", serviceAccountPrivateKey: (_b = process.env.PURCHASE_ANDROID_SERVICEACCOUNT_PRIVATE_KEY) !== null && _b !== void 0 ? _b : "", packageName: query.data.packageName, productId: query.data.productId, purchaseToken: query.data.purchaseToken, }); const time = new Date().getTime(); const startTimeMillis = parseInt(res["startTimeMillis"]); const expiryTimeMillis = parseInt(res["expiryTimeMillis"]); if (res === null || isNaN(startTimeMillis) || isNaN(expiryTimeMillis) || startTimeMillis <= 0) { throw new functions.https.HttpsError("not-found", "Illegal receipt."); } if (expiryTimeMillis <= time) { res["expired"] = true; } /* ==== ここまでAndroid検証 ==== */ /* ==== Firestoreの更新ここから ==== */ let error = null; const firestoreDatabaseIds = (_c = options.firestoreDatabaseIds) !== null && _c !== void 0 ? _c : [""]; for (const databaseId of firestoreDatabaseIds) { try { const firestoreInstance = (0, firebase_loader_1.firestoreLoader)(databaseId); yield subscriber.updateSubscription({ targetCollectionPath: (_d = query.data.path) !== null && _d !== void 0 ? _d : process.env.PURCHASE_SUBSCRIPTIONPATH, targetDocumentId: query.data.purchaseToken, data: res, additionalData: query.data, userId: query.data.userId, platform: "Android", orderId: res["orderId"], productId: query.data.productId, purchaseId: query.data.purchaseId, packageName: query.data.packageName, token: query.data.purchaseToken, expiryDate: expiryTimeMillis, firestoreInstance: firestoreInstance, }); } catch (err) { error = err; } } if (error) { console.error(error); throw new functions.https.HttpsError("unknown", "Unknown error."); } /* ==== ここまでFirestoreの更新 ==== */ return res; } catch (err) { console.error(err); throw new functions.https.HttpsError("unknown", "Unknown error."); } })); }; //# sourceMappingURL=subscription_verify_android.js.map