jose 4.13.1 → 4.14.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 (37) hide show
  1. package/README.md +94 -88
  2. package/dist/browser/jws/general/sign.js +1 -1
  3. package/dist/browser/lib/jwt_claims_set.js +17 -6
  4. package/dist/node/cjs/jws/general/sign.js +1 -1
  5. package/dist/node/cjs/lib/jwt_claims_set.js +17 -6
  6. package/dist/node/esm/jws/general/sign.js +1 -1
  7. package/dist/node/esm/lib/jwt_claims_set.js +17 -6
  8. package/dist/types/jwe/compact/decrypt.d.ts +4 -14
  9. package/dist/types/jwe/compact/encrypt.d.ts +3 -13
  10. package/dist/types/jwe/flattened/decrypt.d.ts +4 -24
  11. package/dist/types/jwe/flattened/encrypt.d.ts +3 -14
  12. package/dist/types/jwe/general/decrypt.d.ts +4 -28
  13. package/dist/types/jwe/general/encrypt.d.ts +2 -16
  14. package/dist/types/jwk/embedded.d.ts +0 -14
  15. package/dist/types/jwk/thumbprint.d.ts +2 -30
  16. package/dist/types/jwks/local.d.ts +0 -59
  17. package/dist/types/jwks/remote.d.ts +5 -48
  18. package/dist/types/jws/compact/sign.d.ts +3 -13
  19. package/dist/types/jws/compact/verify.d.ts +5 -17
  20. package/dist/types/jws/flattened/sign.d.ts +3 -13
  21. package/dist/types/jws/flattened/verify.d.ts +5 -22
  22. package/dist/types/jws/general/sign.d.ts +3 -16
  23. package/dist/types/jws/general/verify.d.ts +5 -25
  24. package/dist/types/jwt/decrypt.d.ts +4 -18
  25. package/dist/types/jwt/encrypt.d.ts +12 -22
  26. package/dist/types/jwt/sign.d.ts +3 -93
  27. package/dist/types/jwt/unsecured.d.ts +0 -23
  28. package/dist/types/jwt/verify.d.ts +5 -84
  29. package/dist/types/key/export.d.ts +0 -26
  30. package/dist/types/key/generate_key_pair.d.ts +1 -9
  31. package/dist/types/key/generate_secret.d.ts +1 -8
  32. package/dist/types/key/import.d.ts +15 -80
  33. package/dist/types/types.d.ts +63 -95
  34. package/dist/types/util/decode_jwt.d.ts +0 -7
  35. package/dist/types/util/decode_protected_header.d.ts +0 -7
  36. package/dist/types/util/errors.d.ts +57 -14
  37. package/package.json +1 -1
package/README.md CHANGED
@@ -1,31 +1,19 @@
1
1
  # jose
2
2
 
3
- > "JSON Web Almost Everything" - JWA, JWS, JWE, JWT, JWK, JWKS for Node.js, Browser, Cloudflare Workers, Deno, Bun, and other Web-interoperable runtimes.
4
-
5
- ## Implemented specs & features
6
-
7
- The following specifications are implemented by `jose`
8
-
9
- - JSON Web Signature (JWS) - [RFC7515][spec-jws]
10
- - JSON Web Encryption (JWE) - [RFC7516][spec-jwe]
11
- - JSON Web Key (JWK) - [RFC7517][spec-jwk]
12
- - JSON Web Algorithms (JWA) - [RFC7518][spec-jwa]
13
- - JSON Web Token (JWT) - [RFC7519][spec-jwt]
14
- - JSON Web Key Thumbprint - [RFC7638][spec-thumbprint]
15
- - JSON Web Key Thumbprint URI - [RFC9278][spec-thumbprint-uri]
16
- - JWS Unencoded Payload Option - [RFC7797][spec-b64]
17
- - CFRG Elliptic Curve ECDH and Signatures - [RFC8037][spec-okp]
18
- - secp256k1 EC Key curve support - [JOSE Registrations for WebAuthn Algorithms][spec-secp256k1]
19
-
20
- The test suite utilizes examples defined in [RFC7520][spec-cookbook] to confirm its JOSE
21
- implementation is correct.
3
+ `jose` is JavaScript module for JSON Object Signing and Encryption, providing support for JSON Web Tokens (JWT), JSON Web Signature (JWS), JSON Web Encryption (JWE), JSON Web Key (JWK), JSON Web Key Set (JWKS), and more. The module is designed to work across various Web-interoperable runtimes including Node.js, browsers, Cloudflare Workers, Deno, Bun, and others.
22
4
 
23
5
  ## [💗 Help the project](https://github.com/sponsors/panva)
24
6
 
