unjwt 0.4.0 → 0.5.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 +360 -190
- package/dist/index.d.mts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.mjs +5 -5
- package/dist/jwe.d.mts +3 -3
- package/dist/jwe.d.ts +3 -3
- package/dist/jwe.mjs +4 -3
- package/dist/jwk.d.mts +2 -2
- package/dist/jwk.d.ts +2 -2
- package/dist/jwk.mjs +2 -2
- package/dist/jws.d.mts +3 -3
- package/dist/jws.d.ts +3 -3
- package/dist/jws.mjs +4 -4
- package/dist/shared/unjwt.B4e_rwol.mjs +453 -0
- package/dist/shared/unjwt.B9uGngq-.d.mts +154 -0
- package/dist/shared/unjwt.BKG_8Y9G.d.ts +189 -0
- package/dist/shared/unjwt.BOEZHxi_.d.ts +154 -0
- package/dist/shared/unjwt.Bzcov22m.mjs +167 -0
- package/dist/shared/unjwt.C0bBN0AC.d.mts +200 -0
- package/dist/shared/unjwt.C0bBN0AC.d.ts +200 -0
- package/dist/shared/unjwt.CJ_ohCFC.d.mts +46 -0
- package/dist/shared/unjwt.CjHZv3F3.mjs +57 -0
- package/dist/shared/unjwt.D90BbcE1.d.mts +52 -0
- package/dist/shared/unjwt.DKq5VF7t.mjs +1186 -0
- package/dist/shared/unjwt.DYFIh1YE.d.mts +100 -0
- package/dist/shared/unjwt.DoVzOnty.d.ts +52 -0
- package/dist/shared/unjwt.Dz5Oe9BG.d.mts +189 -0
- package/dist/shared/unjwt.mc4AEAT7.d.ts +46 -0
- package/dist/shared/unjwt.ppm5Y0k7.d.ts +100 -0
- package/dist/shared/unjwt.sux3_Z19.mjs +101 -0
- package/dist/utils.d.mts +2 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.mjs +1 -0
- package/package.json +8 -4
- package/dist/shared/unjwt.4uzJv0cA.d.mts +0 -110
- package/dist/shared/unjwt.A7HKFx3n.d.mts +0 -52
- package/dist/shared/unjwt.BdjvpIUY.d.mts +0 -79
- package/dist/shared/unjwt.BmjmfFWo.d.ts +0 -79
- package/dist/shared/unjwt.C9ELsKeK.mjs +0 -120
- package/dist/shared/unjwt.CVUxLNoy.d.ts +0 -52
- package/dist/shared/unjwt.ChP7VhqI.d.ts +0 -110
- package/dist/shared/unjwt.DD0p18Gd.mjs +0 -81
- package/dist/shared/unjwt.DGsIvgyn.d.ts +0 -106
- package/dist/shared/unjwt.DPTwlVpK.mjs +0 -28
- package/dist/shared/unjwt.Dr-mxtq9.d.mts +0 -106
- package/dist/shared/unjwt.KOd0vXyx.mjs +0 -90
- package/dist/shared/unjwt._GivNhs2.mjs +0 -213
- package/dist/shared/unjwt.zb2wBNCJ.d.mts +0 -82
- package/dist/shared/unjwt.zb2wBNCJ.d.ts +0 -82
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, w as JWK_PBES2, D as DeriveKeyOptions, c as DeriveKeyReturn, l as JWK_oct, m as JWK, K as KeyManagementAlgorithm, W as WrapKeyOptions, e as WrapKeyResult, U as UnwrapKeyOptions, C as CompositeKey, d as ContentEncryptionAlgorithm, A as JWKAlgorithm, J as JWKParameters, u as JWK_AES_CBC_HMAC, v as JWK_AES_GCM, t as JWK_AES_GCM_KW, s as JWK_AES_KW, z as JWK_Asymmetric_Algorithm, h as JWK_EC, x as JWK_ECDH_ES, q as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, n as JWK_HMAC, k as JWK_RSA, r as JWK_RSA_ENC, p as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, o as JWK_RSA_SIGN, y as JWK_Symmetric_Algorithm } from './unjwt.C0bBN0AC.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generates a cryptographic key for the specified algorithm.
|
|
5
|
+
*
|
|
6
|
+
* @param alg The JWA algorithm identifier (e.g., "HS256", "RS256", "A128GCM").
|
|
7
|
+
* @param options Configuration options for key generation.
|
|
8
|
+
* @returns A Promise resolving to the generated key (CryptoKey, CryptoKeyPair, or Uint8Array) or its JWK representation.
|
|
9
|
+
*/
|
|
10
|
+
declare function generateKey<TAlg extends GenerateKeyAlgorithm, TOptions extends GenerateKeyOptions>(alg: TAlg, options?: TOptions): Promise<GenerateKeyReturn<TAlg, TOptions>>;
|
|
11
|
+
/**
|
|
12
|
+
* Derives a key from a password using PBKDF2 as specified by PBES2 algorithms.
|
|
13
|
+
*
|
|
14
|
+
* @param password The password to derive the key from (string or Uint8Array).
|
|
15
|
+
* @param alg The PBES2 algorithm identifier (e.g., "PBES2-HS256+A128KW").
|
|
16
|
+
* @param options Configuration options including salt and iterations.
|
|
17
|
+
* @returns A Promise resolving to the derived key (CryptoKey) or its JWK_oct representation.
|
|
18
|
+
*/
|
|
19
|
+
declare function deriveKeyFromPassword<TAlg extends JWK_PBES2, TOptions extends DeriveKeyOptions>(password: string | Uint8Array, alg: TAlg, options: TOptions): Promise<DeriveKeyReturn<TOptions>>;
|
|
20
|
+
/**
|
|
21
|
+
* Imports a key from various formats (CryptoKey, JWK, Uint8Array).
|
|
22
|
+
*
|
|
23
|
+
* - If `key` is a CryptoKey, it's returned directly.
|
|
24
|
+
* - If `key` is a Uint8Array, it's returned directly.
|
|
25
|
+
* - If `key` is a JWK_oct (symmetric key with 'k'), the raw key bytes are returned as Uint8Array.
|
|
26
|
+
* - If `key` is any other JWK type (asymmetric), the `alg` parameter is required, and a CryptoKey is returned.
|
|
27
|
+
*
|
|
28
|
+
* @param key The key to import.
|
|
29
|
+
* @param alg The algorithm hint, required when importing asymmetric JWKs.
|
|
30
|
+
* @returns A Promise resolving to the imported key as CryptoKey or Uint8Array.
|
|
31
|
+
*/
|
|
32
|
+
declare function importKey(key: string): Promise<Uint8Array>;
|
|
33
|
+
declare function importKey(key: Uint8Array): Promise<Uint8Array>;
|
|
34
|
+
declare function importKey(key: CryptoKey): Promise<CryptoKey>;
|
|
35
|
+
declare function importKey(key: JWK_oct): Promise<Uint8Array>;
|
|
36
|
+
declare function importKey(key: Exclude<JWK, JWK_oct>, alg: string): Promise<CryptoKey>;
|
|
37
|
+
declare function importKey(key: CryptoKey | JWK | Uint8Array | string, alg?: string): Promise<CryptoKey | Uint8Array>;
|
|
38
|
+
/**
|
|
39
|
+
* Exports a CryptoKey to a JWK (JSON Web Key) format.
|
|
40
|
+
*
|
|
41
|
+
* @param key The CryptoKey to export.
|
|
42
|
+
* @param jwk Optional partial JWK to merge with the exported key, allowing overrides.
|
|
43
|
+
* @returns A Promise resolving to the exported JWK.
|
|
44
|
+
*/
|
|
45
|
+
declare function exportKey(key: CryptoKey, jwk?: Partial<JWK>): Promise<JWK>;
|
|
46
|
+
/**
|
|
47
|
+
* Wraps a Content Encryption Key (CEK) using the specified algorithm and wrapping key.
|
|
48
|
+
*
|
|
49
|
+
* @param alg The JWA key management algorithm (e.g., "A128KW", "RSA-OAEP", "PBES2-HS256+A128KW").
|
|
50
|
+
* @param keyToWrap The key to be wrapped (CEK), typically a symmetric key as Uint8Array or CryptoKey.
|
|
51
|
+
* @param wrappingKey The key used to wrap the CEK (CryptoKey, JWK, or password string/Uint8Array for PBES2).
|
|
52
|
+
* @param options Additional options required by certain algorithms (e.g., p2s, p2c for PBES2).
|
|
53
|
+
* @returns A Promise resolving to an object containing the wrapped key and any necessary parameters (iv, tag, epk, etc.).
|
|
54
|
+
*/
|
|
55
|
+
declare function wrapKey(alg: KeyManagementAlgorithm, keyToWrap: CryptoKey | Uint8Array, wrappingKey: CryptoKey | JWK | string | Uint8Array, options?: WrapKeyOptions): Promise<WrapKeyResult>;
|
|
56
|
+
/**
|
|
57
|
+
* Unwraps a Content Encryption Key (CEK) using the specified algorithm and unwrapping key.
|
|
58
|
+
*
|
|
59
|
+
* @param alg The JWA key management algorithm (e.g., "A128KW", "RSA-OAEP", "PBES2-HS256+A128KW").
|
|
60
|
+
* @param wrappedKey The wrapped key (ciphertext) as Uint8Array.
|
|
61
|
+
* @param unwrappingKey The key used to unwrap the CEK (CryptoKey, JWK, or password string/Uint8Array for PBES2).
|
|
62
|
+
* @param options Additional options required by certain algorithms (e.g., iv, tag, p2s, p2c, epk).
|
|
63
|
+
* @returns A Promise resolving to the unwrapped key (CEK) as a CryptoKey or Uint8Array.
|
|
64
|
+
*/
|
|
65
|
+
declare function unwrapKey<T extends boolean | undefined = undefined>(alg: KeyManagementAlgorithm, wrappedKey: Uint8Array, unwrappingKey: CryptoKey | JWK | string | Uint8Array, options?: UnwrapKeyOptions & {
|
|
66
|
+
returnAs?: T;
|
|
67
|
+
}): Promise<T extends false ? Uint8Array : CryptoKey>;
|
|
68
|
+
|
|
69
|
+
declare const jwk_CompositeKey: typeof CompositeKey;
|
|
70
|
+
declare const jwk_ContentEncryptionAlgorithm: typeof ContentEncryptionAlgorithm;
|
|
71
|
+
declare const jwk_DeriveKeyOptions: typeof DeriveKeyOptions;
|
|
72
|
+
declare const jwk_DeriveKeyReturn: typeof DeriveKeyReturn;
|
|
73
|
+
declare const jwk_GenerateKeyAlgorithm: typeof GenerateKeyAlgorithm;
|
|
74
|
+
declare const jwk_GenerateKeyOptions: typeof GenerateKeyOptions;
|
|
75
|
+
declare const jwk_GenerateKeyReturn: typeof GenerateKeyReturn;
|
|
76
|
+
declare const jwk_JWK: typeof JWK;
|
|
77
|
+
declare const jwk_JWKAlgorithm: typeof JWKAlgorithm;
|
|
78
|
+
declare const jwk_JWKParameters: typeof JWKParameters;
|
|
79
|
+
declare const jwk_JWK_AES_CBC_HMAC: typeof JWK_AES_CBC_HMAC;
|
|
80
|
+
declare const jwk_JWK_AES_GCM: typeof JWK_AES_GCM;
|
|
81
|
+
declare const jwk_JWK_AES_GCM_KW: typeof JWK_AES_GCM_KW;
|
|
82
|
+
declare const jwk_JWK_AES_KW: typeof JWK_AES_KW;
|
|
83
|
+
declare const jwk_JWK_Asymmetric_Algorithm: typeof JWK_Asymmetric_Algorithm;
|
|
84
|
+
declare const jwk_JWK_EC: typeof JWK_EC;
|
|
85
|
+
declare const jwk_JWK_ECDH_ES: typeof JWK_ECDH_ES;
|
|
86
|
+
declare const jwk_JWK_ECDSA: typeof JWK_ECDSA;
|
|
87
|
+
declare const jwk_JWK_EC_Private: typeof JWK_EC_Private;
|
|
88
|
+
declare const jwk_JWK_EC_Public: typeof JWK_EC_Public;
|
|
89
|
+
declare const jwk_JWK_HMAC: typeof JWK_HMAC;
|
|
90
|
+
declare const jwk_JWK_PBES2: typeof JWK_PBES2;
|
|
91
|
+
declare const jwk_JWK_RSA: typeof JWK_RSA;
|
|
92
|
+
declare const jwk_JWK_RSA_ENC: typeof JWK_RSA_ENC;
|
|
93
|
+
declare const jwk_JWK_RSA_PSS: typeof JWK_RSA_PSS;
|
|
94
|
+
declare const jwk_JWK_RSA_Private: typeof JWK_RSA_Private;
|
|
95
|
+
declare const jwk_JWK_RSA_Public: typeof JWK_RSA_Public;
|
|
96
|
+
declare const jwk_JWK_RSA_SIGN: typeof JWK_RSA_SIGN;
|
|
97
|
+
declare const jwk_JWK_Symmetric_Algorithm: typeof JWK_Symmetric_Algorithm;
|
|
98
|
+
declare const jwk_JWK_oct: typeof JWK_oct;
|
|
99
|
+
declare const jwk_KeyManagementAlgorithm: typeof KeyManagementAlgorithm;
|
|
100
|
+
declare const jwk_UnwrapKeyOptions: typeof UnwrapKeyOptions;
|
|
101
|
+
declare const jwk_WrapKeyOptions: typeof WrapKeyOptions;
|
|
102
|
+
declare const jwk_WrapKeyResult: typeof WrapKeyResult;
|
|
103
|
+
declare const jwk_deriveKeyFromPassword: typeof deriveKeyFromPassword;
|
|
104
|
+
declare const jwk_exportKey: typeof exportKey;
|
|
105
|
+
declare const jwk_generateKey: typeof generateKey;
|
|
106
|
+
declare const jwk_importKey: typeof importKey;
|
|
107
|
+
declare const jwk_unwrapKey: typeof unwrapKey;
|
|
108
|
+
declare const jwk_wrapKey: typeof wrapKey;
|
|
109
|
+
declare namespace jwk {
|
|
110
|
+
export {
|
|
111
|
+
jwk_CompositeKey as CompositeKey,
|
|
112
|
+
jwk_ContentEncryptionAlgorithm as ContentEncryptionAlgorithm,
|
|
113
|
+
jwk_DeriveKeyOptions as DeriveKeyOptions,
|
|
114
|
+
jwk_DeriveKeyReturn as DeriveKeyReturn,
|
|
115
|
+
jwk_GenerateKeyAlgorithm as GenerateKeyAlgorithm,
|
|
116
|
+
jwk_GenerateKeyOptions as GenerateKeyOptions,
|
|
117
|
+
jwk_GenerateKeyReturn as GenerateKeyReturn,
|
|
118
|
+
jwk_JWK as JWK,
|
|
119
|
+
jwk_JWKAlgorithm as JWKAlgorithm,
|
|
120
|
+
jwk_JWKParameters as JWKParameters,
|
|
121
|
+
jwk_JWK_AES_CBC_HMAC as JWK_AES_CBC_HMAC,
|
|
122
|
+
jwk_JWK_AES_GCM as JWK_AES_GCM,
|
|
123
|
+
jwk_JWK_AES_GCM_KW as JWK_AES_GCM_KW,
|
|
124
|
+
jwk_JWK_AES_KW as JWK_AES_KW,
|
|
125
|
+
jwk_JWK_Asymmetric_Algorithm as JWK_Asymmetric_Algorithm,
|
|
126
|
+
jwk_JWK_EC as JWK_EC,
|
|
127
|
+
jwk_JWK_ECDH_ES as JWK_ECDH_ES,
|
|
128
|
+
jwk_JWK_ECDSA as JWK_ECDSA,
|
|
129
|
+
jwk_JWK_EC_Private as JWK_EC_Private,
|
|
130
|
+
jwk_JWK_EC_Public as JWK_EC_Public,
|
|
131
|
+
jwk_JWK_HMAC as JWK_HMAC,
|
|
132
|
+
jwk_JWK_PBES2 as JWK_PBES2,
|
|
133
|
+
jwk_JWK_RSA as JWK_RSA,
|
|
134
|
+
jwk_JWK_RSA_ENC as JWK_RSA_ENC,
|
|
135
|
+
jwk_JWK_RSA_PSS as JWK_RSA_PSS,
|
|
136
|
+
jwk_JWK_RSA_Private as JWK_RSA_Private,
|
|
137
|
+
jwk_JWK_RSA_Public as JWK_RSA_Public,
|
|
138
|
+
jwk_JWK_RSA_SIGN as JWK_RSA_SIGN,
|
|
139
|
+
jwk_JWK_Symmetric_Algorithm as JWK_Symmetric_Algorithm,
|
|
140
|
+
jwk_JWK_oct as JWK_oct,
|
|
141
|
+
jwk_KeyManagementAlgorithm as KeyManagementAlgorithm,
|
|
142
|
+
jwk_UnwrapKeyOptions as UnwrapKeyOptions,
|
|
143
|
+
jwk_WrapKeyOptions as WrapKeyOptions,
|
|
144
|
+
jwk_WrapKeyResult as WrapKeyResult,
|
|
145
|
+
jwk_deriveKeyFromPassword as deriveKeyFromPassword,
|
|
146
|
+
jwk_exportKey as exportKey,
|
|
147
|
+
jwk_generateKey as generateKey,
|
|
148
|
+
jwk_importKey as importKey,
|
|
149
|
+
jwk_unwrapKey as unwrapKey,
|
|
150
|
+
jwk_wrapKey as wrapKey,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export { deriveKeyFromPassword as d, exportKey as e, generateKey as g, importKey as i, jwk as j, unwrapKey as u, wrapKey as w };
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { K as KeyManagementAlgorithm, d as ContentEncryptionAlgorithm, f as JWK_EC_Public, m as JWK } from './unjwt.C0bBN0AC.js';
|
|
2
|
+
import { J as JoseHeaderParameters, a as JWTClaims } from './unjwt.DoVzOnty.js';
|
|
3
|
+
|
|
4
|
+
/** Recognized JWE Header Parameters, any other Header Members may also be present. */
|
|
5
|
+
interface JWEHeaderParameters extends JoseHeaderParameters {
|
|
6
|
+
/**
|
|
7
|
+
* `alg` (Algorithm): Header Parameter - Key Management Algorithm
|
|
8
|
+
*/
|
|
9
|
+
alg?: string;
|
|
10
|
+
/**
|
|
11
|
+
* `enc` (Encryption Algorithm): Header Parameter - Content Encryption Algorithm
|
|
12
|
+
*/
|
|
13
|
+
enc?: string;
|
|
14
|
+
/**
|
|
15
|
+
* `p2c` (PBES2 Count): Header Parameter
|
|
16
|
+
*/
|
|
17
|
+
p2c?: number;
|
|
18
|
+
/**
|
|
19
|
+
* `p2s` (PBES2 Salt): Header Parameter. Base64URL encoded.
|
|
20
|
+
*/
|
|
21
|
+
p2s?: string;
|
|
22
|
+
/**
|
|
23
|
+
* "iv" (Initialization Vector) Header Parameter for AES GCM Key Wrapping. Base64URL encoded.
|
|
24
|
+
*/
|
|
25
|
+
iv?: string;
|
|
26
|
+
/**
|
|
27
|
+
* "tag" (Authentication Tag) Header Parameter for AES GCM Key Wrapping. Base64URL encoded.
|
|
28
|
+
*/
|
|
29
|
+
tag?: string;
|
|
30
|
+
/**
|
|
31
|
+
* "epk" (Ephemeral Public Key) Header Parameter for ECDH-ES.
|
|
32
|
+
*/
|
|
33
|
+
epk?: JWK_EC_Public;
|
|
34
|
+
/**
|
|
35
|
+
* "apu" (Agreement PartyUInfo) Header Parameter for ECDH-ES. Base64URL encoded.
|
|
36
|
+
*/
|
|
37
|
+
apu?: string;
|
|
38
|
+
/**
|
|
39
|
+
* "apv" (Agreement PartyVInfo) Header Parameter for ECDH-ES. Base64URL encoded.
|
|
40
|
+
*/
|
|
41
|
+
apv?: string;
|
|
42
|
+
/** Any other JWE Header member. */
|
|
43
|
+
[propName: string]: unknown;
|
|
44
|
+
}
|
|
45
|
+
/** Recognized JWE Key Management-related Header Parameters. */
|
|
46
|
+
interface JWEKeyManagementHeaderParameters {
|
|
47
|
+
apu?: Uint8Array;
|
|
48
|
+
apv?: Uint8Array;
|
|
49
|
+
/**
|
|
50
|
+
* For Internal use only
|
|
51
|
+
*/
|
|
52
|
+
p2c?: number;
|
|
53
|
+
/**
|
|
54
|
+
* For Internal use only
|
|
55
|
+
*/
|
|
56
|
+
p2s?: Uint8Array;
|
|
57
|
+
/**
|
|
58
|
+
* For Internal use only
|
|
59
|
+
*/
|
|
60
|
+
iv?: Uint8Array;
|
|
61
|
+
/**
|
|
62
|
+
* For Internal use only
|
|
63
|
+
*/
|
|
64
|
+
epk?: CryptoKey;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* JWE (JSON Web Encryption) encryption options
|
|
68
|
+
*/
|
|
69
|
+
interface JWEEncryptOptions {
|
|
70
|
+
/** JWE "alg" (Algorithm) Header Parameter. Key Management Algorithm. */
|
|
71
|
+
alg?: KeyManagementAlgorithm;
|
|
72
|
+
/** JWE "enc" (Encryption Algorithm) Header Parameter. Content Encryption Algorithm. */
|
|
73
|
+
enc?: ContentEncryptionAlgorithm;
|
|
74
|
+
/** Additional JWE Protected Header parameters. */
|
|
75
|
+
protectedHeader?: Omit<JWEHeaderParameters, "alg" | "enc" | "iv" | "tag" | "p2s" | "p2c" | "apu" | "apv">;
|
|
76
|
+
/** Initialization Vector for AES-GCMKW key wrapping. Generated if not provided. */
|
|
77
|
+
keyManagementIV?: Uint8Array;
|
|
78
|
+
/** PBES2 Salt value (p2s). Required for PBES2 algorithms. */
|
|
79
|
+
p2s?: Uint8Array;
|
|
80
|
+
/** PBES2 Iteration count (p2c). Required for PBES2 algorithms. */
|
|
81
|
+
p2c?: number;
|
|
82
|
+
/** ECDH-ES Agreement PartyUInfo. */
|
|
83
|
+
ecdhPartyUInfo?: Uint8Array;
|
|
84
|
+
/** ECDH-ES Agreement PartyVInfo. */
|
|
85
|
+
ecdhPartyVInfo?: Uint8Array;
|
|
86
|
+
/**
|
|
87
|
+
* Content Encryption Key (CEK) to use.
|
|
88
|
+
* If provided, it will be used directly. Otherwise, a CEK will be generated.
|
|
89
|
+
*/
|
|
90
|
+
cek?: Uint8Array;
|
|
91
|
+
/**
|
|
92
|
+
* Initialization Vector for content encryption.
|
|
93
|
+
* If provided, it will be used. Otherwise, one will be generated.
|
|
94
|
+
*/
|
|
95
|
+
contentEncryptionIV?: Uint8Array;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Key lookup function for JWE decryption.
|
|
99
|
+
* @param header The JWE Protected Header.
|
|
100
|
+
* @returns The key material (CryptoKey, JWK, password string, or raw Uint8Array) or a Promise resolving to it.
|
|
101
|
+
*/
|
|
102
|
+
type JWEKeyLookupFunction = (header: JWEHeaderParameters) => CryptoKey | JWK | string | Uint8Array | Promise<CryptoKey | JWK | string | Uint8Array>;
|
|
103
|
+
/**
|
|
104
|
+
* JWE (JSON Web Encryption) decryption options
|
|
105
|
+
*/
|
|
106
|
+
interface JWEDecryptOptions {
|
|
107
|
+
/** A list of allowed JWE "alg" (Algorithm) Header Parameter values for key management. */
|
|
108
|
+
algorithms?: KeyManagementAlgorithm[];
|
|
109
|
+
/** A list of allowed JWE "enc" (Encryption Algorithm) Header Parameter values for content encryption. */
|
|
110
|
+
encryptionAlgorithms?: ContentEncryptionAlgorithm[];
|
|
111
|
+
/**
|
|
112
|
+
* Critical Header Parameters to be understood and processed.
|
|
113
|
+
* If the JWE contains critical headers not in this list (and not inherently understood by the library), decryption will fail.
|
|
114
|
+
*/
|
|
115
|
+
critical?: string[];
|
|
116
|
+
/** Algorithm to import the unwrapped CEK as (e.g., { name: 'AES-GCM' }). Defaults based on 'enc'. */
|
|
117
|
+
unwrappedKeyAlgorithm?: Parameters<typeof crypto.subtle.importKey>[2];
|
|
118
|
+
/** Key usages for the unwrapped CEK. Defaults based on 'enc' (typically ['encrypt', 'decrypt']). */
|
|
119
|
+
keyUsage?: KeyUsage[];
|
|
120
|
+
/** Mark the unwrapped CEK as extractable. Defaults to true. */
|
|
121
|
+
extractable?: boolean;
|
|
122
|
+
/** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred based on JWE headers. */
|
|
123
|
+
forceUint8Array?: boolean;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Result of a JWE decryption operation.
|
|
127
|
+
*/
|
|
128
|
+
interface JWEDecryptResult<T = JWTClaims | string> {
|
|
129
|
+
/** The decrypted payload. */
|
|
130
|
+
payload: T;
|
|
131
|
+
/** The JWE Protected Header. */
|
|
132
|
+
protectedHeader: JWEHeaderParameters;
|
|
133
|
+
/** The Content Encryption Key (CEK) used for decryption, as Uint8Array. */
|
|
134
|
+
cek: Uint8Array;
|
|
135
|
+
/** The Additional Authenticated Data (AAD) used, as Uint8Array. */
|
|
136
|
+
aad: Uint8Array;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Encrypts a payload to produce a JWE Compact Serialization string.
|
|
141
|
+
*
|
|
142
|
+
* @param payload The payload to encrypt. Can be a string, Uint8Array, or a JSON-serializable object.
|
|
143
|
+
* @param key The key encryption key (KEK). Can be a CryptoKey, JWK, or for PBES2, a password string/Uint8Array.
|
|
144
|
+
* @param options JWE encryption options, including `alg` (key management algorithm) and `enc` (content encryption algorithm).
|
|
145
|
+
* @returns A Promise resolving to the JWE Compact Serialization string.
|
|
146
|
+
*/
|
|
147
|
+
declare function encrypt(payload: JWTClaims, key: JWK | string | Uint8Array, options?: JWEEncryptOptions): Promise<string>;
|
|
148
|
+
declare function encrypt(payload: string | Uint8Array | Record<string, any>, key: JWK | string | Uint8Array, options?: JWEEncryptOptions): Promise<string>;
|
|
149
|
+
declare function encrypt(payload: JWTClaims, key: CryptoKey, options: JWEEncryptOptions & {
|
|
150
|
+
alg: KeyManagementAlgorithm;
|
|
151
|
+
enc: ContentEncryptionAlgorithm;
|
|
152
|
+
}): Promise<string>;
|
|
153
|
+
declare function encrypt(payload: string | Uint8Array | Record<string, any>, key: CryptoKey, options: JWEEncryptOptions & {
|
|
154
|
+
alg: KeyManagementAlgorithm;
|
|
155
|
+
enc: ContentEncryptionAlgorithm;
|
|
156
|
+
}): Promise<string>;
|
|
157
|
+
declare function encrypt(payload: string | Uint8Array | Record<string, any>, key: CryptoKey | JWK | string | Uint8Array, options: JWEEncryptOptions & {
|
|
158
|
+
alg: KeyManagementAlgorithm;
|
|
159
|
+
enc: ContentEncryptionAlgorithm;
|
|
160
|
+
}): Promise<string>;
|
|
161
|
+
/**
|
|
162
|
+
* Decrypts a JWE Compact Serialization string.
|
|
163
|
+
*
|
|
164
|
+
* @param jwe The JWE Compact Serialization string.
|
|
165
|
+
* @param key The key decryption key (KEK) or a function to look up the key.
|
|
166
|
+
* @param options JWE decryption options.
|
|
167
|
+
* @returns A Promise resolving to an object containing the decrypted plaintext, protected header, CEK, and AAD.
|
|
168
|
+
* @throws If JWE is invalid, decryption fails, or options are not met.
|
|
169
|
+
*/
|
|
170
|
+
declare function decrypt<T = JWTClaims | string>(jwe: string, key: CryptoKey | JWK | string | Uint8Array | JWEKeyLookupFunction, options?: JWEDecryptOptions): Promise<JWEDecryptResult<T>>;
|
|
171
|
+
declare function decrypt(jwe: string, key: CryptoKey | JWK | string | Uint8Array | JWEKeyLookupFunction, options: JWEDecryptOptions & {
|
|
172
|
+
forceUint8Array: true;
|
|
173
|
+
}): Promise<JWEDecryptResult<Uint8Array>>;
|
|
174
|
+
|
|
175
|
+
type jwe_JWEDecryptOptions = JWEDecryptOptions;
|
|
176
|
+
type jwe_JWEDecryptResult<T = JWTClaims | string> = JWEDecryptResult<T>;
|
|
177
|
+
type jwe_JWEEncryptOptions = JWEEncryptOptions;
|
|
178
|
+
type jwe_JWEHeaderParameters = JWEHeaderParameters;
|
|
179
|
+
type jwe_JWEKeyLookupFunction = JWEKeyLookupFunction;
|
|
180
|
+
type jwe_JWEKeyManagementHeaderParameters = JWEKeyManagementHeaderParameters;
|
|
181
|
+
declare const jwe_decrypt: typeof decrypt;
|
|
182
|
+
declare const jwe_encrypt: typeof encrypt;
|
|
183
|
+
declare namespace jwe {
|
|
184
|
+
export { jwe_decrypt as decrypt, jwe_encrypt as encrypt };
|
|
185
|
+
export type { jwe_JWEDecryptOptions as JWEDecryptOptions, jwe_JWEDecryptResult as JWEDecryptResult, jwe_JWEEncryptOptions as JWEEncryptOptions, jwe_JWEHeaderParameters as JWEHeaderParameters, jwe_JWEKeyLookupFunction as JWEKeyLookupFunction, jwe_JWEKeyManagementHeaderParameters as JWEKeyManagementHeaderParameters };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export { encrypt as f, decrypt as g, jwe as j };
|
|
189
|
+
export type { JWEHeaderParameters as J, JWEKeyManagementHeaderParameters as a, JWEEncryptOptions as b, JWEKeyLookupFunction as c, JWEDecryptOptions as d, JWEDecryptResult as e };
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, w as JWK_PBES2, D as DeriveKeyOptions, c as DeriveKeyReturn, l as JWK_oct, m as JWK, K as KeyManagementAlgorithm, W as WrapKeyOptions, e as WrapKeyResult, U as UnwrapKeyOptions, C as CompositeKey, d as ContentEncryptionAlgorithm, A as JWKAlgorithm, J as JWKParameters, u as JWK_AES_CBC_HMAC, v as JWK_AES_GCM, t as JWK_AES_GCM_KW, s as JWK_AES_KW, z as JWK_Asymmetric_Algorithm, h as JWK_EC, x as JWK_ECDH_ES, q as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, n as JWK_HMAC, k as JWK_RSA, r as JWK_RSA_ENC, p as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, o as JWK_RSA_SIGN, y as JWK_Symmetric_Algorithm } from './unjwt.C0bBN0AC.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Generates a cryptographic key for the specified algorithm.
|
|
5
|
+
*
|
|
6
|
+
* @param alg The JWA algorithm identifier (e.g., "HS256", "RS256", "A128GCM").
|
|
7
|
+
* @param options Configuration options for key generation.
|
|
8
|
+
* @returns A Promise resolving to the generated key (CryptoKey, CryptoKeyPair, or Uint8Array) or its JWK representation.
|
|
9
|
+
*/
|
|
10
|
+
declare function generateKey<TAlg extends GenerateKeyAlgorithm, TOptions extends GenerateKeyOptions>(alg: TAlg, options?: TOptions): Promise<GenerateKeyReturn<TAlg, TOptions>>;
|
|
11
|
+
/**
|
|
12
|
+
* Derives a key from a password using PBKDF2 as specified by PBES2 algorithms.
|
|
13
|
+
*
|
|
14
|
+
* @param password The password to derive the key from (string or Uint8Array).
|
|
15
|
+
* @param alg The PBES2 algorithm identifier (e.g., "PBES2-HS256+A128KW").
|
|
16
|
+
* @param options Configuration options including salt and iterations.
|
|
17
|
+
* @returns A Promise resolving to the derived key (CryptoKey) or its JWK_oct representation.
|
|
18
|
+
*/
|
|
19
|
+
declare function deriveKeyFromPassword<TAlg extends JWK_PBES2, TOptions extends DeriveKeyOptions>(password: string | Uint8Array, alg: TAlg, options: TOptions): Promise<DeriveKeyReturn<TOptions>>;
|
|
20
|
+
/**
|
|
21
|
+
* Imports a key from various formats (CryptoKey, JWK, Uint8Array).
|
|
22
|
+
*
|
|
23
|
+
* - If `key` is a CryptoKey, it's returned directly.
|
|
24
|
+
* - If `key` is a Uint8Array, it's returned directly.
|
|
25
|
+
* - If `key` is a JWK_oct (symmetric key with 'k'), the raw key bytes are returned as Uint8Array.
|
|
26
|
+
* - If `key` is any other JWK type (asymmetric), the `alg` parameter is required, and a CryptoKey is returned.
|
|
27
|
+
*
|
|
28
|
+
* @param key The key to import.
|
|
29
|
+
* @param alg The algorithm hint, required when importing asymmetric JWKs.
|
|
30
|
+
* @returns A Promise resolving to the imported key as CryptoKey or Uint8Array.
|
|
31
|
+
*/
|
|
32
|
+
declare function importKey(key: string): Promise<Uint8Array>;
|
|
33
|
+
declare function importKey(key: Uint8Array): Promise<Uint8Array>;
|
|
34
|
+
declare function importKey(key: CryptoKey): Promise<CryptoKey>;
|
|
35
|
+
declare function importKey(key: JWK_oct): Promise<Uint8Array>;
|
|
36
|
+
declare function importKey(key: Exclude<JWK, JWK_oct>, alg: string): Promise<CryptoKey>;
|
|
37
|
+
declare function importKey(key: CryptoKey | JWK | Uint8Array | string, alg?: string): Promise<CryptoKey | Uint8Array>;
|
|
38
|
+
/**
|
|
39
|
+
* Exports a CryptoKey to a JWK (JSON Web Key) format.
|
|
40
|
+
*
|
|
41
|
+
* @param key The CryptoKey to export.
|
|
42
|
+
* @param jwk Optional partial JWK to merge with the exported key, allowing overrides.
|
|
43
|
+
* @returns A Promise resolving to the exported JWK.
|
|
44
|
+
*/
|
|
45
|
+
declare function exportKey(key: CryptoKey, jwk?: Partial<JWK>): Promise<JWK>;
|
|
46
|
+
/**
|
|
47
|
+
* Wraps a Content Encryption Key (CEK) using the specified algorithm and wrapping key.
|
|
48
|
+
*
|
|
49
|
+
* @param alg The JWA key management algorithm (e.g., "A128KW", "RSA-OAEP", "PBES2-HS256+A128KW").
|
|
50
|
+
* @param keyToWrap The key to be wrapped (CEK), typically a symmetric key as Uint8Array or CryptoKey.
|
|
51
|
+
* @param wrappingKey The key used to wrap the CEK (CryptoKey, JWK, or password string/Uint8Array for PBES2).
|
|
52
|
+
* @param options Additional options required by certain algorithms (e.g., p2s, p2c for PBES2).
|
|
53
|
+
* @returns A Promise resolving to an object containing the wrapped key and any necessary parameters (iv, tag, epk, etc.).
|
|
54
|
+
*/
|
|
55
|
+
declare function wrapKey(alg: KeyManagementAlgorithm, keyToWrap: CryptoKey | Uint8Array, wrappingKey: CryptoKey | JWK | string | Uint8Array, options?: WrapKeyOptions): Promise<WrapKeyResult>;
|
|
56
|
+
/**
|
|
57
|
+
* Unwraps a Content Encryption Key (CEK) using the specified algorithm and unwrapping key.
|
|
58
|
+
*
|
|
59
|
+
* @param alg The JWA key management algorithm (e.g., "A128KW", "RSA-OAEP", "PBES2-HS256+A128KW").
|
|
60
|
+
* @param wrappedKey The wrapped key (ciphertext) as Uint8Array.
|
|
61
|
+
* @param unwrappingKey The key used to unwrap the CEK (CryptoKey, JWK, or password string/Uint8Array for PBES2).
|
|
62
|
+
* @param options Additional options required by certain algorithms (e.g., iv, tag, p2s, p2c, epk).
|
|
63
|
+
* @returns A Promise resolving to the unwrapped key (CEK) as a CryptoKey or Uint8Array.
|
|
64
|
+
*/
|
|
65
|
+
declare function unwrapKey<T extends boolean | undefined = undefined>(alg: KeyManagementAlgorithm, wrappedKey: Uint8Array, unwrappingKey: CryptoKey | JWK | string | Uint8Array, options?: UnwrapKeyOptions & {
|
|
66
|
+
returnAs?: T;
|
|
67
|
+
}): Promise<T extends false ? Uint8Array : CryptoKey>;
|
|
68
|
+
|
|
69
|
+
declare const jwk_CompositeKey: typeof CompositeKey;
|
|
70
|
+
declare const jwk_ContentEncryptionAlgorithm: typeof ContentEncryptionAlgorithm;
|
|
71
|
+
declare const jwk_DeriveKeyOptions: typeof DeriveKeyOptions;
|
|
72
|
+
declare const jwk_DeriveKeyReturn: typeof DeriveKeyReturn;
|
|
73
|
+
declare const jwk_GenerateKeyAlgorithm: typeof GenerateKeyAlgorithm;
|
|
74
|
+
declare const jwk_GenerateKeyOptions: typeof GenerateKeyOptions;
|
|
75
|
+
declare const jwk_GenerateKeyReturn: typeof GenerateKeyReturn;
|
|
76
|
+
declare const jwk_JWK: typeof JWK;
|
|
77
|
+
declare const jwk_JWKAlgorithm: typeof JWKAlgorithm;
|
|
78
|
+
declare const jwk_JWKParameters: typeof JWKParameters;
|
|
79
|
+
declare const jwk_JWK_AES_CBC_HMAC: typeof JWK_AES_CBC_HMAC;
|
|
80
|
+
declare const jwk_JWK_AES_GCM: typeof JWK_AES_GCM;
|
|
81
|
+
declare const jwk_JWK_AES_GCM_KW: typeof JWK_AES_GCM_KW;
|
|
82
|
+
declare const jwk_JWK_AES_KW: typeof JWK_AES_KW;
|
|
83
|
+
declare const jwk_JWK_Asymmetric_Algorithm: typeof JWK_Asymmetric_Algorithm;
|
|
84
|
+
declare const jwk_JWK_EC: typeof JWK_EC;
|
|
85
|
+
declare const jwk_JWK_ECDH_ES: typeof JWK_ECDH_ES;
|
|
86
|
+
declare const jwk_JWK_ECDSA: typeof JWK_ECDSA;
|
|
87
|
+
declare const jwk_JWK_EC_Private: typeof JWK_EC_Private;
|
|
88
|
+
declare const jwk_JWK_EC_Public: typeof JWK_EC_Public;
|
|
89
|
+
declare const jwk_JWK_HMAC: typeof JWK_HMAC;
|
|
90
|
+
declare const jwk_JWK_PBES2: typeof JWK_PBES2;
|
|
91
|
+
declare const jwk_JWK_RSA: typeof JWK_RSA;
|
|
92
|
+
declare const jwk_JWK_RSA_ENC: typeof JWK_RSA_ENC;
|
|
93
|
+
declare const jwk_JWK_RSA_PSS: typeof JWK_RSA_PSS;
|
|
94
|
+
declare const jwk_JWK_RSA_Private: typeof JWK_RSA_Private;
|
|
95
|
+
declare const jwk_JWK_RSA_Public: typeof JWK_RSA_Public;
|
|
96
|
+
declare const jwk_JWK_RSA_SIGN: typeof JWK_RSA_SIGN;
|
|
97
|
+
declare const jwk_JWK_Symmetric_Algorithm: typeof JWK_Symmetric_Algorithm;
|
|
98
|
+
declare const jwk_JWK_oct: typeof JWK_oct;
|
|
99
|
+
declare const jwk_KeyManagementAlgorithm: typeof KeyManagementAlgorithm;
|
|
100
|
+
declare const jwk_UnwrapKeyOptions: typeof UnwrapKeyOptions;
|
|
101
|
+
declare const jwk_WrapKeyOptions: typeof WrapKeyOptions;
|
|
102
|
+
declare const jwk_WrapKeyResult: typeof WrapKeyResult;
|
|
103
|
+
declare const jwk_deriveKeyFromPassword: typeof deriveKeyFromPassword;
|
|
104
|
+
declare const jwk_exportKey: typeof exportKey;
|
|
105
|
+
declare const jwk_generateKey: typeof generateKey;
|
|
106
|
+
declare const jwk_importKey: typeof importKey;
|
|
107
|
+
declare const jwk_unwrapKey: typeof unwrapKey;
|
|
108
|
+
declare const jwk_wrapKey: typeof wrapKey;
|
|
109
|
+
declare namespace jwk {
|
|
110
|
+
export {
|
|
111
|
+
jwk_CompositeKey as CompositeKey,
|
|
112
|
+
jwk_ContentEncryptionAlgorithm as ContentEncryptionAlgorithm,
|
|
113
|
+
jwk_DeriveKeyOptions as DeriveKeyOptions,
|
|
114
|
+
jwk_DeriveKeyReturn as DeriveKeyReturn,
|
|
115
|
+
jwk_GenerateKeyAlgorithm as GenerateKeyAlgorithm,
|
|
116
|
+
jwk_GenerateKeyOptions as GenerateKeyOptions,
|
|
117
|
+
jwk_GenerateKeyReturn as GenerateKeyReturn,
|
|
118
|
+
jwk_JWK as JWK,
|
|
119
|
+
jwk_JWKAlgorithm as JWKAlgorithm,
|
|
120
|
+
jwk_JWKParameters as JWKParameters,
|
|
121
|
+
jwk_JWK_AES_CBC_HMAC as JWK_AES_CBC_HMAC,
|
|
122
|
+
jwk_JWK_AES_GCM as JWK_AES_GCM,
|
|
123
|
+
jwk_JWK_AES_GCM_KW as JWK_AES_GCM_KW,
|
|
124
|
+
jwk_JWK_AES_KW as JWK_AES_KW,
|
|
125
|
+
jwk_JWK_Asymmetric_Algorithm as JWK_Asymmetric_Algorithm,
|
|
126
|
+
jwk_JWK_EC as JWK_EC,
|
|
127
|
+
jwk_JWK_ECDH_ES as JWK_ECDH_ES,
|
|
128
|
+
jwk_JWK_ECDSA as JWK_ECDSA,
|
|
129
|
+
jwk_JWK_EC_Private as JWK_EC_Private,
|
|
130
|
+
jwk_JWK_EC_Public as JWK_EC_Public,
|
|
131
|
+
jwk_JWK_HMAC as JWK_HMAC,
|
|
132
|
+
jwk_JWK_PBES2 as JWK_PBES2,
|
|
133
|
+
jwk_JWK_RSA as JWK_RSA,
|
|
134
|
+
jwk_JWK_RSA_ENC as JWK_RSA_ENC,
|
|
135
|
+
jwk_JWK_RSA_PSS as JWK_RSA_PSS,
|
|
136
|
+
jwk_JWK_RSA_Private as JWK_RSA_Private,
|
|
137
|
+
jwk_JWK_RSA_Public as JWK_RSA_Public,
|
|
138
|
+
jwk_JWK_RSA_SIGN as JWK_RSA_SIGN,
|
|
139
|
+
jwk_JWK_Symmetric_Algorithm as JWK_Symmetric_Algorithm,
|
|
140
|
+
jwk_JWK_oct as JWK_oct,
|
|
141
|
+
jwk_KeyManagementAlgorithm as KeyManagementAlgorithm,
|
|
142
|
+
jwk_UnwrapKeyOptions as UnwrapKeyOptions,
|
|
143
|
+
jwk_WrapKeyOptions as WrapKeyOptions,
|
|
144
|
+
jwk_WrapKeyResult as WrapKeyResult,
|
|
145
|
+
jwk_deriveKeyFromPassword as deriveKeyFromPassword,
|
|
146
|
+
jwk_exportKey as exportKey,
|
|
147
|
+
jwk_generateKey as generateKey,
|
|
148
|
+
jwk_importKey as importKey,
|
|
149
|
+
jwk_unwrapKey as unwrapKey,
|
|
150
|
+
jwk_wrapKey as wrapKey,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export { deriveKeyFromPassword as d, exportKey as e, generateKey as g, importKey as i, jwk as j, unwrapKey as u, wrapKey as w };
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { i as importKey } from './unjwt.DKq5VF7t.mjs';
|
|
2
|
+
import { a as isJWK, b as base64UrlEncode, t as textEncoder, c as textDecoder, d as base64UrlDecode } from './unjwt.CjHZv3F3.mjs';
|
|
3
|
+
import { s as sign$1, v as verify$1 } from './unjwt.sux3_Z19.mjs';
|
|
4
|
+
|
|
5
|
+
async function sign(payload, key, options = {}) {
|
|
6
|
+
const { protectedHeader: additionalProtectedHeader } = options;
|
|
7
|
+
let { alg } = options;
|
|
8
|
+
if (!alg) {
|
|
9
|
+
if (isJWK(key) && key.alg) {
|
|
10
|
+
alg = key.alg;
|
|
11
|
+
} else {
|
|
12
|
+
throw new TypeError('JWS "alg" (Algorithm) must be provided in options');
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const signingKey = await importKey(key, alg);
|
|
16
|
+
const protectedHeader = {
|
|
17
|
+
...additionalProtectedHeader,
|
|
18
|
+
alg,
|
|
19
|
+
typ: additionalProtectedHeader?.typ ?? (typeof payload === "object" && !(payload instanceof Uint8Array) ? "JWT" : void 0)
|
|
20
|
+
// Default typ to JWT for objects
|
|
21
|
+
};
|
|
22
|
+
if (protectedHeader.typ === void 0 && typeof payload === "object" && !(payload instanceof Uint8Array)) {
|
|
23
|
+
protectedHeader.typ = "JWT";
|
|
24
|
+
protectedHeader.cty ||= "json";
|
|
25
|
+
}
|
|
26
|
+
if (protectedHeader.b64 === true) {
|
|
27
|
+
delete protectedHeader.b64;
|
|
28
|
+
}
|
|
29
|
+
const protectedHeaderString = JSON.stringify(protectedHeader);
|
|
30
|
+
const protectedHeaderEncoded = base64UrlEncode(protectedHeaderString);
|
|
31
|
+
let payloadBytes;
|
|
32
|
+
if (payload instanceof Uint8Array) {
|
|
33
|
+
payloadBytes = payload;
|
|
34
|
+
} else if (typeof payload === "string") {
|
|
35
|
+
payloadBytes = textEncoder.encode(payload);
|
|
36
|
+
} else if (typeof payload === "object" && payload !== null) {
|
|
37
|
+
payloadBytes = textEncoder.encode(JSON.stringify(payload));
|
|
38
|
+
} else {
|
|
39
|
+
throw new TypeError(
|
|
40
|
+
"Payload must be a string, Uint8Array, or a JSON-serializable object."
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
const useB64 = protectedHeader.b64 !== false;
|
|
44
|
+
const payloadEncoded = useB64 ? base64UrlEncode(payloadBytes) : textDecoder.decode(payloadBytes);
|
|
45
|
+
const signingInputString = `${protectedHeaderEncoded}.${payloadEncoded}`;
|
|
46
|
+
const signingInputBytes = textEncoder.encode(signingInputString);
|
|
47
|
+
const signatureBytes = await sign$1(alg, signingKey, signingInputBytes);
|
|
48
|
+
const signatureEncoded = base64UrlEncode(signatureBytes);
|
|
49
|
+
return `${signingInputString}.${signatureEncoded}`;
|
|
50
|
+
}
|
|
51
|
+
async function verify(jws, key, options) {
|
|
52
|
+
const parts = jws.split(".");
|
|
53
|
+
if (parts.length !== 3) {
|
|
54
|
+
throw new Error("Invalid JWS: Must contain three parts separated by dots.");
|
|
55
|
+
}
|
|
56
|
+
const [protectedHeaderEncoded, payloadEncoded, signatureEncoded] = parts;
|
|
57
|
+
let protectedHeader;
|
|
58
|
+
try {
|
|
59
|
+
const protectedHeaderJson = base64UrlDecode(protectedHeaderEncoded);
|
|
60
|
+
protectedHeader = JSON.parse(protectedHeaderJson);
|
|
61
|
+
} catch (error_) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`Invalid JWS: Protected header is not valid Base64URL or JSON (${error_ instanceof Error ? error_.message : error_})`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (!protectedHeader || typeof protectedHeader !== "object" || !protectedHeader.alg) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
'Invalid JWS: Protected header must be an object with an "alg" property.'
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
const alg = protectedHeader.alg;
|
|
72
|
+
if (options?.algorithms && !options.algorithms.includes(alg)) {
|
|
73
|
+
throw new Error(`Algorithm not allowed: ${alg}`);
|
|
74
|
+
}
|
|
75
|
+
let signatureBytes;
|
|
76
|
+
try {
|
|
77
|
+
signatureBytes = base64UrlDecode(signatureEncoded, false);
|
|
78
|
+
} catch (error_) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Invalid JWS: Signature is not valid Base64URL (${error_ instanceof Error ? error_.message : error_})`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
const resolvedKey = typeof key === "function" ? await key(protectedHeader) : key;
|
|
84
|
+
const verificationKey = await importKey(resolvedKey, alg);
|
|
85
|
+
const signingInputString = `${protectedHeaderEncoded}.${payloadEncoded}`;
|
|
86
|
+
const signingInputBytes = textEncoder.encode(signingInputString);
|
|
87
|
+
const isValid = await verify$1(
|
|
88
|
+
alg,
|
|
89
|
+
verificationKey,
|
|
90
|
+
signatureBytes,
|
|
91
|
+
signingInputBytes
|
|
92
|
+
);
|
|
93
|
+
if (!isValid) {
|
|
94
|
+
throw new Error("JWS signature verification failed.");
|
|
95
|
+
}
|
|
96
|
+
const useB64 = protectedHeader.b64 !== false;
|
|
97
|
+
let payload;
|
|
98
|
+
try {
|
|
99
|
+
if (useB64) {
|
|
100
|
+
if (options?.forceUint8Array) {
|
|
101
|
+
payload = base64UrlDecode(payloadEncoded, false);
|
|
102
|
+
} else {
|
|
103
|
+
const cty = protectedHeader.cty?.toLowerCase();
|
|
104
|
+
const isJsonOutput = protectedHeader.typ === "JWT" || cty === "json" || cty === "application/json" || cty && cty.endsWith("+json");
|
|
105
|
+
if (isJsonOutput) {
|
|
106
|
+
const decodedString = base64UrlDecode(payloadEncoded, true);
|
|
107
|
+
if (decodedString.startsWith("{") && decodedString.endsWith("}") || decodedString.startsWith("[") && decodedString.endsWith("]")) {
|
|
108
|
+
try {
|
|
109
|
+
payload = JSON.parse(decodedString);
|
|
110
|
+
} catch {
|
|
111
|
+
payload = decodedString;
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
payload = decodedString;
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
payload = base64UrlDecode(payloadEncoded);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
payload = textEncoder.encode(payloadEncoded);
|
|
122
|
+
}
|
|
123
|
+
} catch (error_) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
`Invalid JWS: Payload decoding failed (${error_ instanceof Error ? error_.message : error_})`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
if (options?.critical && protectedHeader.crit) {
|
|
129
|
+
const recognizedParams = /* @__PURE__ */ new Set([
|
|
130
|
+
...Object.keys(protectedHeader),
|
|
131
|
+
...options.critical || [],
|
|
132
|
+
"alg",
|
|
133
|
+
"typ",
|
|
134
|
+
"cty",
|
|
135
|
+
"kid",
|
|
136
|
+
"jwk",
|
|
137
|
+
"jku",
|
|
138
|
+
"x5c",
|
|
139
|
+
"x5t",
|
|
140
|
+
"x5u",
|
|
141
|
+
"b64"
|
|
142
|
+
]);
|
|
143
|
+
for (const critParam of protectedHeader.crit) {
|
|
144
|
+
if (!recognizedParams.has(critParam)) {
|
|
145
|
+
throw new Error(
|
|
146
|
+
`Critical header parameter not understood: ${critParam}`
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
} else if (!options?.critical && protectedHeader.crit) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
`Unprocessed critical header parameters: ${protectedHeader.crit.join(", ")}`
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
payload,
|
|
157
|
+
protectedHeader
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const jws = {
|
|
162
|
+
__proto__: null,
|
|
163
|
+
sign: sign,
|
|
164
|
+
verify: verify
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
export { jws as j, sign as s, verify as v };
|