@nekm/svelte-core 1.7.10 → 1.7.11
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/protect/contracts.d.ts +6 -14
- package/dist/protect/helper.d.ts +3 -0
- package/dist/protect/helper.js +16 -0
- package/dist/protect/protect.d.ts +2 -0
- package/dist/protect/protect.js +8 -17
- package/dist/protect/routes/login.d.ts +1 -1
- package/dist/protect/routes/login.js +3 -1
- package/dist/protect/routes/logout.d.ts +1 -1
- package/dist/protect/routes/redirect-login.d.ts +1 -1
- package/dist/protect/routes/redirect-login.js +12 -8
- package/dist/protect/routes/redirect-logout.d.ts +1 -1
- package/dist/protect/routes/redirect-logout.js +2 -2
- package/dist/protect/routes/routes.d.ts +8 -0
- package/dist/protect/routes/routes.js +17 -0
- package/package.json +3 -1
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
import type {
|
|
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
|
|
10
|
+
readonly statePersist: (event: RequestEvent, state: string) => Promise<void> | void;
|
|
14
11
|
readonly stateGet: (event: RequestEvent) => Promise<string> | string;
|
|
15
|
-
|
|
16
|
-
|
|
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;
|
package/dist/protect/helper.d.ts
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
+
import type { ProtectConfig } from "./contracts.ts";
|
|
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>;
|
package/dist/protect/helper.js
CHANGED
|
@@ -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
|
+
}
|
package/dist/protect/protect.js
CHANGED
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
import { error, redirect } from "@sveltejs/kit";
|
|
2
|
-
import { ROUTE_PATH_LOGIN
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
const
|
|
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.ts";
|
|
5
|
+
export const PROTECT_LOGIN = "/" + ROUTE_PATH_LOGIN;
|
|
6
|
+
export const PROTECT_LOGOUT = "/" + ROUTE_PATH_LOGOUT;
|
|
12
7
|
export function protect(config) {
|
|
13
|
-
const
|
|
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
|
-
|
|
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);
|
|
@@ -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 =
|
|
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,9 +1,10 @@
|
|
|
1
1
|
import { redirect } from "@sveltejs/kit";
|
|
2
|
-
import {
|
|
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
|
|
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
|
-
|
|
37
|
-
const id = await
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
};
|
|
@@ -6,11 +6,11 @@ export const routeRedirectLogoutFactory = (config) => {
|
|
|
6
6
|
if (!config.oauth.logoutPath) {
|
|
7
7
|
return undefined;
|
|
8
8
|
}
|
|
9
|
-
const
|
|
9
|
+
const logout = config.session.logout ?? noop;
|
|
10
10
|
return {
|
|
11
11
|
path: ROUTE_PATH_REDIRECT_LOGOUT,
|
|
12
12
|
async handle({ event }) {
|
|
13
|
-
await
|
|
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.ts";
|
|
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.ts";
|
|
2
|
+
import { routeLogoutFactory } from "./logout.ts";
|
|
3
|
+
import { routeRedirectLogoutFactory } from "./redirect-logout.ts";
|
|
4
|
+
import { routeRedirectLoginFactory } from "./redirect-login.ts";
|
|
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.
|
|
5
|
+
"version": "1.7.11",
|
|
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
|
}
|