@functionalcms/svelte-components 2.17.8 → 2.17.10
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/dist/auth/auth.d.ts +2 -2
- package/dist/auth/auth.js +58 -18
- package/package.json +1 -1
package/dist/auth/auth.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="@sveltejs/kit" />
|
|
2
|
-
type
|
|
3
|
-
export declare const configureAuthentication: (secret: string, issuer: string, clientId: string, clientSecret: string, customClaims?: string[],
|
|
2
|
+
type ProcessSession = (session: any) => any;
|
|
3
|
+
export declare const configureAuthentication: (secret: string, issuer: string, clientId: string, clientSecret: string, customClaims?: string[], sessionTransformation?: ProcessSession) => () => {
|
|
4
4
|
handle: import("@sveltejs/kit").Handle;
|
|
5
5
|
signIn: import("@sveltejs/kit").Action;
|
|
6
6
|
signOut: import("@sveltejs/kit").Action;
|
package/dist/auth/auth.js
CHANGED
|
@@ -1,8 +1,45 @@
|
|
|
1
1
|
import { SvelteKitAuth } from "@auth/sveltekit";
|
|
2
2
|
import Keycloak from "@auth/sveltekit/providers/keycloak";
|
|
3
3
|
const defaultScopes = ["openid", "profile", "offline_access", "functional"];
|
|
4
|
-
const defaultTokenCallback = (
|
|
5
|
-
|
|
4
|
+
const defaultTokenCallback = (session) => { };
|
|
5
|
+
function isTokenRefreshRequired(issued_at) {
|
|
6
|
+
const dateNow = Math.floor(new Date().getTime() / 1000);
|
|
7
|
+
const diffrence = Math.abs(new Date(issued_at).getTime() - dateNow);
|
|
8
|
+
const minutes = Math.floor(diffrence / 60);
|
|
9
|
+
return minutes > 25;
|
|
10
|
+
}
|
|
11
|
+
async function refreshToken(issuer, clientId, clientSecret, session) {
|
|
12
|
+
const user = session?.user;
|
|
13
|
+
if (user) {
|
|
14
|
+
try {
|
|
15
|
+
const request = await fetch(`${issuer}/protocol/openid-connect/token`, {
|
|
16
|
+
method: 'POST',
|
|
17
|
+
headers: {
|
|
18
|
+
'content-type': 'application/x-www-form-urlencoded'
|
|
19
|
+
},
|
|
20
|
+
body: new URLSearchParams({
|
|
21
|
+
grant_type: 'client_credentials',
|
|
22
|
+
client_id: clientId,
|
|
23
|
+
client_secret: clientSecret // ${ISSUER}/api/v2/
|
|
24
|
+
})
|
|
25
|
+
});
|
|
26
|
+
return await request.json();
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.error(`Error while updating token data: ${error?.message}. Reusing existing tokens!`);
|
|
30
|
+
return {
|
|
31
|
+
accessToken: user.access_token,
|
|
32
|
+
expires_at: user.expires_at
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return {
|
|
38
|
+
message: 'User is not authorized to rotate access-token'
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export const configureAuthentication = (secret, issuer, clientId, clientSecret, customClaims = [], sessionTransformation = defaultTokenCallback) => {
|
|
6
43
|
const scope = defaultScopes.concat(customClaims).join(' ');
|
|
7
44
|
return () => SvelteKitAuth({
|
|
8
45
|
providers: [Keycloak({
|
|
@@ -16,35 +53,38 @@ export const configureAuthentication = (secret, issuer, clientId, clientSecret,
|
|
|
16
53
|
}
|
|
17
54
|
})],
|
|
18
55
|
secret: secret,
|
|
56
|
+
trustHost: true,
|
|
19
57
|
callbacks: {
|
|
20
|
-
|
|
21
|
-
// return user;
|
|
22
|
-
// },
|
|
23
|
-
async jwt({ token, user, account, session, profile, trigger }) {
|
|
24
|
-
// Your JWT callback logic here
|
|
58
|
+
async jwt({ token, user, account, profile, session }) {
|
|
25
59
|
if (account) {
|
|
26
|
-
token.
|
|
27
|
-
token.accessToken = account.access_token;
|
|
60
|
+
token.account = account;
|
|
28
61
|
}
|
|
29
62
|
if (user) {
|
|
30
|
-
token.
|
|
31
|
-
token.metadata = user.metadata;
|
|
63
|
+
token.user = user;
|
|
32
64
|
}
|
|
33
65
|
if (profile) {
|
|
34
|
-
token.
|
|
66
|
+
token.profile = profile;
|
|
67
|
+
}
|
|
68
|
+
if (token && isTokenRefreshRequired(token.expires_at)) {
|
|
69
|
+
const updatedToken = await refreshToken(issuer, clientId, clientSecret, session);
|
|
70
|
+
if (updatedToken.access_token) {
|
|
71
|
+
token = {
|
|
72
|
+
...token,
|
|
73
|
+
...updatedToken
|
|
74
|
+
};
|
|
75
|
+
}
|
|
35
76
|
}
|
|
36
77
|
return token;
|
|
37
78
|
},
|
|
38
79
|
async session({ session, token, user }) {
|
|
39
|
-
session.accessToken = token.
|
|
80
|
+
session.accessToken = token.account.access_token;
|
|
40
81
|
if (session.user) {
|
|
41
|
-
session
|
|
42
|
-
session.user.metadata = token.metadata;
|
|
43
|
-
session.userId = token.sub;
|
|
82
|
+
session = { ...session, ...user };
|
|
44
83
|
}
|
|
45
|
-
if (token.
|
|
46
|
-
session.
|
|
84
|
+
if (token.profile) {
|
|
85
|
+
session.profile = token.profile;
|
|
47
86
|
}
|
|
87
|
+
sessionTransformation(session);
|
|
48
88
|
return session;
|
|
49
89
|
}
|
|
50
90
|
}
|