unjwt 0.7.1 → 0.7.3
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/dist/_chunks/_internal.mjs +5 -5
- package/dist/_chunks/index.d.mts +42 -17
- package/dist/_chunks/jwe.d.mts +2 -2
- package/dist/_chunks/jwe.mjs +5 -5
- package/dist/_chunks/jwk.mjs +51 -35
- package/dist/_chunks/jws.d.mts +2 -2
- package/dist/_chunks/jws.mjs +6 -6
- package/dist/_chunks/libs/unsecure.d.mts +1 -1
- package/dist/_chunks/libs/unsecure.mjs +3 -3
- package/dist/_chunks/utils.mjs +8 -6
- package/dist/adapters/elysia/index.d.mts +244 -0
- package/dist/adapters/elysia/index.mjs +608 -0
- package/dist/adapters/h3v1/index.d.mts +120 -42
- package/dist/adapters/h3v1/index.mjs +18 -15
- package/dist/adapters/h3v2/index.d.mts +140 -20
- package/dist/adapters/h3v2/index.mjs +18 -15
- package/dist/core/jwe.d.mts +3 -3
- package/dist/core/jwe.mjs +2 -2
- package/dist/core/jwk.d.mts +10 -10
- package/dist/core/jwk.mjs +2 -2
- package/dist/core/jws.d.mts +3 -3
- package/dist/core/jws.mjs +2 -2
- package/dist/core/utils/index.d.mts +24 -8
- package/dist/core/utils/index.mjs +2 -2
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +4 -4
- package/package.json +34 -21
|
@@ -0,0 +1,244 @@
|
|
|
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, generateJWK, importPEM } from "../../core/jwk.mjs";
|
|
3
|
+
import { Elysia } from "elysia";
|
|
4
|
+
interface CookieAttributes {
|
|
5
|
+
domain?: string;
|
|
6
|
+
expires?: Date;
|
|
7
|
+
httpOnly?: boolean;
|
|
8
|
+
maxAge?: number;
|
|
9
|
+
path?: string;
|
|
10
|
+
priority?: "low" | "medium" | "high";
|
|
11
|
+
sameSite?: boolean | "lax" | "strict" | "none";
|
|
12
|
+
secure?: boolean;
|
|
13
|
+
partitioned?: boolean;
|
|
14
|
+
}
|
|
15
|
+
interface ChunkableCookie {
|
|
16
|
+
value: unknown;
|
|
17
|
+
set(config: CookieAttributes & {
|
|
18
|
+
value: string;
|
|
19
|
+
}): unknown;
|
|
20
|
+
}
|
|
21
|
+
type ChunkableCookieJar = Record<string, ChunkableCookie>;
|
|
22
|
+
type WriteChunkedCookieOptions = CookieAttributes & {
|
|
23
|
+
chunkMaxLength?: number;
|
|
24
|
+
};
|
|
25
|
+
interface SessionContext {
|
|
26
|
+
cookie: ChunkableCookieJar;
|
|
27
|
+
request: {
|
|
28
|
+
headers: {
|
|
29
|
+
get(name: string): string | null;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
interface SessionClaims extends Required<Pick<JWTClaims, "jti" | "iat">>, StrictOmit<JWTClaims, "jti" | "iat"> {}
|
|
34
|
+
type SessionData<T extends Record<string, any> = SessionClaims> = StrictOmit<T, "jti" | "iat" | "exp">;
|
|
35
|
+
type SessionUpdate<T extends Record<string, any> = SessionClaims> = Partial<SessionData<T>> | ((oldData: SessionData<T>) => Partial<SessionData<T>> | undefined);
|
|
36
|
+
interface SessionManager<T extends Record<string, any> = SessionClaims, ConfigMaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
|
|
37
|
+
readonly id: string | undefined;
|
|
38
|
+
readonly createdAt: number;
|
|
39
|
+
readonly expiresAt: ConfigMaxAge extends ExpiresIn ? number : "exp" extends keyof T ? T["exp"] : number | undefined;
|
|
40
|
+
readonly data: SessionData<T>;
|
|
41
|
+
readonly token: string | undefined;
|
|
42
|
+
update: (update?: SessionUpdate<T>) => Promise<SessionManager<T, ConfigMaxAge>>;
|
|
43
|
+
clear: () => Promise<SessionManager<T, ConfigMaxAge>>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Explicit return type for the `jwsSession` / `jweSession` plugin factories.
|
|
47
|
+
*
|
|
48
|
+
* Modelling Elysia's generics by hand is required by `isolatedDeclarations`
|
|
49
|
+
* (the factories return an inferred `new Elysia().resolve().macro()` type that
|
|
50
|
+
* cannot otherwise be emitted). `Resolved` is the scoped `Ephemeral.resolve`
|
|
51
|
+
* augmentation — typically `{ [P in ContextKey]: SessionManager }` — so consumers
|
|
52
|
+
* read a typed `ctx[contextKey]`. `GuardName` is the guard macro key (derived
|
|
53
|
+
* from the context key, e.g. `requireSession`), so multiple session plugins on
|
|
54
|
+
* one app each expose a distinct guard rather than colliding. `macroFn` is left
|
|
55
|
+
* `{}`: consumers only need `Metadata.macro` for the `{ [GuardName]: true }`
|
|
56
|
+
* route option.
|
|
57
|
+
*/
|
|
58
|
+
type SessionPlugin<Resolved extends Record<string, unknown>, GuardName extends string = "requireSession"> = Elysia<"", {
|
|
59
|
+
decorator: {};
|
|
60
|
+
store: {};
|
|
61
|
+
derive: {};
|
|
62
|
+
resolve: {};
|
|
63
|
+
}, {
|
|
64
|
+
typebox: {};
|
|
65
|
+
error: {};
|
|
66
|
+
}, {
|
|
67
|
+
schema: {};
|
|
68
|
+
standaloneSchema: {};
|
|
69
|
+
macro: { [M in GuardName]?: boolean };
|
|
70
|
+
macroFn: {};
|
|
71
|
+
parser: {};
|
|
72
|
+
response: {};
|
|
73
|
+
}, {}, {
|
|
74
|
+
derive: {};
|
|
75
|
+
resolve: Resolved;
|
|
76
|
+
schema: {};
|
|
77
|
+
standaloneSchema: {};
|
|
78
|
+
response: {};
|
|
79
|
+
}, {
|
|
80
|
+
derive: {};
|
|
81
|
+
resolve: {};
|
|
82
|
+
schema: {};
|
|
83
|
+
standaloneSchema: {};
|
|
84
|
+
response: {};
|
|
85
|
+
}>;
|
|
86
|
+
interface SessionJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
|
|
87
|
+
id: string | undefined;
|
|
88
|
+
createdAt: number;
|
|
89
|
+
expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
|
|
90
|
+
data: SessionData<T>;
|
|
91
|
+
token: string | undefined;
|
|
92
|
+
}
|
|
93
|
+
interface SessionHooksJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TContext extends SessionContext = SessionContext> {
|
|
94
|
+
onRead?: (args: {
|
|
95
|
+
session: SessionJWS<T, MaxAge> & {
|
|
96
|
+
id: string;
|
|
97
|
+
token: string;
|
|
98
|
+
};
|
|
99
|
+
context: TContext;
|
|
100
|
+
config: SessionConfigJWS<T, MaxAge, TContext>;
|
|
101
|
+
}) => void | Promise<void>;
|
|
102
|
+
onUpdate?: (args: {
|
|
103
|
+
session: SessionJWS<T, MaxAge> & {
|
|
104
|
+
id: string;
|
|
105
|
+
token: string;
|
|
106
|
+
};
|
|
107
|
+
oldSession: SessionJWS<T, MaxAge>;
|
|
108
|
+
context: TContext;
|
|
109
|
+
config: SessionConfigJWS<T, MaxAge, TContext>;
|
|
110
|
+
}) => void | Promise<void>;
|
|
111
|
+
onClear?: (args: {
|
|
112
|
+
oldSession: SessionJWS<T, MaxAge> | undefined;
|
|
113
|
+
context: TContext;
|
|
114
|
+
config: SessionConfigJWS<T, MaxAge, TContext>;
|
|
115
|
+
}) => void | Promise<void>;
|
|
116
|
+
onExpire?: (args: {
|
|
117
|
+
session: {
|
|
118
|
+
id: string | undefined;
|
|
119
|
+
createdAt: number | undefined;
|
|
120
|
+
expiresAt: number | undefined;
|
|
121
|
+
token: string;
|
|
122
|
+
};
|
|
123
|
+
context: TContext;
|
|
124
|
+
error: Error;
|
|
125
|
+
config: SessionConfigJWS<T, MaxAge, TContext>;
|
|
126
|
+
}) => void | Promise<void>;
|
|
127
|
+
onError?: (args: {
|
|
128
|
+
session: SessionJWS<T, MaxAge>;
|
|
129
|
+
context: TContext;
|
|
130
|
+
error: any;
|
|
131
|
+
config: SessionConfigJWS<T, MaxAge, TContext>;
|
|
132
|
+
}) => void | Promise<void>;
|
|
133
|
+
onVerifyKeyLookup?: (args: {
|
|
134
|
+
header: JWKLookupFunctionHeader;
|
|
135
|
+
context: TContext;
|
|
136
|
+
config: SessionConfigJWS<T, MaxAge, TContext>;
|
|
137
|
+
}) => JWKSet | JWSVerifyJWK | Promise<JWKSet | JWSVerifyJWK>;
|
|
138
|
+
}
|
|
139
|
+
interface SessionConfigJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TContext extends SessionContext = SessionContext> {
|
|
140
|
+
key: JWK_oct<JWK_HMAC> | {
|
|
141
|
+
privateKey: JWSAsymmetricPrivateJWK;
|
|
142
|
+
publicKey: JWSAsymmetricPublicJWK | JWSAsymmetricPublicJWK[] | JWKSet;
|
|
143
|
+
};
|
|
144
|
+
maxAge?: MaxAge;
|
|
145
|
+
name?: string;
|
|
146
|
+
/** Context property the session is exposed under. Default `"session"`. */
|
|
147
|
+
contextKey?: string;
|
|
148
|
+
cookie?: false | (CookieAttributes & {
|
|
149
|
+
chunkMaxLength?: number;
|
|
150
|
+
});
|
|
151
|
+
sessionHeader?: false | string;
|
|
152
|
+
generateId?: () => string;
|
|
153
|
+
jws?: {
|
|
154
|
+
signOptions?: Omit<JWSSignOptions, "expiresIn">;
|
|
155
|
+
verifyOptions?: JWTClaimValidationOptions;
|
|
156
|
+
};
|
|
157
|
+
hooks?: SessionHooksJWS<T, MaxAge, TContext>;
|
|
158
|
+
}
|
|
159
|
+
declare function createJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TContext extends SessionContext = SessionContext>(context: TContext, config: SessionConfigJWS<T, MaxAge, TContext>): Promise<SessionManager<T, MaxAge>>;
|
|
160
|
+
declare function jwsSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, K extends string = "session">(config: SessionConfigJWS<T, MaxAge> & (K extends "session" ? {
|
|
161
|
+
contextKey?: K;
|
|
162
|
+
} : {
|
|
163
|
+
contextKey: K;
|
|
164
|
+
})): SessionPlugin<{ [P in K]: SessionManager<T, MaxAge> }, `require${Capitalize<K>}`>;
|
|
165
|
+
interface SessionJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
|
|
166
|
+
id: string | undefined;
|
|
167
|
+
createdAt: number;
|
|
168
|
+
expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
|
|
169
|
+
data: SessionData<T>;
|
|
170
|
+
token: string | undefined;
|
|
171
|
+
}
|
|
172
|
+
interface SessionHooksJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TContext extends SessionContext = SessionContext> {
|
|
173
|
+
onRead?: (args: {
|
|
174
|
+
session: SessionJWE<T, MaxAge> & {
|
|
175
|
+
id: string;
|
|
176
|
+
token: string;
|
|
177
|
+
};
|
|
178
|
+
context: TContext;
|
|
179
|
+
config: SessionConfigJWE<T, MaxAge, TContext>;
|
|
180
|
+
}) => void | Promise<void>;
|
|
181
|
+
onUpdate?: (args: {
|
|
182
|
+
session: SessionJWE<T, MaxAge> & {
|
|
183
|
+
id: string;
|
|
184
|
+
token: string;
|
|
185
|
+
};
|
|
186
|
+
oldSession: SessionJWE<T, MaxAge>;
|
|
187
|
+
context: TContext;
|
|
188
|
+
config: SessionConfigJWE<T, MaxAge, TContext>;
|
|
189
|
+
}) => void | Promise<void>;
|
|
190
|
+
onClear?: (args: {
|
|
191
|
+
oldSession: SessionJWE<T, MaxAge> | undefined;
|
|
192
|
+
context: TContext;
|
|
193
|
+
config: SessionConfigJWE<T, MaxAge, TContext>;
|
|
194
|
+
}) => void | Promise<void>;
|
|
195
|
+
onExpire?: (args: {
|
|
196
|
+
session: {
|
|
197
|
+
id: string | undefined;
|
|
198
|
+
createdAt: number | undefined;
|
|
199
|
+
expiresAt: number | undefined;
|
|
200
|
+
token: string;
|
|
201
|
+
};
|
|
202
|
+
context: TContext;
|
|
203
|
+
error: Error;
|
|
204
|
+
config: SessionConfigJWE<T, MaxAge, TContext>;
|
|
205
|
+
}) => void | Promise<void>;
|
|
206
|
+
onError?: (args: {
|
|
207
|
+
session: SessionJWE<T, MaxAge>;
|
|
208
|
+
context: TContext;
|
|
209
|
+
error: any;
|
|
210
|
+
config: SessionConfigJWE<T, MaxAge, TContext>;
|
|
211
|
+
}) => void | Promise<void>;
|
|
212
|
+
onUnsealKeyLookup?: (args: {
|
|
213
|
+
header: JWEHeaderParameters;
|
|
214
|
+
context: TContext;
|
|
215
|
+
config: SessionConfigJWE<T, MaxAge, TContext>;
|
|
216
|
+
}) => JWEDecryptJWK | Promise<JWEDecryptJWK>;
|
|
217
|
+
}
|
|
218
|
+
interface SessionConfigJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TContext extends SessionContext = SessionContext> {
|
|
219
|
+
key: string | JWEEncryptJWK | {
|
|
220
|
+
privateKey: JWEAsymmetricPrivateJWK;
|
|
221
|
+
publicKey?: JWEAsymmetricPublicJWK;
|
|
222
|
+
};
|
|
223
|
+
maxAge?: MaxAge;
|
|
224
|
+
name?: string;
|
|
225
|
+
/** Context property the session is exposed under. Default `"session"`. */
|
|
226
|
+
contextKey?: string;
|
|
227
|
+
cookie?: false | (CookieAttributes & {
|
|
228
|
+
chunkMaxLength?: number;
|
|
229
|
+
});
|
|
230
|
+
sessionHeader?: false | string;
|
|
231
|
+
generateId?: () => string;
|
|
232
|
+
jwe?: {
|
|
233
|
+
encryptOptions?: Omit<JWEEncryptOptions, "expiresIn">;
|
|
234
|
+
decryptOptions?: JWTClaimValidationOptions;
|
|
235
|
+
};
|
|
236
|
+
hooks?: SessionHooksJWE<T, MaxAge, TContext>;
|
|
237
|
+
}
|
|
238
|
+
declare function createJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TContext extends SessionContext = SessionContext>(context: TContext, config: SessionConfigJWE<T, MaxAge, TContext>): Promise<SessionManager<T, MaxAge>>;
|
|
239
|
+
declare function jweSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, K extends string = "session">(config: SessionConfigJWE<T, MaxAge> & (K extends "session" ? {
|
|
240
|
+
contextKey?: K;
|
|
241
|
+
} : {
|
|
242
|
+
contextKey: K;
|
|
243
|
+
})): SessionPlugin<{ [P in K]: SessionManager<T, MaxAge> }, `require${Capitalize<K>}`>;
|
|
244
|
+
export { type ChunkableCookie, type ChunkableCookieJar, type CompositeKey, type ContentEncryptionAlgorithm, type CookieAttributes, 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 SessionContext, type SessionData, type SessionHooksJWE, type SessionHooksJWS, type SessionJWE, type SessionJWS, type SessionManager, type SessionPlugin, type SessionUpdate, type UnwrapKeyOptions, type UnwrappingKeyFor, type WrapKeyOptions, type WrapKeyResult, type WrappingKeyFor, type WriteChunkedCookieOptions, createJWESession, createJWSSession, deriveJWKFromPassword, exportPEM, generateJWK, importPEM, jweSession, jwsSession };
|