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,100 @@
|
|
|
1
|
+
import { n as JWK_HMAC, o as JWK_RSA_SIGN, p as JWK_RSA_PSS, q as JWK_ECDSA, m as JWK } from './unjwt.C0bBN0AC.mjs';
|
|
2
|
+
import { J as JoseHeaderParameters, a as JWTClaims } from './unjwt.D90BbcE1.mjs';
|
|
3
|
+
|
|
4
|
+
type JWS_SIGN_EXTRA = "Ed25519" | "EdDSA";
|
|
5
|
+
/** JWS Signing Algorithm Identifier. */
|
|
6
|
+
type JWSAlgorithm = JWK_HMAC | JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWS_SIGN_EXTRA;
|
|
7
|
+
/** Recognized JWS Header Parameters, any other Header Members may also be present. */
|
|
8
|
+
interface JWSHeaderParameters extends JoseHeaderParameters {
|
|
9
|
+
/** JWS "alg" (Algorithm) Header Parameter */
|
|
10
|
+
alg?: JWSAlgorithm;
|
|
11
|
+
/**
|
|
12
|
+
* This JWS Extension Header Parameter modifies the JWS Payload representation and the JWS Signing
|
|
13
|
+
* Input computation as per {@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}.
|
|
14
|
+
* If `false`, the payload is used directly without Base64URL encoding.
|
|
15
|
+
* Defaults to `true`.
|
|
16
|
+
*/
|
|
17
|
+
b64?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** JWS Protected Header */
|
|
20
|
+
interface JWSProtectedHeader extends JWSHeaderParameters {
|
|
21
|
+
alg: JWSAlgorithm;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* JWS (JSON Web Signature) options for signing
|
|
25
|
+
*/
|
|
26
|
+
interface JWSSignOptions {
|
|
27
|
+
/** The JWS Algorithm to use. Must be provided. */
|
|
28
|
+
alg?: JWSAlgorithm;
|
|
29
|
+
/**
|
|
30
|
+
* Additional protected header parameters. `alg` is automatically included.
|
|
31
|
+
* `typ` defaults to "JWT" if not provided.
|
|
32
|
+
*/
|
|
33
|
+
protectedHeader?: JWSHeaderParameters;
|
|
34
|
+
}
|
|
35
|
+
/** Result of JWS verification */
|
|
36
|
+
interface JWSVerifyResult<T = JWTClaims | Uint8Array | string> {
|
|
37
|
+
/** The decoded and verified payload. */
|
|
38
|
+
payload: T;
|
|
39
|
+
/** The JWS Protected Header. */
|
|
40
|
+
protectedHeader: JWSProtectedHeader;
|
|
41
|
+
}
|
|
42
|
+
/** Options for JWS verification */
|
|
43
|
+
interface JWSVerifyOptions {
|
|
44
|
+
/** List of allowed algorithms. If provided, the JWS `alg` must be in this list. */
|
|
45
|
+
algorithms?: JWSAlgorithm[];
|
|
46
|
+
/** List of critical header parameters that must be understood and processed. */
|
|
47
|
+
critical?: string[];
|
|
48
|
+
/** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred. */
|
|
49
|
+
forceUint8Array?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a JWS (JSON Web Signature) in Compact Serialization format.
|
|
54
|
+
*
|
|
55
|
+
* @param payload The payload to sign. Can be a string, Uint8Array, or an object (which will be JSON stringified).
|
|
56
|
+
* @param key The signing key (CryptoKey, JWK, or raw symmetric key as Uint8Array).
|
|
57
|
+
* @param options Signing options, including the algorithm (`alg`) and protected header parameters.
|
|
58
|
+
* @returns A Promise resolving to the JWS Compact Serialization string.
|
|
59
|
+
*/
|
|
60
|
+
declare function sign(payload: JWTClaims, key: JWK | Uint8Array, options?: JWSSignOptions): Promise<string>;
|
|
61
|
+
declare function sign(payload: string | Uint8Array | Record<string, any>, key: JWK | Uint8Array, options?: JWSSignOptions): Promise<string>;
|
|
62
|
+
declare function sign(payload: JWTClaims, key: CryptoKey, options: JWSSignOptions & {
|
|
63
|
+
alg: JWSAlgorithm;
|
|
64
|
+
}): Promise<string>;
|
|
65
|
+
declare function sign(payload: string | Uint8Array | Record<string, any>, key: CryptoKey, options: JWSSignOptions & {
|
|
66
|
+
alg: JWSAlgorithm;
|
|
67
|
+
}): Promise<string>;
|
|
68
|
+
declare function sign(payload: string | Uint8Array | Record<string, any>, key: CryptoKey | JWK | Uint8Array, options: JWSSignOptions & {
|
|
69
|
+
alg: JWSAlgorithm;
|
|
70
|
+
}): Promise<string>;
|
|
71
|
+
type KeyLookupFunction = (header: JWSProtectedHeader) => CryptoKey | JWK | Uint8Array | Promise<CryptoKey | JWK | Uint8Array>;
|
|
72
|
+
/**
|
|
73
|
+
* Verifies a JWS (JSON Web Signature) in Compact Serialization format.
|
|
74
|
+
*
|
|
75
|
+
* @param jws The JWS Compact Serialization string.
|
|
76
|
+
* @param key The verification key (CryptoKey, JWK, raw symmetric key as Uint8Array, or a function resolving the key).
|
|
77
|
+
* @param options Verification options, such as allowed algorithms.
|
|
78
|
+
* @returns A Promise resolving to an object containing the verified payload and protected header.
|
|
79
|
+
* @throws If the JWS is invalid, signature verification fails, or options are not met.
|
|
80
|
+
*/
|
|
81
|
+
declare function verify<T = JWTClaims | Uint8Array | string>(jws: string, key: CryptoKey | JWK | Uint8Array | KeyLookupFunction, options?: JWSVerifyOptions): Promise<JWSVerifyResult<T>>;
|
|
82
|
+
declare function verify(jws: string, key: CryptoKey | JWK | Uint8Array | KeyLookupFunction, options: JWSVerifyOptions & {
|
|
83
|
+
forceUint8Array: true;
|
|
84
|
+
}): Promise<JWSVerifyResult<Uint8Array>>;
|
|
85
|
+
|
|
86
|
+
type jws_JWSAlgorithm = JWSAlgorithm;
|
|
87
|
+
type jws_JWSHeaderParameters = JWSHeaderParameters;
|
|
88
|
+
type jws_JWSProtectedHeader = JWSProtectedHeader;
|
|
89
|
+
type jws_JWSSignOptions = JWSSignOptions;
|
|
90
|
+
type jws_JWSVerifyOptions = JWSVerifyOptions;
|
|
91
|
+
type jws_JWSVerifyResult<T = JWTClaims | Uint8Array | string> = JWSVerifyResult<T>;
|
|
92
|
+
declare const jws_sign: typeof sign;
|
|
93
|
+
declare const jws_verify: typeof verify;
|
|
94
|
+
declare namespace jws {
|
|
95
|
+
export { jws_sign as sign, jws_verify as verify };
|
|
96
|
+
export type { jws_JWSAlgorithm as JWSAlgorithm, jws_JWSHeaderParameters as JWSHeaderParameters, jws_JWSProtectedHeader as JWSProtectedHeader, jws_JWSSignOptions as JWSSignOptions, jws_JWSVerifyOptions as JWSVerifyOptions, jws_JWSVerifyResult as JWSVerifyResult };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { jws as j, sign as s, verify as v };
|
|
100
|
+
export type { JWSAlgorithm as J, JWSHeaderParameters as a, JWSProtectedHeader as b, JWSSignOptions as c, JWSVerifyResult as d, JWSVerifyOptions as e };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { i as JWK_RSA_Public } from './unjwt.C0bBN0AC.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?: JWK_RSA_Public;
|
|
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
|
+
interface JWTClaims {
|
|
27
|
+
/** "iss" (Issuer) Claim */
|
|
28
|
+
iss?: string;
|
|
29
|
+
/** "sub" (Subject) Claim */
|
|
30
|
+
sub?: string;
|
|
31
|
+
/** "aud" (Audience) Claim */
|
|
32
|
+
aud?: string | string[];
|
|
33
|
+
/** "exp" (Expiration Time) Claim */
|
|
34
|
+
exp?: number;
|
|
35
|
+
/** "nbf" (Not Before) Claim */
|
|
36
|
+
nbf?: number;
|
|
37
|
+
/** "iat" (Issued At) Claim */
|
|
38
|
+
iat?: number;
|
|
39
|
+
/** "jti" (JWT ID) Claim */
|
|
40
|
+
jti?: string;
|
|
41
|
+
/** Any other JWT member. */
|
|
42
|
+
[propName: string]: unknown;
|
|
43
|
+
}
|
|
44
|
+
interface JWT<T = JWTClaims> {
|
|
45
|
+
header: JoseHeaderParameters & {
|
|
46
|
+
/** "alg" (Algorithm) Header Parameter */
|
|
47
|
+
alg: string;
|
|
48
|
+
};
|
|
49
|
+
claims: T;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type { JoseHeaderParameters as J, JWTClaims as a, JWT as b };
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { K as KeyManagementAlgorithm, d as ContentEncryptionAlgorithm, f as JWK_EC_Public, m as JWK } from './unjwt.C0bBN0AC.mjs';
|
|
2
|
+
import { J as JoseHeaderParameters, a as JWTClaims } from './unjwt.D90BbcE1.mjs';
|
|
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,46 @@
|
|
|
1
|
+
import { m as JWK } from './unjwt.C0bBN0AC.js';
|
|
2
|
+
|
|
3
|
+
declare const textEncoder: TextEncoder;
|
|
4
|
+
declare const textDecoder: TextDecoder;
|
|
5
|
+
declare function base64UrlEncode(data: Uint8Array | string): string;
|
|
6
|
+
declare function base64UrlDecode(): string;
|
|
7
|
+
declare function base64UrlDecode(str?: string | undefined): string;
|
|
8
|
+
declare function base64UrlDecode<T extends boolean | undefined>(str?: string | undefined, toString?: T): T extends false ? Uint8Array : string;
|
|
9
|
+
declare function randomBytes(length: Readonly<number>): Uint8Array;
|
|
10
|
+
/**
|
|
11
|
+
* Concatenates multiple Uint8Arrays
|
|
12
|
+
* @param arrays Arrays to concatenate
|
|
13
|
+
* @returns Concatenated array
|
|
14
|
+
*/
|
|
15
|
+
declare function concatUint8Arrays(...arrays: Readonly<Uint8Array[]>): Uint8Array;
|
|
16
|
+
declare function isJWK(key: any): key is JWK;
|
|
17
|
+
declare function assertCryptoKey(key: unknown): asserts key is CryptoKey;
|
|
18
|
+
declare function isCryptoKey(key: unknown): key is CryptoKey;
|
|
19
|
+
declare const isCryptoKeyPair: (key: any) => key is CryptoKeyPair;
|
|
20
|
+
|
|
21
|
+
declare const index_assertCryptoKey: typeof assertCryptoKey;
|
|
22
|
+
declare const index_base64UrlDecode: typeof base64UrlDecode;
|
|
23
|
+
declare const index_base64UrlEncode: typeof base64UrlEncode;
|
|
24
|
+
declare const index_concatUint8Arrays: typeof concatUint8Arrays;
|
|
25
|
+
declare const index_isCryptoKey: typeof isCryptoKey;
|
|
26
|
+
declare const index_isCryptoKeyPair: typeof isCryptoKeyPair;
|
|
27
|
+
declare const index_isJWK: typeof isJWK;
|
|
28
|
+
declare const index_randomBytes: typeof randomBytes;
|
|
29
|
+
declare const index_textDecoder: typeof textDecoder;
|
|
30
|
+
declare const index_textEncoder: typeof textEncoder;
|
|
31
|
+
declare namespace index {
|
|
32
|
+
export {
|
|
33
|
+
index_assertCryptoKey as assertCryptoKey,
|
|
34
|
+
index_base64UrlDecode as base64UrlDecode,
|
|
35
|
+
index_base64UrlEncode as base64UrlEncode,
|
|
36
|
+
index_concatUint8Arrays as concatUint8Arrays,
|
|
37
|
+
index_isCryptoKey as isCryptoKey,
|
|
38
|
+
index_isCryptoKeyPair as isCryptoKeyPair,
|
|
39
|
+
index_isJWK as isJWK,
|
|
40
|
+
index_randomBytes as randomBytes,
|
|
41
|
+
index_textDecoder as textDecoder,
|
|
42
|
+
index_textEncoder as textEncoder,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { textDecoder as a, base64UrlEncode as b, base64UrlDecode as c, concatUint8Arrays as d, isJWK as e, assertCryptoKey as f, isCryptoKey as g, isCryptoKeyPair as h, index as i, randomBytes as r, textEncoder as t };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { n as JWK_HMAC, o as JWK_RSA_SIGN, p as JWK_RSA_PSS, q as JWK_ECDSA, m as JWK } from './unjwt.C0bBN0AC.js';
|
|
2
|
+
import { J as JoseHeaderParameters, a as JWTClaims } from './unjwt.DoVzOnty.js';
|
|
3
|
+
|
|
4
|
+
type JWS_SIGN_EXTRA = "Ed25519" | "EdDSA";
|
|
5
|
+
/** JWS Signing Algorithm Identifier. */
|
|
6
|
+
type JWSAlgorithm = JWK_HMAC | JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWS_SIGN_EXTRA;
|
|
7
|
+
/** Recognized JWS Header Parameters, any other Header Members may also be present. */
|
|
8
|
+
interface JWSHeaderParameters extends JoseHeaderParameters {
|
|
9
|
+
/** JWS "alg" (Algorithm) Header Parameter */
|
|
10
|
+
alg?: JWSAlgorithm;
|
|
11
|
+
/**
|
|
12
|
+
* This JWS Extension Header Parameter modifies the JWS Payload representation and the JWS Signing
|
|
13
|
+
* Input computation as per {@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}.
|
|
14
|
+
* If `false`, the payload is used directly without Base64URL encoding.
|
|
15
|
+
* Defaults to `true`.
|
|
16
|
+
*/
|
|
17
|
+
b64?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** JWS Protected Header */
|
|
20
|
+
interface JWSProtectedHeader extends JWSHeaderParameters {
|
|
21
|
+
alg: JWSAlgorithm;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* JWS (JSON Web Signature) options for signing
|
|
25
|
+
*/
|
|
26
|
+
interface JWSSignOptions {
|
|
27
|
+
/** The JWS Algorithm to use. Must be provided. */
|
|
28
|
+
alg?: JWSAlgorithm;
|
|
29
|
+
/**
|
|
30
|
+
* Additional protected header parameters. `alg` is automatically included.
|
|
31
|
+
* `typ` defaults to "JWT" if not provided.
|
|
32
|
+
*/
|
|
33
|
+
protectedHeader?: JWSHeaderParameters;
|
|
34
|
+
}
|
|
35
|
+
/** Result of JWS verification */
|
|
36
|
+
interface JWSVerifyResult<T = JWTClaims | Uint8Array | string> {
|
|
37
|
+
/** The decoded and verified payload. */
|
|
38
|
+
payload: T;
|
|
39
|
+
/** The JWS Protected Header. */
|
|
40
|
+
protectedHeader: JWSProtectedHeader;
|
|
41
|
+
}
|
|
42
|
+
/** Options for JWS verification */
|
|
43
|
+
interface JWSVerifyOptions {
|
|
44
|
+
/** List of allowed algorithms. If provided, the JWS `alg` must be in this list. */
|
|
45
|
+
algorithms?: JWSAlgorithm[];
|
|
46
|
+
/** List of critical header parameters that must be understood and processed. */
|
|
47
|
+
critical?: string[];
|
|
48
|
+
/** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred. */
|
|
49
|
+
forceUint8Array?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a JWS (JSON Web Signature) in Compact Serialization format.
|
|
54
|
+
*
|
|
55
|
+
* @param payload The payload to sign. Can be a string, Uint8Array, or an object (which will be JSON stringified).
|
|
56
|
+
* @param key The signing key (CryptoKey, JWK, or raw symmetric key as Uint8Array).
|
|
57
|
+
* @param options Signing options, including the algorithm (`alg`) and protected header parameters.
|
|
58
|
+
* @returns A Promise resolving to the JWS Compact Serialization string.
|
|
59
|
+
*/
|
|
60
|
+
declare function sign(payload: JWTClaims, key: JWK | Uint8Array, options?: JWSSignOptions): Promise<string>;
|
|
61
|
+
declare function sign(payload: string | Uint8Array | Record<string, any>, key: JWK | Uint8Array, options?: JWSSignOptions): Promise<string>;
|
|
62
|
+
declare function sign(payload: JWTClaims, key: CryptoKey, options: JWSSignOptions & {
|
|
63
|
+
alg: JWSAlgorithm;
|
|
64
|
+
}): Promise<string>;
|
|
65
|
+
declare function sign(payload: string | Uint8Array | Record<string, any>, key: CryptoKey, options: JWSSignOptions & {
|
|
66
|
+
alg: JWSAlgorithm;
|
|
67
|
+
}): Promise<string>;
|
|
68
|
+
declare function sign(payload: string | Uint8Array | Record<string, any>, key: CryptoKey | JWK | Uint8Array, options: JWSSignOptions & {
|
|
69
|
+
alg: JWSAlgorithm;
|
|
70
|
+
}): Promise<string>;
|
|
71
|
+
type KeyLookupFunction = (header: JWSProtectedHeader) => CryptoKey | JWK | Uint8Array | Promise<CryptoKey | JWK | Uint8Array>;
|
|
72
|
+
/**
|
|
73
|
+
* Verifies a JWS (JSON Web Signature) in Compact Serialization format.
|
|
74
|
+
*
|
|
75
|
+
* @param jws The JWS Compact Serialization string.
|
|
76
|
+
* @param key The verification key (CryptoKey, JWK, raw symmetric key as Uint8Array, or a function resolving the key).
|
|
77
|
+
* @param options Verification options, such as allowed algorithms.
|
|
78
|
+
* @returns A Promise resolving to an object containing the verified payload and protected header.
|
|
79
|
+
* @throws If the JWS is invalid, signature verification fails, or options are not met.
|
|
80
|
+
*/
|
|
81
|
+
declare function verify<T = JWTClaims | Uint8Array | string>(jws: string, key: CryptoKey | JWK | Uint8Array | KeyLookupFunction, options?: JWSVerifyOptions): Promise<JWSVerifyResult<T>>;
|
|
82
|
+
declare function verify(jws: string, key: CryptoKey | JWK | Uint8Array | KeyLookupFunction, options: JWSVerifyOptions & {
|
|
83
|
+
forceUint8Array: true;
|
|
84
|
+
}): Promise<JWSVerifyResult<Uint8Array>>;
|
|
85
|
+
|
|
86
|
+
type jws_JWSAlgorithm = JWSAlgorithm;
|
|
87
|
+
type jws_JWSHeaderParameters = JWSHeaderParameters;
|
|
88
|
+
type jws_JWSProtectedHeader = JWSProtectedHeader;
|
|
89
|
+
type jws_JWSSignOptions = JWSSignOptions;
|
|
90
|
+
type jws_JWSVerifyOptions = JWSVerifyOptions;
|
|
91
|
+
type jws_JWSVerifyResult<T = JWTClaims | Uint8Array | string> = JWSVerifyResult<T>;
|
|
92
|
+
declare const jws_sign: typeof sign;
|
|
93
|
+
declare const jws_verify: typeof verify;
|
|
94
|
+
declare namespace jws {
|
|
95
|
+
export { jws_sign as sign, jws_verify as verify };
|
|
96
|
+
export type { jws_JWSAlgorithm as JWSAlgorithm, jws_JWSHeaderParameters as JWSHeaderParameters, jws_JWSProtectedHeader as JWSProtectedHeader, jws_JWSSignOptions as JWSSignOptions, jws_JWSVerifyOptions as JWSVerifyOptions, jws_JWSVerifyResult as JWSVerifyResult };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { jws as j, sign as s, verify as v };
|
|
100
|
+
export type { JWSAlgorithm as J, JWSHeaderParameters as a, JWSProtectedHeader as b, JWSSignOptions as c, JWSVerifyResult as d, JWSVerifyOptions as e };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { c as checkSigCryptoKey } from './unjwt.DKq5VF7t.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This is a fork of Jose library's utility functions.
|
|
5
|
+
* @source https://github.com/panva/jose/tree/69b7960c67e05be55fa2ec31c74b987696c20c60/src/lib/sign.ts
|
|
6
|
+
* @source https://github.com/panva/jose/tree/69b7960c67e05be55fa2ec31c74b987696c20c60/src/lib/verify.ts
|
|
7
|
+
* @license MIT https://github.com/panva/jose/blob/69b7960c67e05be55fa2ec31c74b987696c20c60/LICENSE.md
|
|
8
|
+
*/
|
|
9
|
+
async function sign(alg, key, data) {
|
|
10
|
+
const cryptoKey = await getSignVerifyKey(alg, key, "sign");
|
|
11
|
+
checkKeyLength(alg, cryptoKey);
|
|
12
|
+
const signature = await crypto.subtle.sign(
|
|
13
|
+
subtleAlgorithm(alg, cryptoKey.algorithm),
|
|
14
|
+
cryptoKey,
|
|
15
|
+
data
|
|
16
|
+
);
|
|
17
|
+
return new Uint8Array(signature);
|
|
18
|
+
}
|
|
19
|
+
async function verify(alg, key, signature, data) {
|
|
20
|
+
const cryptoKey = await getSignVerifyKey(alg, key, "verify");
|
|
21
|
+
checkKeyLength(alg, cryptoKey);
|
|
22
|
+
const algorithm = subtleAlgorithm(alg, cryptoKey.algorithm);
|
|
23
|
+
try {
|
|
24
|
+
return await crypto.subtle.verify(algorithm, cryptoKey, signature, data);
|
|
25
|
+
} catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function checkKeyLength(alg, key) {
|
|
30
|
+
if (alg.startsWith("RS") || alg.startsWith("PS")) {
|
|
31
|
+
const { modulusLength } = key.algorithm;
|
|
32
|
+
if (typeof modulusLength !== "number" || modulusLength < 2048) {
|
|
33
|
+
throw new TypeError(
|
|
34
|
+
`${alg} requires key modulusLength to be 2048 bits or larger`
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function getSignVerifyKey(alg, key, usage) {
|
|
40
|
+
if (key instanceof Uint8Array) {
|
|
41
|
+
if (!alg.startsWith("HS")) {
|
|
42
|
+
throw new TypeError(
|
|
43
|
+
`Key must be ${key} of type: CryptoKey or JSON Web Key`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
return crypto.subtle.importKey(
|
|
47
|
+
"raw",
|
|
48
|
+
key,
|
|
49
|
+
{ hash: `SHA-${alg.slice(-3)}`, name: "HMAC" },
|
|
50
|
+
false,
|
|
51
|
+
[usage]
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
checkSigCryptoKey(key, alg, usage);
|
|
55
|
+
return key;
|
|
56
|
+
}
|
|
57
|
+
function subtleAlgorithm(alg, algorithm) {
|
|
58
|
+
const hash = `SHA-${alg.slice(-3)}`;
|
|
59
|
+
switch (alg) {
|
|
60
|
+
case "HS256":
|
|
61
|
+
case "HS384":
|
|
62
|
+
case "HS512": {
|
|
63
|
+
return { hash, name: "HMAC" };
|
|
64
|
+
}
|
|
65
|
+
case "PS256":
|
|
66
|
+
case "PS384":
|
|
67
|
+
case "PS512": {
|
|
68
|
+
return {
|
|
69
|
+
hash,
|
|
70
|
+
name: "RSA-PSS",
|
|
71
|
+
saltLength: Number.parseInt(alg.slice(-3), 10) >> 3
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
case "RS256":
|
|
75
|
+
case "RS384":
|
|
76
|
+
case "RS512": {
|
|
77
|
+
return { hash, name: "RSASSA-PKCS1-v1_5" };
|
|
78
|
+
}
|
|
79
|
+
case "ES256":
|
|
80
|
+
case "ES384":
|
|
81
|
+
case "ES512": {
|
|
82
|
+
return {
|
|
83
|
+
hash,
|
|
84
|
+
name: "ECDSA",
|
|
85
|
+
namedCurve: algorithm.namedCurve
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
case "Ed25519":
|
|
89
|
+
// Fall through
|
|
90
|
+
case "EdDSA": {
|
|
91
|
+
return { name: "Ed25519" };
|
|
92
|
+
}
|
|
93
|
+
default: {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`alg ${alg} is not supported either by JOSE or your javascript runtime`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { checkKeyLength as c, sign as s, verify as v };
|
package/dist/utils.d.mts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import './shared/unjwt.C0bBN0AC.mjs';
|
|
2
|
+
export { f as assertCryptoKey, c as base64UrlDecode, b as base64UrlEncode, d as concatUint8Arrays, g as isCryptoKey, h as isCryptoKeyPair, e as isJWK, r as randomBytes, a as textDecoder, t as textEncoder } from './shared/unjwt.CJ_ohCFC.mjs';
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import './shared/unjwt.C0bBN0AC.js';
|
|
2
|
+
export { f as assertCryptoKey, c as base64UrlDecode, b as base64UrlEncode, d as concatUint8Arrays, g as isCryptoKey, h as isCryptoKeyPair, e as isJWK, r as randomBytes, a as textDecoder, t as textEncoder } from './shared/unjwt.mc4AEAT7.js';
|
package/dist/utils.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { f as assertCryptoKey, d as base64UrlDecode, b as base64UrlEncode, e as concatUint8Arrays, g as isCryptoKey, h as isCryptoKeyPair, a as isJWK, r as randomBytes, c as textDecoder, t as textEncoder } from './shared/unjwt.CjHZv3F3.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unjwt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Low-level JWT utilities",
|
|
5
5
|
"repository": "sandros94/unjwt",
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,6 +26,10 @@
|
|
|
26
26
|
"./jws": {
|
|
27
27
|
"types": "./dist/jws.d.mts",
|
|
28
28
|
"default": "./dist/jws.mjs"
|
|
29
|
+
},
|
|
30
|
+
"./utils": {
|
|
31
|
+
"types": "./dist/utils.d.mts",
|
|
32
|
+
"default": "./dist/utils.mjs"
|
|
29
33
|
}
|
|
30
34
|
},
|
|
31
35
|
"main": "./dist/index.mjs",
|
|
@@ -41,12 +45,12 @@
|
|
|
41
45
|
"lint:fix": "automd && eslint . --fix && prettier -w .",
|
|
42
46
|
"prepack": "pnpm build",
|
|
43
47
|
"release": "pnpm test && pnpm prepack && changelogen --release --push --publish",
|
|
44
|
-
"test": "pnpm lint && pnpm test:types && vitest run --coverage",
|
|
48
|
+
"test": "pnpm lint && pnpm test:types && vitest run --coverage --slowTestThreshold=500",
|
|
45
49
|
"test:types": "tsc --noEmit --skipLibCheck"
|
|
46
50
|
},
|
|
47
51
|
"devDependencies": {
|
|
48
52
|
"@types/node": "^22.13.13",
|
|
49
|
-
"@vitest/coverage-v8": "^3.
|
|
53
|
+
"@vitest/coverage-v8": "^3.1.2",
|
|
50
54
|
"automd": "^0.4.0",
|
|
51
55
|
"changelogen": "^0.6.1",
|
|
52
56
|
"eslint": "^9.23.0",
|
|
@@ -54,7 +58,7 @@
|
|
|
54
58
|
"prettier": "^3.5.3",
|
|
55
59
|
"typescript": "^5.8.2",
|
|
56
60
|
"unbuild": "^3.5.0",
|
|
57
|
-
"vitest": "^3.
|
|
61
|
+
"vitest": "^3.1.2"
|
|
58
62
|
},
|
|
59
63
|
"packageManager": "pnpm@10.7.0"
|
|
60
64
|
}
|