@benchme/site-kit 0.10.0 → 0.12.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/auth-routes.js +5 -2
- package/dist/auth-routes.js.map +1 -1
- package/dist/auth-service.d.ts +9 -1
- package/dist/auth-service.js +14 -3
- package/dist/auth-service.js.map +1 -1
- package/dist/domain-error.d.ts +6 -0
- package/dist/domain-error.js +12 -0
- package/dist/domain-error.js.map +1 -0
- package/dist/doors/bridge-credential.d.ts +47 -0
- package/dist/doors/bridge-credential.js +80 -0
- package/dist/doors/bridge-credential.js.map +1 -0
- package/dist/doors/caller.d.ts +25 -0
- package/dist/doors/caller.js +22 -0
- package/dist/doors/caller.js.map +1 -0
- package/dist/doors/door.d.ts +12 -0
- package/dist/doors/door.js +18 -0
- package/dist/doors/door.js.map +1 -0
- package/dist/doors/register-doors.d.ts +44 -0
- package/dist/doors/register-doors.js +92 -0
- package/dist/doors/register-doors.js.map +1 -0
- package/dist/doors/request-door.d.ts +19 -0
- package/dist/doors/request-door.js +29 -0
- package/dist/doors/request-door.js.map +1 -0
- package/dist/doors/session-carrier.d.ts +42 -0
- package/dist/doors/session-carrier.js +44 -0
- package/dist/doors/session-carrier.js.map +1 -0
- package/dist/doors/sign-in-tools.d.ts +23 -0
- package/dist/doors/sign-in-tools.js +69 -0
- package/dist/doors/sign-in-tools.js.map +1 -0
- package/dist/doors/sign-in.d.ts +20 -0
- package/dist/doors/sign-in.js +31 -0
- package/dist/doors/sign-in.js.map +1 -0
- package/dist/html.d.ts +5 -1
- package/dist/html.js +23 -4
- package/dist/html.js.map +1 -1
- package/dist/index.d.ts +18 -3
- package/dist/index.js +18 -3
- package/dist/index.js.map +1 -1
- package/dist/ledger/auth-rules.d.ts +11 -0
- package/dist/ledger/auth-rules.js +21 -0
- package/dist/ledger/auth-rules.js.map +1 -0
- package/dist/ledger/entry.d.ts +219 -0
- package/dist/ledger/entry.js +40 -0
- package/dist/ledger/entry.js.map +1 -0
- package/dist/ledger/ledger-route.d.ts +15 -0
- package/dist/ledger/ledger-route.js +25 -0
- package/dist/ledger/ledger-route.js.map +1 -0
- package/dist/ledger/ledger.d.ts +38 -0
- package/dist/ledger/ledger.js +55 -0
- package/dist/ledger/ledger.js.map +1 -0
- package/dist/ledger/ledgered.d.ts +36 -0
- package/dist/ledger/ledgered.js +108 -0
- package/dist/ledger/ledgered.js.map +1 -0
- package/dist/ledger/rules.d.ts +45 -0
- package/dist/ledger/rules.js +22 -0
- package/dist/ledger/rules.js.map +1 -0
- package/dist/ledger/schema.d.ts +23 -0
- package/dist/ledger/schema.js +56 -0
- package/dist/ledger/schema.js.map +1 -0
- package/dist/mcp-server.d.ts +11 -6
- package/dist/mcp-server.js +24 -15
- package/dist/mcp-server.js.map +1 -1
- package/dist/refusal.d.ts +8 -0
- package/dist/refusal.js +8 -0
- package/dist/refusal.js.map +1 -0
- package/dist/request-header.d.ts +3 -0
- package/dist/request-header.js +6 -0
- package/dist/request-header.js.map +1 -0
- package/dist/users-repo.d.ts +32 -0
- package/dist/users-repo.js +69 -0
- package/dist/users-repo.js.map +1 -1
- package/dist/webmcp.d.ts +19 -2
- package/dist/webmcp.js +25 -4
- package/dist/webmcp.js.map +1 -1
- package/dist/workspace-scope.d.ts +18 -2
- package/dist/workspace-scope.js +36 -8
- package/dist/workspace-scope.js.map +1 -1
- package/package.json +2 -2
package/dist/auth-routes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "@fastify/cookie";
|
|
2
2
|
import { AuthError } from "./auth-service.js";
|
|
3
|
-
import { authPages, shell } from "./html.js";
|
|
3
|
+
import { apiTokenOpens, authPages, shell } from "./html.js";
|
|
4
4
|
const form = (req) => (req.body ?? {});
|
|
5
5
|
const q = (req) => (req.query ?? {});
|
|
6
6
|
/** Reads the session cookie into a user, or null. */
|
|
@@ -84,12 +84,15 @@ export function registerAuthRoutes(app, d) {
|
|
|
84
84
|
if (!c || !c.user)
|
|
85
85
|
return;
|
|
86
86
|
const full = await d.auth.userForSession(req.workspaceId, req.cookies[d.cookie]);
|
|
87
|
-
return html(reply, shell(c, "Account", authPages.account(req.prefix, { email: c.user.email, displayName: c.user.displayName, apiToken: full?.apiToken ?? null })));
|
|
87
|
+
return html(reply, shell(c, "Account", authPages.account(req.prefix, { email: c.user.email, displayName: c.user.displayName, apiToken: full?.apiToken ?? null }, req.doors)));
|
|
88
88
|
});
|
|
89
89
|
app.post("/account/token", async (req, reply) => {
|
|
90
90
|
const c = await guard(req, reply);
|
|
91
91
|
if (!c || !c.user)
|
|
92
92
|
return;
|
|
93
|
+
// A token opens the REST API and the MCP server: where the workspace is served through neither, there is none to have.
|
|
94
|
+
if (!apiTokenOpens(req.doors))
|
|
95
|
+
return reply.code(404).type("text/html; charset=utf-8").send(shell(c, "Not found", "<h1>No such page</h1>"));
|
|
93
96
|
await d.auth.issueApiToken(req.workspaceId, c.user.email);
|
|
94
97
|
return reply.redirect(`${req.prefix}/account`, 303);
|
|
95
98
|
});
|
package/dist/auth-routes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-routes.js","sourceRoot":"","sources":["../src/auth-routes.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,CAAC;AAEzB,OAAO,EAAE,SAAS,EAAoB,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAiB,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"auth-routes.js","sourceRoot":"","sources":["../src/auth-routes.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,CAAC;AAEzB,OAAO,EAAE,SAAS,EAAoB,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,KAAK,EAAiB,MAAM,WAAW,CAAC;AAa3E,MAAM,IAAI,GAAG,CAAC,GAAmB,EAAK,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,IAAI,EAAE,CAAM,CA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GAAG,CAAC,CAAC;IACtD,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/auth-service.d.ts
CHANGED
|
@@ -34,7 +34,13 @@ export declare class AuthService {
|
|
|
34
34
|
signup(ws: string, email: string, password: string, displayName: string): Promise<UserRow>;
|
|
35
35
|
sendVerification(ws: string, email: string): Promise<void>;
|
|
36
36
|
verify(ws: string, email: string, code: string): Promise<void>;
|
|
37
|
-
|
|
37
|
+
/**
|
|
38
|
+
* `bindTo` stores the session under a token the caller already holds (an MCP
|
|
39
|
+
* session's id) instead of a fresh one, replacing whoever was signed in
|
|
40
|
+
* under it: a client that cannot keep a cookie stays signed in by what it
|
|
41
|
+
* already repeats on every request.
|
|
42
|
+
*/
|
|
43
|
+
login(ws: string, email: string, password: string, bindTo?: string): Promise<{
|
|
38
44
|
token: string;
|
|
39
45
|
expiresAt: Date;
|
|
40
46
|
}>;
|
|
@@ -43,3 +49,5 @@ export declare class AuthService {
|
|
|
43
49
|
userForApiToken(ws: string, token: string | undefined): Promise<UserRow | null>;
|
|
44
50
|
issueApiToken(ws: string, email: string): Promise<string>;
|
|
45
51
|
}
|
|
52
|
+
/** A password as the users table keeps it: `salt:key`, the key scrypt's of the password with that salt. */
|
|
53
|
+
export declare function hashPassword(password: string, salt: string): Promise<string>;
|
package/dist/auth-service.js
CHANGED
|
@@ -41,14 +41,22 @@ export class AuthService {
|
|
|
41
41
|
if (!ok)
|
|
42
42
|
throw new AuthError("BAD_CODE", "that code is wrong or has expired");
|
|
43
43
|
}
|
|
44
|
-
|
|
44
|
+
/**
|
|
45
|
+
* `bindTo` stores the session under a token the caller already holds (an MCP
|
|
46
|
+
* session's id) instead of a fresh one, replacing whoever was signed in
|
|
47
|
+
* under it: a client that cannot keep a cookie stays signed in by what it
|
|
48
|
+
* already repeats on every request.
|
|
49
|
+
*/
|
|
50
|
+
async login(ws, email, password, bindTo) {
|
|
45
51
|
const user = await this.d.users.getUser(ws, email.trim().toLowerCase());
|
|
46
52
|
if (!user || !(await matches(password, user.passwordHash)))
|
|
47
53
|
throw new AuthError("BAD_CREDENTIALS", "wrong email or password", 401);
|
|
48
54
|
if (!user.verifiedAt)
|
|
49
55
|
throw new AuthError("UNVERIFIED", "verify your email before signing in", 403);
|
|
50
|
-
const token = randomBytes(24).toString("hex");
|
|
56
|
+
const token = bindTo ?? randomBytes(24).toString("hex");
|
|
51
57
|
const expiresAt = new Date(this.now().getTime() + this.d.sessionTtlSeconds * 1000);
|
|
58
|
+
if (bindTo)
|
|
59
|
+
await this.d.users.deleteSession(ws, bindTo);
|
|
52
60
|
await this.d.users.createSession(ws, token, user.email, expiresAt);
|
|
53
61
|
return { token, expiresAt };
|
|
54
62
|
}
|
|
@@ -76,7 +84,10 @@ export class AuthService {
|
|
|
76
84
|
}
|
|
77
85
|
}
|
|
78
86
|
async function hash(password) {
|
|
79
|
-
|
|
87
|
+
return hashPassword(password, randomBytes(16).toString("hex"));
|
|
88
|
+
}
|
|
89
|
+
/** A password as the users table keeps it: `salt:key`, the key scrypt's of the password with that salt. */
|
|
90
|
+
export async function hashPassword(password, salt) {
|
|
80
91
|
const key = (await scrypt(password, salt, 32));
|
|
81
92
|
return `${salt}:${key.toString("hex")}`;
|
|
82
93
|
}
|
package/dist/auth-service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-service.js","sourceRoot":"","sources":["../src/auth-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,IAAI,QAAQ,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC1F,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGtC,MAAM,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;AAOnC,MAAM,OAAO,SAAU,SAAQ,KAAK;IAEvB;IAEA;IAHX,YACW,IAAY,EACrB,OAAe,EACN,SAAS,GAAG;QAErB,KAAK,CAAC,OAAO,CAAC,CAAC;QAJN,SAAI,G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AC,MAAM,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,CAAW,CAAC;IACzD,OAAO,GAAG,IAAI,IAAI,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;AAC1C,CAAC;AAED,KAAK,UAAU,OAAO,CAAC,QAAgB,EAAE,MAAc;IACrD,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACtC,IAAI,CAAC,IAAI,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAC;IAChC,MAAM,GAAG,GAAG,CAAC,MAAM,MAAM,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,CAAW,CAAC;IACzD,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACzC,OAAO,GAAG,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,IAAI,eAAe,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;AAC1E,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Thrown by a tool/domain operation to render a structured error (never a 500). */
|
|
2
|
+
export class DomainError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
status;
|
|
5
|
+
constructor(code, message, status = 422) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.name = "DomainError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=domain-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"domain-error.js","sourceRoot":"","sources":["../src/domain-error.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,MAAM,OAAO,WAAY,SAAQ,KAAK;IAEzB;IAEA;IAHX,YACW,IAAY,EACrB,OAAe,EACN,SAAS,GAAG;QAErB,KAAK,CAAC,OAAO,CAAC,CAAC;QAJN,SAAI,GAAJ,IAAI,CAAQ;QAEZ,WAAM,GAAN,MAAM,CAAM;QAGrB,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cookie a page's WebMCP bridge proves itself with. One name for every
|
|
3
|
+
* site: each site sets it on its own path, so a browser sends each site only
|
|
4
|
+
* its own.
|
|
5
|
+
*/
|
|
6
|
+
export declare const BRIDGE_COOKIE = "benchme_bridge";
|
|
7
|
+
/** The header the bridge sends its token in, on every tool call it makes. */
|
|
8
|
+
export declare const BRIDGE_TOKEN_HEADER = "x-benchme-bridge";
|
|
9
|
+
/** The token a page embeds for the bridge cookie `cookie`: an HMAC with the gateway secret over the workspace and the cookie. */
|
|
10
|
+
export declare function bridgeToken(secret: string, workspaceId: string, cookie: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* One request's bridge credential: the bridge cookie it carries, and the
|
|
13
|
+
* token a page embeds for that cookie.
|
|
14
|
+
*
|
|
15
|
+
* A page the site renders with the WebMCP door open embeds a token for the
|
|
16
|
+
* browser's bridge cookie (minting the cookie when the request carried none,
|
|
17
|
+
* so that several tabs share one), and the page's bridge sends that token back
|
|
18
|
+
* on every call to /mcp. A call is the page's bridge only when its token is
|
|
19
|
+
* the one for the cookie the same request carries. The cookie is HttpOnly, so
|
|
20
|
+
* neither a page's scripts nor whoever reads the page's HTML can read it:
|
|
21
|
+
* a token copied out of a page, or a header copied from the bridge, proves
|
|
22
|
+
* nothing without the cookie jar the page was served to.
|
|
23
|
+
*/
|
|
24
|
+
export declare class BridgeCredential {
|
|
25
|
+
private readonly secret;
|
|
26
|
+
private readonly workspaceId;
|
|
27
|
+
private readonly carried;
|
|
28
|
+
private minted;
|
|
29
|
+
private constructor();
|
|
30
|
+
/** The credential of a request for `workspaceId`, read from its Cookie header. */
|
|
31
|
+
static of(secret: string, workspaceId: string, cookieHeader: string | undefined): BridgeCredential;
|
|
32
|
+
/** Whether `token` is the one a page embedded for the bridge cookie this request carries (compared in constant time). */
|
|
33
|
+
proves(token: string | undefined): boolean;
|
|
34
|
+
/** The token for a page rendered on this request: the carried cookie's, or that of a cookie minted for it (once per request). */
|
|
35
|
+
issue(): string;
|
|
36
|
+
/** The cookie this request minted, for its response to set; undefined when it carried one (or rendered no bridge). */
|
|
37
|
+
get mintedCookie(): string | undefined;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The Set-Cookie for a minted bridge cookie: the site's own pages only
|
|
41
|
+
* (`path`, the site's prefix and a slash), never readable by a page's scripts,
|
|
42
|
+
* never sent with a request another site starts. A session cookie: it lasts as
|
|
43
|
+
* long as the browser does.
|
|
44
|
+
*/
|
|
45
|
+
export declare function bridgeSetCookie(value: string, path: string): string;
|
|
46
|
+
/** The value of cookie `name` in a Cookie header (the first, as a browser lists the most specific path first), or undefined. */
|
|
47
|
+
export declare function cookieValue(header: string | undefined, name: string): string | undefined;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* The cookie a page's WebMCP bridge proves itself with. One name for every
|
|
4
|
+
* site: each site sets it on its own path, so a browser sends each site only
|
|
5
|
+
* its own.
|
|
6
|
+
*/
|
|
7
|
+
export const BRIDGE_COOKIE = "benchme_bridge";
|
|
8
|
+
/** The header the bridge sends its token in, on every tool call it makes. */
|
|
9
|
+
export const BRIDGE_TOKEN_HEADER = "x-benchme-bridge";
|
|
10
|
+
/** What a bridge cookie looks like as a site mints one: 128 random bits in hex. Anything else a request carries under the name is not one. */
|
|
11
|
+
const COOKIE_VALUE = /^[0-9a-f]{32}$/;
|
|
12
|
+
/** The token a page embeds for the bridge cookie `cookie`: an HMAC with the gateway secret over the workspace and the cookie. */
|
|
13
|
+
export function bridgeToken(secret, workspaceId, cookie) {
|
|
14
|
+
return createHmac("sha256", secret).update(`bridge:${workspaceId}:${cookie}`).digest("hex");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* One request's bridge credential: the bridge cookie it carries, and the
|
|
18
|
+
* token a page embeds for that cookie.
|
|
19
|
+
*
|
|
20
|
+
* A page the site renders with the WebMCP door open embeds a token for the
|
|
21
|
+
* browser's bridge cookie (minting the cookie when the request carried none,
|
|
22
|
+
* so that several tabs share one), and the page's bridge sends that token back
|
|
23
|
+
* on every call to /mcp. A call is the page's bridge only when its token is
|
|
24
|
+
* the one for the cookie the same request carries. The cookie is HttpOnly, so
|
|
25
|
+
* neither a page's scripts nor whoever reads the page's HTML can read it:
|
|
26
|
+
* a token copied out of a page, or a header copied from the bridge, proves
|
|
27
|
+
* nothing without the cookie jar the page was served to.
|
|
28
|
+
*/
|
|
29
|
+
export class BridgeCredential {
|
|
30
|
+
secret;
|
|
31
|
+
workspaceId;
|
|
32
|
+
carried;
|
|
33
|
+
minted;
|
|
34
|
+
constructor(secret, workspaceId, carried) {
|
|
35
|
+
this.secret = secret;
|
|
36
|
+
this.workspaceId = workspaceId;
|
|
37
|
+
this.carried = carried;
|
|
38
|
+
}
|
|
39
|
+
/** The credential of a request for `workspaceId`, read from its Cookie header. */
|
|
40
|
+
static of(secret, workspaceId, cookieHeader) {
|
|
41
|
+
const value = cookieValue(cookieHeader, BRIDGE_COOKIE);
|
|
42
|
+
return new BridgeCredential(secret, workspaceId, value !== undefined && COOKIE_VALUE.test(value) ? value : undefined);
|
|
43
|
+
}
|
|
44
|
+
/** Whether `token` is the one a page embedded for the bridge cookie this request carries (compared in constant time). */
|
|
45
|
+
proves(token) {
|
|
46
|
+
if (this.carried === undefined || !token)
|
|
47
|
+
return false;
|
|
48
|
+
const expected = Buffer.from(bridgeToken(this.secret, this.workspaceId, this.carried));
|
|
49
|
+
const given = Buffer.from(token);
|
|
50
|
+
return expected.length === given.length && timingSafeEqual(expected, given);
|
|
51
|
+
}
|
|
52
|
+
/** The token for a page rendered on this request: the carried cookie's, or that of a cookie minted for it (once per request). */
|
|
53
|
+
issue() {
|
|
54
|
+
const cookie = this.carried ?? (this.minted ??= randomBytes(16).toString("hex"));
|
|
55
|
+
return bridgeToken(this.secret, this.workspaceId, cookie);
|
|
56
|
+
}
|
|
57
|
+
/** The cookie this request minted, for its response to set; undefined when it carried one (or rendered no bridge). */
|
|
58
|
+
get mintedCookie() {
|
|
59
|
+
return this.minted;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The Set-Cookie for a minted bridge cookie: the site's own pages only
|
|
64
|
+
* (`path`, the site's prefix and a slash), never readable by a page's scripts,
|
|
65
|
+
* never sent with a request another site starts. A session cookie: it lasts as
|
|
66
|
+
* long as the browser does.
|
|
67
|
+
*/
|
|
68
|
+
export function bridgeSetCookie(value, path) {
|
|
69
|
+
return `${BRIDGE_COOKIE}=${value}; Path=${path}; HttpOnly; SameSite=Strict`;
|
|
70
|
+
}
|
|
71
|
+
/** The value of cookie `name` in a Cookie header (the first, as a browser lists the most specific path first), or undefined. */
|
|
72
|
+
export function cookieValue(header, name) {
|
|
73
|
+
for (const pair of (header ?? "").split(";")) {
|
|
74
|
+
const at = pair.indexOf("=");
|
|
75
|
+
if (at > 0 && pair.slice(0, at).trim() === name)
|
|
76
|
+
return pair.slice(at + 1).trim();
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=bridge-credential.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge-credential.js","sourceRoot":"","sources":["../../src/doors/bridge-credential.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEvE;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,gBAAgB,CAAC;AAC9C,6EAA6E;AAC7E,MAAM,CAAC,MAAM,mBAAmB,GAAG,kBAAkB,CAAC;AAEtD,8IAA8I;AAC9I,MAAM,YAAY,GAAG,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EAAE,KAAK,CAAC,CAAC;IAC9E,CAAC;IAED,iIAAiI;IACjI,KAAK;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;QACjF,OAAO,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IAC5D,CAAC;IAED,sHAAsH;IACtH,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,KAAa,EAAE,IAAY;IACzD,OAAO,GAAG,aAAa,IAAI,KAAK,UAAU,IAAI,6BAA6B,CAAC;AAC9E,CAAC;AAED,gIAAgI;AAChI,MAAM,UAAU,WAAW,CAAC,MAA0B,EAAE,IAAY;IAClE,KAAK,MAAM,IAAI,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QAC7C,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,EAAE,GAAG,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,KAAK,IAAI;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IACpF,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Door } from "./door.js";
|
|
2
|
+
import type { SessionCarrier } from "./session-carrier.js";
|
|
3
|
+
/** Who is asking, for the length of one request. */
|
|
4
|
+
export type Caller = {
|
|
5
|
+
readonly door: Door;
|
|
6
|
+
/** The verifier reading state after the fact: it is shown the rows, and no ledger records that it looked. */
|
|
7
|
+
readonly observer: boolean;
|
|
8
|
+
/** The email of whoever is signed in on this request, or null for nobody. Resolved once, when first asked. */
|
|
9
|
+
actor(): Promise<string | null>;
|
|
10
|
+
/** How a sign-in made on this request stays with the caller. */
|
|
11
|
+
readonly session: SessionCarrier;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Carries a request's caller to whatever the request reaches, without every
|
|
15
|
+
* route, tool and repository method passing it along: a door enters the scope
|
|
16
|
+
* once, and the one place every door shares (the site's rules) reads it.
|
|
17
|
+
* One instance per app.
|
|
18
|
+
*/
|
|
19
|
+
export declare class CallerScope {
|
|
20
|
+
private readonly store;
|
|
21
|
+
run<T>(caller: Caller, fn: () => T): T;
|
|
22
|
+
current(): Caller | undefined;
|
|
23
|
+
}
|
|
24
|
+
/** Resolves once and remembers the answer, so a request that asks who is signed in ten times looks it up once. */
|
|
25
|
+
export declare function once<T>(resolve: () => Promise<T>): () => Promise<T>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
/**
|
|
3
|
+
* Carries a request's caller to whatever the request reaches, without every
|
|
4
|
+
* route, tool and repository method passing it along: a door enters the scope
|
|
5
|
+
* once, and the one place every door shares (the site's rules) reads it.
|
|
6
|
+
* One instance per app.
|
|
7
|
+
*/
|
|
8
|
+
export class CallerScope {
|
|
9
|
+
store = new AsyncLocalStorage();
|
|
10
|
+
run(caller, fn) {
|
|
11
|
+
return this.store.run(caller, fn);
|
|
12
|
+
}
|
|
13
|
+
current() {
|
|
14
|
+
return this.store.getStore();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Resolves once and remembers the answer, so a request that asks who is signed in ten times looks it up once. */
|
|
18
|
+
export function once(resolve) {
|
|
19
|
+
let answer;
|
|
20
|
+
return () => (answer ??= resolve());
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=caller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"caller.js","sourceRoot":"","sources":["../../src/doors/caller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAerD;;;;;GAKG;AACH,MAAM,OAAO,WAAW;IACL,KAAK,GAAG,IAAI,iBAAiB,EAAU,CAAC;IAEzD,GAAG,CAAI,MAAc,EAAE,EAAW;QAChC,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACpC,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC/B,CAAC;CACF;AAED,kHAAkH;AAClH,MAAM,UAAU,IAAI,CAAI,OAAyB;IAC/C,IAAI,MAA8B,CAAC;IACnC,OAAO,GAAG,EAAE,CAAC,CAAC,MAAM,KAAK,OAAO,EAAE,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Door } from "@benchme/core";
|
|
2
|
+
export { DOORS, DoorSet, type Door } from "@benchme/core";
|
|
3
|
+
/**
|
|
4
|
+
* The door a request came through: what its path leads to (`doorsAt`), and,
|
|
5
|
+
* at the MCP server, which of its two doors. A call that proves it is a
|
|
6
|
+
* page's WebMCP bridge (`viaBridge`: its token is the one for the bridge
|
|
7
|
+
* cookie it carries) is `webmcp`; any other call to /mcp is a direct MCP
|
|
8
|
+
* client's. Nothing a client can write into a request without the page's
|
|
9
|
+
* cookie says otherwise. The paths that are no door at all (the gateway's own
|
|
10
|
+
* /internal/, the probes) have none.
|
|
11
|
+
*/
|
|
12
|
+
export declare function doorOf(path: string, viaBridge?: boolean): Door | null;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { doorsAt } from "@benchme/core";
|
|
2
|
+
export { DOORS, DoorSet } from "@benchme/core";
|
|
3
|
+
/**
|
|
4
|
+
* The door a request came through: what its path leads to (`doorsAt`), and,
|
|
5
|
+
* at the MCP server, which of its two doors. A call that proves it is a
|
|
6
|
+
* page's WebMCP bridge (`viaBridge`: its token is the one for the bridge
|
|
7
|
+
* cookie it carries) is `webmcp`; any other call to /mcp is a direct MCP
|
|
8
|
+
* client's. Nothing a client can write into a request without the page's
|
|
9
|
+
* cookie says otherwise. The paths that are no door at all (the gateway's own
|
|
10
|
+
* /internal/, the probes) have none.
|
|
11
|
+
*/
|
|
12
|
+
export function doorOf(path, viaBridge = false) {
|
|
13
|
+
const doors = doorsAt(path);
|
|
14
|
+
if (viaBridge && doors.includes("webmcp"))
|
|
15
|
+
return "webmcp";
|
|
16
|
+
return doors.find((door) => door !== "webmcp") ?? null;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=door.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"door.js","sourceRoot":"","sources":["../../src/doors/door.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAa,MAAM,eAAe,CAAC;AAEnD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAa,MAAM,eAAe,CAAC;AAE1D;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAC,IAAY,EAAE,SAAS,GAAG,KAAK;IACpD,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,IAAI,SAAS,IAAI,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC;QAAE,OAAO,QAAQ,CAAC;IAC3D,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,QAAQ,CAAC,IAAI,IAAI,CAAC;AACzD,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import "@fastify/cookie";
|
|
2
|
+
import type { Transactions } from "@benchme/core";
|
|
3
|
+
import type { FastifyInstance } from "fastify";
|
|
4
|
+
import type { AuthService } from "../auth-service.js";
|
|
5
|
+
import type { Ledger } from "../ledger/ledger.js";
|
|
6
|
+
import type { LedgerRules } from "../ledger/rules.js";
|
|
7
|
+
import type { ToolRegistry } from "../tool-registry.js";
|
|
8
|
+
import { CallerScope } from "./caller.js";
|
|
9
|
+
/** The header an MCP client repeats the session id in (the protocol's own). */
|
|
10
|
+
export declare const MCP_SESSION_HEADER = "mcp-session-id";
|
|
11
|
+
export type DoorsDeps = {
|
|
12
|
+
/** The site's change ledger (its own schema's table: `new PgLedger(db, "<app>")`). */
|
|
13
|
+
ledger: Ledger;
|
|
14
|
+
/**
|
|
15
|
+
* The site's `transactions(pool)`. Its pool is the one the site's repositories and accounts are built with, so a
|
|
16
|
+
* write and its ledger entry are one transaction (`untransacted` for a site whose rows are not in Postgres).
|
|
17
|
+
*/
|
|
18
|
+
transactions: Pick<Transactions, "run">;
|
|
19
|
+
auth: AuthService;
|
|
20
|
+
/** The site's session cookie. */
|
|
21
|
+
cookie: string;
|
|
22
|
+
gatewaySecret: string;
|
|
23
|
+
};
|
|
24
|
+
export type Doors = {
|
|
25
|
+
callers: CallerScope;
|
|
26
|
+
ledger: Ledger;
|
|
27
|
+
/** The site's sign-up and sign-in with the ledger kept: hand this one to the routes. */
|
|
28
|
+
auth: AuthService;
|
|
29
|
+
/** The site's rules with the ledger kept, and a sign-in asked of every write: see `ledgered`. */
|
|
30
|
+
ledgered<R extends object>(repo: R, rules: LedgerRules<R>): R;
|
|
31
|
+
/** Adds the sign-in tools to the site's own, so its MCP server and its pages' WebMCP bridge both carry them. */
|
|
32
|
+
withSignIn<C extends {
|
|
33
|
+
workspaceId: string;
|
|
34
|
+
}>(tools: ToolRegistry<C>): ToolRegistry<C>;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Gives a site its doors. Every request is entered in the caller scope with
|
|
38
|
+
* the door it came through and whoever is signed in; the site's rules are
|
|
39
|
+
* wrapped to keep the ledger and to ask a sign-in of every write; a write by
|
|
40
|
+
* nobody is answered by the door it came through; and the verifier gets its
|
|
41
|
+
* route to read the ledger. Call it once, after the workspace scope and
|
|
42
|
+
* before any route is added.
|
|
43
|
+
*/
|
|
44
|
+
export declare function registerDoors(app: FastifyInstance, d: DoorsDeps): Doors;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import "@fastify/cookie";
|
|
2
|
+
import { AUTH_LEDGER } from "../ledger/auth-rules.js";
|
|
3
|
+
import { registerLedgerRoute } from "../ledger/ledger-route.js";
|
|
4
|
+
import { ledgered } from "../ledger/ledgered.js";
|
|
5
|
+
import { header } from "../request-header.js";
|
|
6
|
+
import { CallerScope, once } from "./caller.js";
|
|
7
|
+
import { isObserver } from "./request-door.js";
|
|
8
|
+
import { CookieCarrier, McpSessionCarrier, NoCarrier, isMcpSessionId } from "./session-carrier.js";
|
|
9
|
+
import { registerSignInTools } from "./sign-in-tools.js";
|
|
10
|
+
import { SignInRequired } from "./sign-in.js";
|
|
11
|
+
/** The header an MCP client repeats the session id in (the protocol's own). */
|
|
12
|
+
export const MCP_SESSION_HEADER = "mcp-session-id";
|
|
13
|
+
/**
|
|
14
|
+
* Gives a site its doors. Every request is entered in the caller scope with
|
|
15
|
+
* the door it came through and whoever is signed in; the site's rules are
|
|
16
|
+
* wrapped to keep the ledger and to ask a sign-in of every write; a write by
|
|
17
|
+
* nobody is answered by the door it came through; and the verifier gets its
|
|
18
|
+
* route to read the ledger. Call it once, after the workspace scope and
|
|
19
|
+
* before any route is added.
|
|
20
|
+
*/
|
|
21
|
+
export function registerDoors(app, d) {
|
|
22
|
+
const callers = new CallerScope();
|
|
23
|
+
const { ledger } = d;
|
|
24
|
+
const units = { ledger, callers, transactions: d.transactions };
|
|
25
|
+
const auth = ledgered(d.auth, AUTH_LEDGER, units);
|
|
26
|
+
// A callback hook on purpose: the rest of the request runs inside `done`, so inside the scope. The door is the one the
|
|
27
|
+
// workspace scope read off the request (its route, and at /mcp the page's bridge credential).
|
|
28
|
+
app.addHook("preHandler", (req, reply, done) => {
|
|
29
|
+
const door = req.door;
|
|
30
|
+
if (!door)
|
|
31
|
+
return done();
|
|
32
|
+
callers.run(callerOf(app, req, reply, door, d), done);
|
|
33
|
+
});
|
|
34
|
+
// The rules refuse a write by nobody (SignInRequired); each door says so its own way. A tool's refusal never
|
|
35
|
+
// gets here: the MCP server turns it into the tool's result.
|
|
36
|
+
app.addHook("onRoute", (route) => {
|
|
37
|
+
const inherited = route.errorHandler;
|
|
38
|
+
route.errorHandler = function (err, req, reply) {
|
|
39
|
+
if (!(err instanceof SignInRequired))
|
|
40
|
+
return (inherited ?? app.errorHandler).call(this, err, req, reply);
|
|
41
|
+
if (err.door === "page")
|
|
42
|
+
return reply.redirect(`${req.prefix}/login`, 302);
|
|
43
|
+
return reply.code(err.status).send({ error: err.code, message: err.message });
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
registerLedgerRoute(app, { ledger, gatewaySecret: d.gatewaySecret });
|
|
47
|
+
return {
|
|
48
|
+
callers,
|
|
49
|
+
ledger,
|
|
50
|
+
auth,
|
|
51
|
+
ledgered: (repo, rules) => ledgered(repo, rules, units),
|
|
52
|
+
withSignIn: (tools) => registerSignInTools(tools, { auth, callers }),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A request's caller: the door, whether it is the verifier's own read, and who
|
|
57
|
+
* is signed in. Every door is asked the same three things, in one order: an
|
|
58
|
+
* api token (`Authorization: Bearer`), the site's session cookie, the MCP
|
|
59
|
+
* session a sign-in tool signed in.
|
|
60
|
+
*/
|
|
61
|
+
function callerOf(app, req, reply, door, d) {
|
|
62
|
+
const ws = req.workspaceId;
|
|
63
|
+
const bearer = (req.headers.authorization ?? "").replace(/^Bearer\s+/i, "");
|
|
64
|
+
const session = carrierOf(app, req, reply, door, d.cookie);
|
|
65
|
+
return {
|
|
66
|
+
door,
|
|
67
|
+
observer: isObserver(req, d.gatewaySecret),
|
|
68
|
+
session,
|
|
69
|
+
actor: once(async () => {
|
|
70
|
+
const user = (await d.auth.userForApiToken(ws, bearer || undefined)) ?? (await d.auth.userForSession(ws, req.cookies[d.cookie])) ?? (await d.auth.userForSession(ws, session.token));
|
|
71
|
+
return user?.email ?? null;
|
|
72
|
+
}),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Where a sign-in made on this request would be kept. An MCP client has its
|
|
77
|
+
* session id. A page's bridge has the browser's cookie jar: the answer to its
|
|
78
|
+
* call is written straight to the socket (the MCP transport owns it), so the
|
|
79
|
+
* cookie goes on the raw response, in the words the pages' own sign-in uses.
|
|
80
|
+
* A page or REST request signs in on the sign-in page, never through a tool.
|
|
81
|
+
*/
|
|
82
|
+
function carrierOf(app, req, reply, door, cookie) {
|
|
83
|
+
const mcpSession = header(req, MCP_SESSION_HEADER);
|
|
84
|
+
if (door === "mcp" && isMcpSessionId(mcpSession))
|
|
85
|
+
return new McpSessionCarrier(mcpSession);
|
|
86
|
+
if (door !== "webmcp")
|
|
87
|
+
return new NoCarrier();
|
|
88
|
+
return new CookieCarrier(req.cookies[cookie], (token, expires) => {
|
|
89
|
+
reply.raw.setHeader("set-cookie", app.serializeCookie(cookie, token, { path: `${req.prefix || ""}/`, httpOnly: true, sameSite: "lax", expires }));
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=register-doors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register-doors.js","sourceRoot":"","sources":["../../src/doors/register-doors.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAiB,CAAC;AAIzB,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAEhE,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAGjD,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,IAAI,EAAe,MAAM,aAAa,CAAC;AAE7D,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO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G,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;IACpJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Door } from "@benchme/core";
|
|
2
|
+
import type { FastifyRequest } from "fastify";
|
|
3
|
+
import { type BridgeCredential } from "./bridge-credential.js";
|
|
4
|
+
/**
|
|
5
|
+
* The path a request was routed by: its route's own pattern, so an escape
|
|
6
|
+
* (`/%61pi/v1/…`, which the router decodes to the REST route) cannot pass the
|
|
7
|
+
* REST API off as a page; the URL's path where no route matched.
|
|
8
|
+
*/
|
|
9
|
+
export declare function routedPath(req: FastifyRequest): string;
|
|
10
|
+
/** The door `req` came through: its route, and at /mcp whether it proves it is a page's bridge. */
|
|
11
|
+
export declare function requestDoor(req: FastifyRequest, bridge: BridgeCredential): Door | null;
|
|
12
|
+
/** The verifier reading state after the fact: its own signature, made with the gateway secret, for this workspace. */
|
|
13
|
+
export declare function isObserver(req: FastifyRequest, gatewaySecret: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Whether `req` came through a door its workspace is not served through. The
|
|
16
|
+
* verifier's own reads are no visitor's, so no door is closed to them: only a
|
|
17
|
+
* holder of the gateway secret can sign one, and the gateway itself never does.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isClosed(req: FastifyRequest, gatewaySecret: string): boolean;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { VERIFIER_SIG_HEADER, verifyVerifierHeader } from "@benchme/core";
|
|
2
|
+
import { header } from "../request-header.js";
|
|
3
|
+
import { BRIDGE_TOKEN_HEADER } from "./bridge-credential.js";
|
|
4
|
+
import { doorOf } from "./door.js";
|
|
5
|
+
/**
|
|
6
|
+
* The path a request was routed by: its route's own pattern, so an escape
|
|
7
|
+
* (`/%61pi/v1/…`, which the router decodes to the REST route) cannot pass the
|
|
8
|
+
* REST API off as a page; the URL's path where no route matched.
|
|
9
|
+
*/
|
|
10
|
+
export function routedPath(req) {
|
|
11
|
+
return req.routeOptions.url ?? req.url.split("?")[0] ?? "";
|
|
12
|
+
}
|
|
13
|
+
/** The door `req` came through: its route, and at /mcp whether it proves it is a page's bridge. */
|
|
14
|
+
export function requestDoor(req, bridge) {
|
|
15
|
+
return doorOf(routedPath(req), bridge.proves(header(req, BRIDGE_TOKEN_HEADER)));
|
|
16
|
+
}
|
|
17
|
+
/** The verifier reading state after the fact: its own signature, made with the gateway secret, for this workspace. */
|
|
18
|
+
export function isObserver(req, gatewaySecret) {
|
|
19
|
+
return verifyVerifierHeader(gatewaySecret, req.workspaceId, header(req, VERIFIER_SIG_HEADER) ?? "");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether `req` came through a door its workspace is not served through. The
|
|
23
|
+
* verifier's own reads are no visitor's, so no door is closed to them: only a
|
|
24
|
+
* holder of the gateway secret can sign one, and the gateway itself never does.
|
|
25
|
+
*/
|
|
26
|
+
export function isClosed(req, gatewaySecret) {
|
|
27
|
+
return req.door !== null && !req.doors.has(req.door) && !isObserver(req, gatewaySecret);
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=request-door.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-door.js","sourceRoot":"","sources":["../../src/doors/request-door.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAa,MAAM,eAAe,CAAC;AAErF,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAyB,MAAM,wBAAwB,CAAC;AACpF,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,GAAmB;IAC5C,OAAO,GAAG,CAAC,YAAY,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AAC7D,CAAC;AAED,mGAAmG;AACnG,MAAM,UAAU,WAAW,CAAC,GAAmB,EAAE,MAAwB;IACvE,OAAO,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,mBAAmB,CAAC,CAAC,CAAC,CAAC;AAClF,CAAC;AAED,sHAAsH;AACtH,MAAM,UAAU,UAAU,CAAC,GAAmB,EAAE,aAAqB;IACnE,OAAO,oBAAoB,CAAC,aAAa,EAAE,GAAG,CAAC,WAAW,EAAE,MAAM,CAAC,GAAG,EAAE,mBAAmB,CAAC,IAAI,EAAE,CAAC,CAAC;AACtG,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAmB,EAAE,aAAqB;IACjE,OAAO,GAAG,CAAC,IAAI,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;AAC1F,CAAC"}
|