@crossauth/sveltekit 0.0.2
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/LICENSE +203 -0
- package/README.md +12 -0
- package/dist/index.cjs +7018 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +7018 -0
- package/dist/sveltekitadminclientendpoints.d.ts +150 -0
- package/dist/sveltekitadminendpoints.d.ts +369 -0
- package/dist/sveltekitapikey.d.ts +48 -0
- package/dist/sveltekitoauthclient.d.ts +704 -0
- package/dist/sveltekitoauthserver.d.ts +406 -0
- package/dist/sveltekitresserver.d.ts +101 -0
- package/dist/sveltekitserver.d.ts +286 -0
- package/dist/sveltekitsession.d.ts +629 -0
- package/dist/sveltekitsessionadapter.d.ts +48 -0
- package/dist/sveltekitsharedclientendpoints.d.ts +289 -0
- package/dist/sveltekituserclientendpoints.d.ts +151 -0
- package/dist/sveltekituserendpoints.d.ts +730 -0
- package/dist/tests/sveltekitadminclientendpoints.test.d.ts +6 -0
- package/dist/tests/sveltekitadminendpoints.test.d.ts +6 -0
- package/dist/tests/sveltekitapikeyserver.test.d.ts +1 -0
- package/dist/tests/sveltekitoauthclient.test.d.ts +11 -0
- package/dist/tests/sveltekitoauthresserver.test.d.ts +10 -0
- package/dist/tests/sveltekitoauthserver.test.d.ts +6 -0
- package/dist/tests/sveltekituserclientendpoints.test.d.ts +6 -0
- package/dist/tests/sveltekituserendpoints.test.d.ts +6 -0
- package/dist/tests/sveltemock.test.d.ts +1 -0
- package/dist/tests/sveltemocks.d.ts +77 -0
- package/dist/tests/sveltesessionhooks.test.d.ts +1 -0
- package/dist/tests/testshared.d.ts +79 -0
- package/dist/utils.d.ts +38 -0
- package/package.json +52 -0
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
import { KeyStorage, UserStorage, OAuthClientStorage, SessionManager, Authenticator, Cookie, SessionManagerOptions } from '@crossauth/backend';
|
|
2
|
+
import { Key, User, UserInputFields, OAuthClient } from '@crossauth/common';
|
|
3
|
+
import { RequestEvent, MaybePromise } from '@sveltejs/kit';
|
|
4
|
+
import { SvelteKitUserEndpoints } from './sveltekituserendpoints';
|
|
5
|
+
import { SvelteKitAdminEndpoints } from './sveltekitadminendpoints';
|
|
6
|
+
import { SvelteKitUserClientEndpoints } from './sveltekituserclientendpoints';
|
|
7
|
+
import { SvelteKitAdminClientEndpoints } from './sveltekitadminclientendpoints';
|
|
8
|
+
import { SvelteKitSessionAdapter } from './sveltekitsessionadapter';
|
|
9
|
+
|
|
10
|
+
export declare const CSRFHEADER = "X-CROSSAUTH-CSRF";
|
|
11
|
+
type Header = {
|
|
12
|
+
name: string;
|
|
13
|
+
value: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Options for {@link SvelteKitSessionServer}.
|
|
17
|
+
*/
|
|
18
|
+
export interface SvelteKitSessionServerOptions extends SessionManagerOptions {
|
|
19
|
+
/**
|
|
20
|
+
* If enabling user login, must provide the user storage
|
|
21
|
+
*/
|
|
22
|
+
userStorage?: UserStorage;
|
|
23
|
+
/**
|
|
24
|
+
* If enabling client endpoints, must provide the client storage
|
|
25
|
+
*/
|
|
26
|
+
clientStorage?: OAuthClientStorage;
|
|
27
|
+
/**
|
|
28
|
+
* Factor 1 and 2 authenticators.
|
|
29
|
+
*
|
|
30
|
+
* The key is what appears in the `factor1` and `factor2` filed in the
|
|
31
|
+
* Users table.
|
|
32
|
+
*/
|
|
33
|
+
authenticators?: {
|
|
34
|
+
[key: string]: Authenticator;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* URL to call when factor2 authentication is required
|
|
38
|
+
*/
|
|
39
|
+
factor2Url?: string;
|
|
40
|
+
/**
|
|
41
|
+
* URL to call when login is required.
|
|
42
|
+
*
|
|
43
|
+
* Default "/"
|
|
44
|
+
*/
|
|
45
|
+
loginUrl?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Default URL to go to after login (can be overridden by `next` POST param)
|
|
48
|
+
*
|
|
49
|
+
* Default "/"
|
|
50
|
+
*/
|
|
51
|
+
loginRedirectUrl?: string;
|
|
52
|
+
/**
|
|
53
|
+
* URL to call when change password is required.
|
|
54
|
+
*
|
|
55
|
+
* Default "/changepassword"
|
|
56
|
+
*/
|
|
57
|
+
changePasswordUrl?: string;
|
|
58
|
+
/**
|
|
59
|
+
* URL to call when change password is required.
|
|
60
|
+
*
|
|
61
|
+
* Default "/resetpassword"
|
|
62
|
+
*/
|
|
63
|
+
requestPasswordResetUrl?: string;
|
|
64
|
+
/**
|
|
65
|
+
* URL to call when change factor2 is required.
|
|
66
|
+
*
|
|
67
|
+
* Default "/changefactor2"
|
|
68
|
+
*/
|
|
69
|
+
changeFactor2Url?: string;
|
|
70
|
+
/** OAuth to support. A comma-separated list from {@link @crossauth/common!OAuthFlows}.
|
|
71
|
+
* If [`all`], there must be none other in the list.
|
|
72
|
+
*
|
|
73
|
+
* This is needed not only by the authorization server but also the
|
|
74
|
+
* session server if you are creating endpoints for manipulating
|
|
75
|
+
* the OAuth client table.
|
|
76
|
+
*
|
|
77
|
+
* Default ['all']
|
|
78
|
+
*/
|
|
79
|
+
validFlows?: string[];
|
|
80
|
+
/** Function that throws a {@link @crossauth/common!CrossauthError}
|
|
81
|
+
* with {@link @crossauth/common!ErrorCode} `FormEntry` if the user
|
|
82
|
+
* doesn't confirm to local rules. Doesn't validate passwords */
|
|
83
|
+
validateUserFn?: (user: UserInputFields) => string[];
|
|
84
|
+
/** Function that creates a user from form fields.
|
|
85
|
+
* Default one takes fields that begin with `user_`, removing the `user_`
|
|
86
|
+
* prefix and filtering out anything not in the userEditableFields list in
|
|
87
|
+
* the user storage.
|
|
88
|
+
*/
|
|
89
|
+
createUserFn?: (event: RequestEvent, data: {
|
|
90
|
+
[key: string]: string | undefined;
|
|
91
|
+
}, userEditableFields: string[]) => UserInputFields;
|
|
92
|
+
/** Function that updates a user from form fields.
|
|
93
|
+
* Default one takes fields that begin with `user_`, removing the `user_`
|
|
94
|
+
* prefix and filtering out anything not in the userEditableFields list in
|
|
95
|
+
* the user storage.
|
|
96
|
+
*/
|
|
97
|
+
updateUserFn?: (user: User, event: RequestEvent, data: {
|
|
98
|
+
[key: string]: string | undefined;
|
|
99
|
+
}, userEditableFields: string[]) => User;
|
|
100
|
+
/** Called when a new session token is going to be saved
|
|
101
|
+
* Add additional fields to your session storage here. Return a map of
|
|
102
|
+
* keys to values. Don't consume form data.
|
|
103
|
+
* Use {@link JsonOrFormData }, which takes a copy first. */
|
|
104
|
+
addToSession?: (event: RequestEvent, formData: {
|
|
105
|
+
[key: string]: string;
|
|
106
|
+
}) => {
|
|
107
|
+
[key: string]: string | number | boolean | Date | undefined;
|
|
108
|
+
};
|
|
109
|
+
/** Called after the session ID is validated.
|
|
110
|
+
* Use this to add additional checks based on the request.
|
|
111
|
+
* Throw an exception if cecks fail
|
|
112
|
+
*/
|
|
113
|
+
validateSession?: (session: Key, user: User | undefined, request: RequestEvent) => void;
|
|
114
|
+
/**
|
|
115
|
+
* These page endpoints need the second factor to be entered. Visiting
|
|
116
|
+
* the page redirects the user to the factor2 page.
|
|
117
|
+
*
|
|
118
|
+
* You should include at least any URLs which validate a user, also
|
|
119
|
+
* the url for configuring 2FA.
|
|
120
|
+
*
|
|
121
|
+
* You can have wildcard which is useful for including path info,
|
|
122
|
+
* eg `/resetpassword/*`
|
|
123
|
+
*
|
|
124
|
+
* THe default is empty.
|
|
125
|
+
*/
|
|
126
|
+
factor2ProtectedPageEndpoints?: string[];
|
|
127
|
+
/**
|
|
128
|
+
* These page endpoints need the second factor to be entered. Making
|
|
129
|
+
* a call to these endpoints results in a response of
|
|
130
|
+
* `{"ok": true, "factor2Required": true `}. The user should then
|
|
131
|
+
* make a call to `/api/factor2`. If the credetials are correct, the
|
|
132
|
+
* response will be that of the original request.
|
|
133
|
+
*
|
|
134
|
+
* You can have wildcard which is useful for including path info,
|
|
135
|
+
* eg `/resetpassword/*`
|
|
136
|
+
*/
|
|
137
|
+
factor2ProtectedApiEndpoints?: string[];
|
|
138
|
+
/**
|
|
139
|
+
* These page endpoints need the the user to be logged in. If not,
|
|
140
|
+
* the user is directed to the login page.
|
|
141
|
+
*
|
|
142
|
+
* You can have wildcard which is useful for including path info,
|
|
143
|
+
* eg `/resetpassword/*`
|
|
144
|
+
*
|
|
145
|
+
* The default is empty.
|
|
146
|
+
*
|
|
147
|
+
*/
|
|
148
|
+
loginProtectedPageEndpoints?: string[];
|
|
149
|
+
/**
|
|
150
|
+
* These page endpoints need the the user to be logged in. If not,
|
|
151
|
+
* the user is is sent an unauthorized response
|
|
152
|
+
*
|
|
153
|
+
* The default is empty
|
|
154
|
+
*/
|
|
155
|
+
loginProtectedApiEndpoints?: string[];
|
|
156
|
+
/**
|
|
157
|
+
* See `adminPageEndpoints`
|
|
158
|
+
*/
|
|
159
|
+
unauthorizedUrl?: string;
|
|
160
|
+
/**
|
|
161
|
+
* These page endpoints need an admin user to be logged in.
|
|
162
|
+
*
|
|
163
|
+
* This
|
|
164
|
+
* is defined by the isAdminFn option in {@link SvelteKitServerOptions}.
|
|
165
|
+
* The default one is to check the `admin` boolean field in the user
|
|
166
|
+
* object. If there is no user, or the user is not an admin, a 401
|
|
167
|
+
* page is returned,
|
|
168
|
+
*
|
|
169
|
+
* The default is empty
|
|
170
|
+
*
|
|
171
|
+
* If unauthorizedUrl is defined, that will be rendered. Otherwise
|
|
172
|
+
* a simple text message will be displayed.
|
|
173
|
+
*
|
|
174
|
+
*/
|
|
175
|
+
adminPageEndpoints?: string[];
|
|
176
|
+
/**
|
|
177
|
+
* Same as adminPageEndpoints but returns a JSON error instead of an
|
|
178
|
+
* error page
|
|
179
|
+
*
|
|
180
|
+
* This
|
|
181
|
+
* is defined by the isAdminFn option in {@link SvelteKitServerOptions}.
|
|
182
|
+
* The default one is to check the `admin` boolean field in the user
|
|
183
|
+
* object. If there is no user, or the user is not an admin, a 401
|
|
184
|
+
* page is returned,
|
|
185
|
+
*
|
|
186
|
+
* The default is empty
|
|
187
|
+
*
|
|
188
|
+
*/
|
|
189
|
+
adminApiEndpoints?: string[];
|
|
190
|
+
/**
|
|
191
|
+
* Turns on email verification. This will cause the verification tokens to
|
|
192
|
+
* be sent when the account
|
|
193
|
+
* is activated and when email is changed. Default false.
|
|
194
|
+
*/
|
|
195
|
+
enableEmailVerification?: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Turns on password reset. Default false.
|
|
198
|
+
*/
|
|
199
|
+
enablePasswordReset?: boolean;
|
|
200
|
+
/**
|
|
201
|
+
* CSRF protection is on by default but can be disabled by setting
|
|
202
|
+
* this to false.
|
|
203
|
+
*
|
|
204
|
+
* Sveltekit has its own CSRF protection enabled by default. If you
|
|
205
|
+
* disable it here, make sure you are not doing anything that bypasses
|
|
206
|
+
* Sveltekit's own protection.
|
|
207
|
+
*/
|
|
208
|
+
enableCsrfProtection?: boolean;
|
|
209
|
+
/**
|
|
210
|
+
* This parameter affects users who are not logged in with a session ID
|
|
211
|
+
* but with an OAuth access token. Such users can only update their user
|
|
212
|
+
* record if the scoped named in this variable has been authorized by
|
|
213
|
+
* that user for the client.
|
|
214
|
+
*
|
|
215
|
+
* By default, no scopes are authorized to edit the user.
|
|
216
|
+
*/
|
|
217
|
+
editUserScope?: string;
|
|
218
|
+
/**
|
|
219
|
+
* Admin pages provide functionality for searching for users. By
|
|
220
|
+
* default the search string must exactly match the client name
|
|
221
|
+
* (after normalizing
|
|
222
|
+
* and lowercasing). Override this behaviour with this function
|
|
223
|
+
* @param searchTerm the search term
|
|
224
|
+
* @param userStorage the user storage to search
|
|
225
|
+
* @returns array of matching users
|
|
226
|
+
*/
|
|
227
|
+
userSearchFn?: (searchTerm: string, userStorage: UserStorage, skip?: number, take?: number) => Promise<User[]>;
|
|
228
|
+
/**
|
|
229
|
+
* Admin pages provide functionality for searching for OAuth clients. By
|
|
230
|
+
* default the search string must exactly match the client_name exactly.
|
|
231
|
+
* Override this behaviour with this function
|
|
232
|
+
* @param searchTerm the search term
|
|
233
|
+
* @param clientStorage the client storage to search
|
|
234
|
+
* @returns array of matching users
|
|
235
|
+
*/
|
|
236
|
+
clientSearchFn?: (searchTerm: string, clientStorage: OAuthClientStorage, skip: number, take: number, userid?: string | number | null) => Promise<OAuthClient[]>;
|
|
237
|
+
/** Pass the Sveltekit redirect function */
|
|
238
|
+
redirect?: any;
|
|
239
|
+
/** Pass the Sveltekit error function */
|
|
240
|
+
error?: any;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* The Sveltekit session server.
|
|
244
|
+
*
|
|
245
|
+
* You shouldn't have to instantiate this directly. It is created when
|
|
246
|
+
* you create a {@link SveltekitServer} object.
|
|
247
|
+
*/
|
|
248
|
+
export declare class SvelteKitSessionServer implements SvelteKitSessionAdapter {
|
|
249
|
+
/**
|
|
250
|
+
* Hook to check if the user is logged in and set data in `locals`
|
|
251
|
+
* accordingly.
|
|
252
|
+
*/
|
|
253
|
+
readonly sessionHook: (input: {
|
|
254
|
+
event: RequestEvent;
|
|
255
|
+
}) => MaybePromise<{
|
|
256
|
+
headers: Header[];
|
|
257
|
+
}>;
|
|
258
|
+
readonly twoFAHook: (input: {
|
|
259
|
+
event: RequestEvent;
|
|
260
|
+
}) => MaybePromise<{
|
|
261
|
+
twofa: boolean;
|
|
262
|
+
ok: boolean;
|
|
263
|
+
response?: Response;
|
|
264
|
+
}>;
|
|
265
|
+
/**
|
|
266
|
+
* Key storage taken from constructor args.
|
|
267
|
+
* See {@link SvelteKitSessionServer.constructor}.
|
|
268
|
+
*/
|
|
269
|
+
readonly keyStorage: KeyStorage;
|
|
270
|
+
/**
|
|
271
|
+
* Session Manager taken from constructor args.
|
|
272
|
+
* See {@link SvelteKitSessionServer.constructor}.
|
|
273
|
+
*/
|
|
274
|
+
readonly sessionManager: SessionManager;
|
|
275
|
+
/**
|
|
276
|
+
* User storage taken from constructor args.
|
|
277
|
+
* See {@link SvelteKitSessionServer.constructor}.
|
|
278
|
+
*/
|
|
279
|
+
readonly userStorage?: UserStorage;
|
|
280
|
+
/**
|
|
281
|
+
* User storage taken from constructor args.
|
|
282
|
+
* See {@link SvelteKitSessionServer.constructor}.
|
|
283
|
+
*/
|
|
284
|
+
readonly clientStorage?: OAuthClientStorage;
|
|
285
|
+
/**
|
|
286
|
+
* Funtion to validate users upon creation. Taken from the options during
|
|
287
|
+
* construction or the default value.
|
|
288
|
+
* See {@link FastifySessionServerOptions}.
|
|
289
|
+
*/
|
|
290
|
+
validateUserFn: (user: UserInputFields) => string[];
|
|
291
|
+
/**
|
|
292
|
+
* Funtion to create a user record from form fields. Taken from the options during
|
|
293
|
+
* construction or the default value.
|
|
294
|
+
* See {@link FastifySessionServerOptions}.
|
|
295
|
+
*/
|
|
296
|
+
createUserFn: (event: RequestEvent, data: {
|
|
297
|
+
[key: string]: string | undefined;
|
|
298
|
+
}, userEditableFields: string[]) => UserInputFields;
|
|
299
|
+
/**
|
|
300
|
+
* Funtion to update a user record from form fields. Taken from the options during
|
|
301
|
+
* construction or the default value.
|
|
302
|
+
* See {@link FastifySessionServerOptions}.
|
|
303
|
+
*/
|
|
304
|
+
updateUserFn: (user: User, event: RequestEvent, data: {
|
|
305
|
+
[key: string]: string | undefined;
|
|
306
|
+
}, userEditableFields: string[]) => User;
|
|
307
|
+
/**
|
|
308
|
+
* The set of authenticators taken from constructor args.
|
|
309
|
+
* See {@link FastifySessionServer.constructor}.
|
|
310
|
+
*/
|
|
311
|
+
readonly authenticators: {
|
|
312
|
+
[key: string]: Authenticator;
|
|
313
|
+
};
|
|
314
|
+
/**
|
|
315
|
+
* The set of allowed authenticators taken from the options during
|
|
316
|
+
* construction.
|
|
317
|
+
*
|
|
318
|
+
* The default is `[{name: "none", friendlyName: "none"}]`
|
|
319
|
+
*/
|
|
320
|
+
readonly allowedFactor2: {
|
|
321
|
+
name: string;
|
|
322
|
+
friendlyName: string;
|
|
323
|
+
configurable: boolean;
|
|
324
|
+
}[];
|
|
325
|
+
/**
|
|
326
|
+
* The set of allowed authenticators taken from the options during
|
|
327
|
+
* construction.
|
|
328
|
+
*
|
|
329
|
+
* The default is `["none"]`.
|
|
330
|
+
*/
|
|
331
|
+
readonly allowedFactor2Names: string[];
|
|
332
|
+
/** Called when a new session token is going to be saved
|
|
333
|
+
* Add additional fields to your session storage here. Return a map of
|
|
334
|
+
* keys to values */
|
|
335
|
+
addToSession?: (event: RequestEvent, formData: {
|
|
336
|
+
[key: string]: string;
|
|
337
|
+
}) => {
|
|
338
|
+
[key: string]: string | number | boolean | Date | undefined;
|
|
339
|
+
};
|
|
340
|
+
/**
|
|
341
|
+
* The set of allowed authenticators taken from the options during
|
|
342
|
+
* construction.
|
|
343
|
+
*/
|
|
344
|
+
private validateSession?;
|
|
345
|
+
private factor2ProtectedPageEndpoints;
|
|
346
|
+
private factor2ProtectedApiEndpoints;
|
|
347
|
+
private loginProtectedPageEndpoints;
|
|
348
|
+
private loginProtectedApiEndpoints;
|
|
349
|
+
private adminPageEndpoints;
|
|
350
|
+
private adminApiEndpoints;
|
|
351
|
+
readonly unauthorizedUrl: string | undefined;
|
|
352
|
+
readonly enableCsrfProtection = true;
|
|
353
|
+
/** Whether email verification is enabled.
|
|
354
|
+
*
|
|
355
|
+
* Reads from constructor options
|
|
356
|
+
*/
|
|
357
|
+
readonly enableEmailVerification = false;
|
|
358
|
+
/** Whether password reset is enabled.
|
|
359
|
+
*
|
|
360
|
+
* Reads from constructor options
|
|
361
|
+
*/
|
|
362
|
+
readonly enablePasswordReset = false;
|
|
363
|
+
private factor2Url;
|
|
364
|
+
/**
|
|
365
|
+
* Use these to access the `load` and `action` endpoints for functions
|
|
366
|
+
* provided by Crossauth. These are the ones intended for users to
|
|
367
|
+
* have access to.
|
|
368
|
+
*
|
|
369
|
+
* See {@link SvelteKitUserEndpoints}
|
|
370
|
+
*/
|
|
371
|
+
readonly userEndpoints: SvelteKitUserEndpoints;
|
|
372
|
+
/**
|
|
373
|
+
* Use these to access the `load` and `action` endpoints for functions
|
|
374
|
+
* provided by Crossauth that relate to manipulating OAuth clients in the
|
|
375
|
+
* database. These are the ones intended for users to
|
|
376
|
+
* have access to.
|
|
377
|
+
*
|
|
378
|
+
* See {@link SvelteKitUserEndpoints}
|
|
379
|
+
*/
|
|
380
|
+
readonly userClientEndpoints: SvelteKitUserClientEndpoints;
|
|
381
|
+
/**
|
|
382
|
+
* Use these to access the `load` and `action` endpoints for functions
|
|
383
|
+
* provided by Crossauth that relate to manipulating OAuth clients in the
|
|
384
|
+
* database as admin. These are the ones intended for users to
|
|
385
|
+
* have access to.
|
|
386
|
+
*
|
|
387
|
+
* See {@link SvelteKitAdminEndpoints}
|
|
388
|
+
*/
|
|
389
|
+
readonly adminClientEndpoints: SvelteKitAdminClientEndpoints;
|
|
390
|
+
/**
|
|
391
|
+
* Use these to access the `load` and `action` endpoints for functions
|
|
392
|
+
* provides by Crossauth. These are the ones intended for admins to
|
|
393
|
+
* have access to.
|
|
394
|
+
*
|
|
395
|
+
* See {@link SvelteKitAdminEndpoints}
|
|
396
|
+
*/
|
|
397
|
+
readonly adminEndpoints: SvelteKitAdminEndpoints;
|
|
398
|
+
readonly redirect: any;
|
|
399
|
+
readonly error: any;
|
|
400
|
+
/**
|
|
401
|
+
* This is read from options during construction.
|
|
402
|
+
*
|
|
403
|
+
* See {@link SvelteKitServerOptions}.
|
|
404
|
+
*/
|
|
405
|
+
readonly editUserScope?: string;
|
|
406
|
+
/**
|
|
407
|
+
* Constructor
|
|
408
|
+
* @param keyStorage where session IDs, email verification and reset tokens are stored
|
|
409
|
+
* @param authenticators valid authenticators that can be in `factor1` or `factor2`
|
|
410
|
+
* of the user. See class documentation for {@link SvelteKitServer} for an example.
|
|
411
|
+
* @param options See {@link SvelteKitSessionServerOptions}.
|
|
412
|
+
*/
|
|
413
|
+
constructor(keyStorage: KeyStorage, authenticators: {
|
|
414
|
+
[key: string]: Authenticator;
|
|
415
|
+
}, options?: SvelteKitSessionServerOptions);
|
|
416
|
+
/**
|
|
417
|
+
* Returns the session cookie value from the Sveltekit request event
|
|
418
|
+
* @param event the request event
|
|
419
|
+
* @returns the whole cookie value
|
|
420
|
+
*/
|
|
421
|
+
getSessionCookieValue(event: RequestEvent): string | undefined;
|
|
422
|
+
/**
|
|
423
|
+
* Returns the session cookie value from the Sveltekit request event
|
|
424
|
+
* @param event the request event
|
|
425
|
+
* @returns the whole cookie value
|
|
426
|
+
*/
|
|
427
|
+
getCsrfCookieValue(event: RequestEvent): string | undefined;
|
|
428
|
+
private clearCookie;
|
|
429
|
+
/**
|
|
430
|
+
* Sets headers in the request event.
|
|
431
|
+
*
|
|
432
|
+
* Used internally by {@link SveltekitServer}. Shouldn't be necessary
|
|
433
|
+
* to call this directly.
|
|
434
|
+
* @param headers the headres to set
|
|
435
|
+
* @param resp the response object to set them in
|
|
436
|
+
*/
|
|
437
|
+
setHeaders(headers: Header[], resp: Response): void;
|
|
438
|
+
/**
|
|
439
|
+
* Sets the CSRF cookie.
|
|
440
|
+
*
|
|
441
|
+
* Used internally. Shouldn't be necessary
|
|
442
|
+
* to call this directly.
|
|
443
|
+
* @param cookie the new cookie and parameters
|
|
444
|
+
* @param event the request event
|
|
445
|
+
*/
|
|
446
|
+
setCsrfCookie(cookie: Cookie, event: RequestEvent): void;
|
|
447
|
+
private setHeader;
|
|
448
|
+
/**
|
|
449
|
+
* Returns a hash of the session cookie value.
|
|
450
|
+
*
|
|
451
|
+
* Used only in reporting, so that logs don't contain the actual session ID.
|
|
452
|
+
*
|
|
453
|
+
* @param event the Sveltelkit request event
|
|
454
|
+
* @returns a stering hash of the cookie value
|
|
455
|
+
*/
|
|
456
|
+
getHashOfSessionCookie(event: RequestEvent): string;
|
|
457
|
+
/**
|
|
458
|
+
* Returns a hash of the CSRF cookie value.
|
|
459
|
+
*
|
|
460
|
+
* Used only in reporting, so that logs don't contain the actual CSRF cookie value.
|
|
461
|
+
*
|
|
462
|
+
* @param event the Sveltelkit request event
|
|
463
|
+
* @returns a stering hash of the cookie value
|
|
464
|
+
*/
|
|
465
|
+
getHashOfCsrfCookie(event: RequestEvent): string;
|
|
466
|
+
/**
|
|
467
|
+
* Returns a CSRF token if the CSRF cookie is valid.
|
|
468
|
+
*
|
|
469
|
+
* Used internally. Shouldn't be necessary
|
|
470
|
+
* to call this directly.
|
|
471
|
+
*
|
|
472
|
+
* @param event the request event
|
|
473
|
+
* @param headers headers the token will be added to, as well as
|
|
474
|
+
* adding it to locals
|
|
475
|
+
* @returns the string CSRF token for inclusion in forms
|
|
476
|
+
*/
|
|
477
|
+
csrfToken(event: RequestEvent, headers: Header[]): Promise<string | undefined>;
|
|
478
|
+
/**
|
|
479
|
+
* Used internally to update an existing Sveltekit request object with
|
|
480
|
+
* a new body and headers.
|
|
481
|
+
*
|
|
482
|
+
* Used when restoring a request that was interrupted for 2FA
|
|
483
|
+
*
|
|
484
|
+
* @param event the request event
|
|
485
|
+
* @param params JSON params to add to the new body
|
|
486
|
+
* @param contentType the new content type
|
|
487
|
+
* @returns the updated request event
|
|
488
|
+
*/
|
|
489
|
+
static updateRequest(event: RequestEvent, params: {
|
|
490
|
+
[key: string]: string;
|
|
491
|
+
}, contentType: string): RequestEvent<Partial<Record<string, string>>, string | null>;
|
|
492
|
+
/**
|
|
493
|
+
* Returns a hash of the session ID. Used for logging (for security,
|
|
494
|
+
* the actual session ID is not logged)
|
|
495
|
+
* @param request the Fastify request
|
|
496
|
+
* @returns hash of the session ID
|
|
497
|
+
*/
|
|
498
|
+
getHashOfSessionId(event: RequestEvent): string;
|
|
499
|
+
/**
|
|
500
|
+
* Returns whether or not 2FA authentication was initiated as a result
|
|
501
|
+
* of visiting a page protected by it
|
|
502
|
+
* @param event the request event
|
|
503
|
+
* @returns true or false
|
|
504
|
+
*/
|
|
505
|
+
factor2PageVisitStarted(event: RequestEvent): Promise<boolean>;
|
|
506
|
+
/**
|
|
507
|
+
* Returns whether a page being visited as part of a request event is
|
|
508
|
+
* configured to be protected by login.
|
|
509
|
+
*
|
|
510
|
+
* See {@link SvelteKitSessionServerOptions.loginProtectedPageEndpoints}.
|
|
511
|
+
*
|
|
512
|
+
* @param event the request event
|
|
513
|
+
* @returns true or false
|
|
514
|
+
*/
|
|
515
|
+
isLoginPageProtected(event: RequestEvent | string): boolean;
|
|
516
|
+
/**
|
|
517
|
+
* Returns whether an API call is being visited as part of a request event is
|
|
518
|
+
* configured to be protected by login.
|
|
519
|
+
*
|
|
520
|
+
* See {@link SvelteKitSessionServerOptions.loginProtectedApiEndpoints}.
|
|
521
|
+
*
|
|
522
|
+
* @param event the request event
|
|
523
|
+
* @returns true or false
|
|
524
|
+
*/
|
|
525
|
+
isLoginApiProtected(event: RequestEvent | string): boolean;
|
|
526
|
+
/**
|
|
527
|
+
* Returns whether a page being visited as part of a request event is
|
|
528
|
+
* configured to be protected by 2FA.
|
|
529
|
+
*
|
|
530
|
+
* See {@link SvelteKitSessionServerOptions.factor2ProtectedPageEndpoints}.
|
|
531
|
+
*
|
|
532
|
+
* @param event the request event
|
|
533
|
+
* @returns true or false
|
|
534
|
+
*/
|
|
535
|
+
isFactor2PageProtected(event: RequestEvent | string): boolean;
|
|
536
|
+
/**
|
|
537
|
+
* Returns whether an API call is being visited as part of a request event is
|
|
538
|
+
* configured to be protected by 2FA.
|
|
539
|
+
*
|
|
540
|
+
* See {@link SvelteKitSessionServerOptions.factor2ProtectedApiEndpoints}.
|
|
541
|
+
*
|
|
542
|
+
* @param event the request event
|
|
543
|
+
* @returns true or false
|
|
544
|
+
*/
|
|
545
|
+
isFactor2ApiProtected(event: RequestEvent | string): boolean;
|
|
546
|
+
/**
|
|
547
|
+
* Returns whether a page being visited as part of a request event is
|
|
548
|
+
* configured to be protected as admin only.
|
|
549
|
+
*
|
|
550
|
+
* See {@link SvelteKitSessionServerOptions.adminPageEndpoints}.
|
|
551
|
+
*
|
|
552
|
+
* @param event the request event
|
|
553
|
+
* @returns true or false
|
|
554
|
+
*/
|
|
555
|
+
isAdminPageEndpoint(event: RequestEvent | string): boolean;
|
|
556
|
+
/**
|
|
557
|
+
* Returns whether an AP call being visited as part of a request event is
|
|
558
|
+
* configured to be protected as admin only.
|
|
559
|
+
*
|
|
560
|
+
* See {@link SvelteKitSessionServerOptions.adminApiEndpoints}.
|
|
561
|
+
*
|
|
562
|
+
* @param event the request event
|
|
563
|
+
* @returns true or false
|
|
564
|
+
*/
|
|
565
|
+
isAdminApiEndpoint(event: RequestEvent | string): boolean;
|
|
566
|
+
/**
|
|
567
|
+
* Creates an anonymous session, setting the `Set-Cookue` headers
|
|
568
|
+
* in the reply.
|
|
569
|
+
*
|
|
570
|
+
* An anonymous sessiin is a session cookie that is not associated
|
|
571
|
+
* with a user (`userid` is undefined). It can be used to persist
|
|
572
|
+
* data between sessions just like a regular user session ID.
|
|
573
|
+
*
|
|
574
|
+
* @param request the Fastify request
|
|
575
|
+
* @param reply the Fastify reply
|
|
576
|
+
* @param data session data to save
|
|
577
|
+
* @returns the session cookie value
|
|
578
|
+
*/
|
|
579
|
+
createAnonymousSession(event: RequestEvent, data?: {
|
|
580
|
+
[key: string]: any;
|
|
581
|
+
}): Promise<string>;
|
|
582
|
+
/**
|
|
583
|
+
* Sets locals based on session and CSRF cookies.
|
|
584
|
+
*
|
|
585
|
+
* Sets things like `locals.user`. You can call this if you need them
|
|
586
|
+
* updated based on cookie settings and a page load hasn't been done
|
|
587
|
+
* (ie the hooks haven't run).
|
|
588
|
+
*
|
|
589
|
+
* @param event the Sveltekit request event.
|
|
590
|
+
*/
|
|
591
|
+
refreshLocals(event: RequestEvent): Promise<void>;
|
|
592
|
+
csrfProtectionEnabled(): boolean;
|
|
593
|
+
getCsrfToken(event: RequestEvent): string | undefined;
|
|
594
|
+
getUser(event: RequestEvent): User | undefined;
|
|
595
|
+
/**
|
|
596
|
+
* Returns the data stored along with the session server-side, with the
|
|
597
|
+
* given name
|
|
598
|
+
* @param event the Sveltekit request event
|
|
599
|
+
* @param name tjhe data name to return
|
|
600
|
+
* @returns an object or undefined.
|
|
601
|
+
*/
|
|
602
|
+
getSessionData(event: RequestEvent, name: string): Promise<{
|
|
603
|
+
[key: string]: any;
|
|
604
|
+
} | undefined>;
|
|
605
|
+
/**
|
|
606
|
+
* Updates or sets the given field in the session `data` field.
|
|
607
|
+
*
|
|
608
|
+
* The `data` field in the session record is assumed to be JSON
|
|
609
|
+
*
|
|
610
|
+
* @param event the Sveltekit request event
|
|
611
|
+
* @param name the name of the field to set
|
|
612
|
+
* @param value the value to set it to.
|
|
613
|
+
*/
|
|
614
|
+
updateSessionData(event: RequestEvent, name: string, value: any): Promise<void>;
|
|
615
|
+
updateManySessionData(event: RequestEvent, dataArray: {
|
|
616
|
+
dataName: string;
|
|
617
|
+
value: any;
|
|
618
|
+
}[]): Promise<void>;
|
|
619
|
+
/**
|
|
620
|
+
* Deletes the given field from the session `data` field.
|
|
621
|
+
*
|
|
622
|
+
* The `data` field in the session record is assumed to be JSON
|
|
623
|
+
*
|
|
624
|
+
* @param event the Sveltekit request event
|
|
625
|
+
* @param name the name of the field to set
|
|
626
|
+
*/
|
|
627
|
+
deleteSessionData(event: RequestEvent, name: string): Promise<void>;
|
|
628
|
+
}
|
|
629
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
import { User } from '@crossauth/common';
|
|
3
|
+
|
|
4
|
+
export declare abstract class SvelteKitSessionAdapter {
|
|
5
|
+
abstract csrfProtectionEnabled(): boolean;
|
|
6
|
+
abstract getCsrfToken(event: RequestEvent): string | undefined;
|
|
7
|
+
abstract getUser(event: RequestEvent): User | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Updates a field in the session data in the key storage record,
|
|
10
|
+
*
|
|
11
|
+
* The `data` field is assumed to be JSON. Just the field with the given
|
|
12
|
+
* name is updated and the rest is unchanged.
|
|
13
|
+
* @param event the Sveltekit request event
|
|
14
|
+
* @param name the field within `data` to update
|
|
15
|
+
* @param value the value to set it to
|
|
16
|
+
*/
|
|
17
|
+
abstract updateSessionData(event: RequestEvent, name: string, value: any): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Same as `updateData` but updates many within same transaction
|
|
20
|
+
*
|
|
21
|
+
* The `data` field is assumed to be JSON. Just the field with the given
|
|
22
|
+
* name is updated and the rest is unchanged.
|
|
23
|
+
* @param request the Fastifdy request
|
|
24
|
+
* @param dataArray data to update
|
|
25
|
+
*/
|
|
26
|
+
abstract updateManySessionData(event: RequestEvent, dataArray: {
|
|
27
|
+
dataName: string;
|
|
28
|
+
value: any;
|
|
29
|
+
}[]): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Deletes a field from the session data in the key storage record,
|
|
32
|
+
*
|
|
33
|
+
* The `data` field is assumed to be JSON. Just the field with the given
|
|
34
|
+
* name is updated and the rest is unchanged.
|
|
35
|
+
* @param event the Sveltekit request event
|
|
36
|
+
* @param name the field within `data` to update
|
|
37
|
+
*/
|
|
38
|
+
abstract deleteSessionData(event: RequestEvent, name: string): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Return data stored in the session with key `name` or undefined if not present
|
|
41
|
+
* @param event the Sveltekit request event
|
|
42
|
+
* @param name name of the data to fetch
|
|
43
|
+
* @return an object of the data, or undefined
|
|
44
|
+
*/
|
|
45
|
+
abstract getSessionData(event: RequestEvent, name: string): Promise<{
|
|
46
|
+
[key: string]: any;
|
|
47
|
+
} | undefined>;
|
|
48
|
+
}
|