@nekm/svelte-core 1.7.10 → 1.7.12

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,21 +1,16 @@
1
- import type { Handle, RequestEvent } from "@sveltejs/kit";
1
+ import type { RequestEvent } from "@sveltejs/kit";
2
2
  export interface ProtectTokens {
3
3
  readonly auth: unknown;
4
4
  readonly id: unknown;
5
5
  readonly access: unknown;
6
6
  }
7
7
  export interface ProtectConfig {
8
- readonly protect?: (event: RequestEvent) => Promise<boolean> | boolean;
9
- readonly jwtDecodeAndVerifyIdToken: (token: string) => Promise<unknown> | unknown;
10
- readonly jwtDecodeAndVerifyAccessToken: (token: string) => Promise<unknown> | unknown;
11
8
  readonly session: {
12
9
  readonly exists: (event: RequestEvent) => Promise<boolean> | boolean;
13
- readonly stateGenerate: (event: RequestEvent) => Promise<string> | string;
10
+ readonly statePersist: (event: RequestEvent, state: string) => Promise<void> | void;
14
11
  readonly stateGet: (event: RequestEvent) => Promise<string> | string;
15
- };
16
- readonly hooks?: {
17
- readonly onLogout?: (event: RequestEvent) => Promise<void> | void;
18
- readonly onLogin?: (event: RequestEvent, tokens: ProtectTokens) => Promise<void> | void;
12
+ readonly login: (event: RequestEvent, tokens: ProtectTokens) => Promise<void> | void;
13
+ readonly logout?: (event: RequestEvent) => Promise<void> | void;
19
14
  };
20
15
  readonly oauth: {
21
16
  readonly clientId: string;
@@ -23,10 +18,7 @@ export interface ProtectConfig {
23
18
  readonly baseUrl: string;
24
19
  readonly authorizePath?: string;
25
20
  readonly logoutPath?: string;
21
+ readonly jwksUrl: string;
22
+ readonly issuer: string;
26
23
  };
27
24
  }
28
- export interface Route {
29
- readonly path: string;
30
- readonly handle: Handle;
31
- }
32
- export type RouteFactory = (config: ProtectConfig) => Route | undefined;
@@ -1 +1,4 @@
1
+ import type { ProtectConfig } from "./contracts.js";
1
2
  export declare function getRedirectUri(origin: string, path: string): string;
3
+ export declare function jwtVerifyIdToken(config: ProtectConfig, jwks: unknown, idToken: string): Promise<import("jose").JWTPayload>;
4
+ export declare function jwtVerifyAccessToken(config: ProtectConfig, jwks: unknown, accessToken: string): Promise<import("jose").JWTPayload>;
@@ -1,4 +1,20 @@
1
1
  import { strTrimEnd } from "@nekm/core";
2
+ import { jwtVerify } from "jose";
2
3
  export function getRedirectUri(origin, path) {
3
4
  return `${strTrimEnd(origin, "/")}/${path}`;
4
5
  }
6
+ export async function jwtVerifyIdToken(config, jwks, idToken) {
7
+ const { payload } = await jwtVerify(idToken,
8
+ // @ts-expect-error It's OK.
9
+ jwks, {
10
+ issuer: config.oauth.issuer,
11
+ audience: config.oauth.clientId,
12
+ });
13
+ return payload;
14
+ }
15
+ export async function jwtVerifyAccessToken(config, jwks, accessToken) {
16
+ const { payload } = await jwtVerify(accessToken,
17
+ // @ts-expect-error It's OK.
18
+ jwks, { issuer: config.oauth.issuer });
19
+ return payload;
20
+ }
@@ -1,3 +1,5 @@
1
1
  import { type Handle } from "@sveltejs/kit";
2
2
  import type { ProtectConfig } from "./contracts.js";
3
+ export declare const PROTECT_LOGIN: string;
4
+ export declare const PROTECT_LOGOUT: string;
3
5
  export declare function protect(config: ProtectConfig): Handle;
@@ -1,21 +1,11 @@
1
1
  import { error, redirect } from "@sveltejs/kit";
2
- import { ROUTE_PATH_LOGIN, routeLoginFactory } from "./routes/login.js";
3
- import { routeLogoutFactory } from "./routes/logout.js";
4
- import { routeRedirectLoginFactory } from "./routes/redirect-login.js";
5
- import { routeRedirectLogoutFactory } from "./routes/redirect-logout.js";
6
- const routeFactories = Object.freeze([
7
- routeLoginFactory,
8
- routeLogoutFactory,
9
- routeRedirectLoginFactory,
10
- routeRedirectLogoutFactory,
11
- ]);
2
+ import { ROUTE_PATH_LOGIN } from "./routes/login.js";
3
+ import { ROUTE_PATH_LOGOUT } from "./routes/logout.js";
4
+ import { routeCreate } from "./routes/routes.js";
5
+ export const PROTECT_LOGIN = "/" + ROUTE_PATH_LOGIN;
6
+ export const PROTECT_LOGOUT = "/" + ROUTE_PATH_LOGOUT;
12
7
  export function protect(config) {
13
- const protect = config.protect ?? (() => true);
14
- const routes = new Map(routeFactories
15
- .map(routeFactory => routeFactory(config))
16
- .filter(route => Boolean(route))
17
- // @ts-expect-error Incorrect typing error.
18
- .map(route => [`/${route.path}`, route.handle]));
8
+ const routes = routeCreate(config);
19
9
  return async ({ event, resolve }) => {
20
10
  const routeHandle = routes.get(event.url.pathname);
21
11
  if (routeHandle) {
@@ -23,7 +13,8 @@ export function protect(config) {
23
13
  // Handle should redirect. If it doesn't, something is wrong.
24
14
  throw error(500, "Illegal state");
25
15
  }
26
- if (await protect(event) && !(await config.session.exists(event))) {
16
+ const sessionExists = await config.session.exists(event);
17
+ if (!sessionExists) {
27
18
  throw redirect(303, ROUTE_PATH_LOGIN);
28
19
  }
29
20
  return await resolve(event);
@@ -1,3 +1,3 @@
1
- import type { RouteFactory } from "../contracts.js";
1
+ import type { RouteFactory } from "./routes.js";
2
2
  export declare const ROUTE_PATH_LOGIN = "_auth/login";
3
3
  export declare const routeLoginFactory: RouteFactory;
@@ -2,13 +2,15 @@ import { redirect } from "@sveltejs/kit";
2
2
  import { queryParamsCreate } from "@nekm/core";
3
3
  import { getRedirectUri } from "../helper.js";
4
4
  import { ROUTE_PATH_REDIRECT_LOGIN } from "./redirect-login.js";
5
+ import { randomUUID } from "node:crypto";
5
6
  export const ROUTE_PATH_LOGIN = "_auth/login";
6
7
  export const routeLoginFactory = (config) => {
7
8
  const authorizeUrl = `${config.oauth.baseUrl}/${config.oauth.authorizePath ?? "/oauth2/authorize"}`;
8
9
  return {
9
10
  path: ROUTE_PATH_LOGIN,
10
11
  async handle({ event }) {
11
- const state = await config.session.stateGenerate(event);
12
+ const state = randomUUID();
13
+ await config.session.statePersist(event, state);
12
14
  const params = queryParamsCreate({
13
15
  client_id: config.oauth.clientId,
14
16
  response_type: "code",
@@ -1,3 +1,3 @@
1
- import type { RouteFactory } from "../contracts.js";
1
+ import type { RouteFactory } from "./routes.js";
2
2
  export declare const ROUTE_PATH_LOGOUT = "_auth/logout";
3
3
  export declare const routeLogoutFactory: RouteFactory;
@@ -1,3 +1,3 @@
1
- import type { RouteFactory } from "../contracts.js";
1
+ import type { RouteFactory } from "./routes.js";
2
2
  export declare const ROUTE_PATH_REDIRECT_LOGIN = "_auth/redirect/login";
3
3
  export declare const routeRedirectLoginFactory: RouteFactory;
@@ -1,9 +1,10 @@
1
1
  import { redirect } from "@sveltejs/kit";
2
- import { noop, throwIfUndefined } from "@nekm/core";
3
- import { getRedirectUri } from "../helper.js";
2
+ import { throwIfUndefined } from "@nekm/core";
3
+ import { getRedirectUri, jwtVerifyAccessToken, jwtVerifyIdToken } from "../helper.js";
4
+ import { createRemoteJWKSet } from "jose";
4
5
  export const ROUTE_PATH_REDIRECT_LOGIN = "_auth/redirect/login";
5
6
  export const routeRedirectLoginFactory = (config) => {
6
- const onLogin = config.hooks?.onLogin ?? noop;
7
+ const jwksUrl = new URL(config.oauth.jwksUrl);
7
8
  async function exchangeCodeForToken(fetch, origin, code) {
8
9
  const response = await fetch(`${config.oauth.baseUrl}/oauth2/token`, {
9
10
  method: "POST",
@@ -33,11 +34,14 @@ export const routeRedirectLoginFactory = (config) => {
33
34
  const code = event.url.searchParams.get("code") ?? undefined;
34
35
  throwIfUndefined(code);
35
36
  const auth = await exchangeCodeForToken(fetch, event.url.origin, code);
36
- // @ts-expect-error Ignore, for now
37
- const id = await config.jwtDecodeAndVerifyIdToken(auth.id_token);
38
- // @ts-expect-error Ignore, for now
39
- const access = await config.jwtDecodeAndVerifyAccessToken(auth.access_token);
40
- await onLogin(event, { auth, id, access });
37
+ const jwks = createRemoteJWKSet(jwksUrl);
38
+ const [id, access] = await Promise.all([
39
+ // @ts-expect-error It's OK
40
+ jwtVerifyIdToken(config, jwks, auth.id_token),
41
+ // @ts-expect-error It's OK
42
+ jwtVerifyAccessToken(config, jwks, auth.access_token),
43
+ ]);
44
+ await config.session.login(event, { auth, id, access });
41
45
  throw redirect(302, "/");
42
46
  }
43
47
  };
@@ -1,3 +1,3 @@
1
- import type { RouteFactory } from "../contracts.js";
1
+ import type { RouteFactory } from "./routes.js";
2
2
  export declare const ROUTE_PATH_REDIRECT_LOGOUT = "_auth/redirect/logout";
3
3
  export declare const routeRedirectLogoutFactory: RouteFactory;
@@ -6,11 +6,11 @@ export const routeRedirectLogoutFactory = (config) => {
6
6
  if (!config.oauth.logoutPath) {
7
7
  return undefined;
8
8
  }
9
- const onLogout = config.hooks?.onLogout ?? noop;
9
+ const logout = config.session.logout ?? noop;
10
10
  return {
11
11
  path: ROUTE_PATH_REDIRECT_LOGOUT,
12
12
  async handle({ event }) {
13
- await onLogout(event);
13
+ await logout(event);
14
14
  throw redirect(302, "/");
15
15
  }
16
16
  };
@@ -0,0 +1,8 @@
1
+ import type { Handle } from "@sveltejs/kit";
2
+ import type { ProtectConfig } from "../contracts.js";
3
+ export interface Route {
4
+ readonly path: string;
5
+ readonly handle: Handle;
6
+ }
7
+ export type RouteFactory = (config: ProtectConfig) => Route | undefined;
8
+ export declare function routeCreate(config: ProtectConfig): Map<string, Handle>;
@@ -0,0 +1,17 @@
1
+ import { routeLoginFactory } from "./login.js";
2
+ import { routeLogoutFactory } from "./logout.js";
3
+ import { routeRedirectLogoutFactory } from "./redirect-logout.js";
4
+ import { routeRedirectLoginFactory } from "./redirect-login.js";
5
+ const routeFactories = Object.freeze([
6
+ routeLoginFactory,
7
+ routeLogoutFactory,
8
+ routeRedirectLoginFactory,
9
+ routeRedirectLogoutFactory,
10
+ ]);
11
+ export function routeCreate(config) {
12
+ return new Map(routeFactories
13
+ .map(routeFactory => routeFactory(config))
14
+ .filter(route => Boolean(route))
15
+ // @ts-expect-error Incorrect typing error.
16
+ .map(route => [`/${route.path}`, route.handle]));
17
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@nekm/svelte-core",
3
3
  "description": "Code I find myself re-writing over-and-over, Svelte edition.",
4
4
  "license": "MIT",
5
- "version": "1.7.10",
5
+ "version": "1.7.12",
6
6
  "scripts": {
7
7
  "dev": "vite dev",
8
8
  "build": "vite build && npm run prepack",
@@ -33,6 +33,7 @@
33
33
  "@sveltejs/adapter-auto": "^7.0.0",
34
34
  "@sveltejs/package": "^2.5.7",
35
35
  "@sveltejs/vite-plugin-svelte": "^6.2.1",
36
+ "@types/node": "^25.0.10",
36
37
  "publint": "^0.3.15",
37
38
  "svelte-check": "^4.3.4",
38
39
  "typescript": "^5.9.3",
@@ -45,6 +46,7 @@
45
46
  "dependencies": {
46
47
  "@nekm/core": "1.*",
47
48
  "@sveltejs/kit": "2.*",
49
+ "jose": "6.*",
48
50
  "svelte": "5.*"
49
51
  }
50
52
  }