mppx 0.17.6 → 0.18.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 +43 -0
- package/dist/Currency.d.ts +59 -0
- package/dist/Currency.js +60 -0
- package/dist/Errors.d.ts +13 -0
- package/dist/Errors.js +15 -6
- package/dist/cli/cli.js +138 -138
- package/dist/cli/plugins/index.js +12 -12
- package/dist/cli/plugins/tempo.js +13 -1
- package/dist/client/Mppx.js +13 -4
- package/dist/client/internal/Fetch.d.ts +5 -0
- package/dist/client/internal/Fetch.js +16 -5
- package/dist/cloudflare/ChannelStore.d.ts +1 -2
- package/dist/cloudflare/ChannelStore.js +14 -20
- package/dist/cloudflare/Channels.d.ts +20 -2
- package/dist/cloudflare/Channels.js +47 -4
- package/dist/cloudflare/index.d.ts +1 -1
- package/dist/cloudflare/index.js +1 -1
- package/dist/discovery/Discovery.d.ts +116 -9
- package/dist/discovery/Discovery.js +169 -0
- package/dist/evm/server/Charge.js +2 -3
- package/dist/evm/server/Methods.d.ts +1 -1
- package/dist/evm/server/Methods.js +3 -2
- package/dist/extensions/identity/{email-otp/internal → attestation}/Constants.d.ts +4 -1
- package/dist/extensions/identity/{email-otp/internal → attestation}/Constants.js +4 -1
- package/dist/extensions/identity/{email-otp/internal → attestation}/Jwks.d.ts +5 -5
- package/dist/extensions/identity/{email-otp/internal → attestation}/Jwks.js +1 -1
- package/dist/extensions/identity/{email-otp/internal/Attestation.d.ts → attestation/Token.d.ts} +40 -26
- package/dist/extensions/identity/{email-otp/internal/Attestation.js → attestation/Token.js} +18 -3
- package/dist/extensions/identity/{email-otp/internal → attestation}/Types.d.ts +14 -14
- package/dist/extensions/identity/attestation/client/index.d.ts +66 -0
- package/dist/extensions/identity/attestation/client/index.js +273 -0
- package/dist/extensions/identity/attestation/index.d.ts +20 -0
- package/dist/extensions/identity/attestation/index.js +18 -0
- package/dist/extensions/identity/attestation/internal/Cache.d.ts +120 -0
- package/dist/extensions/identity/attestation/internal/Cache.js +90 -0
- package/dist/extensions/identity/attestation/server/index.d.ts +66 -0
- package/dist/extensions/identity/attestation/server/index.js +119 -0
- package/dist/extensions/identity/email-otp/Constants.d.ts +5 -5
- package/dist/extensions/identity/email-otp/Constants.js +6 -5
- package/dist/extensions/identity/email-otp/Types.d.ts +4 -4
- package/dist/extensions/identity/email-otp/client/index.d.ts +22 -15
- package/dist/extensions/identity/email-otp/client/index.js +21 -9
- package/dist/extensions/identity/email-otp/index.d.ts +3 -8
- package/dist/extensions/identity/email-otp/index.js +3 -8
- package/dist/extensions/identity/email-otp/server/index.d.ts +16 -16
- package/dist/extensions/identity/email-otp/server/index.js +17 -14
- package/dist/extensions/identity/index.d.ts +6 -0
- package/dist/extensions/identity/index.js +6 -0
- package/dist/extensions/identity/internal/Verification.js +9 -5
- package/dist/extensions/identity/link/Constants.d.ts +102 -0
- package/dist/extensions/identity/link/Constants.js +16 -0
- package/dist/extensions/identity/link/client/index.d.ts +80 -0
- package/dist/extensions/identity/link/client/index.js +211 -0
- package/dist/extensions/identity/link/index.d.ts +12 -0
- package/dist/extensions/identity/link/index.js +12 -0
- package/dist/extensions/identity/link/server/index.d.ts +124 -0
- package/dist/extensions/identity/link/server/index.js +518 -0
- package/dist/extensions/identity/persona/Constants.d.ts +24 -0
- package/dist/extensions/identity/persona/Constants.js +24 -0
- package/dist/extensions/identity/persona/Did.d.ts +24 -0
- package/dist/extensions/identity/persona/Did.js +24 -0
- package/dist/extensions/identity/persona/Types.d.ts +21 -0
- package/dist/extensions/identity/persona/Types.js +2 -0
- package/dist/extensions/identity/persona/index.d.ts +10 -0
- package/dist/extensions/identity/persona/index.js +9 -0
- package/dist/extensions/identity/persona/server/index.d.ts +152 -0
- package/dist/extensions/identity/persona/server/index.js +263 -0
- package/dist/identity/Callback.d.ts +24 -5
- package/dist/identity/Callback.js +35 -11
- package/dist/identity/Client.d.ts +78 -5
- package/dist/identity/Client.js +165 -48
- package/dist/identity/Headers.d.ts +21 -0
- package/dist/identity/Headers.js +24 -0
- package/dist/identity/Server.d.ts +159 -37
- package/dist/identity/Server.js +309 -138
- package/dist/identity/Status.d.ts +14 -0
- package/dist/identity/Status.js +19 -0
- package/dist/identity/Types.d.ts +166 -65
- package/dist/identity/index.d.ts +2 -0
- package/dist/identity/index.js +2 -0
- package/dist/identity/internal/Requirements.d.ts +28 -0
- package/dist/identity/internal/Requirements.js +78 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/integrations/x402/Evm.d.ts +1 -0
- package/dist/internal/env.d.ts +14 -0
- package/dist/internal/env.js +38 -0
- package/dist/internal/version.d.ts +2 -2
- package/dist/internal/version.js +1 -1
- package/dist/proxy/internal/Headers.d.ts +1 -1
- package/dist/proxy/internal/Headers.js +11 -7
- package/dist/server/Mppx.d.ts +14 -6
- package/dist/server/Mppx.js +256 -149
- package/dist/server/internal/challengeMeta.d.ts +0 -6
- package/dist/server/internal/challengeMeta.js +3 -17
- package/dist/stripe/server/internal/html.gen.js +1 -1
- package/dist/tempo/client/Charge.d.ts +11 -8
- package/dist/tempo/client/Charge.js +53 -6
- package/dist/tempo/index.d.ts +4 -0
- package/dist/tempo/index.js +4 -0
- package/dist/tempo/internal/auto-swap.d.ts +6 -5
- package/dist/tempo/internal/auto-swap.js +17 -2
- package/dist/tempo/internal/currencies.d.ts +5 -4
- package/dist/tempo/internal/currencies.js +13 -9
- package/dist/tempo/internal/defaults.d.ts +7 -0
- package/dist/tempo/internal/defaults.js +7 -0
- package/dist/tempo/internal/fee-payer.js +8 -2
- package/dist/tempo/internal/machine-token-charge.d.ts +52 -0
- package/dist/tempo/internal/machine-token-charge.js +82 -1
- package/dist/tempo/server/Charge.d.ts +14 -9
- package/dist/tempo/server/Charge.js +64 -3
- package/dist/tempo/server/Methods.d.ts +1 -1
- package/dist/tempo/server/Methods.js +4 -1
- package/dist/tempo/server/Subscription.d.ts +8 -0
- package/dist/tempo/server/internal/fallback-store.d.ts +7 -2
- package/dist/tempo/server/internal/fallback-store.js +12 -3
- package/dist/tempo/server/internal/html.gen.js +1 -1
- package/dist/tempo/server/internal/transport.d.ts +1 -1
- package/dist/tempo/server/internal/transport.js +9 -16
- package/dist/tempo/session/client/CredentialState.js +31 -0
- package/dist/tempo/session/client/Session.d.ts +12 -6
- package/dist/tempo/session/client/Session.js +64 -18
- package/dist/tempo/session/server/ChannelStore.d.ts +22 -1
- package/dist/tempo/session/server/ChannelStore.js +58 -8
- package/dist/tempo/session/server/CloseSupervisor.d.ts +5 -1
- package/dist/tempo/session/server/CloseSupervisor.js +110 -12
- package/dist/tempo/session/server/CredentialVerification.d.ts +2 -0
- package/dist/tempo/session/server/CredentialVerification.js +6 -32
- package/dist/tempo/session/server/Session.d.ts +5 -1
- package/dist/tempo/session/server/Session.js +6 -3
- package/dist/tempo/session/server/Settlement.d.ts +7 -0
- package/dist/tempo/session/server/Settlement.js +36 -6
- package/dist/tempo/session/server/Sse.d.ts +2 -0
- package/dist/tempo/session/server/Sse.js +12 -12
- package/dist/tempo/subscription/KeyAuthorization.d.ts +1 -1
- package/dist/tempo/subscription/KeyAuthorization.js +1 -1
- package/dist/x402/Assets.d.ts +14 -5
- package/dist/x402/Assets.js +11 -16
- package/package.json +19 -3
- package/dist/extensions/identity/email-otp/internal/Prover.d.ts +0 -46
- package/dist/extensions/identity/email-otp/internal/Prover.js +0 -168
- package/dist/extensions/identity/email-otp/internal/Verifier.d.ts +0 -56
- package/dist/extensions/identity/email-otp/internal/Verifier.js +0 -116
- /package/dist/extensions/identity/{email-otp/internal → attestation}/Types.js +0 -0
|
@@ -80,4 +80,173 @@ export const DiscoveryDocument = z.looseObject({
|
|
|
80
80
|
'x-service-info': z.optional(ServiceInfo),
|
|
81
81
|
paths: z.optional(z.record(z.string(), PathItem)),
|
|
82
82
|
});
|
|
83
|
+
const operationMethods = [
|
|
84
|
+
'get',
|
|
85
|
+
'post',
|
|
86
|
+
'put',
|
|
87
|
+
'patch',
|
|
88
|
+
'delete',
|
|
89
|
+
'head',
|
|
90
|
+
'options',
|
|
91
|
+
'trace',
|
|
92
|
+
];
|
|
93
|
+
/**
|
|
94
|
+
* Lists every operation in a discovery document with its offers and JSON request schema.
|
|
95
|
+
*
|
|
96
|
+
* Accepts a raw `openapi.json` body or an already parsed {@link DiscoveryDocument}, and throws
|
|
97
|
+
* when the document is invalid. Prices are advisory: the 402 Challenge stays authoritative.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* import { listOperations } from 'mppx/discovery'
|
|
102
|
+
*
|
|
103
|
+
* const doc = await fetch('https://api.example.com/openapi.json').then((r) => r.json())
|
|
104
|
+
* for (const { method, path, offers } of listOperations(doc))
|
|
105
|
+
* console.log(method, path, offers[0]?.amount)
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export function listOperations(document) {
|
|
109
|
+
const doc = DiscoveryDocument.parse(document);
|
|
110
|
+
const resolve = resolver(doc);
|
|
111
|
+
const merged = new WeakMap();
|
|
112
|
+
const operations = [];
|
|
113
|
+
for (const [path, entry] of Object.entries(doc.paths ?? {})) {
|
|
114
|
+
// OpenAPI paths start with `/` and are relative to the server; skip anything else, such as an
|
|
115
|
+
// absolute URL, rather than hand a client a path that points somewhere unexpected.
|
|
116
|
+
if (!path.startsWith('/') || path.startsWith('//'))
|
|
117
|
+
continue;
|
|
118
|
+
// A Path Item can itself be a `$ref`, such as to `#/components/pathItems/...`.
|
|
119
|
+
// A Path Item `$ref` merges with fields declared next to it (OpenAPI 3.1 leaves conflicts
|
|
120
|
+
// undefined; inline fields win). An invalid target throws, as an invalid inline Path Item does;
|
|
121
|
+
// an unresolvable one contributes nothing.
|
|
122
|
+
let item = entry;
|
|
123
|
+
if (entry.$ref !== undefined) {
|
|
124
|
+
const { $ref: _, ...inline } = entry;
|
|
125
|
+
const target = resolve(entry);
|
|
126
|
+
item = { ...(target === undefined ? {} : PathItem.parse(target)), ...inline };
|
|
127
|
+
}
|
|
128
|
+
for (const method of operationMethods) {
|
|
129
|
+
const operation = item[method];
|
|
130
|
+
if (!operation)
|
|
131
|
+
continue;
|
|
132
|
+
operations.push({
|
|
133
|
+
method,
|
|
134
|
+
path,
|
|
135
|
+
offers: operation['x-payment-info']?.offers ?? [],
|
|
136
|
+
requestSchema: jsonSchema(resolve, merged, operation.requestBody),
|
|
137
|
+
summary: operation.summary,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return operations;
|
|
142
|
+
}
|
|
143
|
+
/** The `application/json` schema of an OpenAPI request body, if any, following local `$ref`s. */
|
|
144
|
+
function jsonSchema(resolve, merged, requestBody) {
|
|
145
|
+
const body = resolve(requestBody);
|
|
146
|
+
const raw = jsonContent(body?.content)?.schema;
|
|
147
|
+
const schema = resolve(raw);
|
|
148
|
+
if (typeof raw === 'boolean')
|
|
149
|
+
return raw;
|
|
150
|
+
if (typeof schema !== 'boolean' && !isObject(schema))
|
|
151
|
+
return undefined;
|
|
152
|
+
if (!isObject(raw))
|
|
153
|
+
return undefined;
|
|
154
|
+
// JSON Schema 2020-12 applies keywords next to `$ref`: keep them alongside the target.
|
|
155
|
+
const object = raw;
|
|
156
|
+
if (object === schema)
|
|
157
|
+
return schema;
|
|
158
|
+
// `false` rejects everything whatever its siblings say; a `true` target adds nothing to them.
|
|
159
|
+
if (schema === false)
|
|
160
|
+
return false;
|
|
161
|
+
// Operations that share a schema object share one merged result, copied once.
|
|
162
|
+
const cached = merged.get(object);
|
|
163
|
+
if (cached)
|
|
164
|
+
return cached;
|
|
165
|
+
const { $ref: _, ...siblings } = object;
|
|
166
|
+
const allOf = Array.isArray(siblings.allOf) ? siblings.allOf : [];
|
|
167
|
+
const result = Object.keys(siblings).length === 0
|
|
168
|
+
? schema
|
|
169
|
+
: schema === true
|
|
170
|
+
? siblings
|
|
171
|
+
: { ...siblings, allOf: [schema, ...allOf] };
|
|
172
|
+
merged.set(object, result);
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Returns a function that follows Reference Objects to local JSON Pointers (`#/components/...`)
|
|
177
|
+
* in the document. Nested references inside a result stay as they are, and a reference that is
|
|
178
|
+
* remote, missing or circular resolves to `undefined`. Results are cached per reference, so
|
|
179
|
+
* operations that share a reference chain walk it once.
|
|
180
|
+
*/
|
|
181
|
+
function resolver(doc) {
|
|
182
|
+
const cache = new Map();
|
|
183
|
+
function resolveValue(value) {
|
|
184
|
+
// A loop rather than recursion, so a long chain can't overflow the stack.
|
|
185
|
+
const chain = new Set();
|
|
186
|
+
let current = value;
|
|
187
|
+
let result;
|
|
188
|
+
for (;;) {
|
|
189
|
+
const ref = current?.$ref;
|
|
190
|
+
if (typeof ref !== 'string') {
|
|
191
|
+
result = current;
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
if (cache.has(ref)) {
|
|
195
|
+
result = cache.get(ref);
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
if (!ref.startsWith('#') || chain.has(ref)) {
|
|
199
|
+
result = undefined;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
chain.add(ref);
|
|
203
|
+
current = pointer(ref);
|
|
204
|
+
}
|
|
205
|
+
for (const ref of chain)
|
|
206
|
+
cache.set(ref, result);
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
function pointer(ref) {
|
|
210
|
+
// RFC 6901: percent-decode the URI fragment first, then split it into reference tokens.
|
|
211
|
+
const fragment = decodeFragment(ref.slice(1));
|
|
212
|
+
if (fragment === undefined || !fragment.startsWith('/'))
|
|
213
|
+
return undefined;
|
|
214
|
+
let target = doc;
|
|
215
|
+
for (const part of fragment.slice(1).split('/')) {
|
|
216
|
+
const key = part.replaceAll('~1', '/').replaceAll('~0', '~');
|
|
217
|
+
if (!target || typeof target !== 'object' || !Object.hasOwn(target, key))
|
|
218
|
+
return undefined;
|
|
219
|
+
target = target[key];
|
|
220
|
+
}
|
|
221
|
+
return target;
|
|
222
|
+
}
|
|
223
|
+
return resolveValue;
|
|
224
|
+
}
|
|
225
|
+
/** Percent-decodes a URI fragment, or `undefined` when its encoding is malformed. */
|
|
226
|
+
function decodeFragment(fragment) {
|
|
227
|
+
try {
|
|
228
|
+
return decodeURIComponent(fragment);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/** Whether a value is a plain JSON object, not an array or `null`. */
|
|
235
|
+
function isObject(value) {
|
|
236
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The `application/json` entry of an OpenAPI `content` map. Media type keys match
|
|
240
|
+
* case-insensitively and may carry parameters, such as `application/json; charset=utf-8`.
|
|
241
|
+
*/
|
|
242
|
+
function jsonContent(content) {
|
|
243
|
+
if (!isObject(content))
|
|
244
|
+
return undefined;
|
|
245
|
+
if (Object.hasOwn(content, 'application/json'))
|
|
246
|
+
return content['application/json'];
|
|
247
|
+
for (const [type, entry] of Object.entries(content))
|
|
248
|
+
if (type.split(';')[0].trim().toLowerCase() === 'application/json')
|
|
249
|
+
return entry;
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
83
252
|
//# sourceMappingURL=Discovery.js.map
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getAddress, recoverTypedDataAddress } from 'viem';
|
|
2
|
+
import * as Currency from '../../Currency.js';
|
|
2
3
|
import { VerificationFailedError } from '../../Errors.js';
|
|
3
4
|
import * as Method from '../../Method.js';
|
|
4
5
|
import * as Receipt from '../../Receipt.js';
|
|
@@ -116,9 +117,7 @@ function resolveConfig(config) {
|
|
|
116
117
|
let authorization = config.authorization;
|
|
117
118
|
let chainId = config.chainId;
|
|
118
119
|
let decimals = config.decimals;
|
|
119
|
-
|
|
120
|
-
chainId ??= Assets.toChainId(currency.network);
|
|
121
|
-
}
|
|
120
|
+
chainId ??= Currency.chainIdOf(currency);
|
|
122
121
|
if (chainId !== undefined) {
|
|
123
122
|
const resolved = Assets.resolve(currency, Types.networkOf(chainId));
|
|
124
123
|
if (!resolved)
|
|
@@ -219,7 +219,7 @@ export declare function evm<const parameters extends evm.Parameters>(parameters:
|
|
|
219
219
|
};
|
|
220
220
|
}, charge_.Defaults, import("../../server/Transport.js").Http, {}, string | undefined>];
|
|
221
221
|
export declare namespace evm {
|
|
222
|
-
/**
|
|
222
|
+
/** An accepted EVM currency: an address, a `viem/tokens` token (chain-indexed or resolved), or a known asset. */
|
|
223
223
|
type Currency = Assets.Currency;
|
|
224
224
|
/** Shared EVM settlement parameters and ordered accepted currencies. */
|
|
225
225
|
type Parameters = Omit<charge_.Parameters, 'currency'> & ({
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as Currency from '../../Currency.js';
|
|
1
2
|
import * as Assets from '../Assets.js';
|
|
2
3
|
import * as Chains from '../Chains.js';
|
|
3
4
|
import { charge as charge_ } from './Charge.js';
|
|
@@ -34,14 +35,14 @@ export function evm(parameters) {
|
|
|
34
35
|
const seen = new Set();
|
|
35
36
|
const methods = [];
|
|
36
37
|
for (const currency of currencies) {
|
|
37
|
-
const chainId = shared.chainId ??
|
|
38
|
+
const chainId = shared.chainId ?? Currency.chainIdOf(currency);
|
|
38
39
|
if (chainId === undefined)
|
|
39
40
|
throw new Error('EVM currencies require `chainId` or known asset metadata.');
|
|
40
41
|
const resolved = Assets.resolve(currency, Assets.toNetwork(chainId));
|
|
41
42
|
if (!resolved)
|
|
42
43
|
continue;
|
|
43
44
|
// One shared price cannot price non-USD or unknown-denomination token definitions.
|
|
44
|
-
if (Assets.
|
|
45
|
+
if (typeof currency !== 'string' && !Assets.isAsset(currency) && currency.currency !== 'USD')
|
|
45
46
|
continue;
|
|
46
47
|
const key = `${chainId}:${resolved.address.toLowerCase()}`;
|
|
47
48
|
if (seen.has(key))
|
|
@@ -12,7 +12,10 @@ export declare const Constants: {
|
|
|
12
12
|
readonly jwksPath: '/.well-known/jwks.json';
|
|
13
13
|
/** Clock tolerance, in seconds, applied to `iat` and `exp`. */
|
|
14
14
|
readonly clockTolerance: 60;
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Seconds before expiry at which a client stops sending an attestation on unpaid attempts and
|
|
17
|
+
* replaces it, capped at half the attestation's lifetime.
|
|
18
|
+
*/
|
|
16
19
|
readonly refreshMargin: 300;
|
|
17
20
|
/** Longest attestation accepted, in characters. */
|
|
18
21
|
readonly maxLength: 8192;
|
|
@@ -12,7 +12,10 @@ export const Constants = {
|
|
|
12
12
|
jwksPath: '/.well-known/jwks.json',
|
|
13
13
|
/** Clock tolerance, in seconds, applied to `iat` and `exp`. */
|
|
14
14
|
clockTolerance: 60,
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Seconds before expiry at which a client stops sending an attestation on unpaid attempts and
|
|
17
|
+
* replaces it, capped at half the attestation's lifetime.
|
|
18
|
+
*/
|
|
16
19
|
refreshMargin: 300,
|
|
17
20
|
/** Longest attestation accepted, in characters. */
|
|
18
21
|
maxLength: 8_192,
|
|
@@ -13,18 +13,18 @@ export declare function resolver(options?: resolver.Options): Types.KeyResolver;
|
|
|
13
13
|
export declare namespace resolver {
|
|
14
14
|
type Options = {
|
|
15
15
|
/** Fetch implementation. @default globalThis.fetch */
|
|
16
|
-
fetch?: typeof globalThis.fetch | undefined;
|
|
16
|
+
readonly fetch?: typeof globalThis.fetch | undefined;
|
|
17
17
|
/** Seconds a fetched key set is cached. @default 300 */
|
|
18
|
-
ttl?: number | undefined;
|
|
18
|
+
readonly ttl?: number | undefined;
|
|
19
19
|
/** Seconds before a key set fetch is abandoned. @default 5 */
|
|
20
|
-
timeout?: number | undefined;
|
|
20
|
+
readonly timeout?: number | undefined;
|
|
21
21
|
/** Minimum seconds between refetches for an unknown key ID, per issuer. @default 30 */
|
|
22
|
-
refreshInterval?: number | undefined;
|
|
22
|
+
readonly refreshInterval?: number | undefined;
|
|
23
23
|
/**
|
|
24
24
|
* Seconds after its fetch that the last good key set keeps verifying while
|
|
25
25
|
* the issuer is unreachable, so a rotated-out key stops working. @default 3600
|
|
26
26
|
*/
|
|
27
|
-
maxStale?: number | undefined;
|
|
27
|
+
readonly maxStale?: number | undefined;
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
//# sourceMappingURL=Jwks.d.ts.map
|
package/dist/extensions/identity/{email-otp/internal/Attestation.d.ts → attestation/Token.d.ts}
RENAMED
|
@@ -2,17 +2,28 @@ import type * as Types from './Types.js';
|
|
|
2
2
|
/** A decoded, unverified attestation. */
|
|
3
3
|
export type Decoded = {
|
|
4
4
|
/** JWS protected header. */
|
|
5
|
-
header: {
|
|
6
|
-
alg?: unknown;
|
|
7
|
-
kid?: unknown;
|
|
8
|
-
typ?: unknown;
|
|
5
|
+
readonly header: {
|
|
6
|
+
readonly alg?: unknown;
|
|
7
|
+
readonly kid?: unknown;
|
|
8
|
+
readonly typ?: unknown;
|
|
9
9
|
};
|
|
10
10
|
/** Unverified claims. */
|
|
11
|
-
claims: Partial<Types.Claims> & Record<string, unknown
|
|
11
|
+
readonly claims: Partial<Types.Claims> & Readonly<Record<string, unknown>>;
|
|
12
12
|
/** The signed bytes: `base64url(header) + '.' + base64url(claims)`. */
|
|
13
|
-
signingInput: Uint8Array;
|
|
13
|
+
readonly signingInput: Uint8Array;
|
|
14
14
|
/** Signature bytes. */
|
|
15
|
-
signature: Uint8Array;
|
|
15
|
+
readonly signature: Uint8Array;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* The outcome of {@link verify}: `unavailable` when the issuer's keys could not be fetched,
|
|
19
|
+
* which says nothing about the token.
|
|
20
|
+
*/
|
|
21
|
+
export type VerifyResult = {
|
|
22
|
+
readonly status: 'valid';
|
|
23
|
+
readonly claims: Types.Claims;
|
|
24
|
+
} | {
|
|
25
|
+
readonly status: 'invalid' | 'unavailable';
|
|
26
|
+
readonly reason: string;
|
|
16
27
|
};
|
|
17
28
|
/**
|
|
18
29
|
* Signs attestation claims as a compact JWS (EdDSA over Ed25519).
|
|
@@ -21,8 +32,8 @@ export type Decoded = {
|
|
|
21
32
|
* published in the issuer's JSON Web Key Set.
|
|
22
33
|
*/
|
|
23
34
|
export declare function sign(claims: Types.Claims, options: {
|
|
24
|
-
key: CryptoKey;
|
|
25
|
-
keyId: string;
|
|
35
|
+
readonly key: CryptoKey;
|
|
36
|
+
readonly keyId: string;
|
|
26
37
|
}): Promise<string>;
|
|
27
38
|
/** Decodes an attestation without verifying it, or returns `undefined` if it is malformed. */
|
|
28
39
|
export declare function decode(token: string): Decoded | undefined;
|
|
@@ -33,34 +44,37 @@ export declare function decode(token: string): Decoded | undefined;
|
|
|
33
44
|
* `iat` and `exp` times (with `clockTolerance`), then the signature with the
|
|
34
45
|
* key `keyResolver` returns for the header's `kid`.
|
|
35
46
|
*/
|
|
36
|
-
export declare function verify(token: string, options: verify.Options): Promise<
|
|
37
|
-
status: 'valid';
|
|
38
|
-
claims: Types.Claims;
|
|
39
|
-
} | {
|
|
40
|
-
status: 'invalid';
|
|
41
|
-
reason: string;
|
|
42
|
-
unavailable?: true;
|
|
43
|
-
}>;
|
|
47
|
+
export declare function verify(token: string, options: verify.Options): Promise<VerifyResult>;
|
|
44
48
|
export declare namespace verify {
|
|
45
49
|
type Options = {
|
|
46
50
|
/** Accepted attestation type, such as `email`. */
|
|
47
|
-
type: string;
|
|
51
|
+
readonly type: string;
|
|
48
52
|
/** Trusted issuer URL. */
|
|
49
|
-
issuer: string;
|
|
53
|
+
readonly issuer: string;
|
|
50
54
|
/** Origin the attestation must be bound to. */
|
|
51
|
-
audience: string;
|
|
55
|
+
readonly audience: string;
|
|
52
56
|
/** Resolves the issuer's public key by key ID. */
|
|
53
|
-
keyResolver: Types.KeyResolver;
|
|
57
|
+
readonly keyResolver: Types.KeyResolver;
|
|
54
58
|
/** Oldest accepted `iat`, in seconds before now. Defaults to the attestation's own `exp`. */
|
|
55
|
-
maxAge?: number | undefined;
|
|
59
|
+
readonly maxAge?: number | undefined;
|
|
56
60
|
/** Clock tolerance in seconds. @default 60 */
|
|
57
|
-
clockTolerance?: number | undefined;
|
|
61
|
+
readonly clockTolerance?: number | undefined;
|
|
58
62
|
/** Current Unix time in seconds, for tests. */
|
|
59
|
-
now?: number | undefined;
|
|
63
|
+
readonly now?: number | undefined;
|
|
60
64
|
};
|
|
61
65
|
}
|
|
62
66
|
/** Splits an `Identity-Attestation` header value into its attestations. */
|
|
63
|
-
export declare function parseHeader(value: string | null): string[];
|
|
67
|
+
export declare function parseHeader(value: string | null): readonly string[];
|
|
68
|
+
/**
|
|
69
|
+
* Throws a `TypeError` unless `clockTolerance`, `maxAge` and `now` are each absent or a
|
|
70
|
+
* non-negative integer of seconds: `NaN` or `Infinity` would turn the time checks off.
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
export declare function assertTimes(options: {
|
|
74
|
+
readonly clockTolerance?: number | undefined;
|
|
75
|
+
readonly maxAge?: number | undefined;
|
|
76
|
+
readonly now?: number | undefined;
|
|
77
|
+
}): void;
|
|
64
78
|
/** Accepts https issuers, and http only on localhost for development. */
|
|
65
79
|
export declare function isIssuer(value: string): boolean;
|
|
66
|
-
//# sourceMappingURL=
|
|
80
|
+
//# sourceMappingURL=Token.d.ts.map
|
|
@@ -43,6 +43,7 @@ export function decode(token) {
|
|
|
43
43
|
* key `keyResolver` returns for the header's `kid`.
|
|
44
44
|
*/
|
|
45
45
|
export async function verify(token, options) {
|
|
46
|
+
assertTimes(options);
|
|
46
47
|
const decoded = decode(token);
|
|
47
48
|
if (!decoded)
|
|
48
49
|
return invalid('The attestation is malformed.');
|
|
@@ -79,7 +80,7 @@ export async function verify(token, options) {
|
|
|
79
80
|
}
|
|
80
81
|
catch {
|
|
81
82
|
// The issuer's keys are unreachable, which says nothing about the token.
|
|
82
|
-
return {
|
|
83
|
+
return { status: 'unavailable', reason: 'The attestation key could not be resolved.' };
|
|
83
84
|
}
|
|
84
85
|
if (!key)
|
|
85
86
|
return invalid('No public key is available for the attestation key ID.');
|
|
@@ -90,7 +91,7 @@ export async function verify(token, options) {
|
|
|
90
91
|
catch { }
|
|
91
92
|
if (!valid)
|
|
92
93
|
return invalid('The attestation signature is invalid.');
|
|
93
|
-
return { status: 'valid', claims: claims };
|
|
94
|
+
return { status: 'valid', claims: Object.freeze({ ...claims }) };
|
|
94
95
|
}
|
|
95
96
|
/** Splits an `Identity-Attestation` header value into its attestations. */
|
|
96
97
|
export function parseHeader(value) {
|
|
@@ -110,6 +111,20 @@ function invalid(reason) {
|
|
|
110
111
|
function isObject(value) {
|
|
111
112
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
112
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Throws a `TypeError` unless `clockTolerance`, `maxAge` and `now` are each absent or a
|
|
116
|
+
* non-negative integer of seconds: `NaN` or `Infinity` would turn the time checks off.
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
export function assertTimes(options) {
|
|
120
|
+
for (const [name, value] of [
|
|
121
|
+
['clockTolerance', options.clockTolerance],
|
|
122
|
+
['maxAge', options.maxAge],
|
|
123
|
+
['now', options.now],
|
|
124
|
+
])
|
|
125
|
+
if (value !== undefined && !isTime(value))
|
|
126
|
+
throw new TypeError(`Attestation ${name} must be a non-negative integer of seconds.`);
|
|
127
|
+
}
|
|
113
128
|
function isTime(value) {
|
|
114
129
|
// Bounded by the largest time a Date holds, so every accepted time converts to a valid Date.
|
|
115
130
|
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 && value <= maxTime;
|
|
@@ -125,4 +140,4 @@ export function isIssuer(value) {
|
|
|
125
140
|
return true;
|
|
126
141
|
return url.protocol === 'http:' && (url.hostname === 'localhost' || url.hostname === '127.0.0.1');
|
|
127
142
|
}
|
|
128
|
-
//# sourceMappingURL=
|
|
143
|
+
//# sourceMappingURL=Token.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MaybePromise } from '
|
|
1
|
+
import type { MaybePromise } from '../../../internal/types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Claims of an MPP identity attestation.
|
|
4
4
|
*
|
|
@@ -8,41 +8,41 @@ import type { MaybePromise } from '../../../../internal/types.js';
|
|
|
8
8
|
*/
|
|
9
9
|
export type Claims = {
|
|
10
10
|
/** Issuer URL. */
|
|
11
|
-
iss: string;
|
|
11
|
+
readonly iss: string;
|
|
12
12
|
/** Origin of the server the attestation is for, such as `https://api.example`. */
|
|
13
|
-
aud: string;
|
|
13
|
+
readonly aud: string;
|
|
14
14
|
/** Attested subject, such as an email address. */
|
|
15
|
-
sub: string;
|
|
15
|
+
readonly sub: string;
|
|
16
16
|
/** Attestation type, such as `email`. */
|
|
17
|
-
type: string;
|
|
17
|
+
readonly type: string;
|
|
18
18
|
/** Issued-at time, in Unix seconds. */
|
|
19
|
-
iat: number;
|
|
19
|
+
readonly iat: number;
|
|
20
20
|
/** Expiry time, in Unix seconds. */
|
|
21
|
-
exp: number;
|
|
21
|
+
readonly exp: number;
|
|
22
22
|
/** Unique attestation identifier. */
|
|
23
|
-
jti?: string | undefined;
|
|
23
|
+
readonly jti?: string | undefined;
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
26
|
* Resolves the issuer's public key for an attestation's `kid`. Returns
|
|
27
27
|
* `undefined` for an unknown or malformed `kid`, and throws only when the keys
|
|
28
|
-
* cannot be fetched: a throw answers the request with 503.
|
|
28
|
+
* cannot be fetched: a throw answers the request with 503 (`unavailable`).
|
|
29
29
|
*/
|
|
30
30
|
export type KeyResolver = (parameters: {
|
|
31
31
|
/** Issuer URL the attestation claims. */
|
|
32
|
-
issuer: string;
|
|
32
|
+
readonly issuer: string;
|
|
33
33
|
/** Key identifier from the JWS header. */
|
|
34
|
-
keyId: string;
|
|
34
|
+
readonly keyId: string;
|
|
35
35
|
}) => MaybePromise<CryptoKey | undefined>;
|
|
36
36
|
/** An attestation a route requires, as listed in `Identity-Attestation-Required`. */
|
|
37
37
|
export type Requirement = {
|
|
38
38
|
/** Attestation type, such as `email`. */
|
|
39
|
-
type: string;
|
|
39
|
+
readonly type: string;
|
|
40
40
|
/** Issuer URL the route trusts, or where to obtain the identity. Absent for a server's own identity system without one. */
|
|
41
|
-
issuer?: string | undefined;
|
|
41
|
+
readonly issuer?: string | undefined;
|
|
42
42
|
};
|
|
43
43
|
/** A verified attestation. */
|
|
44
44
|
export type Verified = {
|
|
45
45
|
/** Verified claims. */
|
|
46
|
-
claims: Claims;
|
|
46
|
+
readonly claims: Claims;
|
|
47
47
|
};
|
|
48
48
|
//# sourceMappingURL=Types.d.ts.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type * as Identity from '../../../../identity/Types.js';
|
|
2
|
+
import type { MaybePromise } from '../../../../internal/types.js';
|
|
3
|
+
import type * as Types from '../Types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a prover that presents identity attestations, and obtains them when a server asks.
|
|
6
|
+
*
|
|
7
|
+
* Each in-scope request carries, in `Identity-Attestation`, the cached attestations whose
|
|
8
|
+
* audience is the request's origin: on a paid attempt every one not expired beyond the clock
|
|
9
|
+
* tolerance, on an unpaid one only those outside the refresh margin, so one close to expiry is
|
|
10
|
+
* replaced before paying. When a server answers 401 or 403 with `Identity-Attestation-Required`,
|
|
11
|
+
* the prover meets each listed group (see {@link requirements}) with one attestation from a
|
|
12
|
+
* trusted issuer, and resends the request with them, keeping the attestations it already carried.
|
|
13
|
+
* Within a group of alternatives it prefers one it holds, and otherwise acquires them in listed
|
|
14
|
+
* order until one succeeds, so a route accepting any of several issuers costs one acquisition.
|
|
15
|
+
*
|
|
16
|
+
* Trust is set by two options together:
|
|
17
|
+
* - `scope`: the origins that may receive attestations and ask for new ones. The audience is
|
|
18
|
+
* always the request's own origin, never one a server names, so a server cannot obtain an
|
|
19
|
+
* attestation it could replay elsewhere.
|
|
20
|
+
* - `issuers`: the issuers `acquire` may be asked for. A server only names an issuer; an
|
|
21
|
+
* untrusted one could relay a code and obtain an attestation for another origin.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* Attestation.Client.prover({
|
|
26
|
+
* scope: ['https://api.example'],
|
|
27
|
+
* issuers: ['https://issuer.example'],
|
|
28
|
+
* types: ['phone'],
|
|
29
|
+
* acquire: ({ audience, issuer }) => obtainPhoneAttestation({ audience, issuer }),
|
|
30
|
+
* })
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function prover(config: prover.Config): Identity.Prover;
|
|
34
|
+
export declare namespace prover {
|
|
35
|
+
type Config = {
|
|
36
|
+
/** Origins that receive attestations and may ask for new ones, or `'*'` for any origin. */
|
|
37
|
+
readonly scope: Identity.Prover['scope'];
|
|
38
|
+
/** Attestations obtained beforehand, such as from an issuer with curl. */
|
|
39
|
+
readonly attestations?: readonly string[] | undefined;
|
|
40
|
+
/** Obtains an attestation a server requires, or returns `undefined` to decline. */
|
|
41
|
+
readonly acquire?: ((requirement: {
|
|
42
|
+
/** Origin the attestation must be bound to. */
|
|
43
|
+
readonly audience: string;
|
|
44
|
+
/** Issuer to obtain it from. */
|
|
45
|
+
readonly issuer: string;
|
|
46
|
+
/** Attestation type. */
|
|
47
|
+
readonly type: string;
|
|
48
|
+
}) => MaybePromise<string | undefined>) | undefined;
|
|
49
|
+
/** Issuers `acquire` may be asked for. Required with `acquire`. */
|
|
50
|
+
readonly issuers?: readonly string[] | undefined;
|
|
51
|
+
/** Attestation types `acquire` may be asked for. Defaults to any. */
|
|
52
|
+
readonly types?: readonly string[] | undefined;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Parses the attestation requirements of a rejected response as groups, every one of which a
|
|
57
|
+
* request must meet with any one of its requirements:
|
|
58
|
+
*
|
|
59
|
+
* - An item, such as `phone;issuer="https://a.example"`, is a group of one.
|
|
60
|
+
* - An inner list, such as `(email;issuer="https://a.example" email;issuer="https://b.example")`,
|
|
61
|
+
* is a group of alternatives, as `Identity.anyOf` sends them.
|
|
62
|
+
*
|
|
63
|
+
* Malformed members are skipped, and a group left with none is dropped.
|
|
64
|
+
*/
|
|
65
|
+
export declare function requirements(headers: Headers): readonly (readonly Types.Requirement[])[];
|
|
66
|
+
//# sourceMappingURL=index.d.ts.map
|