jose 3.19.0 → 3.20.3

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 (87) hide show
  1. package/dist/browser/jwe/flattened/decrypt.js +5 -9
  2. package/dist/browser/jwe/flattened/encrypt.js +2 -5
  3. package/dist/browser/jwk/embedded.js +2 -2
  4. package/dist/browser/jwks/remote.js +7 -10
  5. package/dist/browser/jws/flattened/sign.js +1 -2
  6. package/dist/browser/jws/flattened/verify.js +2 -4
  7. package/dist/browser/jwt/encrypt.js +1 -1
  8. package/dist/browser/{lib/jwt_producer.js → jwt/produce.js} +4 -4
  9. package/dist/browser/jwt/sign.js +1 -1
  10. package/dist/browser/jwt/unsecured.js +1 -1
  11. package/dist/browser/lib/cek.js +3 -2
  12. package/dist/browser/lib/check_key_type.js +33 -16
  13. package/dist/browser/lib/decrypt_key_management.js +35 -35
  14. package/dist/browser/lib/encrypt_key_management.js +3 -5
  15. package/dist/browser/lib/iv.js +3 -2
  16. package/dist/browser/runtime/aesgcmkw.js +1 -3
  17. package/dist/browser/runtime/fetch_jwks.js +16 -3
  18. package/dist/browser/runtime/is_key_like.js +5 -0
  19. package/dist/browser/runtime/random.js +1 -2
  20. package/dist/browser/util/errors.js +73 -34
  21. package/dist/node/cjs/jwe/flattened/decrypt.js +5 -9
  22. package/dist/node/cjs/jwe/flattened/encrypt.js +2 -5
  23. package/dist/node/cjs/jwk/embedded.js +2 -2
  24. package/dist/node/cjs/jwks/remote.js +7 -10
  25. package/dist/node/cjs/jws/flattened/sign.js +1 -2
  26. package/dist/node/cjs/jws/flattened/verify.js +2 -4
  27. package/dist/node/cjs/jwt/encrypt.js +2 -2
  28. package/dist/node/cjs/{lib/jwt_producer.js → jwt/produce.js} +5 -4
  29. package/dist/node/cjs/jwt/sign.js +2 -2
  30. package/dist/node/cjs/jwt/unsecured.js +2 -2
  31. package/dist/node/cjs/lib/cek.js +4 -3
  32. package/dist/node/cjs/lib/check_key_type.js +33 -16
  33. package/dist/node/cjs/lib/decrypt_key_management.js +35 -35
  34. package/dist/node/cjs/lib/encrypt_key_management.js +7 -9
  35. package/dist/node/cjs/lib/iv.js +4 -3
  36. package/dist/node/cjs/runtime/aesgcmkw.js +1 -3
  37. package/dist/node/cjs/runtime/fetch_jwks.js +5 -1
  38. package/dist/node/cjs/runtime/is_key_like.js +11 -0
  39. package/dist/node/cjs/runtime/random.js +3 -2
  40. package/dist/node/cjs/util/errors.js +77 -37
  41. package/dist/node/esm/jwe/flattened/decrypt.js +5 -9
  42. package/dist/node/esm/jwe/flattened/encrypt.js +2 -5
  43. package/dist/node/esm/jwk/embedded.js +2 -2
  44. package/dist/node/esm/jwks/remote.js +7 -10
  45. package/dist/node/esm/jws/flattened/sign.js +1 -2
  46. package/dist/node/esm/jws/flattened/verify.js +2 -4
  47. package/dist/node/esm/jwt/encrypt.js +1 -1
  48. package/dist/node/esm/{lib/jwt_producer.js → jwt/produce.js} +4 -4
  49. package/dist/node/esm/jwt/sign.js +1 -1
  50. package/dist/node/esm/jwt/unsecured.js +1 -1
  51. package/dist/node/esm/lib/cek.js +3 -2
  52. package/dist/node/esm/lib/check_key_type.js +33 -16
  53. package/dist/node/esm/lib/decrypt_key_management.js +35 -35
  54. package/dist/node/esm/lib/encrypt_key_management.js +3 -5
  55. package/dist/node/esm/lib/iv.js +3 -2
  56. package/dist/node/esm/runtime/aesgcmkw.js +1 -3
  57. package/dist/node/esm/runtime/fetch_jwks.js +6 -2
  58. package/dist/node/esm/runtime/is_key_like.js +8 -0
  59. package/dist/node/esm/runtime/random.js +1 -2
  60. package/dist/node/esm/util/errors.js +73 -34
  61. package/dist/types/jwe/compact/decrypt.d.ts +1 -1
  62. package/dist/types/jwe/compact/encrypt.d.ts +1 -1
  63. package/dist/types/jwe/flattened/decrypt.d.ts +1 -1
  64. package/dist/types/jwe/flattened/encrypt.d.ts +1 -1
  65. package/dist/types/jwe/general/decrypt.d.ts +1 -1
  66. package/dist/types/jwk/embedded.d.ts +2 -3
  67. package/dist/types/jwk/from_key_like.d.ts +1 -1
  68. package/dist/types/jwk/parse.d.ts +1 -1
  69. package/dist/types/jws/compact/sign.d.ts +1 -1
  70. package/dist/types/jws/compact/verify.d.ts +1 -1
  71. package/dist/types/jws/flattened/sign.d.ts +1 -1
  72. package/dist/types/jws/flattened/verify.d.ts +1 -1
  73. package/dist/types/jws/general/sign.d.ts +1 -1
  74. package/dist/types/jws/general/verify.d.ts +1 -1
  75. package/dist/types/jwt/decrypt.d.ts +1 -1
  76. package/dist/types/jwt/encrypt.d.ts +2 -2
  77. package/dist/types/{lib/jwt_producer.d.ts → jwt/produce.d.ts} +1 -1
  78. package/dist/types/jwt/sign.d.ts +2 -2
  79. package/dist/types/jwt/unsecured.d.ts +1 -1
  80. package/dist/types/jwt/verify.d.ts +1 -1
  81. package/dist/types/key/export.d.ts +3 -3
  82. package/dist/types/key/import.d.ts +4 -4
  83. package/dist/types/types.d.ts +99 -6
  84. package/dist/types/util/errors.d.ts +24 -16
  85. package/dist/types/util/generate_key_pair.d.ts +2 -2
  86. package/dist/types/util/generate_secret.d.ts +1 -1
  87. package/package.json +3 -4
