unjwt 0.7.2 → 0.8.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.
Files changed (39) hide show
  1. package/README.md +4 -4
  2. package/dist/THIRD-PARTY-LICENSES.md.gz +0 -0
  3. package/dist/_chunks/_codec-jwe.d.mts +170 -0
  4. package/dist/_chunks/_codec-jws.mjs +878 -0
  5. package/dist/_chunks/_internal.d.mts +7 -0
  6. package/dist/_chunks/_jwt-internal.mjs +388 -0
  7. package/dist/_chunks/algorithms.mjs +186 -0
  8. package/dist/_chunks/index.d.mts +512 -818
  9. package/dist/_chunks/index2.d.mts +11 -0
  10. package/dist/_chunks/index3.d.mts +108 -0
  11. package/dist/_chunks/jwe.d.mts +43 -66
  12. package/dist/_chunks/jwe.mjs +384 -252
  13. package/dist/_chunks/jwk.mjs +1390 -622
  14. package/dist/_chunks/jws.d.mts +48 -103
  15. package/dist/_chunks/jws.mjs +372 -184
  16. package/dist/_chunks/jwt.d.mts +56 -0
  17. package/dist/_chunks/libs/unsecure.d.mts +164 -85
  18. package/dist/_chunks/libs/unsecure.mjs +390 -123
  19. package/dist/adapters/elysia/index.d.mts +33 -171
  20. package/dist/adapters/elysia/index.mjs +106 -575
  21. package/dist/adapters/h3v1/index.d.mts +59 -344
  22. package/dist/adapters/h3v1/index.mjs +114 -619
  23. package/dist/adapters/h3v2/index.d.mts +58 -326
  24. package/dist/adapters/h3v2/index.mjs +108 -611
  25. package/dist/core/internal.d.mts +2 -0
  26. package/dist/core/internal.mjs +1 -0
  27. package/dist/core/jwe.d.mts +4 -2
  28. package/dist/core/jwe.mjs +4 -2
  29. package/dist/core/jwk.d.mts +183 -371
  30. package/dist/core/jwk.mjs +4 -3
  31. package/dist/core/jws.d.mts +4 -2
  32. package/dist/core/jws.mjs +4 -2
  33. package/dist/core/utils/index.d.mts +5 -155
  34. package/dist/core/utils/index.mjs +12 -3
  35. package/dist/index.d.mts +8 -4
  36. package/dist/index.mjs +6 -3
  37. package/package.json +39 -36
  38. package/dist/_chunks/_internal.mjs +0 -88
  39. package/dist/_chunks/utils.mjs +0 -390