7
+ Support from the community to continue maintaining and improving this module is welcome. If you find the module useful, please consider supporting the project by [becoming a sponsor](https://github.com/sponsors/panva).
8
+
25
9
  ## Dependencies: 0
26
10
 
11
+ `jose` has no dependencies and it exports tree-shakeable ESM. CJS is also supported.
12
+
27
13
  ## Documentation
28
14
 
15
+ `jose` is distributed via [npmjs.com](https://www.npmjs.com/package/jose), [deno.land/x](https://deno.land/x/jose), [cdnjs.com](https://cdnjs.com/libraries/jose), [jsdelivr.com](https://www.jsdelivr.com/package/npm/jose), and [github.com](https://github.com/panva/jose).
16
+
29
17
  **`example`** ESM import
30
18
  ```js
31
19
  import * as jose from 'jose'
@@ -36,53 +24,96 @@ import * as jose from 'jose'
36
24
  const jose = require('jose')
37
25
  ```
38
26
 
39
- **`example`** Deno import
40
- ```js
41
- import * as jose from 'https://deno.land/x/jose@v4.13.1/index.ts'
42
- ```
27
+ ### JSON Web Tokens (JWT)
28
+
29
+ The `jose` module supports JSON Web Tokens (JWT) and provides functionality for signing and verifying tokens, as well as their JWT Claims Set validation.
30
+
31
+ - [Signing](docs/classes/jwt_sign.SignJWT.md) using the `SignJWT` class
32
+ - [Verification & JWT Claims Set Validation](docs/functions/jwt_verify.jwtVerify.md) using the `jwtVerify` function
33
+ - [Using a remote JWKS](docs/functions/jwks_remote.createRemoteJWKSet.md)
34
+ - [Using a local JWKS](docs/functions/jwks_local.createLocalJWKSet.md)
35
+ - Utility functions
36
+ - [Decoding Token's Protected Header](docs/functions/util_decode_protected_header.decodeProtectedHeader.md)
37
+ - [Decoding JWT Claims Set](docs/functions/util_decode_jwt.decodeJwt.md) prior to its validation
38
+
39
+ ### Encrypted JSON Web Tokens
40
+
41
+ The `jose` module supports encrypted JSON Web Tokens and provides functionality for encrypting and decrypting tokens, as well as their JWT Claims Set validation.
42
+
43
+ - [Encryption](docs/classes/jwt_encrypt.EncryptJWT.md) using the `EncryptJWT` class
44
+ - [Decryption & JWT Claims Set Validation](docs/functions/jwt_decrypt.jwtDecrypt.md) using the `jwtDecrypt` function
45
+ - Utility functions
46
+ - [Decoding Token's Protected Header](docs/functions/util_decode_protected_header.decodeProtectedHeader.md)
47
+
48
+ ### Key Utilities
49
+
50
+ The `jose` module provides functionality for importing, exporting, and generating keys and secrets in various formats, including PEM formats like SPKI, X.509 certificate, and PKCS #8, as well as JSON Web Key (JWK).
51
+
52
+ - Key Import Functions
53
+ - [JWK Import](docs/functions/key_import.importJWK.md)
54
+ - [Public Key Import (SPKI)](docs/functions/key_import.importSPKI.md)
55
+ - [Public Key Import (X.509 Certificate)](docs/functions/key_import.importX509.md)
56
+ - [Private Key Import (PKCS #8)](docs/functions/key_import.importPKCS8.md)
57
+ - Key and Secret Generation Functions
58
+ - [Asymmetric Key Pair Generation](docs/functions/key_generate_key_pair.generateKeyPair.md)
59
+ - [Symmetric Secret Generation](docs/functions/key_generate_secret.generateSecret.md)
60
+ - Key Export Functions
61
+ - [JWK Export](docs/functions/key_export.exportJWK.md)
62
+ - [Private Key Export](docs/functions/key_export.exportPKCS8.md)
63
+ - [Public Key Export](docs/functions/key_export.exportSPKI.md)
64
+
65
+ ### JSON Web Signature (JWS)
43
66
 
44
- - JSON Web Tokens (JWT)
45
- - [Signing](docs/classes/jwt_sign.SignJWT.md#readme)
46
- - [Verification & JWT Claims Set Validation](docs/functions/jwt_verify.jwtVerify.md#readme)
47
- - Encrypted JSON Web Tokens
48
- - [Encryption](docs/classes/jwt_encrypt.EncryptJWT.md#readme)
49
- - [Decryption & JWT Claims Set Validation](docs/functions/jwt_decrypt.jwtDecrypt.md#readme)
50
- - Key Import
51
- - [JWK Import](docs/functions/key_import.importJWK.md#readme)
52
- - [Public Key Import (SPKI)](docs/functions/key_import.importSPKI.md#readme)
53
- - [Public Key Import (X.509 Certificate)](docs/functions/key_import.importX509.md#readme)
54
- - [Private Key Import (PKCS #8)](docs/functions/key_import.importPKCS8.md#readme)
55
- - JSON Web Encryption (JWE)
56
- - Encryption - [Compact](docs/classes/jwe_compact_encrypt.CompactEncrypt.md#readme), [Flattened](docs/classes/jwe_flattened_encrypt.FlattenedEncrypt.md#readme), [General](docs/classes/jwe_general_encrypt.GeneralEncrypt.md#readme)
57
- - Decryption - [Compact](docs/functions/jwe_compact_decrypt.compactDecrypt.md#readme), [Flattened](docs/functions/jwe_flattened_decrypt.flattenedDecrypt.md#readme), [General](docs/functions/jwe_general_decrypt.generalDecrypt.md#readme)
58
- - JSON Web Signature (JWS)
59
- - Signing - [Compact](docs/classes/jws_compact_sign.CompactSign.md#readme), [Flattened](docs/classes/jws_flattened_sign.FlattenedSign.md#readme), [General](docs/classes/jws_general_sign.GeneralSign.md#readme)
60
- - Verification - [Compact](docs/functions/jws_compact_verify.compactVerify.md#readme), [Flattened](docs/functions/jws_flattened_verify.flattenedVerify.md#readme), [General](docs/functions/jws_general_verify.generalVerify.md#readme)
61
- - JSON Web Key (JWK)
62
- - [Calculating JWK Thumbprint](docs/functions/jwk_thumbprint.calculateJwkThumbprint.md#readme)
63
- - [Calculating JWK Thumbprint URI](docs/functions/jwk_thumbprint.calculateJwkThumbprintUri.md#readme)
64
- - [Verification using a JWK Embedded in a JWS Header](docs/functions/jwk_embedded.EmbeddedJWK.md#readme)
65
- - JSON Web Key Set (JWKS)
66
- - [Verify using a local JWKSet](docs/functions/jwks_local.createLocalJWKSet.md#readme)
67
- - [Verify using a remote JWKSet](docs/functions/jwks_remote.createRemoteJWKSet.md#readme)
68
- - Key Pair or Secret Generation
69
- - [Asymmetric Key Pair Generation](docs/functions/key_generate_key_pair.generateKeyPair.md#readme)
70
- - [Symmetric Secret Generation](docs/functions/key_generate_secret.generateSecret.md#readme)
71
- - Key Export
72
- - [JWK Export](docs/functions/key_export.exportJWK.md#readme)
73
- - [Private Key Export](docs/functions/key_export.exportPKCS8.md#readme)
74
- - [Public Key Export](docs/functions/key_export.exportSPKI.md#readme)
75
- - Utilities
76
- - [Decoding Token's Protected Header](docs/functions/util_decode_protected_header.decodeProtectedHeader.md#readme)
77
- - [Decoding JWT Claims Set](docs/functions/util_decode_jwt.decodeJwt.md#readme)
78
- - [Unsecured JWT](docs/classes/jwt_unsecured.UnsecuredJWT.md#readme)
79
- - [JOSE Errors](docs/modules/util_errors.md#readme)
67
+ The `jose` modules supports signing and verification of JWS messages with arbitrary payloads in Compact, Flattened JSON, and General JSON serialization syntaxes.
68
+
69
+ - Signing - [Compact](docs/classes/jws_compact_sign.CompactSign.md), [Flattened JSON](docs/classes/jws_flattened_sign.FlattenedSign.md), [General JSON](docs/classes/jws_general_sign.GeneralSign.md)
70
+ - Verification - [Compact](docs/functions/jws_compact_verify.compactVerify.md), [Flattened JSON](docs/functions/jws_flattened_verify.flattenedVerify.md), [General JSON](docs/functions/jws_general_verify.generalVerify.md)
71
+ - [Verify using a remote JWKS](docs/functions/jwks_remote.createRemoteJWKSet.md)
72
+ - [Verify using a local JWKS](docs/functions/jwks_local.createLocalJWKSet.md)
73
+ - Utility functions
74
+ - [Decoding Token's Protected Header](docs/functions/util_decode_protected_header.decodeProtectedHeader.md)
75
+
76
+ ### JSON Web Encryption (JWE)
77
+
78
+ The `jose` modules supports encryption and decryption of JWE messages with arbitrary plaintext in Compact, Flattened JSON, and General JSON serialization syntaxes.
79
+
80
+ - Encryption - [Compact](docs/classes/jwe_compact_encrypt.CompactEncrypt.md), [Flattened JSON](docs/classes/jwe_flattened_encrypt.FlattenedEncrypt.md), [General JSON](docs/classes/jwe_general_encrypt.GeneralEncrypt.md)
81
+ - Decryption - [Compact](docs/functions/jwe_compact_decrypt.compactDecrypt.md), [Flattened JSON](docs/functions/jwe_flattened_decrypt.flattenedDecrypt.md), [General JSON](docs/functions/jwe_general_decrypt.generalDecrypt.md)
82
+ - Utility functions
83
+ - [Decoding Token's Protected Header](docs/functions/util_decode_protected_header.decodeProtectedHeader.md)
84
+
85
+ ### Other
86
+
87
+ The following are additional features and utilities provided by the `jose` module:
88
+
89
+ - [Calculating JWK Thumbprint](docs/functions/jwk_thumbprint.calculateJwkThumbprint.md)
90
+ - [Calculating JWK Thumbprint URI](docs/functions/jwk_thumbprint.calculateJwkThumbprintUri.md)
91
+ - [Verification using a JWK Embedded in a JWS Header](docs/functions/jwk_embedded.EmbeddedJWK.md)
92
+ - [Unsecured JWT](docs/classes/jwt_unsecured.UnsecuredJWT.md)
93
+ - [JOSE Errors](docs/modules/util_errors.md)
94
+
95
+ ## Implemented specifications
96
+
97
+ The `jose` module implements the following specifications:
98
+
99
+ - JSON Web Signature (JWS) - [RFC7515](https://www.rfc-editor.org/rfc/rfc7515)
100
+ - JSON Web Encryption (JWE) - [RFC7516](https://www.rfc-editor.org/rfc/rfc7516)
101
+ - JSON Web Key (JWK) - [RFC7517](https://www.rfc-editor.org/rfc/rfc7517)
102
+ - JSON Web Algorithms (JWA) - [RFC7518](https://www.rfc-editor.org/rfc/rfc7518)
103
+ - JSON Web Token (JWT) - [RFC7519](https://www.rfc-editor.org/rfc/rfc7519)
104
+ - JSON Web Key Thumbprint - [RFC7638](https://www.rfc-editor.org/rfc/rfc7638)
105
+ - JSON Web Key Thumbprint URI - [RFC9278](https://www.rfc-editor.org/rfc/rfc9278)
106
+ - JWS Unencoded Payload Option - [RFC7797](https://www.rfc-editor.org/rfc/rfc7797)
107
+ - CFRG Elliptic Curve ECDH and Signatures - [RFC8037](https://www.rfc-editor.org/rfc/rfc8037)
108
+ - secp256k1 EC Key curve support - [RFC8812](https://www.rfc-editor.org/rfc/rfc8812)
109
+
110
+ The algorithm implementations have been tested using test vectors from their respective specifications as well as [RFC7520](https://www.rfc-editor.org/rfc/rfc7520).
80
111
 
81
112
  ## Supported Runtimes
82
113
 
83
- The supported JavaScript runtimes include ones that support the utilized Web API globals and standard built-in objects or are Node.js
114
+ The `jose` module is compatible with JavaScript runtimes that support the utilized Web API globals and standard built-in objects or are Node.js.
84
115
 
85
- These are _(this is not an exhaustive list)_:
116
+ The following runtimes are supported _(this is not an exhaustive list)_:
86
117
  - [Bun](https://github.com/panva/jose/issues/471)
87
118
  - [Browsers](https://github.com/panva/jose/issues/263)
88
119
  - [Cloudflare Workers](https://github.com/panva/jose/issues/265)
@@ -91,36 +122,11 @@ These are _(this is not an exhaustive list)_:
91
122
  - [Node.js](https://github.com/panva/jose/issues/262)
92
123
  - [Vercel's Edge Runtime](https://github.com/panva/jose/issues/301)
93
124
 
94
- ## FAQ
125
+ Please note that certain algorithms may not be available depending on the runtime used. You can find a list of available algorithms for each runtime in the specific issue links provided above.
95
126
 
96
- #### Supported Versions
127
+ ## Supported Versions
97
128
 
98
129
  | Version | Security Fixes 🔑 | Other Bug Fixes 🐞 | New Features ⭐ |
99
130
  | ------- | --------- | -------- | -------- |
100
131
  | [v4.x](https://github.com/panva/jose/tree/v4.x) | ✅ | ✅ | ✅ |
101
132
  | [v3.x](https://github.com/panva/jose/tree/v3.x), [v2.x](https://github.com/panva/jose/tree/v2.x), [v1.x](https://github.com/panva/jose/tree/v1.x) | ✅ | ❌ | ❌ |
102
-
103
- #### Uint8Array?!
104
-
105
- - Whenever `Uint8Array` is a valid input, so is [`Buffer`](https://nodejs.org/api/buffer.html#buffer_buffer) since buffers are instances of Uint8Array.
106
- - Whenever `Uint8Array` is returned and you want a `Buffer` instead, use `Buffer.from(uint8array)`.
107
-
108
- #### Bundle Size, Package Size, Tree Shaking
109
-
110
- Yes the bundle size is on the larger side, that is because each module is actually published
111
- multiple times so that it can remain truly without dependencies and be universal / isomorphic.
112
-
113
- Nevertheless, since each module can be required independently and is fully tree-shakeable, the
114
- install size should not be a cause for concern.
115
-
116
- [spec-b64]: https://www.rfc-editor.org/rfc/rfc7797
117
- [spec-cookbook]: https://www.rfc-editor.org/rfc/rfc7520
118
- [spec-jwa]: https://www.rfc-editor.org/rfc/rfc7518
119
- [spec-jwe]: https://www.rfc-editor.org/rfc/rfc7516
120
- [spec-jwk]: https://www.rfc-editor.org/rfc/rfc7517
121
- [spec-jws]: https://www.rfc-editor.org/rfc/rfc7515
122
- [spec-jwt]: https://www.rfc-editor.org/rfc/rfc7519
123
- [spec-okp]: https://www.rfc-editor.org/rfc/rfc8037
124
- [spec-secp256k1]: https://www.rfc-editor.org/rfc/rfc8812
125
- [spec-thumbprint]: https://www.rfc-editor.org/rfc/rfc7638
126
- [spec-thumbprint-uri]: https://www.rfc-editor.org/rfc/rfc9278
@@ -58,7 +58,7 @@ export class GeneralSign {
58
58
  jws.payload = payload;
59
59
  }
60
60
  else if (jws.payload !== payload) {
61
- throw new JWSInvalid('inconsistent use of JWS Unencoded Payload Option (RFC7797)');
61
+ throw new JWSInvalid('inconsistent use of JWS Unencoded Payload (RFC7797)');
62
62
  }
63
63
  jws.signatures.push(rest);
64
64
  }
@@ -29,15 +29,26 @@ export default (protectedHeader, encodedPayload, options = {}) => {
29
29
  if (!isObject(payload)) {
30
30
  throw new JWTInvalid('JWT Claims Set must be a top-level JSON object');
31
31
  }
32
- const { issuer } = options;
32
+ const { requiredClaims = [], issuer, subject, audience, maxTokenAge } = options;
33
+ if (maxTokenAge !== undefined)
34
+ requiredClaims.push('iat');
35
+ if (audience !== undefined)
36
+ requiredClaims.push('aud');
37
+ if (subject !== undefined)
38
+ requiredClaims.push('sub');
39
+ if (issuer !== undefined)
40
+ requiredClaims.push('iss');
41
+ for (const claim of new Set(requiredClaims.reverse())) {
42
+ if (!(claim in payload)) {
43
+ throw new JWTClaimValidationFailed(`missing required "${claim}" claim`, claim, 'missing');
44
+ }
45
+ }
33
46
  if (issuer && !(Array.isArray(issuer) ? issuer : [issuer]).includes(payload.iss)) {
34
47
  throw new JWTClaimValidationFailed('unexpected "iss" claim value', 'iss', 'check_failed');
35
48
  }
36
- const { subject } = options;
37
49
  if (subject && payload.sub !== subject) {
38
50
  throw new JWTClaimValidationFailed('unexpected "sub" claim value', 'sub', 'check_failed');
39
51
  }
40
- const { audience } = options;
41
52
  if (audience &&
42
53
  !checkAudiencePresence(payload.aud, typeof audience === 'string' ? [audience] : audience)) {
43
54
  throw new JWTClaimValidationFailed('unexpected "aud" claim value', 'aud', 'check_failed');
@@ -58,7 +69,7 @@ export default (protectedHeader, encodedPayload, options = {}) => {
58
69
  }
59
70
  const { currentDate } = options;
60
71
  const now = epoch(currentDate || new Date());
61
- if ((payload.iat !== undefined || options.maxTokenAge) && typeof payload.iat !== 'number') {
72
+ if ((payload.iat !== undefined || maxTokenAge) && typeof payload.iat !== 'number') {
62
73
  throw new JWTClaimValidationFailed('"iat" claim must be a number', 'iat', 'invalid');
63
74
  }
64
75
  if (payload.nbf !== undefined) {
@@ -77,9 +88,9 @@ export default (protectedHeader, encodedPayload, options = {}) => {
77
88
  throw new JWTExpired('"exp" claim timestamp check failed', 'exp', 'check_failed');
78
89
  }
79
90
  }
80
- if (options.maxTokenAge) {
91
+ if (maxTokenAge) {
81
92
  const age = now - payload.iat;
82
- const max = typeof options.maxTokenAge === 'number' ? options.maxTokenAge : secs(options.maxTokenAge);
93
+ const max = typeof maxTokenAge === 'number' ? maxTokenAge : secs(maxTokenAge);
83
94
  if (age - tolerance > max) {
84
95
  throw new JWTExpired('"iat" claim timestamp check failed (too far in the past)', 'iat', 'check_failed');
85
96
  }
@@ -61,7 +61,7 @@ class GeneralSign {
61
61
  jws.payload = payload;
62
62
  }
63
63
  else if (jws.payload !== payload) {
64
- throw new errors_js_1.JWSInvalid('inconsistent use of JWS Unencoded Payload Option (RFC7797)');
64
+ throw new errors_js_1.JWSInvalid('inconsistent use of JWS Unencoded Payload (RFC7797)');
65
65
  }
66
66
  jws.signatures.push(rest);
67
67
  }
@@ -31,15 +31,26 @@ exports.default = (protectedHeader, encodedPayload, options = {}) => {
31
31
  if (!(0, is_object_js_1.default)(payload)) {
32
32
  throw new errors_js_1.JWTInvalid('JWT Claims Set must be a top-level JSON object');
33
33
  }
34
- const { issuer } = options;
34
+ const { requiredClaims = [], issuer, subject, audience, maxTokenAge } = options;
35
+ if (maxTokenAge !== undefined)
36
+ requiredClaims.push('iat');
37
+ if (audience !== undefined)
38
+ requiredClaims.push('aud');
39
+ if (subject !== undefined)
40
+ requiredClaims.push('sub');
41
+ if (issuer !== undefined)
42
+ requiredClaims.push('iss');
43
+ for (const claim of new Set(requiredClaims.reverse())) {
44
+ if (!(claim in payload)) {
45
+ throw new errors_js_1.JWTClaimValidationFailed(`missing required "${claim}" claim`, claim, 'missing');
46
+ }
47
+ }
35
48
  if (issuer && !(Array.isArray(issuer) ? issuer : [issuer]).includes(payload.iss)) {
36
49
  throw new errors_js_1.JWTClaimValidationFailed('unexpected "iss" claim value', 'iss', 'check_failed');
37
50
  }
38
- const { subject } = options;
39
51
  if (subject && payload.sub !== subject) {
40
52
  throw new errors_js_1.JWTClaimValidationFailed('unexpected "sub" claim value', 'sub', 'check_failed');
41
53
  }
42
- const { audience } = options;
43
54
  if (audience &&
44
55
  !checkAudiencePresence(payload.aud, typeof audience === 'string' ? [audience] : audience)) {
45
56
  throw new errors_js_1.JWTClaimValidationFailed('unexpected "aud" claim value', 'aud', 'check_failed');
@@ -60,7 +71,7 @@ exports.default = (protectedHeader, encodedPayload, options = {}) => {
60
71
  }
61
72
  const { currentDate } = options;
62
73
  const now = (0, epoch_js_1.default)(currentDate || new Date());
63
- if ((payload.iat !== undefined || options.maxTokenAge) && typeof payload.iat !== 'number') {
74
+ if ((payload.iat !== undefined || maxTokenAge) && typeof payload.iat !== 'number') {
64
75
  throw new errors_js_1.JWTClaimValidationFailed('"iat" claim must be a number', 'iat', 'invalid');
65
76
  }
66
77
  if (payload.nbf !== undefined) {
@@ -79,9 +90,9 @@ exports.default = (protectedHeader, encodedPayload, options = {}) => {
79
90
  throw new errors_js_1.JWTExpired('"exp" claim timestamp check failed', 'exp', 'check_failed');
80
91
  }
81
92
  }
82
- if (options.maxTokenAge) {
93
+ if (maxTokenAge) {
83
94
  const age = now - payload.iat;
84
- const max = typeof options.maxTokenAge === 'number' ? options.maxTokenAge : (0, secs_js_1.default)(options.maxTokenAge);
95
+ const max = typeof maxTokenAge === 'number' ? maxTokenAge : (0, secs_js_1.default)(maxTokenAge);
85
96
  if (age - tolerance > max) {
86
97
  throw new errors_js_1.JWTExpired('"iat" claim timestamp check failed (too far in the past)', 'iat', 'check_failed');
87
98
  }
@@ -58,7 +58,7 @@ export class GeneralSign {
58
58
  jws.payload = payload;
59
59
  }
60
60
  else if (jws.payload !== payload) {
61
- throw new JWSInvalid('inconsistent use of JWS Unencoded Payload Option (RFC7797)');
61
+ throw new JWSInvalid('inconsistent use of JWS Unencoded Payload (RFC7797)');
62
62
  }
63
63
  jws.signatures.push(rest);
64
64
  }
@@ -29,15 +29,26 @@ export default (protectedHeader, encodedPayload, options = {}) => {
29
29
  if (!isObject(payload)) {
30
30
  throw new JWTInvalid('JWT Claims Set must be a top-level JSON object');
31
31
  }
32
- const { issuer } = options;
32
+ const { requiredClaims = [], issuer, subject, audience, maxTokenAge } = options;
33
+ if (maxTokenAge !== undefined)
34
+ requiredClaims.push('iat');
35
+ if (audience !== undefined)
36
+ requiredClaims.push('aud');
37
+ if (subject !== undefined)
38
+ requiredClaims.push('sub');
39
+ if (issuer !== undefined)
40
+ requiredClaims.push('iss');
41
+ for (const claim of new Set(requiredClaims.reverse())) {
42
+ if (!(claim in payload)) {
43
+ throw new JWTClaimValidationFailed(`missing required "${claim}" claim`, claim, 'missing');
44
+ }
45
+ }
33
46
  if (issuer && !(Array.isArray(issuer) ? issuer : [issuer]).includes(payload.iss)) {
34
47
  throw new JWTClaimValidationFailed('unexpected "iss" claim value', 'iss', 'check_failed');
35
48
  }
36
- const { subject } = options;
37
49
  if (subject && payload.sub !== subject) {
38
50
  throw new JWTClaimValidationFailed('unexpected "sub" claim value', 'sub', 'check_failed');
39
51
  }
40
- const { audience } = options;
41
52
  if (audience &&
42
53
  !checkAudiencePresence(payload.aud, typeof audience === 'string' ? [audience] : audience)) {
43
54
  throw new JWTClaimValidationFailed('unexpected "aud" claim value', 'aud', 'check_failed');
@@ -58,7 +69,7 @@ export default (protectedHeader, encodedPayload, options = {}) => {
58
69
  }
59
70
  const { currentDate } = options;
60
71
  const now = epoch(currentDate || new Date());
61
- if ((payload.iat !== undefined || options.maxTokenAge) && typeof payload.iat !== 'number') {
72
+ if ((payload.iat !== undefined || maxTokenAge) && typeof payload.iat !== 'number') {
62
73
  throw new JWTClaimValidationFailed('"iat" claim must be a number', 'iat', 'invalid');
63
74
  }
64
75
  if (payload.nbf !== undefined) {
@@ -77,9 +88,9 @@ export default (protectedHeader, encodedPayload, options = {}) => {
77
88
  throw new JWTExpired('"exp" claim timestamp check failed', 'exp', 'check_failed');
78
89
  }
79
90
  }
80
- if (options.maxTokenAge) {
91
+ if (maxTokenAge) {
81
92
  const age = now - payload.iat;
82
- const max = typeof options.maxTokenAge === 'number' ? options.maxTokenAge : secs(options.maxTokenAge);
93
+ const max = typeof maxTokenAge === 'number' ? maxTokenAge : secs(maxTokenAge);
83
94
  if (age - tolerance > max) {
84
95
  throw new JWTExpired('"iat" claim timestamp check failed (too far in the past)', 'iat', 'check_failed');
85
96
  }
@@ -8,26 +8,16 @@ export interface CompactDecryptGetKey extends GetKeyFunction<CompactJWEHeaderPar
8
8
  /**
9
9
  * Decrypts a Compact JWE.
10
10
  *
11
- * @example Usage
12
- *
13
- * ```js
14
- * const jwe =
15
- * 'eyJhbGciOiJSU0EtT0FFUC0yNTYiLCJlbmMiOiJBMjU2R0NNIn0.nyQ19eq9ogh9wA7fFtnI2oouzy5_8b5DeLkoRMfi2yijgfTs2zEnayCEofz_qhnL-nwszabd9qUeHv0-IwvhhJJS7GUJOU3ikiIe42qcIAFme1A_Fo9CTxw4XTOy-I5qanl8So91u6hwfyN1VxAqVLsSE7_23EC-gfGEg_5znew9PyXXsOIE-K_HH7IQowRrlZ1X_bM_Liu53RzDpLDvRz59mp3S8L56YqpM8FexFGTGpEaoTcEIst375qncYt3-79IVR7gZN1RWsWgjPatfvVbnh74PglQcATSf3UUhaW0OAKn6q7r3PDx6DIKQ35bgHQg5QopuN00eIfLQL2trGw.W3grIVj5HVuAb76X.6PcuDe5D6ttWFYyv0oqqdDXfI2R8wBg1F2Q80UUA_Gv8eEimNWfxIWdLxrjzgQGSvIhxmFKuLM0.a93_Ug3uZHuczj70Zavx8Q'
16
- *
17
- * const { plaintext, protectedHeader } = await jose.compactDecrypt(jwe, privateKey)
18
- *
19
- * console.log(protectedHeader)
20
- * console.log(new TextDecoder().decode(plaintext))
21
- * ```
22
- *
23
11
  * @param jwe Compact JWE.
24
- * @param key Private Key or Secret to decrypt the JWE with.
12
+ * @param key Private Key or Secret to decrypt the JWE with. See
13
+ * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}.
25
14
  * @param options JWE Decryption options.
26
15
  */
27
16
  export declare function compactDecrypt(jwe: string | Uint8Array, key: KeyLike | Uint8Array, options?: DecryptOptions): Promise<CompactDecryptResult>;
28
17
  /**
29
18
  * @param jwe Compact JWE.
30
- * @param getKey Function resolving Private Key or Secret to decrypt the JWE with.
19
+ * @param getKey Function resolving Private Key or Secret to decrypt the JWE with. See
20
+ * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}.
31
21
  * @param options JWE Decryption options.
32
22
  */
33
23
  export declare function compactDecrypt<T extends KeyLike = KeyLike>(jwe: string | Uint8Array, getKey: CompactDecryptGetKey, options?: DecryptOptions): Promise<CompactDecryptResult & ResolvedKey<T>>;
@@ -1,18 +1,7 @@
1
1
  import type { KeyLike, JWEKeyManagementHeaderParameters, CompactJWEHeaderParameters, EncryptOptions } from '../../types';
2
2
  /**
3
- * The CompactEncrypt class is a utility for creating Compact JWE strings.
3
+ * The CompactEncrypt class is used to build and encrypt Compact JWE strings.
4
4
  *
5
- * @example Usage
6
- *
7
- * ```js
8
- * const jwe = await new jose.CompactEncrypt(
9
- * new TextEncoder().encode('It’s a dangerous business, Frodo, going out your door.'),
10
- * )
11
- * .setProtectedHeader({ alg: 'RSA-OAEP-256', enc: 'A256GCM' })
12
- * .encrypt(publicKey)
13
- *
14
- * console.log(jwe)
15
- * ```
16
5
  */
17
6
  export declare class CompactEncrypt {
18
7
  private _flattened;
@@ -53,7 +42,8 @@ export declare class CompactEncrypt {
53
42
  /**
54
43
  * Encrypts and resolves the value of the Compact JWE string.
55
44
  *
56
- * @param key Public Key or Secret to encrypt the JWE with.
45
+ * @param key Public Key or Secret to encrypt the JWE with. See
46
+ * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}.
57
47
  * @param options JWE Encryption options.
58
48
  */
59
49
  encrypt(key: KeyLike | Uint8Array, options?: EncryptOptions): Promise<string>;
@@ -8,36 +8,16 @@ export interface FlattenedDecryptGetKey extends GetKeyFunction<JWEHeaderParamete
8
8
  /**
9
9
  * Decrypts a Flattened JWE.
10
10
  *
11
- * @example Usage
12
- *
13
- * ```js
14
- * const jwe = {
15
- * ciphertext: '9EzjFISUyoG-ifC2mSihfP0DPC80yeyrxhTzKt1C_VJBkxeBG0MI4Te61Pk45RAGubUvBpU9jm4',
16
- * iv: '8Fy7A_IuoX5VXG9s',
17
- * tag: 'W76IYV6arGRuDSaSyWrQNg',
18
- * encrypted_key:
19
- * 'Z6eD4UK_yFb5ZoKvKkGAdqywEG_m0e4IYo0x8Vf30LAMJcsc-_zSgIeiF82teZyYi2YYduHKoqImk7MRnoPZOlEs0Q5BNK1OgBmSOhCE8DFyqh9Zh48TCTP6lmBQ52naqoUJFMtHzu-0LwZH26hxos0GP3Dt19O379MJB837TdKKa87skq0zHaVLAquRHOBF77GI54Bc7O49d8aOrSu1VEFGMThlW2caspPRiTSePDMDPq7_WGk50izRhB3Asl9wmP9wEeaTrkJKRnQj5ips1SAZ1hDBsqEQKKukxP1HtdcopHV5_qgwU8Hjm5EwSLMluMQuiE6hwlkXGOujZLVizA',
20
- * aad: 'VGhlIEZlbGxvd3NoaXAgb2YgdGhlIFJpbmc',
21
- * protected: 'eyJhbGciOiJSU0EtT0FFUC0yNTYiLCJlbmMiOiJBMjU2R0NNIn0',
22
- * }
23
- *
24
- * const { plaintext, protectedHeader, additionalAuthenticatedData } =
25
- * await jose.flattenedDecrypt(jwe, privateKey)
26
- *
27
- * console.log(protectedHeader)
28
- * const decoder = new TextDecoder()
29
- * console.log(decoder.decode(plaintext))
30
- * console.log(decoder.decode(additionalAuthenticatedData))
31
- * ```
32
- *
33
11
  * @param jwe Flattened JWE.
34
- * @param key Private Key or Secret to decrypt the JWE with.
12
+ * @param key Private Key or Secret to decrypt the JWE with. See
13
+ * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}.
35
14
  * @param options JWE Decryption options.
36
15
  */
37
16
  export declare function flattenedDecrypt(jwe: FlattenedJWE, key: KeyLike | Uint8Array, options?: DecryptOptions): Promise<FlattenedDecryptResult>;
38
17
  /**
39
18
  * @param jwe Flattened JWE.
40
- * @param getKey Function resolving Private Key or Secret to decrypt the JWE with.
19
+ * @param getKey Function resolving Private Key or Secret to decrypt the JWE with. See
20
+ * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}.
41
21
  * @param options JWE Decryption options.
42
22
  */
43
23
  export declare function flattenedDecrypt<T extends KeyLike = KeyLike>(jwe: FlattenedJWE, getKey: FlattenedDecryptGetKey, options?: DecryptOptions): Promise<FlattenedDecryptResult & ResolvedKey<T>>;
@@ -2,20 +2,8 @@ import type { KeyLike, FlattenedJWE, JWEHeaderParameters, JWEKeyManagementHeader
2
2
  /** @private */
3
3
  export declare const unprotected: unique symbol;
4
4
  /**
5
- * The FlattenedEncrypt class is a utility for creating Flattened JWE objects.
5
+ * The FlattenedEncrypt class is used to build and encrypt Flattened JWE objects.
6
6
  *
7
- * @example Usage
8
- *
9
- * ```js
10
- * const jwe = await new jose.FlattenedEncrypt(
11
- * new TextEncoder().encode('It’s a dangerous business, Frodo, going out your door.'),
12
- * )
13
- * .setProtectedHeader({ alg: 'RSA-OAEP-256', enc: 'A256GCM' })
14
- * .setAdditionalAuthenticatedData(encoder.encode('The Fellowship of the Ring'))
15
- * .encrypt(publicKey)
16
- *
17
- * console.log(jwe)
18
- * ```
19
7
  */
20
8
  export declare class FlattenedEncrypt {
21
9
  private _plaintext;
@@ -82,7 +70,8 @@ export declare class FlattenedEncrypt {
82
70
  /**
83
71
  * Encrypts and resolves the value of the Flattened JWE object.
84
72
  *
85
- * @param key Public Key or Secret to encrypt the JWE with.
73
+ * @param key Public Key or Secret to encrypt the JWE with. See
74
+ * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}.
86
75
  * @param options JWE Encryption options.
87
76
  */
88
77
  encrypt(key: KeyLike | Uint8Array, options?: EncryptOptions): Promise<FlattenedJWE>;
@@ -8,40 +8,16 @@ export interface GeneralDecryptGetKey extends GetKeyFunction<JWEHeaderParameters
8
8
  /**
9
9
  * Decrypts a General JWE.
10
10
  *
11
- * @example Usage
12
- *
13
- * ```js
14
- * const jwe = {
15
- * ciphertext: '9EzjFISUyoG-ifC2mSihfP0DPC80yeyrxhTzKt1C_VJBkxeBG0MI4Te61Pk45RAGubUvBpU9jm4',
16
- * iv: '8Fy7A_IuoX5VXG9s',
17
- * tag: 'W76IYV6arGRuDSaSyWrQNg',
18
- * aad: 'VGhlIEZlbGxvd3NoaXAgb2YgdGhlIFJpbmc',
19
- * protected: 'eyJhbGciOiJSU0EtT0FFUC0yNTYiLCJlbmMiOiJBMjU2R0NNIn0',
20
- * recipients: [
21
- * {
22
- * encrypted_key:
23
- * 'Z6eD4UK_yFb5ZoKvKkGAdqywEG_m0e4IYo0x8Vf30LAMJcsc-_zSgIeiF82teZyYi2YYduHKoqImk7MRnoPZOlEs0Q5BNK1OgBmSOhCE8DFyqh9Zh48TCTP6lmBQ52naqoUJFMtHzu-0LwZH26hxos0GP3Dt19O379MJB837TdKKa87skq0zHaVLAquRHOBF77GI54Bc7O49d8aOrSu1VEFGMThlW2caspPRiTSePDMDPq7_WGk50izRhB3Asl9wmP9wEeaTrkJKRnQj5ips1SAZ1hDBsqEQKKukxP1HtdcopHV5_qgwU8Hjm5EwSLMluMQuiE6hwlkXGOujZLVizA',
24
- * },
25
- * ],
26
- * }
27
- *
28
- * const { plaintext, protectedHeader, additionalAuthenticatedData } =
29
- * await jose.generalDecrypt(jwe, privateKey)
30
- *
31
- * console.log(protectedHeader)
32
- * const decoder = new TextDecoder()
33
- * console.log(decoder.decode(plaintext))
34
- * console.log(decoder.decode(additionalAuthenticatedData))
35
- * ```
36
- *
37
11
  * @param jwe General JWE.
38
- * @param key Private Key or Secret to decrypt the JWE with.
12
+ * @param key Private Key or Secret to decrypt the JWE with. See
13
+ * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}.
39
14
  * @param options JWE Decryption options.
40
15
  */
41
16
  export declare function generalDecrypt(jwe: GeneralJWE, key: KeyLike | Uint8Array, options?: DecryptOptions): Promise<GeneralDecryptResult>;
42
17
  /**
43
18
  * @param jwe General JWE.
44
- * @param getKey Function resolving Private Key or Secret to decrypt the JWE with.
19
+ * @param getKey Function resolving Private Key or Secret to decrypt the JWE with. See
20
+ * {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}.
45
21
  * @param options JWE Decryption options.
46
22
  */
47
23
  export declare function generalDecrypt<T extends KeyLike = KeyLike>(jwe: GeneralJWE, getKey: GeneralDecryptGetKey, options?: DecryptOptions): Promise<GeneralDecryptResult & ResolvedKey<T>>;
@@ -14,23 +14,8 @@ export interface Recipient {
14
14
  done(): GeneralEncrypt;
15
15
  }
16
16
  /**
17
- * The GeneralEncrypt class is a utility for creating General JWE objects.
17
+ * The GeneralEncrypt class is used to build and encrypt General JWE objects.
18
18
  *
19
- * @example Usage
20
- *
21
- * ```js
22
- * const jwe = await new jose.GeneralEncrypt(
23
- * new TextEncoder().encode('It’s a dangerous business, Frodo, going out your door.'),
24
- * )
25
- * .setProtectedHeader({ enc: 'A256GCM' })
26
- * .addRecipient(ecPublicKey)
27
- * .setUnprotectedHeader({ alg: 'ECDH-ES+A256KW' })
28
- * .addRecipient(rsaPublicKey)
29
- * .setUnprotectedHeader({ alg: 'RSA-OAEP-384' })
30
- * .encrypt()
31
- *
32
- * console.log(jwe)
33
- * ```
34
19
  */
35
20
  export declare class GeneralEncrypt {
36
21
  private _plaintext;
@@ -44,6 +29,7 @@ export declare class GeneralEncrypt {
44
29
  * Adds an additional recipient for the General JWE object.
45
30
  *
46
31
  * @param key Public Key or Secret to encrypt the Content Encryption Key for the recipient with.
32
+ * See {@link https://github.com/panva/jose/issues/210#jwe-alg Algorithm Key Requirements}.
47
33
  * @param options JWE Encryption options.
48
34
  */
49
35
  addRecipient(key: KeyLike | Uint8Array, options?: CritOption): Recipient;
@@ -5,19 +5,5 @@ import type { KeyLike, FlattenedJWSInput, JWSHeaderParameters } from '../types';
5
5
  * token's "jwk" (JSON Web Key) Header Parameter. It is recommended to combine this with the verify
6
6
  * function's `algorithms` option to define accepted JWS "alg" (Algorithm) Header Parameter values.
7
7
  *
8
- * @example Usage
9
- *
10
- * ```js
11
- * const jwt =
12
- * 'eyJqd2siOnsiY3J2IjoiUC0yNTYiLCJ4IjoiVU05ZzVuS25aWFlvdldBbE03NmNMejl2VG96UmpfX0NIVV9kT2wtZ09vRSIsInkiOiJkczhhZVF3MWwyY0RDQTdiQ2tPTnZ3REtwWEFidFhqdnFDbGVZSDhXc19VIiwia3R5IjoiRUMifSwiYWxnIjoiRVMyNTYifQ.eyJpc3MiOiJ1cm46ZXhhbXBsZTppc3N1ZXIiLCJhdWQiOiJ1cm46ZXhhbXBsZTphdWRpZW5jZSIsImlhdCI6MTYwNDU4MDc5NH0.60boak3_dErnW47ZPty1C0nrjeVq86EN_eK0GOq6K8w2OA0thKoBxFK4j-NuU9yZ_A9UKGxPT_G87DladBaV9g'
13
- *
14
- * const { payload, protectedHeader } = await jose.jwtVerify(jwt, jose.EmbeddedJWK, {
15
- * issuer: 'urn:example:issuer',
16
- * audience: 'urn:example:audience',
17
- * })
18
- *
19
- * console.log(protectedHeader)
20
- * console.log(payload)
21
- * ```
22
8
  */
23
9
  export declare function EmbeddedJWK<T extends KeyLike = KeyLike>(protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput): Promise<T>;