@@ -1,108 +1,147 @@
1
1
  export class JOSEError extends Error {
2
2
  constructor(message) {
3
+ var _a;
3
4
  super(message);
4
- this.code = JOSEError.code;
5
+ this.code = 'ERR_JOSE_GENERIC';
5
6
  this.name = this.constructor.name;
6
- if (Error.captureStackTrace) {
7
- Error.captureStackTrace(this, this.constructor);
8
- }
7
+ (_a = Error.captureStackTrace) === null || _a === void 0 ? void 0 : _a.call(Error, this, this.constructor);
8
+ }
9
+ static get code() {
10
+ return 'ERR_JOSE_GENERIC';
9
11
  }
10
12
  }
11
- JOSEError.code = 'ERR_JOSE_GENERIC';
12
13
  export class JWTClaimValidationFailed extends JOSEError {
13
14
  constructor(message, claim = 'unspecified', reason = 'unspecified') {
14
15
  super(message);
15
- this.code = JWTClaimValidationFailed.code;
16
+ this.code = 'ERR_JWT_CLAIM_VALIDATION_FAILED';
16
17
  this.claim = claim;
17
18
  this.reason = reason;
18
19
  }
20
+ static get code() {
21
+ return 'ERR_JWT_CLAIM_VALIDATION_FAILED';
22
+ }
23
+ }
24
+ export class JWTExpired extends JOSEError {
25
+ constructor(message, claim = 'unspecified', reason = 'unspecified') {
26
+ super(message);
27
+ this.code = 'ERR_JWT_EXPIRED';
28
+ this.claim = claim;
29
+ this.reason = reason;
30
+ }
31
+ static get code() {
32
+ return 'ERR_JWT_EXPIRED';
33
+ }
19
34
  }
20
- JWTClaimValidationFailed.code = 'ERR_JWT_CLAIM_VALIDATION_FAILED';
21
35
  export class JOSEAlgNotAllowed extends JOSEError {
22
36
  constructor() {
23
37
  super(...arguments);
24
- this.code = JOSEAlgNotAllowed.code;
38
+ this.code = 'ERR_JOSE_ALG_NOT_ALLOWED';
39
+ }
40
+ static get code() {
41
+ return 'ERR_JOSE_ALG_NOT_ALLOWED';
25
42
  }
26
43
  }
27
- JOSEAlgNotAllowed.code = 'ERR_JOSE_ALG_NOT_ALLOWED';
28
44
  export class JOSENotSupported extends JOSEError {
29
45
  constructor() {
30
46
  super(...arguments);
31
- this.code = JOSENotSupported.code;
47
+ this.code = 'ERR_JOSE_NOT_SUPPORTED';
48
+ }
49
+ static get code() {
50
+ return 'ERR_JOSE_NOT_SUPPORTED';
32
51
  }
33
52
  }
34
- JOSENotSupported.code = 'ERR_JOSE_NOT_SUPPORTED';
35
53
  export class JWEDecryptionFailed extends JOSEError {
36
54
  constructor() {
37
55
  super(...arguments);
38
- this.code = JWEDecryptionFailed.code;
56
+ this.code = 'ERR_JWE_DECRYPTION_FAILED';
39
57
  this.message = 'decryption operation failed';
40
58
  }
59
+ static get code() {
60
+ return 'ERR_JWE_DECRYPTION_FAILED';
61
+ }
41
62
  }
42
- JWEDecryptionFailed.code = 'ERR_JWE_DECRYPTION_FAILED';
43
63
  export class JWEInvalid extends JOSEError {
44
64
  constructor() {
45
65
  super(...arguments);
46
- this.code = JWEInvalid.code;
66
+ this.code = 'ERR_JWE_INVALID';
67
+ }
68
+ static get code() {
69
+ return 'ERR_JWE_INVALID';
47
70
  }
48
71
  }