@@ -1,360 +1,75 @@
1
- import { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, Duration, ExpiresIn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, JOSEPayload, JWEAsymmetricPrivateJWK, JWEAsymmetricPublicJWK, JWEDecryptJWK, JWEDecryptOptions, JWEDecryptResult, JWEEncryptJWK, JWEEncryptOptions, JWEFlattenedSerialization, JWEGeneralRecipient, JWEGeneralSerialization, JWEHeaderParameters, JWEKeyManagementHeaderParameters, JWEMultiDecryptOptions, JWEMultiDecryptResult, JWEMultiEncryptOptions, JWEMultiRecipient, JWEProtectedHeader, JWK, JWKAlgorithm, JWKCacheAdapter, JWKLookupFunction, JWKLookupFunctionHeader, JWKPEMAlgorithm, JWKParameters, JWKSet, JWK_AES_CBC_HMAC, JWK_AES_GCM, JWK_AES_GCM_KW, JWK_AES_KW, JWK_Asymmetric, JWK_Asymmetric_Algorithm, JWK_EC, JWK_ECDH_ES, JWK_ECDSA, JWK_EC_Private, JWK_EC_Public, JWK_HMAC, JWK_OKP, JWK_OKP_Private, JWK_OKP_Public, JWK_OKP_SIGN, JWK_PBES2, JWK_Pair, JWK_Private, JWK_Public, JWK_RSA, JWK_RSA_ENC, JWK_RSA_PSS, JWK_RSA_Private, JWK_RSA_Public, JWK_RSA_SIGN, JWK_Symmetric, JWK_Symmetric_Algorithm, JWK_oct, JWSAlgorithm, JWSAsymmetricPrivateJWK, JWSAsymmetricPublicJWK, JWSFlattenedSerialization, JWSGeneralSerialization, JWSGeneralSignature, JWSHeaderParameters, JWSMultiSignOptions, JWSMultiSigner, JWSMultiVerifyAllOptions, JWSMultiVerifyOptions, JWSMultiVerifyOutcome, JWSMultiVerifyResult, JWSProtectedHeader, JWSSignJWK, JWSSignOptions, JWSVerifyJWK, JWSVerifyOptions, JWSVerifyResult, JWT, JWTClaimValidationOptions, JWTClaims, JoseHeaderParameters, KeyManagementAlgorithm, MaxTokenAge, NotBeforeIn, StrictOmit, UnwrapKeyOptions, UnwrappingKeyFor, WrapKeyOptions, WrapKeyResult, WrappingKeyFor } from "../../_chunks/index.mjs";
2
- import { deriveJWKFromPassword, exportPEM, exportToPEM, generateJWK, importFromPEM, importPEM } from "../../core/jwk.mjs";
3
- import { H3Event } from "h3";
1
+ import { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, Duration, ECDHEphemeralKeyInput, ExpiresIn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, GeneratedJWKMap, GeneratedKeyMap, ImportedKey, ImportedKeyMap, JOSEConsumerOptions, JOSELimits, JOSEPayload, JOSEPayloadMode, JOSEPayloadTypeMap, JWEAsymmetricPrivateJWK, JWEAsymmetricPublicJWK, JWEDecryptJWK, JWEDecryptOptions, JWEDecryptResult, JWEEncryptJWK, JWEEncryptOptions, JWEFlattenedSerialization, JWEGeneralRecipient, JWEGeneralSerialization, JWEHeaderParameters, JWEKeyManagementHeaderParameters, JWELibraryHeaderName, JWEMultiDecryptKey, JWEMultiDecryptOptions, JWEMultiDecryptResult, JWEMultiEncryptOptions, JWEMultiRecipient, JWEProtectedHeader, JWK, JWKAlgorithm, JWKCacheAdapter, JWKLookupFunction, JWKLookupFunctionHeader, JWKMetadata, JWKPEMAlgorithm, JWKParameters, JWKSet, JWK_AES_CBC_HMAC, JWK_AES_GCM, JWK_AES_GCM_KW, JWK_AES_KW, JWK_Asymmetric, JWK_Asymmetric_Algorithm, JWK_EC, JWK_ECDH_ES, JWK_ECDSA, JWK_EC_Private, JWK_EC_Public, JWK_HMAC, JWK_OKP, JWK_OKP_Private, JWK_OKP_Public, JWK_OKP_SIGN, JWK_PBES2, JWK_Pair, JWK_Private, JWK_Public, JWK_RSA, JWK_RSA_ENC, JWK_RSA_PSS, JWK_RSA_Private, JWK_RSA_Public, JWK_RSA_SIGN, JWK_Symmetric, JWK_Symmetric_Algorithm, JWK_oct, JWSAlgorithm, JWSAsymmetricPrivateJWK, JWSAsymmetricPublicJWK, JWSFlattenedSerialization, JWSGeneralSerialization, JWSGeneralSignature, JWSHeaderParameters, JWSMultiSignOptions, JWSMultiSigner, JWSMultiVerifyAllOptions, JWSMultiVerifyOptions, JWSMultiVerifyOutcome, JWSMultiVerifyResult, JWSProtectedHeader, JWSSignJWK, JWSSignOptions, JWSVerifyJWK, JWSVerifyOptions, JWSVerifyResult, JWT, JWTClaimValidationOptions, JWTClaims, JWTTimeClaimOptions, JoseHeaderParameters, KeyManagementAlgorithm, MaxTokenAge, NotBeforeIn, PayloadOf, UnwrapKeyOptions, UnwrapKeyResult, UnwrappingKeyFor, WrapKeyOptions, WrapKeyResult, WrappingKeyFor } from "../../_chunks/index.mjs";
2
+ import { JWESessionConfig, JWESessionHooks, JWSSessionConfig, JWSSessionHooks, SessionClaims, SessionData, SessionDecryptOptions, SessionEncryptOptions, SessionIssueOptions, SessionManager, SessionRecord, SessionSignOptions, SessionState, SessionUpdate, SessionUpdateTrigger, SessionVerifyOptions } from "../../_chunks/_codec-jwe.mjs";
3
+ import { thumbprint } from "../../_chunks/index2.mjs";
4
+ import { deriveJWKFromPassword, exportPEM, generateJWK, importPEM } from "../../core/jwk.mjs";
5
+ import { H3Event, H3EventContext } from "h3";
4
6
  import { CookieSerializeOptions } from "cookie-es";
