@functionalcms/svelte-components 2.28.0 → 2.28.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/dist/auth/tokenRefreshHandle.js +13 -10
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {} from '@sveltejs/kit';
|
|
2
|
-
import { AUTH_KEYCLOAK_ID,
|
|
2
|
+
import { AUTH_KEYCLOAK_ID, AUTH_KEYCLOAK_ISSUER } from '$env/static/private';
|
|
3
3
|
const authSessionCookieName = `auth_session`;
|
|
4
4
|
function isTokenExpired(token) {
|
|
5
5
|
const base64Url = token.split(".")[1];
|
|
@@ -14,13 +14,12 @@ function isTokenExpired(token) {
|
|
|
14
14
|
const expired = Date.now() >= exp * 1000;
|
|
15
15
|
return expired;
|
|
16
16
|
}
|
|
17
|
-
async function refreshToken(clientId,
|
|
17
|
+
async function refreshToken(clientId, refresh_token) {
|
|
18
18
|
const response = await fetch(`${AUTH_KEYCLOAK_ISSUER}/protocol/openid-connect/token`, {
|
|
19
19
|
method: "POST",
|
|
20
20
|
body: new URLSearchParams({
|
|
21
21
|
grant_type: "refresh_token",
|
|
22
22
|
client_id: clientId,
|
|
23
|
-
client_secret: clientSecret,
|
|
24
23
|
refresh_token: refresh_token,
|
|
25
24
|
}),
|
|
26
25
|
headers: {
|
|
@@ -34,15 +33,19 @@ async function refreshToken(clientId, clientSecret, refresh_token) {
|
|
|
34
33
|
export const tokenRefreshHandle = (sessionProvider) => {
|
|
35
34
|
return async ({ event, resolve }) => {
|
|
36
35
|
const locals = event.locals;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
try {
|
|
37
|
+
if (locals?.token?.refresh_token) {
|
|
38
|
+
const isExpired = isTokenExpired(locals.token.access_token);
|
|
39
|
+
if (isExpired) {
|
|
40
|
+
const newToken = await refreshToken(AUTH_KEYCLOAK_ID, locals.token.refresh_token);
|
|
41
|
+
locals.token = newToken;
|
|
42
|
+
const sid = event.cookies.get(authSessionCookieName);
|
|
43
|
+
sessionProvider.updateSession(sid, locals, newToken.expires_in);
|
|
44
|
+
}
|
|
44
45
|
}
|
|
45
46
|
}
|
|
47
|
+
catch {
|
|
48
|
+
}
|
|
46
49
|
return await resolve(event);
|
|
47
50
|
};
|
|
48
51
|
};
|