jose 4.11.1 → 4.11.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.
- package/README.md +1 -1
- package/dist/browser/lib/crypto_key.js +5 -5
- package/dist/browser/runtime/asn1.js +13 -8
- package/dist/browser/runtime/generate.js +13 -13
- package/dist/browser/runtime/jwk_to_key.js +12 -14
- package/dist/browser/runtime/subtle_dsa.js +3 -3
- package/dist/browser/util/errors.js +9 -9
- package/dist/deno/README.md +51 -0
- package/dist/node/cjs/lib/crypto_key.js +5 -5
- package/dist/node/cjs/runtime/flags.js +8 -0
- package/dist/node/cjs/runtime/is_key_like.js +1 -1
- package/dist/node/cjs/runtime/jwk_to_key.js +2 -6
- package/dist/node/cjs/runtime/key_to_jwk.js +2 -6
- package/dist/node/cjs/runtime/node_key.js +7 -12
- package/dist/node/cjs/runtime/verify.js +2 -6
- package/dist/node/cjs/util/errors.js +9 -9
- package/dist/node/esm/lib/crypto_key.js +5 -5
- package/dist/node/esm/runtime/flags.js +5 -0
- package/dist/node/esm/runtime/is_key_like.js +2 -2
- package/dist/node/esm/runtime/jwk_to_key.js +2 -6
- package/dist/node/esm/runtime/key_to_jwk.js +2 -6
- package/dist/node/esm/runtime/node_key.js +1 -6
- package/dist/node/esm/runtime/verify.js +2 -6
- package/dist/node/esm/util/errors.js +9 -9
- package/dist/types/jwk/embedded.d.ts +1 -1
- package/dist/types/util/decode_protected_header.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -71,13 +71,13 @@ export function checkSigCryptoKey(key, alg, ...usages) {
|
|
|
71
71
|
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
72
72
|
break;
|
|
73
73
|
}
|
|
74
|
-
case isCloudflareWorkers() && 'EdDSA': {
|
|
75
|
-
if (!isAlgorithm(key.algorithm, 'NODE-ED25519'))
|
|
76
|
-
throw unusable('NODE-ED25519');
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
74
|
case 'EdDSA': {
|
|
80
75
|
if (key.algorithm.name !== 'Ed25519' && key.algorithm.name !== 'Ed448') {
|
|
76
|
+
if (isCloudflareWorkers()) {
|
|
77
|
+
if (isAlgorithm(key.algorithm, 'NODE-ED25519'))
|
|
78
|
+
break;
|
|
79
|
+
throw unusable('Ed25519, Ed448, or NODE-ED25519');
|
|
80
|
+
}
|
|
81
81
|
throw unusable('Ed25519 or Ed448');
|
|
82
82
|
}
|
|
83
83
|
break;
|
|
@@ -57,7 +57,7 @@ const getNamedCurve = (keyData) => {
|
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
59
|
const genericImport = async (replace, keyFormat, pem, alg, options) => {
|
|
60
|
-
var _a;
|
|
60
|
+
var _a, _b;
|
|
61
61
|
let algorithm;
|
|
62
62
|
let keyUsages;
|
|
63
63
|
const keyData = new Uint8Array(atob(pem.replace(replace, ''))
|
|
@@ -108,12 +108,6 @@ const genericImport = async (replace, keyFormat, pem, alg, options) => {
|
|
|
108
108
|
keyUsages = isPublic ? [] : ['deriveBits'];
|
|
109
109
|
break;
|
|
110
110
|
}
|
|
111
|
-
case isCloudflareWorkers() && 'EdDSA': {
|
|
112
|
-
const namedCurve = getNamedCurve(keyData).toUpperCase();
|
|
113
|
-
algorithm = { name: `NODE-${namedCurve}`, namedCurve: `NODE-${namedCurve}` };
|
|
114
|
-
keyUsages = isPublic ? ['verify'] : ['sign'];
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
111
|
case 'EdDSA':
|
|
118
112
|
algorithm = { name: getNamedCurve(keyData) };
|
|
119
113
|
keyUsages = isPublic ? ['verify'] : ['sign'];
|
|
@@ -121,7 +115,18 @@ const genericImport = async (replace, keyFormat, pem, alg, options) => {
|
|
|
121
115
|
default:
|
|
122
116
|
throw new JOSENotSupported('Invalid or unsupported "alg" (Algorithm) value');
|
|
123
117
|
}
|
|
124
|
-
|
|
118
|
+
try {
|
|
119
|
+
return await crypto.subtle.importKey(keyFormat, keyData, algorithm, (_a = options === null || options === void 0 ? void 0 : options.extractable) !== null && _a !== void 0 ? _a : false, keyUsages);
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
if (algorithm.name === 'Ed25519' &&
|
|
123
|
+
(err === null || err === void 0 ? void 0 : err.name) === 'NotSupportedError' &&
|
|
124
|
+
isCloudflareWorkers()) {
|
|
125
|
+
algorithm = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' };
|
|
126
|
+
return await crypto.subtle.importKey(keyFormat, keyData, algorithm, (_b = options === null || options === void 0 ? void 0 : options.extractable) !== null && _b !== void 0 ? _b : false, keyUsages);
|
|
127
|
+
}
|
|
128
|
+
throw err;
|
|
129
|
+
}
|
|
125
130
|
};
|
|
126
131
|
export const fromPKCS8 = (pem, alg, options) => {
|
|
127
132
|
return genericImport(/(?:-----(?:BEGIN|END) PRIVATE KEY-----|\s)/g, 'pkcs8', pem, alg, options);
|
|
@@ -51,7 +51,7 @@ function getModulusLengthOption(options) {
|
|
|
51
51
|
return modulusLength;
|
|
52
52
|
}
|
|
53
53
|
export async function generateKeyPair(alg, options) {
|
|
54
|
-
var _a, _b, _c;
|
|
54
|
+
var _a, _b, _c, _d;
|
|
55
55
|
let algorithm;
|
|
56
56
|
let keyUsages;
|
|
57
57
|
switch (alg) {
|
|
@@ -101,17 +101,6 @@ export async function generateKeyPair(alg, options) {
|
|
|
101
101
|
algorithm = { name: 'ECDSA', namedCurve: 'P-521' };
|
|
102
102
|
keyUsages = ['sign', 'verify'];
|
|
103
103
|
break;
|
|
104
|
-
case isCloudflareWorkers() && 'EdDSA':
|
|
105
|
-
switch (options === null || options === void 0 ? void 0 : options.crv) {
|
|
106
|
-
case undefined:
|
|
107
|
-
case 'Ed25519':
|
|
108
|
-
algorithm = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' };
|
|
109
|
-
keyUsages = ['sign', 'verify'];
|
|
110
|
-
break;
|
|
111
|
-
default:
|
|
112
|
-
throw new JOSENotSupported('Invalid or unsupported crv option provided');
|
|
113
|
-
}
|
|
114
|
-
break;
|
|
115
104
|
case 'EdDSA':
|
|
116
105
|
keyUsages = ['sign', 'verify'];
|
|
117
106
|
const crv = (_a = options === null || options === void 0 ? void 0 : options.crv) !== null && _a !== void 0 ? _a : 'Ed25519';
|
|
@@ -149,5 +138,16 @@ export async function generateKeyPair(alg, options) {
|
|
|
149
138
|
default:
|
|
150
139
|
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
151
140
|
}
|
|
152
|
-
|
|
141
|
+
try {
|
|
142
|
+
return (await crypto.subtle.generateKey(algorithm, (_c = options === null || options === void 0 ? void 0 : options.extractable) !== null && _c !== void 0 ? _c : false, keyUsages));
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
if (algorithm.name === 'Ed25519' &&
|
|
146
|
+
(err === null || err === void 0 ? void 0 : err.name) === 'NotSupportedError' &&
|
|
147
|
+
isCloudflareWorkers()) {
|
|
148
|
+
algorithm = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' };
|
|
149
|
+
return (await crypto.subtle.generateKey(algorithm, (_d = options === null || options === void 0 ? void 0 : options.extractable) !== null && _d !== void 0 ? _d : false, keyUsages));
|
|
150
|
+
}
|
|
151
|
+
throw err;
|
|
152
|
+
}
|
|
153
153
|
}
|
|
@@ -99,19 +99,6 @@ function subtleMapping(jwk) {
|
|
|
99
99
|
}
|
|
100
100
|
break;
|
|
101
101
|
}
|
|
102
|
-
case isCloudflareWorkers() && 'OKP':
|
|
103
|
-
if (jwk.alg !== 'EdDSA') {
|
|
104
|
-
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
105
|
-
}
|
|
106
|
-
switch (jwk.crv) {
|
|
107
|
-
case 'Ed25519':
|
|
108
|
-
algorithm = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' };
|
|
109
|
-
keyUsages = jwk.d ? ['sign'] : ['verify'];
|
|
110
|
-
break;
|
|
111
|
-
default:
|
|
112
|
-
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
113
|
-
}
|
|
114
|
-
break;
|
|
115
102
|
case 'OKP': {
|
|
116
103
|
switch (jwk.alg) {
|
|
117
104
|
case 'EdDSA':
|
|
@@ -149,6 +136,17 @@ const parse = async (jwk) => {
|
|
|
149
136
|
const keyData = { ...jwk };
|
|
150
137
|
delete keyData.alg;
|
|
151
138
|
delete keyData.use;
|
|
152
|
-
|
|
139
|
+
try {
|
|
140
|
+
return await crypto.subtle.importKey('jwk', keyData, ...rest);
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
if (algorithm.name === 'Ed25519' &&
|
|
144
|
+
(err === null || err === void 0 ? void 0 : err.name) === 'NotSupportedError' &&
|
|
145
|
+
isCloudflareWorkers()) {
|
|
146
|
+
rest[0] = { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' };
|
|
147
|
+
return await crypto.subtle.importKey('jwk', keyData, ...rest);
|
|
148
|
+
}
|
|
149
|
+
throw err;
|
|
150
|
+
}
|
|
153
151
|
};
|
|
154
152
|
export default parse;
|
|
@@ -19,10 +19,10 @@ export default function subtleDsa(alg, algorithm) {
|
|
|
19
19
|
case 'ES384':
|
|
20
20
|
case 'ES512':
|
|
21
21
|
return { hash, name: 'ECDSA', namedCurve: algorithm.namedCurve };
|
|
22
|
-
case isCloudflareWorkers() && 'EdDSA':
|
|
23
|
-
const { namedCurve } = algorithm;
|
|
24
|
-
return { name: namedCurve, namedCurve };
|
|
25
22
|
case 'EdDSA':
|
|
23
|
+
if (isCloudflareWorkers() && algorithm.name === 'NODE-ED25519') {
|
|
24
|
+
return { name: 'NODE-ED25519', namedCurve: 'NODE-ED25519' };
|
|
25
|
+
}
|
|
26
26
|
return { name: algorithm.name };
|
|
27
27
|
default:
|
|
28
28
|
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export class JOSEError extends Error {
|
|
2
|
+
static get code() {
|
|
3
|
+
return 'ERR_JOSE_GENERIC';
|
|
4
|
+
}
|
|
2
5
|
constructor(message) {
|
|
3
6
|
var _a;
|
|
4
7
|
super(message);
|
|
@@ -6,31 +9,28 @@ export class JOSEError extends Error {
|
|
|
6
9
|
this.name = this.constructor.name;
|
|
7
10
|
(_a = Error.captureStackTrace) === null || _a === void 0 ? void 0 : _a.call(Error, this, this.constructor);
|
|
8
11
|
}
|
|
9
|
-
static get code() {
|
|
10
|
-
return 'ERR_JOSE_GENERIC';
|
|
11
|
-
}
|
|
12
12
|
}
|
|
13
13
|
export class JWTClaimValidationFailed extends JOSEError {
|
|
14
|
+
static get code() {
|
|
15
|
+
return 'ERR_JWT_CLAIM_VALIDATION_FAILED';
|
|
16
|
+
}
|
|
14
17
|
constructor(message, claim = 'unspecified', reason = 'unspecified') {
|
|
15
18
|
super(message);
|
|
16
19
|
this.code = 'ERR_JWT_CLAIM_VALIDATION_FAILED';
|
|
17
20
|
this.claim = claim;
|
|
18
21
|
this.reason = reason;
|
|
19
22
|
}
|
|
20
|
-
static get code() {
|
|
21
|
-
return 'ERR_JWT_CLAIM_VALIDATION_FAILED';
|
|
22
|
-
}
|
|
23
23
|
}
|
|
24
24
|
export class JWTExpired extends JOSEError {
|
|
25
|
+
static get code() {
|
|
26
|
+
return 'ERR_JWT_EXPIRED';
|
|
27
|
+
}
|
|
25
28
|
constructor(message, claim = 'unspecified', reason = 'unspecified') {
|
|
26
29
|
super(message);
|
|
27
30
|
this.code = 'ERR_JWT_EXPIRED';
|
|
28
31
|
this.claim = claim;
|
|
29
32
|
this.reason = reason;
|
|
30
33
|
}
|
|
31
|
-
static get code() {
|
|
32
|
-
return 'ERR_JWT_EXPIRED';
|
|
33
|
-
}
|
|
34
34
|
}
|
|
35
35
|
export class JOSEAlgNotAllowed extends JOSEError {
|
|
36
36
|
constructor() {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# `jose` Modules API Documentation
|
|
2
|
+
|
|
3
|
+
> "JSON Web Almost Everything" - JWA, JWS, JWE, JWT, JWK, JWKS with no dependencies using runtime's native crypto.
|
|
4
|
+
|
|
5
|
+
## [💗 Help the project](https://github.com/panva/jose/blob/v4.11.3/docs/https://github.com/sponsors/panva)
|
|
6
|
+
|
|
7
|
+
## Available modules
|
|
8
|
+
|
|
9
|
+
**`example`** Deno import
|
|
10
|
+
```js
|
|
11
|
+
import * as jose from 'https://deno.land/x/jose@v4.11.3/index.ts'
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
- JSON Web Tokens (JWT)
|
|
15
|
+
- [Signing](https://github.com/panva/jose/blob/v4.11.3/docs/classes/jwt_sign.SignJWT.md#readme)
|
|
16
|
+
- [Verification & JWT Claims Set Validation](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwt_verify.jwtVerify.md#readme)
|
|
17
|
+
- Encrypted JSON Web Tokens
|
|
18
|
+
- [Encryption](https://github.com/panva/jose/blob/v4.11.3/docs/classes/jwt_encrypt.EncryptJWT.md#readme)
|
|
19
|
+
- [Decryption & JWT Claims Set Validation](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwt_decrypt.jwtDecrypt.md#readme)
|
|
20
|
+
- Key Import
|
|
21
|
+
- [JWK Import](https://github.com/panva/jose/blob/v4.11.3/docs/functions/key_import.importJWK.md#readme)
|
|
22
|
+
- [Public Key Import (SPKI)](https://github.com/panva/jose/blob/v4.11.3/docs/functions/key_import.importSPKI.md#readme)
|
|
23
|
+
- [Public Key Import (X.509 Certificate)](https://github.com/panva/jose/blob/v4.11.3/docs/functions/key_import.importX509.md#readme)
|
|
24
|
+
- [Private Key Import (PKCS #8)](https://github.com/panva/jose/blob/v4.11.3/docs/functions/key_import.importPKCS8.md#readme)
|
|
25
|
+
- JSON Web Encryption (JWE)
|
|
26
|
+
- Encryption - [Compact](https://github.com/panva/jose/blob/v4.11.3/docs/classes/jwe_compact_encrypt.CompactEncrypt.md#readme), [Flattened](https://github.com/panva/jose/blob/v4.11.3/docs/classes/jwe_flattened_encrypt.FlattenedEncrypt.md#readme), [General](https://github.com/panva/jose/blob/v4.11.3/docs/classes/jwe_general_encrypt.GeneralEncrypt.md#readme)
|
|
27
|
+
- Decryption - [Compact](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwe_compact_decrypt.compactDecrypt.md#readme), [Flattened](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwe_flattened_decrypt.flattenedDecrypt.md#readme), [General](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwe_general_decrypt.generalDecrypt.md#readme)
|
|
28
|
+
- JSON Web Signature (JWS)
|
|
29
|
+
- Signing - [Compact](https://github.com/panva/jose/blob/v4.11.3/docs/classes/jws_compact_sign.CompactSign.md#readme), [Flattened](https://github.com/panva/jose/blob/v4.11.3/docs/classes/jws_flattened_sign.FlattenedSign.md#readme), [General](https://github.com/panva/jose/blob/v4.11.3/docs/classes/jws_general_sign.GeneralSign.md#readme)
|
|
30
|
+
- Verification - [Compact](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jws_compact_verify.compactVerify.md#readme), [Flattened](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jws_flattened_verify.flattenedVerify.md#readme), [General](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jws_general_verify.generalVerify.md#readme)
|
|
31
|
+
- JSON Web Key (JWK)
|
|
32
|
+
- [Calculating JWK Thumbprint](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwk_thumbprint.calculateJwkThumbprint.md#readme)
|
|
33
|
+
- [Calculating JWK Thumbprint URI](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwk_thumbprint.calculateJwkThumbprintUri.md#readme)
|
|
34
|
+
- [Verification using a JWK Embedded in a JWS Header](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwk_embedded.EmbeddedJWK.md#readme)
|
|
35
|
+
- JSON Web Key Set (JWKS)
|
|
36
|
+
- [Verify using a local JWKSet](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwks_local.createLocalJWKSet.md#readme)
|
|
37
|
+
- [Verify using a remote JWKSet](https://github.com/panva/jose/blob/v4.11.3/docs/functions/jwks_remote.createRemoteJWKSet.md#readme)
|
|
38
|
+
- Key Pair or Secret Generation
|
|
39
|
+
- [Asymmetric Key Pair Generation](https://github.com/panva/jose/blob/v4.11.3/docs/functions/key_generate_key_pair.generateKeyPair.md#readme)
|
|
40
|
+
- [Symmetric Secret Generation](https://github.com/panva/jose/blob/v4.11.3/docs/functions/key_generate_secret.generateSecret.md#readme)
|
|
41
|
+
- Key Export
|
|
42
|
+
- [JWK Export](https://github.com/panva/jose/blob/v4.11.3/docs/functions/key_export.exportJWK.md#readme)
|
|
43
|
+
- [Private Key Export](https://github.com/panva/jose/blob/v4.11.3/docs/functions/key_export.exportPKCS8.md#readme)
|
|
44
|
+
- [Public Key Export](https://github.com/panva/jose/blob/v4.11.3/docs/functions/key_export.exportSPKI.md#readme)
|
|
45
|
+
- Utilities
|
|
46
|
+
- [Decoding Token's Protected Header](https://github.com/panva/jose/blob/v4.11.3/docs/functions/util_decode_protected_header.decodeProtectedHeader.md#readme)
|
|
47
|
+
- [Decoding JWT Claims Set](https://github.com/panva/jose/blob/v4.11.3/docs/functions/util_decode_jwt.decodeJwt.md#readme)
|
|
48
|
+
- [Unsecured JWT](https://github.com/panva/jose/blob/v4.11.3/docs/classes/jwt_unsecured.UnsecuredJWT.md#readme)
|
|
49
|
+
- [JOSE Errors](https://github.com/panva/jose/blob/v4.11.3/docs/modules/util_errors.md#readme)
|
|
50
|
+
|
|
51
|
+
[support-sponsor]: https://github.com/sponsors/panva
|
|
@@ -74,13 +74,13 @@ function checkSigCryptoKey(key, alg, ...usages) {
|
|
|
74
74
|
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
75
75
|
break;
|
|
76
76
|
}
|
|
77
|
-
case (0, env_js_1.isCloudflareWorkers)() && 'EdDSA': {
|
|
78
|
-
if (!isAlgorithm(key.algorithm, 'NODE-ED25519'))
|
|
79
|
-
throw unusable('NODE-ED25519');
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
77
|
case 'EdDSA': {
|
|
83
78
|
if (key.algorithm.name !== 'Ed25519' && key.algorithm.name !== 'Ed448') {
|
|
79
|
+
if ((0, env_js_1.isCloudflareWorkers)()) {
|
|
80
|
+
if (isAlgorithm(key.algorithm, 'NODE-ED25519'))
|
|
81
|
+
break;
|
|
82
|
+
throw unusable('Ed25519, Ed448, or NODE-ED25519');
|
|
83
|
+
}
|
|
84
84
|
throw unusable('Ed25519 or Ed448');
|
|
85
85
|
}
|
|
86
86
|
break;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jwkImport = exports.jwkExport = exports.rsaPssParams = exports.oneShotCallback = void 0;
|
|
4
|
+
const [major, minor] = process.versions.node.split('.').map((str) => parseInt(str, 10));
|
|
5
|
+
exports.oneShotCallback = major >= 16 || (major === 15 && minor >= 13);
|
|
6
|
+
exports.rsaPssParams = !('electron' in process.versions) && (major >= 17 || (major === 16 && minor >= 9));
|
|
7
|
+
exports.jwkExport = major >= 16 || (major === 15 && minor >= 9);
|
|
8
|
+
exports.jwkImport = major >= 16 || (major === 15 && minor >= 12);
|
|
@@ -6,6 +6,6 @@ const is_key_object_js_1 = require("./is_key_object.js");
|
|
|
6
6
|
exports.default = (key) => (0, is_key_object_js_1.default)(key) || (0, webcrypto_js_1.isCryptoKey)(key);
|
|
7
7
|
const types = ['KeyObject'];
|
|
8
8
|
exports.types = types;
|
|
9
|
-
if (
|
|
9
|
+
if (globalThis.CryptoKey || (webcrypto_js_1.default === null || webcrypto_js_1.default === void 0 ? void 0 : webcrypto_js_1.default.CryptoKey)) {
|
|
10
10
|
types.push('CryptoKey');
|
|
11
11
|
}
|
|
@@ -7,13 +7,9 @@ const errors_js_1 = require("../util/errors.js");
|
|
|
7
7
|
const get_named_curve_js_1 = require("./get_named_curve.js");
|
|
8
8
|
const check_modulus_length_js_1 = require("./check_modulus_length.js");
|
|
9
9
|
const asn1_sequence_encoder_js_1 = require("./asn1_sequence_encoder.js");
|
|
10
|
-
const
|
|
11
|
-
.slice(1)
|
|
12
|
-
.split('.')
|
|
13
|
-
.map((str) => parseInt(str, 10));
|
|
14
|
-
const jwkImportSupported = major >= 16 || (major === 15 && minor >= 12);
|
|
10
|
+
const flags_js_1 = require("./flags.js");
|
|
15
11
|
const parse = (jwk) => {
|
|
16
|
-
if (
|
|
12
|
+
if (flags_js_1.jwkImport && jwk.kty !== 'oct') {
|
|
17
13
|
return jwk.d
|
|
18
14
|
? (0, crypto_1.createPrivateKey)({ format: 'jwk', key: jwk })
|
|
19
15
|
: (0, crypto_1.createPublicKey)({ format: 'jwk', key: jwk });
|
|
@@ -9,11 +9,7 @@ const webcrypto_js_1 = require("./webcrypto.js");
|
|
|
9
9
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
10
10
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
11
11
|
const is_key_like_js_1 = require("./is_key_like.js");
|
|
12
|
-
const
|
|
13
|
-
.slice(1)
|
|
14
|
-
.split('.')
|
|
15
|
-
.map((str) => parseInt(str, 10));
|
|
16
|
-
const jwkExportSupported = major >= 16 || (major === 15 && minor >= 9);
|
|
12
|
+
const flags_js_1 = require("./flags.js");
|
|
17
13
|
const keyToJWK = (key) => {
|
|
18
14
|
let keyObject;
|
|
19
15
|
if ((0, webcrypto_js_1.isCryptoKey)(key)) {
|
|
@@ -34,7 +30,7 @@ const keyToJWK = (key) => {
|
|
|
34
30
|
else {
|
|
35
31
|
throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types, 'Uint8Array'));
|
|
36
32
|
}
|
|
37
|
-
if (
|
|
33
|
+
if (flags_js_1.jwkExport) {
|
|
38
34
|
if (keyObject.type !== 'secret' &&
|
|
39
35
|
!['rsa', 'ec', 'ed25519', 'x25519', 'ed448', 'x448'].includes(keyObject.asymmetricKeyType)) {
|
|
40
36
|
throw new errors_js_1.JOSENotSupported('Unsupported key asymmetricKeyType');
|
|
@@ -4,12 +4,7 @@ const crypto_1 = require("crypto");
|
|
|
4
4
|
const get_named_curve_js_1 = require("./get_named_curve.js");
|
|
5
5
|
const errors_js_1 = require("../util/errors.js");
|
|
6
6
|
const check_modulus_length_js_1 = require("./check_modulus_length.js");
|
|
7
|
-
const
|
|
8
|
-
.slice(1)
|
|
9
|
-
.split('.')
|
|
10
|
-
.map((str) => parseInt(str, 10));
|
|
11
|
-
const electron = 'electron' in process.versions;
|
|
12
|
-
const rsaPssParams = !electron && (major >= 17 || (major === 16 && minor >= 9));
|
|
7
|
+
const flags_js_1 = require("./flags.js");
|
|
13
8
|
const PSS = {
|
|
14
9
|
padding: crypto_1.constants.RSA_PKCS1_PSS_PADDING,
|
|
15
10
|
saltLength: crypto_1.constants.RSA_PSS_SALTLEN_DIGEST,
|
|
@@ -35,9 +30,9 @@ function keyForCrypto(alg, key) {
|
|
|
35
30
|
}
|
|
36
31
|
(0, check_modulus_length_js_1.default)(key, alg);
|
|
37
32
|
return key;
|
|
38
|
-
case rsaPssParams && 'PS256':
|
|
39
|
-
case rsaPssParams && 'PS384':
|
|
40
|
-
case rsaPssParams && 'PS512':
|
|
33
|
+
case flags_js_1.rsaPssParams && 'PS256':
|
|
34
|
+
case flags_js_1.rsaPssParams && 'PS384':
|
|
35
|
+
case flags_js_1.rsaPssParams && 'PS512':
|
|
41
36
|
if (key.asymmetricKeyType === 'rsa-pss') {
|
|
42
37
|
const { hashAlgorithm, mgf1HashAlgorithm, saltLength } = key.asymmetricKeyDetails;
|
|
43
38
|
const length = parseInt(alg.slice(-3), 10);
|
|
@@ -54,9 +49,9 @@ function keyForCrypto(alg, key) {
|
|
|
54
49
|
}
|
|
55
50
|
(0, check_modulus_length_js_1.default)(key, alg);
|
|
56
51
|
return { key, ...PSS };
|
|
57
|
-
case !rsaPssParams && 'PS256':
|
|
58
|
-
case !rsaPssParams && 'PS384':
|
|
59
|
-
case !rsaPssParams && 'PS512':
|
|
52
|
+
case !flags_js_1.rsaPssParams && 'PS256':
|
|
53
|
+
case !flags_js_1.rsaPssParams && 'PS384':
|
|
54
|
+
case !flags_js_1.rsaPssParams && 'PS512':
|
|
60
55
|
if (key.asymmetricKeyType !== 'rsa') {
|
|
61
56
|
throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be rsa');
|
|
62
57
|
}
|
|
@@ -6,13 +6,9 @@ const dsa_digest_js_1 = require("./dsa_digest.js");
|
|
|
6
6
|
const node_key_js_1 = require("./node_key.js");
|
|
7
7
|
const sign_js_1 = require("./sign.js");
|
|
8
8
|
const get_sign_verify_key_js_1 = require("./get_sign_verify_key.js");
|
|
9
|
-
const
|
|
10
|
-
.slice(1)
|
|
11
|
-
.split('.')
|
|
12
|
-
.map((str) => parseInt(str, 10));
|
|
13
|
-
const oneShotCallbackSupported = major >= 16 || (major === 15 && minor >= 13);
|
|
9
|
+
const flags_js_1 = require("./flags.js");
|
|
14
10
|
let oneShotVerify;
|
|
15
|
-
if (crypto.verify.length > 4 &&
|
|
11
|
+
if (crypto.verify.length > 4 && flags_js_1.oneShotCallback) {
|
|
16
12
|
oneShotVerify = (0, util_1.promisify)(crypto.verify);
|
|
17
13
|
}
|
|
18
14
|
else {
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.JWSSignatureVerificationFailed = exports.JWKSTimeout = exports.JWKSMultipleMatchingKeys = exports.JWKSNoMatchingKey = exports.JWKSInvalid = exports.JWKInvalid = exports.JWTInvalid = exports.JWSInvalid = exports.JWEInvalid = exports.JWEDecryptionFailed = exports.JOSENotSupported = exports.JOSEAlgNotAllowed = exports.JWTExpired = exports.JWTClaimValidationFailed = exports.JOSEError = void 0;
|
|
4
4
|
class JOSEError extends Error {
|
|
5
|
+
static get code() {
|
|
6
|
+
return 'ERR_JOSE_GENERIC';
|
|
7
|
+
}
|
|
5
8
|
constructor(message) {
|
|
6
9
|
var _a;
|
|
7
10
|
super(message);
|
|
@@ -9,33 +12,30 @@ class JOSEError extends Error {
|
|
|
9
12
|
this.name = this.constructor.name;
|
|
10
13
|
(_a = Error.captureStackTrace) === null || _a === void 0 ? void 0 : _a.call(Error, this, this.constructor);
|
|
11
14
|
}
|
|
12
|
-
static get code() {
|
|
13
|
-
return 'ERR_JOSE_GENERIC';
|
|
14
|
-
}
|
|
15
15
|
}
|
|
16
16
|
exports.JOSEError = JOSEError;
|
|
17
17
|
class JWTClaimValidationFailed extends JOSEError {
|
|
18
|
+
static get code() {
|
|
19
|
+
return 'ERR_JWT_CLAIM_VALIDATION_FAILED';
|
|
20
|
+
}
|
|
18
21
|
constructor(message, claim = 'unspecified', reason = 'unspecified') {
|
|
19
22
|
super(message);
|
|
20
23
|
this.code = 'ERR_JWT_CLAIM_VALIDATION_FAILED';
|
|
21
24
|
this.claim = claim;
|
|
22
25
|
this.reason = reason;
|
|
23
26
|
}
|
|
24
|
-
static get code() {
|
|
25
|
-
return 'ERR_JWT_CLAIM_VALIDATION_FAILED';
|
|
26
|
-
}
|
|
27
27
|
}
|
|
28
28
|
exports.JWTClaimValidationFailed = JWTClaimValidationFailed;
|
|
29
29
|
class JWTExpired extends JOSEError {
|
|
30
|
+
static get code() {
|
|
31
|
+
return 'ERR_JWT_EXPIRED';
|
|
32
|
+
}
|
|
30
33
|
constructor(message, claim = 'unspecified', reason = 'unspecified') {
|
|
31
34
|
super(message);
|
|
32
35
|
this.code = 'ERR_JWT_EXPIRED';
|
|
33
36
|
this.claim = claim;
|
|
34
37
|
this.reason = reason;
|
|
35
38
|
}
|
|
36
|
-
static get code() {
|
|
37
|
-
return 'ERR_JWT_EXPIRED';
|
|
38
|
-
}
|
|
39
39
|
}
|
|
40
40
|
exports.JWTExpired = JWTExpired;
|
|
41
41
|
class JOSEAlgNotAllowed extends JOSEError {
|
|
@@ -71,13 +71,13 @@ export function checkSigCryptoKey(key, alg, ...usages) {
|
|
|
71
71
|
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
72
72
|
break;
|
|
73
73
|
}
|
|
74
|
-
case isCloudflareWorkers() && 'EdDSA': {
|
|
75
|
-
if (!isAlgorithm(key.algorithm, 'NODE-ED25519'))
|
|
76
|
-
throw unusable('NODE-ED25519');
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
74
|
case 'EdDSA': {
|
|
80
75
|
if (key.algorithm.name !== 'Ed25519' && key.algorithm.name !== 'Ed448') {
|
|
76
|
+
if (isCloudflareWorkers()) {
|
|
77
|
+
if (isAlgorithm(key.algorithm, 'NODE-ED25519'))
|
|
78
|
+
break;
|
|
79
|
+
throw unusable('Ed25519, Ed448, or NODE-ED25519');
|
|
80
|
+
}
|
|
81
81
|
throw unusable('Ed25519 or Ed448');
|
|
82
82
|
}
|
|
83
83
|
break;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
const [major, minor] = process.versions.node.split('.').map((str) => parseInt(str, 10));
|
|
2
|
+
export const oneShotCallback = major >= 16 || (major === 15 && minor >= 13);
|
|
3
|
+
export const rsaPssParams = !('electron' in process.versions) && (major >= 17 || (major === 16 && minor >= 9));
|
|
4
|
+
export const jwkExport = major >= 16 || (major === 15 && minor >= 9);
|
|
5
|
+
export const jwkImport = major >= 16 || (major === 15 && minor >= 12);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isCryptoKey } from './webcrypto.js';
|
|
1
|
+
import webcrypto, { isCryptoKey } from './webcrypto.js';
|
|
2
2
|
import isKeyObject from './is_key_object.js';
|
|
3
3
|
export default (key) => isKeyObject(key) || isCryptoKey(key);
|
|
4
4
|
const types = ['KeyObject'];
|
|
5
|
-
if (
|
|
5
|
+
if (globalThis.CryptoKey || (webcrypto === null || webcrypto === void 0 ? void 0 : webcrypto.CryptoKey)) {
|
|
6
6
|
types.push('CryptoKey');
|
|
7
7
|
}
|
|
8
8
|
export { types };
|
|
@@ -5,13 +5,9 @@ import { JOSENotSupported } from '../util/errors.js';
|
|
|
5
5
|
import { setCurve } from './get_named_curve.js';
|
|
6
6
|
import { setModulusLength } from './check_modulus_length.js';
|
|
7
7
|
import Asn1SequenceEncoder from './asn1_sequence_encoder.js';
|
|
8
|
-
|
|
9
|
-
.slice(1)
|
|
10
|
-
.split('.')
|
|
11
|
-
.map((str) => parseInt(str, 10));
|
|
12
|
-
const jwkImportSupported = major >= 16 || (major === 15 && minor >= 12);
|
|
8
|
+
import { jwkImport } from './flags.js';
|
|
13
9
|
const parse = (jwk) => {
|
|
14
|
-
if (
|
|
10
|
+
if (jwkImport && jwk.kty !== 'oct') {
|
|
15
11
|
return jwk.d
|
|
16
12
|
? createPrivateKey({ format: 'jwk', key: jwk })
|
|
17
13
|
: createPublicKey({ format: 'jwk', key: jwk });
|
|
@@ -7,11 +7,7 @@ import { isCryptoKey } from './webcrypto.js';
|
|
|
7
7
|
import isKeyObject from './is_key_object.js';
|
|
8
8
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
9
9
|
import { types } from './is_key_like.js';
|
|
10
|
-
|
|
11
|
-
.slice(1)
|
|
12
|
-
.split('.')
|
|
13
|
-
.map((str) => parseInt(str, 10));
|
|
14
|
-
const jwkExportSupported = major >= 16 || (major === 15 && minor >= 9);
|
|
10
|
+
import { jwkExport } from './flags.js';
|
|
15
11
|
const keyToJWK = (key) => {
|
|
16
12
|
let keyObject;
|
|
17
13
|
if (isCryptoKey(key)) {
|
|
@@ -32,7 +28,7 @@ const keyToJWK = (key) => {
|
|
|
32
28
|
else {
|
|
33
29
|
throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
|
|
34
30
|
}
|
|
35
|
-
if (
|
|
31
|
+
if (jwkExport) {
|
|
36
32
|
if (keyObject.type !== 'secret' &&
|
|
37
33
|
!['rsa', 'ec', 'ed25519', 'x25519', 'ed448', 'x448'].includes(keyObject.asymmetricKeyType)) {
|
|
38
34
|
throw new JOSENotSupported('Unsupported key asymmetricKeyType');
|
|
@@ -2,12 +2,7 @@ import { constants } from 'crypto';
|
|
|
2
2
|
import getNamedCurve from './get_named_curve.js';
|
|
3
3
|
import { JOSENotSupported } from '../util/errors.js';
|
|
4
4
|
import checkModulusLength from './check_modulus_length.js';
|
|
5
|
-
|
|
6
|
-
.slice(1)
|
|
7
|
-
.split('.')
|
|
8
|
-
.map((str) => parseInt(str, 10));
|
|
9
|
-
const electron = 'electron' in process.versions;
|
|
10
|
-
const rsaPssParams = !electron && (major >= 17 || (major === 16 && minor >= 9));
|
|
5
|
+
import { rsaPssParams } from './flags.js';
|
|
11
6
|
const PSS = {
|
|
12
7
|
padding: constants.RSA_PKCS1_PSS_PADDING,
|
|
13
8
|
saltLength: constants.RSA_PSS_SALTLEN_DIGEST,
|
|
@@ -4,13 +4,9 @@ import nodeDigest from './dsa_digest.js';
|
|
|
4
4
|
import nodeKey from './node_key.js';
|
|
5
5
|
import sign from './sign.js';
|
|
6
6
|
import getVerifyKey from './get_sign_verify_key.js';
|
|
7
|
-
|
|
8
|
-
.slice(1)
|
|
9
|
-
.split('.')
|
|
10
|
-
.map((str) => parseInt(str, 10));
|
|
11
|
-
const oneShotCallbackSupported = major >= 16 || (major === 15 && minor >= 13);
|
|
7
|
+
import { oneShotCallback } from './flags.js';
|
|
12
8
|
let oneShotVerify;
|
|
13
|
-
if (crypto.verify.length > 4 &&
|
|
9
|
+
if (crypto.verify.length > 4 && oneShotCallback) {
|
|
14
10
|
oneShotVerify = promisify(crypto.verify);
|
|
15
11
|
}
|
|
16
12
|
else {
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export class JOSEError extends Error {
|
|
2
|
+
static get code() {
|
|
3
|
+
return 'ERR_JOSE_GENERIC';
|
|
4
|
+
}
|
|
2
5
|
constructor(message) {
|
|
3
6
|
var _a;
|
|
4
7
|
super(message);
|
|
@@ -6,31 +9,28 @@ export class JOSEError extends Error {
|
|
|
6
9
|
this.name = this.constructor.name;
|
|
7
10
|
(_a = Error.captureStackTrace) === null || _a === void 0 ? void 0 : _a.call(Error, this, this.constructor);
|
|
8
11
|
}
|
|
9
|
-
static get code() {
|
|
10
|
-
return 'ERR_JOSE_GENERIC';
|
|
11
|
-
}
|
|
12
12
|
}
|
|
13
13
|
export class JWTClaimValidationFailed extends JOSEError {
|
|
14
|
+
static get code() {
|
|
15
|
+
return 'ERR_JWT_CLAIM_VALIDATION_FAILED';
|
|
16
|
+
}
|
|
14
17
|
constructor(message, claim = 'unspecified', reason = 'unspecified') {
|
|
15
18
|
super(message);
|
|
16
19
|
this.code = 'ERR_JWT_CLAIM_VALIDATION_FAILED';
|
|
17
20
|
this.claim = claim;
|
|
18
21
|
this.reason = reason;
|
|
19
22
|
}
|
|
20
|
-
static get code() {
|
|
21
|
-
return 'ERR_JWT_CLAIM_VALIDATION_FAILED';
|
|
22
|
-
}
|
|
23
23
|
}
|
|
24
24
|
export class JWTExpired extends JOSEError {
|
|
25
|
+
static get code() {
|
|
26
|
+
return 'ERR_JWT_EXPIRED';
|
|
27
|
+
}
|
|
25
28
|
constructor(message, claim = 'unspecified', reason = 'unspecified') {
|
|
26
29
|
super(message);
|
|
27
30
|
this.code = 'ERR_JWT_EXPIRED';
|
|
28
31
|
this.claim = claim;
|
|
29
32
|
this.reason = reason;
|
|
30
33
|
}
|
|
31
|
-
static get code() {
|
|
32
|
-
return 'ERR_JWT_EXPIRED';
|
|
33
|
-
}
|
|
34
34
|
}
|
|
35
35
|
export class JOSEAlgNotAllowed extends JOSEError {
|
|
36
36
|
constructor() {
|
|
@@ -3,7 +3,7 @@ import type { FlattenedJWSInput, JWSHeaderParameters } from '../types';
|
|
|
3
3
|
* EmbeddedJWK is an implementation of a GetKeyFunction intended to be used with the JWS/JWT verify
|
|
4
4
|
* operations whenever you need to opt-in to verify signatures with a public key embedded in the
|
|
5
5
|
* token's "jwk" (JSON Web Key) Header Parameter. It is recommended to combine this with the verify
|
|
6
|
-
* algorithms option to
|
|
6
|
+
* function's `algorithms` option to define accepted JWS "alg" (Algorithm) Header Parameter values.
|
|
7
7
|
*
|
|
8
8
|
* @example Usage
|
|
9
9
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { JWSHeaderParameters, JWEHeaderParameters } from '../types';
|
|
2
|
-
export
|
|
2
|
+
export type ProtectedHeaderParameters = JWSHeaderParameters & JWEHeaderParameters;
|
|
3
3
|
/**
|
|
4
4
|
* Decodes the Protected Header of a JWE/JWS/JWT token utilizing any JOSE serialization.
|
|
5
5
|
*
|
package/package.json
CHANGED