unjwt 0.7.3 → 0.8.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.
- package/README.md +4 -4
- package/dist/THIRD-PARTY-LICENSES.md.gz +0 -0
- package/dist/_chunks/_codec-jwe.d.mts +170 -0
- package/dist/_chunks/_codec-jws.mjs +878 -0
- package/dist/_chunks/_internal.d.mts +7 -0
- package/dist/_chunks/_jwt-internal.mjs +388 -0
- package/dist/_chunks/algorithms.mjs +186 -0
- package/dist/_chunks/index.d.mts +500 -808
- package/dist/_chunks/index2.d.mts +11 -0
- package/dist/_chunks/index3.d.mts +108 -0
- package/dist/_chunks/jwe.d.mts +43 -66
- package/dist/_chunks/jwe.mjs +384 -252
- package/dist/_chunks/jwk.mjs +1374 -607
- package/dist/_chunks/jws.d.mts +48 -103
- package/dist/_chunks/jws.mjs +372 -184
- package/dist/_chunks/jwt.d.mts +56 -0
- package/dist/_chunks/libs/unsecure.d.mts +164 -85
- package/dist/_chunks/libs/unsecure.mjs +390 -123
- package/dist/adapters/elysia/index.d.mts +33 -171
- package/dist/adapters/elysia/index.mjs +106 -575
- package/dist/adapters/h3v1/index.d.mts +59 -344
- package/dist/adapters/h3v1/index.mjs +114 -619
- package/dist/adapters/h3v2/index.d.mts +58 -326
- package/dist/adapters/h3v2/index.mjs +108 -611
- package/dist/core/internal.d.mts +2 -0
- package/dist/core/internal.mjs +1 -0
- package/dist/core/jwe.d.mts +4 -2
- package/dist/core/jwe.mjs +4 -2
- package/dist/core/jwk.d.mts +183 -371
- package/dist/core/jwk.mjs +4 -3
- package/dist/core/jws.d.mts +4 -2
- package/dist/core/jws.mjs +4 -2
- package/dist/core/utils/index.d.mts +5 -158
- package/dist/core/utils/index.mjs +12 -3
- package/dist/index.d.mts +8 -4
- package/dist/index.mjs +6 -3
- package/package.json +29 -30
- package/dist/_chunks/_internal.mjs +0 -88
- package/dist/_chunks/utils.mjs +0 -392
package/dist/core/jwk.d.mts
CHANGED
|
@@ -1,387 +1,199 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* `
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* - `
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
* - `modulusLength?` — RSA only; defaults to `2048`.
|
|
70
|
-
* - `publicExponent?` — RSA only; defaults to `0x010001`.
|
|
71
|
-
* - `namedCurve?` — EC and OKP only; defaults to `"P-256"` for EC and `"Ed25519"` for OKP.
|
|
72
|
-
* - JWK metadata (`kid`, `use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is merged onto the
|
|
73
|
-
* result. `alg`, `kty`, `key_ops`, and `ext` are managed by the library and cannot
|
|
74
|
-
* be overridden here.
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```ts
|
|
78
|
-
* // HS256 → JWK_oct
|
|
79
|
-
* const hmac = await generateJWK("HS256");
|
|
80
|
-
*
|
|
81
|
-
* // RS256 → { privateKey: JWK_RSA_Private; publicKey: JWK_RSA_Public }
|
|
82
|
-
* const rsa = await generateJWK("RS256", { modulusLength: 2048, kid: "rsa-1" });
|
|
83
|
-
*
|
|
84
|
-
* // ES256 with explicit metadata
|
|
85
|
-
* const { privateKey, publicKey } = await generateJWK("ES256", { kid: "ec-1", use: "sig" });
|
|
86
|
-
* ```
|
|
87
|
-
*/
|
|
88
|
-
declare function generateJWK<TAlg extends GenerateKeyAlgorithm>(alg: TAlg, options?: GenerateJWKOptions & Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">): Promise<GenerateJWKReturn<TAlg>>;
|
|
89
|
-
/**
|
|
90
|
-
* Derives a key from a password using PBKDF2 as specified by PBES2 algorithms.
|
|
91
|
-
*
|
|
92
|
-
* @param password The password to derive the key from (string or Uint8Array).
|
|
93
|
-
* @param alg The PBES2 algorithm identifier (e.g., "PBES2-HS256+A128KW").
|
|
94
|
-
* @param options Configuration options including salt and iterations.
|
|
95
|
-
* @returns A Promise resolving to the derived key (CryptoKey) or its JWK_oct representation.
|
|
96
|
-
*/
|
|
97
|
-
declare function deriveKeyFromPassword<TAlg extends JWK_PBES2, TOptions extends DeriveKeyOptions>(password: string | Uint8Array<ArrayBuffer>, alg: TAlg, options: TOptions): Promise<DeriveKeyReturn<TOptions>>;
|
|
98
|
-
/**
|
|
99
|
-
* Derives a `JWK_oct` from a password using PBKDF2 as specified by PBES2
|
|
100
|
-
* (RFC 7518 §4.8). The resulting JWK's `alg` is the underlying AES-KW variant
|
|
101
|
-
* (`A128KW` / `A192KW` / `A256KW`), ready to use with {@link wrapKey} /
|
|
102
|
-
* {@link unwrapKey}.
|
|
103
|
-
*
|
|
104
|
-
* For a `CryptoKey` result without JWK metadata, use {@link deriveKeyFromPassword}.
|
|
105
|
-
*
|
|
106
|
-
* @param password The password to derive the key from (string or `Uint8Array`).
|
|
107
|
-
* @param alg The PBES2 algorithm identifier (e.g. `"PBES2-HS256+A128KW"`).
|
|
108
|
-
* @param options
|
|
109
|
-
* - `salt` — PBKDF2 salt input (`p2s`). Must be at least 8 octets (RFC 7518 §4.8.1.1).
|
|
110
|
-
* - `iterations` — PBKDF2 iteration count (`p2c`). Must be a positive integer.
|
|
111
|
-
* - `keyUsage?` — defaults to `["wrapKey", "unwrapKey"]`.
|
|
112
|
-
* - JWK metadata (`kid`, `use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is merged onto the
|
|
113
|
-
* result. `alg`, `kty`, `key_ops`, and `ext` are managed by the library and cannot
|
|
114
|
-
* be overridden here.
|
|
115
|
-
*/
|
|
116
|
-
declare function deriveJWKFromPassword(password: string | Uint8Array<ArrayBuffer>, alg: JWK_PBES2, options: Omit<DeriveKeyOptions, "toJWK" | "extractable"> & Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">): Promise<JWK_oct>;
|
|
117
|
-
/**
|
|
118
|
-
* Default JWK import cache backed by a WeakMap.
|
|
119
|
-
*
|
|
120
|
-
* The outer WeakMap is keyed by the JWK object reference — a cache hit only
|
|
121
|
-
* occurs when the exact same object variable is passed to {@link importKey}.
|
|
122
|
-
* Reconstructing a structurally identical JWK (e.g. `{ ...jwk }`) will miss
|
|
123
|
-
* the cache.
|
|
124
|
-
*
|
|
125
|
-
* The inner `Record<string, CryptoKey>` uses a plain object rather than `Map`
|
|
126
|
-
* because typical entries have 1–2 algorithm strings per key. V8 applies
|
|
127
|
-
* hidden-class optimisation to small plain objects, making property access
|
|
128
|
-
* faster than `Map.get()` at this cardinality.
|
|
129
|
-
*/
|
|
130
|
-
declare class WeakMapJWKCache implements JWKCacheAdapter {
|
|
1
|
+
import { UnsecureError, UnsecureErrorCode } from "../_chunks/libs/unsecure.mjs";
|
|
2
|
+
import { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, ECDHEphemeralKeyInput, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, GeneratedJWKMap, GeneratedKeyMap, ImportedKey, ImportedKeyMap, JWK, JWKAlgorithm, JWKCacheAdapter, JWKLookupFunction, JWKLookupFunctionHeader, JWKMetadata, JWKPEMAlgorithm, JWKParameters, JWKSet, JWK_AES_CBC_HMAC, JWK_AES_GCM, JWK_AES_GCM_KW, JWK_AES_KW, JWK_Asymmetric, JWK_Asymmetric_Algorithm, JWK_EC, JWK_ECDH_ES, JWK_ECDSA, JWK_EC_Private, JWK_EC_Public, JWK_HMAC, JWK_OKP, JWK_OKP_Private, JWK_OKP_Public, JWK_OKP_SIGN, JWK_PBES2, JWK_Pair, JWK_Private, JWK_Public, JWK_RSA, JWK_RSA_ENC, JWK_RSA_PSS, JWK_RSA_Private, JWK_RSA_Public, JWK_RSA_SIGN, JWK_Symmetric, JWK_Symmetric_Algorithm, JWK_oct, JWTError, JWTErrorCauseMap, JWTErrorCode, KeyManagementAlgorithm, KnownJWTErrorCode, UnwrapKeyOptions, UnwrapKeyResult, UnwrappingKeyFor, WrapKeyOptions, WrapKeyResult, WrappingKeyFor, isJWTError, isUnsecureError } from "../_chunks/index.mjs";
|
|
3
|
+
import { ThumbprintHash, thumbprint } from "../_chunks/index2.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Generates a key for a JWA algorithm: a `CryptoKey` for symmetric algorithms, raw bytes for
|
|
6
|
+
* the `A*CBC-HS*` composites, a `CryptoKeyPair` for asymmetric ones; with `toJWK: true`, a JWK
|
|
7
|
+
* (or JWK pair) named by the `kid` option or its RFC 7638 thumbprint. PBES2, `"dir"` and `"none"` are refused.
|
|
8
|
+
*
|
|
9
|
+
* @param options
|
|
10
|
+
* - `extractable?` — defaults to `true`; ignored when `toJWK` is set.
|
|
11
|
+
* - `kid?` — with `toJWK`, a string set verbatim or `false` for no `kid`, either skipping the
|
|
12
|
+
* thumbprint digest; anything else throws `ERR_INVALID_OPTION`.
|
|
13
|
+
* - `keyUsage?` — narrows the usages derived from `alg`; leaving either half of a pair with
|
|
14
|
+
* no usage throws `ERR_INVALID_OPTION`.
|
|
15
|
+
* - `modulusLength?` — RSA only; defaults to `2048`.
|
|
16
|
+
* - `publicExponent?` — RSA only; big-endian bytes, defaults to `0x010001`.
|
|
17
|
+
* - `namedCurve?` — EC/OKP only; defaults to `"P-256"` / `"Ed25519"`. One that contradicts
|
|
18
|
+
* the curve `ES*` or `Ed25519` names throws `ERR_INVALID_OPTION`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function generateKey<TAlg extends GenerateKeyAlgorithm, ToJWK extends boolean = false, Kid extends string | false = string>(alg: TAlg, options?: GenerateKeyOptions<ToJWK, Kid>): Promise<GenerateKeyReturn<TAlg, GenerateKeyOptions<ToJWK, Kid>>>;
|
|
21
|
+
/**
|
|
22
|
+
* Generates a JWK (or JWK pair) for a JWA algorithm, its `kid` the caller's, none with
|
|
23
|
+
* `kid: false`, or else its RFC 7638 thumbprint. PBES2, `"dir"` and `"none"` are refused.
|
|
24
|
+
*
|
|
25
|
+
* @param options
|
|
26
|
+
* - `keyUsage?`, `modulusLength?` (default `2048`), `publicExponent?` (default `0x010001`),
|
|
27
|
+
* `namedCurve?` (default `"P-256"` / `"Ed25519"`); a narrowing that empties a pair half or
|
|
28
|
+
* a curve that contradicts `alg` throws `ERR_INVALID_OPTION`.
|
|
29
|
+
* - `kid?` — a string set verbatim or `false` for no `kid`, either skipping the thumbprint
|
|
30
|
+
* digest; anything else throws `ERR_INVALID_OPTION`.
|
|
31
|
+
* - JWK metadata (`use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is merged onto the result;
|
|
32
|
+
* `alg`, `kty`, `key_ops`, `ext` and key-material members are reserved (`ERR_INVALID_OPTION`).
|
|
33
|
+
*/
|
|
34
|
+
export declare function generateJWK<TAlg extends GenerateKeyAlgorithm, Kid extends string | false = string>(alg: TAlg, options?: GenerateJWKOptions<Kid> & Omit<JWKMetadata, "kid">): Promise<GenerateJWKReturn<TAlg, Kid>>;
|
|
35
|
+
/**
|
|
36
|
+
* Derives an AES-KW key from a password with PBKDF2 per PBES2 (RFC 7518 §4.8).
|
|
37
|
+
*
|
|
38
|
+
* @param options
|
|
39
|
+
* - `alg` — a PBES2 algorithm; any other throws `ERR_JWK_UNSUPPORTED`.
|
|
40
|
+
* - `salt` — PBKDF2 salt (`p2s`), at least 8 octets (RFC 7518 §4.8.1.1).
|
|
41
|
+
* - `iterations` — PBKDF2 count (`p2c`), an integer of at least 1000 (RFC 7518 §4.8.1.2);
|
|
42
|
+
* anything else throws `ERR_INVALID_OPTION`.
|
|
43
|
+
* - `keyUsage?` — defaults to `["wrapKey", "unwrapKey"]`.
|
|
44
|
+
* - `extractable?` — defaults to `false`; ignored when `toJWK` is set.
|
|
45
|
+
* - `toJWK?` — return a `JWK_oct` carrying the inner AES-KW `alg` instead of a `CryptoKey`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function deriveKeyFromPassword<TOptions extends DeriveKeyOptions>(password: string | Uint8Array<ArrayBuffer>, options: TOptions): Promise<DeriveKeyReturn<TOptions, TOptions["alg"]>>;
|
|
48
|
+
/**
|
|
49
|
+
* Derives a `JWK_oct` from a password per PBES2 (RFC 7518 §4.8); its `alg` is the inner AES-KW
|
|
50
|
+
* variant and its `kid` the caller's, none with `kid: false`, or else its RFC 7638 thumbprint.
|
|
51
|
+
*
|
|
52
|
+
* @param options
|
|
53
|
+
* - `alg` (PBES2 only, else `ERR_JWK_UNSUPPORTED`), `salt` (at least 8 octets), `iterations`
|
|
54
|
+
* (integer of at least 1000, else `ERR_INVALID_OPTION`), `keyUsage?` (default
|
|
55
|
+
* `["wrapKey", "unwrapKey"]`).
|
|
56
|
+
* - `kid?` — a string set verbatim or `false` for no `kid`, either skipping the thumbprint
|
|
57
|
+
* digest; anything else throws `ERR_INVALID_OPTION`.
|
|
58
|
+
* - JWK metadata (`use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is merged onto the result;
|
|
59
|
+
* `alg`, `kty`, `key_ops`, `ext` and key-material members are reserved (`ERR_INVALID_OPTION`).
|
|
60
|
+
*/
|
|
61
|
+
export declare function deriveJWKFromPassword(password: string | Uint8Array<ArrayBuffer>, options: Omit<DeriveKeyOptions, "toJWK" | "extractable"> & Omit<JWKMetadata, "kid"> & {
|
|
62
|
+
kid?: string | false;
|
|
63
|
+
}): Promise<JWK_oct>;
|
|
64
|
+
/**
|
|
65
|
+
* Default JWK import cache, keyed by JWK object identity: a copied JWK misses it, and a JWK
|
|
66
|
+
* mutated in place keeps serving the old `CryptoKey`. Rotate by replacing the object.
|
|
67
|
+
*/
|
|
68
|
+
export declare class WeakMapJWKCache implements JWKCacheAdapter {
|
|
131
69
|
private readonly _map;
|
|
132
70
|
get(jwk: JWK, alg: string): CryptoKey | undefined;
|
|
133
71
|
set(jwk: JWK, alg: string, key: CryptoKey): void;
|
|
134
72
|
}
|
|
135
73
|
/**
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* ```ts
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* });
|
|
153
|
-
* ```
|
|
154
|
-
*/
|
|
155
|
-
declare function configureJWKCache(cache: JWKCacheAdapter | false): void;
|
|
156
|
-
/**
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
* - If `key` is a `CryptoKey`, it's returned directly.
|
|
166
|
-
* - If `key` is a `Uint8Array`, it's returned directly.
|
|
167
|
-
* - If `key` is a `JWK_oct` (symmetric key with `k`), the raw key bytes are returned as `Uint8Array`.
|
|
168
|
-
* - If `key` is any other JWK type (asymmetric), an algorithm hint is required (positional or via options)
|
|
169
|
-
* and a `CryptoKey` is returned.
|
|
170
|
-
*
|
|
171
|
-
* ### `expect` option (asymmetric only)
|
|
172
|
-
*
|
|
173
|
-
* Pass `expect: "public"` or `expect: "private"` to validate the caller's intent against the key's
|
|
174
|
-
* shape before the import. A private JWK passed with `expect: "public"` throws `ERR_JWK_INVALID`
|
|
175
|
-
* instead of silently importing the private material. Callers who deliberately want to drop private
|
|
176
|
-
* fields can do so themselves — no silent stripping.
|
|
177
|
-
*
|
|
178
|
-
* `expect` is a no-op for symmetric (`oct`) JWKs and for `CryptoKey` instances whose
|
|
179
|
-
* `algorithm.type` is `"secret"`.
|
|
180
|
-
*/
|
|
181
|
-
declare function importKey(key: string): Promise<Uint8Array<ArrayBuffer>>;
|
|
182
|
-
declare function importKey(key: Uint8Array<ArrayBuffer>): Promise<Uint8Array<ArrayBuffer>>;
|
|
183
|
-
declare function importKey(key: CryptoKey): Promise<CryptoKey>;
|
|
184
|
-
declare function importKey(key: JWK_oct): Promise<Uint8Array<ArrayBuffer>>;
|
|
185
|
-
declare function importKey(key: JWK_oct, options: {
|
|
186
|
-
asCryptoKey: true; /** Algorithm to import the key as (e.g. `{ name: "AES-GCM", length: 256 }`). */
|
|
187
|
-
algorithm: Parameters<typeof crypto.subtle.importKey>[2]; /** Key usages for the resulting CryptoKey. */
|
|
188
|
-
usage: KeyUsage[]; /** Mark the key as extractable. Defaults to `false`. */
|
|
74
|
+
* Replaces the JWK import cache used by `importKey`, or disables it with `false`.
|
|
75
|
+
*
|
|
76
|
+
* A cache shared across JWK objects keys on the RFC 7638 thumbprint, the pair half (`d` present)
|
|
77
|
+
* and `alg`: the thumbprint covers public members only, and a `kid` may name different keys.
|
|
78
|
+
* `get` and `set` may return promises.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const cacheKey = async (jwk: JWK, alg: string) =>
|
|
83
|
+
* `${await thumbprint(jwk)}:${"d" in jwk ? "private" : "public"}:${alg}`;
|
|
84
|
+
* const map = new Map<string, CryptoKey>();
|
|
85
|
+
* configureJWKCache({
|
|
86
|
+
* get: async (jwk, alg) => map.get(await cacheKey(jwk, alg)),
|
|
87
|
+
* set: async (jwk, alg, key) => {
|
|
88
|
+
* map.set(await cacheKey(jwk, alg), key);
|
|
89
|
+
* },
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function configureJWKCache(cache: JWKCacheAdapter | false): void;
|
|
94
|
+
/** Resets the JWK import cache to a fresh `WeakMapJWKCache`. */
|
|
95
|
+
export declare function clearJWKCache(): void;
|
|
96
|
+
/** Imports the secret of an `oct` JWK as a `CryptoKey` under `algorithm` and `usage`. */
|
|
97
|
+
export declare function importKey(key: JWK_oct, options: {
|
|
98
|
+
asCryptoKey: true;
|
|
99
|
+
/** Algorithm to import the key as (e.g. `{ name: "AES-GCM", length: 256 }`). */
|
|
100
|
+
algorithm: Parameters<typeof crypto.subtle.importKey>[2];
|
|
101
|
+
usage: KeyUsage[];
|
|
102
|
+
/** Mark the key as extractable. Defaults to `false`. */
|
|
189
103
|
extractable?: boolean;
|
|
190
104
|
}): Promise<CryptoKey>;
|
|
191
|
-
declare function importKey(key: CryptoKey | JWK | Uint8Array<ArrayBuffer> | string, options: {
|
|
192
|
-
/** Algorithm hint. Required when `key.alg` is absent on an asymmetric JWK. */alg?: string; /** Reject on shape mismatch instead of silently importing. */
|
|
193
|
-
expect?: "public" | "private";
|
|
194
|
-
}): Promise<CryptoKey | Uint8Array<ArrayBuffer>>;
|
|
195
|
-
declare function importKey(key: CryptoKey | JWK | Uint8Array<ArrayBuffer> | string, alg?: string): Promise<CryptoKey | Uint8Array<ArrayBuffer>>;
|
|
196
|
-
/**
|
|
197
|
-
* Exports a CryptoKey to a JWK (JSON Web Key) format.
|
|
198
|
-
*
|
|
199
|
-
* @param key The CryptoKey to export.
|
|
200
|
-
* @param jwk Optional partial JWK to merge with the exported key, allowing overrides.
|
|
201
|
-
* @returns A Promise resolving to the exported JWK.
|
|
202
|
-
*/
|
|
203
|
-
declare function exportKey<T extends JWK>(key: CryptoKey, jwkParams?: Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">): Promise<T>;
|
|
204
105
|
/**
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
*
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
declare function
|
|
232
|
-
/**
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
* `
|
|
244
|
-
*
|
|
245
|
-
* @
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
* `
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
* const jwk = await importPEM(pemString, "RS256", { jwkParams: { kid: "rsa-1" } });
|
|
265
|
-
*
|
|
266
|
-
* // Explicit override.
|
|
267
|
-
* const jwk2 = await importPEM(pemString, "RS256", { pemType: "x509" });
|
|
268
|
-
* ```
|
|
269
|
-
*/
|
|
270
|
-
declare function importPEM<T extends JWK>(pem: string, alg: JWKPEMAlgorithm, options?: {
|
|
271
|
-
/** PEM type. Inferred from the PEM label when omitted. */pemType?: "pkcs8" | "spki" | "x509"; /** Additional JWK properties merged into the exported key (e.g. `kid`). */
|
|
272
|
-
jwkParams?: Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">;
|
|
273
|
-
}): Promise<T>;
|
|
274
|
-
/**
|
|
275
|
-
* @deprecated Use {@link importPEM} instead. `pemType` has moved into the options object and
|
|
276
|
-
* is now inferred from the PEM label by default.
|
|
277
|
-
*/
|
|
278
|
-
declare function importFromPEM<T extends JWK>(pem: string, pemType: "pkcs8" | "spki" | "x509", alg: JWKPEMAlgorithm, options?: {
|
|
279
|
-
jwkParams?: Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">;
|
|
106
|
+
* Imports key material: a `CryptoKey` or `Uint8Array` is returned as is, an `oct` JWK as its raw
|
|
107
|
+
* bytes, an asymmetric JWK as a `CryptoKey` under its own `alg` or the hint. A string throws
|
|
108
|
+
* `ERR_JWK_INVALID`.
|
|
109
|
+
*
|
|
110
|
+
* `expect` and `extractable` apply to asymmetric keys only; `expect` rejects the other pair half
|
|
111
|
+
* with `ERR_JWK_INVALID`. A JWK's `alg` is authoritative (RFC 7517 §4.4): a different requested
|
|
112
|
+
* `alg` throws `ERR_JWK_INVALID`, except an `oct` JWK declaring a content encryption `alg` under
|
|
113
|
+
* `"dir"`.
|
|
114
|
+
*/
|
|
115
|
+
export declare function importKey<K extends CryptoKey | JWK | Uint8Array<ArrayBuffer>>(key: K, options?: string | {
|
|
116
|
+
/** Algorithm hint. Required when `key.alg` is absent on an asymmetric JWK. */
|
|
117
|
+
alg?: string;
|
|
118
|
+
/** Reject on shape mismatch instead of silently importing. */
|
|
119
|
+
expect?: "public" | "private";
|
|
120
|
+
/** Defaults to `false` for private, `true` for public material; JWK `ext: false` wins. */
|
|
121
|
+
extractable?: boolean;
|
|
122
|
+
}): Promise<ImportedKey<K>>;
|
|
123
|
+
/**
|
|
124
|
+
* Exports an extractable `CryptoKey` as a JWK, merging optional `jwkParams` metadata.
|
|
125
|
+
*
|
|
126
|
+
* The `alg` is WebCrypto's, which knows no JOSE roles: an AES-GCM key minted for `A128GCMKW`
|
|
127
|
+
* exports as `A128GCM`, so key-wrapping keys are minted with `generateJWK` instead.
|
|
128
|
+
*
|
|
129
|
+
* @throws `ERR_INVALID_OPTION` if `key` is not a `CryptoKey` or `jwkParams` sets a reserved member
|
|
130
|
+
* (`alg`, `kty`, `key_ops`, `ext`, key material); `ERR_JWK_INVALID` for a non-extractable key.
|
|
131
|
+
*/
|
|
132
|
+
export declare function exportKey<T extends JWK = JWK>(key: CryptoKey, jwkParams?: JWKMetadata): Promise<T>;
|
|
133
|
+
/**
|
|
134
|
+
* Wraps a CEK under a JWA key management algorithm; the result shape follows `alg`.
|
|
135
|
+
*
|
|
136
|
+
* @param wrappingKey A `CryptoKey`, a JWK, or for PBES2 a password string or bytes.
|
|
137
|
+
* @param options `alg` plus its parameters (`p2s`/`p2c` for PBES2, `ecdh` for ECDH-ES, …).
|
|
138
|
+
*/
|
|
139
|
+
export declare function wrapKey<TAlg extends KeyManagementAlgorithm>(keyToWrap: CryptoKey | Uint8Array<ArrayBuffer>, wrappingKey: WrappingKeyFor<TAlg>, options: WrapKeyOptions<TAlg>): Promise<WrapKeyResult<TAlg>>;
|
|
140
|
+
/**
|
|
141
|
+
* Unwraps a CEK under a JWA key management algorithm.
|
|
142
|
+
*
|
|
143
|
+
* @param unwrappingKey A `CryptoKey`, a JWK, or for PBES2 a password string or bytes.
|
|
144
|
+
* @param options `alg`, `format?` (`"cryptokey"` by default, or `"raw"`), plus the algorithm's
|
|
145
|
+
* parameters (`iv`/`tag`, `p2s`/`p2c`, `epk`, …).
|
|
146
|
+
* @throws `ERR_JWE_DECRYPTION_FAILED` for any failed unwrap, its cause undisclosed (RFC 7516
|
|
147
|
+
* §11.5); `ERR_INVALID_OPTION` for a malformed `wrappedKey` or `options`; `ERR_JWE_INVALID` for
|
|
148
|
+
* missing or malformed algorithm parameters.
|
|
149
|
+
*/
|
|
150
|
+
export declare function unwrapKey<TOptions extends UnwrapKeyOptions>(wrappedKey: Uint8Array<ArrayBuffer>, unwrappingKey: UnwrappingKeyFor<TOptions["alg"]>, options: TOptions): Promise<UnwrapKeyResult<TOptions>>;
|
|
151
|
+
/**
|
|
152
|
+
* Imports a PEM key as a JWK carrying `alg`. The type follows the label — `PRIVATE KEY`
|
|
153
|
+
* (PKCS#8), `PUBLIC KEY` (SPKI), `CERTIFICATE` (X.509, only its SPKI kept) — unless
|
|
154
|
+
* `options.pemType` overrides it. The JWK holds the key material in plain JavaScript.
|
|
155
|
+
*
|
|
156
|
+
* @throws `ERR_JWK_INVALID` if the type cannot be inferred or does not match the label;
|
|
157
|
+
* `ERR_JWK_UNSUPPORTED` for an unknown `pemType`; `ERR_INVALID_OPTION` if `jwkParams` sets
|
|
158
|
+
* `alg`, `kty`, `key_ops`, `ext` or a key-material member.
|
|
159
|
+
*/
|
|
160
|
+
export declare function importPEM<T extends JWK>(pem: string, alg: JWKPEMAlgorithm, options?: {
|
|
161
|
+
/** PEM type. Inferred from the PEM label when omitted. */
|
|
162
|
+
pemType?: "pkcs8" | "spki" | "x509";
|
|
163
|
+
/** Additional JWK properties merged into the exported key (e.g. `kid`). */
|
|
164
|
+
jwkParams?: JWKMetadata;
|
|
280
165
|
}): Promise<T>;
|
|
281
166
|
/**
|
|
282
|
-
* Exports
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
*
|
|
292
|
-
* Symmetric `oct` JWKs cannot be exported to PEM — PKCS#8/SPKI are asymmetric-key formats.
|
|
293
|
-
*
|
|
294
|
-
* @param jwk The JWK to export. Must not be `kty: "oct"`.
|
|
295
|
-
* @param options
|
|
296
|
-
* - `pemFormat?` — force the output format. Inferred from the JWK shape when omitted.
|
|
297
|
-
* - `alg?` — algorithm hint used only when `jwk.alg` is absent (both `kty: "RSA"` / `"EC"` /
|
|
298
|
-
* `"OKP"` JWKs need an algorithm to be round-tripped through `crypto.subtle.importKey`).
|
|
299
|
-
* @throws `JWTError("ERR_JWK_UNSUPPORTED")` for `oct` JWKs, or an unrecognised `pemFormat`.
|
|
300
|
-
* @throws `JWTError("ERR_JWK_INVALID")` if the JWK requires an `alg` hint that was not supplied,
|
|
301
|
-
* or if `pemFormat` is forced to a value incompatible with the JWK's public/private shape.
|
|
302
|
-
*
|
|
303
|
-
* @example
|
|
304
|
-
* ```ts
|
|
305
|
-
* // Inferred — private JWKs become PKCS#8, public JWKs become SPKI.
|
|
306
|
-
* const pem = await exportPEM(jwk);
|
|
307
|
-
*
|
|
308
|
-
* // Override (will throw if incompatible with the JWK shape).
|
|
309
|
-
* const spki = await exportPEM(jwk, { pemFormat: "spki" });
|
|
310
|
-
*
|
|
311
|
-
* // JWK without `alg` — provide a hint.
|
|
312
|
-
* const pem2 = await exportPEM(rawJwk, { alg: "RS256" });
|
|
313
|
-
* ```
|
|
314
|
-
*/
|
|
315
|
-
declare function exportPEM(jwk: JWK, options?: {
|
|
316
|
-
/** PEM format. Inferred from the JWK shape when omitted. */pemFormat?: "pkcs8" | "spki"; /** Algorithm hint used when `jwk.alg` is absent. */
|
|
167
|
+
* Exports an asymmetric JWK as PEM: PKCS#8 when it carries `d`, SPKI otherwise.
|
|
168
|
+
*
|
|
169
|
+
* @throws `ERR_JWK_UNSUPPORTED` for `oct` JWKs or an unknown format or algorithm;
|
|
170
|
+
* `ERR_JWK_INVALID` for a missing `alg` or a `pemFormat` that contradicts the JWK's half.
|
|
171
|
+
*/
|
|
172
|
+
export declare function exportPEM(jwk: JWK, options?: {
|
|
173
|
+
/** PEM format. Inferred from the JWK shape when omitted. */
|
|
174
|
+
pemFormat?: "pkcs8" | "spki";
|
|
175
|
+
/** Algorithm hint used when `jwk.alg` is absent. */
|
|
317
176
|
alg?: JWKPEMAlgorithm;
|
|
318
177
|
}): Promise<string>;
|
|
319
178
|
/**
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
*
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
*
|
|
334
|
-
* For multi-recipient JWE, call this once per recipient with their public key
|
|
335
|
-
* and a fresh ephemeral private key, then wrap the shared CEK with the derived
|
|
336
|
-
* material using {@link wrapKey}.
|
|
337
|
-
*
|
|
338
|
-
* @param publicKey The recipient's static public key (or the sender's
|
|
339
|
-
* ephemeral public key on the decryption side).
|
|
340
|
-
* @param privateKey The sender's ephemeral private key (or the recipient's
|
|
341
|
-
* static private key on the decryption side).
|
|
342
|
-
* @param alg The algorithm identifier used as the `AlgorithmID` in the
|
|
343
|
-
* concat KDF info structure. Pass a {@link JWK_ECDH_ES} variant
|
|
344
|
-
* (e.g. `"ECDH-ES+A128KW"`) or a {@link ContentEncryptionAlgorithm}
|
|
345
|
-
* (e.g. `"A256GCM"`) for direct key agreement.
|
|
346
|
-
* @param options Optional overrides for key length and party info.
|
|
347
|
-
*/
|
|
348
|
-
declare function deriveSharedSecret(publicKey: CryptoKey | JWK_EC_Public, privateKey: CryptoKey | JWK_EC_Private, alg: JWK_ECDH_ES | ContentEncryptionAlgorithm, options?: {
|
|
349
|
-
/** Override the derived key length in bits. Defaults to the standard length for `alg`. */keyLength?: number; /** Agreement PartyUInfo (apu). */
|
|
350
|
-
partyUInfo?: Uint8Array<ArrayBuffer>; /** Agreement PartyVInfo (apv). */
|
|
179
|
+
* Derives ECDH-ES key material (RFC 7518 §4.6): ECDH followed by the Concat KDF.
|
|
180
|
+
*
|
|
181
|
+
* @param publicKey The recipient's static public key (the ephemeral one when decrypting).
|
|
182
|
+
* @param privateKey The ephemeral private key (the recipient's static one when decrypting).
|
|
183
|
+
*/
|
|
184
|
+
export declare function deriveSharedSecret(publicKey: CryptoKey | JWK_EC_Public, privateKey: CryptoKey | JWK_EC_Private, options: {
|
|
185
|
+
/** Algorithm identifier fed to the concat KDF as `AlgorithmID`. */
|
|
186
|
+
enc: JWK_ECDH_ES | ContentEncryptionAlgorithm;
|
|
187
|
+
/** Derived length in bits. Defaults to `enc`'s length; required for `"ECDH-ES"`. */
|
|
188
|
+
keyLength?: number;
|
|
189
|
+
/** Agreement PartyUInfo (apu). */
|
|
190
|
+
partyUInfo?: Uint8Array<ArrayBuffer>;
|
|
191
|
+
/** Agreement PartyVInfo (apv). */
|
|
351
192
|
partyVInfo?: Uint8Array<ArrayBuffer>;
|
|
352
193
|
}): Promise<Uint8Array<ArrayBuffer>>;
|
|
353
194
|
/**
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
* properties from a JOSE Header.
|
|
360
|
-
*
|
|
361
|
-
* @param jwkSet The JWKSet to search within.
|
|
362
|
-
* @param kidOrProtectedHeader Either a string representing the 'kid' (Key ID)
|
|
363
|
-
* or an object representing the JOSE Protected Header. If an object is
|
|
364
|
-
* provided, it must contain a 'kid' property. It can optionally contain
|
|
365
|
-
* 'alg' (Algorithm) and 'kty' (Key Type) to further refine the search.
|
|
366
|
-
* @returns The matching JWK from the set.
|
|
367
|
-
* @throws TypeError if `jwkSet` is invalid or `kidOrProtectedHeader` is not a
|
|
368
|
-
* string or a valid header object.
|
|
369
|
-
* @throws Error if no key is found matching the provided criteria, or if the
|
|
370
|
-
* header object is missing the 'kid' property.
|
|
371
|
-
*/
|
|
372
|
-
declare function getJWKFromSet(jwkSet: JWKSet, kidOrProtectedHeader: string | (JoseHeaderParameters & {
|
|
373
|
-
alg?: string;
|
|
374
|
-
kty?: string;
|
|
375
|
-
})): JWK;
|
|
376
|
-
/**
|
|
377
|
-
* Returns all JWKs from a JWK Set, optionally narrowed by a predicate.
|
|
378
|
-
*
|
|
379
|
-
* Useful for multi-key verification retry, key rotation tooling, and
|
|
380
|
-
* constructing multi-recipient JWE JSON Serialization structures.
|
|
381
|
-
*
|
|
382
|
-
* @param jwkSet The JWK Set to search.
|
|
383
|
-
* @param filter Optional predicate `(jwk: JWK) => boolean`. Returns all keys when omitted.
|
|
384
|
-
* @returns An array of matching JWKs.
|
|
385
|
-
*/
|
|
386
|
-
declare function getJWKsFromSet(jwkSet: JWKSet, filter?: (jwk: JWK) => boolean): JWK[];
|
|
387
|
-
export { type CompositeKey, type ContentEncryptionAlgorithm, type DeriveKeyOptions, type DeriveKeyReturn, type GenerateJWKOptions, type GenerateJWKReturn, type GenerateKeyAlgorithm, type GenerateKeyOptions, type GenerateKeyReturn, type JWK, type JWKAlgorithm, type JWKCacheAdapter, type JWKLookupFunction, type JWKLookupFunctionHeader, type JWKPEMAlgorithm, type JWKParameters, type JWKSet, type JWK_AES_CBC_HMAC, type JWK_AES_GCM, type JWK_AES_GCM_KW, type JWK_AES_KW, type JWK_Asymmetric, type JWK_Asymmetric_Algorithm, type JWK_EC, type JWK_ECDH_ES, type JWK_ECDSA, type JWK_EC_Private, type JWK_EC_Public, type JWK_HMAC, type JWK_OKP, type JWK_OKP_Private, type JWK_OKP_Public, type JWK_OKP_SIGN, type JWK_PBES2, type JWK_Pair, type JWK_Private, type JWK_Public, type JWK_RSA, type JWK_RSA_ENC, type JWK_RSA_PSS, type JWK_RSA_Private, type JWK_RSA_Public, type JWK_RSA_SIGN, type JWK_Symmetric, type JWK_Symmetric_Algorithm, type JWK_oct, JWTError, type JWTErrorCauseMap, type JWTErrorCode, type KeyManagementAlgorithm, type UnwrapKeyOptions, type UnwrappingKeyFor, WeakMapJWKCache, type WrapKeyOptions, type WrapKeyResult, type WrappingKeyFor, clearJWKCache, configureJWKCache, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, exportKey, exportPEM, exportToPEM, generateJWK, generateKey, getJWKFromSet, getJWKsFromSet, importFromPEM, importKey, importPEM, isJWTError, unwrapKey, wrapKey };
|
|
195
|
+
* Returns the JWKs of a JWK Set, dropping non-JWK entries, optionally narrowed by `filter`.
|
|
196
|
+
* An invalid set throws `ERR_JWK_INVALID`.
|
|
197
|
+
*/
|
|
198
|
+
export declare function getJWKsFromSet(jwkSet: JWKSet, filter?: (jwk: JWK) => boolean): JWK[];
|
|
199
|
+
export { type CompositeKey, type ContentEncryptionAlgorithm, type DeriveKeyOptions, type DeriveKeyReturn, type ECDHEphemeralKeyInput, type GenerateJWKOptions, type GenerateJWKReturn, type GenerateKeyAlgorithm, type GenerateKeyOptions, type GenerateKeyReturn, type GeneratedJWKMap, type GeneratedKeyMap, type ImportedKey, type ImportedKeyMap, type JWK, type JWKAlgorithm, type JWKCacheAdapter, type JWKLookupFunction, type JWKLookupFunctionHeader, type JWKMetadata, type JWKPEMAlgorithm, type JWKParameters, type JWKSet, type JWK_AES_CBC_HMAC, type JWK_AES_GCM, type JWK_AES_GCM_KW, type JWK_AES_KW, type JWK_Asymmetric, type JWK_Asymmetric_Algorithm, type JWK_EC, type JWK_ECDH_ES, type JWK_ECDSA, type JWK_EC_Private, type JWK_EC_Public, type JWK_HMAC, type JWK_OKP, type JWK_OKP_Private, type JWK_OKP_Public, type JWK_OKP_SIGN, type JWK_PBES2, type JWK_Pair, type JWK_Private, type JWK_Public, type JWK_RSA, type JWK_RSA_ENC, type JWK_RSA_PSS, type JWK_RSA_Private, type JWK_RSA_Public, type JWK_RSA_SIGN, type JWK_Symmetric, type JWK_Symmetric_Algorithm, type JWK_oct, JWTError, type JWTErrorCauseMap, type JWTErrorCode, type KeyManagementAlgorithm, type KnownJWTErrorCode, type ThumbprintHash, UnsecureError, type UnsecureErrorCode, type UnwrapKeyOptions, type UnwrapKeyResult, type UnwrappingKeyFor, type WrapKeyOptions, type WrapKeyResult, type WrappingKeyFor, isJWTError, isUnsecureError, thumbprint };
|
package/dist/core/jwk.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { UnsecureError } from "../_chunks/libs/unsecure.mjs";
|
|
2
|
+
import { JWTError, isJWTError, isUnsecureError } from "../_chunks/_jwt-internal.mjs";
|
|
3
|
+
import { WeakMapJWKCache, clearJWKCache, configureJWKCache, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, exportKey, exportPEM, generateJWK, generateKey, getJWKsFromSet, importKey, importPEM, thumbprint, unwrapKey, wrapKey } from "../_chunks/jwk.mjs";
|
|
4
|
+
export { JWTError, UnsecureError, WeakMapJWKCache, clearJWKCache, configureJWKCache, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, exportKey, exportPEM, generateJWK, generateKey, getJWKsFromSet, importKey, importPEM, isJWTError, isUnsecureError, thumbprint, unwrapKey, wrapKey };
|