@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.
@@ -0,0 +1,289 @@
1
+ import { SvelteKitSessionServer, SvelteKitSessionServerOptions } from './sveltekitsession';
2
+ import { OAuthClientManager, OAuthClientStorage } from '@crossauth/backend';
3
+ import { OAuthClient, CrossauthError } from '@crossauth/common';
4
+ import { RequestEvent } from '@sveltejs/kit';
5
+
6
+ /**
7
+ * Return type for {@link SvelteKitUserClientEndpoints.searchClient}
8
+ * {@link SvelteKitAdminClientEndpoints.searchClient} load.
9
+ *
10
+ * See class documentation for {@link SvelteKitSharedClientEndpoints} for more details.
11
+ */
12
+ export type SearchClientsPageData = {
13
+ ok: boolean;
14
+ clients?: OAuthClient[];
15
+ skip: number;
16
+ take: number;
17
+ search?: string;
18
+ error?: string;
19
+ exception?: CrossauthError;
20
+ hasPrevious: boolean;
21
+ hasNext: boolean;
22
+ clientUserId?: string | number;
23
+ };
24
+ /**
25
+ * Return type for {@link SvelteKitUserClientEndpoints.updateClientEndpoint}
26
+ * {@link SvelteKitAdminClientEndpoints.updateClientEndpoint} load.
27
+ *
28
+ * See class documentation for {@link SvelteKitSharedClientEndpoints} for more details.
29
+ */
30
+ export type UpdateClientPageData = {
31
+ ok: boolean;
32
+ client?: OAuthClient;
33
+ client_id?: string;
34
+ clientUsername?: string;
35
+ error?: string;
36
+ exception?: CrossauthError;
37
+ validFlows: string[];
38
+ valid_flowNames: {
39
+ [key: string]: string;
40
+ };
41
+ };
42
+ /**
43
+ * Return type for {@link SvelteKitUserClientEndpoints.updateClientEndpoint}
44
+ * {@link SvelteKitAdminClientEndpoints.updateClienEndpoint} actions.
45
+ *
46
+ * See class documentation for {@link SvelteKitSharedClientEndpoints} for more details.
47
+ */
48
+ export type UpdateClientFormData = {
49
+ ok: boolean;
50
+ client?: OAuthClient;
51
+ error?: string;
52
+ exception?: CrossauthError;
53
+ formData?: {
54
+ [key: string]: string;
55
+ };
56
+ plaintextSecret?: string;
57
+ };
58
+ /**
59
+ * Return type for {@link SvelteKitUserClientEndpoints.createClientEndpoints}
60
+ * {@link SvelteKitAdminClientEndpoints.createClient} load.
61
+ *
62
+ * See class documentation for {@link SvelteKitSharedClientEndpoints} for more details.
63
+ */
64
+ export type CreateClientPageData = {
65
+ ok: boolean;
66
+ clientUserId?: string | number;
67
+ clientUsername?: string;
68
+ error?: string;
69
+ exception?: CrossauthError;
70
+ validFlows: string[];
71
+ valid_flowNames: {
72
+ [key: string]: string;
73
+ };
74
+ };
75
+ /**
76
+ * Return type for {@link SvelteKitUserClientEndpoints.createClientEndpoint}
77
+ * {@link SvelteKitAdminClientEndpoints.createClientEndpoint} actions.
78
+ *
79
+ * See class documentation for {@link SvelteKitSharedClientEndpoints} for more details.
80
+ */
81
+ export type CreateClientFormData = {
82
+ ok: boolean;
83
+ client?: OAuthClient;
84
+ error?: string;
85
+ exception?: CrossauthError;
86
+ formData?: {
87
+ [key: string]: string;
88
+ };
89
+ };
90
+ /**
91
+ * Return type for {@link SvelteKitUserClientEndpoints.deleteClientEndpoint}
92
+ * {@link SvelteKitAdminClientEndpoints.deleteClientEndpoint} load.
93
+ *
94
+ * See class documentation for {@link SvelteKitSharedClientEndpoints} for more details.
95
+ */
96
+ export type DeleteClientPageData = {
97
+ ok: boolean;
98
+ client?: OAuthClient;
99
+ client_id?: string;
100
+ clientUsername?: string;
101
+ error?: string;
102
+ exception?: CrossauthError;
103
+ };
104
+ /**
105
+ * Return type for {@link SvelteKitUserClientEndpoints.deleteClientEndpoint}
106
+ * {@link SvelteKitAdminClientEndpoints.deleteClientEndpoint} actions.
107
+ *
108
+ * See class documentation for {@link SvelteKitSharedClientEndpoints} for more details.
109
+ */
110
+ export type DeleteClientFormData = {
111
+ ok: boolean;
112
+ error?: string;
113
+ exception?: CrossauthError;
114
+ };
115
+ /**
116
+ * The `selectclient` and `admin/selectclient` endpoints have a customisable
117
+ * function for searching for a client. This is the default
118
+ * @param searchTerm the search term passed in the query string
119
+ * @param clientStorage the client storage to search
120
+ * @param userid the user id to se3arch for, or null for clients not owned
121
+ * by a user
122
+ * @returns An array of matching {@link @crossauth/common!OAuthClient} objects,
123
+ */
124
+ export declare function defaultClientSearchFn(searchTerm: string, clientStorage: OAuthClientStorage, skip: number, _take: number, userid?: string | number | null): Promise<OAuthClient[]>;
125
+ /**
126
+ * Base class for user and admin endpoints that manipulate the OAuth
127
+ * clients table
128
+ */
129
+ export declare class SvelteKitSharedClientEndpoints {
130
+ /**
131
+ * The session server that instantiated this.
132
+ *
133
+ * Set in the constructor
134
+ */
135
+ protected sessionServer: SvelteKitSessionServer;
136
+ /**
137
+ * The login URL taken from the {@link SvelteKitSessionServerOptions}
138
+ * in the constructor.
139
+ */
140
+ protected loginUrl: string;
141
+ /**
142
+ * Function for searching the client table. Default is to make
143
+ * an exact match search on `client_name`.
144
+ */
145
+ protected clientSearchFn: (searchTerm: string, clientStorage: OAuthClientStorage, skip: number, take: number, userid?: string | number | null) => Promise<OAuthClient[]>;
146
+ /**
147
+ * The redirect function taken from the {@link SvelteKitSessionServerOptions}
148
+ * in the constructor.
149
+ */
150
+ protected redirect: any;
151
+ /**
152
+ * The error function taken from the {@link SvelteKitSessionServerOptions}
153
+ * in the constructor.
154
+ */
155
+ protected error: any;
156
+ /**
157
+ * Taken from the {@link SvelteKitSessionServerOptions}
158
+ * in the constructor.
159
+ */
160
+ protected validFlows: string[];
161
+ /**
162
+ * Friendly names for `validFlows`
163
+ */
164
+ protected valid_flowNames: {
165
+ [key: string]: string;
166
+ };
167
+ /**
168
+ * The OAuth client manager instantiated during construction
169
+ */
170
+ protected clientManager: OAuthClientManager;
171
+ /**
172
+ * Taken from the {@link SvelteKitSessionServerOptions}
173
+ * in the constructor.
174
+ */
175
+ protected clientStorage?: OAuthClientStorage;
176
+ /**
177
+ * Constructor
178
+ *
179
+ * @param sessionServer the session server to add these endpoints to
180
+ * @param options See {@link SvelteKitSessionServerOptions}
181
+ */
182
+ constructor(sessionServer: SvelteKitSessionServer, options: SvelteKitSessionServerOptions);
183
+ /**
184
+ * Returns either a list of all clients for the user matching a search term.
185
+ *
186
+ * The returned list is pagenaed using the `skip` and `take` parameters.
187
+ *
188
+ * The searching is done with `clientSearchFn` that was passed in the
189
+ * options (see {@link SvelteKitSessionServerOptions }). THe default
190
+ * is an exact username match.
191
+ *
192
+ * By default, the searh and pagination parameters are taken from
193
+ * the query parameters in the request but can be overridden.
194
+ *
195
+ * @param event the Sveltekit request event. The following query parameters
196
+ * are read:
197
+ * - `search` the search term which is ignored if it is undefined, null
198
+ * or the empty string.
199
+ * - `skip` the number to start returning from. 0 if not defined
200
+ * - `take` the maximum number to return. 10 if not defined.
201
+ * @param search overrides the search term from the query.
202
+ * @param skip overrides the skip term from the query
203
+ * @param take overrides the take term from the query
204
+ *
205
+ * @return an object with the following members:
206
+ * - `ok` true or false depending on whether there was an error
207
+ * - `clients` the matching array of clients
208
+ * - `error` error message if `ok` is false
209
+ * - `exception` a {@link @crossauth/common!CrossauthError} if there was
210
+ * an error.
211
+ * - `search` the search term that was used
212
+ * - `skip` the skip term that was used
213
+ * - `take` the take term that was used
214
+ * - `hasNext` whether there are still more results after the ones that
215
+ * were returned
216
+ * - `hasPrevious` whether there are more results before the ones that
217
+ * were returned.
218
+ */
219
+ searchClients_internal(event: RequestEvent, searchTerm?: string, skip?: number, take?: number, userid?: string | number): Promise<SearchClientsPageData>;
220
+ /**
221
+ * The base class of the load function for updating an OAuth client.
222
+ *
223
+ * @param event the Sveltekit request event. The following are taken:
224
+ * - `client_id` from the URL path parameters
225
+ * @returns {@see UpdateClientPageData}
226
+ */
227
+ protected loadClient_internal(event: RequestEvent): Promise<UpdateClientPageData>;
228
+ /**
229
+ * The base class of the actions function for updating an OAuth client.
230
+ *
231
+ * @param event the Sveltekit request event. The following are taken:
232
+ * - `client_id` from the URL path parameters
233
+ * - `client_name` from the body form data
234
+ * - `redirect_uri` from the body form data (space-separated)
235
+ * - `confidential` from the body form data: 1, `on`, `yes` or `true` are true
236
+ * _ `resetSecret` if true (1, `on`, `yes` or `true`), create and return a new secret. Ignored if not confidential
237
+ * - Flow names from {@link @crossauth/common/OAuthFlows} taken from the body form data. 1, `on`, `yes` or `true` are true
238
+ * @returns {@see UpdateClientFormData}. If a new secret was created, it will be placed as plaintext in the client that is returned.
239
+ */
240
+ protected updateClient_internal(event: RequestEvent, isAdmin: boolean): Promise<UpdateClientFormData>;
241
+ /**
242
+ * The base class of the load function for creating an OAuth client.
243
+ *
244
+ * @param event the Sveltekit request event. The following are taken:
245
+ * - `userid` from the body parameters parameters. Ignored if `isAdmin` is false. Can be undefined
246
+ * -
247
+ * @returns {@see CreateClientPageData}.
248
+ */
249
+ protected emptyClient_internal(event: RequestEvent, isAdmin: boolean): Promise<CreateClientPageData>;
250
+ /**
251
+ * The base class of the actions function for creating an OAuth client.
252
+ *
253
+ * @param event the Sveltekit request event. The following are taken:
254
+ * - `userid` from the URL query parameters. Ignored if `isAdmin` is false. Can be undefined
255
+ * - `client_name` from the body form data
256
+ * - `redirect_uri` from the body form data (space-separated)
257
+ * - `confidential` from the body form data: 1, `on`, `yes` or `true` are true
258
+ * - Flow names from {@link @crossauth/common/OAuthFlows} taken from the body form data. 1, `on`, `yes` or `true` are true
259
+ * @returns {@see UpdateClientFormData}. If a secret was created, it will be placed as plaintext in the client that is returned. A random `client_id` is created.
260
+ */
261
+ protected createClient_internal(event: RequestEvent, isAdmin: boolean): Promise<CreateClientFormData>;
262
+ /**
263
+ * The base class of the load function for deleting an OAuth client.
264
+ *
265
+ * @param event the Sveltekit request event. The following are taken:
266
+ * - `client_id` from the URL path parameters
267
+ * @returns {@see DeleteClientPageData}
268
+ */
269
+ protected loadDeleteClient_internal(event: RequestEvent): Promise<DeleteClientPageData>;
270
+ /**
271
+ * The base class of the actions function for deleting an OAuth client.
272
+ *
273
+ * @param event the Sveltekit request event. The following are taken:
274
+ * - `client_id` from the URL path parameters
275
+ * @returns {@see DeleteClientFormData}
276
+ */
277
+ protected deleteClient_internal(event: RequestEvent, isAdmin: boolean): Promise<DeleteClientFormData>;
278
+ /**
279
+ * Returned by all endpoitns
280
+ * @param event the sveltekit request event
281
+ * @returns object with
282
+ * - `user` - the logged in user
283
+ * - `csrfToken` the CSRF token if using
284
+ */
285
+ baseEndpoint(event: RequestEvent): {
286
+ user: import('@crossauth/common').User | undefined;
287
+ csrfToken: string | undefined;
288
+ };
289
+ }
@@ -0,0 +1,151 @@
1
+ import { SvelteKitSessionServer, SvelteKitSessionServerOptions } from './sveltekitsession';
2
+ import { RequestEvent } from '@sveltejs/kit';
3
+ import { SearchClientsPageData, UpdateClientPageData, UpdateClientFormData, DeleteClientPageData, DeleteClientFormData, SvelteKitSharedClientEndpoints } from './sveltekitsharedclientendpoints';
4
+ import { CrossauthError } from '@crossauth/common';
5
+
6
+ /**
7
+ * Endpoints for manipulating the OAuth client table, for use by users.
8
+ *
9
+ * You do not instantiate this directly - it is created when you create
10
+ * a {@link SvelteKitServer}.
11
+ *
12
+ * **Endpoints**
13
+ *
14
+ * These endpoints can only be called if an admin user is logged in, as defined
15
+ * by the {@link SveltekitSessionServer.isAdminFn}. If the user does not
16
+ * have this permission, a 401 error is raised.
17
+ *
18
+ * | Name | Description | PageData (returned by load) | ActionData (return by actions) | Form fields expected by actions | URL param |
19
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
20
+ * | baseEndpoint | This PageData is returned by all endpoints' load function. | - `user` logged in {@link @crossauth/common!User} | *Not provided* | | |
21
+ * | | | - `csrfToken` CSRF token if enabled | | | | | loginPage |
22
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
23
+ * | searchClientsEndpoint | Returns a paginated set of clients or those matching search | See {@link SearchClientsPageData} | *Not provided* | | |
24
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
25
+ * | updateClientEndpoint | Updates a client | See {@link UpdateClientsPageData} | `default`: | | |
26
+ * | | | | See {@link UpdateClientsFormData} | See {@link SvelteKitSharedClientEndpoints.updateClient_internal} | client_id |
27
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
28
+ * | createClientEndpoint | Creates a new client | See {@link CreateClientsPageData} | `default`: | | |
29
+ * | | | | See {@link CreateClientsFormData} | See {@link SvelteKitSharedClientEndpoints.createClient_internal} | client_id |
30
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
31
+ * | deleteClientEndpoint | Deletes a client | See {@link DeleteClientsPageData} | `default`: | | |
32
+ * | | | | See {@link DeleteClientsFormData} | See {@link SvelteKitSharedClientEndpoints.deleteClient_internal} | client_id |
33
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
34
+ */
35
+ export declare class SvelteKitUserClientEndpoints extends SvelteKitSharedClientEndpoints {
36
+ /**
37
+ * Constructor
38
+ * @param sessionServer the session server which will have these endpoints
39
+ * @param options See {@link SvelteKitSessionServerOptions}.
40
+ */
41
+ constructor(sessionServer: SvelteKitSessionServer, options: SvelteKitSessionServerOptions);
42
+ /**
43
+ * See {@link SvelteKitSharedClientEndpoints.searchClients_internal}
44
+ */
45
+ searchClients(event: RequestEvent, searchTerm?: string, skip?: number, take?: number): Promise<SearchClientsPageData>;
46
+ /**
47
+ * See {@link SvelteKitSharedClientEndpoints.loadClient_internal}
48
+ */
49
+ loadClient(event: RequestEvent): Promise<UpdateClientPageData>;
50
+ /**
51
+ * See {@link SvelteKitSharedClientEndpoints.updateClient_internal}
52
+ */
53
+ updateClient(event: RequestEvent): Promise<UpdateClientFormData>;
54
+ /**
55
+ * See {@link SvelteKitSharedClientEndpoints.loadDeleteClient_internal}
56
+ */
57
+ loadDeleteClient(event: RequestEvent): Promise<DeleteClientPageData>;
58
+ /**
59
+ * See {@link SvelteKitSharedClientEndpoints.deleteClient_internal}
60
+ */
61
+ deleteClient(event: RequestEvent): Promise<DeleteClientFormData>;
62
+ /**
63
+ * See {@link SvelteKitSharedClientEndpoints.emptyClient_internal}
64
+ */
65
+ emptyClient(event: RequestEvent): Promise<UpdateClientPageData>;
66
+ /**
67
+ * See {@link SvelteKitSharedClientEndpoints.createClient_internal}
68
+ */
69
+ createClient(event: RequestEvent): Promise<UpdateClientFormData>;
70
+ /**
71
+ * See class documentation.
72
+ */
73
+ readonly searchClientsEndpoint: {
74
+ load: (event: RequestEvent) => Promise<{
75
+ ok: boolean;
76
+ clients?: import('@crossauth/common').OAuthClient[] | undefined;
77
+ skip: number;
78
+ take: number;
79
+ search?: string | undefined;
80
+ error?: string | undefined;
81
+ exception?: CrossauthError | undefined;
82
+ hasPrevious: boolean;
83
+ hasNext: boolean;
84
+ clientUserId?: string | number | undefined;
85
+ user: import('@crossauth/common').User | undefined;
86
+ csrfToken: string | undefined;
87
+ }>;
88
+ };
89
+ /**
90
+ * See class documentation.
91
+ */
92
+ readonly updateClientEndpoint: {
93
+ load: (event: RequestEvent) => Promise<{
94
+ ok: boolean;
95
+ client?: import('@crossauth/common').OAuthClient | undefined;
96
+ client_id?: string | undefined;
97
+ clientUsername?: string | undefined;
98
+ error?: string | undefined;
99
+ exception?: CrossauthError | undefined;
100
+ validFlows: string[];
101
+ valid_flowNames: {
102
+ [key: string]: string;
103
+ };
104
+ user: import('@crossauth/common').User | undefined;
105
+ csrfToken: string | undefined;
106
+ }>;
107
+ actions: {
108
+ default: (event: RequestEvent) => Promise<UpdateClientFormData>;
109
+ };
110
+ };
111
+ /**
112
+ * See class documentation.
113
+ */
114
+ readonly createClientEndpoint: {
115
+ load: (event: RequestEvent) => Promise<{
116
+ ok: boolean;
117
+ client?: import('@crossauth/common').OAuthClient | undefined;
118
+ client_id?: string | undefined;
119
+ clientUsername?: string | undefined;
120
+ error?: string | undefined;
121
+ exception?: CrossauthError | undefined;
122
+ validFlows: string[];
123
+ valid_flowNames: {
124
+ [key: string]: string;
125
+ };
126
+ user: import('@crossauth/common').User | undefined;
127
+ csrfToken: string | undefined;
128
+ }>;
129
+ actions: {
130
+ default: (event: RequestEvent) => Promise<UpdateClientFormData>;
131
+ };
132
+ };
133
+ /**
134
+ * See class documentation.
135
+ */
136
+ readonly deleteClientEndpoint: {
137
+ load: (event: RequestEvent) => Promise<{
138
+ ok: boolean;
139
+ client?: import('@crossauth/common').OAuthClient | undefined;
140
+ client_id?: string | undefined;
141
+ clientUsername?: string | undefined;
142
+ error?: string | undefined;
143
+ exception?: CrossauthError | undefined;
144
+ user: import('@crossauth/common').User | undefined;
145
+ csrfToken: string | undefined;
146
+ }>;
147
+ actions: {
148
+ default: (event: RequestEvent) => Promise<DeleteClientFormData>;
149
+ };
150
+ };
151
+ }