Repository navigation
Expand file tree
/
Copy pathclient-bridge.mjs
More file actions
70 lines (67 loc) · 2.43 KB
/
Copy pathclient-bridge.mjs
File metadata and controls
70 lines (67 loc) · 2.43 KB
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
import assert from "node:assert/strict";
import { operation, validate } from "./contract.mjs";
// Run on the app backend; the provider still authenticates the store evidence.
export function toVerifyPurchaseInput(purchase) {
const { store, purchaseToken } = purchase ?? {};
if (!["apple", "google"].includes(store))
throw new Error("This adapter supports Apple and Google purchases only");
if (typeof purchaseToken !== "string" || !purchaseToken.trim())
throw new Error("Purchase evidence is required");
const input = {
store,
...(store === "apple"
? { apple: { jws: purchaseToken } }
: { google: { purchaseToken } }),
};
if (!validate(operation("verifyPurchase").input, input))
throw new Error("Purchase evidence does not match the protocol input");
return input;
}
export function runBridgeDemo() {
const checks = [];
for (const store of ["apple", "google"]) {
const token = `fictional-${store}-evidence`;
const input = toVerifyPurchaseInput({
store,
purchaseToken: token,
productId: "premium.monthly",
userId: "untrusted-client-claim",
});
assert.deepEqual(input, {
store,
...(store === "apple"
? { apple: { jws: token } }
: { google: { purchaseToken: token } }),
});
checks.push(`${store}: maps evidence without forwarding client identity`);
assert(validate(operation("verifyPurchase").input, input));
checks.push(`${store}: matches the installed verification input schema`);
}
for (const [label, purchase] of [
["missing purchase", null],
["unknown store", { store: "unknown", purchaseToken: "fictional" }],
[
"Amazon needs its own adapter",
{ store: "amazon", purchaseToken: "fictional" },
],
[
"Horizon needs its own adapter",
{ store: "horizon", purchaseToken: "fictional" },
],
["missing evidence", { store: "apple" }],
["blank evidence", { store: "google", purchaseToken: " " }],
["non-string evidence", { store: "apple", purchaseToken: 123 }],
[
"oversized evidence",
{ store: "apple", purchaseToken: "x".repeat(16385) },
],
]) {
assert.throws(() => toVerifyPurchaseInput(purchase), Error);
checks.push(`Rejects ${label}`);
}
return checks;
}
if (import.meta.main)
console.log(
`Client boundary: ${runBridgeDemo().length} checks passed against the installed contract. Fixture fields only; no SDK checkout or store contacted.`,
);