mppx 0.17.6 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/dist/cli/cli.js +139 -139
  3. package/dist/cli/plugins/index.js +6 -6
  4. package/dist/client/Mppx.js +13 -4
  5. package/dist/client/internal/Fetch.js +9 -5
  6. package/dist/extensions/identity/{email-otp/internal → attestation}/Constants.d.ts +4 -1
  7. package/dist/extensions/identity/{email-otp/internal → attestation}/Constants.js +4 -1
  8. package/dist/extensions/identity/{email-otp/internal → attestation}/Jwks.d.ts +5 -5
  9. package/dist/extensions/identity/{email-otp/internal → attestation}/Jwks.js +1 -1
  10. package/dist/extensions/identity/{email-otp/internal/Attestation.d.ts → attestation/Token.d.ts} +40 -26
  11. package/dist/extensions/identity/{email-otp/internal/Attestation.js → attestation/Token.js} +18 -3
  12. package/dist/extensions/identity/{email-otp/internal → attestation}/Types.d.ts +14 -14
  13. package/dist/extensions/identity/attestation/client/index.d.ts +66 -0
  14. package/dist/extensions/identity/attestation/client/index.js +273 -0
  15. package/dist/extensions/identity/attestation/index.d.ts +20 -0
  16. package/dist/extensions/identity/attestation/index.js +18 -0
  17. package/dist/extensions/identity/attestation/internal/Cache.d.ts +120 -0
  18. package/dist/extensions/identity/attestation/internal/Cache.js +90 -0
  19. package/dist/extensions/identity/attestation/server/index.d.ts +66 -0
  20. package/dist/extensions/identity/attestation/server/index.js +119 -0
  21. package/dist/extensions/identity/email-otp/Constants.d.ts +5 -5
  22. package/dist/extensions/identity/email-otp/Constants.js +6 -5
  23. package/dist/extensions/identity/email-otp/Types.d.ts +4 -4
  24. package/dist/extensions/identity/email-otp/client/index.d.ts +22 -15
  25. package/dist/extensions/identity/email-otp/client/index.js +17 -5
  26. package/dist/extensions/identity/email-otp/index.d.ts +3 -8
  27. package/dist/extensions/identity/email-otp/index.js +3 -8
  28. package/dist/extensions/identity/email-otp/server/index.d.ts +16 -16
  29. package/dist/extensions/identity/email-otp/server/index.js +17 -14
  30. package/dist/extensions/identity/index.d.ts +2 -0
  31. package/dist/extensions/identity/index.js +2 -0
  32. package/dist/extensions/identity/internal/Verification.js +9 -5
  33. package/dist/identity/Callback.d.ts +24 -5
  34. package/dist/identity/Callback.js +35 -11
  35. package/dist/identity/Client.d.ts +78 -5
  36. package/dist/identity/Client.js +165 -48
  37. package/dist/identity/Headers.d.ts +16 -0
  38. package/dist/identity/Headers.js +19 -0
  39. package/dist/identity/Server.d.ts +159 -37
  40. package/dist/identity/Server.js +307 -138
  41. package/dist/identity/Status.d.ts +14 -0
  42. package/dist/identity/Status.js +19 -0
  43. package/dist/identity/Types.d.ts +161 -65
  44. package/dist/identity/index.d.ts +2 -0
  45. package/dist/identity/index.js +2 -0
  46. package/dist/identity/internal/Requirements.d.ts +28 -0
  47. package/dist/identity/internal/Requirements.js +78 -0
  48. package/dist/integrations/x402/Evm.d.ts +1 -0
  49. package/dist/internal/env.d.ts +14 -0
  50. package/dist/internal/env.js +38 -0
  51. package/dist/internal/version.d.ts +2 -2
  52. package/dist/internal/version.js +1 -1
  53. package/dist/proxy/internal/Headers.d.ts +1 -1
  54. package/dist/proxy/internal/Headers.js +7 -3
  55. package/dist/server/Mppx.d.ts +14 -6
  56. package/dist/server/Mppx.js +252 -148
  57. package/dist/server/internal/challengeMeta.d.ts +0 -6
  58. package/dist/server/internal/challengeMeta.js +3 -17
  59. package/dist/tempo/server/Charge.d.ts +14 -9
  60. package/dist/tempo/server/Charge.js +5 -1
  61. package/dist/tempo/server/Methods.js +4 -1
  62. package/dist/tempo/server/Subscription.d.ts +8 -0
  63. package/dist/tempo/server/internal/fallback-store.d.ts +7 -2
  64. package/dist/tempo/server/internal/fallback-store.js +12 -3
  65. package/dist/tempo/server/internal/html.gen.js +1 -1
  66. package/dist/tempo/session/server/Session.d.ts +5 -1
  67. package/package.json +5 -1
  68. package/dist/extensions/identity/email-otp/internal/Prover.d.ts +0 -46
  69. package/dist/extensions/identity/email-otp/internal/Prover.js +0 -168
  70. package/dist/extensions/identity/email-otp/internal/Verifier.d.ts +0 -56
  71. package/dist/extensions/identity/email-otp/internal/Verifier.js +0 -116
  72. /package/dist/extensions/identity/{email-otp/internal → attestation}/Types.js +0 -0
