unjwt 0.6.1 → 0.7.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.
@@ -0,0 +1,111 @@
1
+ import { A as JWSSignOptions, C as JWSMultiSignOptions, D as JWSMultiVerifyOutcome, E as JWSMultiVerifyOptions, M as JWSVerifyResult, Mt as JWK_Symmetric, O as JWSMultiVerifyResult, R as JOSEPayload, T as JWSMultiVerifyAllOptions, Tt as JWK_Public, at as JWKSet, b as JWSGeneralSerialization, j as JWSVerifyOptions, tt as JWKLookupFunction, v as JWSAlgorithm, w as JWSMultiSigner, wt as JWK_Private, y as JWSFlattenedSerialization } from "./index.mjs";
2
+ /**
3
+ * Sign a payload with multiple signers using JWS General JSON Serialization
4
+ * (RFC 7515 §7.2.1).
5
+ *
6
+ * The payload is shared across all signatures; each signer independently
7
+ * produces a signature over `BASE64URL(its own protected header) . BASE64URL(payload)`
8
+ * using its own key-declared algorithm. Per-signer `alg` is read from
9
+ * `key.alg` and throws `ERR_JWS_SIGNER_ALG_INFERENCE` when absent.
10
+ *
11
+ * Per RFC 7797 §3, `b64` MUST be consistent across every signer. If one
12
+ * signer sets `b64: false` in its protected header, every other signer
13
+ * must match or this function throws `ERR_JWS_B64_INCONSISTENT`.
14
+ *
15
+ * @example
16
+ * const jws = await signMulti({ sub: "u1" }, [
17
+ * { key: alicePrivateJwk },
18
+ * { key: bobPrivateJwk, protectedHeader: { typ: "vc+jwt" } },
19
+ * ], { expiresIn: "1h" });
20
+ */
21
+ declare function signMulti(payload: JOSEPayload, signers: JWSMultiSigner[], options?: JWSMultiSignOptions): Promise<JWSGeneralSerialization>;
22
+ /**
23
+ * Verify a JWS in JSON Serialization form (General RFC 7515 §7.2.1 or
24
+ * Flattened §7.2.2 — both accepted). Returns the first signature that
25
+ * verifies against the supplied key.
26
+ *
27
+ * Matches signers by, in order: `kid` equality, `kty`/`crv`/length
28
+ * compatibility, then trial verification. Set `strictSignerMatch: true`
29
+ * to disable the trial fallback.
30
+ *
31
+ * For compact (string) tokens, use {@link verify}.
32
+ */
33
+ declare function verifyMulti<T extends JOSEPayload = JOSEPayload>(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyOrLookup: CryptoKey | JWKSet | JWK_Public | JWK_Symmetric | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSMultiVerifyOptions): Promise<JWSMultiVerifyResult<T>>;
34
+ /**
35
+ * Verify every signature in a JWS independently and return a per-signature
36
+ * outcome array — the caller applies their own policy (all-must-verify,
37
+ * M-of-N quorum, specific-signer checks, audit logs, etc.).
38
+ *
39
+ * Unlike {@link verifyMulti}, this function never throws on an individual
40
+ * signature's failure — malformed protected headers, disallowed `alg`s,
41
+ * `typ` mismatches, key-resolver errors, bad signatures, critical header
42
+ * violations, and JWT claim failures are all collected into
43
+ * {@link JWSMultiVerifyOutcome} entries with `verified: false`.
44
+ *
45
+ * Structural errors in the envelope itself (non-object input, missing
46
+ * `payload` / `signatures[]`) still throw `ERR_JWS_INVALID_SERIALIZATION`
47
+ * — there's no per-signature outcome to return in that case.
48
+ *
49
+ * Requires a {@link JWKLookupFunction} because different signatures almost
50
+ * always need different keys. Wrap a static `JWKSet` as `(header) => mySet`
51
+ * if that's all you have.
52
+ *
53
+ * @example
54
+ * // All signatures must verify
55
+ * const outcomes = await verifyMultiAll(jws, myKeyResolver);
56
+ * if (!outcomes.every((o) => o.verified)) {
57
+ * throw new Error("not all signatures verified");
58
+ * }
59
+ *
60
+ * @example
61
+ * // Quorum — at least 2 valid signatures from a recognised signer set
62
+ * const outcomes = await verifyMultiAll(jws, myKeyResolver);
63
+ * const validKids = outcomes
64
+ * .filter((o) => o.verified)
65
+ * .map((o) => o.protectedHeader.kid);
66
+ * if (new Set(validKids).size < 2) {
67
+ * throw new Error("quorum not met");
68
+ * }
69
+ */
70
+ declare function verifyMultiAll<T extends JOSEPayload = JOSEPayload>(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyResolver: JWKLookupFunction, options?: JWSMultiVerifyAllOptions): Promise<JWSMultiVerifyOutcome<T>[]>;
71
+ /**
72
+ * Convert a {@link JWSGeneralSerialization} with exactly one signature into
73
+ * the {@link JWSFlattenedSerialization} form (RFC 7515 §7.2.2). Throws
74
+ * `ERR_JWS_INVALID_SERIALIZATION` when the input has zero or multiple
75
+ * signatures — Flattened is strictly single-signature.
76
+ *
77
+ * `signMulti` never emits Flattened itself (stable return shape across
78
+ * signer counts); this is the canonical post-processing step when
79
+ * interoperating with a strict Flattened-only consumer.
80
+ *
81
+ * @example
82
+ * const general = await signMulti(payload, [signer], opts);
83
+ * const flattened = generalToFlattenedJWS(general);
84
+ */
85
+ declare function generalToFlattenedJWS(jws: JWSGeneralSerialization): JWSFlattenedSerialization;
86
+ /**
87
+ * Creates a JWS (JSON Web Signature) in Compact Serialization format.
88
+ *
89
+ * @param payload The payload to sign. Can be a string, Uint8Array, or an object (which will be JSON stringified).
90
+ * @param key The signing key (CryptoKey, JWK, or raw symmetric key as Uint8Array).
91
+ * @param options Signing options, including the algorithm (`alg`) and protected header parameters.
92
+ * @returns A Promise resolving to the JWS Compact Serialization string.
93
+ */
94
+ declare function sign(payload: JOSEPayload, key: JWK_Symmetric | JWK_Private, options?: JWSSignOptions): Promise<string>;
95
+ declare function sign(payload: JOSEPayload, key: CryptoKey | Uint8Array<ArrayBuffer>, options: JWSSignOptions & {
96
+ alg: JWSAlgorithm;
97
+ }): Promise<string>;
98
+ /**
99
+ * Verifies a JWS (JSON Web Signature) in Compact Serialization format.
100
+ *
101
+ * @param jws The JWS Compact Serialization string.
102
+ * @param key The verification key (CryptoKey, JWK, JWKSet, raw symmetric key as Uint8Array, or a function resolving the key or set).
103
+ * @param options Verification options, such as allowed algorithms.
104
+ * @returns A Promise resolving to an object containing the verified payload and protected header.
105
+ * @throws If the JWS is invalid, signature verification fails, or options are not met.
106
+ */
107
+ declare function verify<T extends JOSEPayload>(jws: string, key: CryptoKey | JWKSet | JWK_Public | JWK_Symmetric | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSVerifyOptions): Promise<JWSVerifyResult<T>>;
108
+ declare function verify(jws: string, key: CryptoKey | JWKSet | JWK_Public | JWK_Symmetric | Uint8Array<ArrayBuffer> | JWKLookupFunction, options: JWSVerifyOptions & {
109
+ forceUint8Array: true;
110
+ }): Promise<JWSVerifyResult<Uint8Array<ArrayBuffer>>>;
111
+ export { verifyMulti as a, signMulti as i, verify as n, verifyMultiAll as o, generalToFlattenedJWS as r, sign as t };