49
- JWEInvalid.code = 'ERR_JWE_INVALID';
50
72
  export class JWSInvalid extends JOSEError {
51
73
  constructor() {
52
74
  super(...arguments);
53
- this.code = JWSInvalid.code;
75
+ this.code = 'ERR_JWS_INVALID';
76
+ }
77
+ static get code() {
78
+ return 'ERR_JWS_INVALID';
54
79
  }
55
80
  }
56
- JWSInvalid.code = 'ERR_JWS_INVALID';
57
81
  export class JWTInvalid extends JOSEError {
58
82
  constructor() {
59
83
  super(...arguments);
60
- this.code = JWTInvalid.code;
84
+ this.code = 'ERR_JWT_INVALID';
85
+ }
86
+ static get code() {
87
+ return 'ERR_JWT_INVALID';
61
88
  }
62
89
  }
63
- JWTInvalid.code = 'ERR_JWT_INVALID';
64
90
  export class JWKInvalid extends JOSEError {
65
91
  constructor() {
66
92
  super(...arguments);
67
- this.code = JWKInvalid.code;
93
+ this.code = 'ERR_JWK_INVALID';
94
+ }
95
+ static get code() {
96
+ return 'ERR_JWK_INVALID';
68
97
  }
69
98
  }
70
- JWKInvalid.code = 'ERR_JWK_INVALID';
71
99
  export class JWKSInvalid extends JOSEError {
72
100
  constructor() {
73
101
  super(...arguments);
74
- this.code = JWKSInvalid.code;
102
+ this.code = 'ERR_JWKS_INVALID';
103
+ }
104
+ static get code() {
105
+ return 'ERR_JWKS_INVALID';
75
106
  }
76
107
  }
77
- JWKSInvalid.code = 'ERR_JWKS_INVALID';
78
108
  export class JWKSNoMatchingKey extends JOSEError {
79
109
  constructor() {
80
110
  super(...arguments);
81
- this.code = JWKSNoMatchingKey.code;
111
+ this.code = 'ERR_JWKS_NO_MATCHING_KEY';
82
112
  this.message = 'no applicable key found in the JSON Web Key Set';
83
113
  }
114
+ static get code() {
115
+ return 'ERR_JWKS_NO_MATCHING_KEY';
116
+ }
84
117
  }
85
- JWKSNoMatchingKey.code = 'ERR_JWKS_NO_MATCHING_KEY';
86
118
  export class JWKSMultipleMatchingKeys extends JOSEError {
87
119
  constructor() {
88
120
  super(...arguments);
89
- this.code = JWKSMultipleMatchingKeys.code;
121
+ this.code = 'ERR_JWKS_MULTIPLE_MATCHING_KEYS';
90
122
  this.message = 'multiple matching keys found in the JSON Web Key Set';
91
123
  }
124
+ static get code() {
125
+ return 'ERR_JWKS_MULTIPLE_MATCHING_KEYS';
126
+ }
92
127
  }
