mppx 0.3.13 → 0.3.14
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/dist/Challenge.d.ts +1 -1
- package/dist/Challenge.d.ts.map +1 -1
- package/dist/Challenge.js +107 -15
- package/dist/Challenge.js.map +1 -1
- package/dist/server/Mppx.d.ts.map +1 -1
- package/dist/server/Mppx.js +2 -1
- package/dist/server/Mppx.js.map +1 -1
- package/dist/stripe/Methods.d.ts +0 -3
- package/dist/stripe/Methods.d.ts.map +1 -1
- package/dist/stripe/Methods.js +0 -2
- package/dist/stripe/Methods.js.map +1 -1
- package/dist/stripe/client/Charge.d.ts +0 -3
- package/dist/stripe/client/Charge.d.ts.map +1 -1
- package/dist/stripe/client/Charge.js +2 -2
- package/dist/stripe/client/Charge.js.map +1 -1
- package/dist/stripe/client/Methods.d.ts +0 -3
- package/dist/stripe/client/Methods.d.ts.map +1 -1
- package/dist/stripe/server/Charge.d.ts +0 -3
- package/dist/stripe/server/Charge.d.ts.map +1 -1
- package/dist/stripe/server/Charge.js +2 -2
- package/dist/stripe/server/Charge.js.map +1 -1
- package/dist/stripe/server/Methods.d.ts +0 -3
- package/dist/stripe/server/Methods.d.ts.map +1 -1
- package/dist/tempo/Methods.d.ts +0 -3
- package/dist/tempo/Methods.d.ts.map +1 -1
- package/dist/tempo/Methods.js +3 -3
- package/dist/tempo/Methods.js.map +1 -1
- package/dist/tempo/client/Charge.d.ts +13 -3
- package/dist/tempo/client/Charge.d.ts.map +1 -1
- package/dist/tempo/client/Charge.js +18 -1
- package/dist/tempo/client/Charge.js.map +1 -1
- package/dist/tempo/client/Methods.d.ts +4 -3
- package/dist/tempo/client/Methods.d.ts.map +1 -1
- package/dist/tempo/server/Charge.d.ts +0 -3
- package/dist/tempo/server/Charge.d.ts.map +1 -1
- package/dist/tempo/server/Charge.js +2 -1
- package/dist/tempo/server/Charge.js.map +1 -1
- package/dist/tempo/server/Methods.d.ts +0 -3
- package/dist/tempo/server/Methods.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/Challenge.test.ts +94 -18
- package/src/Challenge.ts +118 -15
- package/src/PaymentRequest.test.ts +0 -5
- package/src/client/Mppx.test.ts +5 -5
- package/src/client/Transport.test.ts +5 -8
- package/src/client/internal/Fetch.browser.test.ts +135 -0
- package/src/client/internal/Fetch.test.ts +0 -88
- package/src/mcp-sdk/client/McpClient.test.ts +1 -1
- package/src/server/Mppx.ts +3 -1
- package/src/server/Transport.test.ts +6 -9
- package/src/stripe/Methods.ts +0 -2
- package/src/stripe/client/Charge.ts +2 -2
- package/src/stripe/server/Charge.ts +2 -2
- package/src/tempo/Methods.test.ts +22 -0
- package/src/tempo/Methods.ts +3 -3
- package/src/tempo/client/Charge.ts +29 -1
- package/src/tempo/server/Charge.test.ts +34 -72
- package/src/tempo/server/Charge.ts +2 -1
package/src/Challenge.ts
CHANGED
|
@@ -125,7 +125,7 @@ export function from<
|
|
|
125
125
|
secretKey,
|
|
126
126
|
} = parameters
|
|
127
127
|
|
|
128
|
-
const expires =
|
|
128
|
+
const expires = parameters.expires as string
|
|
129
129
|
const id = secretKey
|
|
130
130
|
? computeId({ ...parameters, expires, ...(meta && { opaque: meta }) }, { secretKey })
|
|
131
131
|
: (parameters as { id: string }).id
|
|
@@ -205,11 +205,11 @@ export declare namespace from {
|
|
|
205
205
|
* Methods.charge,
|
|
206
206
|
* {
|
|
207
207
|
* realm: 'api.example.com',
|
|
208
|
+
* expires: '2025-01-06T12:00:00Z',
|
|
208
209
|
* request: {
|
|
209
210
|
* amount: '1000000',
|
|
210
211
|
* currency: '0x20c0000000000000000000000000000000000001',
|
|
211
212
|
* recipient: '0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00',
|
|
212
|
-
* expires: '2025-01-06T12:00:00Z',
|
|
213
213
|
* },
|
|
214
214
|
* },
|
|
215
215
|
* { secretKey: 'my-secret' },
|
|
@@ -319,20 +319,10 @@ export function deserialize<const methods extends readonly Method.Method[] | und
|
|
|
319
319
|
value: string,
|
|
320
320
|
options?: from.Options<methods>,
|
|
321
321
|
): from.ReturnType<from.Parameters, methods> {
|
|
322
|
-
const
|
|
323
|
-
if (!
|
|
322
|
+
const params = extractPaymentAuthParams(value)
|
|
323
|
+
if (!params) throw new Error('Missing Payment scheme.')
|
|
324
324
|
|
|
325
|
-
const
|
|
326
|
-
const result: Record<string, string> = {}
|
|
327
|
-
|
|
328
|
-
for (const match of params.matchAll(/(\w+)="([^"]+)"/g)) {
|
|
329
|
-
const key = match[1]
|
|
330
|
-
const value = match[2]
|
|
331
|
-
if (key && value) {
|
|
332
|
-
if (key in result) throw new Error(`Duplicate parameter: ${key}`)
|
|
333
|
-
result[key] = value
|
|
334
|
-
}
|
|
335
|
-
}
|
|
325
|
+
const result = parseAuthParams(params)
|
|
336
326
|
|
|
337
327
|
const { request, opaque, ...rest } = result
|
|
338
328
|
if (!request) throw new Error('Missing request parameter.')
|
|
@@ -349,6 +339,119 @@ export function deserialize<const methods extends readonly Method.Method[] | und
|
|
|
349
339
|
)
|
|
350
340
|
}
|
|
351
341
|
|
|
342
|
+
/** @internal Extracts the `Payment` scheme from a WWW-Authenticate value that may contain multiple schemes. */
|
|
343
|
+
function extractPaymentAuthParams(header: string): string | null {
|
|
344
|
+
const token = 'Payment'
|
|
345
|
+
let inQuotes = false
|
|
346
|
+
let escaped = false
|
|
347
|
+
|
|
348
|
+
for (let i = 0; i < header.length; i++) {
|
|
349
|
+
const char = header[i]
|
|
350
|
+
|
|
351
|
+
if (inQuotes) {
|
|
352
|
+
if (escaped) escaped = false
|
|
353
|
+
else if (char === '\\') escaped = true
|
|
354
|
+
else if (char === '"') inQuotes = false
|
|
355
|
+
continue
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (char === '"') {
|
|
359
|
+
inQuotes = true
|
|
360
|
+
continue
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (!startsWithSchemeToken(header, i, token)) continue
|
|
364
|
+
|
|
365
|
+
const prefix = header.slice(0, i)
|
|
366
|
+
if (prefix.trim() && !prefix.trimEnd().endsWith(',')) continue
|
|
367
|
+
|
|
368
|
+
let paramsStart = i + token.length
|
|
369
|
+
while (paramsStart < header.length && /\s/.test(header[paramsStart] ?? '')) paramsStart++
|
|
370
|
+
return header.slice(paramsStart)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return null
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** @internal Parses auth-params with support for escaped quoted-string values. */
|
|
377
|
+
function parseAuthParams(input: string): Record<string, string> {
|
|
378
|
+
const result: Record<string, string> = {}
|
|
379
|
+
let i = 0
|
|
380
|
+
|
|
381
|
+
while (i < input.length) {
|
|
382
|
+
while (i < input.length && /[\s,]/.test(input[i] ?? '')) i++
|
|
383
|
+
if (i >= input.length) break
|
|
384
|
+
|
|
385
|
+
const keyStart = i
|
|
386
|
+
while (i < input.length && /[A-Za-z0-9_-]/.test(input[i] ?? '')) i++
|
|
387
|
+
const key = input.slice(keyStart, i)
|
|
388
|
+
if (!key) throw new Error('Malformed auth-param.')
|
|
389
|
+
|
|
390
|
+
while (i < input.length && /\s/.test(input[i] ?? '')) i++
|
|
391
|
+
|
|
392
|
+
// If there is no '=' after a token, this is likely another auth scheme.
|
|
393
|
+
if (input[i] !== '=') break
|
|
394
|
+
i++
|
|
395
|
+
|
|
396
|
+
while (i < input.length && /\s/.test(input[i] ?? '')) i++
|
|
397
|
+
|
|
398
|
+
const [value, nextIndex] = readAuthParamValue(input, i)
|
|
399
|
+
i = nextIndex
|
|
400
|
+
|
|
401
|
+
if (key in result) throw new Error(`Duplicate parameter: ${key}`)
|
|
402
|
+
result[key] = value
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return result
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** @internal */
|
|
409
|
+
function readAuthParamValue(input: string, start: number): [value: string, nextIndex: number] {
|
|
410
|
+
if (input[start] === '"') return readQuotedAuthParamValue(input, start + 1)
|
|
411
|
+
|
|
412
|
+
let i = start
|
|
413
|
+
while (i < input.length && input[i] !== ',') i++
|
|
414
|
+
return [input.slice(start, i).trim(), i]
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** @internal */
|
|
418
|
+
function readQuotedAuthParamValue(
|
|
419
|
+
input: string,
|
|
420
|
+
start: number,
|
|
421
|
+
): [value: string, nextIndex: number] {
|
|
422
|
+
let i = start
|
|
423
|
+
let value = ''
|
|
424
|
+
let escaped = false
|
|
425
|
+
|
|
426
|
+
while (i < input.length) {
|
|
427
|
+
const char = input[i]!
|
|
428
|
+
i++
|
|
429
|
+
|
|
430
|
+
if (escaped) {
|
|
431
|
+
value += char
|
|
432
|
+
escaped = false
|
|
433
|
+
continue
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (char === '\\') {
|
|
437
|
+
escaped = true
|
|
438
|
+
continue
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (char === '"') return [value, i]
|
|
442
|
+
value += char
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
throw new Error('Unterminated quoted-string.')
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** @internal */
|
|
449
|
+
function startsWithSchemeToken(value: string, index: number, token: string): boolean {
|
|
450
|
+
if (!value.slice(index).toLowerCase().startsWith(token.toLowerCase())) return false
|
|
451
|
+
const next = value[index + token.length]
|
|
452
|
+
return Boolean(next && /\s/.test(next))
|
|
453
|
+
}
|
|
454
|
+
|
|
352
455
|
/**
|
|
353
456
|
* Extracts the challenge from a Headers object.
|
|
354
457
|
*
|
|
@@ -26,13 +26,11 @@ describe('fromMethod', () => {
|
|
|
26
26
|
currency: '0x20c0000000000000000000000000000000000001',
|
|
27
27
|
decimals: 6,
|
|
28
28
|
recipient: '0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00',
|
|
29
|
-
expires: '2025-01-06T12:00:00Z',
|
|
30
29
|
})
|
|
31
30
|
expect(request).toMatchInlineSnapshot(`
|
|
32
31
|
{
|
|
33
32
|
"amount": "1000000",
|
|
34
33
|
"currency": "0x20c0000000000000000000000000000000000001",
|
|
35
|
-
"expires": "2025-01-06T12:00:00Z",
|
|
36
34
|
"recipient": "0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00",
|
|
37
35
|
}
|
|
38
36
|
`)
|
|
@@ -44,14 +42,12 @@ describe('fromMethod', () => {
|
|
|
44
42
|
currency: '0x20c0000000000000000000000000000000000001',
|
|
45
43
|
decimals: 6,
|
|
46
44
|
recipient: '0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00',
|
|
47
|
-
expires: '2025-01-06T12:00:00Z',
|
|
48
45
|
chainId: 42431,
|
|
49
46
|
})
|
|
50
47
|
expect(request).toMatchInlineSnapshot(`
|
|
51
48
|
{
|
|
52
49
|
"amount": "1000000",
|
|
53
50
|
"currency": "0x20c0000000000000000000000000000000000001",
|
|
54
|
-
"expires": "2025-01-06T12:00:00Z",
|
|
55
51
|
"methodDetails": {
|
|
56
52
|
"chainId": 42431,
|
|
57
53
|
},
|
|
@@ -66,7 +62,6 @@ describe('fromMethod', () => {
|
|
|
66
62
|
amount: 123,
|
|
67
63
|
currency: '0x20c0000000000000000000000000000000000001',
|
|
68
64
|
recipient: '0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00',
|
|
69
|
-
expires: '2025-01-06T12:00:00Z',
|
|
70
65
|
} as any),
|
|
71
66
|
).toThrowErrorMatchingInlineSnapshot(`
|
|
72
67
|
[$ZodError: [
|
package/src/client/Mppx.test.ts
CHANGED
|
@@ -82,12 +82,12 @@ describe('createCredential', () => {
|
|
|
82
82
|
const challenge = Challenge.fromMethod(Methods.charge, {
|
|
83
83
|
realm,
|
|
84
84
|
secretKey,
|
|
85
|
+
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
85
86
|
request: {
|
|
86
87
|
amount: '1000',
|
|
87
88
|
currency: '0x1234567890123456789012345678901234567890',
|
|
88
89
|
decimals: 6,
|
|
89
90
|
recipient: '0x1234567890123456789012345678901234567890',
|
|
90
|
-
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
91
91
|
},
|
|
92
92
|
})
|
|
93
93
|
|
|
@@ -164,11 +164,11 @@ describe('createCredential', () => {
|
|
|
164
164
|
realm,
|
|
165
165
|
method: 'stripe',
|
|
166
166
|
intent: 'charge',
|
|
167
|
+
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
167
168
|
request: {
|
|
168
169
|
amount: '2000',
|
|
169
170
|
currency: '0xabcd',
|
|
170
171
|
recipient: '0xefgh',
|
|
171
|
-
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
172
172
|
},
|
|
173
173
|
})
|
|
174
174
|
|
|
@@ -195,12 +195,12 @@ describe('createCredential', () => {
|
|
|
195
195
|
const challenge = Challenge.fromMethod(Methods.charge, {
|
|
196
196
|
realm,
|
|
197
197
|
secretKey,
|
|
198
|
+
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
198
199
|
request: {
|
|
199
200
|
amount: '1000',
|
|
200
201
|
currency: '0x1234567890123456789012345678901234567890',
|
|
201
202
|
decimals: 6,
|
|
202
203
|
recipient: '0x1234567890123456789012345678901234567890',
|
|
203
|
-
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
204
204
|
},
|
|
205
205
|
})
|
|
206
206
|
|
|
@@ -227,12 +227,12 @@ describe('createCredential', () => {
|
|
|
227
227
|
const challenge = Challenge.fromMethod(Methods.charge, {
|
|
228
228
|
realm,
|
|
229
229
|
secretKey,
|
|
230
|
+
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
230
231
|
request: {
|
|
231
232
|
amount: '1000',
|
|
232
233
|
currency: '0x1234567890123456789012345678901234567890',
|
|
233
234
|
decimals: 6,
|
|
234
235
|
recipient: '0x1234567890123456789012345678901234567890',
|
|
235
|
-
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
236
236
|
},
|
|
237
237
|
})
|
|
238
238
|
|
|
@@ -258,12 +258,12 @@ describe('createCredential', () => {
|
|
|
258
258
|
const challenge = Challenge.fromMethod(Methods.charge, {
|
|
259
259
|
realm,
|
|
260
260
|
secretKey,
|
|
261
|
+
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
261
262
|
request: {
|
|
262
263
|
amount: '1000',
|
|
263
264
|
currency: '0x1234567890123456789012345678901234567890',
|
|
264
265
|
decimals: 6,
|
|
265
266
|
recipient: '0x1234567890123456789012345678901234567890',
|
|
266
|
-
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
267
267
|
},
|
|
268
268
|
})
|
|
269
269
|
|
|
@@ -9,12 +9,12 @@ const secretKey = 'test-secret-key'
|
|
|
9
9
|
const challenge = Challenge.fromMethod(Methods.charge, {
|
|
10
10
|
realm,
|
|
11
11
|
secretKey,
|
|
12
|
+
expires: '2025-01-01T00:00:00.000Z',
|
|
12
13
|
request: {
|
|
13
14
|
amount: '0.001',
|
|
14
15
|
currency: '0x20c0000000000000000000000000000000000001',
|
|
15
16
|
decimals: 6,
|
|
16
17
|
recipient: '0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00',
|
|
17
|
-
expires: '2025-01-01T00:00:00.000Z',
|
|
18
18
|
},
|
|
19
19
|
})
|
|
20
20
|
|
|
@@ -60,14 +60,13 @@ describe('http', () => {
|
|
|
60
60
|
expect(transport.getChallenge(response)).toMatchInlineSnapshot(`
|
|
61
61
|
{
|
|
62
62
|
"expires": "2025-01-01T00:00:00.000Z",
|
|
63
|
-
"id": "
|
|
63
|
+
"id": "0hnrySRDqWfttlDIJpuxV4mJsRJIS7d7RjnufuonJOE",
|
|
64
64
|
"intent": "charge",
|
|
65
65
|
"method": "tempo",
|
|
66
66
|
"realm": "api.example.com",
|
|
67
67
|
"request": {
|
|
68
68
|
"amount": "1000",
|
|
69
69
|
"currency": "0x20c0000000000000000000000000000000000001",
|
|
70
|
-
"expires": "2025-01-01T00:00:00.000Z",
|
|
71
70
|
"recipient": "0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00",
|
|
72
71
|
},
|
|
73
72
|
}
|
|
@@ -91,7 +90,7 @@ describe('http', () => {
|
|
|
91
90
|
const headers = result.headers as Headers
|
|
92
91
|
|
|
93
92
|
expect(headers.get('Authorization')).toMatchInlineSnapshot(
|
|
94
|
-
`"Payment
|
|
93
|
+
`"Payment eyJjaGFsbGVuZ2UiOnsiZXhwaXJlcyI6IjIwMjUtMDEtMDFUMDA6MDA6MDAuMDAwWiIsImlkIjoiMGhucnlTUkRxV2Z0dGxESUpwdXhWNG1Kc1JKSVM3ZDdSam51ZnVvbkpPRSIsImludGVudCI6ImNoYXJnZSIsIm1ldGhvZCI6InRlbXBvIiwicmVhbG0iOiJhcGkuZXhhbXBsZS5jb20iLCJyZXF1ZXN0IjoiZXlKaGJXOTFiblFpT2lJeE1EQXdJaXdpWTNWeWNtVnVZM2tpT2lJd2VESXdZekF3TURBd01EQXdNREF3TURBd01EQXdNREF3TURBd01EQXdNREF3TURBd01EQXdNREVpTENKeVpXTnBjR2xsYm5RaU9pSXdlRGMwTW1Rek5VTmpOall6TkVNd05UTXlPVEkxWVROaU9EUTBRbU01WlRjMU9UVm1PR1pGTURBaWZRIn0sInBheWxvYWQiOnsic2lnbmF0dXJlIjoiMHhhYmMxMjMiLCJ0eXBlIjoidHJhbnNhY3Rpb24ifX0"`,
|
|
95
94
|
)
|
|
96
95
|
})
|
|
97
96
|
|
|
@@ -182,14 +181,13 @@ describe('mcp', () => {
|
|
|
182
181
|
expect(transport.getChallenge(response)).toMatchInlineSnapshot(`
|
|
183
182
|
{
|
|
184
183
|
"expires": "2025-01-01T00:00:00.000Z",
|
|
185
|
-
"id": "
|
|
184
|
+
"id": "0hnrySRDqWfttlDIJpuxV4mJsRJIS7d7RjnufuonJOE",
|
|
186
185
|
"intent": "charge",
|
|
187
186
|
"method": "tempo",
|
|
188
187
|
"realm": "api.example.com",
|
|
189
188
|
"request": {
|
|
190
189
|
"amount": "1000",
|
|
191
190
|
"currency": "0x20c0000000000000000000000000000000000001",
|
|
192
|
-
"expires": "2025-01-01T00:00:00.000Z",
|
|
193
191
|
"recipient": "0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00",
|
|
194
192
|
},
|
|
195
193
|
}
|
|
@@ -239,14 +237,13 @@ describe('mcp', () => {
|
|
|
239
237
|
"org.paymentauth/credential": {
|
|
240
238
|
"challenge": {
|
|
241
239
|
"expires": "2025-01-01T00:00:00.000Z",
|
|
242
|
-
"id": "
|
|
240
|
+
"id": "0hnrySRDqWfttlDIJpuxV4mJsRJIS7d7RjnufuonJOE",
|
|
243
241
|
"intent": "charge",
|
|
244
242
|
"method": "tempo",
|
|
245
243
|
"realm": "api.example.com",
|
|
246
244
|
"request": {
|
|
247
245
|
"amount": "1000",
|
|
248
246
|
"currency": "0x20c0000000000000000000000000000000000001",
|
|
249
|
-
"expires": "2025-01-01T00:00:00.000Z",
|
|
250
247
|
"recipient": "0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00",
|
|
251
248
|
},
|
|
252
249
|
},
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
2
|
+
import * as Fetch from './Fetch.js'
|
|
3
|
+
|
|
4
|
+
const noopMethod = {
|
|
5
|
+
name: 'test',
|
|
6
|
+
intent: 'test',
|
|
7
|
+
context: undefined,
|
|
8
|
+
createCredential: async () => 'credential',
|
|
9
|
+
} as any
|
|
10
|
+
|
|
11
|
+
function make402() {
|
|
12
|
+
const request = btoa(JSON.stringify({ amount: '1' }))
|
|
13
|
+
.replace(/\+/g, '-')
|
|
14
|
+
.replace(/\//g, '_')
|
|
15
|
+
.replace(/=+$/, '')
|
|
16
|
+
return new Response(null, {
|
|
17
|
+
status: 402,
|
|
18
|
+
headers: {
|
|
19
|
+
'WWW-Authenticate': `Payment id="abc", realm="test", method="test", intent="test", request="${request}"`,
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Returns a fetch wrapper and the init captured from the 402 retry call. */
|
|
25
|
+
function setup() {
|
|
26
|
+
const calls: (RequestInit | undefined)[] = []
|
|
27
|
+
let callCount = 0
|
|
28
|
+
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
29
|
+
calls.push(init)
|
|
30
|
+
callCount++
|
|
31
|
+
if (callCount === 1) return make402()
|
|
32
|
+
return new Response('OK', { status: 200 })
|
|
33
|
+
}
|
|
34
|
+
const fetch = Fetch.from({ fetch: mockFetch, methods: [noopMethod] })
|
|
35
|
+
return {
|
|
36
|
+
fetch,
|
|
37
|
+
/** Headers sent on the retry (second) request. */
|
|
38
|
+
retryHeaders: async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
39
|
+
await fetch(input, init)
|
|
40
|
+
return (calls[1] as Record<string, unknown>)?.headers as Record<string, string>
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('Fetch.from: browser header normalization', () => {
|
|
46
|
+
test('preserves Headers instance', async () => {
|
|
47
|
+
const { retryHeaders } = setup()
|
|
48
|
+
const h = await retryHeaders('https://example.com', {
|
|
49
|
+
headers: new Headers({ 'X-Custom': 'value', 'Content-Type': 'application/json' }),
|
|
50
|
+
})
|
|
51
|
+
expect(h['x-custom']).toBe('value')
|
|
52
|
+
expect(h['content-type']).toBe('application/json')
|
|
53
|
+
expect(h.Authorization).toBe('credential')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('preserves header tuples', async () => {
|
|
57
|
+
const { retryHeaders } = setup()
|
|
58
|
+
const h = await retryHeaders('https://example.com', {
|
|
59
|
+
headers: [
|
|
60
|
+
['X-Custom', 'value'],
|
|
61
|
+
['Accept', 'application/json'],
|
|
62
|
+
],
|
|
63
|
+
})
|
|
64
|
+
expect(h['X-Custom']).toBe('value')
|
|
65
|
+
expect(h.Accept).toBe('application/json')
|
|
66
|
+
expect(h.Authorization).toBe('credential')
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('replaces authorization case-insensitively', async () => {
|
|
70
|
+
const { retryHeaders } = setup()
|
|
71
|
+
const h = await retryHeaders('https://example.com', {
|
|
72
|
+
headers: { authorization: 'Bearer stale', 'X-Custom': 'value' },
|
|
73
|
+
})
|
|
74
|
+
expect(h.authorization).toBeUndefined()
|
|
75
|
+
expect(h.Authorization).toBe('credential')
|
|
76
|
+
expect(h['X-Custom']).toBe('value')
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('preserves plain object headers', async () => {
|
|
80
|
+
const { retryHeaders } = setup()
|
|
81
|
+
const h = await retryHeaders('https://example.com', { headers: { 'X-Custom': 'val' } })
|
|
82
|
+
expect(h['X-Custom']).toBe('val')
|
|
83
|
+
expect(h.Authorization).toBe('credential')
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('adds Authorization when no headers provided', async () => {
|
|
87
|
+
const { retryHeaders } = setup()
|
|
88
|
+
const h = await retryHeaders('https://example.com')
|
|
89
|
+
expect(h.Authorization).toBe('credential')
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
describe('Fetch.polyfill / restore: browser', () => {
|
|
94
|
+
test('restore is a no-op when polyfill was never called', () => {
|
|
95
|
+
const before = globalThis.fetch
|
|
96
|
+
Fetch.restore()
|
|
97
|
+
expect(globalThis.fetch).toBe(before)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('restore reverts to original fetch', () => {
|
|
101
|
+
const original = globalThis.fetch
|
|
102
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
103
|
+
expect(globalThis.fetch).not.toBe(original)
|
|
104
|
+
Fetch.restore()
|
|
105
|
+
expect(globalThis.fetch).toBe(original)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test('stacked polyfill calls preserve the true original', () => {
|
|
109
|
+
const original = globalThis.fetch
|
|
110
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
111
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
112
|
+
Fetch.restore()
|
|
113
|
+
expect(globalThis.fetch).toBe(original)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('double restore does not clobber fetch', () => {
|
|
117
|
+
const original = globalThis.fetch
|
|
118
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
119
|
+
Fetch.restore()
|
|
120
|
+
Fetch.restore()
|
|
121
|
+
expect(globalThis.fetch).toBe(original)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('restore is a no-op when fetch was replaced externally', () => {
|
|
125
|
+
const original = globalThis.fetch
|
|
126
|
+
const external = vi.fn(
|
|
127
|
+
async () => new Response('external'),
|
|
128
|
+
) as unknown as typeof globalThis.fetch
|
|
129
|
+
Fetch.polyfill({ methods: [noopMethod] })
|
|
130
|
+
globalThis.fetch = external
|
|
131
|
+
Fetch.restore()
|
|
132
|
+
expect(globalThis.fetch).toBe(external)
|
|
133
|
+
globalThis.fetch = original
|
|
134
|
+
})
|
|
135
|
+
})
|
|
@@ -552,94 +552,6 @@ describe('Fetch.from: 402 retry path', () => {
|
|
|
552
552
|
})
|
|
553
553
|
})
|
|
554
554
|
|
|
555
|
-
describe('Fetch.from: 402 retry headers normalization', () => {
|
|
556
|
-
test('preserves headers when passed as a Headers instance', async () => {
|
|
557
|
-
let callCount = 0
|
|
558
|
-
const calls: { init: RequestInit | undefined }[] = []
|
|
559
|
-
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
560
|
-
calls.push({ init })
|
|
561
|
-
callCount++
|
|
562
|
-
if (callCount === 1) return make402()
|
|
563
|
-
return new Response('OK', { status: 200 })
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
const fetch = Fetch.from({
|
|
567
|
-
fetch: mockFetch,
|
|
568
|
-
methods: [noopMethod],
|
|
569
|
-
})
|
|
570
|
-
|
|
571
|
-
const headers = new Headers({ 'X-Custom': 'value', 'Content-Type': 'application/json' })
|
|
572
|
-
await fetch('https://example.com/api', { headers })
|
|
573
|
-
|
|
574
|
-
const retryHeaders = (calls[1]!.init as Record<string, unknown>).headers as Record<
|
|
575
|
-
string,
|
|
576
|
-
string
|
|
577
|
-
>
|
|
578
|
-
expect(retryHeaders['x-custom']).toBe('value')
|
|
579
|
-
expect(retryHeaders['content-type']).toBe('application/json')
|
|
580
|
-
expect(retryHeaders.Authorization).toBe('credential')
|
|
581
|
-
})
|
|
582
|
-
|
|
583
|
-
test('preserves headers when passed as array of tuples', async () => {
|
|
584
|
-
let callCount = 0
|
|
585
|
-
const calls: { init: RequestInit | undefined }[] = []
|
|
586
|
-
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
587
|
-
calls.push({ init })
|
|
588
|
-
callCount++
|
|
589
|
-
if (callCount === 1) return make402()
|
|
590
|
-
return new Response('OK', { status: 200 })
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
const fetch = Fetch.from({
|
|
594
|
-
fetch: mockFetch,
|
|
595
|
-
methods: [noopMethod],
|
|
596
|
-
})
|
|
597
|
-
|
|
598
|
-
await fetch('https://example.com/api', {
|
|
599
|
-
headers: [
|
|
600
|
-
['X-Custom', 'value'],
|
|
601
|
-
['Accept', 'application/json'],
|
|
602
|
-
],
|
|
603
|
-
})
|
|
604
|
-
|
|
605
|
-
const retryHeaders = (calls[1]!.init as Record<string, unknown>).headers as Record<
|
|
606
|
-
string,
|
|
607
|
-
string
|
|
608
|
-
>
|
|
609
|
-
expect(retryHeaders['X-Custom']).toBe('value')
|
|
610
|
-
expect(retryHeaders.Accept).toBe('application/json')
|
|
611
|
-
expect(retryHeaders.Authorization).toBe('credential')
|
|
612
|
-
})
|
|
613
|
-
|
|
614
|
-
test('replaces existing authorization header case-insensitively', async () => {
|
|
615
|
-
let callCount = 0
|
|
616
|
-
const calls: { init: RequestInit | undefined }[] = []
|
|
617
|
-
const mockFetch: typeof globalThis.fetch = async (_input, init) => {
|
|
618
|
-
calls.push({ init })
|
|
619
|
-
callCount++
|
|
620
|
-
if (callCount === 1) return make402()
|
|
621
|
-
return new Response('OK', { status: 200 })
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
const fetch = Fetch.from({
|
|
625
|
-
fetch: mockFetch,
|
|
626
|
-
methods: [noopMethod],
|
|
627
|
-
})
|
|
628
|
-
|
|
629
|
-
await fetch('https://example.com/api', {
|
|
630
|
-
headers: { authorization: 'Bearer stale-token', 'X-Custom': 'value' },
|
|
631
|
-
})
|
|
632
|
-
|
|
633
|
-
const retryHeaders = (calls[1]!.init as Record<string, unknown>).headers as Record<
|
|
634
|
-
string,
|
|
635
|
-
string
|
|
636
|
-
>
|
|
637
|
-
expect(retryHeaders.authorization).toBeUndefined()
|
|
638
|
-
expect(retryHeaders.Authorization).toBe('credential')
|
|
639
|
-
expect(retryHeaders['X-Custom']).toBe('value')
|
|
640
|
-
})
|
|
641
|
-
})
|
|
642
|
-
|
|
643
555
|
describe('Fetch.from: input passthrough', () => {
|
|
644
556
|
test('passes URL input through on both initial and retry calls', async () => {
|
|
645
557
|
let callCount = 0
|
|
@@ -157,11 +157,11 @@ describe('McpClient.wrap', () => {
|
|
|
157
157
|
const challenge = Challenge.fromMethod(tempo_server.charge({ getClient: () => testClient }), {
|
|
158
158
|
realm,
|
|
159
159
|
secretKey,
|
|
160
|
+
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
160
161
|
request: {
|
|
161
162
|
amount: '1',
|
|
162
163
|
currency: asset,
|
|
163
164
|
decimals: 6,
|
|
164
|
-
expires: new Date(Date.now() + 60_000).toISOString(),
|
|
165
165
|
recipient: accounts[0].address,
|
|
166
166
|
},
|
|
167
167
|
})
|
package/src/server/Mppx.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
|
2
2
|
import * as Challenge from '../Challenge.js'
|
|
3
3
|
import type * as Credential from '../Credential.js'
|
|
4
4
|
import * as Errors from '../Errors.js'
|
|
5
|
+
import * as Expires from '../Expires.js'
|
|
5
6
|
import * as Env from '../internal/env.js'
|
|
6
7
|
import type * as Method from '../Method.js'
|
|
7
8
|
import type * as Receipt from '../Receipt.js'
|
|
@@ -173,7 +174,8 @@ function createMethodFn(parameters: createMethodFn.Parameters): createMethodFn.R
|
|
|
173
174
|
return Object.assign(
|
|
174
175
|
async (input: Transport.InputOf): Promise<MethodFn.Response> => {
|
|
175
176
|
const { description, meta, ...rest } = options
|
|
176
|
-
const expires =
|
|
177
|
+
const expires =
|
|
178
|
+
'expires' in options ? (options.expires as string | undefined) : Expires.minutes(5)
|
|
177
179
|
|
|
178
180
|
// Merge defaults with per-request options
|
|
179
181
|
const merged = { ...defaults, ...rest }
|
|
@@ -10,12 +10,12 @@ const secretKey = 'test-secret-key'
|
|
|
10
10
|
const challenge = Challenge.fromMethod(Methods.charge, {
|
|
11
11
|
realm,
|
|
12
12
|
secretKey,
|
|
13
|
+
expires: '2025-01-01T00:00:00.000Z',
|
|
13
14
|
request: {
|
|
14
15
|
amount: '1000',
|
|
15
16
|
currency: '0x20c0000000000000000000000000000000000001',
|
|
16
17
|
decimals: 6,
|
|
17
18
|
recipient: '0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00',
|
|
18
|
-
expires: '2025-01-01T00:00:00.000Z',
|
|
19
19
|
},
|
|
20
20
|
})
|
|
21
21
|
|
|
@@ -43,14 +43,13 @@ describe('http', () => {
|
|
|
43
43
|
{
|
|
44
44
|
"challenge": {
|
|
45
45
|
"expires": "2025-01-01T00:00:00.000Z",
|
|
46
|
-
"id": "
|
|
46
|
+
"id": "QNLtjAvrKKR0VlEGSIowhULqcGlCDU4fjrP-O7js8XE",
|
|
47
47
|
"intent": "charge",
|
|
48
48
|
"method": "tempo",
|
|
49
49
|
"realm": "api.example.com",
|
|
50
50
|
"request": {
|
|
51
51
|
"amount": "1000000000",
|
|
52
52
|
"currency": "0x20c0000000000000000000000000000000000001",
|
|
53
|
-
"expires": "2025-01-01T00:00:00.000Z",
|
|
54
53
|
"recipient": "0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00",
|
|
55
54
|
},
|
|
56
55
|
},
|
|
@@ -93,7 +92,7 @@ describe('http', () => {
|
|
|
93
92
|
{
|
|
94
93
|
"headers": {
|
|
95
94
|
"cache-control": "no-store",
|
|
96
|
-
"www-authenticate": "Payment id="
|
|
95
|
+
"www-authenticate": "Payment id="QNLtjAvrKKR0VlEGSIowhULqcGlCDU4fjrP-O7js8XE", realm="api.example.com", method="tempo", intent="charge", request="eyJhbW91bnQiOiIxMDAwMDAwMDAwIiwiY3VycmVuY3kiOiIweDIwYzAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDEiLCJyZWNpcGllbnQiOiIweDc0MmQzNUNjNjYzNEMwNTMyOTI1YTNiODQ0QmM5ZTc1OTVmOGZFMDAifQ", expires="2025-01-01T00:00:00.000Z"",
|
|
97
96
|
},
|
|
98
97
|
"status": 402,
|
|
99
98
|
}
|
|
@@ -183,14 +182,13 @@ describe('mcp', () => {
|
|
|
183
182
|
{
|
|
184
183
|
"challenge": {
|
|
185
184
|
"expires": "2025-01-01T00:00:00.000Z",
|
|
186
|
-
"id": "
|
|
185
|
+
"id": "QNLtjAvrKKR0VlEGSIowhULqcGlCDU4fjrP-O7js8XE",
|
|
187
186
|
"intent": "charge",
|
|
188
187
|
"method": "tempo",
|
|
189
188
|
"realm": "api.example.com",
|
|
190
189
|
"request": {
|
|
191
190
|
"amount": "1000000000",
|
|
192
191
|
"currency": "0x20c0000000000000000000000000000000000001",
|
|
193
|
-
"expires": "2025-01-01T00:00:00.000Z",
|
|
194
192
|
"recipient": "0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00",
|
|
195
193
|
},
|
|
196
194
|
},
|
|
@@ -221,14 +219,13 @@ describe('mcp', () => {
|
|
|
221
219
|
"challenges": [
|
|
222
220
|
{
|
|
223
221
|
"expires": "2025-01-01T00:00:00.000Z",
|
|
224
|
-
"id": "
|
|
222
|
+
"id": "QNLtjAvrKKR0VlEGSIowhULqcGlCDU4fjrP-O7js8XE",
|
|
225
223
|
"intent": "charge",
|
|
226
224
|
"method": "tempo",
|
|
227
225
|
"realm": "api.example.com",
|
|
228
226
|
"request": {
|
|
229
227
|
"amount": "1000000000",
|
|
230
228
|
"currency": "0x20c0000000000000000000000000000000000001",
|
|
231
|
-
"expires": "2025-01-01T00:00:00.000Z",
|
|
232
229
|
"recipient": "0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00",
|
|
233
230
|
},
|
|
234
231
|
},
|
|
@@ -262,7 +259,7 @@ describe('mcp', () => {
|
|
|
262
259
|
"result": {
|
|
263
260
|
"_meta": {
|
|
264
261
|
"org.paymentauth/receipt": {
|
|
265
|
-
"challengeId": "
|
|
262
|
+
"challengeId": "QNLtjAvrKKR0VlEGSIowhULqcGlCDU4fjrP-O7js8XE",
|
|
266
263
|
"method": "tempo",
|
|
267
264
|
"reference": "0xtxhash",
|
|
268
265
|
"status": "success",
|