5
- interface SessionClaims extends Required<Pick<JWTClaims, "jti" | "iat">>, StrictOmit<JWTClaims, "jti" | "iat"> {}
6
- type SessionData<T extends Record<string, any> = SessionClaims> = StrictOmit<T, "jti" | "iat" | "exp">;
7
- type SessionUpdate<T extends Record<string, any> = SessionClaims> = Partial<SessionData<T>> | ((oldData: SessionData<T>) => Partial<SessionData<T>> | undefined);
8
- interface SessionManager<T extends Record<string, any> = SessionClaims, ConfigMaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
9
- readonly id: string | undefined;
10
- readonly createdAt: number;
11
- readonly expiresAt: ConfigMaxAge extends ExpiresIn ? number : "exp" extends keyof T ? T["exp"] : number | undefined;
12
- readonly data: SessionData<T>;
13
- readonly token: string | undefined;
14
- update: (update?: SessionUpdate<T>) => Promise<SessionManager<T, ConfigMaxAge> & {
15
- readonly id: string;
16
- readonly token: string;
17
- }>;
18
- clear: () => Promise<SessionManager<T, ConfigMaxAge> & {
19
- readonly id: undefined;
20
- readonly createdAt: number;
21
- readonly expiresAt: undefined;
22
- }>;
23
- }
24
- declare const kGetSessionPromise$1: unique symbol;
25
- /**
26
- * In-memory session state for the current request, mirroring the claims of
27
- * the underlying compact JWE token.
28
- */
29
- interface SessionJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
30
- /** Session ID — mirrors the JWT `jti` claim. `undefined` until the session is persisted. */
31
- id: string | undefined;
32
- /** Session creation time in ms — mirrors the JWT `iat` claim (seconds). */
33
- createdAt: number;
34
- /** Session expiry time in ms — mirrors the JWT `exp` claim (seconds). */
35
- expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
36
- /** Session data — carried in the token payload, spread at the top level. */
37
- data: SessionData<T>;
38
- /** The current compact JWE token. `undefined` until the session is persisted. */
39
- token: string | undefined;
40
- [kGetSessionPromise$1]?: Promise<SessionJWE<T, MaxAge>>;
41
- }
42
- /**
43
- * Lifecycle hooks for JWE sessions. `onRead`, `onExpire`, and `onError` are
44
- * mutually exclusive per incoming token — `onRead` fires only when a session
45
- * was successfully established from it.
46
- */
47
- interface SessionHooksJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event> {
48
- /**
49
- * Fires when an incoming token was decrypted and the session established.
50
- * May fire more than once per request when `useJWESession` is called
51
- * concurrently in the same handler — dedupe via `session.id` if needed.
52
- */
53
- onRead?: (args: {
54
- session: SessionJWE<T, MaxAge> & {
55
- id: string;
56
- token: string;
57
- };
58
- event: TEvent;
59
- config: SessionConfigJWE<T, MaxAge, TEvent>;
60
- }) => void | Promise<void>;
61
- /** Fires after a new token was sealed — including pure token refreshes (update with no data). */
62
- onUpdate?: (args: {
63
- /** Session after the update. */session: SessionJWE<T, MaxAge> & {
64
- id: string;
65
- token: string;
66
- }; /** Deep-copied snapshot of the session before the update. */
67
- oldSession: SessionJWE<T, MaxAge>;
68
- event: TEvent;
69
- config: SessionConfigJWE<T, MaxAge, TEvent>;
70
- }) => void | Promise<void>;
71
- /** Fires when the session is cleared. */
72
- onClear?: (args: {
73
- /** Last known session before clearing, if any. */oldSession: SessionJWE<T, MaxAge> | undefined;
74
- event: TEvent;
75
- config: SessionConfigJWE<T, MaxAge, TEvent>;
76
- }) => void | Promise<void>;
77
- /** Fires when the incoming token is expired. Receives the expired token's decoded claims. */
78
- onExpire?: (args: {
79
- session: {
80
- id: string | undefined;
81
- createdAt: number | undefined;
82
- expiresAt: number | undefined;
83
- token: string;
84
- };
85
- event: TEvent;
86
- error: Error;
87
- config: SessionConfigJWE<T, MaxAge, TEvent>;
88
- }) => void | Promise<void>;
89
- /** Fires on read-path failures other than expiry, and on write-path seal failures. */
90
- onError?: (args: {
91
- /**
92
- * The session involved in the error.
93
- */
94
- session: SessionJWE<T, MaxAge>;
95
- event: TEvent;
96
- error: any;
97
- config: SessionConfigJWE<T, MaxAge, TEvent>;
98
- }) => void | Promise<void>;
99
- /** Resolve the decryption key for the incoming token from its protected header (e.g. by `kid`). */
100
- onUnsealKeyLookup?: (args: {
101
- header: JWEHeaderParameters;
102
- event: TEvent;
103
- config: SessionConfigJWE<T, MaxAge, TEvent>;
104
- }) => JWEDecryptJWK | Promise<JWEDecryptJWK>;
105
- }
106
- /**
107
- * Configuration for a JWE-backed (encrypted) session.
108
- */
109
- interface SessionConfigJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event> {
110
- /**
111
- * Secret used for sealing — a password string (PBES2), a symmetric JWK, or
112
- * an asymmetric pair: the public key seals, the private key unseals.
113
- */
114
- key: string | JWEEncryptJWK | {
115
- privateKey: JWEAsymmetricPrivateJWK;
116
- publicKey?: JWEAsymmetricPublicJWK;
117
- };
118
- /**
119
- * Session lifetime in seconds — sets `exp = iat + maxAge` in the token and
120
- * drives the cookie expiry. Without it the library writes no `exp` — though
121
- * an `exp` key in the session data is then carried into the token as its
122
- * actual expiry claim.
123
- */
124
- maxAge?: MaxAge;
125
- /** Session name — drives the cookie name and the default session header. Default `"h3-jwe"`. */
126
- name?: string;
127
- /** Cookie options (`false` to disable). Defaults: `path="/"`, `secure`, `httpOnly: true`. */
128
- cookie?: false | CookieSerializeOptions;
129
- /** Request header to read the token from (`false` to disable). Default `x-<name>-session`. */
130
- sessionHeader?: false | string;
131
- /** Session ID (`jti`) generator. Default `crypto.randomUUID`. */
132
- generateId?: () => string;
133
- /** JWE encrypt/decrypt overrides. Set `encryptOptions.alg` and `enc` to pin the session algorithms — unsealing then accepts only them. */
134
- jwe?: {
135
- encryptOptions?: Omit<JWEEncryptOptions, "expiresIn">;
136
- decryptOptions?: JWTClaimValidationOptions;
137
- };
138
- /** Lifecycle hooks — see {@link SessionHooksJWE}. */
139
- hooks?: SessionHooksJWE<T, MaxAge, TEvent>;
140
- }
141
- type CompatEvent$1 = {
7
+ /** Event shapes the adapter reads from: an h3 v1 event, or any request-like object. */
8
+ type CompatEvent = {
142
9
  request: {
143
10
  headers: Headers;
144
11
  };
145
- context: any;
12
+ context: H3EventContext;
146
13
  } | {
147
14
  headers: Headers;
148
- context: any;
15
+ context: H3EventContext;
149
16
  };
150
- /**
151
- * Create a session manager for the current request using JWE (encrypted).
152
- */
153
- declare function useJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<SessionManager<T, MaxAge>>;
154
- /**
155
- * Get (and lazily initialize) the session for the current request.
156
- */
157
- declare function getJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<SessionJWE<T, MaxAge>>;
158
- /**
159
- * Read the raw session token for the request. The session header (default
160
- * `x-<name>-session`, `Bearer ` prefix stripped) takes precedence, then a
161
- * freshly-minted token on the response's `Set-Cookie`, then the session cookie.
162
- */
163
- declare function getJWESessionToken<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): string | undefined;
164
- /**
165
- * Update the session data (if provided) and reissue the JWE token.
166
- */
167
- declare function updateJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>, update?: SessionUpdate<T>): Promise<SessionJWE<T, MaxAge> & {
17
+ /** Session state for the current request, mirroring the claims of its JWE token. */
18
+ interface SessionJWE<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> extends SessionState<T, MaxAge> {}
19
+ /** JWE session hooks; `onRead`, `onExpire` and `onError` are mutually exclusive per token. */
20
+ interface SessionHooksJWE<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event> extends JWESessionHooks<SessionJWE<T, MaxAge>, SessionManager<T, MaxAge>, {
21
+ event: TEvent;
22
+ }, SessionConfigJWE<T, MaxAge, TEvent>> {}
23
+ /** Configuration for a JWE-backed (encrypted) session. */
24
+ interface SessionConfigJWE<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event> extends JWESessionConfig<MaxAge, CookieSerializeOptions> {
25
+ /** Cookie and session-header name. Default `"h3-jwe"`; a `.<digits>` suffix throws `ERR_SESSION_CONFIG_INVALID`. */
26
+ name?: string;
27
+ hooks?: SessionHooksJWE<T, MaxAge, TEvent>;
28
+ }
29
+ /** Create a JWE (encrypted) session manager for the request. */
30
+ export declare function useJWESession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<SessionManager<T, MaxAge>>;
31
+ /** Get (and lazily initialize) the session for the current request. */
32
+ export declare function getJWESession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<SessionJWE<T, MaxAge>>;
33
+ /** Read the raw session token: cookie first, then the session header (default `x-<name>-session`, optional `Bearer`). */
34
+ export declare function getJWESessionToken<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): string | undefined;
35
+ /** Update the session data (if provided) and reissue the JWE; `options.maxAge` overrides this token's lifetime. */
36
+ export declare function updateJWESession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>, update?: SessionUpdate<T>, options?: SessionIssueOptions): Promise<SessionJWE<T, MaxAge> & {
168
37
  id: string;
169
38
  token: string;
170
39
  }>;