93
- JWKSMultipleMatchingKeys.code = 'ERR_JWKS_MULTIPLE_MATCHING_KEYS';
94
- export class JWSSignatureVerificationFailed extends JOSEError {
128
+ export class JWKSTimeout extends JOSEError {
95
129
  constructor() {
96
130
  super(...arguments);
97
- this.code = JWSSignatureVerificationFailed.code;
98
- this.message = 'signature verification failed';
131
+ this.code = 'ERR_JWKS_TIMEOUT';
132
+ this.message = 'request timed out';
133
+ }
134
+ static get code() {
135
+ return 'ERR_JWKS_TIMEOUT';
99
136
  }
100
137
  }
101
- JWSSignatureVerificationFailed.code = 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED';
102
- export class JWTExpired extends JWTClaimValidationFailed {
138
+ export class JWSSignatureVerificationFailed extends JOSEError {
103
139
  constructor() {
104
140
  super(...arguments);
105
- this.code = JWTExpired.code;
141
+ this.code = 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED';
142
+ this.message = 'signature verification failed';
143
+ }
144
+ static get code() {
145
+ return 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED';
106
146
  }
107
147
  }
108
- JWTExpired.code = 'ERR_JWT_EXPIRED';
@@ -4,7 +4,6 @@ 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
6
  const zlib_js_1 = require("../../runtime/zlib.js");
7
- const random_js_1 = require("../../runtime/random.js");
8
7
  const errors_js_1 = require("../../util/errors.js");
9
8
  const is_disjoint_js_1 = require("../../lib/is_disjoint.js");
10
9
  const is_object_js_1 = require("../../lib/is_object.js");
@@ -13,10 +12,6 @@ const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
13
12
  const cek_js_1 = require("../../lib/cek.js");
14
13
  const validate_crit_js_1 = require("../../lib/validate_crit.js");
15
14
  const validate_algorithms_js_1 = require("../../lib/validate_algorithms.js");
16
- const generateCek = (0, cek_js_1.default)(random_js_1.default);
17
- const checkExtensions = validate_crit_js_1.default.bind(undefined, errors_js_1.JWEInvalid, new Map());
18
- const checkAlgOption = validate_algorithms_js_1.default.bind(undefined, 'keyManagementAlgorithms');
19
- const checkEncOption = validate_algorithms_js_1.default.bind(undefined, 'contentEncryptionAlgorithms');
20
15
  async function flattenedDecrypt(jwe, key, options) {
21
16
  var _a;
22
17
  if (!(0, is_object_js_1.default)(jwe)) {
@@ -67,7 +62,7 @@ async function flattenedDecrypt(jwe, key, options) {
67
62
  ...jwe.header,
68
63
  ...jwe.unprotected,
69
64
  };
70
- checkExtensions(options === null || options === void 0 ? void 0 : options.crit, parsedProt, joseHeader);
65
+ (0, validate_crit_js_1.default)(errors_js_1.JWEInvalid, new Map(), options === null || options === void 0 ? void 0 : options.crit, parsedProt, joseHeader);
71
66
  if (joseHeader.zip !== undefined) {
72
67
  if (!parsedProt || !parsedProt.zip) {
73
68
  throw new errors_js_1.JWEInvalid('JWE "zip" (Compression Algorithm) Header MUST be integrity protected');
@@ -83,8 +78,9 @@ async function flattenedDecrypt(jwe, key, options) {
83
78
  if (typeof enc !== 'string' || !enc) {
84
79
  throw new errors_js_1.JWEInvalid('missing JWE Encryption Algorithm (enc) in JWE Header');
85
80
  }
86
- const keyManagementAlgorithms = options && checkAlgOption(options.keyManagementAlgorithms);
87
- const contentEncryptionAlgorithms = options && checkEncOption(options.contentEncryptionAlgorithms);
81
+ const keyManagementAlgorithms = options && (0, validate_algorithms_js_1.default)('keyManagementAlgorithms', options.keyManagementAlgorithms);
82
+ const contentEncryptionAlgorithms = options &&
83
+ (0, validate_algorithms_js_1.default)('contentEncryptionAlgorithms', options.contentEncryptionAlgorithms);
88
84
  if (keyManagementAlgorithms && !keyManagementAlgorithms.has(alg)) {
89
85
  throw new errors_js_1.JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed');
90
86
  }
@@ -108,7 +104,7 @@ async function flattenedDecrypt(jwe, key, options) {
108
104
  if (err instanceof TypeError) {
109
105
  throw err;
110
106
  }
111
- cek = generateCek(enc);
107
+ cek = (0, cek_js_1.default)(enc);
112
108
  }
113
109
  const iv = (0, base64url_js_1.decode)(jwe.iv);
114
110
  const tag = (0, base64url_js_1.decode)(jwe.tag);
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.FlattenedEncrypt = void 0;
4
4
  const base64url_js_1 = require("../../runtime/base64url.js");
5
- const random_js_1 = require("../../runtime/random.js");
6
5
  const encrypt_js_1 = require("../../runtime/encrypt.js");
7
6
  const zlib_js_1 = require("../../runtime/zlib.js");
8
7
  const iv_js_1 = require("../../lib/iv.js");
@@ -11,8 +10,6 @@ const errors_js_1 = require("../../util/errors.js");
11
10
  const is_disjoint_js_1 = require("../../lib/is_disjoint.js");
12
11
  const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
13
12
  const validate_crit_js_1 = require("../../lib/validate_crit.js");
14
- const generateIv = (0, iv_js_1.default)(random_js_1.default);
15
- const checkExtensions = validate_crit_js_1.default.bind(undefined, errors_js_1.JWEInvalid, new Map());
16
13
  class FlattenedEncrypt {
17
14
  constructor(plaintext) {
18
15
  if (!(plaintext instanceof Uint8Array)) {
@@ -78,7 +75,7 @@ class FlattenedEncrypt {
78
75
  ...this._unprotectedHeader,
79
76
  ...this._sharedUnprotectedHeader,
80
77
  };
81
- checkExtensions(options === null || options === void 0 ? void 0 : options.crit, this._protectedHeader, joseHeader);
78
+ (0, validate_crit_js_1.default)(errors_js_1.JWEInvalid, new Map(), options === null || options === void 0 ? void 0 : options.crit, this._protectedHeader, joseHeader);
82
79
  if (joseHeader.zip !== undefined) {
83
80
  if (!this._protectedHeader || !this._protectedHeader.zip) {
84
81
  throw new errors_js_1.JWEInvalid('JWE "zip" (Compression Algorithm) Header MUST be integrity protected');
@@ -118,7 +115,7 @@ class FlattenedEncrypt {
118
115
  }
119
116
  }
120
117
  }
121
- this._iv || (this._iv = generateIv(enc));
118
+ this._iv || (this._iv = (0, iv_js_1.default)(enc));
122
119
  let additionalData;
123
120
  let protectedHeader;
124
121
  let aadMember;
@@ -12,8 +12,8 @@ async function EmbeddedJWK(protectedHeader, token) {
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));
16
- if (key.type !== 'public') {
15
+ const key = await (0, import_js_1.importJWK)({ ...joseHeader.jwk, ext: true }, joseHeader.alg, true);
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
  }
19
19
  return key;
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createRemoteJWKSet = void 0;
4
4
  const fetch_jwks_js_1 = require("../runtime/fetch_jwks.js");
5
- const parse_js_1 = require("../jwk/parse.js");
5
+ const import_js_1 = require("../key/import.js");
6
6
  const errors_js_1 = require("../util/errors.js");
7
7
  const is_object_js_1 = require("../lib/is_object.js");
8
8
  function getKtyFromAlg(alg) {
9
- switch (alg.substr(0, 2)) {
9
+ switch (typeof alg === 'string' && alg.substr(0, 2)) {
10
10
  case 'RS':
11
11
  case 'PS':
12
12
  return 'RSA';
@@ -35,7 +35,7 @@ class RemoteJWKSet {
35
35
  typeof (options === null || options === void 0 ? void 0 : options.cooldownDuration) === 'number' ? options === null || options === void 0 ? void 0 : options.cooldownDuration : 30000;
36
36
  }
37
37
  coolingDown() {
38
- if (typeof this._cooldownStarted === 'undefined') {
38
+ if (!this._cooldownStarted) {
39
39
  return false;
40
40
  }
41
41
  return Date.now() < this._cooldownStarted + this._cooldownDuration;
@@ -59,7 +59,7 @@ class RemoteJWKSet {
59
59
  candidate = jwk.key_ops.includes('verify');
60
60
  }
61
61
  if (candidate && protectedHeader.alg === 'EdDSA') {
62
- candidate = ['Ed25519', 'Ed448'].includes(jwk.crv);
62
+ candidate = jwk.crv === 'Ed25519' || jwk.crv === 'Ed448';
63
63
  }
64
64
  if (candidate) {
65
65
  switch (protectedHeader.alg) {
@@ -91,13 +91,10 @@ class RemoteJWKSet {
91
91
  else if (length !== 1) {
92
92
  throw new errors_js_1.JWKSMultipleMatchingKeys();
93
93
  }
94
- if (!this._cached.has(jwk)) {
95
- this._cached.set(jwk, {});
96
- }
97
- const cached = this._cached.get(jwk);
94
+ const cached = this._cached.get(jwk) || this._cached.set(jwk, {}).get(jwk);
98
95
  if (cached[protectedHeader.alg] === undefined) {
99
- const keyObject = (await (0, parse_js_1.default)({ ...jwk, alg: protectedHeader.alg, ext: true }));
100
- if (keyObject.type !== 'public') {
96
+ const keyObject = await (0, import_js_1.importJWK)({ ...jwk, ext: true }, protectedHeader.alg);
97
+ if (keyObject instanceof Uint8Array || keyObject.type !== 'public') {
101
98
  throw new errors_js_1.JWKSInvalid('JSON Web Key Set members must be public keys');
102
99
  }
103
100
  cached[protectedHeader.alg] = keyObject;
@@ -8,7 +8,6 @@ const errors_js_1 = require("../../util/errors.js");
8
8
  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
- const checkExtensions = validate_crit_js_1.default.bind(undefined, errors_js_1.JWSInvalid, new Map([['b64', true]]));
12
11
  class FlattenedSign {
13
12
  constructor(payload) {
14
13
  if (!(payload instanceof Uint8Array)) {
@@ -41,7 +40,7 @@ class FlattenedSign {
41
40
  ...this._protectedHeader,
42
41
  ...this._unprotectedHeader,
43
42
  };
44
- const extensions = checkExtensions(options === null || options === void 0 ? void 0 : options.crit, this._protectedHeader, joseHeader);
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);
45
44
  let b64 = true;
46
45
  if (extensions.has('b64')) {
47
46
  b64 = this._protectedHeader.b64;
@@ -10,8 +10,6 @@ const is_object_js_1 = require("../../lib/is_object.js");
10
10
  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
- const checkExtensions = validate_crit_js_1.default.bind(undefined, errors_js_1.JWSInvalid, new Map([['b64', true]]));
14
- const checkAlgOption = validate_algorithms_js_1.default.bind(undefined, 'algorithms');
15
13
  async function flattenedVerify(jws, key, options) {
16
14
  var _a;
17
15
  if (!(0, is_object_js_1.default)(jws)) {
@@ -49,7 +47,7 @@ async function flattenedVerify(jws, key, options) {
49
47
  ...parsedProt,
50
48
  ...jws.header,
51
49
  };
52
- const extensions = checkExtensions(options === null || options === void 0 ? void 0 : options.crit, parsedProt, joseHeader);
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);
53
51
  let b64 = true;
54
52
  if (extensions.has('b64')) {
55
53
  b64 = parsedProt.b64;
@@ -61,7 +59,7 @@ async function flattenedVerify(jws, key, options) {
61
59
  if (typeof alg !== 'string' || !alg) {
62
60
  throw new errors_js_1.JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
63
61
  }
64
- const algorithms = options && checkAlgOption(options.algorithms);
62
+ const algorithms = options && (0, validate_algorithms_js_1.default)('algorithms', options.algorithms);
65
63
  if (algorithms && !algorithms.has(alg)) {
66
64
  throw new errors_js_1.JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed');
67
65
  }
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EncryptJWT = void 0;
4
4
  const encrypt_js_1 = require("../jwe/compact/encrypt.js");
5
5
  const buffer_utils_js_1 = require("../lib/buffer_utils.js");
6
- const jwt_producer_js_1 = require("../lib/jwt_producer.js");
7
- class EncryptJWT extends jwt_producer_js_1.default {
6
+ const produce_js_1 = require("./produce.js");
7
+ class EncryptJWT extends produce_js_1.ProduceJWT {
8
8
  setProtectedHeader(protectedHeader) {
9
9
  if (this._protectedHeader) {
10
10
  throw new TypeError('setProtectedHeader can only be called once');
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const epoch_js_1 = require("./epoch.js");
4
- const is_object_js_1 = require("./is_object.js");
5
- const secs_js_1 = require("./secs.js");
3
+ exports.ProduceJWT = void 0;
4
+ const epoch_js_1 = require("../lib/epoch.js");
5
+ const is_object_js_1 = require("../lib/is_object.js");
6
+ const secs_js_1 = require("../lib/secs.js");
6
7
  class ProduceJWT {
7
8
  constructor(payload) {
8
9
  if (!(0, is_object_js_1.default)(payload)) {
@@ -54,4 +55,4 @@ class ProduceJWT {
54
55
  return this;
55
56
  }
56
57
  }
57
- exports.default = ProduceJWT;
58
+ exports.ProduceJWT = ProduceJWT;
@@ -4,8 +4,8 @@ exports.SignJWT = void 0;
4
4
  const sign_js_1 = require("../jws/compact/sign.js");
5
5
  const errors_js_1 = require("../util/errors.js");
6
6
  const buffer_utils_js_1 = require("../lib/buffer_utils.js");
7
- const jwt_producer_js_1 = require("../lib/jwt_producer.js");
8
- class SignJWT extends jwt_producer_js_1.default {
7
+ const produce_js_1 = require("./produce.js");
8
+ class SignJWT extends produce_js_1.ProduceJWT {
9
9
  setProtectedHeader(protectedHeader) {
10
10
  this._protectedHeader = protectedHeader;
11
11
  return this;
@@ -5,8 +5,8 @@ const base64url = require("../runtime/base64url.js");
5
5
  const buffer_utils_js_1 = require("../lib/buffer_utils.js");
6
6
  const errors_js_1 = require("../util/errors.js");
7
7
  const jwt_claims_set_js_1 = require("../lib/jwt_claims_set.js");
8
- const jwt_producer_js_1 = require("../lib/jwt_producer.js");
9
- class UnsecuredJWT extends jwt_producer_js_1.default {
8
+ const produce_js_1 = require("./produce.js");
9
+ class UnsecuredJWT extends produce_js_1.ProduceJWT {
10
10
  encode() {
11
11
  const header = base64url.encode(JSON.stringify({ alg: 'none' }));
12
12
  const payload = base64url.encode(JSON.stringify(this._payload));
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.bitLengths = void 0;
4
4
  const errors_js_1 = require("../util/errors.js");
5
+ const random_js_1 = require("../runtime/random.js");
5
6
  const bitLengths = new Map([
6
7
  ['A128CBC-HS256', 256],
7
8
  ['A128GCM', 128],
@@ -11,11 +12,11 @@ const bitLengths = new Map([
11
12
  ['A256GCM', 256],
12
13
  ]);
13
14
  exports.bitLengths = bitLengths;
14
- const factory = (random) => (alg) => {
15
+ const generateCek = (alg) => {
15
16
  const bitLength = bitLengths.get(alg);
16
17
  if (!bitLength) {
17
18
  throw new errors_js_1.JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
18
19
  }
19
- return random(new Uint8Array(bitLength >> 3));
20
+ return (0, random_js_1.default)(new Uint8Array(bitLength >> 3));
20
21
  };
21
- exports.default = factory;
22
+ exports.default = generateCek;
@@ -1,30 +1,47 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const invalid_key_input_js_1 = require("../runtime/invalid_key_input.js");
4
- const checkKeyType = (alg, key, usage) => {
5
- if (!(key instanceof Uint8Array) && !(key === null || key === void 0 ? void 0 : key.type)) {
6
- throw new TypeError((0, invalid_key_input_js_1.default)(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
4
+ const is_key_like_js_1 = require("../runtime/is_key_like.js");
5
+ const symmetricTypeCheck = (key) => {
6
+ if (key instanceof Uint8Array)
7
+ return;
8
+ if (!(0, is_key_like_js_1.default)(key)) {
9
+ throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types, 'Uint8Array'));
7
10
  }
8
- if (alg.startsWith('HS') ||
9
- alg === 'dir' ||
10
- alg.startsWith('PBES2') ||
11
- alg.match(/^A\d{3}(?:GCM)?KW$/)) {
12
- if (key instanceof Uint8Array || key.type === 'secret') {
13
- return;
14
- }
15
- throw new TypeError('CryptoKey or KeyObject instances for symmetric algorithms must be of type "secret"');
11
+ if (key.type !== 'secret') {
12
+ throw new TypeError(`${is_key_like_js_1.types.join(' or ')} instances for symmetric algorithms must be of type "secret"`);
16
13
  }
17
- if (key instanceof Uint8Array) {
18
- throw new TypeError((0, invalid_key_input_js_1.default)(key, 'KeyObject', 'CryptoKey'));
14
+ };
15
+ const asymmetricTypeCheck = (key, usage) => {
16
+ if (!(0, is_key_like_js_1.default)(key)) {
17
+ throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types));
19
18
  }
20
19
  if (key.type === 'secret') {
21
- throw new TypeError('CryptoKey or KeyObject instances for asymmetric algorithms must not be of type "secret"');
20
+ throw new TypeError(`${is_key_like_js_1.types.join(' or ')} instances for asymmetric algorithms must not be of type "secret"`);
22
21
  }
23
22
  if (usage === 'sign' && key.type === 'public') {
24
- throw new TypeError('CryptoKey or KeyObject instances for asymmetric algorithm signing must be of type "private"');
23
+ throw new TypeError(`${is_key_like_js_1.types.join(' or ')} instances for asymmetric algorithm signing must be of type "private"`);
25
24
  }
26
25
  if (usage === 'decrypt' && key.type === 'public') {
27
- throw new TypeError('CryptoKey or KeyObject instances for asymmetric algorithm decryption must be of type "private"');
26
+ throw new TypeError(`${is_key_like_js_1.types.join(' or ')} instances for asymmetric algorithm decryption must be of type "private"`);
27
+ }
28
+ if (key.algorithm && usage === 'verify' && key.type === 'private') {
29
+ throw new TypeError(`${is_key_like_js_1.types.join(' or ')} instances for asymmetric algorithm verifying must be of type "public"`);
30
+ }
31
+ if (key.algorithm && usage === 'encrypt' && key.type === 'private') {
32
+ throw new TypeError(`${is_key_like_js_1.types.join(' or ')} instances for asymmetric algorithm encryption must be of type "public"`);
33
+ }
34
+ };
35
+ const checkKeyType = (alg, key, usage) => {
36
+ const symmetric = alg.startsWith('HS') ||
37
+ alg === 'dir' ||
38
+ alg.startsWith('PBES2') ||
39
+ /^A\d{3}(?:GCM)?KW$/.test(alg);
40
+ if (symmetric) {
41
+ symmetricTypeCheck(key);
42
+ }
43
+ else {
44
+ asymmetricTypeCheck(key, usage);
28
45
  }
29
46
  };
30
47
  exports.default = checkKeyType;
@@ -10,81 +10,81 @@ const errors_js_1 = require("../util/errors.js");
10
10
  const cek_js_1 = require("../lib/cek.js");
11
11
  const import_js_1 = require("../key/import.js");
12
12
  const check_key_type_js_1 = require("./check_key_type.js");
13
- function assertEnryptedKey(encryptedKey) {
14
- if (!encryptedKey) {
15
- throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
16
- }
17
- }
18
- function assertHeaderParameter(joseHeader, parameter, name) {
19
- if (joseHeader[parameter] === undefined) {
20
- throw new errors_js_1.JWEInvalid(`JOSE Header ${name} (${parameter}) missing`);
21
- }
22
- }
13
+ const is_object_js_1 = require("./is_object.js");
23
14
  async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
24
15
  (0, check_key_type_js_1.default)(alg, key, 'decrypt');
25
16
  switch (alg) {
26
17
  case 'dir': {
27
- if (encryptedKey !== undefined) {
18
+ if (encryptedKey !== undefined)
28
19
  throw new errors_js_1.JWEInvalid('Encountered unexpected JWE Encrypted Key');
29
- }
30
20
  return key;
31
21
  }
32
22
  case 'ECDH-ES':
33
- if (encryptedKey !== undefined) {
23
+ if (encryptedKey !== undefined)
34
24
  throw new errors_js_1.JWEInvalid('Encountered unexpected JWE Encrypted Key');
35
- }
36
25
  case 'ECDH-ES+A128KW':
37
26
  case 'ECDH-ES+A192KW':
38
27
  case 'ECDH-ES+A256KW': {
39
- assertHeaderParameter(joseHeader, 'epk', 'Ephemeral Public Key');
40
- if (!ECDH.ecdhAllowed(key)) {
28
+ if (!(0, is_object_js_1.default)(joseHeader.epk))
29
+ throw new errors_js_1.JWEInvalid(`JOSE Header "epk" (Ephemeral Public Key) missing or invalid`);
30
+ if (!ECDH.ecdhAllowed(key))
41
31
  throw new errors_js_1.JOSENotSupported('ECDH-ES with the provided key is not allowed or not supported by your javascript runtime');
42
- }
43
32
  const epk = await (0, import_js_1.importJWK)(joseHeader.epk, alg);
44
33
  let partyUInfo;
45
34
  let partyVInfo;
46
- if (joseHeader.apu !== undefined)
35
+ if (joseHeader.apu !== undefined) {
36
+ if (typeof joseHeader.apu !== 'string')
37
+ throw new errors_js_1.JWEInvalid(`JOSE Header "apu" (Agreement PartyUInfo) invalid`);
47
38
  partyUInfo = (0, base64url_js_1.decode)(joseHeader.apu);
48
- if (joseHeader.apv !== undefined)
39
+ }
40
+ if (joseHeader.apv !== undefined) {
41
+ if (typeof joseHeader.apv !== 'string')
42
+ throw new errors_js_1.JWEInvalid(`JOSE Header "apv" (Agreement PartyVInfo) invalid`);
49
43
  partyVInfo = (0, base64url_js_1.decode)(joseHeader.apv);
44
+ }
50
45
  const sharedSecret = await ECDH.deriveKey(epk, key, alg === 'ECDH-ES' ? joseHeader.enc : alg, parseInt(alg.substr(-5, 3), 10) || cek_js_1.bitLengths.get(joseHeader.enc), partyUInfo, partyVInfo);
51
- if (alg === 'ECDH-ES') {
46
+ if (alg === 'ECDH-ES')
52
47
  return sharedSecret;
53
- }
54
- assertEnryptedKey(encryptedKey);
55
- const kwAlg = alg.substr(-6);
56
- return (0, aeskw_js_1.unwrap)(kwAlg, sharedSecret, encryptedKey);
48
+ if (encryptedKey === undefined)
49
+ throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
50
+ return (0, aeskw_js_1.unwrap)(alg.substr(-6), sharedSecret, encryptedKey);
57
51
  }
58
52
  case 'RSA1_5':
59
53
  case 'RSA-OAEP':
60
54
  case 'RSA-OAEP-256':
61
55
  case 'RSA-OAEP-384':
62
56
  case 'RSA-OAEP-512': {
63
- assertEnryptedKey(encryptedKey);
57
+ if (encryptedKey === undefined)
58
+ throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
64
59
  return (0, rsaes_js_1.decrypt)(alg, key, encryptedKey);
65
60
  }
66
61
  case 'PBES2-HS256+A128KW':
67
62
  case 'PBES2-HS384+A192KW':
68
63
  case 'PBES2-HS512+A256KW': {
69
- assertEnryptedKey(encryptedKey);
70
- assertHeaderParameter(joseHeader, 'p2c', 'PBES2 Count');
71
- assertHeaderParameter(joseHeader, 'p2s', 'PBES2 Salt');
72
- const { p2c } = joseHeader;
73
- const p2s = (0, base64url_js_1.decode)(joseHeader.p2s);
74
- return (0, pbes2kw_js_1.decrypt)(alg, key, encryptedKey, p2c, p2s);
64
+ if (encryptedKey === undefined)
65
+ throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
66
+ if (typeof joseHeader.p2c !== 'number')
67
+ throw new errors_js_1.JWEInvalid(`JOSE Header "p2c" (PBES2 Count) missing or invalid`);
68
+ if (typeof joseHeader.p2s !== 'string')
69
+ throw new errors_js_1.JWEInvalid(`JOSE Header "p2s" (PBES2 Salt) missing or invalid`);
70
+ return (0, pbes2kw_js_1.decrypt)(alg, key, encryptedKey, joseHeader.p2c, (0, base64url_js_1.decode)(joseHeader.p2s));
75
71
  }
76
72
  case 'A128KW':
77
73
  case 'A192KW':
78
74
  case 'A256KW': {
79
- assertEnryptedKey(encryptedKey);
75
+ if (encryptedKey === undefined)
76
+ throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
80
77
  return (0, aeskw_js_1.unwrap)(alg, key, encryptedKey);
81
78
  }
82
79
  case 'A128GCMKW':
83
80
  case 'A192GCMKW':
84
81
  case 'A256GCMKW': {
85
- assertEnryptedKey(encryptedKey);
86
- assertHeaderParameter(joseHeader, 'iv', 'Initialization Vector');
87
- assertHeaderParameter(joseHeader, 'tag', 'Authentication Tag');
82
+ if (encryptedKey === undefined)
83
+ throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
84
+ if (typeof joseHeader.iv !== 'string')
85
+ throw new errors_js_1.JWEInvalid(`JOSE Header "iv" (Initialization Vector) missing or invalid`);
86
+ if (typeof joseHeader.tag !== 'string')
87
+ throw new errors_js_1.JWEInvalid(`JOSE Header "tag" (Authentication Tag) missing or invalid`);
88
88
  const iv = (0, base64url_js_1.decode)(joseHeader.iv);
89
89
  const tag = (0, base64url_js_1.decode)(joseHeader.tag);
90
90
  return (0, aesgcmkw_js_1.unwrap)(alg, key, encryptedKey, iv, tag);