@crossauth/sveltekit 1.1.0 → 1.1.1

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 (46) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.js +16 -6181
  3. package/dist/sveltekitadminclientendpoints.d.ts +13 -12
  4. package/dist/sveltekitadminclientendpoints.js +187 -0
  5. package/dist/sveltekitadminendpoints.d.ts +5 -4
  6. package/dist/sveltekitadminendpoints.js +766 -0
  7. package/dist/sveltekitapikey.d.ts +4 -4
  8. package/dist/sveltekitapikey.js +81 -0
  9. package/dist/sveltekitoauthclient.d.ts +6 -5
  10. package/dist/sveltekitoauthclient.js +2309 -0
  11. package/dist/sveltekitoauthserver.d.ts +4 -4
  12. package/dist/sveltekitoauthserver.js +1350 -0
  13. package/dist/sveltekitresserver.d.ts +6 -5
  14. package/dist/sveltekitresserver.js +286 -0
  15. package/dist/sveltekitserver.d.ts +11 -10
  16. package/dist/sveltekitserver.js +393 -0
  17. package/dist/sveltekitsession.d.ts +5 -5
  18. package/dist/sveltekitsession.js +1112 -0
  19. package/dist/sveltekitsessionadapter.d.ts +2 -3
  20. package/dist/sveltekitsessionadapter.js +2 -0
  21. package/dist/sveltekitsharedclientendpoints.d.ts +7 -6
  22. package/dist/sveltekitsharedclientendpoints.js +630 -0
  23. package/dist/sveltekituserclientendpoints.d.ts +13 -12
  24. package/dist/sveltekituserclientendpoints.js +270 -0
  25. package/dist/sveltekituserendpoints.d.ts +6 -5
  26. package/dist/sveltekituserendpoints.js +1813 -0
  27. package/dist/tests/sveltekitadminclientendpoints.test.js +330 -0
  28. package/dist/tests/sveltekitadminendpoints.test.js +242 -0
  29. package/dist/tests/sveltekitapikeyserver.test.js +44 -0
  30. package/dist/tests/sveltekitoauthclient.test.d.ts +5 -5
  31. package/dist/tests/sveltekitoauthclient.test.js +1016 -0
  32. package/dist/tests/sveltekitoauthresserver.test.d.ts +4 -4
  33. package/dist/tests/sveltekitoauthresserver.test.js +185 -0
  34. package/dist/tests/sveltekitoauthserver.test.js +673 -0
  35. package/dist/tests/sveltekituserclientendpoints.test.js +244 -0
  36. package/dist/tests/sveltekituserendpoints.test.js +152 -0
  37. package/dist/tests/sveltemock.test.js +36 -0
  38. package/dist/tests/sveltemocks.d.ts +2 -3
  39. package/dist/tests/sveltemocks.js +114 -0
  40. package/dist/tests/sveltesessionhooks.test.js +224 -0
  41. package/dist/tests/testshared.d.ts +8 -8
  42. package/dist/tests/testshared.js +344 -0
  43. package/dist/utils.d.ts +1 -2
  44. package/dist/utils.js +123 -0
  45. package/package.json +6 -4
  46. package/dist/index.cjs +0 -1
