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.
Files changed (144) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/dist/Currency.d.ts +59 -0
  3. package/dist/Currency.js +60 -0
  4. package/dist/Errors.d.ts +13 -0
  5. package/dist/Errors.js +15 -6
  6. package/dist/cli/cli.js +138 -138
  7. package/dist/cli/plugins/index.js +12 -12
  8. package/dist/cli/plugins/tempo.js +13 -1
  9. package/dist/client/Mppx.js +13 -4
  10. package/dist/client/internal/Fetch.d.ts +5 -0
  11. package/dist/client/internal/Fetch.js +16 -5
  12. package/dist/cloudflare/ChannelStore.d.ts +1 -2
  13. package/dist/cloudflare/ChannelStore.js +14 -20
  14. package/dist/cloudflare/Channels.d.ts +20 -2
  15. package/dist/cloudflare/Channels.js +47 -4
  16. package/dist/cloudflare/index.d.ts +1 -1
  17. package/dist/cloudflare/index.js +1 -1
  18. package/dist/discovery/Discovery.d.ts +116 -9
  19. package/dist/discovery/Discovery.js +169 -0
  20. package/dist/evm/server/Charge.js +2 -3
  21. package/dist/evm/server/Methods.d.ts +1 -1
  22. package/dist/evm/server/Methods.js +3 -2
  23. package/dist/extensions/identity/{email-otp/internal → attestation}/Constants.d.ts +4 -1
  24. package/dist/extensions/identity/{email-otp/internal → attestation}/Constants.js +4 -1
  25. package/dist/extensions/identity/{email-otp/internal → attestation}/Jwks.d.ts +5 -5
  26. package/dist/extensions/identity/{email-otp/internal → attestation}/Jwks.js +1 -1
  27. package/dist/extensions/identity/{email-otp/internal/Attestation.d.ts → attestation/Token.d.ts} +40 -26
  28. package/dist/extensions/identity/{email-otp/internal/Attestation.js → attestation/Token.js} +18 -3
  29. package/dist/extensions/identity/{email-otp/internal → attestation}/Types.d.ts +14 -14
  30. package/dist/extensions/identity/attestation/client/index.d.ts +66 -0
  31. package/dist/extensions/identity/attestation/client/index.js +273 -0
  32. package/dist/extensions/identity/attestation/index.d.ts +20 -0
  33. package/dist/extensions/identity/attestation/index.js +18 -0
  34. package/dist/extensions/identity/attestation/internal/Cache.d.ts +120 -0
  35. package/dist/extensions/identity/attestation/internal/Cache.js +90 -0
  36. package/dist/extensions/identity/attestation/server/index.d.ts +66 -0
  37. package/dist/extensions/identity/attestation/server/index.js +119 -0
  38. package/dist/extensions/identity/email-otp/Constants.d.ts +5 -5
  39. package/dist/extensions/identity/email-otp/Constants.js +6 -5
  40. package/dist/extensions/identity/email-otp/Types.d.ts +4 -4
  41. package/dist/extensions/identity/email-otp/client/index.d.ts +22 -15
  42. package/dist/extensions/identity/email-otp/client/index.js +21 -9
  43. package/dist/extensions/identity/email-otp/index.d.ts +3 -8
  44. package/dist/extensions/identity/email-otp/index.js +3 -8
  45. package/dist/extensions/identity/email-otp/server/index.d.ts +16 -16
  46. package/dist/extensions/identity/email-otp/server/index.js +17 -14
  47. package/dist/extensions/identity/index.d.ts +6 -0
  48. package/dist/extensions/identity/index.js +6 -0
  49. package/dist/extensions/identity/internal/Verification.js +9 -5
  50. package/dist/extensions/identity/link/Constants.d.ts +102 -0
  51. package/dist/extensions/identity/link/Constants.js +16 -0
  52. package/dist/extensions/identity/link/client/index.d.ts +80 -0
  53. package/dist/extensions/identity/link/client/index.js +211 -0
  54. package/dist/extensions/identity/link/index.d.ts +12 -0
  55. package/dist/extensions/identity/link/index.js +12 -0
  56. package/dist/extensions/identity/link/server/index.d.ts +124 -0
  57. package/dist/extensions/identity/link/server/index.js +518 -0
  58. package/dist/extensions/identity/persona/Constants.d.ts +24 -0
  59. package/dist/extensions/identity/persona/Constants.js +24 -0
  60. package/dist/extensions/identity/persona/Did.d.ts +24 -0
  61. package/dist/extensions/identity/persona/Did.js +24 -0
  62. package/dist/extensions/identity/persona/Types.d.ts +21 -0
  63. package/dist/extensions/identity/persona/Types.js +2 -0
  64. package/dist/extensions/identity/persona/index.d.ts +10 -0
  65. package/dist/extensions/identity/persona/index.js +9 -0
  66. package/dist/extensions/identity/persona/server/index.d.ts +152 -0
  67. package/dist/extensions/identity/persona/server/index.js +263 -0
  68. package/dist/identity/Callback.d.ts +24 -5
  69. package/dist/identity/Callback.js +35 -11
  70. package/dist/identity/Client.d.ts +78 -5
  71. package/dist/identity/Client.js +165 -48
  72. package/dist/identity/Headers.d.ts +21 -0
  73. package/dist/identity/Headers.js +24 -0
  74. package/dist/identity/Server.d.ts +159 -37
  75. package/dist/identity/Server.js +309 -138
  76. package/dist/identity/Status.d.ts +14 -0
  77. package/dist/identity/Status.js +19 -0
  78. package/dist/identity/Types.d.ts +166 -65
  79. package/dist/identity/index.d.ts +2 -0
  80. package/dist/identity/index.js +2 -0
  81. package/dist/identity/internal/Requirements.d.ts +28 -0
  82. package/dist/identity/internal/Requirements.js +78 -0
  83. package/dist/index.d.ts +1 -0
  84. package/dist/index.js +1 -0
  85. package/dist/integrations/x402/Evm.d.ts +1 -0
  86. package/dist/internal/env.d.ts +14 -0
  87. package/dist/internal/env.js +38 -0
  88. package/dist/internal/version.d.ts +2 -2
  89. package/dist/internal/version.js +1 -1
  90. package/dist/proxy/internal/Headers.d.ts +1 -1
  91. package/dist/proxy/internal/Headers.js +11 -7
  92. package/dist/server/Mppx.d.ts +14 -6
  93. package/dist/server/Mppx.js +256 -149
  94. package/dist/server/internal/challengeMeta.d.ts +0 -6
  95. package/dist/server/internal/challengeMeta.js +3 -17
  96. package/dist/stripe/server/internal/html.gen.js +1 -1
  97. package/dist/tempo/client/Charge.d.ts +11 -8
  98. package/dist/tempo/client/Charge.js +53 -6
  99. package/dist/tempo/index.d.ts +4 -0
  100. package/dist/tempo/index.js +4 -0
  101. package/dist/tempo/internal/auto-swap.d.ts +6 -5
  102. package/dist/tempo/internal/auto-swap.js +17 -2
  103. package/dist/tempo/internal/currencies.d.ts +5 -4
  104. package/dist/tempo/internal/currencies.js +13 -9
  105. package/dist/tempo/internal/defaults.d.ts +7 -0
  106. package/dist/tempo/internal/defaults.js +7 -0
  107. package/dist/tempo/internal/fee-payer.js +8 -2
  108. package/dist/tempo/internal/machine-token-charge.d.ts +52 -0
  109. package/dist/tempo/internal/machine-token-charge.js +82 -1
  110. package/dist/tempo/server/Charge.d.ts +14 -9
  111. package/dist/tempo/server/Charge.js +64 -3
  112. package/dist/tempo/server/Methods.d.ts +1 -1
  113. package/dist/tempo/server/Methods.js +4 -1
  114. package/dist/tempo/server/Subscription.d.ts +8 -0
  115. package/dist/tempo/server/internal/fallback-store.d.ts +7 -2
  116. package/dist/tempo/server/internal/fallback-store.js +12 -3
  117. package/dist/tempo/server/internal/html.gen.js +1 -1
  118. package/dist/tempo/server/internal/transport.d.ts +1 -1
  119. package/dist/tempo/server/internal/transport.js +9 -16
  120. package/dist/tempo/session/client/CredentialState.js +31 -0
  121. package/dist/tempo/session/client/Session.d.ts +12 -6
  122. package/dist/tempo/session/client/Session.js +64 -18
  123. package/dist/tempo/session/server/ChannelStore.d.ts +22 -1
  124. package/dist/tempo/session/server/ChannelStore.js +58 -8
  125. package/dist/tempo/session/server/CloseSupervisor.d.ts +5 -1
  126. package/dist/tempo/session/server/CloseSupervisor.js +110 -12
  127. package/dist/tempo/session/server/CredentialVerification.d.ts +2 -0
  128. package/dist/tempo/session/server/CredentialVerification.js +6 -32
  129. package/dist/tempo/session/server/Session.d.ts +5 -1
  130. package/dist/tempo/session/server/Session.js +6 -3
  131. package/dist/tempo/session/server/Settlement.d.ts +7 -0
  132. package/dist/tempo/session/server/Settlement.js +36 -6
  133. package/dist/tempo/session/server/Sse.d.ts +2 -0
  134. package/dist/tempo/session/server/Sse.js +12 -12
  135. package/dist/tempo/subscription/KeyAuthorization.d.ts +1 -1
  136. package/dist/tempo/subscription/KeyAuthorization.js +1 -1
  137. package/dist/x402/Assets.d.ts +14 -5
  138. package/dist/x402/Assets.js +11 -16
  139. package/package.json +19 -3
  140. package/dist/extensions/identity/email-otp/internal/Prover.d.ts +0 -46
  141. package/dist/extensions/identity/email-otp/internal/Prover.js +0 -168
  142. package/dist/extensions/identity/email-otp/internal/Verifier.d.ts +0 -56
  143. package/dist/extensions/identity/email-otp/internal/Verifier.js +0 -116
  144. /package/dist/extensions/identity/{email-otp/internal → attestation}/Types.js +0 -0
