unjwt 0.5.38 → 0.5.40
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 +148 -670
- package/dist/_chunks/chunk.mjs +13 -0
- package/dist/_chunks/index.d.mts +558 -0
- package/dist/_chunks/index2.d.mts +108 -0
- package/dist/_chunks/jwe.d.mts +43 -0
- package/dist/_chunks/jwe.mjs +132 -0
- package/dist/_chunks/jwk.d.mts +611 -0
- package/dist/_chunks/jwk.mjs +1592 -0
- package/dist/_chunks/jws.d.mts +242 -0
- package/dist/_chunks/jws.mjs +2070 -0
- package/dist/_chunks/jws2.mjs +598 -0
- package/dist/_chunks/utils.mjs +375 -0
- package/dist/adapters/h3.d.mts +3 -267
- package/dist/adapters/h3.mjs +3 -704
- package/dist/adapters/h3v1.d.mts +3 -8
- package/dist/adapters/h3v1.mjs +3 -7
- package/dist/adapters/h3v2.d.mts +145 -147
- package/dist/adapters/h3v2.mjs +501 -594
- package/dist/index.d.mts +6 -8
- package/dist/index.mjs +5 -4
- package/dist/jwe.d.mts +3 -4
- package/dist/jwe.mjs +2 -3
- package/dist/jwk.d.mts +3 -2
- package/dist/jwk.mjs +2 -2
- package/dist/jws.d.mts +3 -3
- package/dist/jws.mjs +2 -3
- package/dist/utils.d.mts +3 -3
- package/dist/utils.mjs +2 -1
- package/package.json +33 -31
- package/dist/shared/unjwt.4JQCoID_.d.mts +0 -161
- package/dist/shared/unjwt.B-v62plj.mjs +0 -249
- package/dist/shared/unjwt.BCi3FEWr.d.mts +0 -328
- package/dist/shared/unjwt.BYDTEWa-.d.mts +0 -269
- package/dist/shared/unjwt.Be8NxwFK.mjs +0 -438
- package/dist/shared/unjwt.BqdNUKDr.d.mts +0 -64
- package/dist/shared/unjwt.CMNtw8ek.d.mts +0 -155
- package/dist/shared/unjwt.Cy2MlkUe.d.mts +0 -174
- package/dist/shared/unjwt.D0_ccT7g.d.mts +0 -166
- package/dist/shared/unjwt.DdLN9vwI.mjs +0 -163
- package/dist/shared/unjwt.Dpl__ZAR.mjs +0 -2121
- package/dist/shared/unjwt.JdKqVANs.d.mts +0 -5
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { $ as JWK_Private, A as JWK, B as JWK_Asymmetric_Algorithm, C as DeriveKeyOptions, D as GenerateKeyAlgorithm, E as GenerateJWKReturn, F as JWK_AES_CBC_HMAC, G as JWK_EC_Public, H as JWK_ECDH_ES, I as JWK_AES_GCM, J as JWK_OKP_Private, K as JWK_HMAC, L as JWK_AES_GCM_KW, M as JWKPEMAlgorithm, N as JWKParameters, O as GenerateKeyOptions, P as JWKSet, Q as JWK_Pair, R as JWK_AES_KW, S as ContentEncryptionAlgorithm, T as GenerateJWKOptions, U as JWK_ECDSA, V as JWK_EC, W as JWK_EC_Private, X as JWK_OKP_SIGN, Y as JWK_OKP_Public, Z as JWK_PBES2, _ as JWT, a as JWEEncryptOptions, at as JWK_RSA_Public, b as JoseHeaderParameters, c as JWEKeyManagementHeaderParameters, ct as JWK_Symmetric_Algorithm, dt as UnwrapKeyOptions, et as JWK_Public, ft as WrapKeyOptions, i as JWEDecryptResult, it as JWK_RSA_Private, j as JWKAlgorithm, k as GenerateKeyReturn, lt as JWK_oct, nt as JWK_RSA_ENC, o as JWEHeaderParameters, ot as JWK_RSA_SIGN, pt as WrapKeyResult, q as JWK_OKP, r as JWEDecryptOptions, rt as JWK_RSA_PSS, s as JWEKeyLookupFunction, st as JWK_Symmetric, tt as JWK_RSA, ut as KeyManagementAlgorithm, v as JWTClaimValidationOptions, w as DeriveKeyReturn, x as CompositeKey, y as JWTClaims, z as JWK_Asymmetric } from "./index.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/core/jwe.d.ts
|
|
4
|
+
declare namespace jwe_d_exports {
|
|
5
|
+
export { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, JWEDecryptOptions, JWEDecryptResult, JWEEncryptOptions, JWEHeaderParameters, JWEKeyLookupFunction, JWEKeyManagementHeaderParameters, JWK, JWKAlgorithm, 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, JWT, JWTClaimValidationOptions, JWTClaims, JoseHeaderParameters, KeyManagementAlgorithm, UnwrapKeyOptions, WrapKeyOptions, WrapKeyResult, decrypt, encrypt };
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Encrypts a payload to produce a JWE Compact Serialization string.
|
|
9
|
+
*
|
|
10
|
+
* @param payload The payload to encrypt. Can be a string, Uint8Array, or a JSON-serializable object.
|
|
11
|
+
* @param key The key encryption key (KEK). Can be a CryptoKey, JWK, or for PBES2, a password string/Uint8Array.
|
|
12
|
+
* @param options JWE encryption options, including `alg` (key management algorithm) and `enc` (content encryption algorithm).
|
|
13
|
+
* @returns A Promise resolving to the JWE Compact Serialization string.
|
|
14
|
+
*/
|
|
15
|
+
declare function encrypt(payload: JWTClaims, key: JWK | string | Uint8Array<ArrayBuffer>, options?: JWEEncryptOptions): Promise<string>;
|
|
16
|
+
declare function encrypt(payload: string | Uint8Array<ArrayBuffer> | Record<string, any>, key: JWK | string | Uint8Array<ArrayBuffer>, options?: JWEEncryptOptions): Promise<string>;
|
|
17
|
+
declare function encrypt(payload: JWTClaims, key: CryptoKey, options: JWEEncryptOptions & {
|
|
18
|
+
alg: KeyManagementAlgorithm;
|
|
19
|
+
enc: ContentEncryptionAlgorithm;
|
|
20
|
+
}): Promise<string>;
|
|
21
|
+
declare function encrypt(payload: string | Uint8Array<ArrayBuffer> | Record<string, any>, key: CryptoKey, options: JWEEncryptOptions & {
|
|
22
|
+
alg: KeyManagementAlgorithm;
|
|
23
|
+
enc: ContentEncryptionAlgorithm;
|
|
24
|
+
}): Promise<string>;
|
|
25
|
+
declare function encrypt(payload: string | Uint8Array<ArrayBuffer> | Record<string, any>, key: CryptoKey | JWK | string | Uint8Array<ArrayBuffer>, options: JWEEncryptOptions & {
|
|
26
|
+
alg: KeyManagementAlgorithm;
|
|
27
|
+
enc: ContentEncryptionAlgorithm;
|
|
28
|
+
}): Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Decrypts a JWE Compact Serialization string.
|
|
31
|
+
*
|
|
32
|
+
* @param jwe The JWE Compact Serialization string.
|
|
33
|
+
* @param key The key decryption key (KEK) or a function to look up the key.
|
|
34
|
+
* @param options JWE decryption options.
|
|
35
|
+
* @returns A Promise resolving to an object containing the decrypted plaintext, protected header, CEK, and AAD.
|
|
36
|
+
* @throws If JWE is invalid, decryption fails, or options are not met.
|
|
37
|
+
*/
|
|
38
|
+
declare function decrypt<T extends JWTClaims | Uint8Array<ArrayBuffer> | string>(jwe: string, key: CryptoKey | JWK_Symmetric | JWK_Private | string | Uint8Array<ArrayBuffer> | JWEKeyLookupFunction, options?: JWEDecryptOptions): Promise<JWEDecryptResult<T>>;
|
|
39
|
+
declare function decrypt(jwe: string, key: CryptoKey | JWK_Symmetric | JWK_Private | string | Uint8Array<ArrayBuffer> | JWEKeyLookupFunction, options: JWEDecryptOptions & {
|
|
40
|
+
forceUint8Array: true;
|
|
41
|
+
}): Promise<JWEDecryptResult<Uint8Array<ArrayBuffer>>>;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { encrypt as n, jwe_d_exports as r, decrypt as t };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk.mjs";
|
|
2
|
+
import { A as validateCriticalHeadersJWE, C as computeJwtTimeClaims, D as decodePayloadFromBytes, M as validateJwtClaims, O as getPlaintextBytes, a as base64UrlEncode, b as sanitizeObject, c as isCryptoKey, g as randomBytes, i as base64UrlDecode, l as isCryptoKeyPair, u as isJWK, v as textEncoder, x as applyTypCtyDefaults } from "./utils.mjs";
|
|
3
|
+
import { d as unwrapKey, g as generateIV, h as encrypt$1, l as importKey, m as decrypt$1, p as encryptKey } from "./jwk.mjs";
|
|
4
|
+
//#region src/core/jwe.ts
|
|
5
|
+
var jwe_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
+
decrypt: () => decrypt,
|
|
7
|
+
encrypt: () => encrypt
|
|
8
|
+
});
|
|
9
|
+
async function encrypt(payload, key, options = {}) {
|
|
10
|
+
const { protectedHeader: additionalProtectedHeader, cek: providedCek, contentEncryptionIV: providedContentIV, keyManagementIV, p2s, p2c, ecdh } = options;
|
|
11
|
+
let { alg, enc } = options;
|
|
12
|
+
if (!alg) {
|
|
13
|
+
if (typeof key === "string" || key instanceof Uint8Array) alg = "PBES2-HS256+A128KW";
|
|
14
|
+
else if (isJWK(key)) alg = key.alg === "A128GCM" || key.alg === "A192GCM" || key.alg === "A256GCM" ? `${key.alg}KW` : key.alg;
|
|
15
|
+
}
|
|
16
|
+
if (!alg) throw new TypeError("JWE \"alg\" (Key Management Algorithm) must be provided in options or inferable from the key");
|
|
17
|
+
if (!enc) enc = isJWK(key) && "enc" in key ? key.enc : "A128GCM";
|
|
18
|
+
const jweKeyManagementParams = {};
|
|
19
|
+
if (keyManagementIV) jweKeyManagementParams.iv = keyManagementIV;
|
|
20
|
+
if (p2s) jweKeyManagementParams.p2s = p2s;
|
|
21
|
+
else if (alg?.startsWith("PBES2")) jweKeyManagementParams.p2s = randomBytes(16);
|
|
22
|
+
if (p2c) jweKeyManagementParams.p2c = p2c;
|
|
23
|
+
else if (alg?.startsWith("PBES2")) jweKeyManagementParams.p2c = 2048;
|
|
24
|
+
if (ecdh?.partyUInfo) jweKeyManagementParams.apu = ecdh.partyUInfo;
|
|
25
|
+
if (ecdh?.partyVInfo) jweKeyManagementParams.apv = ecdh.partyVInfo;
|
|
26
|
+
if (ecdh?.ephemeralKey) {
|
|
27
|
+
const { epk, epkPrivateKey } = parseEphemeralKey(ecdh.ephemeralKey);
|
|
28
|
+
jweKeyManagementParams.epk = epk;
|
|
29
|
+
jweKeyManagementParams.epkPrivateKey = epkPrivateKey;
|
|
30
|
+
}
|
|
31
|
+
const wrappingKeyMaterial = await importKey(key, alg);
|
|
32
|
+
const { cek: finalCek, encryptedKey: jweEncryptedKey, parameters: keyManagementHeaderParams } = await encryptKey(alg, enc, wrappingKeyMaterial, providedCek, jweKeyManagementParams);
|
|
33
|
+
const jweProtectedHeader = {
|
|
34
|
+
...additionalProtectedHeader,
|
|
35
|
+
...keyManagementHeaderParams,
|
|
36
|
+
alg,
|
|
37
|
+
enc
|
|
38
|
+
};
|
|
39
|
+
if (isJWK(key) && key.kid) jweProtectedHeader.kid = key.kid;
|
|
40
|
+
const protectedHeader = applyTypCtyDefaults(jweProtectedHeader, payload);
|
|
41
|
+
const plaintextBytes = getPlaintextBytes(computeJwtTimeClaims(payload, protectedHeader.typ, options.expiresIn, options.currentDate) || payload);
|
|
42
|
+
const protectedHeaderEncoded = base64UrlEncode(JSON.stringify(protectedHeader));
|
|
43
|
+
const aadBytes = textEncoder.encode(protectedHeaderEncoded);
|
|
44
|
+
const contentIVBytes = providedContentIV ?? generateIV(enc);
|
|
45
|
+
const { ciphertext: contentCiphertext, tag: contentAuthTag, iv: actualContentIV } = await encrypt$1(enc, plaintextBytes, finalCek, contentIVBytes, aadBytes);
|
|
46
|
+
if (!actualContentIV) throw new Error("Content encryption IV was not generated or returned.");
|
|
47
|
+
if (!contentAuthTag) throw new Error("Content encryption auth tag was not generated or returned.");
|
|
48
|
+
return [
|
|
49
|
+
protectedHeaderEncoded,
|
|
50
|
+
jweEncryptedKey ? base64UrlEncode(jweEncryptedKey) : "",
|
|
51
|
+
base64UrlEncode(actualContentIV),
|
|
52
|
+
base64UrlEncode(contentCiphertext),
|
|
53
|
+
base64UrlEncode(contentAuthTag)
|
|
54
|
+
].join(".");
|
|
55
|
+
}
|
|
56
|
+
async function decrypt(jwe, key, options = {}) {
|
|
57
|
+
const parts = jwe.split(".");
|
|
58
|
+
if (parts.length !== 5) throw new Error("Invalid JWE: Must contain five sections (RFC7516, section-3).");
|
|
59
|
+
const [protectedHeaderEncoded, encryptedKeyEncoded, ivEncoded, ciphertextEncoded, authTagEncoded] = parts;
|
|
60
|
+
let protectedHeader;
|
|
61
|
+
try {
|
|
62
|
+
const protectedHeaderJson = base64UrlDecode(protectedHeaderEncoded);
|
|
63
|
+
protectedHeader = sanitizeObject(JSON.parse(protectedHeaderJson));
|
|
64
|
+
} catch {
|
|
65
|
+
throw new Error("Invalid JWE: Protected header could not be decoded.");
|
|
66
|
+
}
|
|
67
|
+
if (!protectedHeader || typeof protectedHeader !== "object" || !protectedHeader.alg || !protectedHeader.enc) throw new Error("Invalid JWE: Protected header must be an object with \"alg\" and \"enc\" properties.");
|
|
68
|
+
const alg = protectedHeader.alg;
|
|
69
|
+
const enc = protectedHeader.enc;
|
|
70
|
+
if (options?.algorithms && !options.algorithms.includes(alg)) throw new Error(`Key management algorithm not allowed: ${alg}`);
|
|
71
|
+
if (options?.encryptionAlgorithms && !options.encryptionAlgorithms.includes(enc)) throw new Error(`Content encryption algorithm not allowed: ${enc}`);
|
|
72
|
+
const unwrappingKey = await importKey(typeof key === "function" ? await key(protectedHeader, jwe) : key, alg);
|
|
73
|
+
const encryptedKeyBytes = base64UrlDecode(encryptedKeyEncoded, false);
|
|
74
|
+
const contentIVBytes = base64UrlDecode(ivEncoded, false);
|
|
75
|
+
const contentAuthTagBytes = base64UrlDecode(authTagEncoded, false);
|
|
76
|
+
const ciphertextBytes = base64UrlDecode(ciphertextEncoded, false);
|
|
77
|
+
const unwrapKeyOpts = {
|
|
78
|
+
enc,
|
|
79
|
+
returnAs: enc.includes("GCM"),
|
|
80
|
+
keyUsage: options?.keyUsage || ["decrypt"]
|
|
81
|
+
};
|
|
82
|
+
if (protectedHeader.iv) unwrapKeyOpts.iv = protectedHeader.iv;
|
|
83
|
+
if (protectedHeader.tag) unwrapKeyOpts.tag = protectedHeader.tag;
|
|
84
|
+
if (protectedHeader.p2s) unwrapKeyOpts.p2s = protectedHeader.p2s;
|
|
85
|
+
if (protectedHeader.p2c) unwrapKeyOpts.p2c = protectedHeader.p2c;
|
|
86
|
+
if (protectedHeader.epk) unwrapKeyOpts.epk = protectedHeader.epk;
|
|
87
|
+
if (protectedHeader.apu) unwrapKeyOpts.apu = protectedHeader.apu;
|
|
88
|
+
if (protectedHeader.apv) unwrapKeyOpts.apv = protectedHeader.apv;
|
|
89
|
+
if (options?.unwrappedKeyAlgorithm) unwrapKeyOpts.unwrappedKeyAlgorithm = options.unwrappedKeyAlgorithm;
|
|
90
|
+
if (options?.extractable) unwrapKeyOpts.extractable = options.extractable;
|
|
91
|
+
const cek = await unwrapKey(alg, encryptedKeyBytes, unwrappingKey, unwrapKeyOpts);
|
|
92
|
+
const aadBytes = textEncoder.encode(protectedHeaderEncoded);
|
|
93
|
+
const payload = decodePayloadFromBytes(await decrypt$1(enc, cek, ciphertextBytes, contentIVBytes, contentAuthTagBytes, aadBytes), protectedHeader, options?.forceUint8Array);
|
|
94
|
+
if (protectedHeader.crit || options?.critical?.length || options?.requiredHeaders?.length) validateCriticalHeadersJWE(protectedHeader, [...options?.critical || [], ...options?.requiredHeaders || []]);
|
|
95
|
+
if (payload && typeof payload === "object" && options.validateJWT !== false && (options.validateJWT === true || protectedHeader.typ?.toLowerCase().includes("jwt")) && !options.forceUint8Array && !(payload instanceof Uint8Array)) validateJwtClaims(payload, options);
|
|
96
|
+
const result = {
|
|
97
|
+
payload,
|
|
98
|
+
protectedHeader
|
|
99
|
+
};
|
|
100
|
+
if (options.returnCek) {
|
|
101
|
+
result.cek = isCryptoKey(cek) ? new Uint8Array(await crypto.subtle.exportKey("raw", cek)) : cek;
|
|
102
|
+
result.aad = aadBytes;
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
function parseEphemeralKey(ephemeralKey) {
|
|
107
|
+
let epk;
|
|
108
|
+
let epkPrivateKey;
|
|
109
|
+
if (isCryptoKeyPair(ephemeralKey)) {
|
|
110
|
+
epk = ephemeralKey.publicKey;
|
|
111
|
+
epkPrivateKey = ephemeralKey.privateKey;
|
|
112
|
+
} else if (typeof ephemeralKey === "object" && ephemeralKey !== null && "publicKey" in ephemeralKey && "privateKey" in ephemeralKey) {
|
|
113
|
+
const candidate = ephemeralKey;
|
|
114
|
+
if (!candidate.publicKey || !candidate.privateKey) throw new TypeError("ECDH-ES custom ephemeral key must include both publicKey and privateKey.");
|
|
115
|
+
epk = candidate.publicKey;
|
|
116
|
+
epkPrivateKey = candidate.privateKey;
|
|
117
|
+
} else if (isCryptoKey(ephemeralKey)) {
|
|
118
|
+
if (ephemeralKey.type !== "private") throw new TypeError("ECDH-ES custom ephemeral CryptoKey must include private key material.");
|
|
119
|
+
epk = ephemeralKey;
|
|
120
|
+
epkPrivateKey = ephemeralKey;
|
|
121
|
+
} else if (isJWK(ephemeralKey)) {
|
|
122
|
+
if (!("d" in ephemeralKey) || typeof ephemeralKey.d !== "string") throw new TypeError("ECDH-ES custom ephemeral JWK must include private parameter \"d\".");
|
|
123
|
+
epk = ephemeralKey;
|
|
124
|
+
epkPrivateKey = ephemeralKey;
|
|
125
|
+
} else throw new TypeError("Unsupported ECDH-ES ephemeral key material provided in options.");
|
|
126
|
+
return {
|
|
127
|
+
epk,
|
|
128
|
+
epkPrivateKey
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
//#endregion
|
|
132
|
+
export { encrypt as n, jwe_exports as r, decrypt as t };
|