mppx 0.21.4 → 0.22.1
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 +29 -0
- package/dist/BodyDigest.d.ts +3 -3
- package/dist/BodyDigest.js +5 -3
- package/dist/Challenge.js +12 -2
- package/dist/Credential.js +9 -1
- package/dist/Errors.d.ts +62 -8
- package/dist/Errors.js +57 -6
- package/dist/Store.d.ts +7 -4
- package/dist/Store.js +18 -8
- package/dist/cli/account.d.ts +2 -1
- package/dist/cli/account.js +22 -11
- package/dist/cli/cli.js +135 -135
- package/dist/cli/internal.js +1 -1
- package/dist/cli/plugins/index.js +12 -12
- package/dist/client/Budget.d.ts +49 -0
- package/dist/client/Budget.js +101 -0
- package/dist/client/Mppx.d.ts +8 -3
- package/dist/client/Mppx.js +55 -13
- package/dist/client/Spend.d.ts +134 -0
- package/dist/client/Spend.js +58 -0
- package/dist/client/core.d.ts +2 -0
- package/dist/client/core.js +2 -0
- package/dist/client/internal/Fetch.d.ts +23 -3
- package/dist/client/internal/Fetch.js +155 -37
- package/dist/client/internal/FundsMoved.d.ts +9 -0
- package/dist/client/internal/FundsMoved.js +18 -0
- package/dist/client/internal/SpendGate.d.ts +136 -0
- package/dist/client/internal/SpendGate.js +608 -0
- package/dist/client/internal/SpendHooks.d.ts +42 -0
- package/dist/client/internal/SpendHooks.js +36 -0
- package/dist/client/internal/protocols/Mcp.d.ts +9 -0
- package/dist/client/internal/protocols/Mcp.js +27 -0
- package/dist/client/internal/protocols/Mpp.js +20 -3
- package/dist/client/internal/protocols/Shared.js +29 -3
- package/dist/evm/client/Charge.js +20 -8
- package/dist/extensions/identity/attestation/client/index.js +1 -0
- package/dist/extensions/identity/attestation/server/index.js +1 -0
- package/dist/extensions/identity/email-otp/server/index.js +1 -0
- package/dist/extensions/identity/erc8004/client/index.js +1 -0
- package/dist/extensions/identity/erc8004/server/index.d.ts +7 -1
- package/dist/extensions/identity/erc8004/server/index.js +16 -2
- package/dist/extensions/identity/link/client/index.js +57 -5
- package/dist/extensions/identity/link/server/index.js +1 -0
- package/dist/extensions/identity/poppy/server/index.js +1 -0
- package/dist/extensions/identity/tempo/client/index.js +1 -0
- package/dist/extensions/identity/tempo/server/index.js +7 -1
- package/dist/extensions/identity/world/server/index.js +1 -0
- package/dist/identity/Client.d.ts +7 -3
- package/dist/identity/Client.js +178 -13
- package/dist/identity/Headers.d.ts +3 -2
- package/dist/identity/Headers.js +3 -2
- package/dist/identity/Server.d.ts +8 -0
- package/dist/identity/Server.js +18 -0
- package/dist/identity/Types.d.ts +18 -3
- package/dist/integrations/x402/Negotiator.js +4 -1
- package/dist/integrations/x402/Solana.d.ts +75 -0
- package/dist/integrations/x402/Solana.js +143 -0
- package/dist/internal/challengeMeta.d.ts +17 -2
- package/dist/internal/challengeMeta.js +35 -7
- package/dist/internal/version.d.ts +2 -2
- package/dist/internal/version.js +1 -1
- package/dist/mcp/client/McpClient.d.ts +6 -1
- package/dist/mcp/client/McpClient.js +48 -10
- package/dist/proxy/Proxy.js +59 -46
- package/dist/proxy/internal/Headers.d.ts +9 -2
- package/dist/proxy/internal/Headers.js +16 -3
- package/dist/server/Mppx.d.ts +14 -1
- package/dist/server/Mppx.js +59 -6
- package/dist/server/Transport.js +46 -2
- package/dist/stripe/server/internal/html.gen.js +1 -1
- package/dist/tempo/client/Charge.js +81 -17
- package/dist/tempo/server/Charge.d.ts +7 -1
- package/dist/tempo/server/Charge.js +98 -12
- package/dist/tempo/server/internal/html.gen.js +1 -1
- package/dist/tempo/session/client/SessionManager.js +3 -0
- package/dist/x402/server/EvmCharge.js +3 -0
- package/package.json +1 -1
|
@@ -4,6 +4,8 @@ import type { Protocol } from './Protocol.js';
|
|
|
4
4
|
export declare const adapterPaymentMetaKey = "mppx/payment-client";
|
|
5
5
|
/** Whether the caller's MCP adapter owns approval and payment retry for this request. */
|
|
6
6
|
export declare function adapterOwnsPayment(request?: RequestInit): boolean;
|
|
7
|
+
/** Whether an MCP request already carries a Payment credential in its `_meta`. */
|
|
8
|
+
export declare function hasCredential(request?: RequestInit): boolean;
|
|
7
9
|
type CorePaymentRequiredData = NonNullable<Mcp.ErrorObject['data']>;
|
|
8
10
|
export type PaymentRequiredData = Pick<CorePaymentRequiredData, 'challenges'> & Partial<Pick<CorePaymentRequiredData, 'httpStatus' | 'problem'>>;
|
|
9
11
|
/** Extracts validated payment-required data from MCP errors or tool result metadata. */
|
|
@@ -13,5 +15,12 @@ export declare function paymentRequiredData(message: Mcp.Response | undefined):
|
|
|
13
15
|
* in a normal 200 body (often `text/event-stream`), and the credential rides back in `_meta`.
|
|
14
16
|
*/
|
|
15
17
|
export declare function mcp(): Protocol;
|
|
18
|
+
/**
|
|
19
|
+
* Whether an MCP-over-HTTP response answers the request with a payment-required error whose
|
|
20
|
+
* problem is `invalid-challenge` (a replayed or otherwise unusable Challenge).
|
|
21
|
+
*/
|
|
22
|
+
export declare function isInvalidChallenge(response: Response, request?: RequestInit): Promise<boolean>;
|
|
23
|
+
/** Problem type of an unusable (replayed, expired or unknown) Challenge. */
|
|
24
|
+
export declare const invalidChallengeType = "https://paymentauth.org/problems/invalid-challenge";
|
|
16
25
|
export {};
|
|
17
26
|
//# sourceMappingURL=Mcp.d.ts.map
|
|
@@ -34,6 +34,18 @@ export function adapterOwnsPayment(request) {
|
|
|
34
34
|
return false;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
+
/** Whether an MCP request already carries a Payment credential in its `_meta`. */
|
|
38
|
+
export function hasCredential(request) {
|
|
39
|
+
if (typeof request?.body !== 'string')
|
|
40
|
+
return false;
|
|
41
|
+
try {
|
|
42
|
+
const message = JSON.parse(request.body);
|
|
43
|
+
return message?.jsonrpc === '2.0' && message.params?._meta?.[Mcp.credentialMetaKey] != null;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
37
49
|
// Bound automatic inspection; the caller retains the original response stream.
|
|
38
50
|
const maxInspectionBytes = 1024 * 1024;
|
|
39
51
|
const responseCache = new WeakMap();
|
|
@@ -227,4 +239,19 @@ export function mcp() {
|
|
|
227
239
|
},
|
|
228
240
|
};
|
|
229
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Whether an MCP-over-HTTP response answers the request with a payment-required error whose
|
|
244
|
+
* problem is `invalid-challenge` (a replayed or otherwise unusable Challenge).
|
|
245
|
+
*/
|
|
246
|
+
export async function isInvalidChallenge(response, request) {
|
|
247
|
+
const id = mcpHttpRequestId(request);
|
|
248
|
+
if (response.status === paymentRequiredStatus || id === undefined)
|
|
249
|
+
return false;
|
|
250
|
+
const message = await parseJsonRpcResponse(response);
|
|
251
|
+
if (!message || message.id !== id)
|
|
252
|
+
return false;
|
|
253
|
+
return paymentRequiredData(message)?.problem?.type === invalidChallengeType;
|
|
254
|
+
}
|
|
255
|
+
/** Problem type of an unusable (replayed, expired or unknown) Challenge. */
|
|
256
|
+
export const invalidChallengeType = 'https://paymentauth.org/problems/invalid-challenge';
|
|
230
257
|
//# sourceMappingURL=Mcp.js.map
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as Challenge from '../../../Challenge.js';
|
|
2
2
|
import * as Constants from '../../../Constants.js';
|
|
3
|
+
import * as Credential from '../../../Credential.js';
|
|
3
4
|
import { paymentRequiredStatus, setCredentialHeader } from './Shared.js';
|
|
4
5
|
/** MPP — native HTTP Payment authentication. */
|
|
5
6
|
export function mpp() {
|
|
@@ -14,10 +15,26 @@ export function mpp() {
|
|
|
14
15
|
return Challenge.fromResponseList(response);
|
|
15
16
|
},
|
|
16
17
|
setCredential(request, credential, options) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
// A Payment credential echoes its challenge, so callers that attach one without passing the
|
|
19
|
+
// challenge still send it in the field that challenge selects (`Payment-Authorization`).
|
|
20
|
+
const challenge = options?.challenge ?? credentialChallenge(credential);
|
|
21
|
+
return setCredentialHeader(request, challenge ? Challenge.credentialHeader(challenge) : Constants.Headers.authorization, credential);
|
|
20
22
|
},
|
|
21
23
|
};
|
|
22
24
|
}
|
|
25
|
+
/** `Payment <base64url>`, as `Credential.serialize` writes it. */
|
|
26
|
+
const serializedCredential = /^Payment [A-Za-z0-9_-]+={0,2}$/;
|
|
27
|
+
/** Returns the challenge a serialized Payment credential echoes, or `undefined` for any other value. */
|
|
28
|
+
function credentialChallenge(credential) {
|
|
29
|
+
// Parse only values shaped like a serialized credential, checked in linear time, so arbitrary
|
|
30
|
+
// strings never reach the deserializer's regex.
|
|
31
|
+
if (!serializedCredential.test(credential))
|
|
32
|
+
return undefined;
|
|
33
|
+
try {
|
|
34
|
+
return Credential.deserialize(credential).challenge;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
23
40
|
//# sourceMappingURL=Mpp.js.map
|
|
@@ -5,6 +5,7 @@ export const paymentRequiredStatus = 402;
|
|
|
5
5
|
/** Credential headers purged before a fresh credential is attached. */
|
|
6
6
|
const credentialHeaders = [
|
|
7
7
|
Constants.Headers.authorization,
|
|
8
|
+
Constants.Headers.paymentAuthorization,
|
|
8
9
|
x402_Types.paymentRequiredHeader,
|
|
9
10
|
x402_Types.paymentResponseHeader,
|
|
10
11
|
x402_Types.paymentSignatureHeader,
|
|
@@ -13,12 +14,37 @@ const credentialHeaders = [
|
|
|
13
14
|
export function setCredentialHeader(request, header, credential) {
|
|
14
15
|
const headers = new Headers(request.headers);
|
|
15
16
|
for (const stale of credentialHeaders) {
|
|
16
|
-
|
|
17
|
-
if (
|
|
17
|
+
const value = headers.get(stale);
|
|
18
|
+
if (value === null)
|
|
18
19
|
continue;
|
|
19
|
-
|
|
20
|
+
if (!isPaymentField(stale)) {
|
|
21
|
+
headers.delete(stale);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
// Never erase ordinary application credentials: drop only the Payment members, so a retry
|
|
25
|
+
// never carries a stale credential in the field the new challenge did not select.
|
|
26
|
+
const kept = withoutPaymentMembers(value);
|
|
27
|
+
if (kept === value)
|
|
28
|
+
continue;
|
|
29
|
+
if (kept)
|
|
30
|
+
headers.set(stale, kept);
|
|
31
|
+
else
|
|
32
|
+
headers.delete(stale);
|
|
20
33
|
}
|
|
21
34
|
headers.set(header, credential);
|
|
22
35
|
return { ...request, headers };
|
|
23
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Removes the Payment members from a credential field, matched as servers match them
|
|
39
|
+
* (comma-separated, scheme case-insensitive). Returns `value` itself when it holds none.
|
|
40
|
+
*/
|
|
41
|
+
function withoutPaymentMembers(value) {
|
|
42
|
+
const members = value.split(',').map((member) => member.trim());
|
|
43
|
+
const kept = members.filter((member) => !paymentMember.test(member));
|
|
44
|
+
return kept.length === members.length ? value : kept.filter(Boolean).join(', ');
|
|
45
|
+
}
|
|
46
|
+
const paymentMember = new RegExp(`^${Constants.Schemes.payment}(?:\\s|$)`, 'i');
|
|
47
|
+
function isPaymentField(header) {
|
|
48
|
+
return (header === Constants.Headers.authorization || header === Constants.Headers.paymentAuthorization);
|
|
49
|
+
}
|
|
24
50
|
//# sourceMappingURL=Shared.js.map
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getAddress, parseUnits } from 'viem';
|
|
2
|
+
import * as SpendHooks from '../../client/internal/SpendHooks.js';
|
|
2
3
|
import * as Credential from '../../Credential.js';
|
|
3
4
|
import * as x402_ChallengeBrand from '../../internal/x402/ChallengeBrand.js';
|
|
4
5
|
import * as Method from '../../Method.js';
|
|
@@ -14,7 +15,7 @@ import * as Types from '../Types.js';
|
|
|
14
15
|
* also keeps x402 exact signing for x402 compatibility challenges.
|
|
15
16
|
*/
|
|
16
17
|
export function charge(parameters) {
|
|
17
|
-
|
|
18
|
+
const method = Method.toClient(Methods.charge, {
|
|
18
19
|
canHandleChallenge({ challenge }) {
|
|
19
20
|
if (!isX402Challenge(challenge))
|
|
20
21
|
return true;
|
|
@@ -37,13 +38,7 @@ export function charge(parameters) {
|
|
|
37
38
|
if (!account?.signTypedData)
|
|
38
39
|
throw new Error('EVM authorization requires a typed-data signer.');
|
|
39
40
|
const request = challenge.request;
|
|
40
|
-
|
|
41
|
-
if (!request.methodDetails.credentialTypes?.includes('authorization')) {
|
|
42
|
-
throw new Error('EVM authorization is not accepted by this challenge.');
|
|
43
|
-
}
|
|
44
|
-
if (request.methodDetails.splits?.length) {
|
|
45
|
-
throw new Error('EVM authorization does not support payment splits.');
|
|
46
|
-
}
|
|
41
|
+
assertOffer(parameters, request);
|
|
47
42
|
const authorization = resolveAuthorization(parameters, request);
|
|
48
43
|
const validBefore = challenge.expires
|
|
49
44
|
? Math.floor(new Date(challenge.expires).getTime() / 1000).toString()
|
|
@@ -84,12 +79,29 @@ export function charge(parameters) {
|
|
|
84
79
|
}));
|
|
85
80
|
},
|
|
86
81
|
});
|
|
82
|
+
// The client's spending options refuse what this method refuses before any funder runs.
|
|
83
|
+
return SpendHooks.register(method, {
|
|
84
|
+
assert(challenge) {
|
|
85
|
+
if (!isX402Challenge(challenge))
|
|
86
|
+
assertOffer(parameters, challenge.request);
|
|
87
|
+
},
|
|
88
|
+
});
|
|
87
89
|
}
|
|
88
90
|
function isX402Challenge(challenge) {
|
|
89
91
|
return (x402_ChallengeBrand.is(challenge) &&
|
|
90
92
|
challenge.request.scheme === 'exact' &&
|
|
91
93
|
typeof challenge.request.network === 'string');
|
|
92
94
|
}
|
|
95
|
+
/** Throws for a native offer this client would refuse to sign: its policy, or what it cannot pay. */
|
|
96
|
+
function assertOffer(parameters, request) {
|
|
97
|
+
assertPolicy(parameters, request);
|
|
98
|
+
if (!request.methodDetails.credentialTypes?.includes('authorization')) {
|
|
99
|
+
throw new Error('EVM authorization is not accepted by this challenge.');
|
|
100
|
+
}
|
|
101
|
+
if (request.methodDetails.splits?.length) {
|
|
102
|
+
throw new Error('EVM authorization does not support payment splits.');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
93
105
|
function assertPolicy(parameters, request) {
|
|
94
106
|
const chainId = request.methodDetails.chainId;
|
|
95
107
|
const network = Types.networkOf(chainId);
|
|
@@ -73,6 +73,7 @@ export function prover(config) {
|
|
|
73
73
|
}
|
|
74
74
|
return {
|
|
75
75
|
scope: config.scope,
|
|
76
|
+
headers: [Constants.header.toLowerCase()],
|
|
76
77
|
prove(request, context) {
|
|
77
78
|
const origin = new URL(request.url).origin;
|
|
78
79
|
// A bearer attestation never goes over plaintext HTTP, except to localhost.
|
|
@@ -42,6 +42,7 @@ export function verifier(config) {
|
|
|
42
42
|
]),
|
|
43
43
|
});
|
|
44
44
|
return {
|
|
45
|
+
headers: { request: [Constants.header], response: [Constants.requiredHeader] },
|
|
45
46
|
async verify(request) {
|
|
46
47
|
const audience = origin(typeof config.audience === 'function' ? config.audience(request) : config.audience);
|
|
47
48
|
// At most `maxCandidates` distinct tokens are verified, latest expiry first,
|
|
@@ -19,6 +19,7 @@ export function prover(config) {
|
|
|
19
19
|
});
|
|
20
20
|
return {
|
|
21
21
|
scope: config.scope,
|
|
22
|
+
headers: [Constants.idHeader.toLowerCase(), Constants.signatureHeader.toLowerCase()],
|
|
22
23
|
async prove(request, { created, nonce }) {
|
|
23
24
|
const signature = await config.account.signMessage({
|
|
24
25
|
message: Agent.message({ created, id, nonce, request }),
|
|
@@ -13,7 +13,7 @@ import type * as Types from '../Types.js';
|
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* ```ts
|
|
16
|
-
* const agents = Erc8004.Server.create({ store })
|
|
16
|
+
* const agents = Erc8004.Server.create({ origin: 'https://api.example', store })
|
|
17
17
|
* mppx.charge({ amount: '0.01', identity: { request: [agents.verifier], payment: agents.paymentCheck } })
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
@@ -26,6 +26,12 @@ export declare namespace create {
|
|
|
26
26
|
readonly chainId?: number | undefined;
|
|
27
27
|
/** A viem client for the registry's chain. @default an HTTP client for `rpcUrl` */
|
|
28
28
|
readonly client?: Client | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Trusted receiving origin, independent of request headers (for example, `https://api.example`).
|
|
31
|
+
* Configure this when an adapter derives URLs from Host. Without it, trusted ingress must
|
|
32
|
+
* reject foreign hosts to prevent signed requests for another service from being accepted.
|
|
33
|
+
*/
|
|
34
|
+
readonly origin?: string | undefined;
|
|
29
35
|
/** The Identity Registry. @default '0x8004A169FB4a3325136EB29fA0ceB6D2e539a432' */
|
|
30
36
|
readonly registry?: Address | undefined;
|
|
31
37
|
/** RPC URL of the registry's chain. @default 'https://rpc.tempo.xyz' */
|
|
@@ -33,11 +33,21 @@ const abi = [
|
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
35
|
* ```ts
|
|
36
|
-
* const agents = Erc8004.Server.create({ store })
|
|
36
|
+
* const agents = Erc8004.Server.create({ origin: 'https://api.example', store })
|
|
37
37
|
* mppx.charge({ amount: '0.01', identity: { request: [agents.verifier], payment: agents.paymentCheck } })
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
40
|
export function create(config = {}) {
|
|
41
|
+
const origin = config.origin === undefined ? undefined : new URL(config.origin);
|
|
42
|
+
if (origin &&
|
|
43
|
+
(!['http:', 'https:'].includes(origin.protocol) ||
|
|
44
|
+
origin.username ||
|
|
45
|
+
origin.password ||
|
|
46
|
+
origin.pathname !== '/' ||
|
|
47
|
+
origin.search ||
|
|
48
|
+
origin.hash))
|
|
49
|
+
throw new TypeError('Pass an HTTP(S) origin without credentials, path, query, or fragment.');
|
|
50
|
+
const expectedOrigin = origin?.origin;
|
|
41
51
|
const chainId = config.chainId ?? Constants.chainId;
|
|
42
52
|
const registry = getAddress(config.registry ?? Constants.identityRegistry);
|
|
43
53
|
if (!config.client && !config.rpcUrl && chainId !== Constants.chainId)
|
|
@@ -50,6 +60,8 @@ export function create(config = {}) {
|
|
|
50
60
|
const invalid = (reason) => ({ status: 'invalid', reason });
|
|
51
61
|
/** Reads the request's agent and its wallet from the registry. */
|
|
52
62
|
async function lookup(request) {
|
|
63
|
+
if (expectedOrigin !== undefined && new URL(request.url).origin !== expectedOrigin)
|
|
64
|
+
return invalid('The request origin does not match this API.');
|
|
53
65
|
const id = request.headers.get(Constants.idHeader);
|
|
54
66
|
if (!id)
|
|
55
67
|
return { status: 'missing', reason: 'Sign the request as an ERC-8004 agent.' };
|
|
@@ -141,9 +153,11 @@ export function create(config = {}) {
|
|
|
141
153
|
return result;
|
|
142
154
|
return { status: 'forbidden', reason: 'This agent may not call this API.' };
|
|
143
155
|
}
|
|
156
|
+
const headers = { request: [Constants.idHeader, Constants.signatureHeader] };
|
|
144
157
|
return {
|
|
145
|
-
verifier: { verify: async (request) => allowed(await getSigned(request)) },
|
|
158
|
+
verifier: { headers, verify: async (request) => allowed(await getSigned(request)) },
|
|
146
159
|
paymentCheck: {
|
|
160
|
+
headers,
|
|
147
161
|
requires: [{ type: Constants.type }],
|
|
148
162
|
async verify({ challenge, credential, request }) {
|
|
149
163
|
// Only a tempo charge the source signed binds it to the payer: a transaction or a proof.
|
|
@@ -130,7 +130,7 @@ export function claims(config) {
|
|
|
130
130
|
return undefined;
|
|
131
131
|
const origin = new URL(request.url).origin;
|
|
132
132
|
presentations.delete(origin);
|
|
133
|
-
const challenge = await
|
|
133
|
+
const challenge = await readChallenge(response, request.signal);
|
|
134
134
|
if (!challenge)
|
|
135
135
|
return undefined;
|
|
136
136
|
// Bound to this origin only: a server can't relay another audience's challenge.
|
|
@@ -158,10 +158,11 @@ export function claims(config) {
|
|
|
158
158
|
}
|
|
159
159
|
/** Parses a claims challenge's problem body, or returns `undefined` when it is not one. */
|
|
160
160
|
export async function claimsChallenge(response) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
161
|
+
return readChallenge(response);
|
|
162
|
+
}
|
|
163
|
+
/** {@link claimsChallenge}, giving up when `signal` aborts. */
|
|
164
|
+
async function readChallenge(response, signal) {
|
|
165
|
+
const body = (await readJson(response.clone(), signal));
|
|
165
166
|
if (!body || body.type !== Constants.claimsRequiredType)
|
|
166
167
|
return undefined;
|
|
167
168
|
const strings = (value) => Array.isArray(value) && value.every((item) => typeof item === 'string');
|
|
@@ -208,4 +209,55 @@ function track(sent, current, value) {
|
|
|
208
209
|
sent.delete(old);
|
|
209
210
|
}
|
|
210
211
|
}
|
|
212
|
+
/** Largest claims challenge body read; a larger one is not a claims challenge. */
|
|
213
|
+
const maxChallengeBytes = 64 * 1024;
|
|
214
|
+
/** Longest wait for a claims challenge body; a slower one is not a claims challenge. */
|
|
215
|
+
const challengeTimeoutMs = 2_000;
|
|
216
|
+
/**
|
|
217
|
+
* Parses a response body as JSON, reading at most {@link maxChallengeBytes} within
|
|
218
|
+
* {@link challengeTimeoutMs} and until `signal` aborts; `undefined` otherwise.
|
|
219
|
+
*/
|
|
220
|
+
async function readJson(response, signal) {
|
|
221
|
+
const length = Number(response.headers.get('content-length'));
|
|
222
|
+
if (!response.body || length > maxChallengeBytes || signal?.aborted) {
|
|
223
|
+
void response.body?.cancel().catch(() => { });
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
const reader = response.body.getReader();
|
|
227
|
+
const chunks = [];
|
|
228
|
+
let size = 0;
|
|
229
|
+
// Not awaited: a cloned body's cancel waits for its unread sibling.
|
|
230
|
+
let stopped = false;
|
|
231
|
+
const stop = () => {
|
|
232
|
+
stopped = true;
|
|
233
|
+
void reader.cancel().catch(() => { });
|
|
234
|
+
};
|
|
235
|
+
const timeout = setTimeout(stop, challengeTimeoutMs);
|
|
236
|
+
signal?.addEventListener('abort', stop, { once: true });
|
|
237
|
+
try {
|
|
238
|
+
for (;;) {
|
|
239
|
+
const { done, value } = await reader.read();
|
|
240
|
+
// A cancelled read ends as done: what arrived before the deadline is not the whole body.
|
|
241
|
+
if (stopped)
|
|
242
|
+
return undefined;
|
|
243
|
+
if (done)
|
|
244
|
+
break;
|
|
245
|
+
size += value.byteLength;
|
|
246
|
+
// Not awaited: a cloned body's cancel waits for its unread sibling.
|
|
247
|
+
if (size > maxChallengeBytes) {
|
|
248
|
+
void reader.cancel().catch(() => { });
|
|
249
|
+
return undefined;
|
|
250
|
+
}
|
|
251
|
+
chunks.push(value);
|
|
252
|
+
}
|
|
253
|
+
return JSON.parse(await new Blob(chunks).text());
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
finally {
|
|
259
|
+
clearTimeout(timeout);
|
|
260
|
+
signal?.removeEventListener('abort', stop);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
211
263
|
//# sourceMappingURL=index.js.map
|
|
@@ -121,6 +121,7 @@ export function attestations(config) {
|
|
|
121
121
|
}
|
|
122
122
|
return {
|
|
123
123
|
scope: config.scope,
|
|
124
|
+
headers: [AttestationConstants.header.toLowerCase()],
|
|
124
125
|
async prove(request, context) {
|
|
125
126
|
const { prover, user } = await resolve();
|
|
126
127
|
// A request already in flight (such as a payment retry) may still carry the previous
|
|
@@ -198,10 +198,16 @@ export function create(config) {
|
|
|
198
198
|
verified.set(request, (result = check(request)));
|
|
199
199
|
return result;
|
|
200
200
|
}
|
|
201
|
+
const protocolHeaders = { request: [Constants.idTokenHeader, 'DPoP'] };
|
|
201
202
|
return {
|
|
202
|
-
verifier: {
|
|
203
|
+
verifier: {
|
|
204
|
+
authorization: true,
|
|
205
|
+
headers: protocolHeaders,
|
|
206
|
+
verify: async (request) => allowed(await verify(request)),
|
|
207
|
+
},
|
|
203
208
|
paymentCheck: {
|
|
204
209
|
authorization: true,
|
|
210
|
+
headers: protocolHeaders,
|
|
205
211
|
requires: [{ type: Constants.type, issuer }],
|
|
206
212
|
async verify(parameters) {
|
|
207
213
|
const caller = await verify(parameters.request);
|
|
@@ -20,8 +20,10 @@ export type Client = {
|
|
|
20
20
|
* Requests that carry proofs do not follow redirects: a redirect rejects
|
|
21
21
|
* (`redirect: 'error'`), so proofs never reach another URL. Requests with no
|
|
22
22
|
* prover in scope are sent unchanged. If a prover asks to retry a rejected
|
|
23
|
-
* (non-402) response, the request is proved again and resent once
|
|
24
|
-
*
|
|
23
|
+
* (non-402) response, the request is proved again and resent once. A paid
|
|
24
|
+
* attempt is resent only after a 401 with no receipt, with the same Payment
|
|
25
|
+
* credential and while its Challenge has not expired: servers check other authentication
|
|
26
|
+
* before verifying a Payment credential, so a 401 means nothing settled.
|
|
25
27
|
*/
|
|
26
28
|
readonly send: (fetch: typeof globalThis.fetch, request: Request, context?: SendContext) => Promise<{
|
|
27
29
|
request: Request;
|
|
@@ -44,7 +46,8 @@ export type Client = {
|
|
|
44
46
|
*
|
|
45
47
|
* - `unproved`: nothing sent yet.
|
|
46
48
|
* - `sent`: an unpaid attempt was sent; it may be resent once.
|
|
47
|
-
* - `paid`: a paid attempt was sent; it is
|
|
49
|
+
* - `paid`: a paid attempt was sent; it is resent once, with the same credential, only after a 401
|
|
50
|
+
* while its Challenge has not expired, since any other response may follow settlement.
|
|
48
51
|
* - `resent`: the request is proved again and resent once, with `headers` set.
|
|
49
52
|
* - `done`: the last response is final.
|
|
50
53
|
*
|
|
@@ -85,6 +88,7 @@ export declare const initial: Attempt;
|
|
|
85
88
|
* | `unproved` | `send` (paid) | `paid` |
|
|
86
89
|
* | `unproved` | `send` (unpaid) | `sent` |
|
|
87
90
|
* | `sent` | `response` with resend | `resent` |
|
|
91
|
+
* | `paid` (after a 401) | `response` with resend | `resent` |
|
|
88
92
|
* | `sent`, `paid`, `resent` | `response` without | `done` |
|
|
89
93
|
*
|
|
90
94
|
* @internal
|