unjwt 0.5.38 → 0.5.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +148 -670
  2. package/dist/_chunks/chunk.mjs +13 -0
  3. package/dist/_chunks/index.d.mts +558 -0
  4. package/dist/_chunks/index2.d.mts +108 -0
  5. package/dist/_chunks/jwe.d.mts +43 -0
  6. package/dist/_chunks/jwe.mjs +132 -0
  7. package/dist/_chunks/jwk.d.mts +611 -0
  8. package/dist/_chunks/jwk.mjs +1592 -0
  9. package/dist/_chunks/jws.d.mts +242 -0
  10. package/dist/_chunks/jws.mjs +2070 -0
  11. package/dist/_chunks/jws2.mjs +598 -0
  12. package/dist/_chunks/utils.mjs +375 -0
  13. package/dist/adapters/h3.d.mts +3 -267
  14. package/dist/adapters/h3.mjs +3 -704
  15. package/dist/adapters/h3v1.d.mts +3 -8
  16. package/dist/adapters/h3v1.mjs +3 -7
  17. package/dist/adapters/h3v2.d.mts +145 -147
  18. package/dist/adapters/h3v2.mjs +501 -594
  19. package/dist/index.d.mts +6 -8
  20. package/dist/index.mjs +5 -4
  21. package/dist/jwe.d.mts +3 -4
  22. package/dist/jwe.mjs +2 -3
  23. package/dist/jwk.d.mts +3 -2
  24. package/dist/jwk.mjs +2 -2
  25. package/dist/jws.d.mts +3 -3
  26. package/dist/jws.mjs +2 -3
  27. package/dist/utils.d.mts +3 -3
  28. package/dist/utils.mjs +2 -1
  29. package/package.json +33 -31
  30. package/dist/shared/unjwt.4JQCoID_.d.mts +0 -161
  31. package/dist/shared/unjwt.B-v62plj.mjs +0 -249
  32. package/dist/shared/unjwt.BCi3FEWr.d.mts +0 -328
  33. package/dist/shared/unjwt.BYDTEWa-.d.mts +0 -269
  34. package/dist/shared/unjwt.Be8NxwFK.mjs +0 -438
  35. package/dist/shared/unjwt.BqdNUKDr.d.mts +0 -64
  36. package/dist/shared/unjwt.CMNtw8ek.d.mts +0 -155
  37. package/dist/shared/unjwt.Cy2MlkUe.d.mts +0 -174
  38. package/dist/shared/unjwt.D0_ccT7g.d.mts +0 -166
  39. package/dist/shared/unjwt.DdLN9vwI.mjs +0 -163
  40. package/dist/shared/unjwt.Dpl__ZAR.mjs +0 -2121
  41. package/dist/shared/unjwt.JdKqVANs.d.mts +0 -5
