@nodetool-ai/auth 0.7.0-rc.23 → 0.8.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/README.md +48 -0
- package/dist/.nodetool-build-stamp +1 -0
- package/dist/auth-provider.d.ts +16 -10
- package/dist/auth-provider.d.ts.map +1 -1
- package/dist/auth-provider.js +14 -6
- package/dist/auth-provider.js.map +1 -1
- package/dist/file-user-manager.d.ts +0 -1
- package/dist/file-user-manager.d.ts.map +1 -1
- package/dist/file-user-manager.js +28 -8
- package/dist/file-user-manager.js.map +1 -1
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/dist/json-wire.d.ts +38 -0
- package/dist/json-wire.d.ts.map +1 -0
- package/dist/json-wire.js +36 -0
- package/dist/json-wire.js.map +1 -0
- package/dist/providers/app-session-token-provider.d.ts +80 -0
- package/dist/providers/app-session-token-provider.d.ts.map +1 -0
- package/dist/providers/app-session-token-provider.js +180 -0
- package/dist/providers/app-session-token-provider.js.map +1 -0
- package/dist/providers/delegated-token-provider.d.ts +66 -0
- package/dist/providers/delegated-token-provider.d.ts.map +1 -0
- package/dist/providers/delegated-token-provider.js +136 -0
- package/dist/providers/delegated-token-provider.js.map +1 -0
- package/dist/providers/local-provider.d.ts.map +1 -1
- package/dist/providers/supabase-provider.d.ts +2 -1
- package/dist/providers/supabase-provider.d.ts.map +1 -1
- package/dist/providers/supabase-provider.js +78 -12
- package/dist/providers/supabase-provider.js.map +1 -1
- package/package.json +23 -6
- package/dist/http-auth.d.ts +0 -22
- package/dist/http-auth.d.ts.map +0 -1
- package/dist/http-auth.js +0 -51
- package/dist/http-auth.js.map +0 -1
- package/dist/middleware.d.ts +0 -37
- package/dist/middleware.d.ts.map +0 -1
- package/dist/middleware.js +0 -69
- package/dist/middleware.js.map +0 -1
- package/dist/providers/multi-user-provider.d.ts +0 -16
- package/dist/providers/multi-user-provider.d.ts.map +0 -1
- package/dist/providers/multi-user-provider.js +0 -40
- package/dist/providers/multi-user-provider.js.map +0 -1
- package/dist/providers/static-token-provider.d.ts +0 -23
- package/dist/providers/static-token-provider.d.ts.map +0 -1
- package/dist/providers/static-token-provider.js +0 -57
- package/dist/providers/static-token-provider.js.map +0 -1
- package/dist/user-manager.d.ts +0 -23
- package/dist/user-manager.d.ts.map +0 -1
- package/dist/user-manager.js +0 -30
- package/dist/user-manager.js.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# @nodetool-ai/auth
|
|
2
|
+
|
|
3
|
+
Authentication and authorization for [NodeTool](https://nodetool.ai) — pluggable auth providers (local, Supabase, delegated and app-session tokens) and a file-backed user store.
|
|
4
|
+
|
|
5
|
+
This package owns the auth surface for the NodeTool backend: token verification, user records and admin/role checks. The server (`packages/websocket`) picks a provider at startup and gates HTTP and WebSocket requests in its own `onRequest` hook.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @nodetool-ai/auth
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Exported symbols
|
|
14
|
+
|
|
15
|
+
| Symbol | Kind | Description |
|
|
16
|
+
| --- | --- | --- |
|
|
17
|
+
| `AuthProvider` | class | Base contract every auth provider implements |
|
|
18
|
+
| `AuthResult` | type | Result of a token verification attempt |
|
|
19
|
+
| `TokenType` | enum | Distinguishes static and user tokens |
|
|
20
|
+
| `LocalAuthProvider` | class | Single-user local auth (no external service) |
|
|
21
|
+
| `SupabaseAuthProvider` | class | Verifies Supabase-issued JWTs |
|
|
22
|
+
| `DelegatedTokenProvider` | class | Verifies tokens minted for an external integration |
|
|
23
|
+
| `AppSessionTokenProvider` | class | Verifies scoped tokens minted for a deployed app session |
|
|
24
|
+
| `mintDelegatedToken` / `mintAppSessionToken` | function | Mint the two token kinds above |
|
|
25
|
+
| `isDelegatedToken` / `isAppSessionToken` | function | Prefix checks used to route a token to its provider |
|
|
26
|
+
| `isAdmin` | function | Role check for `{ role: "admin" }` users |
|
|
27
|
+
| `FileUserManager` | class | File-backed user store |
|
|
28
|
+
| `User` / `UserRecord` | interface | User record shapes |
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { LocalAuthProvider, SupabaseAuthProvider } from "@nodetool-ai/auth";
|
|
34
|
+
|
|
35
|
+
const provider = process.env.SUPABASE_JWT_SECRET
|
|
36
|
+
? new SupabaseAuthProvider({ supabaseJwtSecret: process.env.SUPABASE_JWT_SECRET })
|
|
37
|
+
: new LocalAuthProvider();
|
|
38
|
+
|
|
39
|
+
const token = provider.extractTokenFromHeaders(request.headers);
|
|
40
|
+
const result = await provider.verifyToken(token ?? "");
|
|
41
|
+
if (!result.ok) throw new Error(result.error);
|
|
42
|
+
console.log(result.userId);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Links
|
|
46
|
+
|
|
47
|
+
- [NodeTool](https://nodetool.ai)
|
|
48
|
+
- [GitHub](https://github.com/nodetool-ai/nodetool)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2026-09-28T09:19:02.743Z
|
package/dist/auth-provider.d.ts
CHANGED
|
@@ -5,17 +5,29 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export declare enum TokenType {
|
|
7
7
|
STATIC = "static",
|
|
8
|
-
USER = "user"
|
|
8
|
+
USER = "user",
|
|
9
|
+
/**
|
|
10
|
+
* A visitor holding a deployed app's hidden URL. Authenticates as the app
|
|
11
|
+
* owner for that one app's runs and nothing else, so it is a distinct type
|
|
12
|
+
* rather than a `USER` with a note attached — a host that does not know
|
|
13
|
+
* about app sessions cannot mistake one for a full session.
|
|
14
|
+
*/
|
|
15
|
+
APP_SESSION = "app_session"
|
|
9
16
|
}
|
|
10
17
|
export interface AuthResult {
|
|
11
18
|
ok: boolean;
|
|
12
19
|
userId?: string;
|
|
13
20
|
tokenType?: TokenType;
|
|
14
21
|
error?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The one application an `APP_SESSION` token may act on. Set by no other
|
|
24
|
+
* provider, so its presence is what confines the session; a host that reads
|
|
25
|
+
* `userId` without it is holding an unscoped identity by construction.
|
|
26
|
+
*/
|
|
27
|
+
applicationId?: string;
|
|
28
|
+
/** The released version the visitor loaded, for run attribution. */
|
|
29
|
+
applicationVersion?: number;
|
|
15
30
|
}
|
|
16
|
-
/**
|
|
17
|
-
* Common interface for all authentication providers.
|
|
18
|
-
*/
|
|
19
31
|
export declare abstract class AuthProvider {
|
|
20
32
|
static preferHeader(): string;
|
|
21
33
|
/**
|
|
@@ -31,13 +43,7 @@ export declare abstract class AuthProvider {
|
|
|
31
43
|
* Compatibility fallback: allow `?api_key=<token>` query parameter.
|
|
32
44
|
*/
|
|
33
45
|
extractTokenFromWs(headers: Record<string, string> | Headers, queryParams?: Record<string, string> | URLSearchParams): string | null;
|
|
34
|
-
/**
|
|
35
|
-
* Validate a token and return the associated authentication result.
|
|
36
|
-
*/
|
|
37
46
|
abstract verifyToken(token: string): Promise<AuthResult>;
|
|
38
|
-
/**
|
|
39
|
-
* Clear any internal caches held by the provider.
|
|
40
|
-
*/
|
|
41
47
|
clearCaches(): void;
|
|
42
48
|
}
|
|
43
49
|
//# sourceMappingURL=auth-provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-provider.d.ts","sourceRoot":"","sources":["../src/auth-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,oBAAY,SAAS;IACnB,MAAM,WAAW;IACjB,IAAI,SAAS;
|
|
1
|
+
{"version":3,"file":"auth-provider.d.ts","sourceRoot":"","sources":["../src/auth-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,oBAAY,SAAS;IACnB,MAAM,WAAW;IACjB,IAAI,SAAS;IACb;;;;;OAKG;IACH,WAAW,gBAAgB;CAC5B;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,oEAAoE;IACpE,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,8BAAsB,YAAY;IAChC,MAAM,CAAC,YAAY,IAAI,MAAM,CAE5B;IAED;;;;;OAKG;IACH,uBAAuB,CACrB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,GACxC,MAAM,GAAG,IAAI,CAiCf;IAED;;;;OAIG;IACH,kBAAkB,CAChB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,EACzC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,eAAe,GACrD,MAAM,GAAG,IAAI,CAuBf;IAED,QAAQ,CAAC,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAEzD,WAAW,IAAI,IAAI,CAElB;CACF"}
|
package/dist/auth-provider.js
CHANGED
|
@@ -7,10 +7,14 @@ export var TokenType;
|
|
|
7
7
|
(function (TokenType) {
|
|
8
8
|
TokenType["STATIC"] = "static";
|
|
9
9
|
TokenType["USER"] = "user";
|
|
10
|
+
/**
|
|
11
|
+
* A visitor holding a deployed app's hidden URL. Authenticates as the app
|
|
12
|
+
* owner for that one app's runs and nothing else, so it is a distinct type
|
|
13
|
+
* rather than a `USER` with a note attached — a host that does not know
|
|
14
|
+
* about app sessions cannot mistake one for a full session.
|
|
15
|
+
*/
|
|
16
|
+
TokenType["APP_SESSION"] = "app_session";
|
|
10
17
|
})(TokenType || (TokenType = {}));
|
|
11
|
-
/**
|
|
12
|
-
* Common interface for all authentication providers.
|
|
13
|
-
*/
|
|
14
18
|
export class AuthProvider {
|
|
15
19
|
static preferHeader() {
|
|
16
20
|
return "authorization";
|
|
@@ -25,9 +29,14 @@ export class AuthProvider {
|
|
|
25
29
|
const headerName = AuthProvider.preferHeader();
|
|
26
30
|
let authHeader;
|
|
27
31
|
if (headers instanceof Headers) {
|
|
32
|
+
// Stryker disable all: Headers.get is case-insensitive, so the capitalized
|
|
33
|
+
// fallback always resolves to the same value as the primary lookup. Every
|
|
34
|
+
// mutant on these two lines (`??`→`&&`, the regex, the callback) is
|
|
35
|
+
// therefore equivalent — no input produces an observable difference.
|
|
28
36
|
authHeader =
|
|
29
37
|
headers.get(headerName) ??
|
|
30
38
|
headers.get(headerName.replace(/\b\w/g, (c) => c.toUpperCase()));
|
|
39
|
+
// Stryker restore all
|
|
31
40
|
}
|
|
32
41
|
else {
|
|
33
42
|
authHeader =
|
|
@@ -42,6 +51,8 @@ export class AuthProvider {
|
|
|
42
51
|
if (parts.length !== 2 || parts[0].toLowerCase() !== "bearer") {
|
|
43
52
|
return null;
|
|
44
53
|
}
|
|
54
|
+
// Stryker disable next-line MethodExpression: split(/\s+/) yields tokens with
|
|
55
|
+
// no surrounding whitespace, so trim() is a no-op here (equivalent mutant).
|
|
45
56
|
const token = parts[1].trim();
|
|
46
57
|
return token || null;
|
|
47
58
|
}
|
|
@@ -71,9 +82,6 @@ export class AuthProvider {
|
|
|
71
82
|
}
|
|
72
83
|
return null;
|
|
73
84
|
}
|
|
74
|
-
/**
|
|
75
|
-
* Clear any internal caches held by the provider.
|
|
76
|
-
*/
|
|
77
85
|
clearCaches() {
|
|
78
86
|
// No-op by default.
|
|
79
87
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-provider.js","sourceRoot":"","sources":["../src/auth-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,CAAN,IAAY,
|
|
1
|
+
{"version":3,"file":"auth-provider.js","sourceRoot":"","sources":["../src/auth-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH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AAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC9B,OAAO,KAAK,IAAI,IAAI,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,kBAAkB,CAChB,OAAyC,EACzC,WAAsD;QAEtD,MAAM,KAAK,GAAG,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,CAAC;QACpD,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,KAAK,CAAC;QACf,CAAC;QAED,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,IAAI,MAAiC,CAAC;QACtC,IAAI,WAAW,YAAY,eAAe,EAAE,CAAC;YAC3C,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACtC,CAAC;aAAM,CAAC;YACN,MAAM,GAAG,WAAW,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC;QAC1C,CAAC;QAED,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;YACvB,OAAO,MAAM,IAAI,IAAI,CAAC;QACxB,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAID,WAAW;QACT,oBAAoB;IACtB,CAAC;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-user-manager.d.ts","sourceRoot":"","sources":["../src/file-user-manager.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"file-user-manager.d.ts","sourceRoot":"","sources":["../src/file-user-manager.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAOH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACnC;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;CACnB;AAcD,qBAAa,eAAe;IAC1B,OAAO,CAAC,SAAS,CAAS;IAC1B,kEAAkE;IAClE,OAAO,CAAC,SAAS,CAAoC;IAErD,YAAY,SAAS,CAAC,EAAE,MAAM,EAE7B;IAED,iFAAiF;YACnE,QAAQ;YAeR,IAAI;YAeJ,IAAI;IAelB,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,SAAS;IAIjB,OAAO,CAAC,cAAc;IAIhB,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,SAAS,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAmBxE;IAEK,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAShD;IAEK,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAyB5D;IAEK,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAGrD;IAEK,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAM1D;CACF"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* FileUserManager — file-based persistent user management.
|
|
3
3
|
*
|
|
4
|
-
* Port of Python's `nodetool.security.user_manager.UserManager`.
|
|
5
4
|
* Stores users in a JSON file at `~/.config/nodetool/users.json` by default.
|
|
6
5
|
*/
|
|
7
6
|
import { createHash, randomBytes } from "node:crypto";
|
|
8
|
-
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
7
|
+
import { readFile, writeFile, mkdir, chmod } from "node:fs/promises";
|
|
9
8
|
import { dirname, join } from "node:path";
|
|
10
9
|
import { homedir } from "node:os";
|
|
11
10
|
function defaultUsersFilePath() {
|
|
@@ -44,7 +43,13 @@ export class FileUserManager {
|
|
|
44
43
|
}
|
|
45
44
|
async load() {
|
|
46
45
|
try {
|
|
46
|
+
// Stryker disable next-line StringLiteral: an empty/invalid encoding yields a
|
|
47
|
+
// Buffer, which JSON.parse coerces via the default utf8 decoding, so "utf8"
|
|
48
|
+
// and "" are byte-identical here (equivalent mutant).
|
|
47
49
|
const data = await readFile(this.usersFile, "utf8");
|
|
50
|
+
// SAFETY: this file has one writer — `save()` below, which serializes a
|
|
51
|
+
// `UsersFile`. A hand-edited or truncated file throws in `JSON.parse`
|
|
52
|
+
// and is caught as "no users", so a bad shape never reaches a caller.
|
|
48
53
|
return JSON.parse(data);
|
|
49
54
|
}
|
|
50
55
|
catch {
|
|
@@ -52,8 +57,18 @@ export class FileUserManager {
|
|
|
52
57
|
}
|
|
53
58
|
}
|
|
54
59
|
async save(data) {
|
|
55
|
-
|
|
56
|
-
|
|
60
|
+
// The file holds usernames, roles, and token hashes — keep it and its
|
|
61
|
+
// directory owner-only. POSIX modes are ignored on Windows, which is fine.
|
|
62
|
+
await mkdir(dirname(this.usersFile), { recursive: true, mode: 0o700 });
|
|
63
|
+
// Stryker disable next-line StringLiteral: writeFile encodes the string as utf8
|
|
64
|
+
// regardless of an empty encoding argument, so "utf8" vs "" are equivalent.
|
|
65
|
+
await writeFile(this.usersFile, JSON.stringify(data, null, 2), {
|
|
66
|
+
encoding: "utf8",
|
|
67
|
+
mode: 0o600
|
|
68
|
+
});
|
|
69
|
+
// writeFile's mode only applies when the file is created; chmod an existing
|
|
70
|
+
// file down to owner-only too.
|
|
71
|
+
await chmod(this.usersFile, 0o600);
|
|
57
72
|
}
|
|
58
73
|
generateToken() {
|
|
59
74
|
return randomBytes(32).toString("base64url");
|
|
@@ -67,7 +82,7 @@ export class FileUserManager {
|
|
|
67
82
|
async addUser(username, role = "user") {
|
|
68
83
|
return this.withLock(async () => {
|
|
69
84
|
const data = await this.load();
|
|
70
|
-
if (data.users
|
|
85
|
+
if (Object.hasOwn(data.users, username)) {
|
|
71
86
|
throw new Error(`User '${username}' already exists`);
|
|
72
87
|
}
|
|
73
88
|
const token = this.generateToken();
|
|
@@ -87,7 +102,7 @@ export class FileUserManager {
|
|
|
87
102
|
async removeUser(username) {
|
|
88
103
|
return this.withLock(async () => {
|
|
89
104
|
const data = await this.load();
|
|
90
|
-
if (!data.users
|
|
105
|
+
if (!Object.hasOwn(data.users, username)) {
|
|
91
106
|
throw new Error(`User '${username}' not found`);
|
|
92
107
|
}
|
|
93
108
|
delete data.users[username];
|
|
@@ -97,7 +112,9 @@ export class FileUserManager {
|
|
|
97
112
|
async resetToken(username) {
|
|
98
113
|
return this.withLock(async () => {
|
|
99
114
|
const data = await this.load();
|
|
100
|
-
const existing = data.users
|
|
115
|
+
const existing = Object.hasOwn(data.users, username)
|
|
116
|
+
? data.users[username]
|
|
117
|
+
: undefined;
|
|
101
118
|
if (!existing) {
|
|
102
119
|
throw new Error(`User '${username}' not found`);
|
|
103
120
|
}
|
|
@@ -124,7 +141,10 @@ export class FileUserManager {
|
|
|
124
141
|
}
|
|
125
142
|
async getUser(username) {
|
|
126
143
|
const data = await this.load();
|
|
127
|
-
|
|
144
|
+
// Own-property check: a bare index of `__proto__`/`constructor`/`toString`
|
|
145
|
+
// returns an inherited Object.prototype value (truthy), which bypasses the
|
|
146
|
+
// not-found guard and later crashes on the non-UserRecord shape.
|
|
147
|
+
return Object.hasOwn(data.users, username) ? data.users[username] : null;
|
|
128
148
|
}
|
|
129
149
|
}
|
|
130
150
|
//# sourceMappingURL=file-user-manager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-user-manager.js","sourceRoot":"","sources":["../src/file-user-manager.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"file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AAQ,CAAC,EAAE;gBACnB,IAAI,EAAE,QAAQ,CAAC,IAAI;gBACnB,KAAK;gBACL,SAAS;aACV,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,SAAS;QACb,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,QAAgB;QAC5B,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC;QAC/B,2EAA2E;QAC3E,2EAA2E;QAC3E,iEAAiE;QACjE,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAC3E,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
export { TokenType, AuthProvider, type AuthResult } from "./auth-provider.js";
|
|
2
2
|
export { LocalAuthProvider } from "./providers/local-provider.js";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
3
|
+
export { DelegatedTokenProvider, mintDelegatedToken, isDelegatedToken, DELEGATED_TOKEN_PREFIX, type DelegatedTokenProviderOptions, type DelegatedSigningKey, type MintedDelegatedToken } from "./providers/delegated-token-provider.js";
|
|
4
|
+
export { AppSessionTokenProvider, mintAppSessionToken, isAppSessionToken, APP_SESSION_TOKEN_PREFIX, type AppSessionTokenProviderOptions, type AppSessionScope, type AppSessionSigningKey, type MintedAppSessionToken } from "./providers/app-session-token-provider.js";
|
|
5
5
|
export { SupabaseAuthProvider, type SupabaseAuthProviderOptions } from "./providers/supabase-provider.js";
|
|
6
|
-
export { createAuthMiddleware, getUserId, HttpError, type AuthenticatedUser, type AuthMiddlewareOptions } from "./middleware.js";
|
|
7
|
-
export { extractBearerToken, authenticateRequest, requireAuth, type HttpAuthOptions } from "./http-auth.js";
|
|
8
6
|
export interface User {
|
|
9
7
|
id: string;
|
|
10
8
|
role?: string;
|
|
11
9
|
}
|
|
12
10
|
export declare function isAdmin(user: User): boolean;
|
|
13
|
-
export { UserManager, type ManagedUser, type CreateUserOptions } from "./user-manager.js";
|
|
14
11
|
export { FileUserManager, type UserRecord, type UsersFile, type CreateUserResult } from "./file-user-manager.js";
|
|
15
12
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAE9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EACL,sBAAsB,EACtB,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,KAAK,6BAA6B,EAClC,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EAC1B,MAAM,yCAAyC,CAAC;AACjD,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,iBAAiB,EACjB,wBAAwB,EACxB,KAAK,8BAA8B,EACnC,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,qBAAqB,EAC3B,MAAM,2CAA2C,CAAC;AACnD,OAAO,EACL,oBAAoB,EACpB,KAAK,2BAA2B,EACjC,MAAM,kCAAkC,CAAC;AAE1C,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE3C;AAED,OAAO,EACL,eAAe,EACf,KAAK,UAAU,EACf,KAAK,SAAS,EACd,KAAK,gBAAgB,EACtB,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
export { TokenType, AuthProvider } from "./auth-provider.js";
|
|
2
2
|
export { LocalAuthProvider } from "./providers/local-provider.js";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
3
|
+
export { DelegatedTokenProvider, mintDelegatedToken, isDelegatedToken, DELEGATED_TOKEN_PREFIX } from "./providers/delegated-token-provider.js";
|
|
4
|
+
export { AppSessionTokenProvider, mintAppSessionToken, isAppSessionToken, APP_SESSION_TOKEN_PREFIX } from "./providers/app-session-token-provider.js";
|
|
5
5
|
export { SupabaseAuthProvider } from "./providers/supabase-provider.js";
|
|
6
|
-
export { createAuthMiddleware, getUserId, HttpError } from "./middleware.js";
|
|
7
|
-
export { extractBearerToken, authenticateRequest, requireAuth } from "./http-auth.js";
|
|
8
6
|
export function isAdmin(user) {
|
|
9
7
|
return user.role === "admin";
|
|
10
8
|
}
|
|
11
|
-
export { UserManager } from "./user-manager.js";
|
|
12
9
|
export { FileUserManager } from "./file-user-manager.js";
|
|
13
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAmB,MAAM,oBAAoB,CAAC;AAE9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAmB,MAAM,oBAAoB,CAAC;AAE9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EACL,sBAAsB,EACtB,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EAIvB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,iBAAiB,EACjB,wBAAwB,EAKzB,MAAM,2CAA2C,CAAC;AACnD,OAAO,EACL,oBAAoB,EAErB,MAAM,kCAAkC,CAAC;AAO1C,MAAM,UAAU,OAAO,CAAC,IAAU;IAChC,OAAO,IAAI,CAAC,IAAI,KAAK,OAAO,CAAC;AAC/B,CAAC;AAED,OAAO,EACL,eAAe,EAIhB,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape of decoded JSON, before any field of it is trusted.
|
|
3
|
+
*
|
|
4
|
+
* Auth providers read bodies that arrive from an identity service over the
|
|
5
|
+
* network. Typing those bodies as `unknown` (or as an open `Record<string,
|
|
6
|
+
* unknown>`) pushes the question of what is in them to every read site; naming
|
|
7
|
+
* the wire shape once answers it here, and the field-level readers below are
|
|
8
|
+
* the only place a wire value turns into a domain value.
|
|
9
|
+
*/
|
|
10
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
11
|
+
[key: string]: JsonValue;
|
|
12
|
+
};
|
|
13
|
+
/** A decoded JSON object — the only wire shape with readable fields. */
|
|
14
|
+
export type JsonObject = {
|
|
15
|
+
[key: string]: JsonValue;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Whether a decoded body is a JSON object. Generic in the caller's type so it
|
|
19
|
+
* narrows whatever the caller holds, rather than taking bare `unknown`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function isJsonObject<T>(value: T): value is T & JsonObject;
|
|
22
|
+
/**
|
|
23
|
+
* Whether a wire value arrived as text with something in it. Generic in the
|
|
24
|
+
* caller's type so it narrows a JWT claim or an `unknown` just as well as a
|
|
25
|
+
* `JsonValue`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function isNonEmptyString<T>(value: T): value is T & string;
|
|
28
|
+
/** Whether a wire value arrived as a real number. */
|
|
29
|
+
export declare function isFiniteNumber<T>(value: T): value is T & number;
|
|
30
|
+
/**
|
|
31
|
+
* A wire field read as an identifier: text, or a number rendered as text.
|
|
32
|
+
* Anything else — `null`, an object, a missing field — reads as `undefined`
|
|
33
|
+
* rather than as the string `"null"`.
|
|
34
|
+
*/
|
|
35
|
+
export declare function readIdentifier(body: JsonObject, field: string): string | undefined;
|
|
36
|
+
/** A wire field read as a non-empty string, or `undefined` if it is anything else. */
|
|
37
|
+
export declare function readString(body: JsonObject, field: string): string | undefined;
|
|
38
|
+
//# sourceMappingURL=json-wire.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-wire.d.ts","sourceRoot":"","sources":["../src/json-wire.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GACjB,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,SAAS,EAAE,GACX;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEjC,wEAAwE;AACxE,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEtD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,UAAU,CAEjE;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,MAAM,CAEjE;AAED,qDAAqD;AACrD,wBAAgB,cAAc,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,MAAM,CAE/D;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,MAAM,GACZ,MAAM,GAAG,SAAS,CAIpB;AAED,sFAAsF;AACtF,wBAAgB,UAAU,CACxB,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,MAAM,GACZ,MAAM,GAAG,SAAS,CAGpB"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a decoded body is a JSON object. Generic in the caller's type so it
|
|
3
|
+
* narrows whatever the caller holds, rather than taking bare `unknown`.
|
|
4
|
+
*/
|
|
5
|
+
export function isJsonObject(value) {
|
|
6
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Whether a wire value arrived as text with something in it. Generic in the
|
|
10
|
+
* caller's type so it narrows a JWT claim or an `unknown` just as well as a
|
|
11
|
+
* `JsonValue`.
|
|
12
|
+
*/
|
|
13
|
+
export function isNonEmptyString(value) {
|
|
14
|
+
return typeof value === "string" && value !== "";
|
|
15
|
+
}
|
|
16
|
+
/** Whether a wire value arrived as a real number. */
|
|
17
|
+
export function isFiniteNumber(value) {
|
|
18
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A wire field read as an identifier: text, or a number rendered as text.
|
|
22
|
+
* Anything else — `null`, an object, a missing field — reads as `undefined`
|
|
23
|
+
* rather than as the string `"null"`.
|
|
24
|
+
*/
|
|
25
|
+
export function readIdentifier(body, field) {
|
|
26
|
+
const value = body[field];
|
|
27
|
+
if (isNonEmptyString(value))
|
|
28
|
+
return value;
|
|
29
|
+
return isFiniteNumber(value) ? String(value) : undefined;
|
|
30
|
+
}
|
|
31
|
+
/** A wire field read as a non-empty string, or `undefined` if it is anything else. */
|
|
32
|
+
export function readString(body, field) {
|
|
33
|
+
const value = body[field];
|
|
34
|
+
return isNonEmptyString(value) ? value : undefined;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=json-wire.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-wire.js","sourceRoot":"","sources":["../src/json-wire.ts"],"names":[],"mappings":"AAoBA;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAI,KAAQ;IACtC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAI,KAAQ;IAC1C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,EAAE,CAAC;AACnD,CAAC;AAED,qDAAqD;AACrD,MAAM,UAAU,cAAc,CAAI,KAAQ;IACxC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAC7D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC5B,IAAgB,EAChB,KAAa;IAEb,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1B,IAAI,gBAAgB,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC1C,OAAO,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3D,CAAC;AAED,sFAAsF;AACtF,MAAM,UAAU,UAAU,CACxB,IAAgB,EAChB,KAAa;IAEb,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1B,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* App session tokens: short-lived, HMAC-signed bearer tokens that let an
|
|
3
|
+
* anonymous visitor run one deployed mini app, and nothing else.
|
|
4
|
+
*
|
|
5
|
+
* A deployed app is served from a hidden URL with no login (see
|
|
6
|
+
* `isAppDeploymentEnabled` in `@nodetool-ai/protocol`). Its runs execute on the
|
|
7
|
+
* owner's account — their pinned graphs, their provider keys, their spend
|
|
8
|
+
* budget — so the visitor needs an identity, and it cannot be the owner's own.
|
|
9
|
+
* This token carries the owner's user id *and* the application it is confined
|
|
10
|
+
* to, and every consumer is required to read the second field: the token
|
|
11
|
+
* answers "who pays" and "for what" together, so a caller cannot accidentally
|
|
12
|
+
* treat it as an ordinary session.
|
|
13
|
+
*
|
|
14
|
+
* That confinement is the whole point, so it is structural rather than a
|
|
15
|
+
* claim to check later. `TokenType.APP_SESSION` is a distinct type with no
|
|
16
|
+
* overlap with `USER`, and `AuthResult.applicationId` is only ever set by this
|
|
17
|
+
* provider — a host that forgets to narrow on the type still cannot mistake
|
|
18
|
+
* one for a full session, because the field it must consult is present.
|
|
19
|
+
*
|
|
20
|
+
* Like delegated tokens, the signature is the whole state: no token table, no
|
|
21
|
+
* cleanup job. Rotating the signing key invalidates every outstanding token;
|
|
22
|
+
* revoking the deployment stops new ones from minting, and the ones already
|
|
23
|
+
* out die at their expiry.
|
|
24
|
+
*/
|
|
25
|
+
import { AuthProvider, AuthResult } from "../auth-provider.js";
|
|
26
|
+
/**
|
|
27
|
+
* What marks a token as this provider's. A token without it is somebody
|
|
28
|
+
* else's — `verifyToken` says so without spending a comparison, and a chained
|
|
29
|
+
* caller falls through to the next provider.
|
|
30
|
+
*/
|
|
31
|
+
export declare const APP_SESSION_TOKEN_PREFIX = "nda_";
|
|
32
|
+
/** Who a session token pays as, and what it may run. */
|
|
33
|
+
export interface AppSessionScope {
|
|
34
|
+
/** The app owner: whose account the run executes on. */
|
|
35
|
+
userId: string;
|
|
36
|
+
applicationId: string;
|
|
37
|
+
/** The released version the visitor loaded. */
|
|
38
|
+
version: number;
|
|
39
|
+
}
|
|
40
|
+
export interface MintedAppSessionToken {
|
|
41
|
+
token: string;
|
|
42
|
+
/** Expiry as an ISO-8601 string, the shape the public route returns. */
|
|
43
|
+
expiresAt: string;
|
|
44
|
+
}
|
|
45
|
+
/** Whether a token claims to be an app session token at all. */
|
|
46
|
+
export declare function isAppSessionToken(token: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Mint a session token for one deployed app.
|
|
49
|
+
*
|
|
50
|
+
* @param key - HMAC signing key. The same key must reach the provider that
|
|
51
|
+
* verifies the token.
|
|
52
|
+
* @param scope - The app owner, the application, and the release the visitor
|
|
53
|
+
* loaded.
|
|
54
|
+
* @param ttlSeconds - Lifetime from now. A zero or negative value mints an
|
|
55
|
+
* already-expired token, which is how the expiry path is tested.
|
|
56
|
+
*/
|
|
57
|
+
export declare function mintAppSessionToken(key: Buffer | string, scope: AppSessionScope, ttlSeconds: number, now?: () => number): MintedAppSessionToken;
|
|
58
|
+
/**
|
|
59
|
+
* The signing key, or a function returning it. The accessor form lets a host
|
|
60
|
+
* defer key derivation until the first token actually arrives.
|
|
61
|
+
*/
|
|
62
|
+
export type AppSessionSigningKey = Buffer | string | (() => Buffer | string);
|
|
63
|
+
export interface AppSessionTokenProviderOptions {
|
|
64
|
+
/** Injected clock, so expiry is testable without waiting an hour. */
|
|
65
|
+
now?: () => number;
|
|
66
|
+
}
|
|
67
|
+
export declare class AppSessionTokenProvider extends AuthProvider {
|
|
68
|
+
private readonly key;
|
|
69
|
+
private readonly now;
|
|
70
|
+
constructor(key: AppSessionSigningKey, options?: AppSessionTokenProviderOptions);
|
|
71
|
+
private signingKey;
|
|
72
|
+
/**
|
|
73
|
+
* Verify an app session token. Every failure — wrong prefix, malformed,
|
|
74
|
+
* tampered, expired — is a not-ok result rather than a throw, so a chained
|
|
75
|
+
* caller can fall through to the next provider on any of them.
|
|
76
|
+
*/
|
|
77
|
+
verifyToken(token: string): Promise<AuthResult>;
|
|
78
|
+
clearCaches(): void;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=app-session-token-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-session-token-provider.d.ts","sourceRoot":"","sources":["../../src/providers/app-session-token-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAIH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAa,MAAM,qBAAqB,CAAC;AAO1E;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,SAAS,CAAC;AAiB/C,wDAAwD;AACxD,MAAM,WAAW,eAAe;IAC9B,wDAAwD;IACxD,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,EAAE,MAAM,CAAC;IACtB,+CAA+C;IAC/C,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,wEAAwE;IACxE,SAAS,EAAE,MAAM,CAAC;CACnB;AA+BD,gEAAgE;AAChE,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAExD;AAED;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,MAAM,GAAG,MAAM,EACpB,KAAK,EAAE,eAAe,EACtB,UAAU,EAAE,MAAM,EAClB,GAAG,GAAE,MAAM,MAAiB,GAC3B,qBAAqB,CAwBvB;AAqBD;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC;AAQ7E,MAAM,WAAW,8BAA8B;IAC7C,qEAAqE;IACrE,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,qBAAa,uBAAwB,SAAQ,YAAY;IACvD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAuB;IAC3C,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IAEnC,YACE,GAAG,EAAE,oBAAoB,EACzB,OAAO,GAAE,8BAAmC,EA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