jose 4.15.4 → 5.0.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.
Files changed (155) hide show
  1. package/README.md +2 -1
  2. package/dist/browser/index.js +2 -4
  3. package/dist/browser/jwe/flattened/decrypt.js +13 -22
  4. package/dist/browser/jwe/flattened/encrypt.js +3 -18
  5. package/dist/browser/jwe/general/decrypt.js +1 -1
  6. package/dist/browser/jwe/general/encrypt.js +8 -13
  7. package/dist/browser/jwk/embedded.js +2 -2
  8. package/dist/browser/jwk/thumbprint.js +2 -2
  9. package/dist/browser/jwks/local.js +2 -2
  10. package/dist/browser/jwks/remote.js +4 -4
  11. package/dist/browser/jws/flattened/sign.js +1 -1
  12. package/dist/browser/jws/flattened/verify.js +6 -7
  13. package/dist/browser/jws/general/verify.js +1 -1
  14. package/dist/browser/jwt/produce.js +19 -4
  15. package/dist/browser/jwt/sign.js +1 -2
  16. package/dist/browser/jwt/unsecured.js +1 -1
  17. package/dist/browser/jwt/verify.js +1 -2
  18. package/dist/browser/key/import.js +1 -6
  19. package/dist/browser/lib/decrypt_key_management.js +6 -6
  20. package/dist/browser/lib/jwt_claims_set.js +1 -1
  21. package/dist/browser/runtime/asn1.js +1 -2
  22. package/dist/browser/runtime/base64url.js +1 -1
  23. package/dist/browser/runtime/decrypt.js +3 -3
  24. package/dist/browser/runtime/fetch_jwks.js +1 -1
  25. package/dist/browser/runtime/generate.js +5 -8
  26. package/dist/browser/runtime/jwk_to_key.js +2 -45
  27. package/dist/browser/runtime/verify.js +1 -1
  28. package/dist/browser/util/decode_jwt.js +2 -2
  29. package/dist/browser/util/decode_protected_header.js +1 -1
  30. package/dist/browser/util/errors.js +1 -2
  31. package/dist/node/cjs/jwe/compact/encrypt.js +1 -0
  32. package/dist/node/cjs/jwe/flattened/decrypt.js +7 -16
  33. package/dist/node/cjs/jwe/flattened/encrypt.js +12 -19
  34. package/dist/node/cjs/jwe/general/encrypt.js +16 -13
  35. package/dist/node/cjs/jwk/embedded.js +2 -2
  36. package/dist/node/cjs/jwk/thumbprint.js +2 -2
  37. package/dist/node/cjs/jwks/local.js +3 -2
  38. package/dist/node/cjs/jwks/remote.js +13 -6
  39. package/dist/node/cjs/jws/compact/sign.js +1 -0
  40. package/dist/node/cjs/jws/flattened/sign.js +4 -1
  41. package/dist/node/cjs/jws/flattened/verify.js +3 -4
  42. package/dist/node/cjs/jws/general/sign.js +7 -1
  43. package/dist/node/cjs/jwt/encrypt.js +7 -0
  44. package/dist/node/cjs/jwt/produce.js +20 -4
  45. package/dist/node/cjs/jwt/sign.js +2 -2
  46. package/dist/node/cjs/jwt/verify.js +1 -2
  47. package/dist/node/cjs/key/import.js +2 -7
  48. package/dist/node/cjs/lib/aesgcmkw.js +1 -1
  49. package/dist/node/cjs/lib/decrypt_key_management.js +1 -1
  50. package/dist/node/cjs/lib/encrypt_key_management.js +1 -1
  51. package/dist/node/cjs/runtime/aeskw.js +6 -6
  52. package/dist/node/cjs/runtime/asn1.js +8 -8
  53. package/dist/node/cjs/runtime/base64url.js +6 -11
  54. package/dist/node/cjs/runtime/cbc_tag.js +2 -2
  55. package/dist/node/cjs/runtime/check_key_length.js +8 -0
  56. package/dist/node/cjs/runtime/ciphers.js +2 -2
  57. package/dist/node/cjs/runtime/decrypt.js +4 -4
  58. package/dist/node/cjs/runtime/digest.js +2 -2
  59. package/dist/node/cjs/runtime/ecdhes.js +7 -7
  60. package/dist/node/cjs/runtime/encrypt.js +4 -4
  61. package/dist/node/cjs/runtime/fetch_jwks.js +4 -4
  62. package/dist/node/cjs/runtime/generate.js +7 -11
  63. package/dist/node/cjs/runtime/get_named_curve.js +7 -45
  64. package/dist/node/cjs/runtime/get_sign_verify_key.js +4 -4
  65. package/dist/node/cjs/runtime/is_key_like.js +1 -1
  66. package/dist/node/cjs/runtime/is_key_object.js +2 -5
  67. package/dist/node/cjs/runtime/jwk_to_key.js +2 -113
  68. package/dist/node/cjs/runtime/key_to_jwk.js +6 -130
  69. package/dist/node/cjs/runtime/node_key.js +9 -18
  70. package/dist/node/cjs/runtime/pbes2kw.js +4 -4
  71. package/dist/node/cjs/runtime/random.js +2 -2
  72. package/dist/node/cjs/runtime/rsaes.js +8 -8
  73. package/dist/node/cjs/runtime/sign.js +3 -9
  74. package/dist/node/cjs/runtime/timing_safe_equal.js +2 -2
  75. package/dist/node/cjs/runtime/verify.js +3 -10
  76. package/dist/node/cjs/runtime/webcrypto.js +4 -6
  77. package/dist/node/cjs/util/errors.js +26 -59
  78. package/dist/node/esm/jwe/compact/encrypt.js +1 -0
  79. package/dist/node/esm/jwe/flattened/decrypt.js +7 -16
  80. package/dist/node/esm/jwe/flattened/encrypt.js +12 -19
  81. package/dist/node/esm/jwe/general/encrypt.js +17 -14
  82. package/dist/node/esm/jwk/embedded.js +2 -2
  83. package/dist/node/esm/jwk/thumbprint.js +2 -2
  84. package/dist/node/esm/jwks/local.js +3 -2
  85. package/dist/node/esm/jwks/remote.js +13 -6
  86. package/dist/node/esm/jws/compact/sign.js +1 -0
  87. package/dist/node/esm/jws/flattened/sign.js +4 -1
  88. package/dist/node/esm/jws/flattened/verify.js +3 -4
  89. package/dist/node/esm/jws/general/sign.js +7 -1
  90. package/dist/node/esm/jwt/encrypt.js +7 -0
  91. package/dist/node/esm/jwt/produce.js +20 -4
  92. package/dist/node/esm/jwt/sign.js +2 -2
  93. package/dist/node/esm/jwt/verify.js +1 -2
  94. package/dist/node/esm/key/import.js +2 -7
  95. package/dist/node/esm/lib/aesgcmkw.js +1 -1
  96. package/dist/node/esm/lib/decrypt_key_management.js +1 -1
  97. package/dist/node/esm/lib/encrypt_key_management.js +1 -1
  98. package/dist/node/esm/runtime/aeskw.js +2 -2
  99. package/dist/node/esm/runtime/asn1.js +2 -2
  100. package/dist/node/esm/runtime/base64url.js +4 -10
  101. package/dist/node/esm/runtime/cbc_tag.js +1 -1
  102. package/dist/node/esm/runtime/check_key_length.js +6 -0
  103. package/dist/node/esm/runtime/ciphers.js +2 -2
  104. package/dist/node/esm/runtime/decrypt.js +1 -1
  105. package/dist/node/esm/runtime/digest.js +1 -1
  106. package/dist/node/esm/runtime/ecdhes.js +2 -2
  107. package/dist/node/esm/runtime/encrypt.js +1 -1
  108. package/dist/node/esm/runtime/fetch_jwks.js +3 -3
  109. package/dist/node/esm/runtime/generate.js +5 -9
  110. package/dist/node/esm/runtime/get_named_curve.js +5 -42
  111. package/dist/node/esm/runtime/get_sign_verify_key.js +1 -1
  112. package/dist/node/esm/runtime/is_key_like.js +1 -1
  113. package/dist/node/esm/runtime/is_key_object.js +2 -5
  114. package/dist/node/esm/runtime/jwk_to_key.js +2 -113
  115. package/dist/node/esm/runtime/key_to_jwk.js +5 -129
  116. package/dist/node/esm/runtime/node_key.js +7 -16
  117. package/dist/node/esm/runtime/pbes2kw.js +2 -2
  118. package/dist/node/esm/runtime/random.js +1 -1
  119. package/dist/node/esm/runtime/rsaes.js +3 -3
  120. package/dist/node/esm/runtime/sign.js +3 -9
  121. package/dist/node/esm/runtime/timing_safe_equal.js +1 -1
  122. package/dist/node/esm/runtime/verify.js +3 -10
  123. package/dist/node/esm/runtime/webcrypto.js +3 -6
  124. package/dist/node/esm/util/errors.js +26 -59
  125. package/dist/types/index.d.ts +1 -1
  126. package/dist/types/jwe/compact/decrypt.d.ts +1 -1
  127. package/dist/types/jwe/flattened/decrypt.d.ts +1 -1
  128. package/dist/types/jwe/general/decrypt.d.ts +1 -1
  129. package/dist/types/jwe/general/encrypt.d.ts +3 -7
  130. package/dist/types/jwk/embedded.d.ts +1 -1
  131. package/dist/types/jwks/local.d.ts +3 -3
  132. package/dist/types/jwks/remote.d.ts +1 -1
  133. package/dist/types/jws/compact/verify.d.ts +1 -1
  134. package/dist/types/jws/flattened/verify.d.ts +1 -1
  135. package/dist/types/jws/general/verify.d.ts +1 -1
  136. package/dist/types/jwt/decrypt.d.ts +3 -3
  137. package/dist/types/jwt/produce.d.ts +12 -12
  138. package/dist/types/jwt/unsecured.d.ts +3 -3
  139. package/dist/types/jwt/verify.d.ts +3 -3
  140. package/dist/types/key/generate_key_pair.d.ts +7 -5
  141. package/dist/types/key/generate_secret.d.ts +1 -1
  142. package/dist/types/key/import.d.ts +4 -6
  143. package/dist/types/types.d.ts +13 -50
  144. package/package.json +1 -1
  145. package/dist/browser/runtime/zlib.js +0 -7
  146. package/dist/node/cjs/runtime/asn1_sequence_decoder.js +0 -47
  147. package/dist/node/cjs/runtime/asn1_sequence_encoder.js +0 -91
  148. package/dist/node/cjs/runtime/check_modulus_length.js +0 -52
  149. package/dist/node/cjs/runtime/flags.js +0 -8
  150. package/dist/node/cjs/runtime/zlib.js +0 -11
  151. package/dist/node/esm/runtime/asn1_sequence_decoder.js +0 -44
  152. package/dist/node/esm/runtime/asn1_sequence_encoder.js +0 -88
  153. package/dist/node/esm/runtime/check_modulus_length.js +0 -48
  154. package/dist/node/esm/runtime/flags.js +0 -5
  155. package/dist/node/esm/runtime/zlib.js +0 -6