171
- /**
172
- * Seal the current session as a compact JWE.
173
- * Payload: `session.data` spread at the top level, plus the reserved claims
174
- * `jti`, `iat`, and `exp` (when `maxAge` is set), which overwrite any
175
- * same-named keys in the session data.
176
- */
177
- declare function sealJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<string>;
178
- /**
179
- * Decrypt and parse a compact JWE into a Session structure.
180
- * Performs:
181
- * - authenticated decryption
182
- * - claim validation (always on): `jti` & `iat` required, `exp` honoured
183
- * - `typ` header check and alg/enc pinning when configured
184
- * - `maxAge` enforced as `maxTokenAge` when provided
185
- */
186
- declare function unsealJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>, sealed: string): Promise<Partial<SessionJWE<T, MaxAge>>>;
187
- /**
188
- * Clear the session (delete from context and drop cookie).
189
- */
190
- declare function clearJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<void>;
191
- declare const kGetSessionPromise: unique symbol;
192
- /**
193
- * In-memory session state for the current request, mirroring the claims of
194
- * the underlying compact JWS token.
195
- */
196
- interface SessionJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
197
- /** Session ID — mirrors the JWT `jti` claim. `undefined` until the session is persisted. */
198
- id: string | undefined;
199
- /** Session creation time in ms — mirrors the JWT `iat` claim (seconds). */
200
- createdAt: number;
201
- /** Session expiry time in ms — mirrors the JWT `exp` claim (seconds). */
202
- expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
203
- /** Session data — carried in the token payload, spread at the top level. */
204
- data: SessionData<T>;
205
- /** The current compact JWS token. `undefined` until the session is persisted. */
206
- token: string | undefined;
207
- [kGetSessionPromise]?: Promise<SessionJWS<T, MaxAge>>;
208
- }
209
- /**
210
- * Lifecycle hooks for JWS sessions. `onRead`, `onExpire`, and `onError` are
211
- * mutually exclusive per incoming token — `onRead` fires only when a session
212
- * was successfully established from it.
213
- */
214
- interface SessionHooksJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event> {
215
- /**
216
- * Fires when an incoming token was verified and the session established.
217
- * May fire more than once per request when `useJWSSession` is called
218
- * concurrently in the same handler — dedupe via `session.id` if needed.
219
- */
220
- onRead?: (args: {
221
- session: SessionJWS<T, MaxAge> & {
222
- id: string;
223
- token: string;
224
- };
225
- event: TEvent;
226
- config: SessionConfigJWS<T, MaxAge, TEvent>;
227
- }) => void | Promise<void>;
228
- /** Fires after a new token was signed — including pure token refreshes (update with no data). */
229
- onUpdate?: (args: {
230
- /** Session after the update. */session: SessionJWS<T, MaxAge> & {
231
- id: string;
232
- token: string;
233
- }; /** Deep-copied snapshot of the session before the update. */
234
- oldSession: SessionJWS<T, MaxAge>;
235
- event: TEvent;
236
- config: SessionConfigJWS<T, MaxAge, TEvent>;
237
- }) => void | Promise<void>;
238
- /** Fires when the session is cleared. */
239
- onClear?: (args: {
240
- /** Last known session before clearing, if any. */oldSession: SessionJWS<T, MaxAge> | undefined;
241
- event: TEvent;
242
- config: SessionConfigJWS<T, MaxAge, TEvent>;
243
- }) => void | Promise<void>;
244
- /** Fires when the incoming token is expired. Receives the expired token's decoded claims. */
245
- onExpire?: (args: {
246
- session: {
247
- id: string | undefined;
248
- createdAt: number | undefined;
249
- expiresAt: number | undefined;
250
- token: string;
251
- };
252
- event: TEvent;
253
- error: Error;
254
- config: SessionConfigJWS<T, MaxAge, TEvent>;
255
- }) => void | Promise<void>;
256
- /** Fires on read-path failures other than expiry, and on write-path sign failures. */
257
- onError?: (args: {
258
- /**
259
- * The session involved in the error.
260
- */
261
- session: SessionJWS<T, MaxAge>;
262
- event: TEvent;
263
- error: any;
264
- config: SessionConfigJWS<T, MaxAge, TEvent>;
265
- }) => void | Promise<void>;
266
- /** Resolve the verification key(s) for the incoming token from its protected header (e.g. by `kid`). */
267
- onVerifyKeyLookup?: (args: {
268
- header: JWKLookupFunctionHeader;
269
- event: TEvent;
270
- config: SessionConfigJWS<T, MaxAge, TEvent>;
271
- }) => JWKSet | JWSVerifyJWK | Promise<JWKSet | JWSVerifyJWK>;
272
- }
273
- /**
274
- * Configuration for a JWS-backed (signed, not encrypted) session.
275
- */
276
- interface SessionConfigJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event> {
277
- /**
278
- * JWK used for signing — a symmetric `oct` (HS*) key, or an asymmetric pair:
279
- * the private key signs, the public side verifies (single JWK, array, or
280
- * JWKSet for rotation).
281
- */
282
- key: JWK_oct<JWK_HMAC> | {
283
- privateKey: JWSAsymmetricPrivateJWK;
284
- publicKey: JWSAsymmetricPublicJWK | JWSAsymmetricPublicJWK[] | JWKSet;
285
- };
286
- /**
287
- * Session lifetime in seconds — sets `exp = iat + maxAge` in the token and
288
- * drives the cookie expiry. Without it the library writes no `exp` — though
289
- * an `exp` key in the session data is then carried into the token as its
290
- * actual expiry claim.
291
- */
292
- maxAge?: MaxAge;
293
- /** Session name — drives the cookie name and the default session header. Default `"h3-jws"`. */
40
+ /** Seal the session as a compact JWE without writing a cookie; fresh `jti` if no ID, no token source needed. */
41
+ export declare function sealJWESession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<string>;
42
+ /** Decrypt a compact JWE into a session, checking `typ`, pinned `alg`/`enc`, `jti`/`iat` and `exp` (required with `maxAge`). */
43
+ export declare function unsealJWESession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>, sealed: string): Promise<Partial<SessionJWE<T, MaxAge>>>;
44
+ /** Drop the session cookie and leave an empty session for the rest of the request. */
45
+ export declare function clearJWESession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<void>;
46
+ /** Session state for the current request, mirroring the claims of its JWS token. */
47
+ interface SessionJWS<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> extends SessionState<T, MaxAge> {}
48
+ /** JWS session hooks; `onRead`, `onExpire` and `onError` are mutually exclusive per token. */
49
+ interface SessionHooksJWS<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event> extends JWSSessionHooks<SessionJWS<T, MaxAge>, SessionManager<T, MaxAge>, {
50
+ event: TEvent;
51
+ }, SessionConfigJWS<T, MaxAge, TEvent>> {}
52
+ /** Configuration for a JWS-backed (signed, not encrypted) session. */
53
+ interface SessionConfigJWS<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event> extends JWSSessionConfig<MaxAge, CookieSerializeOptions> {
54
+ /** Cookie and session-header name. Default `"h3-jws"`; a `.<digits>` suffix throws `ERR_SESSION_CONFIG_INVALID`. */
294
55
  name?: string;
295
- /** Cookie options (`false` to disable). Defaults: `path="/"`, `secure`, `httpOnly: false` — the JWS payload is client-readable by design. */
296
- cookie?: false | CookieSerializeOptions;
297
- /** Request header to read the token from (`false` to disable). Default `x-<name>-session`. */
298
- sessionHeader?: false | string;
299
- /** Session ID (`jti`) generator. Default `crypto.randomUUID`. */
300
- generateId?: () => string;
301
- /** JWS sign/verify overrides. Set `signOptions.alg` to pin the session algorithm — verification then accepts only it. */
302
- jws?: {
303
- signOptions?: Omit<JWSSignOptions, "expiresIn">;
304
- verifyOptions?: JWTClaimValidationOptions;
305
- };
306
- /** Lifecycle hooks — see {@link SessionHooksJWS}. */
307
56
  hooks?: SessionHooksJWS<T, MaxAge, TEvent>;
308
57
  }
