Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 13x 13x 13x 13x 13x 13x 12x 12x 12x 1x 11x 11x 11x 11x 11x 11x 3x 3x 1x 2x 2x 1x 9x 9x 12x 9x 8x 1x 1x 7x 1x 1x 6x 1x 1x 5x 2x 2x 1x 1x 4x 17x 13x | /**
* Apple App Store StoreKit 2 receipt verification.
*
* iOS clients send `Transaction.jwsRepresentation` (a signed JWS payload)
* as the `purchaseToken`. Server-side we use the official Apple library
* `@apple/app-store-server-library` to verify the JWS signature against
* Apple's root CA certificates, decode the transaction, and validate
* bundleId / productId / revocationReason. Mirrors the role of
* `playStore.js` for Google Play.
*
* **Required environment variables (production):**
* - `APPLE_ROOT_CERTS_DIR` — directory containing Apple Root CA certs
* (`AppleRootCA-G3.cer`, `AppleRootCA-G2.cer`, plus the legacy
* `AppleIncRootCertificate.cer` and `AppleComputerRootCertificate.cer`).
* Download from {@link https://www.apple.com/certificateauthority/}.
* - `APPLE_APP_STORE_ENV` — `production` or `sandbox` (defaults to
* `sandbox` so a misconfigured prod deploy fails closed instead of
* accepting sandbox-signed transactions as real purchases).
* - `APPLE_APP_STORE_APP_ID` — numeric Apple App ID from App Store Connect
* (App Information → "Apple ID"). **Required when `APPLE_APP_STORE_ENV=production`** —
* Apple's `SignedDataVerifier` constructor throws without it in PRODUCTION
* mode, and the notification verifier needs it to bind the JWS to our
* specific app identity (defence-in-depth against cross-app payload reuse).
*
* Note: This module verifies signed transactions the client passes in.
* For server-side App Store Server Notifications (refund webhooks,
* subscription renewals), use `AppStoreServerAPIClient` separately —
* that path also needs `APPLE_APP_STORE_KEY_ID`, `APPLE_APP_STORE_ISSUER_ID`,
* `APPLE_APP_STORE_PRIVATE_KEY` (the P8 private key downloaded from
* App Store Connect → Users and Access → Integrations → In-App Purchase).
*/
const fs = require('node:fs');
const path = require('node:path');
const { SignedDataVerifier, Environment } = require('@apple/app-store-server-library');
const log = require('./log');
const BUNDLE_ID = 'com.shyden.shytalk';
let verifier = null;
function getVerifier() {
Iif (verifier) return verifier;
const certsDir = process.env.APPLE_ROOT_CERTS_DIR;
if (!certsDir) {
throw new Error(
'APPLE_ROOT_CERTS_DIR environment variable is required — set to a directory ' +
'containing the Apple Root CA certificates (downloadable from ' +
'https://www.apple.com/certificateauthority/). Defence-in-depth fail-closed ' +
'so a misconfigured prod deploy never accepts unverified Apple receipts.',
);
}
const certFiles = fs
.readdirSync(certsDir)
.filter((f) => f.endsWith('.cer') || f.endsWith('.pem'));
Iif (certFiles.length === 0) {
throw new Error(
`APPLE_ROOT_CERTS_DIR (${certsDir}) contains no .cer or .pem files — ` +
'cannot construct SignedDataVerifier without Apple root certs.',
);
}
const rootCerts = certFiles.map((f) => fs.readFileSync(path.join(certsDir, f)));
// Default to SANDBOX so a misconfigured prod environment that forgot the
// env var doesn't accept real-money purchases without verification —
// sandbox-signed JWS payloads will fail the signature check against
// production root certs.
const environment =
process.env.APPLE_APP_STORE_ENV === 'production' ? Environment.PRODUCTION : Environment.SANDBOX;
// Apple's SignedDataVerifier constructor THROWS if environment is
// PRODUCTION and appAppleId is undefined. The notification verifier
// also uses appAppleId to bind the JWS to our specific app identity.
// Sandbox doesn't require it (and passing undefined there is fine —
// the library validates the requirement only in PRODUCTION).
let appAppleId;
if (environment === Environment.PRODUCTION) {
const raw = process.env.APPLE_APP_STORE_APP_ID;
if (!raw) {
throw new Error(
'APPLE_APP_STORE_APP_ID environment variable is required when ' +
'APPLE_APP_STORE_ENV=production. Find it in App Store Connect → ' +
'App Information → "Apple ID" (numeric).',
);
}
appAppleId = Number(raw);
if (!Number.isFinite(appAppleId)) {
throw new Error(
`APPLE_APP_STORE_APP_ID must be numeric (got "${raw}"). Take the ` +
'value from App Store Connect → App Information → "Apple ID".',
);
}
}
// SignedDataVerifier(rootCerts, performOnlineRevocationChecking, environment, bundleId, appAppleId?)
verifier = new SignedDataVerifier(rootCerts, true, environment, BUNDLE_ID, appAppleId);
return verifier;
}
/**
* Verify an Apple StoreKit 2 signed transaction and return normalised data.
*
* @param {string} expectedProductId - Product ID the client claimed to purchase
* @param {string} signedTransactionInfo - JWS from `Transaction.jwsRepresentation`
* @param {boolean} isSubscription - Whether this is a subscription product
* @returns {Promise<{orderId: string, productId: string, purchaseDate: number, expiresDate?: number}>}
* @throws {Error} If JWS signature is invalid, productId/bundleId mismatch, transaction revoked, or subscription expired
*/
async function verifyApplePurchase(expectedProductId, signedTransactionInfo, isSubscription) {
const v = getVerifier();
const transaction = await v.verifyAndDecodeTransaction(signedTransactionInfo);
// The library already enforces bundleId at the SignedDataVerifier level, but
// re-check here so a future library version that loosens this contract still
// fails closed at our layer.
if (transaction.bundleId !== BUNDLE_ID) {
log.warn('appleStore', 'bundleId mismatch in verified transaction', {
expectedBundleId: BUNDLE_ID,
actualBundleId: transaction.bundleId,
productId: transaction.productId,
});
throw new Error(
`Apple transaction bundleId mismatch: expected ${BUNDLE_ID}, got ${transaction.bundleId}`,
);
}
if (transaction.productId !== expectedProductId) {
log.warn('appleStore', 'productId mismatch in verified transaction', {
expectedProductId,
actualProductId: transaction.productId,
orderId: transaction.transactionId,
});
throw new Error(
`Apple transaction productId mismatch: expected ${expectedProductId}, got ${transaction.productId}`,
);
}
// Reject refunds and family-share revokes — `revocationReason` is a number
// (1 = refund, 2 = family-share revoke, etc.) per Apple's docs. Both `null`
// and `undefined` mean "not revoked".
if (transaction.revocationReason !== undefined && transaction.revocationReason !== null) {
log.warn('appleStore', 'transaction revoked', {
productId: transaction.productId,
orderId: transaction.transactionId,
revocationReason: transaction.revocationReason,
});
throw new Error(
`Apple transaction revoked (reason=${transaction.revocationReason}, ` +
`orderId=${transaction.transactionId})`,
);
}
// For subscriptions, refuse expired tokens at validation time. The client
// shouldn't send expired tokens, but this is the server-side guarantee the
// grant logic relies on.
if (isSubscription && transaction.expiresDate) {
const now = Date.now();
if (transaction.expiresDate < now) {
log.warn('appleStore', 'subscription transaction expired', {
productId: transaction.productId,
orderId: transaction.transactionId,
expiresDate: transaction.expiresDate,
now,
});
throw new Error(`Apple subscription expired at ${transaction.expiresDate} (now=${now})`);
}
}
return {
orderId: transaction.transactionId,
productId: transaction.productId,
purchaseDate: transaction.purchaseDate,
expiresDate: transaction.expiresDate,
};
}
/**
* Verify an App Store Server Notifications V2 signed payload and return
* the decoded notification body. Used by the
* `/api/apple-notifications/v2` webhook to handle refunds, renewals,
* revokes, etc. Reuses the same `SignedDataVerifier` instance as
* `verifyApplePurchase`.
*/
async function verifyAppleNotification(signedPayload) {
return getVerifier().verifyAndDecodeNotification(signedPayload);
}
/**
* Verify a standalone signed transaction payload (e.g.
* `data.signedTransactionInfo` from an App Store Server Notification)
* and return the decoded transaction. Convenience wrapper for
* notification handlers that need the embedded transaction without
* the productId / bundleId / revocation enforcement that
* `verifyApplePurchase` runs (those are caller-controlled here).
*/
async function verifyAppleSignedTransaction(signedTransactionInfo) {
return getVerifier().verifyAndDecodeTransaction(signedTransactionInfo);
}
// Exposed for testing — allows resetting the cached verifier instance
function _resetVerifier() {
verifier = null;
}
module.exports = {
verifyApplePurchase,
verifyAppleNotification,
verifyAppleSignedTransaction,
_resetVerifier,
};
|