@@ -1,10 +1,19 @@
1
- const members = Symbol('mppx.identity.members');
1
+ import * as Requirements from './internal/Requirements.js';
2
+ import * as Status from './Status.js';
3
+ /**
4
+ * The members of each verifier `anyOf` returned, keyed by that exact object, so a copy or a
5
+ * derived verifier (spread, `Object.create`) is not taken for one and its own `verify` runs.
6
+ */
7
+ const anyOfMembers = new WeakMap();
2
8
  /**
3
9
  * Accepts the first of several identity protocols that verifies.
4
10
  *
5
- * Every member runs. The result is `valid` if any member verifies, `invalid` if none verifies and
6
- * any member was presented but failed, and `missing` otherwise, with every
7
- * member's challenge headers. All members must run at the same stage.
11
+ * Every member runs. The result is `valid` if any member verifies (the first valid member
12
+ * wins). Otherwise the rejected member whose status has the highest precedence
13
+ * (`Identity.Status.precedence`) decides the status and reason, with every member's headers.
14
+ * Members' `Identity-Attestation-Required` values become one inner list of alternatives, such
15
+ * as `(email;issuer="https://a.example" email;issuer="https://b.example")`, so a client
16
+ * obtains one of them rather than all.
8
17
  *
9
18
  * @example
10
19
  * ```ts
@@ -12,63 +21,184 @@ const members = Symbol('mppx.identity.members');
12
21
  * ```
13
22
  */
