@functionalcms/svelte-components 2.18.1 → 2.18.3

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,15 +1,36 @@
1
1
  import {} from '@sveltejs/kit';
2
- import { createSession, getSession } from './sessionstorage.js';
2
+ import { createSession, getSession, deleteSession } from './sessionStorage.js';
3
3
  import { keycloak } from './authenticationProvider.js';
4
- const cookieName = `auth_state`;
5
- const logout = (cookies) => {
4
+ const authStateCookieName = `auth_state`;
5
+ const authSessionCookieName = `auth_session`;
6
+ const logout = (cookies, afterLogoutPath = '/') => {
6
7
  const headers = new Headers();
7
- const sid = cookies.get(cookieName);
8
- if (sid) {
9
- const deleteSessionCookieHeader = `${cookieName}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`;
10
- const deleteAuthCookieHeader = `auth_session=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`;
11
- headers.append('Set-Cookie', deleteSessionCookieHeader);
12
- headers.append('Set-Cookie', deleteAuthCookieHeader);
8
+ const state = cookies.get(authSessionCookieName);
9
+ if (state) {
10
+ cookies.delete('auth_session', { path: '/' });
11
+ const sid = cookies.get('auth_session');
12
+ deleteSession(sid);
13
+ headers.append('Set-Cookie', 'auth_session=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT;');
14
+ headers.append('Location', afterLogoutPath);
15
+ }
16
+ else {
17
+ headers.append('Location', '/');
18
+ }
19
+ return headers;
20
+ };
21
+ const createUserSession = async (provider, event) => {
22
+ const token = await provider.getValidation(event);
23
+ const session = await provider.getUser(token);
24
+ 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
+ if (session !== undefined) {
28
+ session.userId = session.sub;
29
+ event.locals.session = session;
30
+ event.locals.accessToken = token.access_token;
31
+ 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
+ headers.append('Location', provider.redirectPath);
13
34
  }
14
35
  return headers;
15
36
  };
@@ -24,45 +45,27 @@ const loadUserFromSession = (cookies, locals) => {
24
45
  locals.username = "";
25
46
  }
26
47
  };
27
- const getHeadersWithCookie = (sessionId, expiresIn) => {
28
- const cookieHeader = `auth_session=${sessionId}; HttpOnly; Secure; SameSite=strict; Max-Age=${expiresIn}; Path=/`;
29
- const headers = new Headers();
30
- headers.append('Set-Cookie', cookieHeader);
31
- return headers;
32
- };
33
48
  export const authenticationHandle = (issuer, clientId, scope = '', redirectPath = '/') => {
34
49
  const provider = keycloak(issuer, clientId, scope, redirectPath);
35
50
  return async ({ event, resolve }) => {
36
- //login user check
51
+ //login user check
37
52
  if (event.url.pathname.startsWith('/')) {
38
53
  loadUserFromSession(event.cookies, event.locals);
39
54
  }
40
- //logout
41
- if (event.url.pathname === '/auth/logout' && event.cookies.get(cookieName) === undefined) {
42
- const headers = logout(event.cookie);
55
+ //logout
56
+ if (event.url.pathname === '/auth/logout') {
57
+ const headers = logout(event.cookies);
43
58
  return new Response('Logging Out...', { status: 303, headers });
44
59
  }
45
- // login
60
+ // login
46
61
  else if (event.url.pathname === '/auth/login') {
47
62
  const authProvider = await provider.getAuthIdentity(event.url.origin);
48
63
  return new Response('Redirect', { status: 302, headers: authProvider });
49
64
  }
50
65
  //validate
51
66
  else if (event.url.pathname === "/auth/validate") {
52
- const token = await provider.getValidation(event);
53
- const session = await provider.getUser(token);
54
- if (session !== undefined) {
55
- session.userId = session.sub;
56
- event.locals.session = session;
57
- event.locals.accessToken = token.access_token;
58
- const sessionId = createSession({ session, token }, token.expires_in);
59
- const headers = getHeadersWithCookie(sessionId, token.expires_in);
60
- headers.append('Location', provider.redirectPath);
61
- return new Response('Redirect', { status: 303, headers });
62
- }
63
- else {
64
- return new Response(`Error in authorizing user`);
65
- }
67
+ const validationResponse = await createUserSession(provider, event);
68
+ return new Response('Redirect', { status: 302, headers: validationResponse });
66
69
  }
67
70
  return await resolve(event);
68
71
  };
@@ -1,9 +1,9 @@
1
1
  import * as o from "oauth4webapi";
2
- const cookieName = 'auth_state';
2
+ const aurthStateCookieName = '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 = `${cookieName}=${state}; HttpOnly; Secure; SameSite=strict; Max-Age=3600; Path=/`;
6
+ const cookieHeader = `${aurthStateCookieName}=${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(cookieName);
23
+ const storedState = event.cookies.get(aurthStateCookieName);
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,7 @@ 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(cookieName, { path: '/' });
53
+ event.cookies.delete(aurthStateCookieName, { path: '/' });
54
54
  return token;
55
55
  };
56
56
  const getUser = async (issuer, token) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@functionalcms/svelte-components",
3
- "version": "2.18.1",
3
+ "version": "2.18.3",
4
4
  "watch": {
5
5
  "build": {
6
6
  "patterns": [