@functionalcms/svelte-components 2.19.0 → 2.19.2
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,2 +1,2 @@
|
|
|
1
1
|
import { type Handle } from '@sveltejs/kit';
|
|
2
|
-
export declare const authenticationHandle: (
|
|
2
|
+
export declare const authenticationHandle: (scope?: string, redirectPath?: string) => Handle;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { AUTH_KEYCLOAK_ID, AUTH_KEYCLOAK_ISSUER } from '$env/static/private';
|
|
1
2
|
import {} from '@sveltejs/kit';
|
|
2
|
-
import { createSession, getSession, deleteSession } from './
|
|
3
|
+
import { createSession, getSession, deleteSession } from './sessionstorage.js';
|
|
3
4
|
import { keycloak } from './authenticationProvider.js';
|
|
4
5
|
const authStateCookieName = `auth_state`;
|
|
5
6
|
const authSessionCookieName = `auth_session`;
|
|
@@ -10,7 +11,7 @@ const logout = (cookies, afterLogoutPath = '/') => {
|
|
|
10
11
|
cookies.delete('auth_session', { path: '/' });
|
|
11
12
|
const sid = cookies.get('auth_session');
|
|
12
13
|
deleteSession(sid);
|
|
13
|
-
headers.append('Set-Cookie',
|
|
14
|
+
headers.append('Set-Cookie', `${authSessionCookieName}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`);
|
|
14
15
|
headers.append('Location', afterLogoutPath);
|
|
15
16
|
}
|
|
16
17
|
else {
|
|
@@ -22,20 +23,21 @@ const createUserSession = async (provider, event) => {
|
|
|
22
23
|
const token = await provider.getValidation(event);
|
|
23
24
|
const session = await provider.getUser(token);
|
|
24
25
|
const headers = new Headers();
|
|
25
|
-
headers.append('Location', '/');
|
|
26
|
-
headers.append('Set-Cookie', 'auth_state=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT;');
|
|
27
26
|
if (session !== undefined) {
|
|
28
27
|
session.userId = session.sub;
|
|
29
28
|
event.locals.session = session;
|
|
30
29
|
event.locals.accessToken = token.access_token;
|
|
31
30
|
const sessionId = createSession({ session, token }, token.expires_in);
|
|
32
|
-
headers.append('Set-Cookie', `auth_session=${sessionId}; HttpOnly; Secure; SameSite=strict; Max-Age=${token.expires_in}; Path=/`);
|
|
33
31
|
headers.append('Location', provider.redirectPath);
|
|
32
|
+
headers.append('Set-Cookie', `${authSessionCookieName}=${sessionId}; HttpOnly; Secure; SameSite=strict; Max-Age=${token.expires_in}; Path=/`);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
headers.append('Location', '/');
|
|
34
36
|
}
|
|
35
37
|
return headers;
|
|
36
38
|
};
|
|
37
39
|
const loadUserFromSession = (cookies, locals) => {
|
|
38
|
-
const sid = cookies.get(
|
|
40
|
+
const sid = cookies.get(authSessionCookieName);
|
|
39
41
|
const session = getSession(sid);
|
|
40
42
|
if (session) {
|
|
41
43
|
locals.session = session.session;
|
|
@@ -45,10 +47,10 @@ const loadUserFromSession = (cookies, locals) => {
|
|
|
45
47
|
locals.username = "";
|
|
46
48
|
}
|
|
47
49
|
};
|
|
48
|
-
export const authenticationHandle = (
|
|
49
|
-
const provider = keycloak(
|
|
50
|
+
export const authenticationHandle = (scope = '', redirectPath = '/') => {
|
|
51
|
+
const provider = keycloak(AUTH_KEYCLOAK_ISSUER, AUTH_KEYCLOAK_ID, scope, redirectPath);
|
|
50
52
|
return async ({ event, resolve }) => {
|
|
51
|
-
//login user check
|
|
53
|
+
//login user check + refresh
|
|
52
54
|
if (event.url.pathname.startsWith('/')) {
|
|
53
55
|
loadUserFromSession(event.cookies, event.locals);
|
|
54
56
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as o from "oauth4webapi";
|
|
2
|
-
const
|
|
2
|
+
const authStateCookieName = 'auth_state';
|
|
3
3
|
const stateIdGenerator = () => crypto.randomUUID();
|
|
4
4
|
const getKeycloakIdentity = async (issuer, client_id, scope, redirectUrl) => {
|
|
5
5
|
const state = stateIdGenerator();
|
|
6
|
-
const cookieHeader = `${
|
|
6
|
+
const cookieHeader = `${authStateCookieName}=${state}; HttpOnly; Secure; SameSite=strict; Max-Age=3600; Path=/`;
|
|
7
7
|
const code_challenge = await o.calculatePKCECodeChallenge(state);
|
|
8
8
|
const authorizationUrlSearchParams = new URLSearchParams({
|
|
9
9
|
client_id: client_id,
|
|
@@ -20,7 +20,7 @@ const getKeycloakIdentity = async (issuer, client_id, scope, redirectUrl) => {
|
|
|
20
20
|
return headers;
|
|
21
21
|
};
|
|
22
22
|
const getKeycloakValidation = async (issuer, client_id, scope, event, redirectUrl) => {
|
|
23
|
-
const storedState = event.cookies.get(
|
|
23
|
+
const storedState = event.cookies.get(authStateCookieName);
|
|
24
24
|
const state = event.url.searchParams.get("state");
|
|
25
25
|
if (!storedState || !state || storedState !== state) {
|
|
26
26
|
throw new Error('State not valid');
|
|
@@ -50,7 +50,6 @@ const getKeycloakValidation = async (issuer, client_id, scope, event, redirectUr
|
|
|
50
50
|
throw new Error('Token not validated.');
|
|
51
51
|
}
|
|
52
52
|
const token = await response.json();
|
|
53
|
-
event.cookies.delete(aurthStateCookieName, { path: '/' });
|
|
54
53
|
return token;
|
|
55
54
|
};
|
|
56
55
|
const getUser = async (issuer, token) => {
|
package/package.json
CHANGED
|
File without changes
|