@@ -0,0 +1,242 @@
1
+ import { C as JWTClaims, R as JWKSet, S as JWTClaimValidationOptions, _ as JWSProtectedHeader, a as ExpiresIn, dt as JWK_Symmetric, it as JWK_Public, l as JWEEncryptOptions, p as StrictOmit, rt as JWK_Private, u as JWEHeaderParameters, v as JWSSignOptions } from "./jwk.mjs";
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
+ }
91
+ type CompatEvent$1 = {
92
+ request: {
93
+ headers: Headers;
94
+ };
95
+ context: any;
96
+ } | {
97
+ headers: Headers;
98
+ context: any;
99
+ };
100
+ /**
101
+ * Create a session manager for the current request.
102
+ */
103
+ declare function useJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>): Promise<SessionManager<T, MaxAge>>;
104
+ /**
105
+ * Get (and lazily initialize) the session for the current request.
106
+ */
107
+ declare function getJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>): Promise<SessionJWE<T, MaxAge>>;
108
+ declare function getJWESessionToken<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>): string | undefined;
109
+ /**
110
+ * Update the session (optionally mutating the session data) and re-issue the JWE token.
111
+ */
112
+ declare function updateJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event, config: SessionConfigJWE<T, MaxAge>, update?: SessionUpdate<T>): Promise<SessionJWE<T, MaxAge>>;
113
+ /**
114
+ * Produce a JWE for the current session.
115
+ * Payload structure:
116
+ * {
117
+ * jti: string;
118
+ * iat: number; (seconds)
119
+ * exp?: number; (seconds)
120
+ * data: Record<string, any>;
121
+ * }
122
+ */
123
+ declare function sealJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>): Promise<string>;
124
+ /**
125
+ * Decrypt the JWE and return a Session-compatible object.
126
+ */
127
+ declare function unsealJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>, sealed: string): Promise<Partial<SessionJWE<T, MaxAge>>>;
128
+ /**
129
+ * Clear the session (delete from context and drop cookie).
130
+ */
131
+ declare function clearJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event, config: Partial<SessionConfigJWE<T, MaxAge>>): Promise<void>;
132
+ //#endregion
133
+ //#region src/adapters/h3v1/session/jws.d.ts
134
+ declare const kGetSessionPromise: unique symbol;
135
+ interface SessionJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
136
+ id: string | undefined;
137
+ createdAt: number;
138
+ expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
139
+ data: SessionData<T>;
140
+ [kGetSessionPromise]?: Promise<SessionJWS<T, MaxAge>>;
141
+ }
142
+ interface SessionHooksJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
143
+ onRead?: (args: {
144
+ session: SessionJWS<T, MaxAge>;
145
+ event: H3Event;
146
+ config: SessionConfigJWS<T, MaxAge>;
147
+ }) => void | Promise<void>;
148
+ onUpdate?: (args: {
149
+ session: SessionJWS<T, MaxAge>;
150
+ oldSession: SessionJWS<T, MaxAge>;
151
+ event: H3Event;
152
+ config: SessionConfigJWS<T, MaxAge>;
153
+ }) => void | Promise<void>;
154
+ onClear?: (args: {
155
+ session: SessionJWS<T, MaxAge> | undefined;
156
+ event: H3Event;
157
+ config: Partial<SessionConfigJWS<T, MaxAge>>;
158
+ }) => void | Promise<void>;
159
+ onExpire?: (args: {
160
+ event: H3Event;
161
+ error: Error;
162
+ config: SessionConfigJWS<T, MaxAge>;
163
+ }) => void | Promise<void>;
164
+ onError?: (args: {
165
+ event: H3Event;
166
+ error: any;
167
+ config: SessionConfigJWS<T, MaxAge>;
168
+ }) => void | Promise<void>;
169
+ onVerifyKeyLookup?: (args: {
170
+ header: JWSProtectedHeader;
171
+ event: H3Event;
172
+ config: SessionConfigJWS<T, MaxAge>;
173
+ }) => JWKSet | JWK_Symmetric | JWK_Public | Promise<JWKSet | JWK_Symmetric | JWK_Public>;
174
+ }
175
+ interface SessionConfigJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
176
+ /**
177
+ * JWK (private for signing with RS/ES/PS, or symmetric oct) used for signing.
178
+ */
179
+ key: JWK_Symmetric | {
180
+ privateKey: JWK_Private;
181
+ publicKey: JWK_Public | JWK_Public[] | JWKSet;
182
+ };
183
+ /** Session lifetime in seconds (sets exp = iat + maxAge) */
184
+ maxAge?: MaxAge;
185
+ /** Default cookie / header name base */
186
+ name?: string;
187
+ /** Cookie options (false to disable cookies) */
188
+ cookie?: false | CookieSerializeOptions;
189
+ /** Custom header (false to disable header lookup) */
190
+ sessionHeader?: false | string;
191
+ /** Custom ID generator (defaults to crypto.randomUUID) */
192
+ generateId?: () => string;
193
+ /** JWS customization */
194
+ jws?: {
195
+ signOptions?: Omit<JWSSignOptions, "expiresIn">;
196
+ verifyOptions?: JWTClaimValidationOptions;
197
+ };
198
+ hooks?: SessionHooksJWS<T, MaxAge>;
199
+ }
200
+ type CompatEvent = {
201
+ request: {
202
+ headers: Headers;
203
+ };
204
+ context: any;
205
+ } | {
206
+ headers: Headers;
207
+ context: any;
208
+ };
209
+ /**
210
+ * Create a session manager for the current request using JWS (signed only, not encrypted).
211
+ * NOTE: Contents are visible to clients (Base64URL-decoded JSON). Do not store secrets in session data.
212
+ */
213
+ declare function useJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent, config: SessionConfigJWS<T, MaxAge>): Promise<SessionManager<T, MaxAge>>;
214
+ /**
215
+ * Retrieve (and lazily initialize) the session.
216
+ */
217
+ declare function getJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent, config: SessionConfigJWS<T, MaxAge>): Promise<SessionJWS<T, MaxAge>>;
218
+ /**
219
+ * Update session data (if provided) and reissue JWS.
220
+ */
221
+ declare function updateJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event, config: SessionConfigJWS<T, MaxAge>, update?: SessionUpdate<T>): Promise<SessionJWS<T, MaxAge>>;
222
+ /**
223
+ * Sign current session as a compact JWS.
224
+ * Payload claims:
225
+ * jti, iat, exp?, data, plus optional extraClaims (cannot override reserved)
226
+ */
227
+ declare function signJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent, config: SessionConfigJWS<T, MaxAge>): Promise<string>;
228
+ /**
229
+ * Verify and parse a compact JWS into a Session structure.
230
+ * Performs:
231
+ * - cryptographic signature verification
232
+ * - (optional) standard claim checks if validateJWT true
233
+ * - ensures jti & iat presence
234
+ * - enforces maxAge if provided
235
+ */
236
+ declare function verifyJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent, config: SessionConfigJWS<T, MaxAge>, token: string): Promise<Partial<SessionJWS<T, MaxAge>>>;
237
+ /**
238
+ * Destroy the session (context + cookie).
239
+ */
240
+ declare function clearJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event, config: Partial<SessionConfigJWS<T, MaxAge>>): Promise<void>;
241
+ //#endregion
242
+ export { SessionUpdate as C, SessionManager as S, unsealJWESession as _, getJWSSession as a, SessionClaims as b, useJWSSession as c, SessionHooksJWE as d, SessionJWE as f, sealJWESession as g, getJWESessionToken as h, clearJWSSession as i, verifyJWSSession as l, getJWESession as m, SessionHooksJWS as n, signJWSSession as o, clearJWESession as p, SessionJWS as r, updateJWSSession as s, SessionConfigJWS as t, SessionConfigJWE as u, updateJWESession as v, SessionData as x, useJWESession as y };