309
- type CompatEvent = {
310
- request: {
311
- headers: Headers;
312
- };
313
- context: any;
314
- } | {
315
- headers: Headers;
316
- context: any;
317
- };
318
- /**
319
- * Create a session manager for the current request using JWS (signed only, not encrypted).
320
- * NOTE: Contents are visible to clients (Base64URL-decoded JSON). Do not store secrets in session data.
321
- */
322
- declare function useJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<SessionManager<T, MaxAge>>;
323
- /**
324
- * Get (and lazily initialize) the session for the current request.
325
- */
326
- declare function getJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<SessionJWS<T, MaxAge>>;
327
- /**
328
- * Read the raw session token for the request. The session header (default
329
- * `x-<name>-session`, `Bearer ` prefix stripped) takes precedence, then a
330
- * freshly-minted token on the response's `Set-Cookie`, then the session cookie.
331
- */
332
- declare function getJWSSessionToken<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): string | undefined;
333
- /**
334
- * Update the session data (if provided) and reissue the JWS token.
335
- */
336
- declare function updateJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>, update?: SessionUpdate<T>): Promise<SessionJWS<T, MaxAge> & {
58
+ /** Create a JWS (signed, not encrypted) session manager for the request; clients can read its data. */
59
+ export declare function useJWSSession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<SessionManager<T, MaxAge>>;
60
+ /** Get (and lazily initialize) the session for the current request. */
61
+ export declare function getJWSSession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<SessionJWS<T, MaxAge>>;
62
+ /** Read the raw session token: cookie first, then the session header (default `x-<name>-session`, optional `Bearer`). */
63
+ export declare function getJWSSessionToken<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): string | undefined;
64
+ /** Update the session data (if provided) and reissue the JWS; `options.maxAge` overrides this token's lifetime. */
65
+ export declare function updateJWSSession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>, update?: SessionUpdate<T>, options?: SessionIssueOptions): Promise<SessionJWS<T, MaxAge> & {
337
66
  id: string;
338
67
  token: string;
339
68
  }>;
