unjwt 0.2.1 → 0.4.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/LICENSE +21 -21
- package/README.md +348 -48
- package/dist/index.d.mts +5 -79
- package/dist/index.d.ts +5 -79
- package/dist/index.mjs +5 -141
- package/dist/jwe.d.mts +3 -0
- package/dist/jwe.d.ts +3 -0
- package/dist/jwe.mjs +3 -0
- package/dist/jwk.d.mts +2 -0
- package/dist/jwk.d.ts +2 -0
- package/dist/jwk.mjs +2 -0
- package/dist/jws.d.mts +3 -0
- package/dist/jws.d.ts +3 -0
- package/dist/jws.mjs +4 -0
- package/dist/shared/unjwt.4uzJv0cA.d.mts +110 -0
- package/dist/shared/unjwt.A7HKFx3n.d.mts +52 -0
- package/dist/shared/unjwt.BdjvpIUY.d.mts +79 -0
- package/dist/shared/unjwt.BmjmfFWo.d.ts +79 -0
- package/dist/shared/unjwt.C9ELsKeK.mjs +120 -0
- package/dist/shared/unjwt.CVUxLNoy.d.ts +52 -0
- package/dist/shared/unjwt.ChP7VhqI.d.ts +110 -0
- package/dist/shared/unjwt.DD0p18Gd.mjs +81 -0
- package/dist/shared/unjwt.DGsIvgyn.d.ts +106 -0
- package/dist/shared/unjwt.DPTwlVpK.mjs +28 -0
- package/dist/shared/unjwt.Dr-mxtq9.d.mts +106 -0
- package/dist/shared/unjwt.KOd0vXyx.mjs +90 -0
- package/dist/shared/unjwt._GivNhs2.mjs +213 -0
- package/dist/shared/unjwt.zb2wBNCJ.d.mts +82 -0
- package/dist/shared/unjwt.zb2wBNCJ.d.ts +82 -0
- package/package.json +43 -53
- package/dist/crypto/node.d.mts +0 -5
- package/dist/crypto/node.d.ts +0 -5
- package/dist/crypto/node.mjs +0 -11
- package/dist/crypto/web.d.mts +0 -5
- package/dist/crypto/web.d.ts +0 -5
- package/dist/crypto/web.mjs +0 -10
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { J as JWK } from './unjwt.zb2wBNCJ.js';
|
|
2
|
+
|
|
3
|
+
/** Header Parameters common to JWE and JWS */
|
|
4
|
+
interface JoseHeaderParameters {
|
|
5
|
+
/** "kid" (Key ID) Header Parameter */
|
|
6
|
+
kid?: string;
|
|
7
|
+
/** "x5t" (X.509 Certificate SHA-1 Thumbprint) Header Parameter */
|
|
8
|
+
x5t?: string;
|
|
9
|
+
/** "x5c" (X.509 Certificate Chain) Header Parameter */
|
|
10
|
+
x5c?: string[];
|
|
11
|
+
/** "x5u" (X.509 URL) Header Parameter */
|
|
12
|
+
x5u?: string;
|
|
13
|
+
/** "jku" (JWK Set URL) Header Parameter */
|
|
14
|
+
jku?: string;
|
|
15
|
+
/** "jwk" (JSON Web Key) Header Parameter */
|
|
16
|
+
jwk?: Pick<JWK, "kty" | "crv" | "x" | "y" | "e" | "n">;
|
|
17
|
+
/** "typ" (Type) Header Parameter */
|
|
18
|
+
typ?: string;
|
|
19
|
+
/** "cty" (Content Type) Header Parameter */
|
|
20
|
+
cty?: string;
|
|
21
|
+
/** "crit" (Critical) Header Parameter */
|
|
22
|
+
crit?: string[];
|
|
23
|
+
/** Any other JWS/JWE Header member. */
|
|
24
|
+
[propName: string]: unknown;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Supported key wrapping algorithms
|
|
29
|
+
*/
|
|
30
|
+
declare const KEY_WRAPPING_ALGORITHMS: Readonly<{
|
|
31
|
+
"PBES2-HS256+A128KW": {
|
|
32
|
+
hash: string;
|
|
33
|
+
keyLength: number;
|
|
34
|
+
};
|
|
35
|
+
"PBES2-HS384+A192KW": {
|
|
36
|
+
hash: string;
|
|
37
|
+
keyLength: number;
|
|
38
|
+
};
|
|
39
|
+
"PBES2-HS512+A256KW": {
|
|
40
|
+
hash: string;
|
|
41
|
+
keyLength: number;
|
|
42
|
+
};
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Supported JWS symmetric algorithms
|
|
46
|
+
*/
|
|
47
|
+
declare const JWS_SYMMETRIC_ALGORITHMS: Readonly<{
|
|
48
|
+
HS256: {
|
|
49
|
+
hash: string;
|
|
50
|
+
};
|
|
51
|
+
HS384: {
|
|
52
|
+
hash: string;
|
|
53
|
+
};
|
|
54
|
+
HS512: {
|
|
55
|
+
hash: string;
|
|
56
|
+
};
|
|
57
|
+
}>;
|
|
58
|
+
/**
|
|
59
|
+
* Supported content encryption algorithms
|
|
60
|
+
*/
|
|
61
|
+
declare const CONTENT_ENCRYPTION_ALGORITHMS: Readonly<{
|
|
62
|
+
A128GCM: {
|
|
63
|
+
type: string;
|
|
64
|
+
keyLength: number;
|
|
65
|
+
tagLength: number;
|
|
66
|
+
ivLength: number;
|
|
67
|
+
};
|
|
68
|
+
A192GCM: {
|
|
69
|
+
type: string;
|
|
70
|
+
keyLength: number;
|
|
71
|
+
tagLength: number;
|
|
72
|
+
ivLength: number;
|
|
73
|
+
};
|
|
74
|
+
A256GCM: {
|
|
75
|
+
type: string;
|
|
76
|
+
keyLength: number;
|
|
77
|
+
tagLength: number;
|
|
78
|
+
ivLength: number;
|
|
79
|
+
};
|
|
80
|
+
"A128CBC-HS256": {
|
|
81
|
+
type: string;
|
|
82
|
+
keyLength: number;
|
|
83
|
+
encKeyLength: number;
|
|
84
|
+
macKeyLength: number;
|
|
85
|
+
tagLength: number;
|
|
86
|
+
ivLength: number;
|
|
87
|
+
macAlgorithm: string;
|
|
88
|
+
};
|
|
89
|
+
"A192CBC-HS384": {
|
|
90
|
+
type: string;
|
|
91
|
+
keyLength: number;
|
|
92
|
+
encKeyLength: number;
|
|
93
|
+
macKeyLength: number;
|
|
94
|
+
tagLength: number;
|
|
95
|
+
ivLength: number;
|
|
96
|
+
macAlgorithm: string;
|
|
97
|
+
};
|
|
98
|
+
"A256CBC-HS512": {
|
|
99
|
+
type: string;
|
|
100
|
+
keyLength: number;
|
|
101
|
+
encKeyLength: number;
|
|
102
|
+
macKeyLength: number;
|
|
103
|
+
tagLength: number;
|
|
104
|
+
ivLength: number;
|
|
105
|
+
macAlgorithm: string;
|
|
106
|
+
};
|
|
107
|
+
}>;
|
|
108
|
+
|
|
109
|
+
export { CONTENT_ENCRYPTION_ALGORITHMS as C, KEY_WRAPPING_ALGORITHMS as K, JWS_SYMMETRIC_ALGORITHMS as a };
|
|
110
|
+
export type { JoseHeaderParameters as J };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const KEY_WRAPPING_ALGORITHMS = /* @__PURE__ */ Object.freeze({
|
|
2
|
+
"PBES2-HS256+A128KW": {
|
|
3
|
+
hash: "SHA-256",
|
|
4
|
+
keyLength: 128
|
|
5
|
+
},
|
|
6
|
+
"PBES2-HS384+A192KW": {
|
|
7
|
+
hash: "SHA-384",
|
|
8
|
+
keyLength: 192
|
|
9
|
+
},
|
|
10
|
+
"PBES2-HS512+A256KW": {
|
|
11
|
+
hash: "SHA-512",
|
|
12
|
+
keyLength: 256
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const JWS_SYMMETRIC_ALGORITHMS = /* @__PURE__ */ Object.freeze({
|
|
16
|
+
HS256: { hash: "SHA-256" },
|
|
17
|
+
HS384: { hash: "SHA-384" },
|
|
18
|
+
HS512: { hash: "SHA-512" }
|
|
19
|
+
});
|
|
20
|
+
const CONTENT_ENCRYPTION_ALGORITHMS = /* @__PURE__ */ Object.freeze({
|
|
21
|
+
// GCM algorithms
|
|
22
|
+
A128GCM: {
|
|
23
|
+
type: "gcm",
|
|
24
|
+
keyLength: 128,
|
|
25
|
+
tagLength: 16,
|
|
26
|
+
ivLength: 12
|
|
27
|
+
},
|
|
28
|
+
A192GCM: {
|
|
29
|
+
type: "gcm",
|
|
30
|
+
keyLength: 192,
|
|
31
|
+
tagLength: 16,
|
|
32
|
+
ivLength: 12
|
|
33
|
+
},
|
|
34
|
+
A256GCM: {
|
|
35
|
+
type: "gcm",
|
|
36
|
+
keyLength: 256,
|
|
37
|
+
tagLength: 16,
|
|
38
|
+
ivLength: 12
|
|
39
|
+
},
|
|
40
|
+
// TODO: implement future CBC algorithms
|
|
41
|
+
"A128CBC-HS256": {
|
|
42
|
+
type: "cbc",
|
|
43
|
+
keyLength: 256,
|
|
44
|
+
// Combined key length (encryption + HMAC)
|
|
45
|
+
encKeyLength: 128,
|
|
46
|
+
macKeyLength: 128,
|
|
47
|
+
tagLength: 16,
|
|
48
|
+
ivLength: 16,
|
|
49
|
+
macAlgorithm: "SHA-256"
|
|
50
|
+
},
|
|
51
|
+
"A192CBC-HS384": {
|
|
52
|
+
type: "cbc",
|
|
53
|
+
keyLength: 384,
|
|
54
|
+
// Combined key length (encryption + HMAC)
|
|
55
|
+
encKeyLength: 192,
|
|
56
|
+
macKeyLength: 192,
|
|
57
|
+
tagLength: 24,
|
|
58
|
+
ivLength: 16,
|
|
59
|
+
macAlgorithm: "SHA-384"
|
|
60
|
+
},
|
|
61
|
+
"A256CBC-HS512": {
|
|
62
|
+
type: "cbc",
|
|
63
|
+
keyLength: 512,
|
|
64
|
+
// Combined key length (encryption + HMAC)
|
|
65
|
+
encKeyLength: 256,
|
|
66
|
+
macKeyLength: 256,
|
|
67
|
+
tagLength: 32,
|
|
68
|
+
ivLength: 16,
|
|
69
|
+
macAlgorithm: "SHA-512"
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
function lookupAlgorithm(alg, supportedAlgorithms, algorithmType) {
|
|
74
|
+
const config = supportedAlgorithms[alg];
|
|
75
|
+
if (!config) {
|
|
76
|
+
throw new Error(`Unsupported ${algorithmType} algorithm: ${String(alg)}`);
|
|
77
|
+
}
|
|
78
|
+
return { ...config, alg };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export { CONTENT_ENCRYPTION_ALGORITHMS as C, JWS_SYMMETRIC_ALGORITHMS as J, KEY_WRAPPING_ALGORITHMS as K, lookupAlgorithm as l };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { K as KEY_WRAPPING_ALGORITHMS, C as CONTENT_ENCRYPTION_ALGORITHMS, J as JoseHeaderParameters } from './unjwt.ChP7VhqI.js';
|
|
2
|
+
|
|
3
|
+
type KeyWrappingAlgorithmType = keyof typeof KEY_WRAPPING_ALGORITHMS;
|
|
4
|
+
type ContentEncryptionAlgorithmType = keyof typeof CONTENT_ENCRYPTION_ALGORITHMS;
|
|
5
|
+
interface JWEHeaderParameters extends JoseHeaderParameters {
|
|
6
|
+
/**
|
|
7
|
+
* `alg` (Algorithm): Header Parameter
|
|
8
|
+
*
|
|
9
|
+
* @default "PBES2-HS256+A128KW"
|
|
10
|
+
*/
|
|
11
|
+
alg?: KeyWrappingAlgorithmType;
|
|
12
|
+
/**
|
|
13
|
+
* `enc` (Encryption Algorithm): Header Parameter
|
|
14
|
+
*
|
|
15
|
+
* @default "A256GCM"
|
|
16
|
+
*/
|
|
17
|
+
enc?: ContentEncryptionAlgorithmType;
|
|
18
|
+
/**
|
|
19
|
+
* `p2c` (PBES2 Count): Header Parameter
|
|
20
|
+
*
|
|
21
|
+
* @default 2048
|
|
22
|
+
*/
|
|
23
|
+
p2c?: number;
|
|
24
|
+
/**
|
|
25
|
+
* `p2s` (PBES2 Salt): Header Parameter
|
|
26
|
+
*/
|
|
27
|
+
p2s?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* JWE (JSON Web Encryption) options
|
|
31
|
+
*/
|
|
32
|
+
interface JWEOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Number of iterations for PBES2 key derivation
|
|
35
|
+
* Also accessible as `protectedHeader.p2c`
|
|
36
|
+
*
|
|
37
|
+
* @default 2048
|
|
38
|
+
*/
|
|
39
|
+
iterations?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Size of the salt for PBES2 key derivation
|
|
42
|
+
*
|
|
43
|
+
* @default 16
|
|
44
|
+
*/
|
|
45
|
+
saltSize?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Additional protected header parameters
|
|
48
|
+
*/
|
|
49
|
+
protectedHeader?: JWEHeaderParameters;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The default settings. */
|
|
53
|
+
declare const JWE_DEFAULTS: Readonly<{
|
|
54
|
+
saltSize: 16;
|
|
55
|
+
iterations: 2048;
|
|
56
|
+
alg: KeyWrappingAlgorithmType;
|
|
57
|
+
enc: ContentEncryptionAlgorithmType;
|
|
58
|
+
}>;
|
|
59
|
+
/**
|
|
60
|
+
* Seal (encrypt) data using JWE with configurable algorithms
|
|
61
|
+
* @param data The data to encrypt
|
|
62
|
+
* @param password The password to use for encryption
|
|
63
|
+
* @param options Optional parameters
|
|
64
|
+
* @returns Promise resolving to the compact JWE token
|
|
65
|
+
*/
|
|
66
|
+
declare function seal(data: string | Uint8Array, password: string | Uint8Array, options?: JWEOptions): Promise<string>;
|
|
67
|
+
/**
|
|
68
|
+
* Decrypts a JWE (JSON Web Encryption) token
|
|
69
|
+
* @param token The JWE token string in compact serialization format
|
|
70
|
+
* @param password The password used to derive the encryption key
|
|
71
|
+
* @returns The decrypted content as a string
|
|
72
|
+
*/
|
|
73
|
+
declare function unseal(token: string, password: string | Uint8Array): Promise<string>;
|
|
74
|
+
/**
|
|
75
|
+
* Decrypts a JWE (JSON Web Encryption) token
|
|
76
|
+
* @param token The JWE token string in compact serialization format
|
|
77
|
+
* @param password The password used to derive the encryption key
|
|
78
|
+
* @param options Decryption options
|
|
79
|
+
* @returns The decrypted content as a string
|
|
80
|
+
*/
|
|
81
|
+
declare function unseal(token: string, password: string | Uint8Array, options: {
|
|
82
|
+
textOutput: true;
|
|
83
|
+
}): Promise<string>;
|
|
84
|
+
/**
|
|
85
|
+
* Decrypts a JWE (JSON Web Encryption) token
|
|
86
|
+
* @param token The JWE token string in compact serialization format
|
|
87
|
+
* @param password The password used to derive the encryption key
|
|
88
|
+
* @param options Decryption options
|
|
89
|
+
* @returns The decrypted content as a Uint8Array
|
|
90
|
+
*/
|
|
91
|
+
declare function unseal(token: string, password: string | Uint8Array, options: {
|
|
92
|
+
textOutput: false;
|
|
93
|
+
}): Promise<Uint8Array>;
|
|
94
|
+
|
|
95
|
+
declare const jwe_JWE_DEFAULTS: typeof JWE_DEFAULTS;
|
|
96
|
+
declare const jwe_seal: typeof seal;
|
|
97
|
+
declare const jwe_unseal: typeof unseal;
|
|
98
|
+
declare namespace jwe {
|
|
99
|
+
export {
|
|
100
|
+
jwe_JWE_DEFAULTS as JWE_DEFAULTS,
|
|
101
|
+
jwe_seal as seal,
|
|
102
|
+
jwe_unseal as unseal,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export { JWE_DEFAULTS as J, jwe as j, seal as s, unseal as u };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const textEncoder = /* @__PURE__ */ new TextEncoder();
|
|
2
|
+
const textDecoder = /* @__PURE__ */ new TextDecoder();
|
|
3
|
+
function base64UrlEncode(data) {
|
|
4
|
+
return btoa(String.fromCodePoint(...data)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
5
|
+
}
|
|
6
|
+
function base64UrlDecode(str) {
|
|
7
|
+
if (!str) {
|
|
8
|
+
return new Uint8Array(0);
|
|
9
|
+
}
|
|
10
|
+
str = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
11
|
+
while (str.length % 4) str += "=";
|
|
12
|
+
return Uint8Array.from(atob(str), (b) => b.codePointAt(0));
|
|
13
|
+
}
|
|
14
|
+
function randomBytes(length) {
|
|
15
|
+
return crypto.getRandomValues(new Uint8Array(length));
|
|
16
|
+
}
|
|
17
|
+
function concatUint8Arrays(...arrays) {
|
|
18
|
+
const totalLength = arrays.reduce((length, arr) => length + arr.length, 0);
|
|
19
|
+
const result = new Uint8Array(totalLength);
|
|
20
|
+
let offset = 0;
|
|
21
|
+
for (const arr of arrays) {
|
|
22
|
+
result.set(arr, offset);
|
|
23
|
+
offset += arr.length;
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { textDecoder as a, base64UrlEncode as b, base64UrlDecode as c, concatUint8Arrays as d, randomBytes as r, textEncoder as t };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { K as KEY_WRAPPING_ALGORITHMS, C as CONTENT_ENCRYPTION_ALGORITHMS, J as JoseHeaderParameters } from './unjwt.4uzJv0cA.mjs';
|
|
2
|
+
|
|
3
|
+
type KeyWrappingAlgorithmType = keyof typeof KEY_WRAPPING_ALGORITHMS;
|
|
4
|
+
type ContentEncryptionAlgorithmType = keyof typeof CONTENT_ENCRYPTION_ALGORITHMS;
|
|
5
|
+
interface JWEHeaderParameters extends JoseHeaderParameters {
|
|
6
|
+
/**
|
|
7
|
+
* `alg` (Algorithm): Header Parameter
|
|
8
|
+
*
|
|
9
|
+
* @default "PBES2-HS256+A128KW"
|
|
10
|
+
*/
|
|
11
|
+
alg?: KeyWrappingAlgorithmType;
|
|
12
|
+
/**
|
|
13
|
+
* `enc` (Encryption Algorithm): Header Parameter
|
|
14
|
+
*
|
|
15
|
+
* @default "A256GCM"
|
|
16
|
+
*/
|
|
17
|
+
enc?: ContentEncryptionAlgorithmType;
|
|
18
|
+
/**
|
|
19
|
+
* `p2c` (PBES2 Count): Header Parameter
|
|
20
|
+
*
|
|
21
|
+
* @default 2048
|
|
22
|
+
*/
|
|
23
|
+
p2c?: number;
|
|
24
|
+
/**
|
|
25
|
+
* `p2s` (PBES2 Salt): Header Parameter
|
|
26
|
+
*/
|
|
27
|
+
p2s?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* JWE (JSON Web Encryption) options
|
|
31
|
+
*/
|
|
32
|
+
interface JWEOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Number of iterations for PBES2 key derivation
|
|
35
|
+
* Also accessible as `protectedHeader.p2c`
|
|
36
|
+
*
|
|
37
|
+
* @default 2048
|
|
38
|
+
*/
|
|
39
|
+
iterations?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Size of the salt for PBES2 key derivation
|
|
42
|
+
*
|
|
43
|
+
* @default 16
|
|
44
|
+
*/
|
|
45
|
+
saltSize?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Additional protected header parameters
|
|
48
|
+
*/
|
|
49
|
+
protectedHeader?: JWEHeaderParameters;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The default settings. */
|
|
53
|
+
declare const JWE_DEFAULTS: Readonly<{
|
|
54
|
+
saltSize: 16;
|
|
55
|
+
iterations: 2048;
|
|
56
|
+
alg: KeyWrappingAlgorithmType;
|
|
57
|
+
enc: ContentEncryptionAlgorithmType;
|
|
58
|
+
}>;
|
|
59
|
+
/**
|
|
60
|
+
* Seal (encrypt) data using JWE with configurable algorithms
|
|
61
|
+
* @param data The data to encrypt
|
|
62
|
+
* @param password The password to use for encryption
|
|
63
|
+
* @param options Optional parameters
|
|
64
|
+
* @returns Promise resolving to the compact JWE token
|
|
65
|
+
*/
|
|
66
|
+
declare function seal(data: string | Uint8Array, password: string | Uint8Array, options?: JWEOptions): Promise<string>;
|
|
67
|
+
/**
|
|
68
|
+
* Decrypts a JWE (JSON Web Encryption) token
|
|
69
|
+
* @param token The JWE token string in compact serialization format
|
|
70
|
+
* @param password The password used to derive the encryption key
|
|
71
|
+
* @returns The decrypted content as a string
|
|
72
|
+
*/
|
|
73
|
+
declare function unseal(token: string, password: string | Uint8Array): Promise<string>;
|
|
74
|
+
/**
|
|
75
|
+
* Decrypts a JWE (JSON Web Encryption) token
|
|
76
|
+
* @param token The JWE token string in compact serialization format
|
|
77
|
+
* @param password The password used to derive the encryption key
|
|
78
|
+
* @param options Decryption options
|
|
79
|
+
* @returns The decrypted content as a string
|
|
80
|
+
*/
|
|
81
|
+
declare function unseal(token: string, password: string | Uint8Array, options: {
|
|
82
|
+
textOutput: true;
|
|
83
|
+
}): Promise<string>;
|
|
84
|
+
/**
|
|
85
|
+
* Decrypts a JWE (JSON Web Encryption) token
|
|
86
|
+
* @param token The JWE token string in compact serialization format
|
|
87
|
+
* @param password The password used to derive the encryption key
|
|
88
|
+
* @param options Decryption options
|
|
89
|
+
* @returns The decrypted content as a Uint8Array
|
|
90
|
+
*/
|
|
91
|
+
declare function unseal(token: string, password: string | Uint8Array, options: {
|
|
92
|
+
textOutput: false;
|
|
93
|
+
}): Promise<Uint8Array>;
|
|
94
|
+
|
|
95
|
+
declare const jwe_JWE_DEFAULTS: typeof JWE_DEFAULTS;
|
|
96
|
+
declare const jwe_seal: typeof seal;
|
|
97
|
+
declare const jwe_unseal: typeof unseal;
|
|
98
|
+
declare namespace jwe {
|
|
99
|
+
export {
|
|
100
|
+
jwe_JWE_DEFAULTS as JWE_DEFAULTS,
|
|
101
|
+
jwe_seal as seal,
|
|
102
|
+
jwe_unseal as unseal,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export { JWE_DEFAULTS as J, jwe as j, seal as s, unseal as u };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { b as base64UrlEncode, t as textEncoder, a as textDecoder, c as base64UrlDecode } from './unjwt.DPTwlVpK.mjs';
|
|
2
|
+
import { l as lookupAlgorithm, J as JWS_SYMMETRIC_ALGORITHMS } from './unjwt.DD0p18Gd.mjs';
|
|
3
|
+
import { i as importKey } from './unjwt.C9ELsKeK.mjs';
|
|
4
|
+
|
|
5
|
+
const JWS_DEFAULTS = /* @__PURE__ */ Object.freeze({
|
|
6
|
+
alg: "HS256"
|
|
7
|
+
});
|
|
8
|
+
async function sign(payload, secret, options = {}) {
|
|
9
|
+
if (!secret) {
|
|
10
|
+
throw new Error("Missing secret key");
|
|
11
|
+
}
|
|
12
|
+
const protectedHeader = options.protectedHeader || {};
|
|
13
|
+
const alg = protectedHeader.alg || JWS_DEFAULTS.alg;
|
|
14
|
+
const algConfig = validateJwsAlgorithm(alg);
|
|
15
|
+
const header = {
|
|
16
|
+
alg,
|
|
17
|
+
typ: "JWT",
|
|
18
|
+
...protectedHeader
|
|
19
|
+
};
|
|
20
|
+
const encodedHeader = base64UrlEncode(
|
|
21
|
+
textEncoder.encode(JSON.stringify(header))
|
|
22
|
+
);
|
|
23
|
+
const encodedPayload = base64UrlEncode(
|
|
24
|
+
typeof payload === "string" ? textEncoder.encode(payload) : payload
|
|
25
|
+
);
|
|
26
|
+
const signingInput = textEncoder.encode(`${encodedHeader}.${encodedPayload}`);
|
|
27
|
+
const cryptoKey = await importKey(
|
|
28
|
+
secret,
|
|
29
|
+
{ name: "HMAC", hash: algConfig.hash },
|
|
30
|
+
false,
|
|
31
|
+
["sign"]
|
|
32
|
+
);
|
|
33
|
+
const signature = await crypto.subtle.sign(
|
|
34
|
+
{ name: "HMAC" },
|
|
35
|
+
cryptoKey,
|
|
36
|
+
signingInput
|
|
37
|
+
);
|
|
38
|
+
const encodedSignature = base64UrlEncode(new Uint8Array(signature));
|
|
39
|
+
return `${encodedHeader}.${encodedPayload}.${encodedSignature}`;
|
|
40
|
+
}
|
|
41
|
+
async function verify(token, secret, options = {}) {
|
|
42
|
+
if (!token) {
|
|
43
|
+
throw new Error("Missing JWS token");
|
|
44
|
+
}
|
|
45
|
+
if (!secret) {
|
|
46
|
+
throw new Error("Missing secret key");
|
|
47
|
+
}
|
|
48
|
+
const textOutput = options.textOutput !== false;
|
|
49
|
+
const parts = token.split(".");
|
|
50
|
+
if (parts.length !== 3) {
|
|
51
|
+
throw new Error("Invalid JWS token format");
|
|
52
|
+
}
|
|
53
|
+
const [encodedHeader, encodedPayload, encodedSignature] = parts;
|
|
54
|
+
const header = JSON.parse(
|
|
55
|
+
textDecoder.decode(base64UrlDecode(encodedHeader))
|
|
56
|
+
);
|
|
57
|
+
const alg = header.alg;
|
|
58
|
+
const algConfig = validateJwsAlgorithm(alg);
|
|
59
|
+
const signingInput = textEncoder.encode(`${encodedHeader}.${encodedPayload}`);
|
|
60
|
+
const signature = base64UrlDecode(encodedSignature);
|
|
61
|
+
const cryptoKey = await importKey(
|
|
62
|
+
secret,
|
|
63
|
+
{ name: "HMAC", hash: algConfig.hash },
|
|
64
|
+
false,
|
|
65
|
+
["verify"]
|
|
66
|
+
);
|
|
67
|
+
const isValid = await crypto.subtle.verify(
|
|
68
|
+
{ name: "HMAC" },
|
|
69
|
+
cryptoKey,
|
|
70
|
+
signature,
|
|
71
|
+
signingInput
|
|
72
|
+
);
|
|
73
|
+
if (!isValid) {
|
|
74
|
+
throw new Error("Signature verification failed");
|
|
75
|
+
}
|
|
76
|
+
const payloadBytes = base64UrlDecode(encodedPayload);
|
|
77
|
+
return textOutput ? textDecoder.decode(payloadBytes) : payloadBytes;
|
|
78
|
+
}
|
|
79
|
+
function validateJwsAlgorithm(alg) {
|
|
80
|
+
return lookupAlgorithm(alg, JWS_SYMMETRIC_ALGORITHMS, "JWS");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const jws = {
|
|
84
|
+
__proto__: null,
|
|
85
|
+
JWS_DEFAULTS: JWS_DEFAULTS,
|
|
86
|
+
sign: sign,
|
|
87
|
+
verify: verify
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export { JWS_DEFAULTS as J, jws as j, sign as s, verify as v };
|