mppx 0.18.1 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/dist/Errors.d.ts +40 -4
- package/dist/Errors.js +40 -6
- package/dist/cli/cli.js +96 -96
- package/dist/cli/plugins/index.js +12 -12
- package/dist/extensions/identity/Types.d.ts +26 -0
- package/dist/extensions/identity/attestation/Token.d.ts +6 -4
- package/dist/extensions/identity/attestation/Token.js +11 -5
- package/dist/extensions/identity/index.d.ts +2 -0
- package/dist/extensions/identity/index.js +2 -0
- package/dist/extensions/identity/internal/HttpMessageSignature.d.ts +2 -2
- package/dist/extensions/identity/internal/HttpMessageSignature.js +13 -10
- package/dist/extensions/identity/{web-bot-auth/internal → internal}/JwkThumbprint.js +1 -1
- package/dist/extensions/identity/internal/Signer.d.ts +20 -0
- package/dist/extensions/identity/internal/Signer.js +94 -0
- package/dist/extensions/identity/poppy/Constants.d.ts +26 -0
- package/dist/extensions/identity/poppy/Constants.js +26 -0
- package/dist/extensions/identity/poppy/Types.d.ts +19 -0
- package/dist/extensions/identity/poppy/Types.js +2 -0
- package/dist/extensions/identity/poppy/client/index.d.ts +69 -0
- package/dist/extensions/identity/poppy/client/index.js +81 -0
- package/dist/extensions/identity/poppy/index.d.ts +7 -0
- package/dist/extensions/identity/poppy/index.js +7 -0
- package/dist/extensions/identity/poppy/internal/Jws.d.ts +12 -0
- package/dist/extensions/identity/poppy/internal/Jws.js +78 -0
- package/dist/extensions/identity/poppy/server/index.d.ts +40 -0
- package/dist/extensions/identity/poppy/server/index.js +181 -0
- package/dist/extensions/identity/tap/client/index.d.ts +6 -2
- package/dist/extensions/identity/tap/client/index.js +2 -0
- package/dist/extensions/identity/tempo/Constants.d.ts +14 -0
- package/dist/extensions/identity/tempo/Constants.js +14 -0
- package/dist/extensions/identity/tempo/Types.d.ts +31 -0
- package/dist/extensions/identity/tempo/Types.js +2 -0
- package/dist/extensions/identity/tempo/client/index.d.ts +34 -0
- package/dist/extensions/identity/tempo/client/index.js +60 -0
- package/dist/extensions/identity/tempo/index.d.ts +5 -0
- package/dist/extensions/identity/tempo/index.js +4 -0
- package/dist/extensions/identity/tempo/internal/Jwt.d.ts +36 -0
- package/dist/extensions/identity/tempo/internal/Jwt.js +114 -0
- package/dist/extensions/identity/tempo/server/index.d.ts +65 -0
- package/dist/extensions/identity/tempo/server/index.js +247 -0
- package/dist/extensions/identity/web-bot-auth/client/index.d.ts +6 -4
- package/dist/extensions/identity/web-bot-auth/client/index.js +7 -5
- package/dist/extensions/identity/web-bot-auth/server/index.js +1 -1
- package/dist/extensions/identity/world/Constants.d.ts +50 -0
- package/dist/extensions/identity/world/Constants.js +50 -0
- package/dist/extensions/identity/world/Types.d.ts +52 -0
- package/dist/extensions/identity/world/Types.js +2 -0
- package/dist/extensions/identity/world/client/index.d.ts +103 -0
- package/dist/extensions/identity/world/client/index.js +208 -0
- package/dist/extensions/identity/world/index.d.ts +6 -0
- package/dist/extensions/identity/world/index.js +5 -0
- package/dist/extensions/identity/world/internal/Idp.d.ts +50 -0
- package/dist/extensions/identity/world/internal/Idp.js +265 -0
- package/dist/extensions/identity/world/issuer/index.d.ts +120 -0
- package/dist/extensions/identity/world/issuer/index.js +520 -0
- package/dist/extensions/identity/world/server/index.d.ts +49 -0
- package/dist/extensions/identity/world/server/index.js +63 -0
- package/dist/identity/Headers.d.ts +1 -1
- package/dist/identity/Headers.js +7 -1
- package/dist/identity/Types.d.ts +5 -0
- package/dist/internal/version.d.ts +2 -2
- package/dist/internal/version.js +1 -1
- package/dist/server/Mppx.js +2 -1
- package/dist/tempo/client/Charge.js +39 -16
- package/dist/tempo/internal/simulation.d.ts +33 -0
- package/dist/tempo/internal/simulation.js +80 -0
- package/dist/tempo/internal/zone.d.ts +27 -0
- package/dist/tempo/internal/zone.js +54 -7
- package/dist/tempo/server/internal/html.gen.js +1 -1
- package/dist/tempo/session/client/ChannelOps.js +5 -2
- package/package.json +13 -1
- /package/dist/extensions/identity/{web-bot-auth/internal → internal}/JwkThumbprint.d.ts +0 -0
|
@@ -2,6 +2,32 @@ import type { MaybePromise } from '../../internal/types.js';
|
|
|
2
2
|
import type { Algorithms } from './Constants.js';
|
|
3
3
|
/** RFC 9421 algorithms implemented by the HTTP message signature signer and verifier. */
|
|
4
4
|
export type SignatureAlgorithm = (typeof Algorithms)[keyof typeof Algorithms];
|
|
5
|
+
/**
|
|
6
|
+
* A key pair whose private key stays outside this process, such as in a cloud KMS, an HSM or a
|
|
7
|
+
* wallet service like Turnkey.
|
|
8
|
+
*
|
|
9
|
+
* mppx checks every signature `sign` returns against `publicKey` before it uses it, so a wrong
|
|
10
|
+
* key, a DER- or base64-encoded signature or a wrong RSA-PSS salt fails on the client instead of
|
|
11
|
+
* at the server.
|
|
12
|
+
*/
|
|
13
|
+
export type Signer = {
|
|
14
|
+
/**
|
|
15
|
+
* The public key of the remote private key: Ed25519 or RSA-PSS SHA-512, with the `verify`
|
|
16
|
+
* usage. Its algorithm is the signature algorithm, and identifiers that derive from the key
|
|
17
|
+
* (such as a Web Bot Auth `keyid`) are computed from it.
|
|
18
|
+
*/
|
|
19
|
+
readonly publicKey: CryptoKey;
|
|
20
|
+
/**
|
|
21
|
+
* Signs `payload` (the complete message, not a digest: the RFC 9421 signature base or the JWS
|
|
22
|
+
* signing input) and resolves with the raw signature bytes: 64 bytes for Ed25519, or the RSA
|
|
23
|
+
* modulus length for RSA-PSS SHA-512 (SHA-512, MGF1 with SHA-512, 64-byte salt).
|
|
24
|
+
*/
|
|
25
|
+
readonly sign: (parameters: {
|
|
26
|
+
readonly payload: Uint8Array;
|
|
27
|
+
}) => Promise<Uint8Array>;
|
|
28
|
+
};
|
|
29
|
+
/** A local `CryptoKeyPair`, or a {@link Signer} whose private key stays elsewhere. */
|
|
30
|
+
export type SigningKey = CryptoKeyPair | Signer;
|
|
5
31
|
/** Resolves a trusted public key for a parsed request signature. */
|
|
6
32
|
export type KeyResolver = (parameters: {
|
|
7
33
|
/** RFC 9421 algorithm declared by the signature. */
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { SigningKey } from '../Types.js';
|
|
1
2
|
import type * as Types from './Types.js';
|
|
2
3
|
/** A decoded, unverified attestation. */
|
|
3
4
|
export type Decoded = {
|
|
@@ -28,12 +29,13 @@ export type VerifyResult = {
|
|
|
28
29
|
/**
|
|
29
30
|
* Signs attestation claims as a compact JWS (EdDSA over Ed25519).
|
|
30
31
|
*
|
|
31
|
-
* For issuers. `
|
|
32
|
-
*
|
|
32
|
+
* For issuers. `key` is an Ed25519 key pair, or a {@link SigningKey | Signer} whose private key
|
|
33
|
+
* stays in a KMS, HSM or wallet service. `keyId` defaults to the RFC 7638 thumbprint of its public
|
|
34
|
+
* key, the `kid` an issuer's JSON Web Key Set publishes for it.
|
|
33
35
|
*/
|
|
34
36
|
export declare function sign(claims: Types.Claims, options: {
|
|
35
|
-
readonly key:
|
|
36
|
-
readonly keyId
|
|
37
|
+
readonly key: SigningKey;
|
|
38
|
+
readonly keyId?: string | undefined;
|
|
37
39
|
}): Promise<string>;
|
|
38
40
|
/** Decodes an attestation without verifying it, or returns `undefined` if it is malformed. */
|
|
39
41
|
export declare function decode(token: string): Decoded | undefined;
|
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import { Base64 } from 'ox';
|
|
2
|
+
import { Algorithms } from '../Constants.js';
|
|
3
|
+
import * as JwkThumbprint from '../internal/JwkThumbprint.js';
|
|
4
|
+
import * as Signer from '../internal/Signer.js';
|
|
2
5
|
import { Constants } from './Constants.js';
|
|
3
6
|
/**
|
|
4
7
|
* Signs attestation claims as a compact JWS (EdDSA over Ed25519).
|
|
5
8
|
*
|
|
6
|
-
* For issuers. `
|
|
7
|
-
*
|
|
9
|
+
* For issuers. `key` is an Ed25519 key pair, or a {@link SigningKey | Signer} whose private key
|
|
10
|
+
* stays in a KMS, HSM or wallet service. `keyId` defaults to the RFC 7638 thumbprint of its public
|
|
11
|
+
* key, the `kid` an issuer's JSON Web Key Set publishes for it.
|
|
8
12
|
*/
|
|
9
13
|
export async function sign(claims, options) {
|
|
10
|
-
const
|
|
14
|
+
const signer = Signer.resolve(options.key, [Algorithms.ed25519]);
|
|
15
|
+
const keyId = options.keyId ?? (await JwkThumbprint.fromKey(signer.publicKey));
|
|
16
|
+
const header = { alg: Constants.algorithm, kid: keyId, typ: Constants.tokenType };
|
|
11
17
|
const input = `${encode(header)}.${encode(claims)}`;
|
|
12
|
-
const signature = await
|
|
13
|
-
return `${input}.${Base64.fromBytes(
|
|
18
|
+
const signature = await signer.sign(new TextEncoder().encode(input));
|
|
19
|
+
return `${input}.${Base64.fromBytes(signature, { pad: false, url: true })}`;
|
|
14
20
|
}
|
|
15
21
|
/** Decodes an attestation without verifying it, or returns `undefined` if it is malformed. */
|
|
16
22
|
export function decode(token) {
|
|
@@ -10,6 +10,8 @@ export * as Persona from './persona/index.js';
|
|
|
10
10
|
export * as Tap from './tap/index.js';
|
|
11
11
|
/** Client and server adapters for the Web Bot Auth HTTPS-directory profile. */
|
|
12
12
|
export * as WebBotAuth from './web-bot-auth/index.js';
|
|
13
|
+
/** World ID: agents approved by a World ID-verified human, through World IdP (OpenID Connect). */
|
|
14
|
+
export * as World from './world/index.js';
|
|
13
15
|
export * from './Constants.js';
|
|
14
16
|
export type * from './Types.js';
|
|
15
17
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -10,5 +10,7 @@ export * as Persona from './persona/index.js';
|
|
|
10
10
|
export * as Tap from './tap/index.js';
|
|
11
11
|
/** Client and server adapters for the Web Bot Auth HTTPS-directory profile. */
|
|
12
12
|
export * as WebBotAuth from './web-bot-auth/index.js';
|
|
13
|
+
/** World ID: agents approved by a World ID-verified human, through World IdP (OpenID Connect). */
|
|
14
|
+
export * as World from './world/index.js';
|
|
13
15
|
export * from './Constants.js';
|
|
14
16
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { BareItem } from 'structured-headers';
|
|
2
2
|
import type { MaybePromise } from '../../../internal/types.js';
|
|
3
3
|
import * as Store from '../../../Store.js';
|
|
4
|
-
import type { KeyResolver, SignatureAlgorithm, SigningContext } from '../Types.js';
|
|
4
|
+
import type { KeyResolver, SignatureAlgorithm, SigningContext, SigningKey } from '../Types.js';
|
|
5
5
|
/** RFC 9421 constants used by identity signature profiles. */
|
|
6
6
|
export declare const Constants: {
|
|
7
7
|
/** Derived request components supported by this narrow request profile. */
|
|
@@ -54,7 +54,7 @@ export declare function sign(request: Request, options: {
|
|
|
54
54
|
components: readonly ComponentInput[];
|
|
55
55
|
expiresIn?: number | undefined;
|
|
56
56
|
keyId: string;
|
|
57
|
-
key:
|
|
57
|
+
key: SigningKey;
|
|
58
58
|
label: string;
|
|
59
59
|
context?: SigningContext | undefined;
|
|
60
60
|
tag: string;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isInnerList, parseDictionary, serializeItem, serializeDictionary, serializeInnerList, serializeParameters, } from 'structured-headers';
|
|
2
2
|
import * as Store from '../../../Store.js';
|
|
3
3
|
import { Algorithms, Headers as SignatureHeaders } from '../Constants.js';
|
|
4
|
+
import * as Signer from './Signer.js';
|
|
4
5
|
import * as SigningContextInternal from './SigningContext.js';
|
|
5
6
|
/** RFC 9421 constants used by identity signature profiles. */
|
|
6
7
|
export const Constants = {
|
|
@@ -33,7 +34,8 @@ export async function sign(request, options) {
|
|
|
33
34
|
const context = options.context ?? SigningContextInternal.create();
|
|
34
35
|
const created = context.created;
|
|
35
36
|
const expires = created + (options.expiresIn ?? 60);
|
|
36
|
-
const
|
|
37
|
+
const signer = Signer.resolve(options.key);
|
|
38
|
+
const algorithm = signer.algorithm;
|
|
37
39
|
const parameters = new Map([
|
|
38
40
|
['created', created],
|
|
39
41
|
['expires', expires],
|
|
@@ -53,7 +55,7 @@ export async function sign(request, options) {
|
|
|
53
55
|
parameters,
|
|
54
56
|
tag: options.tag,
|
|
55
57
|
};
|
|
56
|
-
|
|
58
|
+
// Check the existing signatures first, so a request this refuses never reaches a remote signer.
|
|
57
59
|
const headers = new Headers(request.headers);
|
|
58
60
|
const inputs = parseHeaderDictionary(headers, SignatureHeaders.signatureInput);
|
|
59
61
|
const signatures = parseHeaderDictionary(headers, SignatureHeaders.signature);
|
|
@@ -61,9 +63,16 @@ export async function sign(request, options) {
|
|
|
61
63
|
throw new Error('Signature-Input and Signature must contain the same labels.');
|
|
62
64
|
if (inputs.has(input.label) || signatures.has(input.label))
|
|
63
65
|
throw new Error(`HTTP message signature label "${input.label}" already exists.`);
|
|
66
|
+
// `new Request(request)` below throws on a read or locked body: refuse it before signing.
|
|
67
|
+
if (request.bodyUsed || request.body?.locked)
|
|
68
|
+
throw new TypeError('Cannot sign a request whose body has already been read or is locked.');
|
|
64
69
|
inputs.set(input.label, toInnerList(input));
|
|
70
|
+
// Serializing validates the label and parameters before the signer is called.
|
|
71
|
+
const signatureInput = serializeDictionary(inputs);
|
|
72
|
+
const bytes = encode(signatureBase(request, input));
|
|
73
|
+
const signature = (await signer.sign(bytes)).buffer;
|
|
65
74
|
signatures.set(input.label, [signature, new Map()]);
|
|
66
|
-
headers.set(SignatureHeaders.signatureInput,
|
|
75
|
+
headers.set(SignatureHeaders.signatureInput, signatureInput);
|
|
67
76
|
headers.set(SignatureHeaders.signature, serializeDictionary(signatures));
|
|
68
77
|
return new Request(request, { headers });
|
|
69
78
|
}
|
|
@@ -215,12 +224,6 @@ function toSignatureInput(label, entry) {
|
|
|
215
224
|
tag,
|
|
216
225
|
};
|
|
217
226
|
}
|
|
218
|
-
function algorithmFromKey(key) {
|
|
219
|
-
for (const algorithm of Object.values(Algorithms))
|
|
220
|
-
if (keySupportsAlgorithm(key, algorithm))
|
|
221
|
-
return algorithm;
|
|
222
|
-
throw new TypeError('HTTP message signatures require an Ed25519 or RSA-PSS SHA-512 signing key.');
|
|
223
|
-
}
|
|
224
227
|
function isInteger(value) {
|
|
225
228
|
return typeof value === 'number' && Number.isInteger(value);
|
|
226
229
|
}
|
|
@@ -317,6 +320,6 @@ function encode(value) {
|
|
|
317
320
|
return new TextEncoder().encode(value);
|
|
318
321
|
}
|
|
319
322
|
function toArrayBuffer(value) {
|
|
320
|
-
return
|
|
323
|
+
return Uint8Array.from(value).buffer;
|
|
321
324
|
}
|
|
322
325
|
//# sourceMappingURL=HttpMessageSignature.js.map
|
|
@@ -13,7 +13,7 @@ export async function fromKey(key) {
|
|
|
13
13
|
else if (jwk.kty === 'RSA' && jwk.e && jwk.n)
|
|
14
14
|
canonical = JSON.stringify({ e: jwk.e, kty: jwk.kty, n: jwk.n });
|
|
15
15
|
else
|
|
16
|
-
throw new TypeError('
|
|
16
|
+
throw new TypeError('RFC 7638 thumbprints need an Ed25519 or RSA public key.');
|
|
17
17
|
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(canonical));
|
|
18
18
|
return Base64.fromBytes(new Uint8Array(digest), { pad: false, url: true });
|
|
19
19
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SignatureAlgorithm, SigningKey } from '../Types.js';
|
|
2
|
+
/** A {@link SigningKey} checked and ready to sign. */
|
|
3
|
+
export type Resolved = {
|
|
4
|
+
/** Signature algorithm, from the public key. */
|
|
5
|
+
readonly algorithm: SignatureAlgorithm;
|
|
6
|
+
/** Public key that verifies every signature. */
|
|
7
|
+
readonly publicKey: CryptoKey;
|
|
8
|
+
/** Signs the complete message and resolves with the raw signature. */
|
|
9
|
+
readonly sign: (payload: Uint8Array) => Promise<Uint8Array<ArrayBuffer>>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Checks a {@link SigningKey} and returns how to sign with it.
|
|
13
|
+
*
|
|
14
|
+
* Throws a `TypeError` unless the key is a `CryptoKeyPair` or a {@link Signer} whose public key
|
|
15
|
+
* is one of `algorithms`. A `Signer`'s signatures are verified against its public key before
|
|
16
|
+
* they are returned.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolve(key: SigningKey, algorithms?: readonly SignatureAlgorithm[]): Resolved;
|
|
20
|
+
//# sourceMappingURL=Signer.d.ts.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Algorithms } from '../Constants.js';
|
|
2
|
+
const webCrypto = {
|
|
3
|
+
[Algorithms.ed25519]: { name: 'Ed25519' },
|
|
4
|
+
[Algorithms.rsaPssSha512]: { name: 'RSA-PSS', saltLength: 64 },
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Checks a {@link SigningKey} and returns how to sign with it.
|
|
8
|
+
*
|
|
9
|
+
* Throws a `TypeError` unless the key is a `CryptoKeyPair` or a {@link Signer} whose public key
|
|
10
|
+
* is one of `algorithms`. A `Signer`'s signatures are verified against its public key before
|
|
11
|
+
* they are returned.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export function resolve(key, algorithms = Object.values(Algorithms)) {
|
|
15
|
+
if (!isObject(key))
|
|
16
|
+
throw invalidKey();
|
|
17
|
+
const { publicKey } = key;
|
|
18
|
+
if (isCryptoKey(key))
|
|
19
|
+
throw new TypeError('Signing keys are key pairs: pass { privateKey, publicKey } or a Signer, not a private key alone.');
|
|
20
|
+
if (!isCryptoKey(publicKey) || publicKey.type !== 'public')
|
|
21
|
+
throw invalidKey();
|
|
22
|
+
const algorithm = algorithmOf(publicKey);
|
|
23
|
+
if (!algorithm || !algorithms.includes(algorithm))
|
|
24
|
+
throw new TypeError(`Signing keys must be ${describe(algorithms)}.`);
|
|
25
|
+
const parameters = webCrypto[algorithm];
|
|
26
|
+
if (typeof key.sign === 'function') {
|
|
27
|
+
const signer = key;
|
|
28
|
+
if (!publicKey.usages.includes('verify'))
|
|
29
|
+
throw new TypeError('A Signer publicKey must have the verify usage.');
|
|
30
|
+
return {
|
|
31
|
+
algorithm,
|
|
32
|
+
publicKey,
|
|
33
|
+
async sign(payload) {
|
|
34
|
+
const signature = await signer.sign({ payload: Uint8Array.from(payload) });
|
|
35
|
+
if (!isUint8Array(signature))
|
|
36
|
+
throw new TypeError('A Signer must resolve with the raw signature as a Uint8Array.');
|
|
37
|
+
// A copy, so the signer can't change the signature after it is checked.
|
|
38
|
+
const bytes = Uint8Array.from(signature);
|
|
39
|
+
let valid = false;
|
|
40
|
+
try {
|
|
41
|
+
valid = await crypto.subtle.verify(parameters, publicKey, bytes, Uint8Array.from(payload));
|
|
42
|
+
}
|
|
43
|
+
catch { }
|
|
44
|
+
if (!valid)
|
|
45
|
+
throw new Error('The Signer returned a signature that does not verify with its publicKey. Check that it signs the payload itself (not a digest) with that key, and returns raw signature bytes.');
|
|
46
|
+
return bytes;
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const { privateKey } = key;
|
|
51
|
+
if (!isCryptoKey(privateKey) || privateKey.type !== 'private')
|
|
52
|
+
throw invalidKey();
|
|
53
|
+
if (algorithmOf(privateKey) !== algorithm)
|
|
54
|
+
throw new TypeError('The privateKey and publicKey of a signing key pair use different algorithms.');
|
|
55
|
+
return {
|
|
56
|
+
algorithm,
|
|
57
|
+
publicKey,
|
|
58
|
+
async sign(payload) {
|
|
59
|
+
return new Uint8Array(await crypto.subtle.sign(parameters, privateKey, Uint8Array.from(payload)));
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** The signature algorithm a key is for, or `undefined` when it is not supported. */
|
|
64
|
+
function algorithmOf(key) {
|
|
65
|
+
if (key.algorithm.name === 'Ed25519')
|
|
66
|
+
return Algorithms.ed25519;
|
|
67
|
+
if (key.algorithm.name === 'RSA-PSS' &&
|
|
68
|
+
key.algorithm.hash?.name === 'SHA-512')
|
|
69
|
+
return Algorithms.rsaPssSha512;
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
function describe(algorithms) {
|
|
73
|
+
return algorithms
|
|
74
|
+
.map((algorithm) => (algorithm === Algorithms.ed25519 ? 'Ed25519' : 'RSA-PSS SHA-512'))
|
|
75
|
+
.join(' or ');
|
|
76
|
+
}
|
|
77
|
+
function invalidKey() {
|
|
78
|
+
return new TypeError('A signing key must be a CryptoKeyPair or a Signer with publicKey and sign.');
|
|
79
|
+
}
|
|
80
|
+
/** Whether `value` is a `Uint8Array` (or `Buffer`), including one from another realm. */
|
|
81
|
+
function isUint8Array(value) {
|
|
82
|
+
return (ArrayBuffer.isView(value) && Object.prototype.toString.call(value) === '[object Uint8Array]');
|
|
83
|
+
}
|
|
84
|
+
function isObject(value) {
|
|
85
|
+
return typeof value === 'object' && value !== null;
|
|
86
|
+
}
|
|
87
|
+
function isCryptoKey(value) {
|
|
88
|
+
// Duck-typed so keys from another realm or a WebCrypto polyfill keep working.
|
|
89
|
+
return (isObject(value) &&
|
|
90
|
+
typeof value.type === 'string' &&
|
|
91
|
+
isObject(value.algorithm) &&
|
|
92
|
+
Array.isArray(value.usages));
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=Signer.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Poppy (Personal Agent Protocol 1.0) values. */
|
|
2
|
+
export declare const Constants: {
|
|
3
|
+
/** Header that carries the Session JWT on every request. */
|
|
4
|
+
readonly sessionHeader: 'Poppy-Session';
|
|
5
|
+
/** Header that names the protocol version. */
|
|
6
|
+
readonly versionHeader: 'Poppy-Version';
|
|
7
|
+
/** Protocol version this adapter speaks. */
|
|
8
|
+
readonly version: '1.0';
|
|
9
|
+
/** `typ` of a Session JWT. */
|
|
10
|
+
readonly sessionType: 'poppy-session+jwt';
|
|
11
|
+
/** `typ` of a delegation JWT. */
|
|
12
|
+
readonly delegationType: 'poppy-delegation+jwt';
|
|
13
|
+
/** Longest a delegation may last, in seconds (spec 5.2). */
|
|
14
|
+
readonly maxDelegationLifetime: number;
|
|
15
|
+
/** Default delegation lifetime, in seconds. */
|
|
16
|
+
readonly defaultDelegationLifetime: number;
|
|
17
|
+
/** Default window, in seconds, around a Session JWT's `iat`. */
|
|
18
|
+
readonly maxAge: 60;
|
|
19
|
+
/** Seconds a client's metadata and keys are cached. */
|
|
20
|
+
readonly keyCacheTtl: 300;
|
|
21
|
+
/** Seconds before a failed load or an unknown key `kid` loads a client's keys again. */
|
|
22
|
+
readonly keyRetryAfter: 30;
|
|
23
|
+
/** Seconds a client's last loaded keys keep verifying while reloads fail. */
|
|
24
|
+
readonly keyStaleTtl: number;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=Constants.d.ts.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Poppy (Personal Agent Protocol 1.0) values. */
|
|
2
|
+
export const Constants = {
|
|
3
|
+
/** Header that carries the Session JWT on every request. */
|
|
4
|
+
sessionHeader: 'Poppy-Session',
|
|
5
|
+
/** Header that names the protocol version. */
|
|
6
|
+
versionHeader: 'Poppy-Version',
|
|
7
|
+
/** Protocol version this adapter speaks. */
|
|
8
|
+
version: '1.0',
|
|
9
|
+
/** `typ` of a Session JWT. */
|
|
10
|
+
sessionType: 'poppy-session+jwt',
|
|
11
|
+
/** `typ` of a delegation JWT. */
|
|
12
|
+
delegationType: 'poppy-delegation+jwt',
|
|
13
|
+
/** Longest a delegation may last, in seconds (spec 5.2). */
|
|
14
|
+
maxDelegationLifetime: 24 * 60 * 60,
|
|
15
|
+
/** Default delegation lifetime, in seconds. */
|
|
16
|
+
defaultDelegationLifetime: 60 * 60,
|
|
17
|
+
/** Default window, in seconds, around a Session JWT's `iat`. */
|
|
18
|
+
maxAge: 60,
|
|
19
|
+
/** Seconds a client's metadata and keys are cached. */
|
|
20
|
+
keyCacheTtl: 300,
|
|
21
|
+
/** Seconds before a failed load or an unknown key `kid` loads a client's keys again. */
|
|
22
|
+
keyRetryAfter: 30,
|
|
23
|
+
/** Seconds a client's last loaded keys keep verifying while reloads fail. */
|
|
24
|
+
keyStaleTtl: 60 * 60,
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=Constants.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** A verified Poppy Session: who is calling, for whom, through which agent. */
|
|
2
|
+
export type Session = {
|
|
3
|
+
/** The Personal Agent's `client_id`: the URL of its client metadata document. */
|
|
4
|
+
readonly clientId: string;
|
|
5
|
+
/** The pseudonymous User ID the agent assigns this User for this Company. */
|
|
6
|
+
readonly userId: string;
|
|
7
|
+
/** The Session ID. */
|
|
8
|
+
readonly sessionId: string;
|
|
9
|
+
/** The optional Agent ID, when the Personal Agent runs several agents for the User. */
|
|
10
|
+
readonly agentId?: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
/** An ES256 delegated key that signs Session JWTs, used for one User ID only (spec 5.2). */
|
|
13
|
+
export type DelegatedKey = {
|
|
14
|
+
/** The public key as a JWK. */
|
|
15
|
+
readonly jwk: JsonWebKey;
|
|
16
|
+
/** The private key that signs each Session JWT. */
|
|
17
|
+
readonly privateKey: CryptoKey;
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=Types.d.ts.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type * as Identity from '../../../../identity/Types.js';
|
|
2
|
+
import type { MaybePromise } from '../../../../internal/types.js';
|
|
3
|
+
import type * as Types from '../Types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a prover that sends a Poppy `Poppy-Session` header with each request to one Company.
|
|
6
|
+
*
|
|
7
|
+
* A Session JWT is signed fresh for every attempt, including the paid retry, so it names the
|
|
8
|
+
* request's method and URL and, when the request carries an OAuth access token, binds to it with
|
|
9
|
+
* `ath`. The Payment Credential is not an access token: it is never bound.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const mppx = Mppx.create({
|
|
14
|
+
* identity: [
|
|
15
|
+
* Poppy.Client.prover({
|
|
16
|
+
* audience: 'https://api.example.com',
|
|
17
|
+
* clientId: 'https://agent.example/agent.json',
|
|
18
|
+
* delegation: () => delegations.current(),
|
|
19
|
+
* key: delegatedKey,
|
|
20
|
+
* scope: ['https://api.example.com'],
|
|
21
|
+
* userId: 'usr_Q7c1vK',
|
|
22
|
+
* }),
|
|
23
|
+
* ],
|
|
24
|
+
* methods: [tempo({ account })],
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function prover(config: prover.Config): Identity.Prover;
|
|
29
|
+
export declare namespace prover {
|
|
30
|
+
type Config = {
|
|
31
|
+
/** The Company's identifier: its OAuth issuer, or `https://{organization.domain}`. */
|
|
32
|
+
readonly audience: string;
|
|
33
|
+
/** The Personal Agent's `client_id`, the HTTPS URL of its client metadata document. */
|
|
34
|
+
readonly clientId: string;
|
|
35
|
+
/** Returns a current delegation for `key`, signed by a key in the agent's `jwks_uri`. */
|
|
36
|
+
readonly delegation: () => MaybePromise<string>;
|
|
37
|
+
/** The delegated key that signs Session JWTs. Use it for this User ID only. */
|
|
38
|
+
readonly key: Types.DelegatedKey;
|
|
39
|
+
/** Origins of this Company that receive the header. */
|
|
40
|
+
readonly scope: Identity.Prover['scope'];
|
|
41
|
+
/** The User ID for this Company: stable, opaque and not derived from personal information. */
|
|
42
|
+
readonly userId: string;
|
|
43
|
+
/** The optional Agent ID, for a Personal Agent that runs several agents for the User. */
|
|
44
|
+
readonly agentId?: string | undefined;
|
|
45
|
+
/** The Session ID. @default a random ID per prover */
|
|
46
|
+
readonly sessionId?: string | undefined;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Signs a delegation (spec 5.2): a short-lived JWT in which a key from the agent's `jwks_uri`
|
|
51
|
+
* vouches for a delegated key. It names no User or Company.
|
|
52
|
+
*/
|
|
53
|
+
export declare function delegate(parameters: delegate.Parameters): Promise<string>;
|
|
54
|
+
export declare namespace delegate {
|
|
55
|
+
type Parameters = {
|
|
56
|
+
/** The Personal Agent's `client_id`. */
|
|
57
|
+
readonly clientId: string;
|
|
58
|
+
/** The ES256 key from `jwks_uri` that signs the delegation, with its `kid`. */
|
|
59
|
+
readonly key: {
|
|
60
|
+
readonly kid: string;
|
|
61
|
+
readonly privateKey: CryptoKey;
|
|
62
|
+
};
|
|
63
|
+
/** The delegated public key. */
|
|
64
|
+
readonly publicKey: JsonWebKey;
|
|
65
|
+
/** Lifetime in seconds, at most 24 hours. @default 3600 */
|
|
66
|
+
readonly expiresIn?: number | undefined;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as Jwt from '../../tempo/internal/Jwt.js';
|
|
2
|
+
import { Constants } from '../Constants.js';
|
|
3
|
+
import * as Jws from '../internal/Jws.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a prover that sends a Poppy `Poppy-Session` header with each request to one Company.
|
|
6
|
+
*
|
|
7
|
+
* A Session JWT is signed fresh for every attempt, including the paid retry, so it names the
|
|
8
|
+
* request's method and URL and, when the request carries an OAuth access token, binds to it with
|
|
9
|
+
* `ath`. The Payment Credential is not an access token: it is never bound.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const mppx = Mppx.create({
|
|
14
|
+
* identity: [
|
|
15
|
+
* Poppy.Client.prover({
|
|
16
|
+
* audience: 'https://api.example.com',
|
|
17
|
+
* clientId: 'https://agent.example/agent.json',
|
|
18
|
+
* delegation: () => delegations.current(),
|
|
19
|
+
* key: delegatedKey,
|
|
20
|
+
* scope: ['https://api.example.com'],
|
|
21
|
+
* userId: 'usr_Q7c1vK',
|
|
22
|
+
* }),
|
|
23
|
+
* ],
|
|
24
|
+
* methods: [tempo({ account })],
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function prover(config) {
|
|
29
|
+
const sessionId = config.sessionId ?? crypto.randomUUID().replaceAll('-', '');
|
|
30
|
+
let kid;
|
|
31
|
+
return {
|
|
32
|
+
scope: config.scope,
|
|
33
|
+
headers: [Constants.sessionHeader.toLowerCase()],
|
|
34
|
+
async prove(request) {
|
|
35
|
+
const headers = new Headers(request.headers);
|
|
36
|
+
const token = Jws.accessToken(headers);
|
|
37
|
+
// The verifier rejects an access token it can't bind, so don't sign a Session it would refuse.
|
|
38
|
+
if (token === null)
|
|
39
|
+
throw new TypeError('Authorization must hold one Bearer or DPoP token, or none.');
|
|
40
|
+
const session = await Jws.sign({
|
|
41
|
+
alg: 'ES256',
|
|
42
|
+
kid: await (kid ??= Jwt.thumbprint(config.key.jwk)),
|
|
43
|
+
poppy_dlg: await config.delegation(),
|
|
44
|
+
typ: Constants.sessionType,
|
|
45
|
+
}, {
|
|
46
|
+
...(config.agentId && { agent_id: config.agentId }),
|
|
47
|
+
...(token && { ath: await Jwt.sha256(token) }),
|
|
48
|
+
aud: config.audience,
|
|
49
|
+
htm: request.method,
|
|
50
|
+
htu: Jwt.htu(request.url),
|
|
51
|
+
iat: Math.floor(Date.now() / 1_000),
|
|
52
|
+
iss: config.clientId,
|
|
53
|
+
jti: crypto.randomUUID(),
|
|
54
|
+
sid: sessionId,
|
|
55
|
+
sub: config.userId,
|
|
56
|
+
}, config.key.privateKey);
|
|
57
|
+
headers.set(Constants.sessionHeader, session);
|
|
58
|
+
headers.set(Constants.versionHeader, Constants.version);
|
|
59
|
+
return new Request(request, { headers });
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Signs a delegation (spec 5.2): a short-lived JWT in which a key from the agent's `jwks_uri`
|
|
65
|
+
* vouches for a delegated key. It names no User or Company.
|
|
66
|
+
*/
|
|
67
|
+
export function delegate(parameters) {
|
|
68
|
+
const { clientId, expiresIn = Constants.defaultDelegationLifetime, key, publicKey } = parameters;
|
|
69
|
+
if (!Number.isInteger(expiresIn) || expiresIn <= 0 || expiresIn > Constants.maxDelegationLifetime)
|
|
70
|
+
throw new RangeError('A delegation lasts between 1 second and 24 hours.');
|
|
71
|
+
const { crv, kty, x, y } = publicKey;
|
|
72
|
+
const iat = Math.floor(Date.now() / 1_000);
|
|
73
|
+
return Jws.sign({ alg: 'ES256', kid: key.kid, typ: Constants.delegationType }, {
|
|
74
|
+
cnf: { jwk: { crv, kty, x, y } },
|
|
75
|
+
exp: iat + expiresIn,
|
|
76
|
+
iat,
|
|
77
|
+
iss: clientId,
|
|
78
|
+
jti: crypto.randomUUID(),
|
|
79
|
+
}, key.privateKey);
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Client helpers for Poppy (Personal Agent Protocol) Sessions. */
|
|
2
|
+
export * as Client from './client/index.js';
|
|
3
|
+
/** Server helpers for Poppy (Personal Agent Protocol) Sessions. */
|
|
4
|
+
export * as Server from './server/index.js';
|
|
5
|
+
export * from './Constants.js';
|
|
6
|
+
export * from './Types.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Client helpers for Poppy (Personal Agent Protocol) Sessions. */
|
|
2
|
+
export * as Client from './client/index.js';
|
|
3
|
+
/** Server helpers for Poppy (Personal Agent Protocol) Sessions. */
|
|
4
|
+
export * as Server from './server/index.js';
|
|
5
|
+
export * from './Constants.js';
|
|
6
|
+
export * from './Types.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type * as Jwt from '../../tempo/internal/Jwt.js';
|
|
2
|
+
/** Signs a compact ES256 JWS. */
|
|
3
|
+
export declare function sign(header: object, claims: object, privateKey: CryptoKey): Promise<string>;
|
|
4
|
+
/** Verifies a decoded JWS with a public JWK, for `ES256` or `PS384` only. */
|
|
5
|
+
export declare function verify(jws: Jwt.Decoded, jwk: JsonWebKey, algs: readonly string[]): Promise<boolean>;
|
|
6
|
+
/**
|
|
7
|
+
* The OAuth access token a request carries as `Bearer` or `DPoP` (a Payment Credential is not
|
|
8
|
+
* one), `undefined` without one, or `null` when `Authorization` names one of those schemes in any
|
|
9
|
+
* other form, such as several combined values, so it can't skip the `ath` binding.
|
|
10
|
+
*/
|
|
11
|
+
export declare function accessToken(headers: Headers): string | null | undefined;
|
|
12
|
+
//# sourceMappingURL=Jws.d.ts.map
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Base64 } from 'ox';
|
|
2
|
+
/** Signs a compact ES256 JWS. */
|
|
3
|
+
export async function sign(header, claims, privateKey) {
|
|
4
|
+
const input = `${encode(header)}.${encode(claims)}`;
|
|
5
|
+
const signature = await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, privateKey, new TextEncoder().encode(input));
|
|
6
|
+
return `${input}.${Base64.fromBytes(new Uint8Array(signature), { pad: false, url: true })}`;
|
|
7
|
+
}
|
|
8
|
+
/** Verifies a decoded JWS with a public JWK, for `ES256` or `PS384` only. */
|
|
9
|
+
export async function verify(jws, jwk, algs) {
|
|
10
|
+
const alg = jws.header.alg;
|
|
11
|
+
if (!algs.includes(alg))
|
|
12
|
+
return false;
|
|
13
|
+
try {
|
|
14
|
+
if (alg === 'ES256') {
|
|
15
|
+
if (jwk.kty !== 'EC' || jwk.crv !== 'P-256')
|
|
16
|
+
return false;
|
|
17
|
+
const { crv, kty, x, y } = jwk;
|
|
18
|
+
const key = await crypto.subtle.importKey('jwk', { crv, kty, x, y }, { name: 'ECDSA', namedCurve: 'P-256' }, false, ['verify']);
|
|
19
|
+
return crypto.subtle.verify({ name: 'ECDSA', hash: 'SHA-256' }, key, jws.signature, jws.signingInput);
|
|
20
|
+
}
|
|
21
|
+
if (jwk.kty !== 'RSA')
|
|
22
|
+
return false;
|
|
23
|
+
const { e, kty, n } = jwk;
|
|
24
|
+
const key = await crypto.subtle.importKey('jwk', { e, kty, n }, { name: 'RSA-PSS', hash: 'SHA-384' }, false, ['verify']);
|
|
25
|
+
return crypto.subtle.verify({ name: 'RSA-PSS', saltLength: 48 }, key, jws.signature, jws.signingInput);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The OAuth access token a request carries as `Bearer` or `DPoP` (a Payment Credential is not
|
|
33
|
+
* one), `undefined` without one, or `null` when `Authorization` names one of those schemes in any
|
|
34
|
+
* other form, such as several combined values, so it can't skip the `ath` binding.
|
|
35
|
+
*/
|
|
36
|
+
export function accessToken(headers) {
|
|
37
|
+
const value = headers.get('Authorization')?.trim();
|
|
38
|
+
if (!value)
|
|
39
|
+
return undefined;
|
|
40
|
+
const match = /^(?:bearer|dpop)[ \t]+([^\s,"]+)$/i.exec(value);
|
|
41
|
+
if (match)
|
|
42
|
+
return match[1];
|
|
43
|
+
// Quoted auth-param values can contain anything, so a scheme name inside one doesn't count.
|
|
44
|
+
const unquoted = unquote(value);
|
|
45
|
+
// An unterminated quote could hide a scheme name, so it fails closed when one appears anywhere.
|
|
46
|
+
if (unquoted === undefined)
|
|
47
|
+
return /\b(?:bearer|dpop)\b/i.test(value) ? null : undefined;
|
|
48
|
+
return /(?:^|,)\s*(?:bearer|dpop)(?:\s|$)/i.test(unquoted) ? null : undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Blanks the contents of quoted strings in one pass, so the input's length bounds the work.
|
|
52
|
+
* Returns `undefined` for an unterminated quoted string.
|
|
53
|
+
*/
|
|
54
|
+
function unquote(value) {
|
|
55
|
+
let result = '';
|
|
56
|
+
let quoted = false;
|
|
57
|
+
for (let i = 0; i < value.length; i++) {
|
|
58
|
+
const char = value[i];
|
|
59
|
+
if (quoted) {
|
|
60
|
+
if (char === '\\')
|
|
61
|
+
i++;
|
|
62
|
+
else if (char === '"') {
|
|
63
|
+
quoted = false;
|
|
64
|
+
result += char;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
if (char === '"')
|
|
69
|
+
quoted = true;
|
|
70
|
+
result += char;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return quoted ? undefined : result;
|
|
74
|
+
}
|
|
75
|
+
function encode(value) {
|
|
76
|
+
return Base64.fromString(JSON.stringify(value), { pad: false, url: true });
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=Jws.js.map
|