@crossauth/sveltekit 1.0.1 → 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.
- package/README.md +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +16 -6181
- package/dist/sveltekitadminclientendpoints.d.ts +13 -12
- package/dist/sveltekitadminclientendpoints.js +187 -0
- package/dist/sveltekitadminendpoints.d.ts +5 -4
- package/dist/sveltekitadminendpoints.js +766 -0
- package/dist/sveltekitapikey.d.ts +4 -3
- package/dist/sveltekitapikey.js +81 -0
- package/dist/sveltekitoauthclient.d.ts +6 -4
- package/dist/sveltekitoauthclient.js +2309 -0
- package/dist/sveltekitoauthserver.d.ts +4 -4
- package/dist/sveltekitoauthserver.js +1350 -0
- package/dist/sveltekitresserver.d.ts +6 -4
- package/dist/sveltekitresserver.js +286 -0
- package/dist/sveltekitserver.d.ts +11 -9
- package/dist/sveltekitserver.js +393 -0
- package/dist/sveltekitsession.d.ts +6 -5
- package/dist/sveltekitsession.js +1112 -0
- package/dist/sveltekitsessionadapter.d.ts +2 -3
- package/dist/sveltekitsessionadapter.js +2 -0
- package/dist/sveltekitsharedclientendpoints.d.ts +7 -6
- package/dist/sveltekitsharedclientendpoints.js +630 -0
- package/dist/sveltekituserclientendpoints.d.ts +13 -12
- package/dist/sveltekituserclientendpoints.js +270 -0
- package/dist/sveltekituserendpoints.d.ts +6 -5
- package/dist/sveltekituserendpoints.js +1813 -0
- package/dist/tests/sveltekitadminclientendpoints.test.js +330 -0
- package/dist/tests/sveltekitadminendpoints.test.js +242 -0
- package/dist/tests/sveltekitapikeyserver.test.js +44 -0
- package/dist/tests/sveltekitoauthclient.test.d.ts +5 -5
- package/dist/tests/sveltekitoauthclient.test.js +1016 -0
- package/dist/tests/sveltekitoauthresserver.test.d.ts +4 -4
- package/dist/tests/sveltekitoauthresserver.test.js +185 -0
- package/dist/tests/sveltekitoauthserver.test.js +673 -0
- package/dist/tests/sveltekituserclientendpoints.test.js +244 -0
- package/dist/tests/sveltekituserendpoints.test.js +152 -0
- package/dist/tests/sveltemock.test.js +36 -0
- package/dist/tests/sveltemocks.d.ts +22 -8
- package/dist/tests/sveltemocks.js +114 -0
- package/dist/tests/sveltesessionhooks.test.js +224 -0
- package/dist/tests/testshared.d.ts +8 -8
- package/dist/tests/testshared.js +344 -0
- package/dist/utils.d.ts +1 -2
- package/dist/utils.js +123 -0
- package/package.json +23 -15
- package/dist/index.cjs +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { UserStorage, KeyStorage
|
|
2
|
-
import {
|
|
3
|
-
|
|
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';
|
|
4
5
|
/**
|
|
5
6
|
* Options for {@link SvelteKitApiKeyServer }.
|
|
6
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,8 +1,10 @@
|
|
|
1
|
-
import { CrossauthError, ErrorCode
|
|
2
|
-
import {
|
|
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
|
|
5
|
-
|
|
6
|
+
import type { RequestEvent } from '@sveltejs/kit';
|
|
7
|
+
import { type MaybePromise } from './tests/sveltemocks';
|
|
6
8
|
export type SvelteKitErrorFn = (server: SvelteKitServer, event: RequestEvent, ce: CrossauthError) => Promise<Response>;
|
|
7
9
|
/**
|
|
8
10
|
* Options for {@link SvelteKitOAuthClient}.
|