@@ -2,7 +2,6 @@ import crypto from './webcrypto.js';
2
2
  import { JOSENotSupported } from '../util/errors.js';
3
3
  import random from './random.js';
4
4
  export async function generateSecret(alg, options) {
5
- var _a;
6
5
  let length;
7
6
  let algorithm;
8
7
  let keyUsages;
@@ -39,18 +38,16 @@ export async function generateSecret(alg, options) {
39
38
  default:
40
39
  throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
41
40
  }
42
- return crypto.subtle.generateKey(algorithm, (_a = options === null || options === void 0 ? void 0 : options.extractable) !== null && _a !== void 0 ? _a : false, keyUsages);
41
+ return crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages);
43
42
  }
44
43
  function getModulusLengthOption(options) {
45
- var _a;
46
- const modulusLength = (_a = options === null || options === void 0 ? void 0 : options.modulusLength) !== null && _a !== void 0 ? _a : 2048;
44
+ const modulusLength = options?.modulusLength ?? 2048;
47
45
  if (typeof modulusLength !== 'number' || modulusLength < 2048) {
48
46
  throw new JOSENotSupported('Invalid or unsupported modulusLength option provided, 2048 bits or larger keys must be used');
49
47
  }
50
48
  return modulusLength;
51
49
  }
