mppx 0.17.5 → 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.
- package/CHANGELOG.md +40 -0
- package/dist/cli/cli.js +141 -139
- package/dist/cli/plugins/index.js +6 -6
- package/dist/cli/stripe.d.ts +6 -1
- package/dist/cli/stripe.js +89 -5
- package/dist/client/Mppx.js +13 -3
- package/dist/client/internal/Fetch.js +9 -5
- package/dist/extensions/identity/attestation/Constants.d.ts +23 -0
- package/dist/extensions/identity/attestation/Constants.js +23 -0
- package/dist/extensions/identity/attestation/Jwks.d.ts +30 -0
- package/dist/extensions/identity/attestation/Jwks.js +114 -0
- package/dist/extensions/identity/attestation/Token.d.ts +80 -0
- package/dist/extensions/identity/attestation/Token.js +143 -0
- package/dist/extensions/identity/attestation/Types.d.ts +48 -0
- package/dist/extensions/identity/attestation/Types.js +2 -0
- 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 +16 -0
- package/dist/extensions/identity/email-otp/Constants.js +17 -0
- package/dist/extensions/identity/email-otp/Types.d.ts +12 -0
- package/dist/extensions/identity/email-otp/Types.js +2 -0
- package/dist/extensions/identity/email-otp/client/index.d.ts +123 -0
- package/dist/extensions/identity/email-otp/client/index.js +131 -0
- package/dist/extensions/identity/email-otp/index.d.ts +12 -0
- package/dist/extensions/identity/email-otp/index.js +11 -0
- package/dist/extensions/identity/email-otp/server/index.d.ts +48 -0
- package/dist/extensions/identity/email-otp/server/index.js +59 -0
- package/dist/extensions/identity/index.d.ts +4 -0
- package/dist/extensions/identity/index.js +4 -0
- package/dist/extensions/identity/internal/Verification.js +9 -5
- package/dist/identity/Callback.d.ts +66 -0
- package/dist/identity/Callback.js +88 -0
- package/dist/identity/Client.d.ts +78 -5
- package/dist/identity/Client.js +165 -48
- package/dist/identity/Headers.d.ts +16 -0
- package/dist/identity/Headers.js +19 -0
- package/dist/identity/Server.d.ts +159 -37
- package/dist/identity/Server.js +307 -138
- package/dist/identity/Status.d.ts +14 -0
- package/dist/identity/Status.js +19 -0
- package/dist/identity/Types.d.ts +161 -65
- package/dist/identity/index.d.ts +3 -0
- package/dist/identity/index.js +3 -0
- package/dist/identity/internal/Requirements.d.ts +28 -0
- package/dist/identity/internal/Requirements.js +78 -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 +7 -1
- package/dist/server/Mppx.d.ts +14 -6
- package/dist/server/Mppx.js +252 -148
- package/dist/server/internal/challengeMeta.d.ts +0 -6
- package/dist/server/internal/challengeMeta.js +3 -17
- package/dist/tempo/server/Charge.d.ts +14 -9
- package/dist/tempo/server/Charge.js +5 -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/session/server/Session.d.ts +5 -1
- package/package.json +9 -1
|
@@ -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 request-stage verifier for one type of identity attestation from one issuer.
|
|
6
|
+
*
|
|
7
|
+
* Outcomes:
|
|
8
|
+
* - `missing` (401): no attestation of this type and issuer, with an
|
|
9
|
+
* `Identity-Attestation-Required` header naming them, which a client prover answers by
|
|
10
|
+
* obtaining one and retrying.
|
|
11
|
+
* - `invalid` (401): none verified, with the same header so the client can obtain a fresh one.
|
|
12
|
+
* - `forbidden` (403): one verified, but `validate` rejected it.
|
|
13
|
+
* - `unavailable` (503): the issuer's keys could not be fetched, so the client keeps its
|
|
14
|
+
* attestation instead of asking for a new one.
|
|
15
|
+
*
|
|
16
|
+
* Attestations of other types or issuers in the same header are left to their own verifiers.
|
|
17
|
+
* An attestation is a bearer proof bound to this server's origin until it expires; it says what
|
|
18
|
+
* the issuer checked, not what the subject may do.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const phone = Attestation.Server.verifier({
|
|
23
|
+
* audience: 'https://api.example',
|
|
24
|
+
* issuer: 'https://issuer.example',
|
|
25
|
+
* type: 'phone',
|
|
26
|
+
* })
|
|
27
|
+
* mppx.charge({ amount: '1', identity: [phone] })
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function verifier(config: verifier.Config): Identity.Verifier<Types.Verified>;
|
|
31
|
+
export declare namespace verifier {
|
|
32
|
+
/**
|
|
33
|
+
* Derives this server's origin from the request.
|
|
34
|
+
*
|
|
35
|
+
* @deprecated Pass the origin as a string. A function is only safe where the platform
|
|
36
|
+
* guarantees the request's host (such as a Cloudflare Worker route): many Node servers build
|
|
37
|
+
* the URL from the client's `Host` header, which would let another server's attestation through.
|
|
38
|
+
*/
|
|
39
|
+
type AudienceFunction = (request: Request) => string;
|
|
40
|
+
type Config = {
|
|
41
|
+
/** Accepted attestation type, such as `email` or `phone`. */
|
|
42
|
+
readonly type: string;
|
|
43
|
+
/** Trusted issuer URL. */
|
|
44
|
+
readonly issuer: string;
|
|
45
|
+
/**
|
|
46
|
+
* This server's public origin, such as `https://api.example`, which attestations must be
|
|
47
|
+
* bound to. The function form is deprecated: see {@link AudienceFunction}.
|
|
48
|
+
*/
|
|
49
|
+
readonly audience: string | AudienceFunction;
|
|
50
|
+
/** Resolves issuer keys. Defaults to the issuer's JSON Web Key Set. */
|
|
51
|
+
readonly keyResolver?: Types.KeyResolver | undefined;
|
|
52
|
+
/** Fetch used for the default JSON Web Key Set resolver. */
|
|
53
|
+
readonly fetch?: typeof globalThis.fetch | undefined;
|
|
54
|
+
/** Oldest accepted `iat`, in seconds before now. Defaults to the attestation's own `exp`. */
|
|
55
|
+
readonly maxAge?: number | undefined;
|
|
56
|
+
/** Clock tolerance in seconds. @default 60 */
|
|
57
|
+
readonly clockTolerance?: number | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Applies the route's policy to verified claims. Return `undefined` to accept it; any other
|
|
60
|
+
* value, an empty string included, rejects the request as `forbidden` (403), with the value as
|
|
61
|
+
* the reason when it is a non-empty string.
|
|
62
|
+
*/
|
|
63
|
+
readonly validate?: ((claims: Types.Claims, request: Request) => MaybePromise<string | undefined>) | undefined;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -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
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Email OTP attestation values. Values every attestation shares are in `Attestation.Constants`. */
|
|
2
|
+
export declare const Constants: {
|
|
3
|
+
/** @deprecated Use `Attestation.Constants.header`. */
|
|
4
|
+
readonly header: "Identity-Attestation";
|
|
5
|
+
/** @deprecated Use `Attestation.Constants.requiredHeader`. */
|
|
6
|
+
readonly requiredHeader: "Identity-Attestation-Required";
|
|
7
|
+
/** Attestation type for a verified email address. */
|
|
8
|
+
readonly type: 'email';
|
|
9
|
+
/** Issuer path that emails a one-time code. */
|
|
10
|
+
readonly otpPath: '/email/otp';
|
|
11
|
+
/** Issuer path that exchanges a one-time code for an attestation. */
|
|
12
|
+
readonly verifyPath: '/email/verify';
|
|
13
|
+
/** Times the prover asks for a code before giving up. */
|
|
14
|
+
readonly codeAttempts: 3;
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=Constants.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Constants as AttestationConstants } from '../attestation/Constants.js';
|
|
2
|
+
/** Email OTP attestation values. Values every attestation shares are in `Attestation.Constants`. */
|
|
3
|
+
export const Constants = {
|
|
4
|
+
/** @deprecated Use `Attestation.Constants.header`. */
|
|
5
|
+
header: AttestationConstants.header,
|
|
6
|
+
/** @deprecated Use `Attestation.Constants.requiredHeader`. */
|
|
7
|
+
requiredHeader: AttestationConstants.requiredHeader,
|
|
8
|
+
/** Attestation type for a verified email address. */
|
|
9
|
+
type: 'email',
|
|
10
|
+
/** Issuer path that emails a one-time code. */
|
|
11
|
+
otpPath: '/email/otp',
|
|
12
|
+
/** Issuer path that exchanges a one-time code for an attestation. */
|
|
13
|
+
verifyPath: '/email/verify',
|
|
14
|
+
/** Times the prover asks for a code before giving up. */
|
|
15
|
+
codeAttempts: 3,
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=Constants.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** A verified email address. */
|
|
2
|
+
export type VerifiedEmail = {
|
|
3
|
+
/** The email address the issuer sent a one-time code to, lowercased. */
|
|
4
|
+
readonly email: string;
|
|
5
|
+
/** Issuer URL. */
|
|
6
|
+
readonly issuer: string;
|
|
7
|
+
/** When the attestation expires. */
|
|
8
|
+
readonly expiresAt: Date;
|
|
9
|
+
/** Attestation identifier (`jti`), if the issuer set one. */
|
|
10
|
+
readonly id?: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=Types.d.ts.map
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type * as Identity from '../../../../identity/Types.js';
|
|
2
|
+
import type { MaybePromise } from '../../../../internal/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Asks an issuer to email a one-time code for `audience`.
|
|
5
|
+
*
|
|
6
|
+
* Returns the code request's `id`, which {@link verifyCode} exchanges, with the code, for an attestation.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sendCode(parameters: sendCode.Parameters): Promise<sendCode.ReturnType>;
|
|
9
|
+
export declare namespace sendCode {
|
|
10
|
+
type Parameters = {
|
|
11
|
+
/** Issuer URL. */
|
|
12
|
+
issuer: string;
|
|
13
|
+
/** Email address to send the code to. */
|
|
14
|
+
email: string;
|
|
15
|
+
/** Origin of the server the attestation is for. */
|
|
16
|
+
audience: string;
|
|
17
|
+
/** Fetch implementation. @default globalThis.fetch */
|
|
18
|
+
fetch?: typeof globalThis.fetch | undefined;
|
|
19
|
+
};
|
|
20
|
+
type ReturnType = {
|
|
21
|
+
id: string;
|
|
22
|
+
expiresAt: Date;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/** Exchanges an emailed one-time code for an email attestation. */
|
|
26
|
+
export declare function verifyCode(parameters: verifyCode.Parameters): Promise<verifyCode.ReturnType>;
|
|
27
|
+
export declare namespace verifyCode {
|
|
28
|
+
type Parameters = {
|
|
29
|
+
/** Issuer URL. */
|
|
30
|
+
issuer: string;
|
|
31
|
+
/** Code request ID from {@link sendCode}. */
|
|
32
|
+
id: string;
|
|
33
|
+
/** The code from the email. */
|
|
34
|
+
code: string;
|
|
35
|
+
/** Fetch implementation. @default globalThis.fetch */
|
|
36
|
+
fetch?: typeof globalThis.fetch | undefined;
|
|
37
|
+
};
|
|
38
|
+
type ReturnType = {
|
|
39
|
+
attestation: string;
|
|
40
|
+
expiresAt: Date;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
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.
|
|
46
|
+
*
|
|
47
|
+
* When a server answers with `Identity-Attestation-Required: email;issuer="…"`,
|
|
48
|
+
* the prover asks the issuer to email a code to `email`, gets the code from
|
|
49
|
+
* `code` (such as a terminal prompt or a form), exchanges it for an
|
|
50
|
+
* attestation bound to the server's origin and sends the request again with it.
|
|
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.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* const mppx = Mppx.create({
|
|
66
|
+
* identity: [
|
|
67
|
+
* EmailOtp.Client.prover({
|
|
68
|
+
* code: () => prompt('Code:'),
|
|
69
|
+
* email: 'me@example.com',
|
|
70
|
+
* // Only this issuer receives the address, and only this origin the attestation.
|
|
71
|
+
* issuers: ['https://issuer.example'],
|
|
72
|
+
* scope: ['https://api.example'],
|
|
73
|
+
* }),
|
|
74
|
+
* ],
|
|
75
|
+
* methods: [tempo({ account })],
|
|
76
|
+
* })
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function prover(config: prover.Config): Identity.Prover;
|
|
80
|
+
export declare namespace prover {
|
|
81
|
+
type Config = {
|
|
82
|
+
/** Origins that may ask for and receive the attestation, or `'*'` for any origin. */
|
|
83
|
+
readonly scope: Identity.Prover['scope'];
|
|
84
|
+
/** Email address to verify, or a function that returns it (or `undefined` to decline). */
|
|
85
|
+
readonly email: string | ((parameters: {
|
|
86
|
+
audience: string;
|
|
87
|
+
issuer: string;
|
|
88
|
+
}) => MaybePromise<string | undefined>);
|
|
89
|
+
/** Returns the code the user received, or `undefined` to give up. */
|
|
90
|
+
readonly code: (parameters: {
|
|
91
|
+
/** Origin the attestation is for. */
|
|
92
|
+
audience: string;
|
|
93
|
+
/** Email address the code was sent to. */
|
|
94
|
+
email: string;
|
|
95
|
+
/** Issuer URL. */
|
|
96
|
+
issuer: string;
|
|
97
|
+
/** Code request ID. */
|
|
98
|
+
id: string;
|
|
99
|
+
/** When the code expires. */
|
|
100
|
+
expiresAt: Date;
|
|
101
|
+
/** 1 for the first prompt, higher after a rejected code. */
|
|
102
|
+
attempt: number;
|
|
103
|
+
/** Why the previous code was rejected. */
|
|
104
|
+
error?: Error | undefined;
|
|
105
|
+
}) => MaybePromise<string | undefined>;
|
|
106
|
+
/** Attestations obtained beforehand, such as from the issuer with curl. */
|
|
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[];
|
|
110
|
+
/** Fetch used to call the issuer. @default globalThis.fetch */
|
|
111
|
+
readonly fetch?: typeof globalThis.fetch | undefined;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** An issuer rejected an email OTP request. */
|
|
115
|
+
export declare class EmailOtpError extends Error {
|
|
116
|
+
readonly name = "EmailOtpError";
|
|
117
|
+
/** HTTP status the issuer answered with, if any. */
|
|
118
|
+
readonly status: number | undefined;
|
|
119
|
+
constructor(message: string, options?: {
|
|
120
|
+
status?: number | undefined;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { unwrapFetch } from '../../../../client/internal/Fetch.js';
|
|
2
|
+
import * as Attestation from '../../attestation/client/index.js';
|
|
3
|
+
import { isIssuer } from '../../attestation/Token.js';
|
|
4
|
+
import { Constants } from '../Constants.js';
|
|
5
|
+
/**
|
|
6
|
+
* Asks an issuer to email a one-time code for `audience`.
|
|
7
|
+
*
|
|
8
|
+
* Returns the code request's `id`, which {@link verifyCode} exchanges, with the code, for an attestation.
|
|
9
|
+
*/
|
|
10
|
+
export async function sendCode(parameters) {
|
|
11
|
+
const body = await post(parameters.issuer, Constants.otpPath, parameters.fetch, {
|
|
12
|
+
audience: parameters.audience,
|
|
13
|
+
email: parameters.email,
|
|
14
|
+
});
|
|
15
|
+
const expiresAt = date(body.expires_at);
|
|
16
|
+
if (typeof body.id !== 'string' || body.id === '' || !expiresAt)
|
|
17
|
+
throw new EmailOtpError('The issuer returned a malformed code request.');
|
|
18
|
+
return { expiresAt, id: body.id };
|
|
19
|
+
}
|
|
20
|
+
/** Exchanges an emailed one-time code for an email attestation. */
|
|
21
|
+
export async function verifyCode(parameters) {
|
|
22
|
+
const body = await post(parameters.issuer, Constants.verifyPath, parameters.fetch, {
|
|
23
|
+
code: parameters.code,
|
|
24
|
+
id: parameters.id,
|
|
25
|
+
});
|
|
26
|
+
const expiresAt = date(body.expires_at);
|
|
27
|
+
if (typeof body.attestation !== 'string' || body.attestation === '' || !expiresAt)
|
|
28
|
+
throw new EmailOtpError('The issuer returned a malformed attestation.');
|
|
29
|
+
return { attestation: body.attestation, expiresAt };
|
|
30
|
+
}
|
|
31
|
+
/** Parses an issuer timestamp, or returns `undefined` when it is not a valid date. */
|
|
32
|
+
function date(value) {
|
|
33
|
+
if (typeof value !== 'string')
|
|
34
|
+
return undefined;
|
|
35
|
+
const result = new Date(value);
|
|
36
|
+
return Number.isFinite(result.getTime()) ? result : undefined;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
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.
|
|
41
|
+
*
|
|
42
|
+
* When a server answers with `Identity-Attestation-Required: email;issuer="…"`,
|
|
43
|
+
* the prover asks the issuer to email a code to `email`, gets the code from
|
|
44
|
+
* `code` (such as a terminal prompt or a form), exchanges it for an
|
|
45
|
+
* attestation bound to the server's origin and sends the request again with it.
|
|
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.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const mppx = Mppx.create({
|
|
61
|
+
* identity: [
|
|
62
|
+
* EmailOtp.Client.prover({
|
|
63
|
+
* code: () => prompt('Code:'),
|
|
64
|
+
* email: 'me@example.com',
|
|
65
|
+
* // Only this issuer receives the address, and only this origin the attestation.
|
|
66
|
+
* issuers: ['https://issuer.example'],
|
|
67
|
+
* scope: ['https://api.example'],
|
|
68
|
+
* }),
|
|
69
|
+
* ],
|
|
70
|
+
* methods: [tempo({ account })],
|
|
71
|
+
* })
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export function prover(config) {
|
|
75
|
+
return Attestation.prover({
|
|
76
|
+
attestations: config.attestations,
|
|
77
|
+
issuers: config.issuers,
|
|
78
|
+
scope: config.scope,
|
|
79
|
+
types: [Constants.type],
|
|
80
|
+
async acquire({ audience, issuer }) {
|
|
81
|
+
const email = typeof config.email === 'function' ? await config.email({ audience, issuer }) : config.email;
|
|
82
|
+
if (!email)
|
|
83
|
+
return undefined;
|
|
84
|
+
const request = await sendCode({ audience, email, fetch: config.fetch, issuer });
|
|
85
|
+
let error;
|
|
86
|
+
for (let attempt = 1; attempt <= Constants.codeAttempts; attempt++) {
|
|
87
|
+
const code = await config.code({ ...request, attempt, audience, email, error, issuer });
|
|
88
|
+
if (!code)
|
|
89
|
+
return undefined;
|
|
90
|
+
try {
|
|
91
|
+
return (await verifyCode({ code: code.trim(), fetch: config.fetch, id: request.id, issuer })).attestation;
|
|
92
|
+
}
|
|
93
|
+
catch (caught) {
|
|
94
|
+
// Only a rejected code is worth another try; other failures end the attempt.
|
|
95
|
+
if (!(caught instanceof EmailOtpError) || caught.status !== 400)
|
|
96
|
+
throw caught;
|
|
97
|
+
error = caught;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
throw error;
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/** An issuer rejected an email OTP request. */
|
|
105
|
+
export class EmailOtpError extends Error {
|
|
106
|
+
name = 'EmailOtpError';
|
|
107
|
+
/** HTTP status the issuer answered with, if any. */
|
|
108
|
+
status;
|
|
109
|
+
constructor(message, options = {}) {
|
|
110
|
+
super(message);
|
|
111
|
+
this.status = options.status;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async function post(issuer, path, fetch, body) {
|
|
115
|
+
// The address and code never go over plaintext HTTP, except to localhost.
|
|
116
|
+
if (!isIssuer(issuer))
|
|
117
|
+
throw new EmailOtpError('The issuer must be an https URL (http only on localhost).');
|
|
118
|
+
// The unwrapped fetch, so an issuer's 402 is never paid automatically.
|
|
119
|
+
const response = await unwrapFetch(fetch ?? globalThis.fetch)(`${issuer.replace(/\/+$/, '')}${path}`, {
|
|
120
|
+
body: JSON.stringify(body),
|
|
121
|
+
headers: { 'content-type': 'application/json' },
|
|
122
|
+
method: 'POST',
|
|
123
|
+
// Workers reject 'error'; a redirect is not ok, so it fails below.
|
|
124
|
+
redirect: 'manual',
|
|
125
|
+
});
|
|
126
|
+
const json = (await response.json().catch(() => ({})));
|
|
127
|
+
if (!response.ok)
|
|
128
|
+
throw new EmailOtpError(typeof json.error === 'string' ? json.error : `The issuer answered ${response.status}.`, { status: response.status });
|
|
129
|
+
return json;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
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`.
|
|
5
|
+
*/
|
|
6
|
+
/** Client helpers that verify an email address with a one-time code. */
|
|
7
|
+
export * as Client from './client/index.js';
|
|
8
|
+
/** Server helpers that require a verified email address. */
|
|
9
|
+
export * as Server from './server/index.js';
|
|
10
|
+
export * from './Constants.js';
|
|
11
|
+
export type * from './Types.js';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
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`.
|
|
5
|
+
*/
|
|
6
|
+
/** Client helpers that verify an email address with a one-time code. */
|
|
7
|
+
export * as Client from './client/index.js';
|
|
8
|
+
/** Server helpers that require a verified email address. */
|
|
9
|
+
export * as Server from './server/index.js';
|
|
10
|
+
export * from './Constants.js';
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type * as Identity from '../../../../identity/Types.js';
|
|
2
|
+
import type { MaybePromise } from '../../../../internal/types.js';
|
|
3
|
+
import * as Attestation from '../../attestation/server/index.js';
|
|
4
|
+
import type * as AttestationTypes from '../../attestation/Types.js';
|
|
5
|
+
import type * as Types from '../Types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Creates a request-stage verifier that requires a verified email address: an
|
|
8
|
+
* `Attestation.Server.verifier` of type `email` whose `validate` is `allow`.
|
|
9
|
+
*
|
|
10
|
+
* A request without one gets 401 with `Identity-Attestation-Required: email;issuer="…"`.
|
|
11
|
+
* The client obtains an attestation from the issuer with a one-time code sent to
|
|
12
|
+
* the address and sends it in `Identity-Attestation`. An address `allow` rejects is
|
|
13
|
+
* `forbidden` (403).
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const email = EmailOtp.Server.verifier({
|
|
18
|
+
* audience: 'https://api.example',
|
|
19
|
+
* issuer: 'https://issuer.example',
|
|
20
|
+
* allow: (email) => email.endsWith('@example.com'),
|
|
21
|
+
* })
|
|
22
|
+
* const result = await mppx.charge({ amount: '1', identity: [email] })(request)
|
|
23
|
+
* if (result.status === 200) result.identity?.get(email)?.email
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function verifier(config: verifier.Config): Identity.Verifier<Types.VerifiedEmail>;
|
|
27
|
+
export declare namespace verifier {
|
|
28
|
+
type Config = {
|
|
29
|
+
/** Trusted issuer URL. */
|
|
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;
|
|
33
|
+
/**
|
|
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
|
+
*/
|
|
37
|
+
readonly audience: string | Attestation.verifier.AudienceFunction;
|
|
38
|
+
/** Resolves issuer keys. Defaults to the issuer's JSON Web Key Set. */
|
|
39
|
+
readonly keyResolver?: AttestationTypes.KeyResolver | undefined;
|
|
40
|
+
/** Fetch used for the default JSON Web Key Set resolver. */
|
|
41
|
+
readonly fetch?: typeof globalThis.fetch | undefined;
|
|
42
|
+
/** Oldest accepted attestation, in seconds since it was issued. Defaults to its own expiry. */
|
|
43
|
+
readonly maxAge?: number | undefined;
|
|
44
|
+
/** Clock tolerance in seconds. @default 60 */
|
|
45
|
+
readonly clockTolerance?: number | undefined;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import * as Attestation from '../../attestation/server/index.js';
|
|
2
|
+
import { Constants } from '../Constants.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a request-stage verifier that requires a verified email address: an
|
|
5
|
+
* `Attestation.Server.verifier` of type `email` whose `validate` is `allow`.
|
|
6
|
+
*
|
|
7
|
+
* A request without one gets 401 with `Identity-Attestation-Required: email;issuer="…"`.
|
|
8
|
+
* The client obtains an attestation from the issuer with a one-time code sent to
|
|
9
|
+
* the address and sends it in `Identity-Attestation`. An address `allow` rejects is
|
|
10
|
+
* `forbidden` (403).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const email = EmailOtp.Server.verifier({
|
|
15
|
+
* audience: 'https://api.example',
|
|
16
|
+
* issuer: 'https://issuer.example',
|
|
17
|
+
* allow: (email) => email.endsWith('@example.com'),
|
|
18
|
+
* })
|
|
19
|
+
* const result = await mppx.charge({ amount: '1', identity: [email] })(request)
|
|
20
|
+
* if (result.status === 200) result.identity?.get(email)?.email
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function verifier(config) {
|
|
24
|
+
const { allow } = config;
|
|
25
|
+
const inner = Attestation.verifier({
|
|
26
|
+
audience: config.audience,
|
|
27
|
+
clockTolerance: config.clockTolerance,
|
|
28
|
+
fetch: config.fetch,
|
|
29
|
+
issuer: config.issuer,
|
|
30
|
+
keyResolver: config.keyResolver,
|
|
31
|
+
maxAge: config.maxAge,
|
|
32
|
+
type: Constants.type,
|
|
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
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
async verify(request) {
|
|
43
|
+
const result = await inner.verify(request);
|
|
44
|
+
if (result.status !== 'valid')
|
|
45
|
+
return result;
|
|
46
|
+
const { claims } = result.value;
|
|
47
|
+
return {
|
|
48
|
+
status: 'valid',
|
|
49
|
+
value: Object.freeze({
|
|
50
|
+
email: claims.sub.toLowerCase(),
|
|
51
|
+
expiresAt: new Date(claims.exp * 1_000),
|
|
52
|
+
id: claims.jti,
|
|
53
|
+
issuer: claims.iss,
|
|
54
|
+
}),
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** Identity attestations: issuer-signed tokens a server requires and a client presents. */
|
|
2
|
+
export * as Attestation from './attestation/index.js';
|
|
3
|
+
/** Email address verification with a one-time code. */
|
|
4
|
+
export * as EmailOtp from './email-otp/index.js';
|
|
1
5
|
/** Client and server adapters for the TAP agent-recognition signature profile. */
|
|
2
6
|
export * as Tap from './tap/index.js';
|
|
3
7
|
/** Client and server adapters for the Web Bot Auth HTTPS-directory profile. */
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** Identity attestations: issuer-signed tokens a server requires and a client presents. */
|
|
2
|
+
export * as Attestation from './attestation/index.js';
|
|
3
|
+
/** Email address verification with a one-time code. */
|
|
4
|
+
export * as EmailOtp from './email-otp/index.js';
|
|
1
5
|
/** Client and server adapters for the TAP agent-recognition signature profile. */
|
|
2
6
|
export * as Tap from './tap/index.js';
|
|
3
7
|
/** Client and server adapters for the Web Bot Auth HTTPS-directory profile. */
|