@nekm/svelte-core 1.6.2 → 1.7.0
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/external-link.svelte +8 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/protect/contracts.d.ts +27 -0
- package/dist/protect/contracts.js +1 -0
- package/dist/protect/helper.d.ts +1 -0
- package/dist/protect/helper.js +4 -0
- package/dist/protect/protect.d.ts +3 -0
- package/dist/protect/protect.js +31 -0
- package/dist/protect/routes/login.d.ts +3 -0
- package/dist/protect/routes/login.js +21 -0
- package/dist/protect/routes/logout.d.ts +3 -0
- package/dist/protect/routes/logout.js +22 -0
- package/dist/protect/routes/redirect-login.d.ts +3 -0
- package/dist/protect/routes/redirect-login.js +44 -0
- package/dist/protect/routes/redirect-logout.d.ts +3 -0
- package/dist/protect/routes/redirect-logout.js +17 -0
- package/package.json +2 -1
|
@@ -11,11 +11,17 @@
|
|
|
11
11
|
|
|
12
12
|
const { href, children, type = "nofollow", origin }: ExternalLinkProps = $props();
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const host = $derived.by(() => {
|
|
15
|
+
if (!origin) {
|
|
16
|
+
return page.url.host;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return origin.replace(/http(s)?:\/\//ig, "");
|
|
20
|
+
});
|
|
15
21
|
|
|
16
22
|
const hrefWithSource = $derived.by(() => {
|
|
17
23
|
const sign = href.includes("?") ? "&" : "?";
|
|
18
|
-
return href + sign + `utm_source=${encodeURIComponent(
|
|
24
|
+
return href + sign + `utm_source=${encodeURIComponent(host)}`;
|
|
19
25
|
});
|
|
20
26
|
|
|
21
27
|
const rel = $derived(type === "dofollow" ? undefined : type);
|
package/dist/index.d.ts
CHANGED
|
@@ -14,3 +14,5 @@ export * from "./ld-json.svelte";
|
|
|
14
14
|
export { default as ExternalLink } from "./external-link.svelte";
|
|
15
15
|
export * from "./external-link.svelte";
|
|
16
16
|
export * from "./hooks.js";
|
|
17
|
+
export * from "./protect/protect.ts";
|
|
18
|
+
export type { ProtectConfig } from "./protect/contracts.ts";
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Handle, RequestEvent } from "@sveltejs/kit";
|
|
2
|
+
export interface ProtectConfig<TAuthToken = unknown, TIdToken = unknown, TAccessToken = unknown> {
|
|
3
|
+
readonly protect?: (event: RequestEvent) => Promise<boolean> | boolean;
|
|
4
|
+
readonly jwtDecodeAndVerifyIdToken: (token: string) => Promise<TIdToken> | TIdToken;
|
|
5
|
+
readonly jwtDecodeAndVerifyAccessToken: (token: string) => Promise<TAccessToken> | TAccessToken;
|
|
6
|
+
readonly session: {
|
|
7
|
+
readonly exists: (event: RequestEvent) => (event: RequestEvent) => Promise<boolean> | boolean;
|
|
8
|
+
readonly stateGenerate: () => Promise<string> | string;
|
|
9
|
+
readonly stateGet: () => Promise<string> | string;
|
|
10
|
+
};
|
|
11
|
+
readonly hooks?: {
|
|
12
|
+
readonly onLogout?: () => Promise<void> | void;
|
|
13
|
+
readonly onLogin?: (authToken: TAuthToken, idToken: TIdToken, accessToken: TAccessToken) => Promise<void> | void;
|
|
14
|
+
};
|
|
15
|
+
readonly oauth: {
|
|
16
|
+
readonly clientId: string;
|
|
17
|
+
readonly clientSecret: string;
|
|
18
|
+
readonly baseUrl: string;
|
|
19
|
+
readonly authorizePath?: string;
|
|
20
|
+
readonly logoutPath?: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface Route {
|
|
24
|
+
readonly path: string;
|
|
25
|
+
readonly handle: Handle;
|
|
26
|
+
}
|
|
27
|
+
export type RouteFactory = (config: ProtectConfig) => Route | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getRedirectUri(origin: string, path: string): string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { error, redirect } from "@sveltejs/kit";
|
|
2
|
+
import { ROUTE_PATH_LOGIN, routeLoginFactory } from "./routes/login.ts";
|
|
3
|
+
import { routeLogoutFactory } from "./routes/logout.ts";
|
|
4
|
+
import { routeRedirectLoginFactory } from "./routes/redirect-login.ts";
|
|
5
|
+
import { routeRedirectLogoutFactory } from "./routes/redirect-logout.ts";
|
|
6
|
+
const routeFactories = Object.freeze([
|
|
7
|
+
routeLoginFactory,
|
|
8
|
+
routeLogoutFactory,
|
|
9
|
+
routeRedirectLoginFactory,
|
|
10
|
+
routeRedirectLogoutFactory,
|
|
11
|
+
]);
|
|
12
|
+
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]));
|
|
19
|
+
return async ({ event, resolve }) => {
|
|
20
|
+
const routeHandle = routes.get(event.url.pathname);
|
|
21
|
+
if (routeHandle) {
|
|
22
|
+
await routeHandle({ event, resolve });
|
|
23
|
+
// Handle should redirect. If it doesn't, something is wrong.
|
|
24
|
+
throw error(500, "Illegal state");
|
|
25
|
+
}
|
|
26
|
+
if (await protect(event) && !(await config.session.exists(event))) {
|
|
27
|
+
throw redirect(303, ROUTE_PATH_LOGIN);
|
|
28
|
+
}
|
|
29
|
+
return await resolve(event);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { redirect } from "@sveltejs/kit";
|
|
2
|
+
import { queryParamsCreate } from "@nekm/core";
|
|
3
|
+
import { getRedirectUri } from "../helper.ts";
|
|
4
|
+
import { ROUTE_PATH_REDIRECT_LOGIN } from "./redirect-login.ts";
|
|
5
|
+
export const ROUTE_PATH_LOGIN = "_auth/login";
|
|
6
|
+
export const routeLoginFactory = (config) => {
|
|
7
|
+
const authorizeUrl = `${config.oauth.baseUrl}/${config.oauth.authorizePath ?? "/oauth2/authorize"}`;
|
|
8
|
+
return {
|
|
9
|
+
path: ROUTE_PATH_LOGIN,
|
|
10
|
+
async handle({ event }) {
|
|
11
|
+
const state = await config.session.stateGenerate();
|
|
12
|
+
const params = queryParamsCreate({
|
|
13
|
+
client_id: config.oauth.clientId,
|
|
14
|
+
response_type: "code",
|
|
15
|
+
redirect_uri: getRedirectUri(event.url.origin, ROUTE_PATH_REDIRECT_LOGIN),
|
|
16
|
+
state,
|
|
17
|
+
});
|
|
18
|
+
throw redirect(302, `${authorizeUrl}?${params}`);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { redirect } from "@sveltejs/kit";
|
|
2
|
+
import { queryParamsCreate } from "@nekm/core";
|
|
3
|
+
import { getRedirectUri } from "../helper.ts";
|
|
4
|
+
import { ROUTE_PATH_REDIRECT_LOGOUT } from "./redirect-logout.ts";
|
|
5
|
+
export const ROUTE_PATH_LOGOUT = "_auth/logout";
|
|
6
|
+
export const routeLogoutFactory = (config) => {
|
|
7
|
+
// Check if the oauth provider supports a logout path.
|
|
8
|
+
if (!config.oauth.logoutPath) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
const logoutUrl = `${config.oauth.baseUrl}/${config.oauth.logoutPath}`;
|
|
12
|
+
return {
|
|
13
|
+
path: ROUTE_PATH_LOGOUT,
|
|
14
|
+
async handle({ event }) {
|
|
15
|
+
const params = queryParamsCreate({
|
|
16
|
+
logout_uri: getRedirectUri(event.url.origin, ROUTE_PATH_REDIRECT_LOGOUT),
|
|
17
|
+
client_id: config.oauth.clientId,
|
|
18
|
+
});
|
|
19
|
+
throw redirect(302, `${logoutUrl}?${params}`);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { redirect } from "@sveltejs/kit";
|
|
2
|
+
import { noop, throwIfUndefined } from "@nekm/core";
|
|
3
|
+
import { getRedirectUri } from "../helper.ts";
|
|
4
|
+
export const ROUTE_PATH_REDIRECT_LOGIN = "_auth/redirect/login";
|
|
5
|
+
export const routeRedirectLoginFactory = (config) => {
|
|
6
|
+
const onLogin = config.hooks?.onLogin ?? noop;
|
|
7
|
+
async function exchangeCodeForToken(fetch, origin, code) {
|
|
8
|
+
const response = await fetch(`${config.oauth.baseUrl}/oauth2/token`, {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers: {
|
|
11
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
12
|
+
"Accept": "application/json",
|
|
13
|
+
},
|
|
14
|
+
body: new URLSearchParams({
|
|
15
|
+
grant_type: "authorization_code",
|
|
16
|
+
client_id: config.oauth.clientId,
|
|
17
|
+
client_secret: config.oauth.clientSecret,
|
|
18
|
+
code,
|
|
19
|
+
redirect_uri: getRedirectUri(origin, ROUTE_PATH_REDIRECT_LOGIN),
|
|
20
|
+
}).toString(),
|
|
21
|
+
});
|
|
22
|
+
return await response.json();
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
path: ROUTE_PATH_REDIRECT_LOGIN,
|
|
26
|
+
async handle({ event }) {
|
|
27
|
+
const state = event.url.searchParams.get("state") ?? undefined;
|
|
28
|
+
;
|
|
29
|
+
const stateSession = await config.session.stateGet();
|
|
30
|
+
if (state !== stateSession) {
|
|
31
|
+
throw new Error("State do not match");
|
|
32
|
+
}
|
|
33
|
+
const code = event.url.searchParams.get("code") ?? undefined;
|
|
34
|
+
throwIfUndefined(code);
|
|
35
|
+
const authToken = await exchangeCodeForToken(fetch, event.url.origin, code);
|
|
36
|
+
// @ts-expect-error Ignore, for now
|
|
37
|
+
const idToken = await config.jwtDecodeAndVerifyIdToken(authToken.id_token);
|
|
38
|
+
// @ts-expect-error Ignore, for now
|
|
39
|
+
const accessToken = await config.jwtDecodeAndVerifyAccessToken(authToken.access_token);
|
|
40
|
+
await onLogin(authToken, idToken, accessToken);
|
|
41
|
+
throw redirect(302, "/");
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { redirect } from "@sveltejs/kit";
|
|
2
|
+
import { noop } from "@nekm/core";
|
|
3
|
+
export const ROUTE_PATH_REDIRECT_LOGOUT = "_auth/redirect/logout";
|
|
4
|
+
export const routeRedirectLogoutFactory = (config) => {
|
|
5
|
+
// Check if the oauth provider supports a logout path.
|
|
6
|
+
if (!config.oauth.logoutPath) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
const onLogout = config.hooks?.onLogout ?? noop;
|
|
10
|
+
return {
|
|
11
|
+
path: ROUTE_PATH_REDIRECT_LOGOUT,
|
|
12
|
+
async handle() {
|
|
13
|
+
await onLogout();
|
|
14
|
+
throw redirect(302, "/");
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
};
|
package/package.json
CHANGED