@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,7 @@
1
- import { UserStorage, KeyStorage, ApiKeyManagerOptions } from '@crossauth/backend';
2
- import { RequestEvent } from '@sveltejs/kit';
3
- import { MaybePromise } from './tests/sveltemocks';
4
-
1
+ import { UserStorage, KeyStorage } from '@crossauth/backend';
2
+ import type { ApiKeyManagerOptions } from '@crossauth/backend';
3
+ import type { RequestEvent } from '@sveltejs/kit';
4
+ import { type MaybePromise } from './tests/sveltemocks';
5
5
  /**
6
6
  * Options for {@link SvelteKitApiKeyServer }.
7
7
  *
@@ -0,0 +1,81 @@
1
+ // Copyright (c) 2026 Matthew Baker. All rights reserved. Licenced under the Apache Licence 2.0. See LICENSE file
2
+ import { ApiKeyManager, UserStorage, KeyStorage } from '@crossauth/backend';
3
+ import { CrossauthLogger, j } from '@crossauth/common';
4
+ import {} from './tests/sveltemocks';
5
+ /**
6
+ * This class adds API key functionality to the Fatify server.
7
+ *
8
+ * You shouldn't have to instantiate this directly. It is created
9
+ * when instantiating {@link SvelteKitServer} if enabling API key support-
10
+ *
11
+ * API keys are bearer tokens than have to be manually created for a user.
12
+ * They can be used in place of username/password login and session cookies.
13
+ *
14
+ * This class adds a `preHandler` hook that sets the `user` field in the
15
+ * SvelteKit request. It also sets `scopes` in the request object if there
16
+ * is a `scope` field in the JSON object in the `data` field in in the API
17
+ * record in key storage.
18
+ */
19
+ export class SvelteKitApiKeyServer {
20
+ userStorage;
21
+ apiKeyManager;
22
+ /**
23
+ * Hook to check if the user is logged in and set data in `locals`
24
+ * accordingly.
25
+ */
26
+ hook;
27
+ /**
28
+ * Constructor
29
+ *
30
+ * @param userStorage the user storage with user accounts
31
+ * @param keyStorage the storage for finding API keys
32
+ * @param options See {@link SvelteKitApiKeyServerOptions}
33
+ */
34
+ constructor(userStorage, keyStorage, options = {}) {
35
+ this.userStorage = userStorage;
36
+ this.apiKeyManager = new ApiKeyManager(keyStorage, options);
37
+ this.hook = async ({ event } /*, response*/) => {
38
+ CrossauthLogger.logger.debug("APIKey hook");
39
+ const authzHeader = event.request.headers.get("authorization");
40
+ if (authzHeader) {
41
+ try {
42
+ CrossauthLogger.logger.debug(j({
43
+ msg: "Received authorization header"
44
+ }));
45
+ const key = await this.apiKeyManager.validateToken(authzHeader);
46
+ CrossauthLogger.logger.debug(j({
47
+ msg: "Valid API key",
48
+ hahedApiKey: ApiKeyManager.hashSignedApiKeyValue(key.value)
49
+ }));
50
+ const data = KeyStorage.decodeData(key.data);
51
+ event.locals.apiKey = { ...key, ...data };
52
+ if ("scope" in data && Array.isArray(data.scope)) {
53
+ let scopes = [];
54
+ for (let scope of data.scope) {
55
+ if (typeof scope == "string")
56
+ scopes.push(scope);
57
+ }
58
+ event.locals.scope = scopes;
59
+ }
60
+ if (key.userid) {
61
+ try {
62
+ const { user } = await this.userStorage.getUserById(key.userid);
63
+ event.locals.user = user;
64
+ event.locals.authType = "apiKey";
65
+ CrossauthLogger.logger.debug(j({ msg: "API key is for user", userid: user.id, user: user.username, hahedApiKey: ApiKeyManager.hashSignedApiKeyValue(key.value) }));
66
+ }
67
+ catch (e2) {
68
+ CrossauthLogger.logger.error(j({ msg: "API key has invalid user", userid: key.userid, hashedApiKey: ApiKeyManager.hashSignedApiKeyValue(key.value) }));
69
+ CrossauthLogger.logger.debug(j({ err: e2 }));
70
+ }
71
+ }
72
+ }
73
+ catch (e) {
74
+ CrossauthLogger.logger.error(j({ msg: "Invalid authorization header received", header: authzHeader }));
75
+ CrossauthLogger.logger.debug(j({ err: e }));
76
+ }
77
+ }
78
+ ;
79
+ };
80
+ }
81
+ }
@@ -1,9 +1,10 @@
1
- import { CrossauthError, ErrorCode, OAuthTokenResponse, OAuthDeviceAuthorizationResponse, User } from '@crossauth/common';
2
- import { OAuthClientBackend, OAuthClientOptions } from '@crossauth/backend';
1
+ import { CrossauthError, ErrorCode } from '@crossauth/common';
2
+ import type { OAuthTokenResponse, OAuthDeviceAuthorizationResponse, User } from '@crossauth/common';
3
+ import { OAuthClientBackend } from '@crossauth/backend';
4
+ import type { OAuthClientOptions } from '@crossauth/backend';
3
5
  import { SvelteKitServer } from './sveltekitserver';
4
- import { RequestEvent } from '@sveltejs/kit';
5
- import { MaybePromise } from './tests/sveltemocks';
6
-
6
+ import type { RequestEvent } from '@sveltejs/kit';
7
+ import { type MaybePromise } from './tests/sveltemocks';
7
8
  export type SvelteKitErrorFn = (server: SvelteKitServer, event: RequestEvent, ce: CrossauthError) => Promise<Response>;
8
9
  /**
9
10
  * Options for {@link SvelteKitOAuthClient}.