52
50
  export async function generateKeyPair(alg, options) {
53
- var _a, _b, _c;
54
51
  let algorithm;
55
52
  let keyUsages;
56
53
  switch (alg) {
@@ -102,7 +99,7 @@ export async function generateKeyPair(alg, options) {
102
99
  break;
103
100
  case 'EdDSA':
104
101
  keyUsages = ['sign', 'verify'];
105
- const crv = (_a = options === null || options === void 0 ? void 0 : options.crv) !== null && _a !== void 0 ? _a : 'Ed25519';
102
+ const crv = options?.crv ?? 'Ed25519';
106
103
  switch (crv) {
107
104
  case 'Ed25519':
108
105
  case 'Ed448':
@@ -117,7 +114,7 @@ export async function generateKeyPair(alg, options) {
117
114
  case 'ECDH-ES+A192KW':
118
115
  case 'ECDH-ES+A256KW': {
119
116
  keyUsages = ['deriveKey', 'deriveBits'];
120
- const crv = (_b = options === null || options === void 0 ? void 0 : options.crv) !== null && _b !== void 0 ? _b : 'P-256';
117
+ const crv = options?.crv ?? 'P-256';
121
118
  switch (crv) {
122
119
  case 'P-256':
123
120
  case 'P-384':
@@ -137,5 +134,5 @@ export async function generateKeyPair(alg, options) {
137
134
  default:
138
135
  throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
139
136
  }
140
- return (crypto.subtle.generateKey(algorithm, (_c = options === null || options === void 0 ? void 0 : options.extractable) !== null && _c !== void 0 ? _c : false, keyUsages));
137
+ return (crypto.subtle.generateKey(algorithm, options?.extractable ?? false, keyUsages));
141
138
  }
@@ -1,48 +1,9 @@
1
1
  import crypto from './webcrypto.js';
2
2
  import { JOSENotSupported } from '../util/errors.js';
3
- import { decode as base64url } from './base64url.js';
4
3
  function subtleMapping(jwk) {
5
4
  let algorithm;
6
5
  let keyUsages;
7
6
  switch (jwk.kty) {
8
- case 'oct': {
9
- switch (jwk.alg) {
10
- case 'HS256':
11
- case 'HS384':
12
- case 'HS512':
13
- algorithm = { name: 'HMAC', hash: `SHA-${jwk.alg.slice(-3)}` };
14
- keyUsages = ['sign', 'verify'];
15
- break;
16
- case 'A128CBC-HS256':
17
- case 'A192CBC-HS384':
18
- case 'A256CBC-HS512':
19
- throw new JOSENotSupported(`${jwk.alg} keys cannot be imported as CryptoKey instances`);
20
- case 'A128GCM':
21
- case 'A192GCM':
22
- case 'A256GCM':
23
- case 'A128GCMKW':
24
- case 'A192GCMKW':
25
- case 'A256GCMKW':
26
- algorithm = { name: 'AES-GCM' };
27
- keyUsages = ['encrypt', 'decrypt'];
28
- break;
29
- case 'A128KW':
30
- case 'A192KW':
31
- case 'A256KW':
32
- algorithm = { name: 'AES-KW' };
33
- keyUsages = ['wrapKey', 'unwrapKey'];
34
- break;
35
- case 'PBES2-HS256+A128KW':
36
- case 'PBES2-HS384+A192KW':
37
- case 'PBES2-HS512+A256KW':
38
- algorithm = { name: 'PBKDF2' };
39
- keyUsages = ['deriveBits'];
40
- break;
41
- default:
42
- throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
43
- }
44
- break;
45
- }
46
7
  case 'RSA': {
47
8
  switch (jwk.alg) {
48
9
  case 'PS256':
@@ -122,19 +83,15 @@ function subtleMapping(jwk) {
122
83
  return { algorithm, keyUsages };
123
84
  }
124
85
  const parse = async (jwk) => {
125
- var _a, _b;
126
86
  if (!jwk.alg) {
127
87
  throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
128
88
  }
129
89
  const { algorithm, keyUsages } = subtleMapping(jwk);
130
90
  const rest = [
131
91
  algorithm,
132
- (_a = jwk.ext) !== null && _a !== void 0 ? _a : false,
133
- (_b = jwk.key_ops) !== null && _b !== void 0 ? _b : keyUsages,
92
+ jwk.ext ?? false,
93
+ jwk.key_ops ?? keyUsages,
134
94
  ];
135
- if (algorithm.name === 'PBKDF2') {
136
- return crypto.subtle.importKey('raw', base64url(jwk.k), ...rest);
137
- }
138
95
  const keyData = { ...jwk };
139
96
  delete keyData.alg;
140
97
  delete keyData.use;
@@ -9,7 +9,7 @@ const verify = async (alg, key, signature, data) => {
9
9
  try {
10
10
  return await crypto.subtle.verify(algorithm, cryptoKey, signature, data);
11
11
  }
12
- catch (_a) {
12
+ catch {
13
13
  return false;
14
14
  }
15
15
  };
@@ -16,14 +16,14 @@ export function decodeJwt(jwt) {
16
16
  try {
17
17
  decoded = base64url(payload);
18
18
  }
19
- catch (_a) {
19
+ catch {
20
20
  throw new JWTInvalid('Failed to base64url decode the payload');
21
21
  }
22
22
  let result;
23
23
  try {
24
24
  result = JSON.parse(decoder.decode(decoded));
25
25
  }
26
- catch (_b) {
26
+ catch {
27
27
  throw new JWTInvalid('Failed to parse the decoded payload as JSON');
28
28
  }
29
29
  if (!isObject(result))
@@ -28,7 +28,7 @@ export function decodeProtectedHeader(token) {
28
28
  }
29
29
  return result;
30
30
  }
31
- catch (_a) {
31
+ catch {
32
32
  throw new TypeError('Invalid Token or Protected Header formatting');
33
33
  }
34
34
  }
@@ -3,11 +3,10 @@ export class JOSEError extends Error {
3
3
  return 'ERR_JOSE_GENERIC';
4
4
  }
5
5
  constructor(message) {
6
- var _a;
7
6
  super(message);
8
7
  this.code = 'ERR_JOSE_GENERIC';
9
8
  this.name = this.constructor.name;
10
- (_a = Error.captureStackTrace) === null || _a === void 0 ? void 0 : _a.call(Error, this, this.constructor);
9
+ Error.captureStackTrace?.(this, this.constructor);
11
10
  }
12
11
  }
13
12
  export class JWTClaimValidationFailed extends JOSEError {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CompactEncrypt = void 0;
4
4
  const encrypt_js_1 = require("../flattened/encrypt.js");
5
5
  class CompactEncrypt {
6
+ _flattened;
6
7
  constructor(plaintext) {
7
8
  this._flattened = new encrypt_js_1.FlattenedEncrypt(plaintext);
8
9
  }
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.flattenedDecrypt = void 0;
4
4
  const base64url_js_1 = require("../../runtime/base64url.js");
5
5
  const decrypt_js_1 = require("../../runtime/decrypt.js");
6
- const zlib_js_1 = require("../../runtime/zlib.js");
7
6
  const errors_js_1 = require("../../util/errors.js");
8
7
  const is_disjoint_js_1 = require("../../lib/is_disjoint.js");
9
8
  const is_object_js_1 = require("../../lib/is_object.js");
@@ -13,7 +12,6 @@ const cek_js_1 = require("../../lib/cek.js");
13
12
  const validate_crit_js_1 = require("../../lib/validate_crit.js");
14
13
  const validate_algorithms_js_1 = require("../../lib/validate_algorithms.js");
15
14
  async function flattenedDecrypt(jwe, key, options) {
16
- var _a;
17
15
  if (!(0, is_object_js_1.default)(jwe)) {
18
16
  throw new errors_js_1.JWEInvalid('Flattened JWE must be an object');
19
17
  }
@@ -62,14 +60,9 @@ async function flattenedDecrypt(jwe, key, options) {
62
60
  ...jwe.header,
63
61
  ...jwe.unprotected,
64
62
  };
65
- (0, validate_crit_js_1.default)(errors_js_1.JWEInvalid, new Map(), options === null || options === void 0 ? void 0 : options.crit, parsedProt, joseHeader);
63
+ (0, validate_crit_js_1.default)(errors_js_1.JWEInvalid, new Map(), options?.crit, parsedProt, joseHeader);
66
64
  if (joseHeader.zip !== undefined) {
67
- if (!parsedProt || !parsedProt.zip) {
68
- throw new errors_js_1.JWEInvalid('JWE "zip" (Compression Algorithm) Header MUST be integrity protected');
69
- }
70
- if (joseHeader.zip !== 'DEF') {
71
- throw new errors_js_1.JOSENotSupported('Unsupported JWE "zip" (Compression Algorithm) Header Parameter value');
72
- }
65
+ throw new errors_js_1.JOSENotSupported('JWE "zip" (Compression Algorithm) Header Parameter is not supported.');
73
66
  }
74
67
  const { alg, enc } = joseHeader;
75
68
  if (typeof alg !== 'string' || !alg) {
@@ -81,11 +74,12 @@ async function flattenedDecrypt(jwe, key, options) {
81
74
  const keyManagementAlgorithms = options && (0, validate_algorithms_js_1.default)('keyManagementAlgorithms', options.keyManagementAlgorithms);
82
75
  const contentEncryptionAlgorithms = options &&
83
76
  (0, validate_algorithms_js_1.default)('contentEncryptionAlgorithms', options.contentEncryptionAlgorithms);
84
- if (keyManagementAlgorithms && !keyManagementAlgorithms.has(alg)) {
85
- throw new errors_js_1.JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed');
77
+ if ((keyManagementAlgorithms && !keyManagementAlgorithms.has(alg)) ||
78
+ (!keyManagementAlgorithms && alg.startsWith('PBES2'))) {
79
+ throw new errors_js_1.JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter value not allowed');
86
80
  }
87
81
  if (contentEncryptionAlgorithms && !contentEncryptionAlgorithms.has(enc)) {
88
- throw new errors_js_1.JOSEAlgNotAllowed('"enc" (Encryption Algorithm) Header Parameter not allowed');
82
+ throw new errors_js_1.JOSEAlgNotAllowed('"enc" (Encryption Algorithm) Header Parameter value not allowed');
89
83
  }
90
84
  let encryptedKey;
91
85
  if (jwe.encrypted_key !== undefined) {
@@ -125,7 +119,7 @@ async function flattenedDecrypt(jwe, key, options) {
125
119
  catch {
126
120
  throw new errors_js_1.JWEInvalid('Failed to base64url decode the tag');
127
121
  }
128
- const protectedHeader = buffer_utils_js_1.encoder.encode((_a = jwe.protected) !== null && _a !== void 0 ? _a : '');
122
+ const protectedHeader = buffer_utils_js_1.encoder.encode(jwe.protected ?? '');
129
123
  let additionalData;
130
124
  if (jwe.aad !== undefined) {
131
125
  additionalData = (0, buffer_utils_js_1.concat)(protectedHeader, buffer_utils_js_1.encoder.encode('.'), buffer_utils_js_1.encoder.encode(jwe.aad));
@@ -141,9 +135,6 @@ async function flattenedDecrypt(jwe, key, options) {
141
135
  throw new errors_js_1.JWEInvalid('Failed to base64url decode the ciphertext');
142
136
  }
143
137
  let plaintext = await (0, decrypt_js_1.default)(enc, cek, ciphertext, iv, tag, additionalData);
144
- if (joseHeader.zip === 'DEF') {
145
- plaintext = await ((options === null || options === void 0 ? void 0 : options.inflateRaw) || zlib_js_1.inflate)(plaintext);
146
- }
147
138
  const result = { plaintext };
148
139
  if (jwe.protected !== undefined) {
149
140
  result.protectedHeader = parsedProt;
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FlattenedEncrypt = exports.unprotected = void 0;
4
4
  const base64url_js_1 = require("../../runtime/base64url.js");
5
5
  const encrypt_js_1 = require("../../runtime/encrypt.js");
6
- const zlib_js_1 = require("../../runtime/zlib.js");
7
6
  const iv_js_1 = require("../../lib/iv.js");
8
7
  const encrypt_key_management_js_1 = require("../../lib/encrypt_key_management.js");
9
8
  const errors_js_1 = require("../../util/errors.js");
@@ -12,6 +11,14 @@ const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
12
11
  const validate_crit_js_1 = require("../../lib/validate_crit.js");
13
12
  exports.unprotected = Symbol();
14
13
  class FlattenedEncrypt {
14
+ _plaintext;
15
+ _protectedHeader;
16
+ _sharedUnprotectedHeader;
17
+ _unprotectedHeader;
18
+ _aad;
19
+ _cek;
20
+ _iv;
21
+ _keyManagementParameters;
15
22
  constructor(plaintext) {
16
23
  if (!(plaintext instanceof Uint8Array)) {
17
24
  throw new TypeError('plaintext must be an instance of Uint8Array');
@@ -76,14 +83,9 @@ class FlattenedEncrypt {
76
83
  ...this._unprotectedHeader,
77
84
  ...this._sharedUnprotectedHeader,
78
85
  };
79
- (0, validate_crit_js_1.default)(errors_js_1.JWEInvalid, new Map(), options === null || options === void 0 ? void 0 : options.crit, this._protectedHeader, joseHeader);
86
+ (0, validate_crit_js_1.default)(errors_js_1.JWEInvalid, new Map(), options?.crit, this._protectedHeader, joseHeader);
80
87
  if (joseHeader.zip !== undefined) {
81
- if (!this._protectedHeader || !this._protectedHeader.zip) {
82
- throw new errors_js_1.JWEInvalid('JWE "zip" (Compression Algorithm) Header MUST be integrity protected');
83
- }
84
- if (joseHeader.zip !== 'DEF') {
85
- throw new errors_js_1.JOSENotSupported('Unsupported JWE "zip" (Compression Algorithm) Header Parameter value');
86
- }
88
+ throw new errors_js_1.JOSENotSupported('JWE "zip" (Compression Algorithm) Header Parameter is not supported.');
87
89
  }
88
90
  const { alg, enc } = joseHeader;
89
91
  if (typeof alg !== 'string' || !alg) {
@@ -126,7 +128,7 @@ class FlattenedEncrypt {
126
128
  }
127
129
  }
128
130
  }
129
- this._iv || (this._iv = (0, iv_js_1.default)(enc));
131
+ this._iv ||= (0, iv_js_1.default)(enc);
130
132
  let additionalData;
131
133
  let protectedHeader;
132
134
  let aadMember;
@@ -143,16 +145,7 @@ class FlattenedEncrypt {
143
145
  else {
144
146
  additionalData = protectedHeader;
145
147
  }
146
- let ciphertext;
147
- let tag;
148
- if (joseHeader.zip === 'DEF') {
149
- const deflated = await ((options === null || options === void 0 ? void 0 : options.deflateRaw) || zlib_js_1.deflate)(this._plaintext);
150
- ({ ciphertext, tag } = await (0, encrypt_js_1.default)(enc, deflated, cek, this._iv, additionalData));
151
- }
152
- else {
153
- ;
154
- ({ ciphertext, tag } = await (0, encrypt_js_1.default)(enc, this._plaintext, cek, this._iv, additionalData));
155
- }
148
+ const { ciphertext, tag } = await (0, encrypt_js_1.default)(enc, this._plaintext, cek, this._iv, additionalData);
156
149
  const jwe = {
157
150
  ciphertext: (0, base64url_js_1.encode)(ciphertext),
158
151
  iv: (0, base64url_js_1.encode)(this._iv),
@@ -9,6 +9,10 @@ const encrypt_key_management_js_1 = require("../../lib/encrypt_key_management.js
9
9
  const base64url_js_1 = require("../../runtime/base64url.js");
10
10
  const validate_crit_js_1 = require("../../lib/validate_crit.js");
11
11
  class IndividualRecipient {
12
+ parent;
13
+ unprotectedHeader;
14
+ key;
15
+ options;
12
16
  constructor(enc, key, options) {
13
17
  this.parent = enc;
14
18
  this.key = key;
@@ -32,12 +36,16 @@ class IndividualRecipient {
32
36
  }
33
37
  }
34
38
  class GeneralEncrypt {
39
+ _plaintext;
40
+ _recipients = [];
41
+ _protectedHeader;
42
+ _unprotectedHeader;
43
+ _aad;
35
44
  constructor(plaintext) {
36
- this._recipients = [];
37
45
  this._plaintext = plaintext;
38
46
  }
39
47
  addRecipient(key, options) {
40
- const recipient = new IndividualRecipient(this, key, { crit: options === null || options === void 0 ? void 0 : options.crit });
48
+ const recipient = new IndividualRecipient(this, key, { crit: options?.crit });
41
49
  this._recipients.push(recipient);
42
50
  return recipient;
43
51
  }
@@ -59,12 +67,10 @@ class GeneralEncrypt {
59
67
  this._aad = aad;
60
68
  return this;
61
69
  }
62
- async encrypt(options) {
63
- var _a, _b, _c;
70
+ async encrypt() {
64
71
  if (!this._recipients.length) {
65
72
  throw new errors_js_1.JWEInvalid('at least one recipient must be added');
66
73
  }
67
- options = { deflateRaw: options === null || options === void 0 ? void 0 : options.deflateRaw };
68
74
  if (this._recipients.length === 1) {
69
75
  const [recipient] = this._recipients;
70
76
  const flattened = await new encrypt_js_1.FlattenedEncrypt(this._plaintext)
@@ -72,7 +78,7 @@ class GeneralEncrypt {
72
78
  .setProtectedHeader(this._protectedHeader)
73
79
  .setSharedUnprotectedHeader(this._unprotectedHeader)
74
80
  .setUnprotectedHeader(recipient.unprotectedHeader)
75
- .encrypt(recipient.key, { ...recipient.options, ...options });
81
+ .encrypt(recipient.key, { ...recipient.options });
76
82
  let jwe = {
77
83
  ciphertext: flattened.ciphertext,
78
84
  iv: flattened.iv,
@@ -120,9 +126,7 @@ class GeneralEncrypt {
120
126
  }
121
127
  (0, validate_crit_js_1.default)(errors_js_1.JWEInvalid, new Map(), recipient.options.crit, this._protectedHeader, joseHeader);
122
128
  if (joseHeader.zip !== undefined) {
123
- if (!this._protectedHeader || !this._protectedHeader.zip) {
124
- throw new errors_js_1.JWEInvalid('JWE "zip" (Compression Algorithm) Header MUST be integrity protected');
125
- }
129
+ throw new errors_js_1.JOSENotSupported('JWE "zip" (Compression Algorithm) Header Parameter is not supported.');
126
130
  }
127
131
  }
128
132
  const cek = (0, cek_js_1.default)(enc);
@@ -152,7 +156,6 @@ class GeneralEncrypt {
152
156
  .setKeyManagementParameters({ p2c })
153
157
  .encrypt(recipient.key, {
154
158
  ...recipient.options,
155
- ...options,
156
159
  [encrypt_js_1.unprotected]: true,
157
160
  });
158
161
  jwe.ciphertext = flattened.ciphertext;
@@ -169,9 +172,9 @@ class GeneralEncrypt {
169
172
  target.header = flattened.header;
170
173
  continue;
171
174
  }
172
- const { encryptedKey, parameters } = await (0, encrypt_key_management_js_1.default)(((_a = recipient.unprotectedHeader) === null || _a === void 0 ? void 0 : _a.alg) ||
173
- ((_b = this._protectedHeader) === null || _b === void 0 ? void 0 : _b.alg) ||
174
- ((_c = this._unprotectedHeader) === null || _c === void 0 ? void 0 : _c.alg), enc, recipient.key, cek, { p2c });
175
+ const { encryptedKey, parameters } = await (0, encrypt_key_management_js_1.default)(recipient.unprotectedHeader?.alg ||
176
+ this._protectedHeader?.alg ||
177
+ this._unprotectedHeader?.alg, enc, recipient.key, cek, { p2c });
175
178
  target.encrypted_key = (0, base64url_js_1.encode)(encryptedKey);
176
179
  if (recipient.unprotectedHeader || parameters)
177
180
  target.header = { ...recipient.unprotectedHeader, ...parameters };
@@ -7,12 +7,12 @@ const errors_js_1 = require("../util/errors.js");
7
7
  async function EmbeddedJWK(protectedHeader, token) {
8
8
  const joseHeader = {
9
9
  ...protectedHeader,
10
- ...token === null || token === void 0 ? void 0 : token.header,
10
+ ...token?.header,
11
11
  };
12
12
  if (!(0, is_object_js_1.default)(joseHeader.jwk)) {
13
13
  throw new errors_js_1.JWSInvalid('"jwk" (JSON Web Key) Header Parameter must be a JSON object');
14
14
  }
15
- const key = await (0, import_js_1.importJWK)({ ...joseHeader.jwk, ext: true }, joseHeader.alg, true);
15
+ const key = await (0, import_js_1.importJWK)({ ...joseHeader.jwk, ext: true }, joseHeader.alg);
16
16
  if (key instanceof Uint8Array || key.type !== 'public') {
17
17
  throw new errors_js_1.JWSInvalid('"jwk" (JSON Web Key) Header Parameter must be a public key');
18
18
  }
@@ -15,7 +15,7 @@ async function calculateJwkThumbprint(jwk, digestAlgorithm) {
15
15
  if (!(0, is_object_js_1.default)(jwk)) {
16
16
  throw new TypeError('JWK must be an object');
17
17
  }
18
- digestAlgorithm !== null && digestAlgorithm !== void 0 ? digestAlgorithm : (digestAlgorithm = 'sha256');
18
+ digestAlgorithm ??= 'sha256';
19
19
  if (digestAlgorithm !== 'sha256' &&
20
20
  digestAlgorithm !== 'sha384' &&
21
21
  digestAlgorithm !== 'sha512') {
@@ -51,7 +51,7 @@ async function calculateJwkThumbprint(jwk, digestAlgorithm) {
51
51
  }
52
52
  exports.calculateJwkThumbprint = calculateJwkThumbprint;
53
53
  async function calculateJwkThumbprintUri(jwk, digestAlgorithm) {
54
- digestAlgorithm !== null && digestAlgorithm !== void 0 ? digestAlgorithm : (digestAlgorithm = 'sha256');
54
+ digestAlgorithm ??= 'sha256';
55
55
  const thumbprint = await calculateJwkThumbprint(jwk, digestAlgorithm);
56
56
  return `urn:ietf:params:oauth:jwk-thumbprint:sha-${digestAlgorithm.slice(-3)}:${thumbprint}`;
57
57
  }
@@ -34,15 +34,16 @@ function clone(obj) {
34
34
  return JSON.parse(JSON.stringify(obj));
35
35
  }
36
36
  class LocalJWKSet {
37
+ _jwks;
38
+ _cached = new WeakMap();
37
39
  constructor(jwks) {
38
- this._cached = new WeakMap();
39
40
  if (!isJWKSLike(jwks)) {
40
41
  throw new errors_js_1.JWKSInvalid('JSON Web Key Set malformed');
41
42
  }
42
43
  this._jwks = clone(jwks);
43
44
  }
44
45
  async getKey(protectedHeader, token) {
45
- const { alg, kid } = { ...protectedHeader, ...token === null || token === void 0 ? void 0 : token.header };
46
+ const { alg, kid } = { ...protectedHeader, ...token?.header };
46
47
  const kty = getKtyFromAlg(alg);
47
48
  const candidates = this._jwks.keys.filter((jwk) => {
48
49
  let candidate = kty === jwk.kty;
@@ -10,6 +10,13 @@ function isCloudflareWorkers() {
10
10
  (typeof EdgeRuntime !== 'undefined' && EdgeRuntime === 'vercel'));
11
11
  }
12
12
  class RemoteJWKSet extends local_js_1.LocalJWKSet {
13
+ _url;
14
+ _timeoutDuration;
15
+ _cooldownDuration;
16
+ _cacheMaxAge;
17
+ _jwksTimestamp;
18
+ _pendingFetch;
19
+ _options;
13
20
  constructor(url, options) {
14
21
  super({ keys: [] });
15
22
  this._jwks = undefined;
@@ -17,12 +24,12 @@ class RemoteJWKSet extends local_js_1.LocalJWKSet {
17
24
  throw new TypeError('url must be an instance of URL');
18
25
  }
19
26
  this._url = new URL(url.href);
20
- this._options = { agent: options === null || options === void 0 ? void 0 : options.agent, headers: options === null || options === void 0 ? void 0 : options.headers };
27
+ this._options = { agent: options?.agent, headers: options?.headers };
21
28
  this._timeoutDuration =
22
- typeof (options === null || options === void 0 ? void 0 : options.timeoutDuration) === 'number' ? options === null || options === void 0 ? void 0 : options.timeoutDuration : 5000;
29
+ typeof options?.timeoutDuration === 'number' ? options?.timeoutDuration : 5000;
23
30
  this._cooldownDuration =
24
- typeof (options === null || options === void 0 ? void 0 : options.cooldownDuration) === 'number' ? options === null || options === void 0 ? void 0 : options.cooldownDuration : 30000;
25
- this._cacheMaxAge = typeof (options === null || options === void 0 ? void 0 : options.cacheMaxAge) === 'number' ? options === null || options === void 0 ? void 0 : options.cacheMaxAge : 600000;
31
+ typeof options?.cooldownDuration === 'number' ? options?.cooldownDuration : 30000;
32
+ this._cacheMaxAge = typeof options?.cacheMaxAge === 'number' ? options?.cacheMaxAge : 600000;
26
33
  }
27
34
  coolingDown() {
28
35
  return typeof this._jwksTimestamp === 'number'
@@ -55,7 +62,7 @@ class RemoteJWKSet extends local_js_1.LocalJWKSet {
55
62
  if (this._pendingFetch && isCloudflareWorkers()) {
56
63
  this._pendingFetch = undefined;
57
64
  }
58
- this._pendingFetch || (this._pendingFetch = (0, fetch_jwks_js_1.default)(this._url, this._timeoutDuration, this._options)
65
+ this._pendingFetch ||= (0, fetch_jwks_js_1.default)(this._url, this._timeoutDuration, this._options)
59
66
  .then((json) => {
60
67
  if (!(0, local_js_1.isJWKSLike)(json)) {
61
68
  throw new errors_js_1.JWKSInvalid('JSON Web Key Set malformed');
@@ -67,7 +74,7 @@ class RemoteJWKSet extends local_js_1.LocalJWKSet {
67
74
  .catch((err) => {
68
75
  this._pendingFetch = undefined;
69
76
  throw err;
70
- }));
77
+ });
71
78
  await this._pendingFetch;
72
79
  }
73
80
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CompactSign = void 0;
4
4
  const sign_js_1 = require("../flattened/sign.js");
5
5
  class CompactSign {
6
+ _flattened;
6
7
  constructor(payload) {
7
8
  this._flattened = new sign_js_1.FlattenedSign(payload);
8
9
  }
@@ -9,6 +9,9 @@ const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
9
9
  const check_key_type_js_1 = require("../../lib/check_key_type.js");
10
10
  const validate_crit_js_1 = require("../../lib/validate_crit.js");
11
11
  class FlattenedSign {
12
+ _payload;
13
+ _protectedHeader;
14
+ _unprotectedHeader;
12
15
  constructor(payload) {
13
16
  if (!(payload instanceof Uint8Array)) {
14
17
  throw new TypeError('payload must be an instance of Uint8Array');
@@ -40,7 +43,7 @@ class FlattenedSign {
40
43
  ...this._protectedHeader,
41
44
  ...this._unprotectedHeader,
42
45
  };
43
- const extensions = (0, validate_crit_js_1.default)(errors_js_1.JWSInvalid, new Map([['b64', true]]), options === null || options === void 0 ? void 0 : options.crit, this._protectedHeader, joseHeader);
46
+ const extensions = (0, validate_crit_js_1.default)(errors_js_1.JWSInvalid, new Map([['b64', true]]), options?.crit, this._protectedHeader, joseHeader);
44
47
  let b64 = true;
45
48
  if (extensions.has('b64')) {
46
49
  b64 = this._protectedHeader.b64;
@@ -11,7 +11,6 @@ const check_key_type_js_1 = require("../../lib/check_key_type.js");
11
11
  const validate_crit_js_1 = require("../../lib/validate_crit.js");
12
12
  const validate_algorithms_js_1 = require("../../lib/validate_algorithms.js");
13
13
  async function flattenedVerify(jws, key, options) {
14
- var _a;
15
14
  if (!(0, is_object_js_1.default)(jws)) {
16
15
  throw new errors_js_1.JWSInvalid('Flattened JWS must be an object');
17
16
  }
@@ -47,7 +46,7 @@ async function flattenedVerify(jws, key, options) {
47
46
  ...parsedProt,
48
47
  ...jws.header,
49
48
  };
50
- const extensions = (0, validate_crit_js_1.default)(errors_js_1.JWSInvalid, new Map([['b64', true]]), options === null || options === void 0 ? void 0 : options.crit, parsedProt, joseHeader);
49
+ const extensions = (0, validate_crit_js_1.default)(errors_js_1.JWSInvalid, new Map([['b64', true]]), options?.crit, parsedProt, joseHeader);
51
50
  let b64 = true;
52
51
  if (extensions.has('b64')) {
53
52
  b64 = parsedProt.b64;
@@ -61,7 +60,7 @@ async function flattenedVerify(jws, key, options) {
61
60
  }
62
61
  const algorithms = options && (0, validate_algorithms_js_1.default)('algorithms', options.algorithms);
63
62
  if (algorithms && !algorithms.has(alg)) {
64
- throw new errors_js_1.JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed');
63
+ throw new errors_js_1.JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter value not allowed');
65
64
  }
66
65
  if (b64) {
67
66
  if (typeof jws.payload !== 'string') {
@@ -77,7 +76,7 @@ async function flattenedVerify(jws, key, options) {
77
76
  resolvedKey = true;
78
77
  }
79
78
  (0, check_key_type_js_1.default)(alg, key, 'verify');
80
- const data = (0, buffer_utils_js_1.concat)(buffer_utils_js_1.encoder.encode((_a = jws.protected) !== null && _a !== void 0 ? _a : ''), buffer_utils_js_1.encoder.encode('.'), typeof jws.payload === 'string' ? buffer_utils_js_1.encoder.encode(jws.payload) : jws.payload);
79
+ const data = (0, buffer_utils_js_1.concat)(buffer_utils_js_1.encoder.encode(jws.protected ?? ''), buffer_utils_js_1.encoder.encode('.'), typeof jws.payload === 'string' ? buffer_utils_js_1.encoder.encode(jws.payload) : jws.payload);
81
80
  let signature;
82
81
  try {
83
82
  signature = (0, base64url_js_1.decode)(jws.signature);
@@ -4,6 +4,11 @@ exports.GeneralSign = void 0;
4
4
  const sign_js_1 = require("../flattened/sign.js");
5
5
  const errors_js_1 = require("../../util/errors.js");
6
6
  class IndividualSignature {
7
+ parent;
8
+ protectedHeader;
9
+ unprotectedHeader;
10
+ options;
11
+ key;
7
12
  constructor(sig, key, options) {
8
13
  this.parent = sig;
9
14
  this.key = key;
@@ -34,8 +39,9 @@ class IndividualSignature {
34
39
  }
35
40
  }
36
41
  class GeneralSign {
42
+ _payload;
43
+ _signatures = [];
37
44
  constructor(payload) {
38
- this._signatures = [];
39
45
  this._payload = payload;
40
46
  }
41
47
  addSignature(key, options) {
@@ -5,6 +5,13 @@ const encrypt_js_1 = require("../jwe/compact/encrypt.js");
5
5
  const buffer_utils_js_1 = require("../lib/buffer_utils.js");
6
6
  const produce_js_1 = require("./produce.js");
7
7
  class EncryptJWT extends produce_js_1.ProduceJWT {
8
+ _cek;
9
+ _iv;
10
+ _keyManagementParameters;
11
+ _protectedHeader;
12
+ _replicateIssuerAsHeader;
13
+ _replicateSubjectAsHeader;
14
+ _replicateAudienceAsHeader;
8
15
  setProtectedHeader(protectedHeader) {
9
16
  if (this._protectedHeader) {
10
17
  throw new TypeError('setProtectedHeader can only be called once');