@@ -1,7 +1,8 @@
1
- import { SvelteKitSessionServer, SvelteKitSessionServerOptions } from './sveltekitsession';
2
- import { RequestEvent } from '@sveltejs/kit';
3
- import { SearchClientsPageData, UpdateClientPageData, UpdateClientFormData, DeleteClientPageData, DeleteClientFormData, SvelteKitSharedClientEndpoints } from './sveltekitsharedclientendpoints';
4
-
1
+ import { SvelteKitSessionServer } from './sveltekitsession';
2
+ import type { SvelteKitSessionServerOptions } from './sveltekitsession';
3
+ import type { RequestEvent } from '@sveltejs/kit';
4
+ import type { SearchClientsPageData, UpdateClientPageData, UpdateClientFormData, DeleteClientPageData, DeleteClientFormData } from './sveltekitsharedclientendpoints';
5
+ import { SvelteKitSharedClientEndpoints } from './sveltekitsharedclientendpoints';
5
6
  /**
6
7
  * Endpoints for manipulating the OAuth client table, for use by users.
7
8
  *
@@ -72,7 +73,7 @@ export declare class SvelteKitUserClientEndpoints extends SvelteKitSharedClientE
72
73
  readonly searchClientsEndpoint: {
73
74
  load: (event: RequestEvent) => Promise<{
74
75
  ok: boolean;
75
- clients?: import('@crossauth/common').OAuthClient[] | undefined;
76
+ clients?: import("@crossauth/common").OAuthClient[] | undefined;
76
77
  skip: number;
77
78
  take: number;
78
79
  search?: string | undefined;
@@ -82,7 +83,7 @@ export declare class SvelteKitUserClientEndpoints extends SvelteKitSharedClientE
82
83
  hasPrevious: boolean;
83
84
  hasNext: boolean;
84
85
  clientUserId?: string | number | undefined;
85
- user: import('@crossauth/common').User | undefined;
86
+ user: import("@crossauth/common").User | undefined;
86
87
  csrfToken: string | undefined;
87
88
  }>;
88
89
  };
@@ -92,7 +93,7 @@ export declare class SvelteKitUserClientEndpoints extends SvelteKitSharedClientE
92
93
  readonly updateClientEndpoint: {
93
94
  load: (event: RequestEvent) => Promise<{
94
95
  ok: boolean;
95
- client?: import('@crossauth/common').OAuthClient | undefined;
96
+ client?: import("@crossauth/common").OAuthClient | undefined;
96
97
  client_id?: string | undefined;
97
98
  clientUsername?: string | undefined;
98
99
  error?: string | undefined;
@@ -102,7 +103,7 @@ export declare class SvelteKitUserClientEndpoints extends SvelteKitSharedClientE
102
103
  valid_flowNames: {
103
104
  [key: string]: string;
104
105
  };
105
- user: import('@crossauth/common').User | undefined;
106
+ user: import("@crossauth/common").User | undefined;
106
107
  csrfToken: string | undefined;
107
108
  }>;
108
109
  actions: {
@@ -115,7 +116,7 @@ export declare class SvelteKitUserClientEndpoints extends SvelteKitSharedClientE
115
116
  readonly createClientEndpoint: {
116
117
  load: (event: RequestEvent) => Promise<{
117
118
  ok: boolean;
118
- client?: import('@crossauth/common').OAuthClient | undefined;
119
+ client?: import("@crossauth/common").OAuthClient | undefined;
119
120
  client_id?: string | undefined;
120
121
  clientUsername?: string | undefined;
121
122
  error?: string | undefined;
@@ -125,7 +126,7 @@ export declare class SvelteKitUserClientEndpoints extends SvelteKitSharedClientE
125
126
  valid_flowNames: {
126
127
  [key: string]: string;
127
128
  };
128
- user: import('@crossauth/common').User | undefined;
129
+ user: import("@crossauth/common").User | undefined;
129
130
  csrfToken: string | undefined;
130
131
  }>;
131
132
  actions: {
@@ -138,13 +139,13 @@ export declare class SvelteKitUserClientEndpoints extends SvelteKitSharedClientE
138
139
  readonly deleteClientEndpoint: {
139
140
  load: (event: RequestEvent) => Promise<{
140
141
  ok: boolean;
141
- client?: import('@crossauth/common').OAuthClient | undefined;
142
+ client?: import("@crossauth/common").OAuthClient | undefined;
142
143
  client_id?: string | undefined;
143
144
  clientUsername?: string | undefined;
144
145
  error?: string | undefined;
145
146
  errorCode?: number | undefined;
146
147
  errorCodeName?: string | undefined;
147
- user: import('@crossauth/common').User | undefined;
148
+ user: import("@crossauth/common").User | undefined;
148
149
  csrfToken: string | undefined;
149
150
  }>;
150
151
  actions: {
@@ -0,0 +1,270 @@
1
+ // Copyright (c) 2026 Matthew Baker. All rights reserved. Licenced under the Apache Licence 2.0. See LICENSE file
2
+ import { SvelteKitServer } from './sveltekitserver';
3
+ import { SvelteKitSessionServer } from './sveltekitsession';
4
+ import { setParameter, ParamType, } from '@crossauth/backend';
5
+ import { SvelteKitSharedClientEndpoints } from './sveltekitsharedclientendpoints';
6
+ import { CrossauthError, j, CrossauthLogger, ErrorCode } from '@crossauth/common';
7
+ //////////////////////////////////////////////////////////////////////
8
+ // Class
9
+ /**
10
+ * Endpoints for manipulating the OAuth client table, for use by users.
11
+ *
12
+ * You do not instantiate this directly - it is created when you create
13
+ * a {@link SvelteKitServer}.
14
+ *
15
+ * **Endpoints**
16
+ *
17
+ * These endpoints can only be called if an admin user is logged in, as defined
18
+ * by the {@link SvelteKitServer.isAdminFn}. If the user does not
19
+ * have this permission, a 401 error is raised.
20
+ *
21
+ * | Name | Description | PageData (returned by load) | ActionData (return by actions) | Form fields expected by actions | URL param |
22
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
23
+ * | baseEndpoint | This PageData is returned by all endpoints' load function. | - `user` logged in {@link @crossauth/common!User} | *Not provided* | | |
24
+ * | | | - `csrfToken` CSRF token if enabled | | | | | loginPage |
25
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
26
+ * | searchClientsEndpoint | Returns a paginated set of clients or those matching search | See {@link SearchClientsPageData} | *Not provided* | | |
27
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
28
+ * | updateClientEndpoint | Updates a client | See {@link UpdateClientPageData} | `default`: | | |
29
+ * | | | | See {@link UpdateClientFormData} | See {@link SvelteKitSharedClientEndpoints.updateClient_internal} | client_id |
30
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
31
+ * | createClientEndpoint | Creates a new client | See {@link CreateClientPageData} | `default`: | | |
32
+ * | | | | See {@link CreateClientFormData} | See {@link SvelteKitSharedClientEndpoints.createClient_internal} | client_id |
33
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
34
+ * | deleteClientEndpoint | Deletes a client | See {@link DeleteClientPageData} | `default`: | | |
35
+ * | | | | See {@link DeleteClientFormData} | See {@link SvelteKitSharedClientEndpoints.deleteClient_internal} | client_id |
36
+ * | -------------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- | --------- |
37
+ */
38
+ export class SvelteKitUserClientEndpoints extends SvelteKitSharedClientEndpoints {
39
+ /**
40
+ * Constructor
41
+ * @param sessionServer the session server which will have these endpoints
42
+ * @param options See {@link SvelteKitSessionServerOptions}.
43
+ */
44
+ constructor(sessionServer, options) {
45
+ super(sessionServer, options);
46
+ this.sessionServer = sessionServer;
47
+ setParameter("loginUrl", ParamType.JsonArray, this, options, "LOGIN_URL");
48
+ if (options.clientSearchFn)
49
+ this.clientSearchFn = options.clientSearchFn;
50
+ this.redirect = options.redirect;
51
+ this.error = options.error;
52
+ }
53
+ ///////////////////////////////////////////////////////////////////
54
+ // Functions callable from apps
55
+ /**
56
+ * See {@link SvelteKitSharedClientEndpoints.searchClients_internal}
57
+ */
58
+ async searchClients(event, searchTerm, skip, take) {
59
+ if (!event.locals.user)
60
+ throw this.redirect(302, this.loginUrl + "?next=" + encodeURIComponent(event.request.url));
61
+ return this.searchClients_internal(event, searchTerm, skip, take, event.locals.user?.id);
62
+ }
63
+ /**
64
+ * See {@link SvelteKitSharedClientEndpoints.loadClient_internal}
65
+ */
66
+ async loadClient(event) {
67
+ if (!event.locals.user)
68
+ throw this.redirect(302, this.loginUrl + "?next=" + encodeURIComponent(event.request.url));
69
+ // check user owns client
70
+ try {
71
+ const client_id = event.params.client_id;
72
+ if (!client_id)
73
+ throw new CrossauthError(ErrorCode.BadRequest, "No client ID given");
74
+ const client = await this.clientStorage?.getClientById(client_id);
75
+ if (client?.userid != event.locals.user.id)
76
+ return this.error(401, "Access denied");
77
+ }
78
+ catch (e) {
79
+ if (SvelteKitServer.isSvelteKitRedirect(e) || SvelteKitServer.isSvelteKitError(e))
80
+ throw e;
81
+ const ce = CrossauthError.asCrossauthError(e);
82
+ CrossauthLogger.logger.debug(j({ err: ce }));
83
+ CrossauthLogger.logger.error(j({ cerr: ce }));
84
+ return {
85
+ ok: false,
86
+ error: ce.message,
87
+ errorCode: ce.code,
88
+ errorCodeName: ce.codeName,
89
+ validFlows: this.validFlows,
90
+ valid_flowNames: this.valid_flowNames,
91
+ };
92
+ }
93
+ return this.loadClient_internal(event);
94
+ }
95
+ /**
96
+ * See {@link SvelteKitSharedClientEndpoints.updateClient_internal}
97
+ */
98
+ async updateClient(event) {
99
+ if (!event.locals.user)
100
+ throw this.redirect(302, this.loginUrl + "?next=" + encodeURIComponent(event.request.url));
101
+ // check user owns client
102
+ try {
103
+ const client_id = event.params.client_id;
104
+ if (!client_id)
105
+ throw new CrossauthError(ErrorCode.BadRequest, "No client ID given");
106
+ const client = await this.clientStorage?.getClientById(client_id);
107
+ if (client?.userid != event.locals.user.id)
108
+ return this.error(401, "Access denied");
109
+ }
110
+ catch (e) {
111
+ if (SvelteKitServer.isSvelteKitRedirect(e) || SvelteKitServer.isSvelteKitError(e))
112
+ throw e;
113
+ const ce = CrossauthError.asCrossauthError(e);
114
+ CrossauthLogger.logger.debug(j({ err: ce }));
115
+ CrossauthLogger.logger.error(j({ cerr: ce }));
116
+ return {
117
+ ok: false,
118
+ error: ce.message,
119
+ errorCode: ce.code,
120
+ errorCodeName: ce.codeName,
121
+ };
122
+ }
123
+ return this.updateClient_internal(event, false);
124
+ }
125
+ /**
126
+ * See {@link SvelteKitSharedClientEndpoints.loadDeleteClient_internal}
127
+ */
128
+ async loadDeleteClient(event) {
129
+ if (!event.locals.user)
130
+ throw this.redirect(302, this.loginUrl + "?next=" + encodeURIComponent(event.request.url));
131
+ // check user owns client
132
+ try {
133
+ const client_id = event.params.client_id;
134
+ if (!client_id)
135
+ throw new CrossauthError(ErrorCode.BadRequest, "No client ID given");
136
+ const client = await this.clientStorage?.getClientById(client_id);
137
+ if (client?.userid != event.locals.user.id)
138
+ return this.error(401, "Access denied");
139
+ }
140
+ catch (e) {
141
+ if (SvelteKitServer.isSvelteKitRedirect(e) || SvelteKitServer.isSvelteKitError(e))
142
+ throw e;
143
+ const ce = CrossauthError.asCrossauthError(e);
144
+ CrossauthLogger.logger.debug(j({ err: ce }));
145
+ CrossauthLogger.logger.error(j({ cerr: ce }));
146
+ return {
147
+ ok: false,
148
+ error: ce.message,
149
+ errorCode: ce.code,
150
+ errorCodeName: ce.codeName,
151
+ };
152
+ }
153
+ return this.loadDeleteClient_internal(event);
154
+ }
155
+ /**
156
+ * See {@link SvelteKitSharedClientEndpoints.deleteClient_internal}
157
+ */
158
+ async deleteClient(event) {
159
+ if (!event.locals.user)
160
+ throw this.redirect(302, this.loginUrl + "?next=" + encodeURIComponent(event.request.url));
161
+ // check user owns client
162
+ try {
163
+ const client_id = event.params.client_id;
164
+ if (!client_id)
165
+ throw new CrossauthError(ErrorCode.BadRequest, "No client ID given");
166
+ const client = await this.clientStorage?.getClientById(client_id);
167
+ if (client?.userid != event.locals.user.id)
168
+ return this.error(401, "Access denied");
169
+ }
170
+ catch (e) {
171
+ if (SvelteKitServer.isSvelteKitRedirect(e) || SvelteKitServer.isSvelteKitError(e))
172
+ throw e;
173
+ const ce = CrossauthError.asCrossauthError(e);
174
+ CrossauthLogger.logger.debug(j({ err: ce }));
175
+ CrossauthLogger.logger.error(j({ cerr: ce }));
176
+ return {
177
+ ok: false,
178
+ error: ce.message,
179
+ errorCode: ce.code,
180
+ errorCodeName: ce.codeName,
181
+ };
182
+ }
183
+ return this.deleteClient_internal(event, false);
184
+ }
185
+ /**
186
+ * See {@link SvelteKitSharedClientEndpoints.emptyClient_internal}
187
+ */
188
+ async emptyClient(event) {
189
+ if (!event.locals.user)
190
+ throw this.redirect(302, this.loginUrl + "?next=" + encodeURIComponent(event.request.url));
191
+ return this.emptyClient_internal(event, false);
192
+ }
193
+ /**
194
+ * See {@link SvelteKitSharedClientEndpoints.createClient_internal}
195
+ */
196
+ async createClient(event) {
197
+ if (!event.locals.user)
198
+ throw this.redirect(302, this.loginUrl + "?next=" + encodeURIComponent(event.request.url));
199
+ return this.createClient_internal(event, false);
200
+ }
201
+ /////////////////////////////////////////////////////////////////
202
+ // Endpoints
203
+ /**
204
+ * See class documentation.
205
+ */
206
+ searchClientsEndpoint = {
207
+ load: async (event) => {
208
+ const resp = await this.searchClients(event);
209
+ return {
210
+ ...this.baseEndpoint(event),
211
+ ...resp,
212
+ };
213
+ },
214
+ };
215
+ /**
216
+ * See class documentation.
217
+ */
218
+ updateClientEndpoint = {
219
+ load: async (event) => {
220
+ const resp = await this.loadClient(event);
221
+ return {
222
+ ...this.baseEndpoint(event),
223
+ ...resp,
224
+ };
225
+ },
226
+ actions: {
227
+ default: async (event) => {
228
+ let resp = await this.updateClient(event);
229
+ return resp;
230
+ }
231
+ }
232
+ };
233
+ /**
234
+ * See class documentation.
235
+ */
236
+ createClientEndpoint = {
237
+ load: async (event) => {
238
+ const resp = await this.emptyClient(event);
239
+ return {
240
+ ...this.baseEndpoint(event),
241
+ ...resp,
242
+ };
243
+ },
244
+ actions: {
245
+ default: async (event) => {
246
+ let resp = await this.createClient(event);
247
+ return resp;
248
+ }
249
+ }
250
+ };
251
+ /**
252
+ * See class documentation.
253
+ */
254
+ deleteClientEndpoint = {
255
+ load: async (event) => {
256
+ const resp = await this.loadDeleteClient(event);
257
+ return {
258
+ ...this.baseEndpoint(event),
259
+ ...resp,
260
+ };
261
+ },
262
+ actions: {
263
+ default: async (event) => {
264
+ let resp = await this.deleteClient(event);
265
+ return resp;
266
+ }
267
+ }
268
+ };
269
+ }
270
+ ;
@@ -1,8 +1,9 @@
1
- import { SveltekitEndpoint } from './sveltekitserver';
2
- import { SvelteKitSessionServer, SvelteKitSessionServerOptions } from './sveltekitsession';
3
- import { User, UserInputFields, ErrorCode } from '@crossauth/common';
4
- import { RequestEvent } from '@sveltejs/kit';
5
-
1
+ import { type SveltekitEndpoint } from './sveltekitserver';
2
+ import { SvelteKitSessionServer } from './sveltekitsession';
3
+ import type { SvelteKitSessionServerOptions } from './sveltekitsession';
4
+ import type { User, UserInputFields } from '@crossauth/common';
5
+ import { ErrorCode } from '@crossauth/common';
6
+ import type { RequestEvent } from '@sveltejs/kit';
6
7
  /**
7
8
  * Return type for {@link SvelteKitUserEndpoints.login},
8
9
  * {@link SvelteKitUserEndpoints.loginFactor2} and the