@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,286 @@
|
|
|
1
|
+
import { SvelteKitSessionServer, SvelteKitSessionServerOptions } from './sveltekitsession';
|
|
2
|
+
import { SvelteKitApiKeyServer, SvelteKitApiKeyServerOptions } from './sveltekitapikey';
|
|
3
|
+
import { SvelteKitAuthorizationServer, SvelteKitAuthorizationServerOptions } from './sveltekitoauthserver';
|
|
4
|
+
import { UserStorage, KeyStorage, OAuthClientStorage } from '@crossauth/backend';
|
|
5
|
+
import { User } from '@crossauth/common';
|
|
6
|
+
import { Handle, RequestEvent, ResolveOptions, MaybePromise } from '@sveltejs/kit';
|
|
7
|
+
import { SvelteKitOAuthClient, SvelteKitOAuthClientOptions } from './sveltekitoauthclient';
|
|
8
|
+
import { SvelteKitOAuthResourceServer, SvelteKitOAuthResourceServerOptions } from './sveltekitresserver';
|
|
9
|
+
import { SvelteKitSessionAdapter } from './sveltekitsessionadapter';
|
|
10
|
+
|
|
11
|
+
export interface SvelteKitServerOptions extends SvelteKitSessionServerOptions, SvelteKitApiKeyServerOptions, SvelteKitAuthorizationServerOptions, SvelteKitOAuthClientOptions, SvelteKitOAuthResourceServerOptions {
|
|
12
|
+
/** User can set this to check if the user is an administrator.
|
|
13
|
+
* By default, the admin booloean field in the user object is checked
|
|
14
|
+
*/
|
|
15
|
+
isAdminFn?: (user: User) => boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* This is the type for endpoint objects that provide `load` and `action`
|
|
19
|
+
* exports for your pages. See the {@link SvelteKitAdminEndpoints}
|
|
20
|
+
* and {@link SvelteKitAdminEndpoints} for more details.
|
|
21
|
+
*/
|
|
22
|
+
export type SveltekitEndpoint = {
|
|
23
|
+
load?: (event: RequestEvent) => Promise<{
|
|
24
|
+
[key: string]: any;
|
|
25
|
+
}>;
|
|
26
|
+
actions?: {
|
|
27
|
+
[key: string]: (event: RequestEvent) => Promise<{
|
|
28
|
+
[key: string]: any;
|
|
29
|
+
}>;
|
|
30
|
+
};
|
|
31
|
+
get?: (event: RequestEvent) => Promise<Response>;
|
|
32
|
+
post?: (event: RequestEvent) => Promise<Response>;
|
|
33
|
+
};
|
|
34
|
+
export type Resolver = (event: RequestEvent, opts?: ResolveOptions) => MaybePromise<Response>;
|
|
35
|
+
/**
|
|
36
|
+
* This is the main class for adding Crossauth to Svelekit applications.
|
|
37
|
+
*
|
|
38
|
+
* To use it, create a file in your `src/lib` directory, eg
|
|
39
|
+
* `src/lib/crossauthsessiion.ts`, something like this:
|
|
40
|
+
*
|
|
41
|
+
* ```
|
|
42
|
+
* export const prisma = new PrismaClient();
|
|
43
|
+
* const userStorage = new PrismaUserStorage({prismaClient : prisma, userEditableFields: ["email"]});
|
|
44
|
+
* const keyStorage = new PrismaKeyStorage({prismaClient : prisma});
|
|
45
|
+
* const passwordAuthenticator = new LocalPasswordAuthenticator(userStorage);
|
|
46
|
+
* export const crossauth = new SvelteKitServer({
|
|
47
|
+
* session: {
|
|
48
|
+
* keyStorage: keyStorage,
|
|
49
|
+
* }}, {
|
|
50
|
+
* userStorage: userStorage,
|
|
51
|
+
* authenticators: {
|
|
52
|
+
* localpassword: passwordAuthenticator,
|
|
53
|
+
* },
|
|
54
|
+
* loginProtectedPageEndpoints: ["/account"],
|
|
55
|
+
* redirect,
|
|
56
|
+
* error
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* Note that we pass Sveltekit's `action` and `error` methods because, as
|
|
61
|
+
* a module compiled without your Sveltekit application, this class has
|
|
62
|
+
* no access to them otherwise, and they are use internally for things like
|
|
63
|
+
* redirecting to your login page.
|
|
64
|
+
*
|
|
65
|
+
* **Component Servers**
|
|
66
|
+
*
|
|
67
|
+
* The above example creates a ccookie-based session server. This class
|
|
68
|
+
* has several optional component servers which can be instantiated
|
|
69
|
+
* individually or together. They are:
|
|
70
|
+
*
|
|
71
|
+
* - `sessionServer` Session cookie management server. Uses sesion ID
|
|
72
|
+
* and CSRF cookies. See {@link SvelteKitSessionServer}.
|
|
73
|
+
* - `sessionAdapter` If you are using only the oAuthClient and don't want
|
|
74
|
+
* to use Crossauth's session server, you can implement
|
|
75
|
+
* a minimal {@link SveltekitSessionAdapter} instead.
|
|
76
|
+
* - `oAuthAuthServer` OAuth authorization server. See
|
|
77
|
+
* {@link SvelteKitAuthorizationServer}
|
|
78
|
+
* - `oAuthClient` OAuth client. See {@link SvelteKitOAuthClient}.
|
|
79
|
+
* - `oAuthClients` Array of OAuth clients if you want more than one. See {@link SvelteKitOAuthClient}.
|
|
80
|
+
* - `oAuthResServer` OAuth resource server. See
|
|
81
|
+
* {@link SvelteKitOAuthResourceServer}.
|
|
82
|
+
*
|
|
83
|
+
* Use either `oAuthClient` or `oAuthClients` but not both.
|
|
84
|
+
*
|
|
85
|
+
* There is also an API key server which is not available as a variable as
|
|
86
|
+
* it has no functions other than the hook it registers.
|
|
87
|
+
* See {@link SvelteKitApiKeyServer}.
|
|
88
|
+
*
|
|
89
|
+
* **Hooks**
|
|
90
|
+
*
|
|
91
|
+
* This class provides hooks which you can add to by putting the following
|
|
92
|
+
* in your `hooks.server.ts`:
|
|
93
|
+
*
|
|
94
|
+
* ```
|
|
95
|
+
* import { type Handle } from '@sveltejs/kit';
|
|
96
|
+
* import { crossauth } from '$lib/server/crossauthsession';
|
|
97
|
+
* import { CrossauthLogger } from '@crossauth/common';
|
|
98
|
+
* export const handle: Handle = crossauth.hooks;
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* **Locals**
|
|
102
|
+
*
|
|
103
|
+
* This will set the following in `event.locals`:
|
|
104
|
+
*
|
|
105
|
+
* - `user`: the logged in {@link @crossauth/common!User} or undefined,
|
|
106
|
+
* - `csrfToken` a CSRF token if the request is a `GET`, `HEAD` or `OPTIONS`,
|
|
107
|
+
* - `authType` authentication type, currently only `cookie`,
|
|
108
|
+
* - `apiKey` the valid API key if one was used,
|
|
109
|
+
* - `oAuthAuthServer` OAuth authorization server. See
|
|
110
|
+
* {@link SvelteKitAuthorizationServer}
|
|
111
|
+
* - `accessTokenPayload` payload for the OAuth access token (not currently supported),
|
|
112
|
+
* - `authError` string error during authentication process (not currently used)
|
|
113
|
+
* - `authErrorDescription` error during authentication (not currently used),
|
|
114
|
+
* - `sessionId` session ID of logged in user, session ID for anonymous user, or undefined,
|
|
115
|
+
* - `scope` oAuth scope, not currently used,
|
|
116
|
+
*
|
|
117
|
+
* **Authenticators**
|
|
118
|
+
*
|
|
119
|
+
* One and two factor authentication is supported. Authentication is provided
|
|
120
|
+
* by classes implementing {@link Authenticator}. They are passed as an
|
|
121
|
+
* object to this class, keyed on the name that appears in the user record
|
|
122
|
+
* as `factor1` or `factor2`.
|
|
123
|
+
*
|
|
124
|
+
* For example, if you have passwords in your user database, you can use
|
|
125
|
+
* {@link @crossauth/backend!LocalPasswordAuthenticator}. If this method of authentication
|
|
126
|
+
* is called `password` in the `factor1` field of the user record,
|
|
127
|
+
* pass it in the `authenticators` parameter in the constructor with a key
|
|
128
|
+
* of `password`.
|
|
129
|
+
*
|
|
130
|
+
* **Use in Pages**
|
|
131
|
+
*
|
|
132
|
+
* For instructions about how to use this class in your endpoints, see
|
|
133
|
+
* {@link SvelkteKitUserEndpoints} and {@link SvelteKitAdminEndpoints}
|
|
134
|
+
* for cookie-based session management.
|
|
135
|
+
*/
|
|
136
|
+
export declare class SvelteKitServer {
|
|
137
|
+
/** The User storage that was passed during construction */
|
|
138
|
+
readonly userStorage?: UserStorage;
|
|
139
|
+
/** The session server if one was requested during construction */
|
|
140
|
+
readonly sessionServer?: SvelteKitSessionServer;
|
|
141
|
+
/** See class documentation. If you pass `sessionServer` here instead,
|
|
142
|
+
* `sessionAdapter` will also be set to it
|
|
143
|
+
*/
|
|
144
|
+
readonly sessionAdapter?: SvelteKitSessionAdapter;
|
|
145
|
+
/** The api key server if one was requested during construction */
|
|
146
|
+
readonly apiKeyServer?: SvelteKitApiKeyServer;
|
|
147
|
+
/** The OAuth authorization server if one was requested */
|
|
148
|
+
readonly oAuthAuthServer?: SvelteKitAuthorizationServer;
|
|
149
|
+
/** For adding in your `hooks.server.ts. See class documentation
|
|
150
|
+
* for details
|
|
151
|
+
*/
|
|
152
|
+
readonly hooks: (Handle);
|
|
153
|
+
private loginUrl;
|
|
154
|
+
/**
|
|
155
|
+
* User-defined function for determining whether a user is an admin.
|
|
156
|
+
*
|
|
157
|
+
* The default is to look at the `admin` member of the
|
|
158
|
+
* {@link @crossauth/common!User} object.
|
|
159
|
+
*/
|
|
160
|
+
static isAdminFn: (user: User) => boolean;
|
|
161
|
+
/**
|
|
162
|
+
* OAuth client instance
|
|
163
|
+
*/
|
|
164
|
+
readonly oAuthClient?: SvelteKitOAuthClient;
|
|
165
|
+
/**
|
|
166
|
+
* Array of OAuth client instances as an alternative to `oAuthClient`
|
|
167
|
+
*/
|
|
168
|
+
readonly oAuthClients?: SvelteKitOAuthClient[];
|
|
169
|
+
/** OAuth resource server instance */
|
|
170
|
+
readonly oAuthResServer?: SvelteKitOAuthResourceServer;
|
|
171
|
+
/**
|
|
172
|
+
* Constructor.
|
|
173
|
+
*
|
|
174
|
+
* @param config an object with configuration:
|
|
175
|
+
* - `session` if you want a session (session cookie-based
|
|
176
|
+
* authentication), include this. See the class documentation for
|
|
177
|
+
* details. Note that the options in the third parameter of this
|
|
178
|
+
* constructor are concatinated with the options defined in
|
|
179
|
+
* `session.options`, so that you can have global as well as
|
|
180
|
+
* session server-specific configuration.
|
|
181
|
+
* - `apiKey` if passed, instantiate the session server (see class
|
|
182
|
+
* documentation). The value is an object with a `keyStorage` field
|
|
183
|
+
* which must be present and should be the {@link KeyStorage} instance
|
|
184
|
+
* where API keys are stored. A field called `options` whose
|
|
185
|
+
* value is an {@link SveltekitApiKeyServerOptions} may also be
|
|
186
|
+
* provided.
|
|
187
|
+
* - `oAuthAuthServer` if passed, instantiate the session server (see class
|
|
188
|
+
* documentation). The value is an object with a `keyStorage` field
|
|
189
|
+
* which must be present and should be the {@link KeyStorage} instance
|
|
190
|
+
* where authorization codes are stored. This may be the same as
|
|
191
|
+
* the table storing session IDs or may be different. A field
|
|
192
|
+
* called `clientStorage` with a value of type {@link OAuthClientStorage}
|
|
193
|
+
* must be provided and is where OAuth client details are stored.
|
|
194
|
+
* A field called `options` whose
|
|
195
|
+
* value is an {@link SvelteKitAuthorizationServerOptions} may also be
|
|
196
|
+
* provided.
|
|
197
|
+
* - `oAuthClient` if present, an OAuth client will be created.
|
|
198
|
+
* There must be a field called `authServerBaseUrl` and is the
|
|
199
|
+
* base URL for the authorization server. When validating access
|
|
200
|
+
* tokens, the `iss` claim must match this.
|
|
201
|
+
* - `oAuthClients` use this instead of `oAuthClient` if you want more
|
|
202
|
+
* than one OAuth client.
|
|
203
|
+
* - `oAuthResServer` OAuth resource server. See
|
|
204
|
+
* {@link SvelteKitOAuthResourceServer}.
|
|
205
|
+
* - `options` Configuration that applies to the whole application,
|
|
206
|
+
* not just the session server.
|
|
207
|
+
*/
|
|
208
|
+
constructor({ session, sessionAdapter, apiKey, oAuthAuthServer, oAuthClient, oAuthClients, oAuthResServer, options, }: {
|
|
209
|
+
session?: {
|
|
210
|
+
keyStorage: KeyStorage;
|
|
211
|
+
options?: SvelteKitSessionServerOptions;
|
|
212
|
+
};
|
|
213
|
+
sessionAdapter?: SvelteKitSessionAdapter;
|
|
214
|
+
apiKey?: {
|
|
215
|
+
keyStorage: KeyStorage;
|
|
216
|
+
options?: SvelteKitApiKeyServerOptions;
|
|
217
|
+
};
|
|
218
|
+
oAuthAuthServer?: {
|
|
219
|
+
clientStorage: OAuthClientStorage;
|
|
220
|
+
keyStorage: KeyStorage;
|
|
221
|
+
options?: SvelteKitAuthorizationServerOptions;
|
|
222
|
+
};
|
|
223
|
+
oAuthClient?: {
|
|
224
|
+
authServerBaseUrl: string;
|
|
225
|
+
options?: SvelteKitOAuthClientOptions;
|
|
226
|
+
};
|
|
227
|
+
oAuthClients?: {
|
|
228
|
+
authServerBaseUrl: string;
|
|
229
|
+
options?: SvelteKitOAuthClientOptions;
|
|
230
|
+
}[];
|
|
231
|
+
oAuthResServer?: {
|
|
232
|
+
options?: SvelteKitOAuthResourceServerOptions;
|
|
233
|
+
};
|
|
234
|
+
options?: SvelteKitServerOptions;
|
|
235
|
+
});
|
|
236
|
+
/**
|
|
237
|
+
* See class documentation for {@link SvelteKitUserEndpoints}.
|
|
238
|
+
*
|
|
239
|
+
* This is an empty `load` which serves no purpose other than to stop
|
|
240
|
+
* Typescript complaining that `load` may be undefined.
|
|
241
|
+
* @param _event Sveltekit event object
|
|
242
|
+
* @returns an empty object
|
|
243
|
+
*/
|
|
244
|
+
dummyLoad: (event: RequestEvent) => Promise<{
|
|
245
|
+
[key: string]: any;
|
|
246
|
+
}>;
|
|
247
|
+
/**
|
|
248
|
+
* See class documentation for {@link SvelteKitUserEndpoints}.
|
|
249
|
+
*
|
|
250
|
+
* This is an empty `load` which serves no purpose other than to stop
|
|
251
|
+
* Typescript complaining that `actions` may be undefined.
|
|
252
|
+
* @returns an empty object
|
|
253
|
+
*/
|
|
254
|
+
dummyActions: {
|
|
255
|
+
[key: string]: (event: RequestEvent) => Promise<{
|
|
256
|
+
[key: string]: any;
|
|
257
|
+
}>;
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* See class documentation for {@link SvelteKitUserEndpoints}.
|
|
261
|
+
*
|
|
262
|
+
* This is an empty `bff action` which serves no purpose other than to stop
|
|
263
|
+
* Typescript complaining that the action may be undefined.
|
|
264
|
+
* @param _event Sveltekit event object
|
|
265
|
+
* @returns an empty object
|
|
266
|
+
*/
|
|
267
|
+
dummyBff: (event: RequestEvent) => Promise<{
|
|
268
|
+
[key: string]: any;
|
|
269
|
+
}>;
|
|
270
|
+
/**
|
|
271
|
+
* It is not possible to get any meaninfgul info about an exception class
|
|
272
|
+
* with `typeof` or `instanceof`. This method heuristically determines
|
|
273
|
+
* if an exception is a Sveltekit redirect. It is used internally
|
|
274
|
+
* @param e an exception
|
|
275
|
+
* @returns true or false
|
|
276
|
+
*/
|
|
277
|
+
static isSvelteKitRedirect(e: any): boolean;
|
|
278
|
+
/**
|
|
279
|
+
* It is not possible to get any meaninfgul info about an exception class
|
|
280
|
+
* with `typeof` or `instanceof`. This method heuristically determines
|
|
281
|
+
* if an exception is a Sveltekit error. It is used internally
|
|
282
|
+
* @param e an exception
|
|
283
|
+
* @returns true or false
|
|
284
|
+
*/
|
|
285
|
+
static isSvelteKitError(e: any, status?: number): boolean;
|
|
286
|
+
}
|