@functionalcms/svelte-components 2.17.9 → 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.
@@ -1,6 +1,6 @@
1
1
  /// <reference types="@sveltejs/kit" />
2
- type Tokencallback = (token: any, profile: any) => any;
3
- export declare const configureAuthentication: (secret: string, issuer: string, clientId: string, clientSecret: string, customClaims?: string[], jwtCallback?: Tokencallback) => () => {
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 = (profile) => profile;
5
- export const configureAuthentication = (secret, issuer, clientId, clientSecret, customClaims = [], jwtCallback = defaultTokenCallback) => {
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({
@@ -18,34 +55,36 @@ export const configureAuthentication = (secret, issuer, clientId, clientSecret,
18
55
  secret: secret,
19
56
  trustHost: true,
20
57
  callbacks: {
21
- // async signIn({ user, account, profile, email, credentials }: any) {
22
- // return user;
23
- // },
24
- async jwt({ token, user, account, session, profile, trigger }) {
25
- // Your JWT callback logic here
58
+ async jwt({ token, user, account, profile, session }) {
26
59
  if (account) {
27
- token.id = account.providerAccountId;
28
- token.accessToken = account.access_token;
60
+ token.account = account;
29
61
  }
30
62
  if (user) {
31
- token.roles = user.roles;
32
- token.metadata = user.metadata;
63
+ token.user = user;
33
64
  }
34
65
  if (profile) {
35
- token.custom = jwtCallback(profile);
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
+ }
36
76
  }
37
77
  return token;
38
78
  },
39
79
  async session({ session, token, user }) {
40
- session.accessToken = token.accessToken;
80
+ session.accessToken = token.account.access_token;
41
81
  if (session.user) {
42
- session.user.roles = token.roles;
43
- session.user.metadata = token.metadata;
44
- session.userId = token.sub;
82
+ session = { ...session, ...user };
45
83
  }
46
- if (token.custom) {
47
- session.custom = token.custom;
84
+ if (token.profile) {
85
+ session.profile = token.profile;
48
86
  }
87
+ sessionTransformation(session);
49
88
  return session;
50
89
  }
51
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@functionalcms/svelte-components",
3
- "version": "2.17.9",
3
+ "version": "2.17.10",
4
4
  "watch": {
5
5
  "build": {
6
6
  "patterns": [