@@ -0,0 +1,119 @@
1
+ import { serializeList, Token as SfToken } from 'structured-headers';
2
+ import * as Status from '../../../../identity/Status.js';
3
+ import { Constants } from '../Constants.js';
4
+ import * as Jwks from '../Jwks.js';
5
+ import * as Token from '../Token.js';
6
+ /**
7
+ * Creates a request-stage verifier for one type of identity attestation from one issuer.
8
+ *
9
+ * Outcomes:
10
+ * - `missing` (401): no attestation of this type and issuer, with an
11
+ * `Identity-Attestation-Required` header naming them, which a client prover answers by
12
+ * obtaining one and retrying.
13
+ * - `invalid` (401): none verified, with the same header so the client can obtain a fresh one.
14
+ * - `forbidden` (403): one verified, but `validate` rejected it.
15
+ * - `unavailable` (503): the issuer's keys could not be fetched, so the client keeps its
16
+ * attestation instead of asking for a new one.
17
+ *
18
+ * Attestations of other types or issuers in the same header are left to their own verifiers.
19
+ * An attestation is a bearer proof bound to this server's origin until it expires; it says what
20
+ * the issuer checked, not what the subject may do.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const phone = Attestation.Server.verifier({
25
+ * audience: 'https://api.example',
26
+ * issuer: 'https://issuer.example',
27
+ * type: 'phone',
28
+ * })
29
+ * mppx.charge({ amount: '1', identity: [phone] })
30
+ * ```
31
+ */
32
+ export function verifier(config) {
33
+ const issuer = config.issuer.replace(/\/+$/, '');
34
+ if (!Token.isIssuer(issuer))
35
+ throw new TypeError('Attestation issuer must be an https URL (http only on localhost).');
36
+ Token.assertTimes({ clockTolerance: config.clockTolerance, maxAge: config.maxAge });
37
+ const keyResolver = config.keyResolver ?? Jwks.resolver({ fetch: config.fetch });
38
+ const tolerance = config.clockTolerance ?? Constants.clockTolerance;
39
+ const required = Object.freeze({
40
+ [Constants.requiredHeader]: serializeList([
41
+ [new SfToken(config.type), new Map([['issuer', issuer]])],
42
+ ]),
43
+ });
44
+ return {
45
+ async verify(request) {
46
+ const audience = origin(typeof config.audience === 'function' ? config.audience(request) : config.audience);
47
+ // At most `maxCandidates` distinct tokens are verified, latest expiry first,
48
+ // so repeated or forged tokens cost a bounded number of signature checks.
49
+ // A stray forged token does not hide a valid one, but whoever controls the
50
+ // header can always withhold a valid token, so the bound is not a guarantee
51
+ // against that sender. Without a usable one, the first is verified so its
52
+ // rejection says why.
53
+ const now = Math.floor(Date.now() / 1_000);
54
+ const candidates = Token.parseHeader(request.headers.get(Constants.header))
55
+ .map((token) => ({ token, claims: Token.decode(token)?.claims }))
56
+ .filter(({ claims }) => claims?.iss === issuer && claims.type === config.type);
57
+ const usable = candidates
58
+ .filter(({ claims }) => claims?.aud === audience && (claims.exp ?? 0) > now - tolerance)
59
+ .sort((a, b) => (b.claims?.exp ?? 0) - (a.claims?.exp ?? 0));
60
+ const tokens = [
61
+ ...new Set((usable.length > 0 ? usable : candidates).map(({ token }) => token)),
62
+ ].slice(0, usable.length > 0 ? maxCandidates : 1);
63
+ if (tokens.length === 0)
64
+ return {
65
+ status: 'missing',
66
+ headers: required,
67
+ reason: `An identity attestation of type "${config.type}" is required.`,
68
+ };
69
+ // Every token's outcome, so the most decisive one answers when none is valid.
70
+ const rejections = [];
71
+ for (const token of tokens) {
72
+ const result = await Token.verify(token, {
73
+ audience,
74
+ clockTolerance: config.clockTolerance,
75
+ issuer,
76
+ keyResolver,
77
+ maxAge: config.maxAge,
78
+ type: config.type,
79
+ });
80
+ if (result.status !== 'valid') {
81
+ rejections.push(result);
82
+ continue;
83
+ }
84
+ // A later attestation, such as another address, may still be allowed.
85
+ // Only `undefined` accepts, so an empty or otherwise falsy reason still rejects.
86
+ const reason = config.validate ? await config.validate(result.claims, request) : undefined;
87
+ if (reason !== undefined) {
88
+ rejections.push({
89
+ status: 'forbidden',
90
+ reason: typeof reason === 'string' && reason ? reason : defaultForbiddenReason,
91
+ });
92
+ continue;
93
+ }
94
+ return { status: 'valid', value: Object.freeze({ claims: result.claims }) };
95
+ }
96
+ const rejection = decisive(rejections);
97
+ // Only an invalid attestation is worth replacing: the client may obtain a fresh one.
98
+ return rejection.status === 'invalid' ? { ...rejection, headers: required } : rejection;
99
+ },
100
+ };
101
+ }
102
+ /** The reason sent when `validate` rejects without a usable one. */
103
+ const defaultForbiddenReason = 'This identity attestation is not allowed.';
104
+ /** Returns the first rejection of the status with the highest precedence. `rejections` is never empty. */
105
+ function decisive(rejections) {
106
+ for (const status of Status.precedence) {
107
+ const rejection = rejections.find((candidate) => candidate.status === status);
108
+ if (rejection)
109
+ return rejection;
110
+ }
111
+ return rejections[0];
112
+ }
113
+ /** Most distinct attestations of one type and issuer verified per request. */
114
+ const maxCandidates = 3;
115
+ /** An audience as the origin attestations carry, so `https://api.example/` matches. */
116
+ function origin(audience) {
117
+ return URL.canParse(audience) ? new URL(audience).origin : audience;
118
+ }
119
+ //# sourceMappingURL=index.js.map
@@ -1,9 +1,9 @@
1
- /** Email OTP attestation values. */
1
+ /** Email OTP attestation values. Values every attestation shares are in `Attestation.Constants`. */
2
2
  export declare const Constants: {
3
- /** Request header carrying the email attestation. */
4
- readonly header: 'Identity-Attestation';
5
- /** Response header naming the issuer to get an attestation from. */
6
- readonly requiredHeader: 'Identity-Attestation-Required';
3
+ /** @deprecated Use `Attestation.Constants.header`. */
4
+ readonly header: "Identity-Attestation";
5
+ /** @deprecated Use `Attestation.Constants.requiredHeader`. */
6
+ readonly requiredHeader: "Identity-Attestation-Required";
7
7
  /** Attestation type for a verified email address. */
8
8
  readonly type: 'email';
9
9
  /** Issuer path that emails a one-time code. */
@@ -1,9 +1,10 @@
1
- /** Email OTP attestation values. */
1
+ import { Constants as AttestationConstants } from '../attestation/Constants.js';
2
+ /** Email OTP attestation values. Values every attestation shares are in `Attestation.Constants`. */
2
3
  export const Constants = {
3
- /** Request header carrying the email attestation. */
4
- header: 'Identity-Attestation',
5
- /** Response header naming the issuer to get an attestation from. */
6
- requiredHeader: 'Identity-Attestation-Required',
4
+ /** @deprecated Use `Attestation.Constants.header`. */
5
+ header: AttestationConstants.header,
6
+ /** @deprecated Use `Attestation.Constants.requiredHeader`. */
7
+ requiredHeader: AttestationConstants.requiredHeader,
7
8
  /** Attestation type for a verified email address. */
8
9
  type: 'email',
9
10
  /** Issuer path that emails a one-time code. */
@@ -1,12 +1,12 @@
1
1
  /** A verified email address. */
2
2
  export type VerifiedEmail = {
3
3
  /** The email address the issuer sent a one-time code to, lowercased. */
4
- email: string;
4
+ readonly email: string;
5
5
  /** Issuer URL. */
6
- issuer: string;
6
+ readonly issuer: string;
7
7
  /** When the attestation expires. */
8
- expiresAt: Date;
8
+ readonly expiresAt: Date;
9
9
  /** Attestation identifier (`jti`), if the issuer set one. */
10
- id?: string | undefined;
10
+ readonly id?: string | undefined;
11
11
  };
12
12
  //# sourceMappingURL=Types.d.ts.map
@@ -41,13 +41,24 @@ export declare namespace verifyCode {
41
41
  };
42
42
  }
43
43
  /**
44
- * Creates a prover that verifies an email address when a server asks for one.
44
+ * Creates a prover that verifies an email address when a server asks for one: an
45
+ * `Attestation.Client.prover` of type `email` whose `acquire` sends the code, prompts and verifies.
45
46
  *
46
47
  * When a server answers with `Identity-Attestation-Required: email;issuer="…"`,
47
48
  * the prover asks the issuer to email a code to `email`, gets the code from
48
49
  * `code` (such as a terminal prompt or a form), exchanges it for an
49
50
  * attestation bound to the server's origin and sends the request again with it.
50
- * Later requests to that origin reuse it until it expires.
51
+ * Later requests to that origin reuse it until it expires. A route that accepts any of several
52
+ * issuers (`Identity.anyOf`) lists them as alternatives: the prover asks the first trusted one,
53
+ * and the next only if that one fails or is declined. A route that requires several issuers gets
54
+ * a code from, and a prompt for, each of them.
55
+ *
56
+ * Trust is set by two options together:
57
+ * - `scope`: the origins that may ask for and receive the attestation. It is always
58
+ * bound to the request's own origin, never one a server names.
59
+ * - `issuers`: the issuers the email address may be sent to. A server only names the
60
+ * issuer it wants; an untrusted one could relay a code and obtain an attestation
61
+ * for another server.
51
62
  *
52
63
  * @example
53
64
  * ```ts
@@ -56,8 +67,9 @@ export declare namespace verifyCode {
56
67
  * EmailOtp.Client.prover({
57
68
  * code: () => prompt('Code:'),
58
69
  * email: 'me@example.com',
70
+ * // Only this issuer receives the address, and only this origin the attestation.
59
71
  * issuers: ['https://issuer.example'],
60
- * scope: '*',
72
+ * scope: ['https://api.example'],
61
73
  * }),
62
74
  * ],
63
75
  * methods: [tempo({ account })],
@@ -68,14 +80,14 @@ export declare function prover(config: prover.Config): Identity.Prover;
68
80
  export declare namespace prover {
69
81
  type Config = {
70
82
  /** Origins that may ask for and receive the attestation, or `'*'` for any origin. */
71
- scope: Identity.Prover['scope'];
83
+ readonly scope: Identity.Prover['scope'];
72
84
  /** Email address to verify, or a function that returns it (or `undefined` to decline). */
73
- email: string | ((parameters: {
85
+ readonly email: string | ((parameters: {
74
86
  audience: string;
75
87
  issuer: string;
76
88
  }) => MaybePromise<string | undefined>);
77
89
  /** Returns the code the user received, or `undefined` to give up. */
78
- code: (parameters: {
90
+ readonly code: (parameters: {
79
91
  /** Origin the attestation is for. */
80
92
  audience: string;
81
93
  /** Email address the code was sent to. */
@@ -92,16 +104,11 @@ export declare namespace prover {
92
104
  error?: Error | undefined;
93
105
  }) => MaybePromise<string | undefined>;
94
106
  /** Attestations obtained beforehand, such as from the issuer with curl. */
95
- attestations?: readonly string[] | undefined;
96
- /**
97
- * Issuers the prover may send the email address to, such as `['https://identity.mpp.tempo.xyz']`.
98
- *
99
- * A server only names the issuer it wants; an untrusted one could relay a code
100
- * and obtain an attestation for another server.
101
- */
102
- issuers: readonly string[];
107
+ readonly attestations?: readonly string[] | undefined;
108
+ /** Issuers the prover may send the email address to, such as `['https://identity.mpp.tempo.xyz']`. */
109
+ readonly issuers: readonly string[];
103
110
  /** Fetch used to call the issuer. @default globalThis.fetch */
104
- fetch?: typeof globalThis.fetch | undefined;
111
+ readonly fetch?: typeof globalThis.fetch | undefined;
105
112
  };
106
113
  }
107
114
  /** An issuer rejected an email OTP request. */
@@ -1,7 +1,7 @@
1
- import { unwrapFetch } from '../../../../client/internal/Fetch.js';
1
+ import { unwrapFetch, withoutPayment } from '../../../../client/internal/Fetch.js';
2
+ import * as Attestation from '../../attestation/client/index.js';
3
+ import { isIssuer } from '../../attestation/Token.js';
2
4
  import { Constants } from '../Constants.js';
3
- import { isIssuer } from '../internal/Attestation.js';
4
- import * as Attestation from '../internal/Prover.js';
5
5
  /**
6
6
  * Asks an issuer to email a one-time code for `audience`.
7
7
  *
@@ -36,13 +36,24 @@ function date(value) {
36
36
  return Number.isFinite(result.getTime()) ? result : undefined;
37
37
  }
38
38
  /**
39
- * Creates a prover that verifies an email address when a server asks for one.
39
+ * Creates a prover that verifies an email address when a server asks for one: an
40
+ * `Attestation.Client.prover` of type `email` whose `acquire` sends the code, prompts and verifies.
40
41
  *
41
42
  * When a server answers with `Identity-Attestation-Required: email;issuer="…"`,
42
43
  * the prover asks the issuer to email a code to `email`, gets the code from
43
44
  * `code` (such as a terminal prompt or a form), exchanges it for an
44
45
  * attestation bound to the server's origin and sends the request again with it.
45
- * Later requests to that origin reuse it until it expires.
46
+ * Later requests to that origin reuse it until it expires. A route that accepts any of several
47
+ * issuers (`Identity.anyOf`) lists them as alternatives: the prover asks the first trusted one,
48
+ * and the next only if that one fails or is declined. A route that requires several issuers gets
49
+ * a code from, and a prompt for, each of them.
50
+ *
51
+ * Trust is set by two options together:
52
+ * - `scope`: the origins that may ask for and receive the attestation. It is always
53
+ * bound to the request's own origin, never one a server names.
54
+ * - `issuers`: the issuers the email address may be sent to. A server only names the
55
+ * issuer it wants; an untrusted one could relay a code and obtain an attestation
56
+ * for another server.
46
57
  *
47
58
  * @example
48
59
  * ```ts
@@ -51,8 +62,9 @@ function date(value) {
51
62
  * EmailOtp.Client.prover({
52
63
  * code: () => prompt('Code:'),
53
64
  * email: 'me@example.com',
65
+ * // Only this issuer receives the address, and only this origin the attestation.
54
66
  * issuers: ['https://issuer.example'],
55
- * scope: '*',
67
+ * scope: ['https://api.example'],
56
68
  * }),
57
69
  * ],
58
70
  * methods: [tempo({ account })],
@@ -103,14 +115,14 @@ async function post(issuer, path, fetch, body) {
103
115
  // The address and code never go over plaintext HTTP, except to localhost.
104
116
  if (!isIssuer(issuer))
105
117
  throw new EmailOtpError('The issuer must be an https URL (http only on localhost).');
106
- // The unwrapped fetch, so an issuer's 402 is never paid automatically.
107
- const response = await unwrapFetch(fetch ?? globalThis.fetch)(`${issuer.replace(/\/+$/, '')}${path}`, {
118
+ // Untagged forwarding wrappers survive unwrapping, so also opt out explicitly.
119
+ const response = await unwrapFetch(fetch ?? globalThis.fetch)(`${issuer.replace(/\/+$/, '')}${path}`, withoutPayment({
108
120
  body: JSON.stringify(body),
109
121
  headers: { 'content-type': 'application/json' },
110
122
  method: 'POST',
111
123
  // Workers reject 'error'; a redirect is not ok, so it fails below.
112
124
  redirect: 'manual',
113
- });
125
+ }));
114
126
  const json = (await response.json().catch(() => ({})));
115
127
  if (!response.ok)
116
128
  throw new EmailOtpError(typeof json.error === 'string' ? json.error : `The issuer answered ${response.status}.`, { status: response.status });
@@ -1,12 +1,7 @@
1
1
  /**
2
- * Email OTP identity: an issuer emails a one-time code and signs an attestation
3
- * the client sends in `Identity-Attestation`.
4
- *
5
- * Limitation: the attestation format, its key lookup and the
6
- * `Identity-Attestation` headers are internal to this extension. Another
7
- * attestation type (phone, KYC) needs its own extension, or this one made
8
- * generic, once a second issuer exists. For your own identity system, use
9
- * `Identity.from` from `mppx/identity`.
2
+ * Email OTP identity: an issuer emails a one-time code and signs an `email` attestation
3
+ * (`mppx/extensions/identity/attestation`) the client sends in `Identity-Attestation`.
4
+ * For your own identity system, use `Identity.from` from `mppx/identity`.
10
5
  */
11
6
  /** Client helpers that verify an email address with a one-time code. */
12
7
  export * as Client from './client/index.js';
@@ -1,12 +1,7 @@
1
1
  /**
2
- * Email OTP identity: an issuer emails a one-time code and signs an attestation
3
- * the client sends in `Identity-Attestation`.
4
- *
5
- * Limitation: the attestation format, its key lookup and the
6
- * `Identity-Attestation` headers are internal to this extension. Another
7
- * attestation type (phone, KYC) needs its own extension, or this one made
8
- * generic, once a second issuer exists. For your own identity system, use
9
- * `Identity.from` from `mppx/identity`.
2
+ * Email OTP identity: an issuer emails a one-time code and signs an `email` attestation
3
+ * (`mppx/extensions/identity/attestation`) the client sends in `Identity-Attestation`.
4
+ * For your own identity system, use `Identity.from` from `mppx/identity`.
10
5
  */
11
6
  /** Client helpers that verify an email address with a one-time code. */
12
7
  export * as Client from './client/index.js';
@@ -1,14 +1,16 @@
1
1
  import type * as Identity from '../../../../identity/Types.js';
2
2
  import type { MaybePromise } from '../../../../internal/types.js';
3
- import type * as AttestationTypes from '../internal/Types.js';
3
+ import * as Attestation from '../../attestation/server/index.js';
4
+ import type * as AttestationTypes from '../../attestation/Types.js';
4
5
  import type * as Types from '../Types.js';
5
6
  /**
6
- * Creates a request-stage verifier that requires a verified email address.
7
+ * Creates a request-stage verifier that requires a verified email address: an
8
+ * `Attestation.Server.verifier` of type `email` whose `validate` is `allow`.
7
9
  *
8
10
  * A request without one gets 401 with `Identity-Attestation-Required: email;issuer="…"`.
9
11
  * The client obtains an attestation from the issuer with a one-time code sent to
10
- * the address and sends it in `Identity-Attestation`. `allow` applies the
11
- * route's policy, such as a domain allowlist.
12
+ * the address and sends it in `Identity-Attestation`. An address `allow` rejects is
13
+ * `forbidden` (403).
12
14
  *
13
15
  * @example
14
16
  * ```ts
@@ -25,24 +27,22 @@ export declare function verifier(config: verifier.Config): Identity.Verifier<Typ
25
27
  export declare namespace verifier {
26
28
  type Config = {
27
29
  /** Trusted issuer URL. */
28
- issuer: string;
29
- /** Accepts or rejects (403) a verified, lowercased email address. Defaults to any. */
30
- allow?: ((email: string, request: Request) => MaybePromise<boolean>) | undefined;
30
+ readonly issuer: string;
31
+ /** Accepts or rejects (`forbidden`, 403) a verified, lowercased email address. Defaults to any. */
32
+ readonly allow?: ((email: string, request: Request) => MaybePromise<boolean>) | undefined;
31
33
  /**
32
- * This server's public origin, such as `https://api.example`, which attestations must be bound to.
33
- *
34
- * A function may derive it from the request only where the platform
35
- * guarantees the request's host (such as a Cloudflare Worker route).
34
+ * This server's public origin, such as `https://api.example`, which attestations must be
35
+ * bound to. The function form is deprecated: see `Attestation.Server.verifier.AudienceFunction`.
36
36
  */
37
- audience: string | ((request: Request) => string);
37
+ readonly audience: string | Attestation.verifier.AudienceFunction;
38
38
  /** Resolves issuer keys. Defaults to the issuer's JSON Web Key Set. */
39
- keyResolver?: AttestationTypes.KeyResolver | undefined;
39
+ readonly keyResolver?: AttestationTypes.KeyResolver | undefined;
40
40
  /** Fetch used for the default JSON Web Key Set resolver. */
41
- fetch?: typeof globalThis.fetch | undefined;
41
+ readonly fetch?: typeof globalThis.fetch | undefined;
42
42
  /** Oldest accepted attestation, in seconds since it was issued. Defaults to its own expiry. */
43
- maxAge?: number | undefined;
43
+ readonly maxAge?: number | undefined;
44
44
  /** Clock tolerance in seconds. @default 60 */
45
- clockTolerance?: number | undefined;
45
+ readonly clockTolerance?: number | undefined;
46
46
  };
47
47
  }
48
48
  //# sourceMappingURL=index.d.ts.map
@@ -1,12 +1,13 @@
1
+ import * as Attestation from '../../attestation/server/index.js';
1
2
  import { Constants } from '../Constants.js';
2
- import * as Attestation from '../internal/Verifier.js';
3
3
  /**
4
- * Creates a request-stage verifier that requires a verified email address.
4
+ * Creates a request-stage verifier that requires a verified email address: an
5
+ * `Attestation.Server.verifier` of type `email` whose `validate` is `allow`.
5
6
  *
6
7
  * A request without one gets 401 with `Identity-Attestation-Required: email;issuer="…"`.
7
8
  * The client obtains an attestation from the issuer with a one-time code sent to
8
- * the address and sends it in `Identity-Attestation`. `allow` applies the
9
- * route's policy, such as a domain allowlist.
9
+ * the address and sends it in `Identity-Attestation`. An address `allow` rejects is
10
+ * `forbidden` (403).
10
11
  *
11
12
  * @example
12
13
  * ```ts
@@ -20,6 +21,7 @@ import * as Attestation from '../internal/Verifier.js';
20
21
  * ```
21
22
  */
22
23
  export function verifier(config) {
24
+ const { allow } = config;
23
25
  const inner = Attestation.verifier({
24
26
  audience: config.audience,
25
27
  clockTolerance: config.clockTolerance,
@@ -28,27 +30,28 @@ export function verifier(config) {
28
30
  keyResolver: config.keyResolver,
29
31
  maxAge: config.maxAge,
30
32
  type: Constants.type,
31
- async validate(claims, request) {
32
- if (!config.allow || (await config.allow(claims.sub.toLowerCase(), request)))
33
- return undefined;
34
- return 'This email address is not allowed.';
35
- },
33
+ ...(allow && {
34
+ async validate(claims, request) {
35
+ if (await allow(claims.sub.toLowerCase(), request))
36
+ return undefined;
37
+ return 'This email address is not allowed.';
38
+ },
39
+ }),
36
40
  });
37
41
  return {
38
- stage: 'request',
39
- async verify(request, context) {
40
- const result = await inner.verify(request, context);
42
+ async verify(request) {
43
+ const result = await inner.verify(request);
41
44
  if (result.status !== 'valid')
42
45
  return result;
43
46
  const { claims } = result.value;
44
47
  return {
45
48
  status: 'valid',
46
- value: {
49
+ value: Object.freeze({
47
50
  email: claims.sub.toLowerCase(),
48
51
  expiresAt: new Date(claims.exp * 1_000),
49
52
  id: claims.jti,
50
53
  issuer: claims.iss,
51
- },
54
+ }),
52
55
  };
53
56
  },
54
57
  };
@@ -1,5 +1,11 @@
1
+ /** Identity attestations: issuer-signed tokens a server requires and a client presents. */
2
+ export * as Attestation from './attestation/index.js';
1
3
  /** Email address verification with a one-time code. */
2
4
  export * as EmailOtp from './email-otp/index.js';
5
+ /** Link agent identity: attestation tokens and Link-signed claims. */
6
+ export * as Link from './link/index.js';
7
+ /** Persona KYC: a payment check that requires an approved Persona inquiry. */
8
+ export * as Persona from './persona/index.js';
3
9
  /** Client and server adapters for the TAP agent-recognition signature profile. */
4
10
  export * as Tap from './tap/index.js';
5
11
  /** Client and server adapters for the Web Bot Auth HTTPS-directory profile. */
@@ -1,5 +1,11 @@
1
+ /** Identity attestations: issuer-signed tokens a server requires and a client presents. */
2
+ export * as Attestation from './attestation/index.js';
1
3
  /** Email address verification with a one-time code. */
2
4
  export * as EmailOtp from './email-otp/index.js';
5
+ /** Link agent identity: attestation tokens and Link-signed claims. */
6
+ export * as Link from './link/index.js';
7
+ /** Persona KYC: a payment check that requires an approved Persona inquiry. */
8
+ export * as Persona from './persona/index.js';
3
9
  /** Client and server adapters for the TAP agent-recognition signature profile. */
4
10
  export * as Tap from './tap/index.js';
5
11
  /** Client and server adapters for the Web Bot Auth HTTPS-directory profile. */
@@ -1,10 +1,14 @@
1
+ /** The identity status of each non-verified HTTP message signature result. */
2
+ const statuses = {
3
+ absent: 'missing',
4
+ invalid: 'invalid',
5
+ // The signature was presented, but its key is not trusted or could not be resolved.
6
+ unverified: 'forbidden',
7
+ };
1
8
  /** Maps a non-verified HTTP message signature result to an identity verification. */
2
9
  export function fromSignature(result) {
3
10
  if (result.status === 'absent')
4
- return { status: 'missing' };
5
- if (result.status === 'invalid')
6
- return { status: 'invalid', reason: result.reason };
7
- // The signature was presented but its key is not trusted or available.
8
- return { status: 'invalid', code: 403, reason: result.reason };
11
+ return { status: statuses.absent };
12
+ return { status: statuses[result.status], reason: result.reason };
9
13
  }
10
14
  //# sourceMappingURL=Verification.js.map
@@ -0,0 +1,102 @@
1
+ import type { MaybePromise } from '../../../internal/types.js';
2
+ /** Link agent identity values, as Link's Agent Identity SDK and Link CLI use them. */
3
+ export declare const Constants: {
4
+ /** Link's issuer, the only one the Agent Identity SDK trusts. */
5
+ readonly issuer: 'https://api.link.com';
6
+ /** `Authorization` scheme of an Agent Attestation Token (RFC 9577). */
7
+ readonly attestationScheme: 'PrivateToken';
8
+ /** Request header carrying an identity presentation, and its `WWW-Authenticate` scheme. */
9
+ readonly presentationHeader: 'Identity-Presentation';
10
+ /** `type` of the problem body of a claims challenge. */
11
+ readonly claimsRequiredType: 'urn:stripe:link:claims-required';
12
+ /** Presentation format of a claims challenge. */
13
+ readonly format: 'dc+sd-jwt';
14
+ /** Seconds a claims challenge's nonce is accepted for. */
15
+ readonly nonceTtl: 300;
16
+ };
17
+ /** One failure of a Link verification, as `@stripe/agent-identity` reports it. */
18
+ export type Failure = {
19
+ readonly code: string;
20
+ readonly message: string;
21
+ };
22
+ /** Verifies Link Agent Attestation Tokens: a `LinkVerifier`, or `Link.Server.tokenVerifier()`. */
23
+ export type AttestationVerifier = Pick<LinkVerifier, 'attestationChallenge' | 'verifyAttestation'>;
24
+ /** Verifies Link identity presentations: a `LinkVerifier`, or `Link.Server.presentationVerifier()`. */
25
+ export type ClaimsVerifier = Pick<LinkVerifier, 'verifyClaims'>;
26
+ /**
27
+ * The parts of `LinkVerifier` from `@stripe/agent-identity` the verifiers use. Pass one
28
+ * `new LinkVerifier({ origin })` per process, so every request shares its issuer key cache.
29
+ */
30
+ export type LinkVerifier = {
31
+ attestationChallenge(): Promise<{
32
+ wwwAuthenticate: string[];
33
+ }>;
34
+ verifyAttestation(authorization: string | null | undefined): Promise<{
35
+ valid: true;
36
+ issuer: string;
37
+ tokenKeyId: string;
38
+ bindingMode?: 'bearer';
39
+ } | {
40
+ valid: false;
41
+ failures: readonly Failure[];
42
+ }>;
43
+ verifyClaims(presentation: string | null | undefined, options: {
44
+ nonce: string;
45
+ requiredClaims: string[];
46
+ }): Promise<{
47
+ valid: true;
48
+ issuer: string;
49
+ vct: string;
50
+ claims: Record<string, unknown>;
51
+ holderKeyThumbprint: string;
52
+ } | {
53
+ valid: false;
54
+ failures: readonly Failure[];
55
+ }>;
56
+ };
57
+ /** A verified Link Agent Attestation Token: Link vouches for the agent, and says nothing else. */
58
+ export type VerifiedAttestation = {
59
+ /** Issuer whose key signed the token. */
60
+ readonly issuer: string;
61
+ /** base64url of the token's `token_key_id`. */
62
+ readonly tokenKeyId: string;
63
+ };
64
+ /** Claims Link signed and the holder disclosed to this server. */
65
+ export type VerifiedClaims = {
66
+ /** Issuer of the credential. */
67
+ readonly issuer: string;
68
+ /** Credential type (`vct`). */
69
+ readonly vct: string;
70
+ /** The disclosed claims, by name. */
71
+ readonly claims: Readonly<Record<string, unknown>>;
72
+ /** RFC 7638 thumbprint of the holder key that signed the presentation. */
73
+ readonly holderKeyThumbprint: string;
74
+ /** The verified email address, lowercased, when `email` and a true `email_verified` were disclosed. */
75
+ readonly email?: string | undefined;
76
+ };
77
+ /** A claims challenge: the problem body of a 401 with `WWW-Authenticate: Identity-Presentation`. */
78
+ export type ClaimsChallenge = {
79
+ readonly type: 'urn:stripe:link:claims-required';
80
+ /** Origin the presentation must be bound to. */
81
+ readonly aud: string;
82
+ /** Nonce the presentation must sign. */
83
+ readonly nonce: string;
84
+ /** Claims to disclose. */
85
+ readonly claims: readonly string[];
86
+ /** Why the server asks, for the person whose claims they are. */
87
+ readonly purpose?: string | undefined;
88
+ readonly formats: readonly string[];
89
+ readonly trusted_issuers: readonly string[];
90
+ };
91
+ /** Creates a presentation for a claims challenge, such as with `link-cli identity credentials present`. */
92
+ export type Present = (parameters: {
93
+ /** Origin the presentation is for: always the request's own origin. */
94
+ audience: string;
95
+ /** Nonce from the challenge. */
96
+ nonce: string;
97
+ /** Claims to disclose. */
98
+ claims: readonly string[];
99
+ /** Why the server asks. */
100
+ purpose?: string | undefined;
101
+ }) => MaybePromise<string | undefined>;
102
+ //# sourceMappingURL=Constants.d.ts.map
@@ -0,0 +1,16 @@
1
+ /** Link agent identity values, as Link's Agent Identity SDK and Link CLI use them. */
2
+ export const Constants = {
3
+ /** Link's issuer, the only one the Agent Identity SDK trusts. */
4
+ issuer: 'https://api.link.com',
5
+ /** `Authorization` scheme of an Agent Attestation Token (RFC 9577). */
6
+ attestationScheme: 'PrivateToken',
7
+ /** Request header carrying an identity presentation, and its `WWW-Authenticate` scheme. */
8
+ presentationHeader: 'Identity-Presentation',
9
+ /** `type` of the problem body of a claims challenge. */
10
+ claimsRequiredType: 'urn:stripe:link:claims-required',
11
+ /** Presentation format of a claims challenge. */
12
+ format: 'dc+sd-jwt',
13
+ /** Seconds a claims challenge's nonce is accepted for. */
14
+ nonceTtl: 300,
15
+ };
16
+ //# sourceMappingURL=Constants.js.map