@ultrst-armada/kloks 2.0.0 → 2.0.1
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/package.json +1 -1
- package/packages/client/dist/auth.d.ts +20 -1
- package/packages/client/dist/auth.js +13 -3
- package/packages/client/dist/chunk-5JMYSB7L.js +133 -0
- package/packages/client/dist/index.d.ts +1 -1
- package/packages/client/dist/index.js +13 -3
- package/packages/client/package.json +1 -1
- package/packages/client/dist/chunk-B4WOPCRZ.js +0 -26
package/package.json
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { DeploymentEnvironment } from '@dinilmusick/gutts/auth';
|
|
2
2
|
export { LuthAuthClient as CanonicalAuthClient, DEFAULT_LUTH_URL, DeploymentEnvironment, EmailSignInCredentials, EmailSignUpData, GoogleSignInOptions, LuthAuthClient, LuthAuthClientConfig, LuthAuthError, LuthAuthTokenStore, LuthSession, LuthSessionResult, LuthUser, RegisterApplicationResult, createLuthAuthClient as createCanonicalAuthClient, createLuthAuthClient, hydrateLuthTokenFromUrl, resolveClientEnvironment, resolveClientOrigin } from '@dinilmusick/gutts/auth';
|
|
3
3
|
|
|
4
|
+
interface LuthPrincipal {
|
|
5
|
+
id: string;
|
|
6
|
+
email: string;
|
|
7
|
+
name: string;
|
|
8
|
+
role: string;
|
|
9
|
+
}
|
|
10
|
+
declare const CANONICAL_PUBLIC_ROUTES: (string | RegExp)[];
|
|
11
|
+
declare function extractLuthToken(request: Request): string;
|
|
12
|
+
declare function isPublicRoute(pathname: string, extraPublicRoutes?: (string | RegExp)[]): boolean;
|
|
13
|
+
declare function verifyLuthRequest(request: Request, options?: {
|
|
14
|
+
luthUrl?: string;
|
|
15
|
+
extraPublicRoutes?: (string | RegExp)[];
|
|
16
|
+
}): Promise<LuthPrincipal | Response | null>;
|
|
17
|
+
|
|
4
18
|
interface LuthDeploymentRecord {
|
|
5
19
|
id: string;
|
|
6
20
|
url: string;
|
|
@@ -11,4 +25,9 @@ interface LuthDeploymentRecord {
|
|
|
11
25
|
priority?: number;
|
|
12
26
|
}
|
|
13
27
|
|
|
14
|
-
|
|
28
|
+
/** Creates an authenticated fetch function that automatically injects Luth bearer headers and credentials: 'include'. */
|
|
29
|
+
declare function createUniversalAuthFetch(authClient: {
|
|
30
|
+
getAuthHeaders: () => HeadersInit;
|
|
31
|
+
}): (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
32
|
+
|
|
33
|
+
export { CANONICAL_PUBLIC_ROUTES, type LuthDeploymentRecord, type LuthPrincipal, createUniversalAuthFetch, extractLuthToken, isPublicRoute, verifyLuthRequest };
|
|
@@ -1,22 +1,32 @@
|
|
|
1
1
|
import {
|
|
2
|
+
CANONICAL_PUBLIC_ROUTES,
|
|
2
3
|
DEFAULT_LUTH_URL,
|
|
3
4
|
LuthAuthClient,
|
|
4
5
|
LuthAuthClient2,
|
|
5
6
|
LuthAuthError,
|
|
6
7
|
createLuthAuthClient,
|
|
7
8
|
createLuthAuthClient2,
|
|
9
|
+
createUniversalAuthFetch,
|
|
10
|
+
extractLuthToken,
|
|
8
11
|
hydrateLuthTokenFromUrl,
|
|
12
|
+
isPublicRoute,
|
|
9
13
|
resolveClientEnvironment,
|
|
10
|
-
resolveClientOrigin
|
|
11
|
-
|
|
14
|
+
resolveClientOrigin,
|
|
15
|
+
verifyLuthRequest
|
|
16
|
+
} from "./chunk-5JMYSB7L.js";
|
|
12
17
|
export {
|
|
18
|
+
CANONICAL_PUBLIC_ROUTES,
|
|
13
19
|
LuthAuthClient2 as CanonicalAuthClient,
|
|
14
20
|
DEFAULT_LUTH_URL,
|
|
15
21
|
LuthAuthClient,
|
|
16
22
|
LuthAuthError,
|
|
17
23
|
createLuthAuthClient2 as createCanonicalAuthClient,
|
|
18
24
|
createLuthAuthClient,
|
|
25
|
+
createUniversalAuthFetch,
|
|
26
|
+
extractLuthToken,
|
|
19
27
|
hydrateLuthTokenFromUrl,
|
|
28
|
+
isPublicRoute,
|
|
20
29
|
resolveClientEnvironment,
|
|
21
|
-
resolveClientOrigin
|
|
30
|
+
resolveClientOrigin,
|
|
31
|
+
verifyLuthRequest
|
|
22
32
|
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// src/auth.ts
|
|
2
|
+
import {
|
|
3
|
+
LuthAuthClient,
|
|
4
|
+
createLuthAuthClient,
|
|
5
|
+
hydrateLuthTokenFromUrl,
|
|
6
|
+
resolveClientOrigin,
|
|
7
|
+
resolveClientEnvironment,
|
|
8
|
+
LuthAuthError,
|
|
9
|
+
DEFAULT_LUTH_URL as DEFAULT_LUTH_URL2
|
|
10
|
+
} from "@dinilmusick/gutts/auth";
|
|
11
|
+
import {
|
|
12
|
+
LuthAuthClient as LuthAuthClient2,
|
|
13
|
+
createLuthAuthClient as createLuthAuthClient2
|
|
14
|
+
} from "@dinilmusick/gutts/auth";
|
|
15
|
+
|
|
16
|
+
// src/UniversalLuthGuard.ts
|
|
17
|
+
import { DEFAULT_LUTH_URL } from "@dinilmusick/gutts/auth";
|
|
18
|
+
var CANONICAL_PUBLIC_ROUTES = [
|
|
19
|
+
"/health",
|
|
20
|
+
"/api/health",
|
|
21
|
+
"/api/status",
|
|
22
|
+
"/api/auth",
|
|
23
|
+
/^\/api(\/v1)?\/auth(\/.*)?$/,
|
|
24
|
+
/^\/api(\/v1)?\/database\/status$/,
|
|
25
|
+
/^\/api(\/v1)?\/guest(\/.*)?$/,
|
|
26
|
+
/^\/api(\/v1)?\/discovery(\/.*)?$/,
|
|
27
|
+
/^\/api(\/v1)?\/spec(\/.*)?$/
|
|
28
|
+
];
|
|
29
|
+
var SESSION_CACHE_TTL_MS = 6e4;
|
|
30
|
+
var sessionCache = /* @__PURE__ */ new Map();
|
|
31
|
+
function extractLuthToken(request) {
|
|
32
|
+
const authorization = request.headers.get("authorization") || "";
|
|
33
|
+
const bearer = /^Bearer\s+([^\s]+)$/i.exec(authorization.trim());
|
|
34
|
+
if (bearer) return bearer[1];
|
|
35
|
+
const apiKey = request.headers.get("x-api-key")?.trim();
|
|
36
|
+
if (apiKey) return apiKey;
|
|
37
|
+
const cookieHeader = request.headers.get("cookie") || "";
|
|
38
|
+
const cookies = cookieHeader.split(";").map((c) => c.trim());
|
|
39
|
+
for (const prefix of ["better-auth.session_token=", "__Secure-better-auth.session_token=", "luth_session="]) {
|
|
40
|
+
const found = cookies.find((c) => c.startsWith(prefix));
|
|
41
|
+
if (found) {
|
|
42
|
+
try {
|
|
43
|
+
return decodeURIComponent(found.slice(prefix.length));
|
|
44
|
+
} catch {
|
|
45
|
+
return "";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return "";
|
|
50
|
+
}
|
|
51
|
+
function isPublicRoute(pathname, extraPublicRoutes) {
|
|
52
|
+
const routes = extraPublicRoutes ? [...CANONICAL_PUBLIC_ROUTES, ...extraPublicRoutes] : CANONICAL_PUBLIC_ROUTES;
|
|
53
|
+
return routes.some((route) => typeof route === "string" ? pathname === route : route.test(pathname));
|
|
54
|
+
}
|
|
55
|
+
async function verifyLuthRequest(request, options) {
|
|
56
|
+
const url = new URL(request.url);
|
|
57
|
+
if (request.method === "OPTIONS") return null;
|
|
58
|
+
if (isPublicRoute(url.pathname, options?.extraPublicRoutes)) return null;
|
|
59
|
+
const isMcp = url.pathname.includes("/mcp");
|
|
60
|
+
const token = extractLuthToken(request);
|
|
61
|
+
if (!token) {
|
|
62
|
+
const payload = isMcp ? { jsonrpc: "2.0", id: null, error: { code: -32001, message: "A verified Luth session is required." } } : { success: false, error: "A verified Luth session is required." };
|
|
63
|
+
return new Response(JSON.stringify(payload), { status: 401, headers: { "Content-Type": "application/json" } });
|
|
64
|
+
}
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
const cached = sessionCache.get(token);
|
|
67
|
+
if (cached && cached.expiresAt > now) {
|
|
68
|
+
return cached.principal;
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
const luthUrl = options?.luthUrl || DEFAULT_LUTH_URL;
|
|
72
|
+
const res = await fetch(`${luthUrl}/api/session`, {
|
|
73
|
+
headers: {
|
|
74
|
+
authorization: `Bearer ${token}`,
|
|
75
|
+
cookie: request.headers.get("cookie") || "",
|
|
76
|
+
origin: url.origin
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
if (!res.ok) {
|
|
80
|
+
const payload = isMcp ? { jsonrpc: "2.0", id: null, error: { code: -32001, message: "The Luth session is invalid or expired." } } : { success: false, error: "The Luth session is invalid or expired." };
|
|
81
|
+
return new Response(JSON.stringify(payload), { status: 401, headers: { "Content-Type": "application/json" } });
|
|
82
|
+
}
|
|
83
|
+
const data = await res.json();
|
|
84
|
+
const user = data?.user || (data?.authenticated ? data?.session?.user : null);
|
|
85
|
+
if (!user || !user.id) {
|
|
86
|
+
const payload = isMcp ? { jsonrpc: "2.0", id: null, error: { code: -32001, message: "The Luth session is invalid or expired." } } : { success: false, error: "The Luth session is invalid or expired." };
|
|
87
|
+
return new Response(JSON.stringify(payload), { status: 401, headers: { "Content-Type": "application/json" } });
|
|
88
|
+
}
|
|
89
|
+
const principal = {
|
|
90
|
+
id: String(user.id),
|
|
91
|
+
email: String(user.email || ""),
|
|
92
|
+
name: String(user.name || user.email?.split("@")[0] || ""),
|
|
93
|
+
role: String(user.role || "user")
|
|
94
|
+
};
|
|
95
|
+
sessionCache.set(token, { principal, expiresAt: now + SESSION_CACHE_TTL_MS });
|
|
96
|
+
return principal;
|
|
97
|
+
} catch {
|
|
98
|
+
const payload = isMcp ? { jsonrpc: "2.0", id: null, error: { code: -32603, message: "Canonical Luth authentication service unavailable." } } : { success: false, error: "Canonical Luth authentication service unavailable." };
|
|
99
|
+
return new Response(JSON.stringify(payload), { status: 503, headers: { "Content-Type": "application/json" } });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/auth.ts
|
|
104
|
+
function createUniversalAuthFetch(authClient) {
|
|
105
|
+
return async function universalAuthFetch(input, init = {}) {
|
|
106
|
+
const headers = new Headers(init.headers);
|
|
107
|
+
new Headers(authClient.getAuthHeaders()).forEach((value, key) => {
|
|
108
|
+
if (!headers.has(key)) headers.set(key, value);
|
|
109
|
+
});
|
|
110
|
+
return fetch(input, {
|
|
111
|
+
...init,
|
|
112
|
+
headers,
|
|
113
|
+
credentials: init.credentials ?? "include"
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export {
|
|
119
|
+
CANONICAL_PUBLIC_ROUTES,
|
|
120
|
+
extractLuthToken,
|
|
121
|
+
isPublicRoute,
|
|
122
|
+
verifyLuthRequest,
|
|
123
|
+
createUniversalAuthFetch,
|
|
124
|
+
LuthAuthClient,
|
|
125
|
+
createLuthAuthClient,
|
|
126
|
+
hydrateLuthTokenFromUrl,
|
|
127
|
+
resolveClientOrigin,
|
|
128
|
+
resolveClientEnvironment,
|
|
129
|
+
LuthAuthError,
|
|
130
|
+
DEFAULT_LUTH_URL2 as DEFAULT_LUTH_URL,
|
|
131
|
+
LuthAuthClient2,
|
|
132
|
+
createLuthAuthClient2
|
|
133
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { LuthDeploymentRecord } from './auth.js';
|
|
1
|
+
export { CANONICAL_PUBLIC_ROUTES, LuthDeploymentRecord, LuthPrincipal, createUniversalAuthFetch, extractLuthToken, isPublicRoute, verifyLuthRequest } from './auth.js';
|
|
2
2
|
export { LuthAuthClient as CanonicalAuthClient, DEFAULT_LUTH_URL, DeploymentEnvironment, EmailSignInCredentials, EmailSignUpData, GoogleSignInOptions, LuthAuthClient, LuthAuthClientConfig, LuthAuthError, LuthAuthTokenStore, LuthSession, LuthSessionResult, LuthUser, RegisterApplicationResult, createLuthAuthClient as createCanonicalAuthClient, createLuthAuthClient, hydrateLuthTokenFromUrl, resolveClientEnvironment, resolveClientOrigin } from '@dinilmusick/gutts/auth';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
|
+
CANONICAL_PUBLIC_ROUTES,
|
|
2
3
|
DEFAULT_LUTH_URL,
|
|
3
4
|
LuthAuthClient,
|
|
4
5
|
LuthAuthClient2,
|
|
5
6
|
LuthAuthError,
|
|
6
7
|
createLuthAuthClient,
|
|
7
8
|
createLuthAuthClient2,
|
|
9
|
+
createUniversalAuthFetch,
|
|
10
|
+
extractLuthToken,
|
|
8
11
|
hydrateLuthTokenFromUrl,
|
|
12
|
+
isPublicRoute,
|
|
9
13
|
resolveClientEnvironment,
|
|
10
|
-
resolveClientOrigin
|
|
11
|
-
|
|
14
|
+
resolveClientOrigin,
|
|
15
|
+
verifyLuthRequest
|
|
16
|
+
} from "./chunk-5JMYSB7L.js";
|
|
12
17
|
|
|
13
18
|
// src/browserExecutionClient.ts
|
|
14
19
|
var BrowserExecutionClient = class {
|
|
@@ -184,6 +189,7 @@ var OPFSWorkspaceStorage = class {
|
|
|
184
189
|
};
|
|
185
190
|
export {
|
|
186
191
|
BrowserExecutionClient,
|
|
192
|
+
CANONICAL_PUBLIC_ROUTES,
|
|
187
193
|
LuthAuthClient2 as CanonicalAuthClient,
|
|
188
194
|
DEFAULT_LUTH_URL,
|
|
189
195
|
LuthAuthClient,
|
|
@@ -193,7 +199,11 @@ export {
|
|
|
193
199
|
createBrowserExecutionClient,
|
|
194
200
|
createLuthAuthClient2 as createCanonicalAuthClient,
|
|
195
201
|
createLuthAuthClient,
|
|
202
|
+
createUniversalAuthFetch,
|
|
203
|
+
extractLuthToken,
|
|
196
204
|
hydrateLuthTokenFromUrl,
|
|
205
|
+
isPublicRoute,
|
|
197
206
|
resolveClientEnvironment,
|
|
198
|
-
resolveClientOrigin
|
|
207
|
+
resolveClientOrigin,
|
|
208
|
+
verifyLuthRequest
|
|
199
209
|
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
// src/auth.ts
|
|
2
|
-
import {
|
|
3
|
-
LuthAuthClient,
|
|
4
|
-
createLuthAuthClient,
|
|
5
|
-
hydrateLuthTokenFromUrl,
|
|
6
|
-
resolveClientOrigin,
|
|
7
|
-
resolveClientEnvironment,
|
|
8
|
-
LuthAuthError,
|
|
9
|
-
DEFAULT_LUTH_URL
|
|
10
|
-
} from "@dinilmusick/gutts/auth";
|
|
11
|
-
import {
|
|
12
|
-
LuthAuthClient as LuthAuthClient2,
|
|
13
|
-
createLuthAuthClient as createLuthAuthClient2
|
|
14
|
-
} from "@dinilmusick/gutts/auth";
|
|
15
|
-
|
|
16
|
-
export {
|
|
17
|
-
LuthAuthClient,
|
|
18
|
-
createLuthAuthClient,
|
|
19
|
-
hydrateLuthTokenFromUrl,
|
|
20
|
-
resolveClientOrigin,
|
|
21
|
-
resolveClientEnvironment,
|
|
22
|
-
LuthAuthError,
|
|
23
|
-
DEFAULT_LUTH_URL,
|
|
24
|
-
LuthAuthClient2,
|
|
25
|
-
createLuthAuthClient2
|
|
26
|
-
};
|