unjwt 0.8.0 → 0.8.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.
@@ -318,7 +318,7 @@ function getEncryptKey(config, prefix) {
318
318
  if (isCryptoKey(key) || isSymmetricJWK(key)) resolved = key;
319
319
  else if (isKeyPair(key)) {
320
320
  assertJWEKeyPair(key, prefix);
321
- resolved = key.publicKey ?? key.privateKey;
321
+ resolved = key.publicKey;
322
322
  }
323
323
  if (!resolved) throw invalidKeyError(`${prefix} Invalid JWE key. It must be a JWK or a CryptoKey.`, key);
324
324
  assertPinnedAlgEnc(resolved, config, prefix);
@@ -341,7 +341,7 @@ function assertJWEKeyPair(key, prefix) {
341
341
  assertKeyPair(key, {
342
342
  prefix,
343
343
  family: "JWE",
344
- isPublic: (half) => half === void 0 || isCryptoKey(half) || isPublicJWK(half),
344
+ isPublic: (half) => isCryptoKey(half) || isPublicJWK(half),
345
345
  publicHalf: "a public JWK or a CryptoKey"
346
346
  });
347
347
  }
@@ -68,7 +68,7 @@ function isSymmetricJWK(key) {
68
68
  return isJWK(key) && key.kty === "oct" && typeof key.k === "string";
69
69
  }
70
70
  function isAsymmetricJWK(key) {
71
- return !isSymmetricJWK(key);
71
+ return isJWK(key) && key.kty !== "oct";
72
72
  }
73
73
  function isPrivateJWK(key) {
74
74
  if (!isJWK(key) || key.kty === "oct") return false;
@@ -451,7 +451,7 @@ export interface JoseHeaderParameters {
451
451
  [propName: string]: unknown;
452
452
  }
453
453
  /** Payload accepted by `sign` and `encrypt`: a string, raw bytes, or a JSON object. */
454
- export type JOSEPayload = string | Uint8Array<ArrayBuffer> | Record<string, unknown>;
454
+ export type JOSEPayload = string | Uint8Array<ArrayBuffer> | object;
455
455
  /** How a verified or decrypted payload is decoded: `"json"` object, UTF-8 `"string"`, or raw `"bytes"`. */
456
456
  export type JOSEPayloadMode = keyof JOSEPayloadTypeMap;
457
457
  /** The payload type each `JOSEPayloadMode` resolves to. */
@@ -57,19 +57,19 @@ export declare function isCryptoKey(key: unknown): key is CryptoKey;
57
57
  /** Whether `key` is a `CryptoKeyPair` of two CryptoKeys. */
58
58
  export declare const isCryptoKeyPair: (key: unknown) => key is CryptoKeyPair;
59
59
  /** Whether the JWK is symmetric (`oct`); its `k` is always secret. */
60
- export declare function isSymmetricJWK<T>(key: T): key is Extract<T, JWK_oct>;
60
+ export declare function isSymmetricJWK<T extends JWK>(key: T): key is Extract<T, JWK_oct>;
61
61
  export declare function isSymmetricJWK(key: unknown): key is JWK_oct;
62
62
  /** Whether the JWK is asymmetric (RSA, EC, OKP). */
63
- export declare function isAsymmetricJWK<T>(key: T): key is Exclude<T, JWK_oct>;
63
+ export declare function isAsymmetricJWK<T extends JWK>(key: T): key is Exclude<T, JWK_oct>;
64
64
  export declare function isAsymmetricJWK(key: unknown): key is Exclude<JWK, JWK_oct>;
65
65
  /**
66
66
  * Whether the JWK holds asymmetric private material (`d`). It is `false` for `oct` keys,
67
67
  * so `!isPrivateJWK(key)` is not a "safe to publish" check; use `isPublicJWK`.
68
68
  */
69
- export declare function isPrivateJWK<T>(key: T): key is Extract<T, JWK_EC_Private | JWK_OKP_Private | JWK_RSA_Private>;
69
+ export declare function isPrivateJWK<T extends JWK>(key: T): key is Extract<T, JWK_EC_Private | JWK_OKP_Private | JWK_RSA_Private>;
70
70
  export declare function isPrivateJWK(key: unknown): key is JWK_Private;
71
71
  /** Whether the JWK is an asymmetric public key without `d`: the "safe to publish" check. */
72
- export declare function isPublicJWK<T>(key: T): key is Exclude<Extract<T, JWK_EC_Public | JWK_OKP_Public | JWK_RSA_Public>, JWK_Private>;
72
+ export declare function isPublicJWK<T extends JWK>(key: T): key is Exclude<Extract<T, JWK_EC_Public | JWK_OKP_Public | JWK_RSA_Public>, JWK_Private>;
73
73
  export declare function isPublicJWK(key: unknown): key is JWK_Public;
74
74
  /** How a JWK Set's entries are read during algorithm inference. */
75
75
  export interface InferAlgorithmsOptions {
@@ -13,7 +13,7 @@ export declare function encryptMulti(payload: JOSEPayload, recipients: JWEMultiR
13
13
  * `ERR_JWE_NO_MATCHING_RECIPIENT` when none qualifies; a JWK Set with no `kid` candidate
14
14
  * throws `ERR_JWK_KEY_NOT_FOUND`. The payload type follows `options.payload`.
15
15
  */
16
- export declare function decryptMulti<T extends Record<string, unknown> = JWTClaims>(jwe: JWEGeneralSerialization | JWEFlattenedSerialization, keyOrLookup: JWEMultiDecryptKey, options?: JWEMultiDecryptOptions<"json"> & {
16
+ export declare function decryptMulti<T extends object = JWTClaims>(jwe: JWEGeneralSerialization | JWEFlattenedSerialization, keyOrLookup: JWEMultiDecryptKey, options?: JWEMultiDecryptOptions<"json"> & {
17
17
  forceUint8Array?: false;
18
18
  }): Promise<JWEMultiDecryptResult<T>>;
19
19
  /** @deprecated `forceUint8Array` is replaced by `payload: "bytes"`; removed in v0.9.0. */
@@ -50,7 +50,7 @@ export declare function encrypt(payload: JOSEPayload, key: CryptoKey | JWEEncryp
50
50
  * Decrypt a JWE Compact Serialization string; `key` may be a lookup function.
51
51
  * The payload type follows `options.payload` (claims `T` by default).
52
52
  */
53
- export declare function decrypt<T extends Record<string, unknown> = JWTClaims>(jwe: string, key: CryptoKey | JWKSet | JWEDecryptJWK | string | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWEDecryptOptions<"json"> & {
53
+ export declare function decrypt<T extends object = JWTClaims>(jwe: string, key: CryptoKey | JWKSet | JWEDecryptJWK | string | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWEDecryptOptions<"json"> & {
54
54
  forceUint8Array?: false;
55
55
  }): Promise<JWEDecryptResult<T>>;
56
56
  /** @deprecated `forceUint8Array` is replaced by `payload: "bytes"`; removed in v0.9.0. */
@@ -1378,10 +1378,7 @@ function keyMatchesHeader(header, keyMaterial, options) {
1378
1378
  }));
1379
1379
  return candidates.length === 0 || candidates.some((k) => keyServesHeaderAlg(header, k, ctx));
1380
1380
  }
1381
- if (isJWK(keyMaterial)) {
1382
- if (header.kid && keyMaterial.kid && header.kid !== keyMaterial.kid) return false;
1383
- return keyServesHeaderAlg(header, keyMaterial, ctx);
1384
- }
1381
+ if (isJWK(keyMaterial)) return keyServesHeaderAlg(header, keyMaterial, ctx);
1385
1382
  if (isCryptoKey(keyMaterial)) return keyServesHeaderAlg(header, keyMaterial, ctx);
1386
1383
  return true;
1387
1384
  }
@@ -11,7 +11,7 @@ export declare function signMulti(payload: JOSEPayload, signers: JWSMultiSigner[
11
11
  * Per-signature failures are collected and the last one is thrown when none verifies.
12
12
  * The payload type follows `options.payload`, as in `verify`.
13
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"> & {
14
+ export declare function verifyMulti<T extends object = JWTClaims>(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyOrLookup: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSMultiVerifyOptions<"json"> & {
15
15
  forceUint8Array?: false;
16
16
  }): Promise<JWSMultiVerifyResult<T>>;
17
17
  /** @deprecated `forceUint8Array` is replaced by `payload: "bytes"`; removed in v0.9.0. */
@@ -27,7 +27,7 @@ export declare function verifyMulti<Mode extends JOSEPayloadMode = JOSEPayloadMo
27
27
  * const outcomes = await verifyMultiAll(jws, (header) => jwkSet);
28
28
  * if (!outcomes.every((o) => o.verified)) throw new Error("not all signatures verified");
29
29
  */
30
- export declare function verifyMultiAll<T extends Record<string, unknown> = JWTClaims>(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyResolver: JWKLookupFunction, options?: JWSMultiVerifyAllOptions<"json"> & {
30
+ export declare function verifyMultiAll<T extends object = JWTClaims>(jws: JWSGeneralSerialization | JWSFlattenedSerialization, keyResolver: JWKLookupFunction, options?: JWSMultiVerifyAllOptions<"json"> & {
31
31
  forceUint8Array?: false;
32
32
  }): Promise<JWSMultiVerifyOutcome<T>[]>;
33
33
  /** @deprecated `forceUint8Array` is replaced by `payload: "bytes"`; removed in v0.9.0. */
@@ -46,7 +46,7 @@ export declare function sign(payload: JOSEPayload, key: CryptoKey | Uint8Array<A
46
46
  * Verifies a JWS Compact Serialization against a key, JWK Set or key resolver.
47
47
  * The payload type follows `options.payload`: claims `T` for `"json"` (default), `string`, or `Uint8Array`.
48
48
  */
49
- export declare function verify<T extends Record<string, unknown> = JWTClaims>(jws: string, key: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSVerifyOptions<"json"> & {
49
+ export declare function verify<T extends object = JWTClaims>(jws: string, key: CryptoKey | JWKSet | JWSVerifyJWK | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSVerifyOptions<"json"> & {
50
50
  forceUint8Array?: false;
51
51
  }): Promise<JWSVerifyResult<T>>;
52
52
  /** @deprecated `forceUint8Array` is replaced by `payload: "bytes"`; removed in v0.9.0. */
@@ -1,4 +1,4 @@
1
- import { Duration, JOSEPayloadMode, JWTClaimValidationOptions, JWTClaims, JWTErrorCode, JWTTimeClaimOptions, PayloadOf } from "./index.mjs";
1
+ import { Duration, JOSEPayload, JOSEPayloadMode, JWTClaimValidationOptions, JWTClaims, JWTErrorCode, JWTTimeClaimOptions, PayloadOf } from "./index.mjs";
2
2
  /** Set `typ: "JWT"` on a header without `typ` when the payload is an object (not a `Uint8Array`). */
3
3
  export declare function applyTypDefault<T extends {
4
4
  typ?: string;
@@ -19,7 +19,7 @@ export declare function parsePayloadText(bytes: Uint8Array<ArrayBuffer>, options
19
19
  export declare function parsePayloadSegment<T = JWTClaims>(payloadEncoded: string, options?: ParsePayloadOptions<"json">): T;
20
20
  export declare function parsePayloadSegment<Mode extends JOSEPayloadMode = JOSEPayloadMode>(payloadEncoded: string, options?: ParsePayloadOptions<Mode>): PayloadOf<Mode>;
21
21
  /** Encode a payload to bytes; a non-serializable one throws `ERR_INVALID_OPTION`. */
22
- export declare function getPlaintextBytes(payload: string | Uint8Array<ArrayBuffer> | Record<string, unknown>): Uint8Array<ArrayBuffer>;
22
+ export declare function getPlaintextBytes(payload: JOSEPayload): Uint8Array<ArrayBuffer>;
23
23
  /** Options for {@link computeJwtTimeClaims}. */
24
24
  export type ComputeJwtTimeClaimsOptions = JWTTimeClaimOptions;
25
25
  /**
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "unjwt",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Low-level JWT utilities",
5
+ "homepage": "https://unjwt.s94.dev",
5
6
  "license": "MIT",
6
7
  "author": {
7
8
  "name": "Sandro Circi",