unjwt 0.5.39 → 0.6.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 +156 -13
- package/dist/_chunks/_crypto.mjs +1014 -0
- package/dist/_chunks/error.d.mts +67 -0
- package/dist/_chunks/index.d.mts +631 -219
- package/dist/_chunks/jwk.d.mts +220 -87
- package/dist/_chunks/jws.mjs +729 -682
- package/dist/_chunks/utils.mjs +74 -69
- package/dist/adapters/h3v1/index.d.mts +282 -0
- package/dist/{_chunks/h3v1.mjs → adapters/h3v1/index.mjs} +204 -91
- package/dist/adapters/h3v2/index.d.mts +213 -0
- package/dist/adapters/{h3v2.mjs → h3v2/index.mjs} +215 -113
- package/dist/core/_crypto/index.d.mts +71 -0
- package/dist/core/_crypto/index.mjs +2 -0
- package/dist/core/jwe.d.mts +40 -0
- package/dist/{_chunks → core}/jwe.mjs +61 -32
- package/dist/core/jwk.d.mts +231 -0
- package/dist/core/jwk.mjs +686 -0
- package/dist/core/jws.d.mts +31 -0
- package/dist/core/jws.mjs +104 -0
- package/dist/{_chunks/index2.d.mts → core/utils/index.d.mts} +13 -12
- package/dist/core/utils/index.mjs +2 -0
- package/dist/index.d.mts +7 -6
- package/dist/index.mjs +5 -5
- package/package.json +50 -41
- package/dist/_chunks/chunk.mjs +0 -13
- package/dist/_chunks/jwe.d.mts +0 -43
- package/dist/_chunks/jwk.mjs +0 -1592
- package/dist/_chunks/jws.d.mts +0 -46
- package/dist/adapters/h3.d.mts +0 -3
- package/dist/adapters/h3.mjs +0 -3
- package/dist/adapters/h3v1.d.mts +0 -3
- package/dist/adapters/h3v1.mjs +0 -3
- package/dist/adapters/h3v2.d.mts +0 -184
- package/dist/jwe.d.mts +0 -3
- package/dist/jwe.mjs +0 -4
- package/dist/jwk.d.mts +0 -3
- package/dist/jwk.mjs +0 -3
- package/dist/jws.d.mts +0 -3
- package/dist/jws.mjs +0 -4
- package/dist/utils.d.mts +0 -3
- package/dist/utils.mjs +0 -2
package/dist/_chunks/index.d.mts
CHANGED
|
@@ -1,250 +1,662 @@
|
|
|
1
|
-
|
|
2
|
-
import { H3Event } from "h3";
|
|
3
|
-
import { CookieSerializeOptions } from "cookie-es";
|
|
4
|
-
|
|
5
|
-
//#region src/adapters/h3v1/session/types.d.ts
|
|
6
|
-
interface SessionClaims extends Required<Pick<JWTClaims, "jti" | "iat">>, StrictOmit<JWTClaims, "jti" | "iat"> {}
|
|
7
|
-
type SessionData<T extends Record<string, any> = SessionClaims> = StrictOmit<T, "jti" | "iat" | "exp">;
|
|
8
|
-
type SessionUpdate<T extends Record<string, any> = SessionClaims> = Partial<SessionData<T>> | ((oldData: SessionData<T>) => Partial<SessionData<T>> | undefined);
|
|
9
|
-
interface SessionManager<T extends Record<string, any> = SessionClaims, ConfigMaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
|
|
10
|
-
readonly id: string | undefined;
|
|
11
|
-
readonly createdAt: number;
|
|
12
|
-
readonly expiresAt: ConfigMaxAge extends ExpiresIn ? number : "exp" extends keyof T ? T["exp"] : number | undefined;
|
|
13
|
-
readonly data: SessionData<T>;
|
|
14
|
-
readonly token: string | undefined;
|
|
15
|
-
update: (update: SessionUpdate<T>) => Promise<SessionManager<T, ConfigMaxAge> & {
|
|
16
|
-
readonly id: string;
|
|
17
|
-
}>;
|
|
18
|
-
clear: () => Promise<SessionManager<T, ConfigMaxAge>>;
|
|
19
|
-
}
|
|
20
|
-
//#endregion
|
|
21
|
-
//#region src/adapters/h3v1/session/jwe.d.ts
|
|
22
|
-
declare const kGetSessionPromise$1: unique symbol;
|
|
23
|
-
interface SessionJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
|
|
24
|
-
id: string | undefined;
|
|
25
|
-
createdAt: number;
|
|
26
|
-
expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
|
|
27
|
-
data: SessionData<T>;
|
|
28
|
-
[kGetSessionPromise$1]?: Promise<SessionJWE<T, MaxAge>>;
|
|
29
|
-
}
|
|
30
|
-
interface SessionHooksJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
|
|
31
|
-
onRead?: (args: {
|
|
32
|
-
session: SessionJWE<T, MaxAge>;
|
|
33
|
-
event: H3Event;
|
|
34
|
-
config: SessionConfigJWE<T, MaxAge>;
|
|
35
|
-
}) => void | Promise<void>;
|
|
36
|
-
onUpdate?: (args: {
|
|
37
|
-
session: SessionJWE<T, MaxAge>;
|
|
38
|
-
oldSession: SessionJWE<T, MaxAge>;
|
|
39
|
-
event: H3Event;
|
|
40
|
-
config: SessionConfigJWE<T, MaxAge>;
|
|
41
|
-
}) => void | Promise<void>;
|
|
42
|
-
onClear?: (args: {
|
|
43
|
-
session: SessionJWE<T, MaxAge> | undefined;
|
|
44
|
-
event: H3Event;
|
|
45
|
-
config: Partial<SessionConfigJWE<T, MaxAge>>;
|
|
46
|
-
}) => void | Promise<void>;
|
|
47
|
-
onExpire?: (args: {
|
|
48
|
-
event: H3Event;
|
|
49
|
-
error: Error;
|
|
50
|
-
config: SessionConfigJWE<T, MaxAge>;
|
|
51
|
-
}) => void | Promise<void>;
|
|
52
|
-
onError?: (args: {
|
|
53
|
-
event: H3Event;
|
|
54
|
-
error: any;
|
|
55
|
-
config: SessionConfigJWE<T, MaxAge>;
|
|
56
|
-
}) => void | Promise<void>;
|
|
57
|
-
onUnsealKeyLookup?: (args: {
|
|
58
|
-
header: JWEHeaderParameters;
|
|
59
|
-
event: H3Event;
|
|
60
|
-
config: SessionConfigJWE<T, MaxAge>;
|
|
61
|
-
}) => JWK_Symmetric | JWK_Private | Promise<JWK_Symmetric | JWK_Private>;
|
|
62
|
-
}
|
|
63
|
-
interface SessionConfigJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
|
|
64
|
-
/** @deprecated use `key` instead */
|
|
65
|
-
secret?: string | JWK_Symmetric | {
|
|
66
|
-
privateKey: JWK_Private | JWK_Symmetric;
|
|
67
|
-
publicKey?: JWK_Public;
|
|
68
|
-
};
|
|
69
|
-
/** Shared key used, string for PBES2 or Json Web Key (JWK) */
|
|
70
|
-
key: string | JWK_Symmetric | JWK_Private | {
|
|
71
|
-
privateKey: JWK_Private | JWK_Symmetric;
|
|
72
|
-
publicKey?: JWK_Public;
|
|
73
|
-
};
|
|
74
|
-
/** Session lifetime in seconds (used to derive exp from iat) */
|
|
75
|
-
maxAge?: MaxAge;
|
|
76
|
-
/** Default is "h3" */
|
|
77
|
-
name?: string;
|
|
78
|
-
/** Default is secure, httpOnly, path="/" */
|
|
79
|
-
cookie?: false | CookieSerializeOptions;
|
|
80
|
-
/** Default is x-h3-session / x-{name}-session */
|
|
81
|
-
sessionHeader?: false | string;
|
|
82
|
-
/** Default is crypto.randomUUID */
|
|
83
|
-
generateId?: () => string;
|
|
84
|
-
/** JWE configuration overrides */
|
|
85
|
-
jwe?: {
|
|
86
|
-
encryptOptions?: Omit<JWEEncryptOptions, "expiresIn">;
|
|
87
|
-
decryptOptions?: JWTClaimValidationOptions;
|
|
88
|
-
};
|
|
89
|
-
hooks?: SessionHooksJWE<T, MaxAge>;
|
|
90
|
-
}
|
|
1
|
+
//#region src/core/types/jwk.d.ts
|
|
91
2
|
/**
|
|
92
|
-
*
|
|
3
|
+
* The decoded JOSE protected header received by a {@link JWKLookupFunction}.
|
|
4
|
+
*
|
|
5
|
+
* The most commonly inspected fields are explicitly typed; additional
|
|
6
|
+
* algorithm-specific parameters (`epk`, `p2s`, `p2c`, `apu`, `apv`, …) are
|
|
7
|
+
* accessible through the index signature.
|
|
8
|
+
*
|
|
9
|
+
* `alg` is always present on both JWS and JWE tokens. `enc` is only present
|
|
10
|
+
* on JWE tokens.
|
|
93
11
|
*/
|
|
94
|
-
type
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
headers: Headers;
|
|
102
|
-
context: any;
|
|
12
|
+
type JWKLookupFunctionHeader = {
|
|
13
|
+
kid?: string;
|
|
14
|
+
alg?: string;
|
|
15
|
+
enc?: string;
|
|
16
|
+
typ?: string;
|
|
17
|
+
crit?: string[];
|
|
18
|
+
[propName: string]: unknown;
|
|
103
19
|
};
|
|
104
20
|
/**
|
|
105
|
-
*
|
|
21
|
+
* A callback that resolves the decryption or verification key from a token's
|
|
22
|
+
* decoded protected header and the raw compact token string.
|
|
23
|
+
*
|
|
24
|
+
* Accepted by both {@link verify} (JWS) and {@link decrypt} (JWE). When a
|
|
25
|
+
* {@link JWKSet} is returned, the library tries each key whose `kid` and `alg`
|
|
26
|
+
* are compatible with the header in order, stopping at the first that succeeds
|
|
27
|
+
* — see {@link getJWKsFromSet} for the selection logic.
|
|
28
|
+
*
|
|
29
|
+
* Returning a `string` is meaningful for JWE only (PBES2 password); for JWS
|
|
30
|
+
* a string return is encoded to bytes and used as a raw symmetric key.
|
|
106
31
|
*/
|
|
107
|
-
|
|
32
|
+
type JWKLookupFunction = (header: JWKLookupFunctionHeader, token: string) => CryptoKey | JWK | JWKSet | string | Uint8Array<ArrayBuffer> | Promise<CryptoKey | JWK | JWKSet | string | Uint8Array<ArrayBuffer>>;
|
|
108
33
|
/**
|
|
109
|
-
*
|
|
34
|
+
* Interface for custom JWK import cache implementations.
|
|
35
|
+
*
|
|
36
|
+
* The adapter receives the full JWK object so each implementation can choose
|
|
37
|
+
* its own keying strategy:
|
|
38
|
+
* - {@link WeakMapJWKCache} uses the object reference (GC-friendly, no string hashing needed)
|
|
39
|
+
* - A `kid`-keyed cache extracts `jwk.kid` and uses it as a string key
|
|
40
|
+
* - A content-hash cache hashes `jwk.k` or `jwk.n`+`jwk.e`
|
|
41
|
+
*
|
|
42
|
+
* @example Use a custom kid-keyed cache:
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { configureJWKCache } from 'unjwt/jwk';
|
|
45
|
+
* const map = new Map<string, CryptoKey>();
|
|
46
|
+
* configureJWKCache({
|
|
47
|
+
* get: (jwk, alg) => map.get(`${jwk.kid}:${alg}`),
|
|
48
|
+
* set: (jwk, alg, key) => map.set(`${jwk.kid}:${alg}`, key),
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
110
51
|
*/
|
|
111
|
-
|
|
112
|
-
|
|
52
|
+
interface JWKCacheAdapter {
|
|
53
|
+
get(jwk: JWK, alg: string): CryptoKey | undefined;
|
|
54
|
+
set(jwk: JWK, alg: string, key: CryptoKey): void;
|
|
55
|
+
}
|
|
56
|
+
/** Structure returned for composite AES-CBC + HMAC keys. */
|
|
57
|
+
type CompositeKey = {
|
|
58
|
+
/** The AES-CBC encryption/decryption key. */encryptionKey: CryptoKey; /** The HMAC key for integrity/authentication. */
|
|
59
|
+
macKey: CryptoKey;
|
|
60
|
+
};
|
|
61
|
+
type GenerateKeyAlgorithm = Exclude<JWKAlgorithm, "none" | "dir" | JWK_PBES2>;
|
|
62
|
+
/** Options for the generateKey function. */
|
|
63
|
+
interface GenerateKeyOptions {
|
|
64
|
+
/** Key usages for the generated key(s). Note: For composite keys (CBC), default usages are applied separately. */
|
|
65
|
+
keyUsage?: KeyUsage[];
|
|
66
|
+
/** Mark the key(s) as extractable. Defaults to true. */
|
|
67
|
+
extractable?: boolean;
|
|
68
|
+
/** RSA modulus length. Defaults to 2048. */
|
|
69
|
+
modulusLength?: number;
|
|
70
|
+
/** RSA public exponent. Defaults to 65537 (0x010001). */
|
|
71
|
+
publicExponent?: Uint8Array<ArrayBuffer>;
|
|
72
|
+
/** Named curve for EC or OKP keys. Defaults to "P-256" for EC and "Ed25519" for OKP. */
|
|
73
|
+
namedCurve?: "P-256" | "P-384" | "P-521" | "X25519" | "Ed25519" | "Ed448";
|
|
74
|
+
/**
|
|
75
|
+
* Export the generated key(s) as JWK. When `true`, keys are returned in JWK
|
|
76
|
+
* format with a generated `kid`. To include additional JWK parameters
|
|
77
|
+
* (e.g. a custom `kid`), use {@link generateJWK} instead.
|
|
78
|
+
*/
|
|
79
|
+
toJWK?: boolean;
|
|
80
|
+
}
|
|
81
|
+
type GenerateKeyReturnJWK<TAlg extends GenerateKeyAlgorithm> = TAlg extends JWK_Asymmetric_Algorithm ? TAlg extends JWK_RSA_SIGN | JWK_RSA_PSS | JWK_RSA_ENC ? {
|
|
82
|
+
privateKey: JWK_RSA_Private;
|
|
83
|
+
publicKey: JWK_RSA_Public;
|
|
84
|
+
} : TAlg extends JWK_ECDSA | JWK_ECDH_ES ? {
|
|
85
|
+
privateKey: JWK_EC_Private;
|
|
86
|
+
publicKey: JWK_EC_Public;
|
|
87
|
+
} : TAlg extends JWK_OKP_SIGN ? {
|
|
88
|
+
privateKey: JWK_OKP_Private;
|
|
89
|
+
publicKey: JWK_OKP_Public;
|
|
90
|
+
} : never : TAlg extends JWK_AES_CBC_HMAC | JWK_Symmetric_Algorithm ? JWK_oct : never;
|
|
91
|
+
type GenerateKeyReturnCrypto<TAlg extends GenerateKeyAlgorithm> = TAlg extends JWK_AES_CBC_HMAC ? Uint8Array<ArrayBuffer> : TAlg extends JWK_Asymmetric_Algorithm ? CryptoKeyPair : CryptoKey;
|
|
92
|
+
type GenerateKeyReturn<TAlg extends GenerateKeyAlgorithm, TOptions extends GenerateKeyOptions> = TOptions["toJWK"] extends true ? GenerateKeyReturnJWK<TAlg> : GenerateKeyReturnCrypto<TAlg>;
|
|
93
|
+
type GenerateJWKOptions = Omit<GenerateKeyOptions, "toJWK">;
|
|
94
|
+
type GenerateJWKReturn<TAlg extends GenerateKeyAlgorithm> = GenerateKeyReturnJWK<TAlg>;
|
|
95
|
+
/** Options for the deriveKeyFromPassword function. */
|
|
96
|
+
interface DeriveKeyOptions {
|
|
97
|
+
/** Salt value (p2s). Must be at least 8 bytes. */
|
|
98
|
+
salt: Uint8Array<ArrayBuffer>;
|
|
99
|
+
/** Iteration count (p2c). Must be a positive integer. */
|
|
100
|
+
iterations: number;
|
|
101
|
+
/** Key usages for the derived key. Defaults to ["wrapKey", "unwrapKey"]. */
|
|
102
|
+
keyUsage?: KeyUsage[];
|
|
103
|
+
/** Mark the derived key as extractable. Defaults to false. */
|
|
104
|
+
extractable?: boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Export the derived key as JWK_oct. When `true`, returns the derived key in
|
|
107
|
+
* JWK format. To include additional JWK parameters (e.g. a custom `kid`),
|
|
108
|
+
* use {@link deriveJWKFromPassword} instead.
|
|
109
|
+
*/
|
|
110
|
+
toJWK?: boolean;
|
|
111
|
+
}
|
|
112
|
+
type DeriveKeyReturn<TOptions extends DeriveKeyOptions> = TOptions["toJWK"] extends true ? JWK_oct : TOptions["toJWK"] extends object ? JWK_oct : CryptoKey;
|
|
113
|
+
type KeyManagementAlgorithm = JWK_RSA_ENC | JWK_AES_KW | JWK_AES_GCM | JWK_AES_GCM_KW | JWK_PBES2 | JWK_ECDH_ES | "dir";
|
|
114
|
+
type ContentEncryptionAlgorithm = JWK_AES_GCM | JWK_AES_CBC_HMAC;
|
|
115
|
+
/** Options for the wrapKey function. */
|
|
116
|
+
interface WrapKeyOptions {
|
|
117
|
+
/** Initialization Vector for AES-GCMKW. Generated if not provided. */
|
|
118
|
+
iv?: Uint8Array<ArrayBuffer>;
|
|
119
|
+
/** PBES2 Salt value (p2s). Required for PBES2 algorithms. */
|
|
120
|
+
p2s?: Uint8Array<ArrayBuffer>;
|
|
121
|
+
/** PBES2 Iteration count (p2c). Required for PBES2 algorithms. */
|
|
122
|
+
p2c?: number;
|
|
123
|
+
/** ECDH-ES specific options. */
|
|
124
|
+
ecdh?: {
|
|
125
|
+
/**
|
|
126
|
+
* ECDH-ES ephemeral key material. A fresh ephemeral key pair is generated
|
|
127
|
+
* automatically when this is not provided.
|
|
128
|
+
*/
|
|
129
|
+
ephemeralKey?: CryptoKey | JWK_EC_Private | CryptoKeyPair | {
|
|
130
|
+
publicKey: CryptoKey | JWK_EC_Public;
|
|
131
|
+
privateKey: CryptoKey | JWK_EC_Private;
|
|
132
|
+
}; /** Agreement PartyUInfo (apu). */
|
|
133
|
+
partyUInfo?: Uint8Array<ArrayBuffer>; /** Agreement PartyVInfo (apv). */
|
|
134
|
+
partyVInfo?: Uint8Array<ArrayBuffer>;
|
|
135
|
+
/**
|
|
136
|
+
* Content encryption algorithm used for key-length derivation.
|
|
137
|
+
* Required only when `alg` is `"ECDH-ES"` (direct key agreement).
|
|
138
|
+
* Not needed for `ECDH-ES+A128KW`, `+A192KW`, or `+A256KW`.
|
|
139
|
+
*/
|
|
140
|
+
enc?: ContentEncryptionAlgorithm;
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/** Result of the wrapKey function. */
|
|
144
|
+
interface WrapKeyResult {
|
|
145
|
+
/** The wrapped key (Ciphertext). */
|
|
146
|
+
encryptedKey: Uint8Array<ArrayBuffer>;
|
|
147
|
+
/** Initialization Vector used (only for AES-GCMKW). Base64URL encoded. */
|
|
148
|
+
iv?: string;
|
|
149
|
+
/** Authentication Tag generated (only for AES-GCMKW). Base64URL encoded. */
|
|
150
|
+
tag?: string;
|
|
151
|
+
/** PBES2 Salt value used (only for PBES2). Base64URL encoded. */
|
|
152
|
+
p2s?: string;
|
|
153
|
+
/** PBES2 Iteration count used (only for PBES2). */
|
|
154
|
+
p2c?: number;
|
|
155
|
+
/** ECDH-ES Ephemeral Public Key used (only for ECDH-ES). */
|
|
156
|
+
epk?: JWK_EC_Public;
|
|
157
|
+
/** ECDH-ES Agreement PartyUInfo used (only for ECDH-ES). Base64URL encoded. */
|
|
158
|
+
apu?: string;
|
|
159
|
+
/** ECDH-ES Agreement PartyVInfo used (only for ECDH-ES). Base64URL encoded. */
|
|
160
|
+
apv?: string;
|
|
161
|
+
}
|
|
162
|
+
/** Options for the unwrapKey function. */
|
|
163
|
+
interface UnwrapKeyOptions {
|
|
164
|
+
/**
|
|
165
|
+
* Output format of the unwrapped key.
|
|
166
|
+
* - `"cryptokey"` (default) — returns a `CryptoKey` ready for use with WebCrypto.
|
|
167
|
+
* - `"raw"` — returns the raw key bytes as `Uint8Array` (useful for
|
|
168
|
+
* interoperability or when the algorithm is not directly importable).
|
|
169
|
+
*/
|
|
170
|
+
format?: "cryptokey" | "raw";
|
|
171
|
+
/** Initialization Vector (required for AES-GCMKW). Base64URL encoded or Uint8Array. */
|
|
172
|
+
iv?: Uint8Array<ArrayBuffer> | string;
|
|
173
|
+
/** Authentication Tag (required for AES-GCMKW). Base64URL encoded or Uint8Array. */
|
|
174
|
+
tag?: Uint8Array<ArrayBuffer> | string;
|
|
175
|
+
/** PBES2 Salt value (required for PBES2). Base64URL encoded or Uint8Array. */
|
|
176
|
+
p2s?: Uint8Array<ArrayBuffer> | string;
|
|
177
|
+
/** PBES2 Iteration count (required for PBES2). */
|
|
178
|
+
p2c?: number;
|
|
179
|
+
/** ECDH-ES Ephemeral Public Key (required for ECDH-ES). */
|
|
180
|
+
epk?: JWK_EC_Public | CryptoKey;
|
|
181
|
+
/** ECDH-ES Agreement PartyUInfo. Base64URL encoded or Uint8Array. */
|
|
182
|
+
apu?: Uint8Array<ArrayBuffer> | string;
|
|
183
|
+
/** ECDH-ES Agreement PartyVInfo. Base64URL encoded or Uint8Array. */
|
|
184
|
+
apv?: Uint8Array<ArrayBuffer> | string;
|
|
185
|
+
/** Content Encryption Algorithm used with ECDH-ES (required for direct key agreement). */
|
|
186
|
+
enc?: ContentEncryptionAlgorithm;
|
|
187
|
+
/** Expected unwrapped key algorithm (e.g., 'AES-GCM', 'AES-CBC'). Used by crypto.subtle.unwrapKey. */
|
|
188
|
+
unwrappedKeyAlgorithm?: string | Algorithm | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams;
|
|
189
|
+
/** Expected key usages for the unwrapped key. */
|
|
190
|
+
keyUsage?: KeyUsage[];
|
|
191
|
+
/** Mark the unwrapped key as extractable. Defaults to true. */
|
|
192
|
+
extractable?: boolean;
|
|
193
|
+
}
|
|
113
194
|
/**
|
|
114
|
-
*
|
|
195
|
+
* Forked from https://github.com/panva/jose/tree/v6.0.10
|
|
196
|
+
* Copyright (c) 2018 Filip Skokan.
|
|
197
|
+
* LICENSE: https://github.com/panva/jose/blob/v6.0.10/LICENSE.md
|
|
115
198
|
*/
|
|
116
|
-
|
|
199
|
+
/** Generic JSON Web Key Parameters. */
|
|
200
|
+
interface JWKParameters {
|
|
201
|
+
/** JWK "kty" (Key Type) Parameter */
|
|
202
|
+
kty: string;
|
|
203
|
+
/**
|
|
204
|
+
* JWK "alg" (Algorithm) Parameter
|
|
205
|
+
*
|
|
206
|
+
* @see {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}
|
|
207
|
+
*/
|
|
208
|
+
alg?: string;
|
|
209
|
+
/** JWK "key_ops" (Key Operations) Parameter */
|
|
210
|
+
key_ops?: KeyUsage[];
|
|
211
|
+
/** JWK "ext" (Extractable) Parameter */
|
|
212
|
+
ext?: boolean;
|
|
213
|
+
/** JWK "use" (Public Key Use) Parameter */
|
|
214
|
+
use?: string;
|
|
215
|
+
/** JWK "x5c" (X.509 Certificate Chain) Parameter */
|
|
216
|
+
x5c?: string[];
|
|
217
|
+
/** JWK "x5t" (X.509 Certificate SHA-1 Thumbprint) Parameter */
|
|
218
|
+
x5t?: string;
|
|
219
|
+
/** JWK "x5t#S256" (X.509 Certificate SHA-256 Thumbprint) Parameter */
|
|
220
|
+
"x5t#S256"?: string;
|
|
221
|
+
/** JWK "x5u" (X.509 URL) Parameter */
|
|
222
|
+
x5u?: string;
|
|
223
|
+
/** JWK "kid" (Key ID) Parameter */
|
|
224
|
+
kid?: string;
|
|
225
|
+
/**
|
|
226
|
+
* Non-standard JWK content encryption algorithm hint.
|
|
227
|
+
* When `alg` is `"dir"`, the library reads this field to infer the
|
|
228
|
+
* content encryption algorithm (`enc`) when it is not provided in options.
|
|
229
|
+
*/
|
|
230
|
+
enc?: ContentEncryptionAlgorithm;
|
|
231
|
+
}
|
|
232
|
+
/** Public EC JSON Web Keys */
|
|
233
|
+
interface JWK_EC_Public extends JWKParameters {
|
|
234
|
+
/** EC JWK "kty" (Key Type) Parameter */
|
|
235
|
+
kty: "EC";
|
|
236
|
+
/** EC JWK "crv" (Curve) Parameter */
|
|
237
|
+
crv: string;
|
|
238
|
+
/** EC JWK "x" (X Coordinate) Parameter */
|
|
239
|
+
x: string;
|
|
240
|
+
/** EC JWK "y" (Y Coordinate) Parameter */
|
|
241
|
+
y: string;
|
|
242
|
+
}
|
|
243
|
+
/** Private EC JSON Web Keys */
|
|
244
|
+
interface JWK_EC_Private extends JWK_EC_Public {
|
|
245
|
+
/** EC JWK "d" (ECC Private Key) Parameter */
|
|
246
|
+
d: string;
|
|
247
|
+
}
|
|
248
|
+
/** EC JSON Web Keys */
|
|
249
|
+
type JWK_EC = JWK_EC_Public | JWK_EC_Private;
|
|
250
|
+
/** Public RSA JSON Web Keys */
|
|
251
|
+
interface JWK_RSA_Public extends JWKParameters {
|
|
252
|
+
/** RSA JWK "kty" (Key Type) Parameter */
|
|
253
|
+
kty: "RSA";
|
|
254
|
+
/** RSA JWK "e" (Exponent) Parameter */
|
|
255
|
+
e: string;
|
|
256
|
+
/** RSA JWK "n" (Modulus) Parameter */
|
|
257
|
+
n: string;
|
|
258
|
+
oth?: RsaOtherPrimesInfo[];
|
|
259
|
+
}
|
|
260
|
+
/** Private RSA JSON Web Keys */
|
|
261
|
+
interface JWK_RSA_Private extends JWK_RSA_Public {
|
|
262
|
+
/** RSA JWK "d" (Private Exponent) Parameter */
|
|
263
|
+
d: string;
|
|
264
|
+
/** RSA JWK "dp" (First Factor CRT Exponent) Parameter */
|
|
265
|
+
dp: string;
|
|
266
|
+
/** RSA JWK "dq" (Second Factor CRT Exponent) Parameter */
|
|
267
|
+
dq: string;
|
|
268
|
+
/** RSA JWK "p" (First Prime Factor) Parameter */
|
|
269
|
+
p: string;
|
|
270
|
+
/** RSA JWK "q" (Second Prime Factor) Parameter */
|
|
271
|
+
q: string;
|
|
272
|
+
/** RSA JWK "qi" (First CRT Coefficient) Parameter */
|
|
273
|
+
qi: string;
|
|
274
|
+
}
|
|
275
|
+
/** RSA JSON Web Keys */
|
|
276
|
+
type JWK_RSA = JWK_RSA_Public | JWK_RSA_Private;
|
|
277
|
+
/** Public ED JSON Web Keys */
|
|
278
|
+
interface JWK_OKP_Public extends JWKParameters {
|
|
279
|
+
/** ED JWK "kty" (Key Type) Parameter */
|
|
280
|
+
kty: "OKP";
|
|
281
|
+
/** ED JWK "crv" (Curve) Parameter */
|
|
282
|
+
crv: string;
|
|
283
|
+
/** ED JWK "x" (X Coordinate) Parameter */
|
|
284
|
+
x: string;
|
|
285
|
+
}
|
|
286
|
+
/** Private ED JSON Web Keys */
|
|
287
|
+
interface JWK_OKP_Private extends JWK_OKP_Public {
|
|
288
|
+
/** ED JWK "d" (Private Key) Parameter */
|
|
289
|
+
d: string;
|
|
290
|
+
}
|
|
291
|
+
/** OKP JSON Web Keys */
|
|
292
|
+
type JWK_OKP = JWK_OKP_Public | JWK_OKP_Private;
|
|
293
|
+
/** oct JSON Web Keys */
|
|
294
|
+
interface JWK_oct extends JWKParameters {
|
|
295
|
+
/** Oct JWK "k" (Key Value) Parameter */
|
|
296
|
+
k: string;
|
|
297
|
+
}
|
|
298
|
+
/** Symmetric JSON Web Keys */
|
|
299
|
+
type JWK_Symmetric = JWK_oct;
|
|
300
|
+
/** Asymmetric JSON Web Keys */
|
|
301
|
+
type JWK_Asymmetric = JWK_RSA | JWK_EC | JWK_OKP;
|
|
302
|
+
/** Public JSON Web Keys */
|
|
303
|
+
type JWK_Public = JWK_RSA_Public | JWK_EC_Public | JWK_OKP_Public;
|
|
304
|
+
/** Private JSON Web Keys */
|
|
305
|
+
type JWK_Private = JWK_RSA_Private | JWK_EC_Private | JWK_OKP_Private;
|
|
117
306
|
/**
|
|
118
|
-
*
|
|
119
|
-
* Payload structure:
|
|
120
|
-
* {
|
|
121
|
-
* jti: string;
|
|
122
|
-
* iat: number; (seconds)
|
|
123
|
-
* exp?: number; (seconds)
|
|
124
|
-
* data: Record<string, any>;
|
|
125
|
-
* }
|
|
307
|
+
* JSON Web Key ({@link https://www.rfc-editor.org/rfc/rfc7517 JWK}). "RSA", "EC", "OKP" and "oct" key types are supported.
|
|
126
308
|
*/
|
|
127
|
-
|
|
309
|
+
type JWK = JWK_oct | JWK_RSA | JWK_EC | JWK_OKP;
|
|
128
310
|
/**
|
|
129
|
-
*
|
|
311
|
+
* A pair of public and private JSON Web Keys.
|
|
130
312
|
*/
|
|
131
|
-
|
|
313
|
+
type JWK_Pair = {
|
|
314
|
+
publicKey: JWK_RSA_Public;
|
|
315
|
+
privateKey: JWK_RSA_Private;
|
|
316
|
+
} | {
|
|
317
|
+
publicKey: JWK_EC_Public;
|
|
318
|
+
privateKey: JWK_EC_Private;
|
|
319
|
+
} | {
|
|
320
|
+
publicKey: JWK_OKP_Public;
|
|
321
|
+
privateKey: JWK_OKP_Private;
|
|
322
|
+
};
|
|
132
323
|
/**
|
|
133
|
-
*
|
|
324
|
+
* JSON Web Key Set ({@link https://www.rfc-editor.org/rfc/rfc7517 JWK Set}). "RSA", "EC" and "oct" key types are supported.
|
|
134
325
|
*/
|
|
135
|
-
|
|
326
|
+
interface JWKSet {
|
|
327
|
+
/** JWK Set "keys" Parameter */
|
|
328
|
+
keys: JWK[];
|
|
329
|
+
[parameter: string]: unknown;
|
|
330
|
+
}
|
|
331
|
+
/** JWK Key Algorithms */
|
|
332
|
+
type JWK_HMAC = "HS256" | "HS384" | "HS512";
|
|
333
|
+
type JWK_RSA_SIGN = "RS256" | "RS384" | "RS512";
|
|
334
|
+
type JWK_RSA_PSS = "PS256" | "PS384" | "PS512";
|
|
335
|
+
type JWK_ECDSA = "ES256" | "ES384" | "ES512";
|
|
336
|
+
type JWK_RSA_ENC = "RSA-OAEP" | "RSA-OAEP-256" | "RSA-OAEP-384" | "RSA-OAEP-512";
|
|
337
|
+
type JWK_AES_KW = "A128KW" | "A192KW" | "A256KW";
|
|
338
|
+
type JWK_AES_CBC_HMAC = "A128CBC-HS256" | "A192CBC-HS384" | "A256CBC-HS512";
|
|
339
|
+
type JWK_AES_GCM = "A128GCM" | "A192GCM" | "A256GCM";
|
|
340
|
+
type JWK_AES_GCM_KW = "A128GCMKW" | "A192GCMKW" | "A256GCMKW";
|
|
341
|
+
type JWK_PBES2 = "PBES2-HS256+A128KW" | "PBES2-HS384+A192KW" | "PBES2-HS512+A256KW";
|
|
342
|
+
type JWK_ECDH_ES = "ECDH-ES" | "ECDH-ES+A128KW" | "ECDH-ES+A192KW" | "ECDH-ES+A256KW";
|
|
343
|
+
type JWK_OKP_SIGN = "Ed25519" | "EdDSA";
|
|
344
|
+
type JWK_Symmetric_Algorithm = JWK_HMAC | JWK_AES_KW | JWK_AES_GCM | JWK_AES_GCM_KW;
|
|
345
|
+
type JWK_Asymmetric_Algorithm = JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWK_RSA_ENC | JWK_OKP_SIGN | JWK_ECDH_ES;
|
|
346
|
+
type JWKAlgorithm = JWK_Symmetric_Algorithm | JWK_Asymmetric_Algorithm | JWK_AES_CBC_HMAC | JWK_PBES2 | JWK_ECDH_ES | "dir";
|
|
347
|
+
type JWKPEMAlgorithm = JWK_RSA_PSS | JWK_RSA_SIGN | JWK_RSA_ENC | JWK_ECDSA | JWK_ECDH_ES | JWK_OKP_SIGN;
|
|
136
348
|
//#endregion
|
|
137
|
-
//#region src/
|
|
138
|
-
|
|
139
|
-
interface
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
event: H3Event;
|
|
161
|
-
config: Partial<SessionConfigJWS<T, MaxAge>>;
|
|
162
|
-
}) => void | Promise<void>;
|
|
163
|
-
onExpire?: (args: {
|
|
164
|
-
event: H3Event;
|
|
165
|
-
error: Error;
|
|
166
|
-
config: SessionConfigJWS<T, MaxAge>;
|
|
167
|
-
}) => void | Promise<void>;
|
|
168
|
-
onError?: (args: {
|
|
169
|
-
event: H3Event;
|
|
170
|
-
error: any;
|
|
171
|
-
config: SessionConfigJWS<T, MaxAge>;
|
|
172
|
-
}) => void | Promise<void>;
|
|
173
|
-
onVerifyKeyLookup?: (args: {
|
|
174
|
-
header: JWSProtectedHeader;
|
|
175
|
-
event: H3Event;
|
|
176
|
-
config: SessionConfigJWS<T, MaxAge>;
|
|
177
|
-
}) => JWKSet | JWK_Symmetric | JWK_Public | Promise<JWKSet | JWK_Symmetric | JWK_Public>;
|
|
178
|
-
}
|
|
179
|
-
interface SessionConfigJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
|
|
180
|
-
/**
|
|
181
|
-
* JWK (private for signing with RS/ES/PS, or symmetric oct) used for signing.
|
|
182
|
-
*/
|
|
183
|
-
key: JWK_Symmetric | {
|
|
184
|
-
privateKey: JWK_Private;
|
|
185
|
-
publicKey: JWK_Public | JWK_Public[] | JWKSet;
|
|
186
|
-
};
|
|
187
|
-
/** Session lifetime in seconds (sets exp = iat + maxAge) */
|
|
188
|
-
maxAge?: MaxAge;
|
|
189
|
-
/** Default cookie / header name base */
|
|
190
|
-
name?: string;
|
|
191
|
-
/** Cookie options (false to disable cookies) */
|
|
192
|
-
cookie?: false | CookieSerializeOptions;
|
|
193
|
-
/** Custom header (false to disable header lookup) */
|
|
194
|
-
sessionHeader?: false | string;
|
|
195
|
-
/** Custom ID generator (defaults to crypto.randomUUID) */
|
|
196
|
-
generateId?: () => string;
|
|
197
|
-
/** JWS customization */
|
|
198
|
-
jws?: {
|
|
199
|
-
signOptions?: Omit<JWSSignOptions, "expiresIn">;
|
|
200
|
-
verifyOptions?: JWTClaimValidationOptions;
|
|
201
|
-
};
|
|
202
|
-
hooks?: SessionHooksJWS<T, MaxAge>;
|
|
349
|
+
//#region src/core/types/jwt.d.ts
|
|
350
|
+
/** Header Parameters common to JWE and JWS */
|
|
351
|
+
interface JoseHeaderParameters {
|
|
352
|
+
/** "kid" (Key ID) Header Parameter */
|
|
353
|
+
kid?: string;
|
|
354
|
+
/** "x5t" (X.509 Certificate SHA-1 Thumbprint) Header Parameter */
|
|
355
|
+
x5t?: string;
|
|
356
|
+
/** "x5c" (X.509 Certificate Chain) Header Parameter */
|
|
357
|
+
x5c?: string[];
|
|
358
|
+
/** "x5u" (X.509 URL) Header Parameter */
|
|
359
|
+
x5u?: string;
|
|
360
|
+
/** "jku" (JWK Set URL) Header Parameter */
|
|
361
|
+
jku?: string;
|
|
362
|
+
/** "jwk" (JSON Web Key) Header Parameter */
|
|
363
|
+
jwk?: JWK_Public;
|
|
364
|
+
/** "typ" (Type) Header Parameter */
|
|
365
|
+
typ?: string;
|
|
366
|
+
/** "cty" (Content Type) Header Parameter */
|
|
367
|
+
cty?: string;
|
|
368
|
+
/** "crit" (Critical) Header Parameter */
|
|
369
|
+
crit?: string[];
|
|
370
|
+
/** Any other JWS/JWE Header member. */
|
|
371
|
+
[propName: string]: unknown;
|
|
203
372
|
}
|
|
204
373
|
/**
|
|
205
|
-
* @
|
|
374
|
+
* Payload accepted by {@link sign} and {@link encrypt}.
|
|
375
|
+
*
|
|
376
|
+
* Covers raw strings and bytes for non-JWT use, plus any JSON-serializable
|
|
377
|
+
* object for JWT and generic structured payloads. JWT-specific claim names
|
|
378
|
+
* are not enforced at this level — use {@link JWTClaims} when you want typed
|
|
379
|
+
* claim names and spec-compliant field types.
|
|
206
380
|
*/
|
|
207
|
-
type
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
381
|
+
type JOSEPayload = string | Uint8Array<ArrayBuffer> | Record<string, unknown>;
|
|
382
|
+
interface JWTClaims {
|
|
383
|
+
/** "iss" (Issuer) Claim */
|
|
384
|
+
iss?: string;
|
|
385
|
+
/** "sub" (Subject) Claim */
|
|
386
|
+
sub?: string;
|
|
387
|
+
/** "aud" (Audience) Claim */
|
|
388
|
+
aud?: string | string[];
|
|
389
|
+
/** "exp" (Expiration Time) Claim */
|
|
390
|
+
exp?: number;
|
|
391
|
+
/** "nbf" (Not Before) Claim */
|
|
392
|
+
nbf?: number;
|
|
393
|
+
/** "iat" (Issued At) Claim */
|
|
394
|
+
iat?: number;
|
|
395
|
+
/** "jti" (JWT ID) Claim */
|
|
396
|
+
jti?: string;
|
|
397
|
+
/** Any other JWT member. */
|
|
398
|
+
[propName: string]: unknown;
|
|
399
|
+
}
|
|
400
|
+
interface JWT<T = JWTClaims> {
|
|
401
|
+
header: JoseHeaderParameters & {
|
|
402
|
+
/** "alg" (Algorithm) Header Parameter */alg: string;
|
|
211
403
|
};
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
404
|
+
claims: T;
|
|
405
|
+
}
|
|
406
|
+
interface JWTClaimValidationOptions {
|
|
407
|
+
/** Expected JWT "aud" (Audience) Claim value(s). Implies presence requirement. */
|
|
408
|
+
audience?: string | string[];
|
|
409
|
+
/** Expected JWT "iss" (Issuer) Claim value(s). Implies presence requirement. */
|
|
410
|
+
issuer?: string | string[];
|
|
411
|
+
/** Expected JWT "sub" (Subject) Claim value. Implies presence requirement. */
|
|
412
|
+
subject?: string;
|
|
413
|
+
/** Maximum token age, from the JWT "iat" (Issued At) Claim value. Implies presence requirement. */
|
|
414
|
+
maxTokenAge?: MaxTokenAge;
|
|
415
|
+
/** Clock skew tolerance (in seconds) for validating time-based claims (nbf/exp/iat). */
|
|
416
|
+
clockTolerance?: number;
|
|
417
|
+
/** Expected JWT "typ" (Type) Header Parameter value. Implies presence requirement. */
|
|
418
|
+
typ?: string;
|
|
419
|
+
/** Date to use when comparing NumericDate claims, defaults to `new Date()`. */
|
|
420
|
+
currentDate?: Date;
|
|
421
|
+
/** Additional required claim names. */
|
|
422
|
+
requiredClaims?: string[];
|
|
423
|
+
/**
|
|
424
|
+
* Critical header parameters that this caller understands and has processed.
|
|
425
|
+
* If the token's `crit` header lists a parameter not present in this list
|
|
426
|
+
* (and not natively understood by the library), verification will fail per
|
|
427
|
+
* RFC 7515 §4.1.11 / RFC 7516 §4.1.13.
|
|
428
|
+
*/
|
|
429
|
+
recognizedHeaders?: string[];
|
|
430
|
+
}
|
|
431
|
+
//#endregion
|
|
432
|
+
//#region src/core/types/jws.d.ts
|
|
433
|
+
/** JWS Signing Algorithm Identifier. */
|
|
434
|
+
type JWSAlgorithm = JWK_HMAC | JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWK_OKP_SIGN;
|
|
435
|
+
/** Recognized JWS Header Parameters, any other Header Members may also be present. */
|
|
436
|
+
interface JWSHeaderParameters extends JoseHeaderParameters {
|
|
437
|
+
/** JWS "alg" (Algorithm) Header Parameter */
|
|
438
|
+
alg?: JWSAlgorithm;
|
|
439
|
+
/**
|
|
440
|
+
* This JWS Extension Header Parameter modifies the JWS Payload representation and the JWS Signing
|
|
441
|
+
* Input computation as per {@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}.
|
|
442
|
+
* If `false`, the payload is used directly without Base64URL encoding.
|
|
443
|
+
* Defaults to `true`.
|
|
444
|
+
*/
|
|
445
|
+
b64?: boolean;
|
|
446
|
+
}
|
|
447
|
+
/** JWS Protected Header */
|
|
448
|
+
interface JWSProtectedHeader extends JWSHeaderParameters {
|
|
449
|
+
alg: JWSAlgorithm;
|
|
450
|
+
}
|
|
222
451
|
/**
|
|
223
|
-
*
|
|
452
|
+
* JWS (JSON Web Signature) options for signing
|
|
224
453
|
*/
|
|
225
|
-
|
|
454
|
+
interface JWSSignOptions {
|
|
455
|
+
/** The JWS Algorithm to use. Must be provided. */
|
|
456
|
+
alg?: JWSAlgorithm;
|
|
457
|
+
/**
|
|
458
|
+
* Additional protected header parameters. `alg` is automatically included.
|
|
459
|
+
* `typ` defaults to "JWT" if not provided and payload is an object.
|
|
460
|
+
*/
|
|
461
|
+
protectedHeader?: JWSHeaderParameters;
|
|
462
|
+
/** Date to use when comparing NumericDate claims, defaults to `new Date()`. */
|
|
463
|
+
currentDate?: Date;
|
|
464
|
+
/**
|
|
465
|
+
* Time at which the JWS should expire, if no `exp` was already provided.
|
|
466
|
+
*/
|
|
467
|
+
expiresIn?: ExpiresIn;
|
|
468
|
+
}
|
|
469
|
+
/** Result of JWS verification */
|
|
470
|
+
interface JWSVerifyResult<T extends JOSEPayload = JOSEPayload> {
|
|
471
|
+
/** The decoded and verified payload. */
|
|
472
|
+
payload: T;
|
|
473
|
+
/** The JWS Protected Header. */
|
|
474
|
+
protectedHeader: JWSProtectedHeader;
|
|
475
|
+
}
|
|
476
|
+
/** Options for JWS verification */
|
|
477
|
+
interface JWSVerifyOptions extends JWTClaimValidationOptions {
|
|
478
|
+
/** List of allowed algorithms. If provided, the JWS `alg` must be in this list. */
|
|
479
|
+
algorithms?: JWSAlgorithm[];
|
|
480
|
+
/** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred. */
|
|
481
|
+
forceUint8Array?: boolean;
|
|
482
|
+
/**
|
|
483
|
+
* Controls JWT claim validation after a successful verification.
|
|
484
|
+
*
|
|
485
|
+
* - `true` — always validate claims regardless of the `typ` header
|
|
486
|
+
* - `false` — skip claim validation entirely
|
|
487
|
+
* - `undefined` (default) — validate when `typ` is `"JWT"` or contains `"jwt"`
|
|
488
|
+
*/
|
|
489
|
+
validateClaims?: boolean;
|
|
490
|
+
}
|
|
491
|
+
//#endregion
|
|
492
|
+
//#region src/core/utils/types.d.ts
|
|
493
|
+
type StrictOmit<T, K extends keyof T | (string & {}) | (number & {}) | (symbol & {})> = { [P in keyof T as P extends K ? never : P]: T[P] };
|
|
494
|
+
//#endregion
|
|
495
|
+
//#region src/core/types/jwe.d.ts
|
|
496
|
+
/** Recognized JWE Header Parameters, any other Header Members may also be present. */
|
|
497
|
+
interface JWEHeaderParameters extends JoseHeaderParameters {
|
|
498
|
+
/**
|
|
499
|
+
* `alg` (Algorithm): Header Parameter - Key Management Algorithm
|
|
500
|
+
*/
|
|
501
|
+
alg?: KeyManagementAlgorithm | (string & {});
|
|
502
|
+
/**
|
|
503
|
+
* `enc` (Encryption Algorithm): Header Parameter - Content Encryption Algorithm
|
|
504
|
+
*/
|
|
505
|
+
enc?: ContentEncryptionAlgorithm | (string & {});
|
|
506
|
+
/**
|
|
507
|
+
* `p2c` (PBES2 Count): Header Parameter
|
|
508
|
+
*/
|
|
509
|
+
p2c?: number;
|
|
510
|
+
/**
|
|
511
|
+
* `p2s` (PBES2 Salt): Header Parameter. Base64URL encoded.
|
|
512
|
+
*/
|
|
513
|
+
p2s?: string;
|
|
514
|
+
/**
|
|
515
|
+
* "iv" (Initialization Vector) Header Parameter for AES GCM Key Wrapping. Base64URL encoded.
|
|
516
|
+
*/
|
|
517
|
+
iv?: string;
|
|
518
|
+
/**
|
|
519
|
+
* "tag" (Authentication Tag) Header Parameter for AES GCM Key Wrapping. Base64URL encoded.
|
|
520
|
+
*/
|
|
521
|
+
tag?: string;
|
|
522
|
+
/**
|
|
523
|
+
* "epk" (Ephemeral Public Key) Header Parameter for ECDH-ES.
|
|
524
|
+
*/
|
|
525
|
+
epk?: JWK_EC_Public;
|
|
526
|
+
/**
|
|
527
|
+
* "apu" (Agreement PartyUInfo) Header Parameter for ECDH-ES. Base64URL encoded.
|
|
528
|
+
*/
|
|
529
|
+
apu?: string;
|
|
530
|
+
/**
|
|
531
|
+
* "apv" (Agreement PartyVInfo) Header Parameter for ECDH-ES. Base64URL encoded.
|
|
532
|
+
*/
|
|
533
|
+
apv?: string;
|
|
534
|
+
/** Any other JWE Header member. */
|
|
535
|
+
[propName: string]: unknown;
|
|
536
|
+
}
|
|
537
|
+
/** Recognized JWE Key Management-related Header Parameters. */
|
|
538
|
+
interface JWEKeyManagementHeaderParameters {
|
|
539
|
+
apu?: Uint8Array<ArrayBuffer>;
|
|
540
|
+
apv?: Uint8Array<ArrayBuffer>;
|
|
541
|
+
/**
|
|
542
|
+
* For Internal use only
|
|
543
|
+
*/
|
|
544
|
+
p2c?: number;
|
|
545
|
+
/**
|
|
546
|
+
* For Internal use only
|
|
547
|
+
*/
|
|
548
|
+
p2s?: Uint8Array<ArrayBuffer>;
|
|
549
|
+
/**
|
|
550
|
+
* For Internal use only
|
|
551
|
+
*/
|
|
552
|
+
iv?: Uint8Array<ArrayBuffer>;
|
|
553
|
+
/**
|
|
554
|
+
* For Internal use only
|
|
555
|
+
*/
|
|
556
|
+
epk?: CryptoKey | JWK_EC_Public;
|
|
557
|
+
/**
|
|
558
|
+
* For Internal use only
|
|
559
|
+
*/
|
|
560
|
+
epkPrivateKey?: CryptoKey | JWK_EC_Private;
|
|
561
|
+
}
|
|
226
562
|
/**
|
|
227
|
-
*
|
|
563
|
+
* JWE (JSON Web Encryption) encryption options
|
|
228
564
|
*/
|
|
229
|
-
|
|
565
|
+
interface JWEEncryptOptions {
|
|
566
|
+
/** JWE "alg" (Algorithm) Header Parameter. Key Management Algorithm. */
|
|
567
|
+
alg?: KeyManagementAlgorithm;
|
|
568
|
+
/** JWE "enc" (Encryption Algorithm) Header Parameter. Content Encryption Algorithm. */
|
|
569
|
+
enc?: ContentEncryptionAlgorithm;
|
|
570
|
+
/** Date to use when computing NumericDate claims, defaults to `new Date()`. */
|
|
571
|
+
currentDate?: Date;
|
|
572
|
+
/** Time at which the JWT should expire, if no `exp` was already provided (only when typ is "JWT" or explicitly parsing JWT). */
|
|
573
|
+
expiresIn?: ExpiresIn;
|
|
574
|
+
/** Additional JWE Protected Header parameters. */
|
|
575
|
+
protectedHeader?: StrictOmit<JWEHeaderParameters, "alg" | "enc" | "iv" | "tag" | "p2s" | "p2c" | "epk" | "apu" | "apv">;
|
|
576
|
+
/** Initialization Vector for AES-GCMKW key wrapping. Generated if not provided. */
|
|
577
|
+
keyManagementIV?: Uint8Array<ArrayBuffer>;
|
|
578
|
+
/** PBES2 Salt value (p2s). Required for PBES2 algorithms. */
|
|
579
|
+
p2s?: Uint8Array<ArrayBuffer>;
|
|
580
|
+
/** PBES2 Iteration count (p2c). Required for PBES2 algorithms. */
|
|
581
|
+
p2c?: number;
|
|
582
|
+
/** ECDH-ES specific options. */
|
|
583
|
+
ecdh?: {
|
|
584
|
+
/**
|
|
585
|
+
* ECDH-ES Ephemeral key material. Provide a private key (CryptoKey or JWK),
|
|
586
|
+
* a CryptoKeyPair, or an object exposing both public and private parts.
|
|
587
|
+
*/
|
|
588
|
+
ephemeralKey?: CryptoKey | JWK_EC_Private | CryptoKeyPair | {
|
|
589
|
+
publicKey: CryptoKey | JWK_EC_Public;
|
|
590
|
+
privateKey: CryptoKey | JWK_EC_Private;
|
|
591
|
+
}; /** ECDH-ES Agreement PartyUInfo. */
|
|
592
|
+
partyUInfo?: Uint8Array<ArrayBuffer>; /** ECDH-ES Agreement PartyVInfo. */
|
|
593
|
+
partyVInfo?: Uint8Array<ArrayBuffer>;
|
|
594
|
+
};
|
|
595
|
+
/**
|
|
596
|
+
* Content Encryption Key (CEK) to use.
|
|
597
|
+
* If provided, it will be used directly. Otherwise, a CEK will be generated.
|
|
598
|
+
*/
|
|
599
|
+
cek?: Uint8Array<ArrayBuffer>;
|
|
600
|
+
/**
|
|
601
|
+
* Initialization Vector for content encryption.
|
|
602
|
+
* If provided, it will be used. Otherwise, one will be generated.
|
|
603
|
+
*/
|
|
604
|
+
contentEncryptionIV?: Uint8Array<ArrayBuffer>;
|
|
605
|
+
}
|
|
230
606
|
/**
|
|
231
|
-
*
|
|
232
|
-
* Payload claims:
|
|
233
|
-
* jti, iat, exp?, data, plus optional extraClaims (cannot override reserved)
|
|
607
|
+
* JWE (JSON Web Encryption) decryption options
|
|
234
608
|
*/
|
|
235
|
-
|
|
609
|
+
interface JWEDecryptOptions extends JWTClaimValidationOptions {
|
|
610
|
+
/** A list of allowed JWE "alg" (Algorithm) Header Parameter values for key management. */
|
|
611
|
+
algorithms?: KeyManagementAlgorithm[];
|
|
612
|
+
/** A list of allowed JWE "enc" (Encryption Algorithm) Header Parameter values for content encryption. */
|
|
613
|
+
encryptionAlgorithms?: ContentEncryptionAlgorithm[];
|
|
614
|
+
/** Algorithm to import the unwrapped CEK as (e.g., { name: 'AES-GCM' }). Defaults based on 'enc'. */
|
|
615
|
+
unwrappedKeyAlgorithm?: Parameters<typeof crypto.subtle.importKey>[2];
|
|
616
|
+
/** Key usages for the unwrapped CEK. Defaults based on 'enc' (typically ['encrypt', 'decrypt']). */
|
|
617
|
+
keyUsage?: KeyUsage[];
|
|
618
|
+
/** Mark the unwrapped CEK as extractable. Defaults to true. */
|
|
619
|
+
extractable?: boolean;
|
|
620
|
+
/** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred based on JWE headers. */
|
|
621
|
+
forceUint8Array?: boolean;
|
|
622
|
+
/**
|
|
623
|
+
* Controls JWT claim validation after a successful decryption.
|
|
624
|
+
*
|
|
625
|
+
* - `true` — always validate claims regardless of the `typ` header
|
|
626
|
+
* - `false` — skip claim validation entirely
|
|
627
|
+
* - `undefined` (default) — validate when `typ` is `"JWT"` or contains `"jwt"`
|
|
628
|
+
*/
|
|
629
|
+
validateClaims?: boolean;
|
|
630
|
+
/** If true, include the Content Encryption Key (CEK) and Additional Authenticated Data (AAD) in the result. */
|
|
631
|
+
returnCek?: boolean;
|
|
632
|
+
}
|
|
236
633
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
* - (optional) standard claim checks if validateJWT true
|
|
241
|
-
* - ensures jti & iat presence
|
|
242
|
-
* - enforces maxAge if provided
|
|
634
|
+
* JWE Protected Header — the parsed and validated protected header returned
|
|
635
|
+
* after a successful {@link decrypt} call. Both `alg` and `enc` are present
|
|
636
|
+
* and strongly typed because decryption could not have succeeded without them.
|
|
243
637
|
*/
|
|
244
|
-
|
|
638
|
+
interface JWEProtectedHeader extends JWEHeaderParameters {
|
|
639
|
+
/** Key management algorithm. */
|
|
640
|
+
alg: KeyManagementAlgorithm;
|
|
641
|
+
/** Content encryption algorithm. */
|
|
642
|
+
enc: ContentEncryptionAlgorithm;
|
|
643
|
+
}
|
|
245
644
|
/**
|
|
246
|
-
*
|
|
645
|
+
* Result of a JWE decryption operation.
|
|
247
646
|
*/
|
|
248
|
-
|
|
647
|
+
interface JWEDecryptResult<T extends JOSEPayload = JOSEPayload> {
|
|
648
|
+
/** The decrypted payload. */
|
|
649
|
+
payload: T;
|
|
650
|
+
/** The JWE Protected Header. */
|
|
651
|
+
protectedHeader: JWEProtectedHeader;
|
|
652
|
+
/** The Content Encryption Key (CEK) used for decryption, as Uint8Array. Only present when `returnCek` is true. */
|
|
653
|
+
cek?: Uint8Array<ArrayBuffer>;
|
|
654
|
+
/** The Additional Authenticated Data (AAD) used, as Uint8Array. Only present when `returnCek` is true. */
|
|
655
|
+
aad?: Uint8Array<ArrayBuffer>;
|
|
656
|
+
}
|
|
657
|
+
//#endregion
|
|
658
|
+
//#region src/core/types/index.d.ts
|
|
659
|
+
type ExpiresIn = number | `${number}` | `${number}${"s" | "second" | "seconds" | "m" | "minute" | "minutes" | "h" | "hour" | "hours" | "D" | "day" | "days" | "W" | "week" | "weeks" | "M" | "month" | "months" | "Y" | "year" | "years"}`;
|
|
660
|
+
type MaxTokenAge = ExpiresIn;
|
|
249
661
|
//#endregion
|
|
250
|
-
export {
|
|
662
|
+
export { JWK_OKP_SIGN as $, JWK as A, JWK_AES_GCM_KW as B, DeriveKeyOptions as C, GenerateKeyAlgorithm as D, GenerateJWKReturn as E, JWKPEMAlgorithm as F, JWK_ECDH_ES as G, JWK_Asymmetric as H, JWKParameters as I, JWK_EC_Public as J, JWK_ECDSA as K, JWKSet as L, JWKCacheAdapter as M, JWKLookupFunction as N, GenerateKeyOptions as O, JWKLookupFunctionHeader as P, JWK_OKP_Public as Q, JWK_AES_CBC_HMAC as R, ContentEncryptionAlgorithm as S, GenerateJWKOptions as T, JWK_Asymmetric_Algorithm as U, JWK_AES_KW as V, JWK_EC as W, JWK_OKP as X, JWK_HMAC as Y, JWK_OKP_Private as Z, JWT as _, JWEEncryptOptions as a, JWK_RSA_ENC as at, JoseHeaderParameters as b, JWEProtectedHeader as c, JWK_RSA_Public as ct, JWSHeaderParameters as d, JWK_Symmetric_Algorithm as dt, JWK_PBES2 as et, JWSProtectedHeader as f, JWK_oct as ft, JOSEPayload as g, WrapKeyResult as gt, JWSVerifyResult as h, WrapKeyOptions as ht, JWEDecryptResult as i, JWK_RSA as it, JWKAlgorithm as j, GenerateKeyReturn as k, StrictOmit as l, JWK_RSA_SIGN as lt, JWSVerifyOptions as m, UnwrapKeyOptions as mt, MaxTokenAge as n, JWK_Private as nt, JWEHeaderParameters as o, JWK_RSA_PSS as ot, JWSSignOptions as p, KeyManagementAlgorithm as pt, JWK_EC_Private as q, JWEDecryptOptions as r, JWK_Public as rt, JWEKeyManagementHeaderParameters as s, JWK_RSA_Private as st, ExpiresIn as t, JWK_Pair as tt, JWSAlgorithm as u, JWK_Symmetric as ut, JWTClaimValidationOptions as v, DeriveKeyReturn as w, CompositeKey as x, JWTClaims as y, JWK_AES_GCM as z };
|