unjwt 0.7.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +4 -4
  2. package/dist/THIRD-PARTY-LICENSES.md.gz +0 -0
  3. package/dist/_chunks/_codec-jwe.d.mts +170 -0
  4. package/dist/_chunks/_codec-jws.mjs +878 -0
  5. package/dist/_chunks/_internal.d.mts +7 -0
  6. package/dist/_chunks/_jwt-internal.mjs +388 -0
  7. package/dist/_chunks/algorithms.mjs +186 -0
  8. package/dist/_chunks/index.d.mts +512 -818
  9. package/dist/_chunks/index2.d.mts +11 -0
  10. package/dist/_chunks/index3.d.mts +108 -0
  11. package/dist/_chunks/jwe.d.mts +43 -66
  12. package/dist/_chunks/jwe.mjs +384 -252
  13. package/dist/_chunks/jwk.mjs +1390 -622
  14. package/dist/_chunks/jws.d.mts +48 -103
  15. package/dist/_chunks/jws.mjs +372 -184
  16. package/dist/_chunks/jwt.d.mts +56 -0
  17. package/dist/_chunks/libs/unsecure.d.mts +164 -85
  18. package/dist/_chunks/libs/unsecure.mjs +390 -123
  19. package/dist/adapters/elysia/index.d.mts +33 -171
  20. package/dist/adapters/elysia/index.mjs +106 -575
  21. package/dist/adapters/h3v1/index.d.mts +59 -344
  22. package/dist/adapters/h3v1/index.mjs +114 -619
  23. package/dist/adapters/h3v2/index.d.mts +58 -326
  24. package/dist/adapters/h3v2/index.mjs +108 -611
  25. package/dist/core/internal.d.mts +2 -0
  26. package/dist/core/internal.mjs +1 -0
  27. package/dist/core/jwe.d.mts +4 -2
  28. package/dist/core/jwe.mjs +4 -2
  29. package/dist/core/jwk.d.mts +183 -371
  30. package/dist/core/jwk.mjs +4 -3
  31. package/dist/core/jws.d.mts +4 -2
  32. package/dist/core/jws.mjs +4 -2
  33. package/dist/core/utils/index.d.mts +5 -155
  34. package/dist/core/utils/index.mjs +12 -3
  35. package/dist/index.d.mts +8 -4
  36. package/dist/index.mjs +6 -3
  37. package/package.json +39 -36
  38. package/dist/_chunks/_internal.mjs +0 -88
  39. package/dist/_chunks/utils.mjs +0 -390
