@fantastic.dev/better-stripe 0.111.0 → 0.112.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts +564 -0
- package/dist/client/billing/renewalAttach/internal/observation/freshActor.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/freshActor.js +103 -21
- package/dist/client/billing/renewalAttach/internal/observation/freshActor.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts +18 -1
- package/dist/client/billing/renewalAttach/internal/observation/freshTransport.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js +54 -1
- package/dist/client/billing/renewalAttach/internal/observation/freshTransport.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts +4 -0
- package/dist/client/billing/renewalAttach/internal/observation/invocation.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/invocation.js +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/invocation.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts +4 -0
- package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js +6 -1
- package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.d.ts +38 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.js +172 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessAuthorization.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessData.d.ts +135 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessData.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessData.js +143 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessData.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.d.ts +85 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.js +121 -0
- package/dist/client/billing/renewalAttach/internal/observation/witnessOperation.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.d.ts +44 -0
- package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.js +4 -0
- package/dist/client/billing/renewalAttach/internal/operations/witnessProfiles.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/configureJournalWork.d.ts +1 -1
- package/dist/client/billing/renewalAttach/internal/producer/configureLineage.d.ts +21 -21
- package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts +3 -3
- package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/component/_generated/api.d.ts +24 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +94 -10
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
- package/dist/component/balanceFunding/queries.d.ts +1 -1
- package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
- package/dist/component/renewalWitnesses/mutations.d.ts +45 -0
- package/dist/component/renewalWitnesses/mutations.d.ts.map +1 -0
- package/dist/component/renewalWitnesses/mutations.js +14 -0
- package/dist/component/renewalWitnesses/mutations.js.map +1 -0
- package/dist/component/renewalWitnesses/queries.d.ts +39 -0
- package/dist/component/renewalWitnesses/queries.d.ts.map +1 -0
- package/dist/component/renewalWitnesses/queries.js +11 -0
- package/dist/component/renewalWitnesses/queries.js.map +1 -0
- package/dist/component/usage/allowance.fixtures.d.ts +16 -16
- package/docs/renewal-witness-protocol.md +61 -10
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/internal/observation/freshActor.ts +142 -19
- package/src/client/billing/renewalAttach/internal/observation/freshTransport.ts +90 -1
- package/src/client/billing/renewalAttach/internal/observation/invocation.ts +2 -0
- package/src/client/billing/renewalAttach/internal/observation/readerAuthorization.ts +6 -1
- package/src/client/billing/renewalAttach/internal/observation/witnessAcquisition.integration.test.ts +348 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessActor.integration.test.ts +204 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessAuthorization.ts +239 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessCapabilities.integration.test.ts +231 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessData.ts +189 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessHistorical.integration.test.ts +98 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessImmutable.integration.test.ts +78 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessInvoice.integration.test.ts +85 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessLifecycle.integration.test.ts +264 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessOperation.ts +205 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessProfile.integration.test.ts +93 -0
- package/src/client/billing/renewalAttach/internal/observation/witnessRPCControls.integration.test.ts +256 -0
- package/src/client/billing/renewalAttach/internal/operations/witnessProfiles.ts +58 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/invoiceObservation.ts +3 -1
- package/src/client/billing/renewalAttach/internal/test-fixtures/producerConfigureAdmission.ts +8 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +18 -7
- package/src/client/billing/renewalAttach/internal/test-fixtures/witnessHistory.ts +70 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/witnessObservation.ts +353 -0
- package/src/component/_generated/api.ts +24 -0
- package/src/component/_generated/component.ts +98 -10
- package/src/component/renewalWitnesses/mutations.ts +17 -0
- package/src/component/renewalWitnesses/queries.ts +11 -0
- package/src/component/renewalWitnesses/routes.test.ts +154 -0
package/src/client/billing/renewalAttach/internal/observation/witnessActor.integration.test.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import {
|
|
3
|
+
witnessControls,
|
|
4
|
+
prepareWitnessObservation,
|
|
5
|
+
authorizeWitnessRetention,
|
|
6
|
+
authorizeWitnessRead,
|
|
7
|
+
authorizeWitnessObservation,
|
|
8
|
+
} from "../test-fixtures/witnessObservation.js";
|
|
9
|
+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
|
10
|
+
import * as actors from "./freshActor.js";
|
|
11
|
+
import { components } from "../../../../../../example/convex/_generated/api.js";
|
|
12
|
+
import { configureAdmissionCase } from "../test-fixtures/producerConfigureAdmission.js";
|
|
13
|
+
import { observerIdentity } from "../test-fixtures/observationReader.js";
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
witnessControls.captures.splice(0);
|
|
17
|
+
witnessControls.readers.splice(0);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => vi.restoreAllMocks());
|
|
21
|
+
|
|
22
|
+
it.each([
|
|
23
|
+
"captureFreshWitnessRenewalObservation",
|
|
24
|
+
"captureHistoricalWitnessRenewalObservation",
|
|
25
|
+
])(
|
|
26
|
+
"EMPTY witness profile refuses %s before reflection or clocks",
|
|
27
|
+
async (name) => {
|
|
28
|
+
let traps = 0;
|
|
29
|
+
const poison = new Proxy(
|
|
30
|
+
{},
|
|
31
|
+
{
|
|
32
|
+
get() {
|
|
33
|
+
traps++;
|
|
34
|
+
throw Error("UNEXPECTED_INPUT_REFLECTION");
|
|
35
|
+
},
|
|
36
|
+
ownKeys() {
|
|
37
|
+
traps++;
|
|
38
|
+
throw Error("UNEXPECTED_INPUT_REFLECTION");
|
|
39
|
+
},
|
|
40
|
+
getPrototypeOf() {
|
|
41
|
+
traps++;
|
|
42
|
+
throw Error("UNEXPECTED_ROOT_REFLECTION");
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
const wall = vi.spyOn(Date, "now"),
|
|
47
|
+
ns = vi.spyOn(process.hrtime, "bigint");
|
|
48
|
+
const capture = Reflect.get(actors, name);
|
|
49
|
+
expect(typeof capture).toBe("function");
|
|
50
|
+
await expect(
|
|
51
|
+
Reflect.apply(capture, actors, [poison, poison]),
|
|
52
|
+
).rejects.toThrow("RENEWAL_WITNESS_UNBOUND");
|
|
53
|
+
expect(traps).toBe(0);
|
|
54
|
+
expect(wall).not.toHaveBeenCalled();
|
|
55
|
+
expect(ns).not.toHaveBeenCalled();
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
for (const retained of [false, true])
|
|
60
|
+
configureAdmissionCase(
|
|
61
|
+
"retains actual fresh native reads and later acquires historical material with captured C9 " +
|
|
62
|
+
retained,
|
|
63
|
+
(h) => prepareWitnessObservation(h, retained),
|
|
64
|
+
async (h, state) => {
|
|
65
|
+
let acknowledgement: Record<string, unknown> | null = null;
|
|
66
|
+
const capture = Reflect.get(
|
|
67
|
+
actors,
|
|
68
|
+
"captureFreshWitnessRenewalObservation",
|
|
69
|
+
);
|
|
70
|
+
const read = Reflect.get(
|
|
71
|
+
actors,
|
|
72
|
+
"captureHistoricalWitnessRenewalObservation",
|
|
73
|
+
);
|
|
74
|
+
expect(typeof capture).toBe("function");
|
|
75
|
+
expect(typeof read).toBe("function");
|
|
76
|
+
const before = h.calls.length;
|
|
77
|
+
let originalPayloadJSON = "";
|
|
78
|
+
await h.inAnotherAction(async (ctx) => {
|
|
79
|
+
const actor = await Reflect.apply(capture, actors, [
|
|
80
|
+
h.sdk,
|
|
81
|
+
{
|
|
82
|
+
actualActionCtx: ctx,
|
|
83
|
+
parentOperationId: state.parentOperationId,
|
|
84
|
+
operationId: state.operationId,
|
|
85
|
+
witnessId: state.scope.identity.witnessId,
|
|
86
|
+
},
|
|
87
|
+
]);
|
|
88
|
+
const output = await actor.invoke();
|
|
89
|
+
assert(output.evidence);
|
|
90
|
+
expect(output.evidence.currentMaterial.predicate).toBe(
|
|
91
|
+
"current_target_material_match",
|
|
92
|
+
);
|
|
93
|
+
assert(
|
|
94
|
+
"witnessRetention" in output &&
|
|
95
|
+
output.witnessRetention?.status === "retained",
|
|
96
|
+
);
|
|
97
|
+
expect(output.witnessRetention.status).toBe("retained");
|
|
98
|
+
expect(output.witnessRetention.descriptor.identity).toEqual(
|
|
99
|
+
state.scope.identity,
|
|
100
|
+
);
|
|
101
|
+
expect(output.witnessRetention.descriptor.nativePeriod).toEqual(
|
|
102
|
+
state.scope.nativePeriod,
|
|
103
|
+
);
|
|
104
|
+
expect(
|
|
105
|
+
output.witnessRetention.descriptor.publicDeliveryEstablished,
|
|
106
|
+
).toBe(false);
|
|
107
|
+
acknowledgement = output.witnessRetention;
|
|
108
|
+
const stored = await ctx.runQuery(
|
|
109
|
+
components.betterStripe.renewalWitnesses.queries
|
|
110
|
+
.readAuthorizedWitness,
|
|
111
|
+
{ identity: state.scope.identity },
|
|
112
|
+
);
|
|
113
|
+
assert(stored);
|
|
114
|
+
originalPayloadJSON = stored.payloadJSON;
|
|
115
|
+
const payload = JSON.parse(stored.payloadJSON);
|
|
116
|
+
const expectedStage = (
|
|
117
|
+
stage: typeof state.full.parent,
|
|
118
|
+
parent: boolean,
|
|
119
|
+
) => {
|
|
120
|
+
const { recordJSON: _recordJSON, ...reservation } =
|
|
121
|
+
stage.attempt.reservation;
|
|
122
|
+
void _recordJSON;
|
|
123
|
+
return {
|
|
124
|
+
record: JSON.parse(stage.operation.recordJSON),
|
|
125
|
+
reservation,
|
|
126
|
+
acknowledgement:
|
|
127
|
+
stage.acknowledgement === null
|
|
128
|
+
? null
|
|
129
|
+
: {
|
|
130
|
+
retention: stage.acknowledgement.retention,
|
|
131
|
+
receipt: JSON.parse(stage.acknowledgement.receiptJSON),
|
|
132
|
+
},
|
|
133
|
+
...(parent
|
|
134
|
+
? { initialBasis: Reflect.get(stage.operation, "initialBasis") }
|
|
135
|
+
: {}),
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
expect(payload.source).toEqual({
|
|
139
|
+
parent: expectedStage(state.full.parent, true),
|
|
140
|
+
configure: expectedStage(state.full.configure, false),
|
|
141
|
+
});
|
|
142
|
+
expect(payload.reads).toEqual({
|
|
143
|
+
...state.reads,
|
|
144
|
+
coupons: h.source.originalBundle.couponReads.map(
|
|
145
|
+
(row) => row.wireCoupon,
|
|
146
|
+
),
|
|
147
|
+
});
|
|
148
|
+
expect(payload.invoiceContext).toBeNull();
|
|
149
|
+
expect(payload.capturedSubject).toBe(observerIdentity.tokenIdentifier);
|
|
150
|
+
expect(payload.acquisition).toEqual({
|
|
151
|
+
observedAtMs: Date.now(),
|
|
152
|
+
elapsedWallMs: 0,
|
|
153
|
+
elapsedNsDecimal: "0",
|
|
154
|
+
apiVersion: "2026-09-30.endive",
|
|
155
|
+
baseReadProfile: "endive-schedule-first-44-v1",
|
|
156
|
+
invoiceReadProfile: null,
|
|
157
|
+
});
|
|
158
|
+
await expect(actor.invoke()).rejects.toThrow();
|
|
159
|
+
});
|
|
160
|
+
expect(witnessControls.retainQueries).toBe(2);
|
|
161
|
+
expect(h.calls.slice(before).map((c) => c.method)).toEqual(
|
|
162
|
+
Array(6).fill("GET"),
|
|
163
|
+
);
|
|
164
|
+
state.reads.subscription.cancel_at_period_end = true;
|
|
165
|
+
await h.inAnotherAction(async (ctx) => {
|
|
166
|
+
const actor = await Reflect.apply(read, actors, [
|
|
167
|
+
h.sdk,
|
|
168
|
+
{
|
|
169
|
+
actualActionCtx: ctx,
|
|
170
|
+
parentOperationId: state.parentOperationId,
|
|
171
|
+
operationId: state.operationId,
|
|
172
|
+
witnessId: state.scope.identity.witnessId,
|
|
173
|
+
},
|
|
174
|
+
]);
|
|
175
|
+
const output = await actor.invoke();
|
|
176
|
+
assert(output.evidence);
|
|
177
|
+
expect(output.evidence.currentMaterial.predicate).toBe("conflict");
|
|
178
|
+
expect(output.evidence.historicalMaterial.predicate).toBe(
|
|
179
|
+
"snapshot_target_material_match",
|
|
180
|
+
);
|
|
181
|
+
assert("witness" in output);
|
|
182
|
+
expect(output.witness.descriptor).toEqual(
|
|
183
|
+
acknowledgement && Reflect.get(acknowledgement, "descriptor"),
|
|
184
|
+
);
|
|
185
|
+
expect(output.operationalAuthority).toBe(false);
|
|
186
|
+
const stored = await ctx.runQuery(
|
|
187
|
+
components.betterStripe.renewalWitnesses.queries
|
|
188
|
+
.readAuthorizedWitness,
|
|
189
|
+
{ identity: state.scope.identity },
|
|
190
|
+
);
|
|
191
|
+
expect(stored?.payloadJSON).toBe(originalPayloadJSON);
|
|
192
|
+
await expect(actor.invoke()).rejects.toThrow();
|
|
193
|
+
});
|
|
194
|
+
expect(witnessControls.readQueries).toBe(2);
|
|
195
|
+
expect(h.calls.slice(before).every((c) => c.method === "GET")).toBe(true);
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
identity: observerIdentity,
|
|
199
|
+
hostQuery: authorizeWitnessObservation,
|
|
200
|
+
targetInterval: "week",
|
|
201
|
+
hostWitnessRetainQuery: authorizeWitnessRetention,
|
|
202
|
+
hostWitnessReadQuery: authorizeWitnessRead,
|
|
203
|
+
},
|
|
204
|
+
);
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { types } from "node:util";
|
|
2
|
+
import { getFunctionAddress } from "convex/server";
|
|
3
|
+
import {
|
|
4
|
+
COMPILED_WITNESS_CAPTURE_ENTRIES,
|
|
5
|
+
COMPILED_WITNESS_READ_ENTRIES,
|
|
6
|
+
type WitnessCaptureProfile,
|
|
7
|
+
type WitnessReadProfile,
|
|
8
|
+
type WitnessScope,
|
|
9
|
+
} from "../operations/witnessProfiles.js";
|
|
10
|
+
import {
|
|
11
|
+
COMPILED_FRESH_OBSERVATION_ENTRIES,
|
|
12
|
+
COMPILED_OBSERVATION_HISTORY_ENTRIES,
|
|
13
|
+
} from "../operations/sourceOperations.js";
|
|
14
|
+
import {
|
|
15
|
+
readMaintainedRootComponent,
|
|
16
|
+
readCheckedJournalActionRPC,
|
|
17
|
+
} from "../producer/classOwnedActionRoot.js";
|
|
18
|
+
import { invoiceProfileGuard } from "./invoiceAuthorization.js";
|
|
19
|
+
import { closed, freeze } from "../c10/ownData.js";
|
|
20
|
+
import { matchConfigureDATA } from "../producer/configureJournalData.js";
|
|
21
|
+
import {
|
|
22
|
+
captureReaderQueryDATA,
|
|
23
|
+
readerText as text,
|
|
24
|
+
} from "./readerAuthorization.js";
|
|
25
|
+
import type { ObservationInvocationState } from "./invocation.js";
|
|
26
|
+
import {
|
|
27
|
+
witnessIdentity,
|
|
28
|
+
witnessNeed as need,
|
|
29
|
+
type WitnessIdentity,
|
|
30
|
+
} from "../../../../../shared/renewalWitness.js";
|
|
31
|
+
|
|
32
|
+
export type WitnessMode = "retain" | "read";
|
|
33
|
+
export type WitnessProfile = WitnessCaptureProfile | WitnessReadProfile;
|
|
34
|
+
const nativeThen = Promise.prototype.then;
|
|
35
|
+
export const witnessAddress = (ref: Parameters<typeof getFunctionAddress>[0]) =>
|
|
36
|
+
JSON.stringify(getFunctionAddress(ref));
|
|
37
|
+
export function witnessProfileGuard(
|
|
38
|
+
profile: WitnessProfile,
|
|
39
|
+
mode: WitnessMode,
|
|
40
|
+
) {
|
|
41
|
+
const entries: readonly WitnessProfile[] =
|
|
42
|
+
mode === "retain"
|
|
43
|
+
? COMPILED_WITNESS_CAPTURE_ENTRIES
|
|
44
|
+
: COMPILED_WITNESS_READ_ENTRIES;
|
|
45
|
+
need(!types.isProxy(profile));
|
|
46
|
+
closed(profile, ["freshProfile", "invoiceProfile", "authorizationQuery"]);
|
|
47
|
+
need(Object.isFrozen(profile) && entries.includes(profile));
|
|
48
|
+
const fresh = profile.freshProfile;
|
|
49
|
+
need(
|
|
50
|
+
COMPILED_FRESH_OBSERVATION_ENTRIES.includes(fresh) &&
|
|
51
|
+
COMPILED_OBSERVATION_HISTORY_ENTRIES.includes(fresh.historyEntry),
|
|
52
|
+
);
|
|
53
|
+
need(
|
|
54
|
+
entries.filter(
|
|
55
|
+
(entry) =>
|
|
56
|
+
entry.freshProfile.historyEntry.component ===
|
|
57
|
+
fresh.historyEntry.component,
|
|
58
|
+
).length === 1,
|
|
59
|
+
);
|
|
60
|
+
if (profile.invoiceProfile !== null) {
|
|
61
|
+
invoiceProfileGuard(profile.invoiceProfile);
|
|
62
|
+
need(profile.invoiceProfile.freshProfile === fresh);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** The EMPTY check precedes root/input reflection, clocks and SDK selection. */
|
|
66
|
+
export function captureWitnessProfile(
|
|
67
|
+
root: object,
|
|
68
|
+
mode: WitnessMode,
|
|
69
|
+
): WitnessProfile {
|
|
70
|
+
const entries: readonly WitnessProfile[] =
|
|
71
|
+
mode === "retain"
|
|
72
|
+
? COMPILED_WITNESS_CAPTURE_ENTRIES
|
|
73
|
+
: COMPILED_WITNESS_READ_ENTRIES;
|
|
74
|
+
if (entries.length === 0) throw Error("RENEWAL_WITNESS_UNBOUND");
|
|
75
|
+
const component = readMaintainedRootComponent(root);
|
|
76
|
+
const matches = entries.filter((profile) => {
|
|
77
|
+
need(!types.isProxy(profile));
|
|
78
|
+
closed(profile, ["freshProfile", "invoiceProfile", "authorizationQuery"]);
|
|
79
|
+
return profile.freshProfile.historyEntry.component === component;
|
|
80
|
+
});
|
|
81
|
+
need(matches.length === 1);
|
|
82
|
+
witnessProfileGuard(matches[0], mode);
|
|
83
|
+
return matches[0];
|
|
84
|
+
}
|
|
85
|
+
/** Brand and settle the original RPC Promise without adopting a public .then substitute. */
|
|
86
|
+
export function witnessRPC<T>(
|
|
87
|
+
state: ObservationInvocationState,
|
|
88
|
+
work: () => Promise<T>,
|
|
89
|
+
guard: () => void,
|
|
90
|
+
): Promise<T> {
|
|
91
|
+
return state.owned(() => {
|
|
92
|
+
const pending = work();
|
|
93
|
+
need(types.isPromise(pending));
|
|
94
|
+
return new Promise<T>((resolve, reject) => {
|
|
95
|
+
Reflect.apply(nativeThen, pending, [resolve, reject]);
|
|
96
|
+
});
|
|
97
|
+
}, guard);
|
|
98
|
+
}
|
|
99
|
+
function checkedScope(raw: unknown): WitnessScope {
|
|
100
|
+
const scope = closed(raw, [
|
|
101
|
+
"identity",
|
|
102
|
+
"providerScope",
|
|
103
|
+
"configureRequestBinding",
|
|
104
|
+
"observedItemId",
|
|
105
|
+
"nativePeriod",
|
|
106
|
+
]);
|
|
107
|
+
const provider = closed(scope.providerScope, [
|
|
108
|
+
"mode",
|
|
109
|
+
"stripeAccount",
|
|
110
|
+
"livemode",
|
|
111
|
+
"customerAccountId",
|
|
112
|
+
"customerId",
|
|
113
|
+
"subscriptionId",
|
|
114
|
+
"scheduleId",
|
|
115
|
+
]);
|
|
116
|
+
need(
|
|
117
|
+
provider.mode === "platform" &&
|
|
118
|
+
provider.stripeAccount === null &&
|
|
119
|
+
typeof provider.livemode === "boolean",
|
|
120
|
+
);
|
|
121
|
+
const period = closed(scope.nativePeriod, ["start", "end"]);
|
|
122
|
+
need(
|
|
123
|
+
typeof period.start === "number" &&
|
|
124
|
+
Number.isSafeInteger(period.start) &&
|
|
125
|
+
period.start >= 0 &&
|
|
126
|
+
typeof period.end === "number" &&
|
|
127
|
+
Number.isSafeInteger(period.end) &&
|
|
128
|
+
period.end > period.start,
|
|
129
|
+
);
|
|
130
|
+
return freeze({
|
|
131
|
+
identity: witnessIdentity(scope.identity),
|
|
132
|
+
providerScope: {
|
|
133
|
+
mode: "platform",
|
|
134
|
+
stripeAccount: null,
|
|
135
|
+
livemode: provider.livemode,
|
|
136
|
+
customerAccountId: text(provider.customerAccountId),
|
|
137
|
+
customerId: text(provider.customerId),
|
|
138
|
+
subscriptionId: text(provider.subscriptionId),
|
|
139
|
+
scheduleId: text(provider.scheduleId),
|
|
140
|
+
},
|
|
141
|
+
configureRequestBinding: text(scope.configureRequestBinding),
|
|
142
|
+
observedItemId: text(scope.observedItemId),
|
|
143
|
+
nativePeriod: { start: period.start, end: period.end },
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
export function createWitnessAuthorization(
|
|
147
|
+
state: ObservationInvocationState,
|
|
148
|
+
profile: WitnessProfile,
|
|
149
|
+
mode: WitnessMode,
|
|
150
|
+
identity: WitnessIdentity,
|
|
151
|
+
expectedScope: WitnessScope | null,
|
|
152
|
+
) {
|
|
153
|
+
const rpc = readCheckedJournalActionRPC(state.binding),
|
|
154
|
+
query = profile.authorizationQuery;
|
|
155
|
+
const address = witnessAddress(query),
|
|
156
|
+
base = state.readCaptured().authorization;
|
|
157
|
+
const args =
|
|
158
|
+
mode === "retain"
|
|
159
|
+
? {
|
|
160
|
+
capability: "renewal_witness_retain_v1" as const,
|
|
161
|
+
scope: expectedScope!,
|
|
162
|
+
}
|
|
163
|
+
: { capability: "renewal_witness_read_v1" as const, identity };
|
|
164
|
+
need(mode !== "retain" || expectedScope);
|
|
165
|
+
let grant: Record<string, unknown> | null = null;
|
|
166
|
+
let expiry: number | null = null;
|
|
167
|
+
function guard() {
|
|
168
|
+
state.guard();
|
|
169
|
+
witnessProfileGuard(profile, mode);
|
|
170
|
+
need(
|
|
171
|
+
profile.authorizationQuery === query &&
|
|
172
|
+
witnessAddress(query) === address &&
|
|
173
|
+
profile.freshProfile.historyEntry === state.historyEntry &&
|
|
174
|
+
readCheckedJournalActionRPC(state.binding) === rpc,
|
|
175
|
+
);
|
|
176
|
+
if (expiry !== null) need(state.check().wallMs < expiry);
|
|
177
|
+
}
|
|
178
|
+
return Object.freeze({
|
|
179
|
+
guard,
|
|
180
|
+
async authorize() {
|
|
181
|
+
guard();
|
|
182
|
+
await state.authorizeUnchanged();
|
|
183
|
+
guard();
|
|
184
|
+
const raw: unknown = await witnessRPC(
|
|
185
|
+
state,
|
|
186
|
+
() => rpc.runQuery.call(rpc.receiver, query, args),
|
|
187
|
+
guard,
|
|
188
|
+
);
|
|
189
|
+
const reply = captureReaderQueryDATA(raw);
|
|
190
|
+
need(new TextEncoder().encode(JSON.stringify(reply)).length <= 8192);
|
|
191
|
+
const data = closed(reply, [
|
|
192
|
+
"capability",
|
|
193
|
+
"scope",
|
|
194
|
+
"tokenIdentifier",
|
|
195
|
+
"authorizationId",
|
|
196
|
+
"authorizationRevision",
|
|
197
|
+
"expiresAtMs",
|
|
198
|
+
...(mode === "read" ? ["witnessRowId", "payloadBinding"] : []),
|
|
199
|
+
]);
|
|
200
|
+
const scope = checkedScope(data.scope);
|
|
201
|
+
need(
|
|
202
|
+
data.capability === args.capability &&
|
|
203
|
+
data.tokenIdentifier === base.tokenIdentifier,
|
|
204
|
+
);
|
|
205
|
+
matchConfigureDATA(scope.identity, identity);
|
|
206
|
+
if (expectedScope) matchConfigureDATA(scope, expectedScope);
|
|
207
|
+
text(data.authorizationId);
|
|
208
|
+
need(
|
|
209
|
+
typeof data.authorizationRevision === "number" &&
|
|
210
|
+
Number.isSafeInteger(data.authorizationRevision) &&
|
|
211
|
+
data.authorizationRevision > 0,
|
|
212
|
+
);
|
|
213
|
+
need(
|
|
214
|
+
typeof data.expiresAtMs === "number" &&
|
|
215
|
+
Number.isSafeInteger(data.expiresAtMs) &&
|
|
216
|
+
data.expiresAtMs > state.check().wallMs,
|
|
217
|
+
);
|
|
218
|
+
if (mode === "read") {
|
|
219
|
+
text(data.witnessRowId);
|
|
220
|
+
need(
|
|
221
|
+
/^renewal-witness:v1:sha256:[0-9a-f]{64}$/.test(
|
|
222
|
+
text(data.payloadBinding),
|
|
223
|
+
),
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
if (grant) matchConfigureDATA(data, grant);
|
|
227
|
+
else {
|
|
228
|
+
grant = freeze(data);
|
|
229
|
+
expiry = data.expiresAtMs;
|
|
230
|
+
}
|
|
231
|
+
guard();
|
|
232
|
+
return freeze({
|
|
233
|
+
scope,
|
|
234
|
+
witnessRowId: mode === "read" ? text(data.witnessRowId) : null,
|
|
235
|
+
payloadBinding: mode === "read" ? text(data.payloadBinding) : null,
|
|
236
|
+
});
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import {
|
|
2
|
+
witnessControls,
|
|
3
|
+
prepareWitnessObservation,
|
|
4
|
+
invokeWitnessCapture,
|
|
5
|
+
invokeWitnessRead,
|
|
6
|
+
readStoredWitness,
|
|
7
|
+
witnessFixtureOptions,
|
|
8
|
+
} from "../test-fixtures/witnessObservation.js";
|
|
9
|
+
import { configureAdmissionCase } from "../test-fixtures/producerConfigureAdmission.js";
|
|
10
|
+
import { expect } from "vitest";
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
function data(raw: unknown) {
|
|
13
|
+
assert(raw !== null && typeof raw === "object");
|
|
14
|
+
return raw;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
configureAdmissionCase(
|
|
18
|
+
"a separately authorized later subject reads immutable capture without becoming its captured subject",
|
|
19
|
+
prepareWitnessObservation,
|
|
20
|
+
async (h, state) => {
|
|
21
|
+
await invokeWitnessCapture(h, state);
|
|
22
|
+
const original = await readStoredWitness(h, state);
|
|
23
|
+
assert(original);
|
|
24
|
+
const reader = {
|
|
25
|
+
subject: "historical-reader",
|
|
26
|
+
issuer: "https://host.test",
|
|
27
|
+
tokenIdentifier: "https://host.test|historical-reader",
|
|
28
|
+
};
|
|
29
|
+
witnessControls.allowedReaders.push(reader.tokenIdentifier);
|
|
30
|
+
const output = await invokeWitnessRead(h, state, { identity: reader });
|
|
31
|
+
assert("witness" in output && output.evidence);
|
|
32
|
+
expect(output.authorization.tokenIdentifier).toBe(reader.tokenIdentifier);
|
|
33
|
+
expect(output.witness.descriptor.capturedSubject).toBe(
|
|
34
|
+
witnessFixtureOptions.identity.tokenIdentifier,
|
|
35
|
+
);
|
|
36
|
+
expect(output.evidence.historicalMaterial.predicate).toBe(
|
|
37
|
+
"snapshot_target_material_match",
|
|
38
|
+
);
|
|
39
|
+
expect((await readStoredWitness(h, state))?.payloadJSON).toBe(
|
|
40
|
+
original.payloadJSON,
|
|
41
|
+
);
|
|
42
|
+
const before = h.calls.length;
|
|
43
|
+
await expect(
|
|
44
|
+
invokeWitnessRead(h, state, {
|
|
45
|
+
identity: {
|
|
46
|
+
...reader,
|
|
47
|
+
subject: "unauthorized",
|
|
48
|
+
tokenIdentifier: "https://host.test|unauthorized",
|
|
49
|
+
},
|
|
50
|
+
}),
|
|
51
|
+
).rejects.toThrow();
|
|
52
|
+
expect(h.calls).toHaveLength(before);
|
|
53
|
+
},
|
|
54
|
+
witnessFixtureOptions,
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
for (const path of [
|
|
58
|
+
["identity", "componentScope"],
|
|
59
|
+
["identity", "parentOperationId"],
|
|
60
|
+
["identity", "operationId"],
|
|
61
|
+
["identity", "witnessId"],
|
|
62
|
+
["providerScope", "customerId"],
|
|
63
|
+
["providerScope", "customerAccountId"],
|
|
64
|
+
["providerScope", "subscriptionId"],
|
|
65
|
+
["providerScope", "scheduleId"],
|
|
66
|
+
["configureRequestBinding"],
|
|
67
|
+
["observedItemId"],
|
|
68
|
+
] as const) {
|
|
69
|
+
configureAdmissionCase(
|
|
70
|
+
"retention refuses a host grant with foreign " + path.join("."),
|
|
71
|
+
prepareWitnessObservation,
|
|
72
|
+
async (h, state) => {
|
|
73
|
+
witnessControls.onRetainGrant = (grant) => {
|
|
74
|
+
const scope = data(Reflect.get(grant, "scope"));
|
|
75
|
+
const target =
|
|
76
|
+
path.length === 1 ? scope : data(Reflect.get(scope, path[0]));
|
|
77
|
+
Reflect.set(target, path[path.length - 1], "controlled-foreign-value");
|
|
78
|
+
};
|
|
79
|
+
await expect(invokeWitnessCapture(h, state)).rejects.toThrow();
|
|
80
|
+
expect(witnessControls.retainQueries).toBe(1);
|
|
81
|
+
expect(await readStoredWitness(h, state)).toBeNull();
|
|
82
|
+
},
|
|
83
|
+
witnessFixtureOptions,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
for (const change of [
|
|
87
|
+
"livemode",
|
|
88
|
+
"mode",
|
|
89
|
+
"account",
|
|
90
|
+
"period",
|
|
91
|
+
"subject",
|
|
92
|
+
"extra",
|
|
93
|
+
] as const) {
|
|
94
|
+
configureAdmissionCase(
|
|
95
|
+
"retention refuses host grant control " + change,
|
|
96
|
+
prepareWitnessObservation,
|
|
97
|
+
async (h, state) => {
|
|
98
|
+
witnessControls.onRetainGrant = (grant) => {
|
|
99
|
+
const scope = data(Reflect.get(grant, "scope")),
|
|
100
|
+
provider = data(Reflect.get(scope, "providerScope"));
|
|
101
|
+
if (change === "livemode") Reflect.set(provider, "livemode", true);
|
|
102
|
+
if (change === "mode") Reflect.set(provider, "mode", "connected");
|
|
103
|
+
if (change === "account")
|
|
104
|
+
Reflect.set(provider, "stripeAccount", "acct_foreign");
|
|
105
|
+
if (change === "period")
|
|
106
|
+
Reflect.set(
|
|
107
|
+
data(Reflect.get(scope, "nativePeriod")),
|
|
108
|
+
"end",
|
|
109
|
+
state.scope.nativePeriod.end + 1,
|
|
110
|
+
);
|
|
111
|
+
if (change === "subject")
|
|
112
|
+
Reflect.set(grant, "tokenIdentifier", "https://host.test|foreign");
|
|
113
|
+
if (change === "extra") Reflect.set(grant, "extra", true);
|
|
114
|
+
};
|
|
115
|
+
await expect(invokeWitnessCapture(h, state)).rejects.toThrow();
|
|
116
|
+
expect(await readStoredWitness(h, state)).toBeNull();
|
|
117
|
+
},
|
|
118
|
+
witnessFixtureOptions,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
configureAdmissionCase(
|
|
122
|
+
"base observation permission cannot substitute for independent retention or later read permission",
|
|
123
|
+
prepareWitnessObservation,
|
|
124
|
+
async (h, state) => {
|
|
125
|
+
witnessControls.denyRetain = true;
|
|
126
|
+
await expect(invokeWitnessCapture(h, state)).rejects.toThrow();
|
|
127
|
+
expect(await readStoredWitness(h, state)).toBeNull();
|
|
128
|
+
witnessControls.denyRetain = false;
|
|
129
|
+
await invokeWitnessCapture(h, state);
|
|
130
|
+
const stored = await readStoredWitness(h, state);
|
|
131
|
+
witnessControls.denyRead = true;
|
|
132
|
+
const before = h.calls.length;
|
|
133
|
+
await expect(invokeWitnessRead(h, state)).rejects.toThrow();
|
|
134
|
+
expect(h.calls).toHaveLength(before);
|
|
135
|
+
expect(await readStoredWitness(h, state)).toEqual(stored);
|
|
136
|
+
},
|
|
137
|
+
witnessFixtureOptions,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
for (const path of [
|
|
141
|
+
["identity", "componentScope"],
|
|
142
|
+
["identity", "parentOperationId"],
|
|
143
|
+
["identity", "operationId"],
|
|
144
|
+
["identity", "witnessId"],
|
|
145
|
+
["providerScope", "customerId"],
|
|
146
|
+
["providerScope", "customerAccountId"],
|
|
147
|
+
["providerScope", "subscriptionId"],
|
|
148
|
+
["providerScope", "scheduleId"],
|
|
149
|
+
["configureRequestBinding"],
|
|
150
|
+
["observedItemId"],
|
|
151
|
+
] as const)
|
|
152
|
+
configureAdmissionCase(
|
|
153
|
+
"later read refuses a grant with foreign " + path.join("."),
|
|
154
|
+
prepareWitnessObservation,
|
|
155
|
+
async (h, state) => {
|
|
156
|
+
await invokeWitnessCapture(h, state);
|
|
157
|
+
const stored = await readStoredWitness(h, state),
|
|
158
|
+
before = h.calls.length;
|
|
159
|
+
witnessControls.onReadGrant = (grant) => {
|
|
160
|
+
const scope = data(Reflect.get(grant, "scope"));
|
|
161
|
+
const target =
|
|
162
|
+
path.length === 1 ? scope : data(Reflect.get(scope, path[0]));
|
|
163
|
+
Reflect.set(target, path[path.length - 1], "controlled-foreign-value");
|
|
164
|
+
};
|
|
165
|
+
await expect(invokeWitnessRead(h, state)).rejects.toThrow();
|
|
166
|
+
expect(witnessControls.readQueries).toBe(1);
|
|
167
|
+
expect(h.calls).toHaveLength(before);
|
|
168
|
+
expect(await readStoredWitness(h, state)).toEqual(stored);
|
|
169
|
+
},
|
|
170
|
+
witnessFixtureOptions,
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
for (const field of [
|
|
174
|
+
"witnessRowId",
|
|
175
|
+
"payloadBinding",
|
|
176
|
+
"tokenIdentifier",
|
|
177
|
+
] as const)
|
|
178
|
+
configureAdmissionCase(
|
|
179
|
+
"later read binds the exact granted " + field,
|
|
180
|
+
prepareWitnessObservation,
|
|
181
|
+
async (h, state) => {
|
|
182
|
+
await invokeWitnessCapture(h, state);
|
|
183
|
+
const stored = await readStoredWitness(h, state),
|
|
184
|
+
before = h.calls.length;
|
|
185
|
+
witnessControls.onReadGrant = (grant) =>
|
|
186
|
+
Reflect.set(
|
|
187
|
+
grant,
|
|
188
|
+
field,
|
|
189
|
+
field === "payloadBinding"
|
|
190
|
+
? "renewal-witness:v1:sha256:" + "0".repeat(64)
|
|
191
|
+
: "controlled-foreign-value",
|
|
192
|
+
);
|
|
193
|
+
await expect(invokeWitnessRead(h, state)).rejects.toThrow();
|
|
194
|
+
expect(h.calls).toHaveLength(before);
|
|
195
|
+
expect(await readStoredWitness(h, state)).toEqual(stored);
|
|
196
|
+
},
|
|
197
|
+
witnessFixtureOptions,
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
for (const mode of [
|
|
201
|
+
"expired-before-retain",
|
|
202
|
+
"changed-retention-revision",
|
|
203
|
+
"changed-read-revision",
|
|
204
|
+
] as const)
|
|
205
|
+
configureAdmissionCase(
|
|
206
|
+
"witness capability control " + mode,
|
|
207
|
+
prepareWitnessObservation,
|
|
208
|
+
async (h, state) => {
|
|
209
|
+
if (mode === "expired-before-retain") {
|
|
210
|
+
witnessControls.onRetainGrant = (grant) =>
|
|
211
|
+
Reflect.set(grant, "expiresAtMs", Date.now());
|
|
212
|
+
await expect(invokeWitnessCapture(h, state)).rejects.toThrow();
|
|
213
|
+
expect(await readStoredWitness(h, state)).toBeNull();
|
|
214
|
+
} else if (mode === "changed-retention-revision") {
|
|
215
|
+
witnessControls.onRetainGrant = (grant, count) => {
|
|
216
|
+
if (count === 2) Reflect.set(grant, "authorizationRevision", 2);
|
|
217
|
+
};
|
|
218
|
+
await expect(invokeWitnessCapture(h, state)).rejects.toThrow();
|
|
219
|
+
expect(await readStoredWitness(h, state)).not.toBeNull();
|
|
220
|
+
} else {
|
|
221
|
+
await invokeWitnessCapture(h, state);
|
|
222
|
+
const stored = await readStoredWitness(h, state);
|
|
223
|
+
witnessControls.onReadGrant = (grant, count) => {
|
|
224
|
+
if (count === 2) Reflect.set(grant, "authorizationRevision", 2);
|
|
225
|
+
};
|
|
226
|
+
await expect(invokeWitnessRead(h, state)).rejects.toThrow();
|
|
227
|
+
expect(await readStoredWitness(h, state)).toEqual(stored);
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
witnessFixtureOptions,
|
|
231
|
+
);
|