340
- /**
341
- * Sign the current session as a compact JWS.
342
- * Payload: `session.data` spread at the top level, plus the reserved claims
343
- * `jti`, `iat`, and `exp` (when `maxAge` is set), which overwrite any
344
- * same-named keys in the session data.
345
- */
346
- declare function signJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<string>;
347
- /**
348
- * Verify and parse a compact JWS into a Session structure.
349
- * Performs:
350
- * - cryptographic signature verification
351
- * - claim validation (always on): `jti` & `iat` required, `exp` honoured
352
- * - `typ` header check and algorithm pinning when configured
353
- * - `maxAge` enforced as `maxTokenAge` when provided
354
- */
355
- declare function verifyJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>, token: string): Promise<Partial<SessionJWS<T, MaxAge>>>;
356
- /**
357
- * Clear the session (delete from context and drop cookie).
358
- */
359
- declare function clearJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<void>;
360
- export { type CompositeKey, type ContentEncryptionAlgorithm, type DeriveKeyOptions, type DeriveKeyReturn, type Duration, type ExpiresIn, type GenerateJWKOptions, type GenerateJWKReturn, type GenerateKeyAlgorithm, type GenerateKeyOptions, type GenerateKeyReturn, type JOSEPayload, type JWEAsymmetricPrivateJWK, type JWEAsymmetricPublicJWK, type JWEDecryptJWK, type JWEDecryptOptions, type JWEDecryptResult, type JWEEncryptJWK, type JWEEncryptOptions, type JWEFlattenedSerialization, type JWEGeneralRecipient, type JWEGeneralSerialization, type JWEHeaderParameters, type JWEKeyManagementHeaderParameters, type JWEMultiDecryptOptions, type JWEMultiDecryptResult, type JWEMultiEncryptOptions, type JWEMultiRecipient, type JWEProtectedHeader, type JWK, type JWKAlgorithm, type JWKCacheAdapter, type JWKLookupFunction, type JWKLookupFunctionHeader, type JWKPEMAlgorithm, type JWKParameters, type JWKSet, type JWK_AES_CBC_HMAC, type JWK_AES_GCM, type JWK_AES_GCM_KW, type JWK_AES_KW, type JWK_Asymmetric, type JWK_Asymmetric_Algorithm, type JWK_EC, type JWK_ECDH_ES, type JWK_ECDSA, type JWK_EC_Private, type JWK_EC_Public, type JWK_HMAC, type JWK_OKP, type JWK_OKP_Private, type JWK_OKP_Public, type JWK_OKP_SIGN, type JWK_PBES2, type JWK_Pair, type JWK_Private, type JWK_Public, type JWK_RSA, type JWK_RSA_ENC, type JWK_RSA_PSS, type JWK_RSA_Private, type JWK_RSA_Public, type JWK_RSA_SIGN, type JWK_Symmetric, type JWK_Symmetric_Algorithm, type JWK_oct, type JWSAlgorithm, type JWSAsymmetricPrivateJWK, type JWSAsymmetricPublicJWK, type JWSFlattenedSerialization, type JWSGeneralSerialization, type JWSGeneralSignature, type JWSHeaderParameters, type JWSMultiSignOptions, type JWSMultiSigner, type JWSMultiVerifyAllOptions, type JWSMultiVerifyOptions, type JWSMultiVerifyOutcome, type JWSMultiVerifyResult, type JWSProtectedHeader, type JWSSignJWK, type JWSSignOptions, type JWSVerifyJWK, type JWSVerifyOptions, type JWSVerifyResult, type JWT, type JWTClaimValidationOptions, type JWTClaims, type JoseHeaderParameters, type KeyManagementAlgorithm, type MaxTokenAge, type NotBeforeIn, type SessionClaims, type SessionConfigJWE, type SessionConfigJWS, type SessionData, type SessionHooksJWE, type SessionHooksJWS, type SessionJWE, type SessionJWS, type SessionManager, type SessionUpdate, type UnwrapKeyOptions, type UnwrappingKeyFor, type WrapKeyOptions, type WrapKeyResult, type WrappingKeyFor, clearJWESession, clearJWSSession, deriveJWKFromPassword, exportPEM, exportToPEM, generateJWK, getJWESession, getJWESessionToken, getJWSSession, getJWSSessionToken, importFromPEM, importPEM, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
69
+ /** Sign the session as a compact JWS without writing a cookie; fresh `jti` if no ID, no token source needed. */
70
+ export declare function signJWSSession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<string>;
71
+ /** Verify a compact JWS into a session, checking `typ`, pinned `alg`, `jti`/`iat` and `exp` (required with `maxAge`). */
72
+ export declare function verifyJWSSession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>, token: string): Promise<Partial<SessionJWS<T, MaxAge>>>;
73
+ /** Drop the session cookie and leave an empty session for the rest of the request. */
74
+ export declare function clearJWSSession<T extends SessionRecord = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<void>;
75
+ export { type CompositeKey, type ContentEncryptionAlgorithm, type DeriveKeyOptions, type DeriveKeyReturn, type Duration, type ECDHEphemeralKeyInput, type ExpiresIn, type GenerateJWKOptions, type GenerateJWKReturn, type GenerateKeyAlgorithm, type GenerateKeyOptions, type GenerateKeyReturn, type GeneratedJWKMap, type GeneratedKeyMap, type ImportedKey, type ImportedKeyMap, type JOSEConsumerOptions, type JOSELimits, type JOSEPayload, type JOSEPayloadMode, type JOSEPayloadTypeMap, type JWEAsymmetricPrivateJWK, type JWEAsymmetricPublicJWK, type JWEDecryptJWK, type JWEDecryptOptions, type JWEDecryptResult, type JWEEncryptJWK, type JWEEncryptOptions, type JWEFlattenedSerialization, type JWEGeneralRecipient, type JWEGeneralSerialization, type JWEHeaderParameters, type JWEKeyManagementHeaderParameters, type JWELibraryHeaderName, type JWEMultiDecryptKey, type JWEMultiDecryptOptions, type JWEMultiDecryptResult, type JWEMultiEncryptOptions, type JWEMultiRecipient, type JWEProtectedHeader, type JWK, type JWKAlgorithm, type JWKCacheAdapter, type JWKLookupFunction, type JWKLookupFunctionHeader, type JWKMetadata, type JWKPEMAlgorithm, type JWKParameters, type JWKSet, type JWK_AES_CBC_HMAC, type JWK_AES_GCM, type JWK_AES_GCM_KW, type JWK_AES_KW, type JWK_Asymmetric, type JWK_Asymmetric_Algorithm, type JWK_EC, type JWK_ECDH_ES, type JWK_ECDSA, type JWK_EC_Private, type JWK_EC_Public, type JWK_HMAC, type JWK_OKP, type JWK_OKP_Private, type JWK_OKP_Public, type JWK_OKP_SIGN, type JWK_PBES2, type JWK_Pair, type JWK_Private, type JWK_Public, type JWK_RSA, type JWK_RSA_ENC, type JWK_RSA_PSS, type JWK_RSA_Private, type JWK_RSA_Public, type JWK_RSA_SIGN, type JWK_Symmetric, type JWK_Symmetric_Algorithm, type JWK_oct, type JWSAlgorithm, type JWSAsymmetricPrivateJWK, type JWSAsymmetricPublicJWK, type JWSFlattenedSerialization, type JWSGeneralSerialization, type JWSGeneralSignature, type JWSHeaderParameters, type JWSMultiSignOptions, type JWSMultiSigner, type JWSMultiVerifyAllOptions, type JWSMultiVerifyOptions, type JWSMultiVerifyOutcome, type JWSMultiVerifyResult, type JWSProtectedHeader, type JWSSignJWK, type JWSSignOptions, type JWSVerifyJWK, type JWSVerifyOptions, type JWSVerifyResult, type JWT, type JWTClaimValidationOptions, type JWTClaims, type JWTTimeClaimOptions, type JoseHeaderParameters, type KeyManagementAlgorithm, type MaxTokenAge, type NotBeforeIn, type PayloadOf, type SessionClaims, type SessionConfigJWE, type SessionConfigJWS, type SessionData, type SessionDecryptOptions, type SessionEncryptOptions, type SessionHooksJWE, type SessionHooksJWS, type SessionIssueOptions, type SessionJWE, type SessionJWS, type SessionManager, type SessionSignOptions, type SessionUpdate, type SessionUpdateTrigger, type SessionVerifyOptions, type UnwrapKeyOptions, type UnwrapKeyResult, type UnwrappingKeyFor, type WrapKeyOptions, type WrapKeyResult, type WrappingKeyFor, deriveJWKFromPassword, exportPEM, generateJWK, importPEM, thumbprint };