14
23
  export function anyOf(...verifiers) {
15
- const stage = stageOf(verifiers[0]);
16
- if (verifiers.some((verifier) => stageOf(verifier) !== stage))
17
- throw new TypeError('Identity anyOf verifiers must run at the same stage.');
18
- const verifier = {
19
- [members]: verifiers,
20
- stage,
21
- async verify(request, context) {
24
+ for (const verifier of verifiers)
25
+ assertRequestStage(verifier);
26
+ const verifier = Object.freeze({
27
+ async verify(request) {
22
28
  // Every member runs, so each presented proof's replay nonce is consumed even when an
23
- // earlier member already verified; the first valid member wins.
24
- const all = [];
29
+ // earlier member already verified.
30
+ const outcomes = [];
25
31
  for (const member of verifiers)
26
- all.push(await run(member, request, context));
27
- const index = all.findIndex((outcome) => outcome.status === 'valid');
28
- const matched = all[index];
29
- if (matched?.status === 'valid')
30
- return { status: 'valid', value: { index, value: matched.value } };
31
- const outcomes = all;
32
- const headers = mergeHeaders(outcomes);
33
- const invalid = outcomes.find((outcome) => outcome.status === 'invalid');
34
- if (invalid)
35
- return { status: 'invalid', code: invalid.code, headers, reason: invalid.reason };
36
- // Keep the first member's custom status so wrapping a verifier does not change its HTTP behavior.
37
- const code = outcomes.find((outcome) => outcome.code !== undefined)?.code;
38
- return {
39
- status: 'missing',
40
- ...(code !== undefined && { code }),
41
- headers,
42
- reason: 'None of the accepted identity proofs was presented.',
43
- };
32
+ outcomes.push(await member.verify(request));
33
+ return combineAnyOf(outcomes);
44
34
  },
45
- };
35
+ });
36
+ anyOfMembers.set(verifier, Object.freeze([...verifiers]));
46
37
  return verifier;
47
38
  }
48
39
  /**
49
- * Runs every verifier at one stage and combines their outcomes.
40
+ * Combines the outcomes of `anyOf` members: `valid` with the first valid member, otherwise
41
+ * the precedence rule with merged headers.
42
+ */
43
+ function combineAnyOf(outcomes) {
44
+ const index = outcomes.findIndex((outcome) => outcome.status === 'valid');
45
+ const matched = outcomes[index];
46
+ if (matched?.status === 'valid')
47
+ return Object.freeze({
48
+ status: 'valid',
49
+ value: Object.freeze({ index, value: matched.value }),
50
+ });
51
+ const rejected = outcomes;
52
+ const decision = decide(rejected);
53
+ return toVerification({
54
+ ...decision,
55
+ headers: groupAlternatives(rejected.map((outcome) => normalize(outcome).headers)),
56
+ ...(decision.status === 'missing' && {
57
+ reason: 'None of the accepted identity proofs was presented.',
58
+ }),
59
+ });
60
+ }
61
+ /**
62
+ * Merges the headers of `anyOf` members: each requirements header
63
+ * (`Requirements.alternativeHeaders`) once, its values grouped as alternatives; every other
64
+ * header as it is, in order.
65
+ */
66
+ function groupAlternatives(members) {
67
+ const grouped = new Map();
68
+ const others = [];
69
+ for (const [name, value] of members.flat()) {
70
+ if (!Requirements.alternativeHeaders.includes(name))
71
+ others.push(Object.freeze([name, value]));
72
+ else
73
+ grouped.set(name, [...(grouped.get(name) ?? []), value]);
74
+ }
75
+ return Object.freeze([
76
+ ...others,
77
+ ...[...grouped].map(([name, values]) => Object.freeze([name, Requirements.alternatives(values)])),
78
+ ]);
79
+ }
80
+ /** The outcome of no verifiers: valid, with no values. @internal */
81
+ export const none = Object.freeze({ status: 'valid', values: new Map() });
82
+ const defaultReasons = {
83
+ missing: 'Identity verification is required.',
84
+ invalid: 'The identity proof is invalid.',
85
+ forbidden: 'This identity is not allowed.',
86
+ unavailable: 'Identity verification is unavailable.',
87
+ };
88
+ /**
89
+ * Decides one rejection from rejected outcomes: the outcome whose fixed status has the highest
90
+ * precedence decides status, code and reason (the first such outcome on ties), and the headers
91
+ * of every outcome are merged. @internal
92
+ */
93
+ export function decide(outcomes) {
94
+ if (outcomes.length === 0)
95
+ throw new TypeError('An identity decision needs a rejected outcome.');
96
+ const normalized = outcomes.map(normalize);
97
+ const rank = (decision) => Status.precedence.indexOf(decision.status);
98
+ const winner = normalized.reduce((best, next) => (rank(next) < rank(best) ? next : best));
99
+ return Object.freeze({
100
+ ...winner,
101
+ headers: Object.freeze(normalized.flatMap((decision) => decision.headers)),
102
+ });
103
+ }
104
+ /**
105
+ * Maps a rejected outcome to its fixed status, honoring the deprecated `code`. Fails closed: an
106
+ * unknown status is `invalid`, and a deprecated `code` that is not an HTTP error status is ignored.
107
+ */
108
+ function normalize(outcome) {
109
+ const known = Object.hasOwn(Status.codes, outcome.status);
110
+ const raw = known && 'code' in outcome ? outcome.code : undefined;
111
+ const code = Number.isInteger(raw) && raw >= 400 && raw <= 599 ? raw : undefined;
112
+ const status = !known
113
+ ? 'invalid'
114
+ : code === Status.codes.forbidden
115
+ ? 'forbidden'
116
+ : code === Status.codes.unavailable
117
+ ? 'unavailable'
118
+ : outcome.status;
119
+ return {
120
+ status,
121
+ code: code ?? Status.codes[status],
122
+ headers: Object.freeze([...new Headers(outcome.headers)].map(([name, value]) => Object.freeze([name, value]))),
123
+ reason: known
124
+ ? (outcome.reason ?? defaultReasons[status])
125
+ : 'The identity verifier returned an unknown status.',
126
+ };
127
+ }
128
+ /** Turns a decision back into a verification, keeping a non-default code. */
129
+ function toVerification(decision) {
130
+ const headers = decision.headers.map(([name, value]) => [name, value]);
131
+ const { code, reason, status } = decision;
132
+ if (status === 'forbidden' || status === 'unavailable')
133
+ return Object.freeze({ status, headers, reason });
134
+ return Object.freeze({
135
+ status,
136
+ ...(code !== Status.codes[status] && { code }),
137
+ headers,
138
+ reason,
139
+ });
140
+ }
141
+ /**
142
+ * The identity state of one request. Every verifier runs at most once per context, so
143
+ * replay-protected nonces are consumed once even when `compose` calls several offers with
144
+ * the same request. `compose` creates one and passes it to every offer it calls; a route
145
+ * called directly creates its own. @internal
146
+ */
147
+ export class Context {
148
+ /** Verification promises by verifier. Internal and mutable; outcomes are frozen. */
149
+ verifications = new Map();
150
+ /**
151
+ * Payment check promises by check, then by Challenge id and attempt (`paid` or `unpaid`), so
152
+ * a check runs once per attempt even when `compose` calls the matched offer again.
153
+ */
154
+ checks = new Map();
155
+ }
156
+ /** Creates the identity context of one request. @internal */
157
+ export function createContext() {
158
+ return new Context();
159
+ }
160
+ /** Returns `value` if it is an identity context, otherwise a new one. @internal */
161
+ export function contextOf(value) {
162
+ return value instanceof Context ? value : createContext();
163
+ }
164
+ /** Runs one verifier, or each `anyOf` member, at most once per context. */
165
+ function evaluate(context, request, verifier) {
166
+ let outcome = context.verifications.get(verifier);
167
+ if (outcome)
168
+ return outcome;
169
+ const nested = anyOfMembers.get(verifier);
170
+ outcome = nested
171
+ ? (async () => {
172
+ const outcomes = [];
173
+ for (const member of nested)
174
+ outcomes.push(await evaluate(context, request, member));
175
+ return combineAnyOf(outcomes);
176
+ })()
177
+ : (async () => verifier.verify(request))();
178
+ context.verifications.set(verifier, outcome);
179
+ return outcome;
180
+ }
181
+ /**
182
+ * Runs request-stage verifiers and combines their outcomes.
50
183
  *
51
- * Every verifier must be valid. Any `invalid` outcome rejects with 401 unless
52
- * it sets its own code; otherwise any `missing` outcome rejects with 403. The
53
- * headers of every rejected outcome are merged. Each verifier runs at most once
54
- * per request and context, so replay-protected nonces are consumed once even
55
- * when several handlers evaluate the same request.
184
+ * Valid only if every verifier is valid, with each value recorded at every `anyOf` level, so
185
+ * `results.get(leaf)` works when nested. Otherwise the precedence rule of {@link decide}.
186
+ * @internal
56
187
  */
57
- export async function verify(request, verifiers, context = {}) {
188
+ export async function verify(context, request, verifiers) {
58
189
  const values = new Map();
59
190
  const rejected = [];
60
191
  for (const verifier of verifiers) {
61
- const outcome = await run(verifier, request, context);
192
+ const outcome = await evaluate(context, request, verifier);
62
193
  if (outcome.status !== 'valid') {
63
194
  rejected.push(outcome);
64
195
  continue;
65
196
  }
66
- // Record the matched member at every anyOf level, so results.get(leaf) works when nested.
67
197
  let current = verifier;
68
198
  let value = outcome.value;
69
199
  for (;;) {
70
200
  values.set(current, value);
71
- const nested = current[members];
201
+ const nested = anyOfMembers.get(current);
72
202
  if (!nested)
73
203
  break;
74
204
  const matched = value;
@@ -76,119 +206,158 @@ export async function verify(request, verifiers, context = {}) {
76
206
  value = matched.value;
77
207
  }
78
208
  }
79
- if (rejected.length === 0)
80
- return { status: 'valid', values };
81
- const headers = mergeHeaders(rejected);
82
- const invalid = rejected.find((outcome) => outcome.status === 'invalid');
83
- if (invalid)
84
- return { status: 'invalid', code: invalid.code ?? 401, headers, reason: invalid.reason };
85
- const missing = rejected[0];
86
- return {
87
- status: 'missing',
88
- code: missing.code ?? 403,
89
- headers,
90
- reason: missing.reason ?? 'Identity verification is required.',
91
- };
209
+ if (rejected.length > 0)
210
+ return decide(rejected);
211
+ return Object.freeze({ status: 'valid', values });
92
212
  }
93
213
  /**
94
- * Collects the challenge headers of payment-stage verifiers for one 402.
95
- *
96
- * Each verifier's hook runs at most once per request and nonce, so every offer
97
- * in a composed 402 carries the same challenge. @internal
214
+ * Runs a payment check at most once per context, Challenge and attempt, and returns its
215
+ * outcome. @internal
98
216
  */
99
- export async function challenge(request, verifiers, nonce) {
100
- const headers = new Headers();
101
- for (const verifier of verifiers) {
102
- for (const member of leaves(verifier)) {
103
- if (!member.challenge)
104
- continue;
105
- let byVerifier = challenges.get(request);
106
- if (!byVerifier)
107
- challenges.set(request, (byVerifier = new Map()));
108
- let byNonce = byVerifier.get(member);
109
- if (!byNonce)
110
- byVerifier.set(member, (byNonce = new Map()));
111
- let result = byNonce.get(nonce);
112
- if (!result) {
113
- result = Promise.resolve(member.challenge(request, { nonce }));
114
- byNonce.set(nonce, result);
115
- }
116
- const init = await result;
117
- if (init)
118
- for (const [name, value] of new Headers(init))
119
- headers.append(name, value);
120
- }
121
- }
122
- return headers;
217
+ export function check(context, paymentCheck, parameters) {
218
+ const key = `${parameters.challenge.id}:${parameters.credential ? 'paid' : 'unpaid'}`;
219
+ const outcomes = context.checks.get(paymentCheck) ?? new Map();
220
+ context.checks.set(paymentCheck, outcomes);
221
+ let outcome = outcomes.get(key);
222
+ if (outcome)
223
+ return outcome;
224
+ outcome = (async () => {
225
+ const result = await paymentCheck.verify(Object.freeze({ ...parameters }));
226
+ if (result.status !== 'valid')
227
+ return decide([result]);
228
+ return Object.freeze({ status: 'valid', values: new Map([[paymentCheck, result.value]]) });
229
+ })();
230
+ outcomes.set(key, outcome);
231
+ return outcome;
123
232
  }
233
+ /** The state of a route call before identity runs. @internal */
234
+ export const initial = Object.freeze({ phase: 'request' });
124
235
  /**
125
- * Returns the identity nonce for every 402 issued on one request.
236
+ * Returns the next identity state. Pure: it never runs verifiers, and it throws a `TypeError`
237
+ * for an event the current phase does not accept.
238
+ *
239
+ * | Phase | Event | Next |
240
+ * | ------------------------ | ---------------------- | --------------------------------- |
241
+ * | `request` | `verified` (valid) | `identified` |
242
+ * | `request` | `verified` (rejected) | `rejected` (stage `request`) |
243
+ * | `identified` | `pay` | `payment` |
244
+ * | `payment` | `checked` (valid) | `accepted` |
245
+ * | `payment` + Credential | `checked` (rejected) | `rejected` (stage `payment`) |
246
+ * | `payment`, no Credential | `checked` (rejected) | `identified` (not authorized) |
247
+ * | `identified`, `accepted` | `challenge` | `challenged` |
126
248
  *
127
- * Composed routes call each offer with the same request, so all offers share it. @internal
249
+ * @internal
128
250
  */
129
- export function nonceFor(request, create) {
130
- let nonce = nonces.get(request);
131
- if (!nonce)
132
- nonces.set(request, (nonce = create()));
133
- return nonce;
134
- }
135
- /** Builds the rejection response for a failed request-stage outcome. @internal */
136
- export function toResponse(outcome) {
137
- const headers = new Headers(outcome.headers);
251
+ export function transition(state, event) {
252
+ switch (event.type) {
253
+ case 'verified':
254
+ if (state.phase !== 'request')
255
+ break;
256
+ if (event.outcome.status !== 'valid')
257
+ return reject('request', event.outcome);
258
+ return Object.freeze({ phase: 'identified', values: event.outcome.values });
259
+ case 'pay':
260
+ if (state.phase !== 'identified')
261
+ break;
262
+ return Object.freeze({
263
+ phase: 'payment',
264
+ values: state.values,
265
+ challenge: event.challenge,
266
+ credential: event.credential,
267
+ });
268
+ case 'checked':
269
+ if (state.phase !== 'payment')
270
+ break;
271
+ if (event.outcome.status === 'valid')
272
+ return Object.freeze({
273
+ phase: 'accepted',
274
+ values: new Map([...state.values, ...event.outcome.values]),
275
+ });
276
+ // A paid retry may already have pushed the payment: reject instead of a fresh 402.
277
+ if (state.credential)
278
+ return reject('payment', event.outcome);
279
+ return Object.freeze({ phase: 'identified', values: state.values });
280
+ case 'challenge':
281
+ if (state.phase !== 'identified' && state.phase !== 'accepted')
282
+ break;
283
+ return Object.freeze({
284
+ phase: 'challenged',
285
+ values: state.values,
286
+ requirements: event.requirements,
287
+ });
288
+ }
289
+ throw new TypeError(`Illegal identity transition "${event.type}" from phase "${state.phase}".`);
290
+ }
291
+ function reject(stage, decision) {
292
+ return Object.freeze({
293
+ phase: 'rejected',
294
+ rejection: Object.freeze({ stage, status: decision.status, reason: decision.reason }),
295
+ code: decision.code,
296
+ headers: decision.headers,
297
+ });
298
+ }
299
+ /** Builds the rejection response of a rejected state. @internal */
300
+ export function toResponse(state) {
301
+ const headers = new Headers(state.headers.map(([name, value]) => [name, value]));
138
302
  headers.set('Cache-Control', 'no-store');
139
- return new Response(outcome.reason, { headers, status: outcome.code });
303
+ return new Response(state.rejection.reason, { headers, status: state.code });
140
304
  }
141
- /** Creates a results lookup from verified values. @internal */
142
- export function results(values = new Map()) {
305
+ /** Returns the frozen results lookup of a state's verified values. @internal */
306
+ export function results(state) {
307
+ const values = 'values' in state ? state.values : new Map();
143
308
  return Object.freeze({
144
309
  get(verifier) {
145
310
  return values.get(verifier);
146
311
  },
147
312
  });
148
313
  }
149
- /** Returns the stage a verifier runs at. @internal */
150
- export function stageOf(verifier) {
314
+ /**
315
+ * Parses a route's `identity` option. Throws a `TypeError` for a payment-stage or unknown-stage
316
+ * verifier, and for payment requirements that cannot be serialized. @internal
317
+ */
318
+ export function route(identity) {
319
+ const { payment = undefined, request = [] } = Array.isArray(identity)
320
+ ? { request: identity }
321
+ : parseObject(identity);
322
+ for (const verifier of request)
323
+ assertRequestStage(verifier);
324
+ // Any value but undefined is a payment check, so a falsy malformed one cannot turn it off.
325
+ if (payment !== undefined) {
326
+ if (typeof payment !== 'object' || payment === null)
327
+ throw new TypeError('Identity payment check must be { requires, verify }.');
328
+ if (!Array.isArray(payment.requires))
329
+ throw new TypeError('Identity payment check must list the identities it requires.');
330
+ if (typeof payment.verify !== 'function')
331
+ throw new TypeError('Identity payment check must have a verify function.');
332
+ Requirements.serialize(payment.requires);
333
+ }
334
+ return Object.freeze({ payment, request: Object.freeze([...request]) });
335
+ }
336
+ /**
337
+ * Parses the object form `{ request?, payment? }`. Any other shape (a bare verifier, an `anyOf`
338
+ * result, a misspelled key) throws, so a route never silently runs without its identity checks.
339
+ */
340
+ function parseObject(identity) {
341
+ if (identity === undefined)
342
+ return {};
343
+ if (typeof identity !== 'object' || identity === null)
344
+ throw new TypeError('Route identity must be an array of verifiers or { request, payment }.');
345
+ const keys = Object.keys(identity);
346
+ const unknown = keys.filter((key) => key !== 'request' && key !== 'payment');
347
+ if (unknown.length > 0 || 'verify' in identity)
348
+ throw new TypeError(`Route identity must be an array of verifiers or { request, payment }; got ${unknown.length > 0 ? `key "${unknown[0]}"` : 'a verifier'}. Wrap a single verifier in an array.`);
349
+ const { request } = identity;
350
+ if (request !== undefined && !Array.isArray(request))
351
+ throw new TypeError('Route identity request must be an array of verifiers.');
352
+ return identity;
353
+ }
354
+ /** Throws unless a verifier runs at the request stage (the only one). */
355
+ function assertRequestStage(verifier) {
151
356
  const stage = verifier.stage ?? 'request';
357
+ if (stage === 'payment')
358
+ throw new TypeError('Payment-stage identity verifiers were removed: pass a payment check as `identity: { payment }` instead.');
152
359
  // An unknown stage would leave the verifier unchecked, so it is rejected (fail closed).
153
- if (stage !== 'request' && stage !== 'payment')
360
+ if (stage !== 'request')
154
361
  throw new TypeError(`Unknown identity verifier stage "${String(stage)}".`);
155
- return stage;
156
- }
157
- /** Returns a verifier and, through `anyOf`, every member it wraps. */
158
- function leaves(verifier) {
159
- const nested = verifier[members];
160
- return nested ? nested.flatMap(leaves) : [verifier];
161
- }
162
- const nonces = new WeakMap();
163
- const challenges = new WeakMap();
164
- const cache = new WeakMap();
165
- function run(verifier, request, context) {
166
- let byVerifier = cache.get(request);
167
- if (!byVerifier)
168
- cache.set(request, (byVerifier = new Map()));
169
- let byContext = byVerifier.get(verifier);
170
- if (!byContext)
171
- byVerifier.set(verifier, (byContext = new Map()));
172
- // Decisions can depend on the credential, so each credential object gets its own entry.
173
- const key = `${context.challenge?.id ?? ''}|${context.nonce ?? ''}`;
174
- let byCredential = byContext.get(key);
175
- if (!byCredential)
176
- byContext.set(key, (byCredential = new Map()));
177
- let outcome = byCredential.get(context.credential);
178
- if (!outcome) {
179
- outcome = Promise.resolve(verifier.verify(request, context));
180
- byCredential.set(context.credential, outcome);
181
- }
182
- return outcome;
183
- }
184
- function mergeHeaders(outcomes) {
185
- const merged = new Headers();
186
- for (const outcome of outcomes) {
187
- if (outcome.status === 'valid' || !outcome.headers)
188
- continue;
189
- for (const [name, value] of new Headers(outcome.headers))
190
- merged.append(name, value);
191
- }
192
- return merged;
193
362
  }
194
363
  //# sourceMappingURL=Server.js.map
@@ -0,0 +1,14 @@
1
+ /** The HTTP status code of each rejected identity status. */
2
+ export declare const codes: {
3
+ readonly missing: 401;
4
+ readonly invalid: 401;
5
+ readonly forbidden: 403;
6
+ readonly unavailable: 503;
7
+ };
8
+ /**
9
+ * Rejected statuses from highest to lowest precedence. When several verifiers reject (or
10
+ * every `anyOf` member does), the outcome with the highest precedence decides the status
11
+ * and reason.
12
+ */
13
+ export declare const precedence: readonly ["forbidden", "unavailable", "invalid", "missing"];
14
+ //# sourceMappingURL=Status.d.ts.map
@@ -0,0 +1,19 @@
1
+ /** The HTTP status code of each rejected identity status. */
2
+ export const codes = {
3
+ missing: 401,
4
+ invalid: 401,
5
+ forbidden: 403,
6
+ unavailable: 503,
7
+ };
8
+ /**
9
+ * Rejected statuses from highest to lowest precedence. When several verifiers reject (or
10
+ * every `anyOf` member does), the outcome with the highest precedence decides the status
11
+ * and reason.
12
+ */
13
+ export const precedence = [
14
+ 'forbidden',
15
+ 'unavailable',
16
+ 'invalid',
17
+ 'missing',
18
+ ];
19
+ //# sourceMappingURL=Status.js.map