@@ -1,111 +1,56 @@
1
- import { JOSEPayload, JWKLookupFunction, JWKSet, JWSAlgorithm, JWSFlattenedSerialization, JWSGeneralSerialization, JWSMultiSignOptions, JWSMultiSigner, JWSMultiVerifyAllOptions, JWSMultiVerifyOptions, JWSMultiVerifyOutcome, JWSMultiVerifyResult, JWSSignJWK, JWSSignOptions, JWSVerifyJWK, JWSVerifyOptions, JWSVerifyResult } from "./index.mjs";
1
+ import { JOSEPayload, JOSEPayloadMode, JWKLookupFunction, JWKSet, JWSAlgorithm, JWSFlattenedSerialization, JWSGeneralSerialization, JWSMultiSignOptions, JWSMultiSigner, JWSMultiVerifyAllOptions, JWSMultiVerifyOptions, JWSMultiVerifyOutcome, JWSMultiVerifyResult, JWSSignJWK, JWSSignOptions, JWSVerifyJWK, JWSVerifyOptions, JWSVerifyResult, JWTClaims, PayloadOf } from "./index.mjs";
2
+ import "./_internal.mjs";
2
3
  /**
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>;
4
+ * Signs a payload with multiple signers as a JWS General JSON Serialization (RFC 7515 §7.2.1).
5
+ * Each signer's `alg` is inferred from its key when omitted (`ERR_JWS_SIGNER_ALG_INFERENCE`);
6
+ * `b64` must match across signers (RFC 7797 §3, `ERR_JWS_B64_INCONSISTENT`).
7
+ */
8
+ export declare function signMulti(payload: JOSEPayload, signers: JWSMultiSigner[], options?: JWSMultiSignOptions): Promise<JWSGeneralSerialization>;
22
9
  /**
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 | JWSVerifyJWK | 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;
10
+ * Verifies a JWS General or Flattened JSON Serialization (RFC 7515 §7.2) and returns the first signature that verifies.
11
+ * Per-signature failures are collected and the last one is thrown when none verifies.
12
+ * The payload type follows `options.payload`, as in `verify`.
13
+ */
14
+ export declare function verifyMulti<T extends Record<string, unknown> = JWTClaims>(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyOrLookup: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSMultiVerifyOptions<"json"> & {
15
+ forceUint8Array?: false;
16
+ }): Promise<JWSMultiVerifyResult<T>>;
17
+ /** @deprecated `forceUint8Array` is replaced by `payload: "bytes"`; removed in v0.9.0. */
18
+ export declare function verifyMulti(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyOrLookup: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options: JWSMultiVerifyOptions & {
19
+ forceUint8Array: true;
20
+ }): Promise<JWSMultiVerifyResult<Uint8Array<ArrayBuffer>>>;
21
+ export declare function verifyMulti<Mode extends JOSEPayloadMode = JOSEPayloadMode>(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyOrLookup: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSMultiVerifyOptions<Mode>): Promise<JWSMultiVerifyResult<PayloadOf<Mode>>>;
86
22
  /**
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: JWSSignJWK, options?: JWSSignOptions): Promise<string>;
95
- declare function sign(payload: JOSEPayload, key: CryptoKey | Uint8Array<ArrayBuffer>, options: JWSSignOptions & {
23
+ * Verifies every signature independently and returns one outcome per signature; only
24
+ * a malformed envelope throws (`ERR_JWS_INVALID_SERIALIZATION`). The payload type follows `options.payload`.
25
+ *
26
+ * @example
27
+ * const outcomes = await verifyMultiAll(jws, (header) => jwkSet);
28
+ * if (!outcomes.every((o) => o.verified)) throw new Error("not all signatures verified");
29
+ */
30
+ export declare function verifyMultiAll<T extends Record<string, unknown> = JWTClaims>(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyResolver: JWKLookupFunction, options?: JWSMultiVerifyAllOptions<"json"> & {
31
+ forceUint8Array?: false;
32
+ }): Promise<JWSMultiVerifyOutcome<T>[]>;
33
+ /** @deprecated `forceUint8Array` is replaced by `payload: "bytes"`; removed in v0.9.0. */
34
+ export declare function verifyMultiAll(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyResolver: JWKLookupFunction, options: JWSMultiVerifyAllOptions & {
35
+ forceUint8Array: true;
36
+ }): Promise<JWSMultiVerifyOutcome<Uint8Array<ArrayBuffer>>[]>;
37
+ export declare function verifyMultiAll<Mode extends JOSEPayloadMode = JOSEPayloadMode>(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyResolver: JWKLookupFunction, options?: JWSMultiVerifyAllOptions<Mode>): Promise<JWSMultiVerifyOutcome<PayloadOf<Mode>>[]>;
38
+ /** Converts a single-signature General JWS to Flattened form (RFC 7515 §7.2.2); otherwise `ERR_JWS_INVALID_SERIALIZATION`. */
39
+ export declare function generalToFlattenedJWS(jws: JWSGeneralSerialization): JWSFlattenedSerialization;
40
+ /** Creates a JWS Compact Serialization; an object payload is JSON-serialized, `alg` is required unless the JWK carries one. */
41
+ export declare function sign(payload: JOSEPayload, key: JWSSignJWK, options?: JWSSignOptions): Promise<string>;
42
+ export declare function sign(payload: JOSEPayload, key: CryptoKey | Uint8Array<ArrayBuffer>, options: JWSSignOptions & {
96
43
  alg: JWSAlgorithm;
97
44
  }): Promise<string>;
98
45
  /**
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 | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSVerifyOptions): Promise<JWSVerifyResult<T>>;
108
- declare function verify(jws: string, key: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options: JWSVerifyOptions & {
46
+ * Verifies a JWS Compact Serialization against a key, JWK Set or key resolver.
47
+ * The payload type follows `options.payload`: claims `T` for `"json"` (default), `string`, or `Uint8Array`.
48
+ */
49
+ export declare function verify<T extends Record<string, unknown> = JWTClaims>(jws: string, key: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSVerifyOptions<"json"> & {
50
+ forceUint8Array?: false;
51
+ }): Promise<JWSVerifyResult<T>>;
52
+ /** @deprecated `forceUint8Array` is replaced by `payload: "bytes"`; removed in v0.9.0. */
53
+ export declare function verify(jws: string, key: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options: JWSVerifyOptions & {
109
54
  forceUint8Array: true;
110
55
  }): Promise<JWSVerifyResult<Uint8Array<ArrayBuffer>>>;
111
- export { generalToFlattenedJWS, sign, signMulti, verify, verifyMulti, verifyMultiAll };
56
+ export declare function verify<Mode extends JOSEPayloadMode = JOSEPayloadMode>(jws: string, key: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSVerifyOptions<Mode>): Promise<JWSVerifyResult<PayloadOf<Mode>>>;