@superforge/infra 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +162 -2
- package/output/agents.d.ts +96 -0
- package/output/agents.js +171 -0
- package/output/answers.d.ts +154 -0
- package/output/answers.js +153 -0
- package/output/choose.d.ts +13 -0
- package/output/choose.js +223 -0
- package/output/client.d.ts +24 -0
- package/output/client.js +246 -0
- package/output/comments.d.ts +37 -0
- package/output/comments.js +58 -0
- package/output/index.d.ts +9 -0
- package/output/index.js +17 -0
- package/output/infra.d.ts +99 -0
- package/output/infra.js +1 -0
- package/output/primitives.d.ts +13 -0
- package/output/primitives.js +20 -0
- package/output/results.d.ts +14 -0
- package/output/results.js +6 -0
- package/output/stand-in.d.ts +43 -0
- package/output/stand-in.js +237 -0
- package/output/version.d.ts +6 -0
- package/output/version.js +12 -0
- package/package.json +47 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Superforge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,163 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @superforge/infra
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Superforge Infra, the backend services every Foundation and Skylevel app
|
|
4
|
+
runs on, as an app's server calls it: login for its people, its comments
|
|
5
|
+
from the toolbar, Claude's login to its MCP, and which project its key is
|
|
6
|
+
for. It's the one way an app reaches Infra: a typed client, the stand-in
|
|
7
|
+
an app's tests and a fresh dev server use in its place, and the choice
|
|
8
|
+
between them. Infra's own API, each call with its fields and errors, is
|
|
9
|
+
its README in superforge's `source/infra/`, which the Superforge MCP's
|
|
10
|
+
`read_infra_docs` hands an agent.
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add @superforge/infra zod
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
It needs Node 22 or later, and zod 4, which an app has already.
|
|
17
|
+
|
|
18
|
+
## Using it
|
|
19
|
+
|
|
20
|
+
An app's server asks for Infra once, and every route and step calls it
|
|
21
|
+
there, never with a bare `fetch` (in the templates, `appInfra()` in
|
|
22
|
+
`source/backend/infra/index.ts`):
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { chooseInfra } from "@superforge/infra"
|
|
26
|
+
|
|
27
|
+
const { infra } = chooseInfra({
|
|
28
|
+
development: import.meta.env.DEV,
|
|
29
|
+
environment: process.env,
|
|
30
|
+
checkout: process.cwd(),
|
|
31
|
+
// The website's hosts, which the stand-in's project has
|
|
32
|
+
domains: ["vmdsystem.com"]
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const started = await infra.startLogin({
|
|
36
|
+
email: "ada@example.com",
|
|
37
|
+
ip: visitor_ip,
|
|
38
|
+
origin: "https://vmdsystem.com"
|
|
39
|
+
})
|
|
40
|
+
if (!started.ok) {
|
|
41
|
+
console.warn("Login didn't start", started.error)
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Every call answers a result, `{ ok: true, data }` or `{ ok: false, error }`
|
|
46
|
+
(`OperationResult` and `AsyncOperationResult`, which the package exports,
|
|
47
|
+
the same shape as the templates' global ones), and throws only for a bug.
|
|
48
|
+
|
|
49
|
+
### Which Infra
|
|
50
|
+
|
|
51
|
+
| Runs as | Has | Calls |
|
|
52
|
+
| ---------- | ------------------------------ | ----------------------------- |
|
|
53
|
+
| a build | `INFRA_SECRET_KEY`, sf_secret_ | Infra, which sends the emails |
|
|
54
|
+
| a build | anything else | nothing: login is down |
|
|
55
|
+
| dev server | `INFRA_TEST_KEY`, sf_test_ | Infra, in test mode |
|
|
56
|
+
| dev server | none | the stand-in |
|
|
57
|
+
|
|
58
|
+
A build never falls back to the stand-in, which shows the code on the page,
|
|
59
|
+
nor takes a test key, which sends no email; a dev server never takes a
|
|
60
|
+
secret key, so it can't email real people. A dev server reads its test key
|
|
61
|
+
from the checkout's `.env` too, and a git worktree with no `.env` of its
|
|
62
|
+
own reads its main checkout's, so the key is set once per computer.
|
|
63
|
+
`INFRA_URL` points the server at another Infra, like a superforge
|
|
64
|
+
checkout's own dev server (`http://infra.localhost:3002/v1`).
|
|
65
|
+
|
|
66
|
+
### The stand-in, for tests
|
|
67
|
+
|
|
68
|
+
`standInInfra()` is Infra's test mode in memory: login sends nothing and
|
|
69
|
+
answers the code and the link, which verify as Infra's do (once each, for
|
|
70
|
+
15 minutes, a code for five tries); a comment is checked as Infra checks
|
|
71
|
+
it and kept nowhere; Claude logs in to nothing, and `checkAgentToken` takes
|
|
72
|
+
only the tokens `standInAgentToken()` mints. An app's tests use it, so they
|
|
73
|
+
never reach Infra.
|
|
74
|
+
|
|
75
|
+
## When something goes wrong
|
|
76
|
+
|
|
77
|
+
Every refusal carries `message`: what went wrong and what fixes it, in
|
|
78
|
+
plain words, for whoever runs the app and their agent. It's Infra's own
|
|
79
|
+
when Infra said why (its README, "Errors"), and the package's when it
|
|
80
|
+
couldn't ask, so an app can show its team why login isn't working instead
|
|
81
|
+
of a shrug:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
{
|
|
85
|
+
reason: "unavailable",
|
|
86
|
+
message: "No one can log in: INFRA_SECRET_KEY isn't set on the app's service in Railway. Make a secret key on superforge.studio, in the project's Settings, and paste it into INFRA_SECRET_KEY on the app's service in Railway."
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- **A refusal the app handles** (`too-soon`, `wrong-code`, `no-link`…) has
|
|
91
|
+
its `reason`, its fields and Infra's `message`.
|
|
92
|
+
- **`unavailable`** is everything else: no key in a build, a key Infra
|
|
93
|
+
refused, an Infra that didn't answer or answered something that isn't
|
|
94
|
+
JSON, a success this version can't read, or an error it doesn't know,
|
|
95
|
+
like a call Infra retired. Its `message` says which, so an app on an
|
|
96
|
+
older version of this package still says what to do when Infra changes
|
|
97
|
+
under it.
|
|
98
|
+
- **Every answer is logged** with its call and status, and every refusal
|
|
99
|
+
with its `error` and `message` (`Infra refused a call`), so an app's
|
|
100
|
+
logs say why without anyone reproducing it.
|
|
101
|
+
|
|
102
|
+
A message is for a person: it can be reworded any time, so code branches on
|
|
103
|
+
`reason`, never on `message`. Show it to the app's own team; a visitor
|
|
104
|
+
should only learn that login isn't working.
|
|
105
|
+
|
|
106
|
+
## What every call tells Infra
|
|
107
|
+
|
|
108
|
+
Every call, Claude's OAuth calls passed through included, carries
|
|
109
|
+
`Infra-Client: @superforge/infra/<version>`, so Infra knows which version
|
|
110
|
+
each app is on, and can say when one's out of date (`PACKAGE_VERSION` and
|
|
111
|
+
`CLIENT_HEADER` are exported). The version is written in
|
|
112
|
+
`source/version.ts`, which a test checks against `package.json`.
|
|
113
|
+
|
|
114
|
+
## Releases
|
|
115
|
+
|
|
116
|
+
Each release is a version on npm, `@superforge/infra@x.y.z`, published by
|
|
117
|
+
CI when a push to `main` changes the version in `package.json`
|
|
118
|
+
(`.github/workflows/ci.yml`): a patch for a fix, a minor for anything an
|
|
119
|
+
app can use that it couldn't before, a major for a change an app has to
|
|
120
|
+
make code changes for. Every PR is a release: it bumps the version, with
|
|
121
|
+
its line at the top of `CHANGELOG.md`. An app takes one by bumping its
|
|
122
|
+
dependency (Dependabot opens a PR a month), never by copying code.
|
|
123
|
+
|
|
124
|
+
CI signs in to npm with trusted publishing: npm trusts this repository's
|
|
125
|
+
`ci.yml` to publish the package (the package's settings on npmjs.com,
|
|
126
|
+
"Trusted publisher"), so no npm token is kept anywhere. npm only lets a
|
|
127
|
+
package that exists trust a workflow, so the package was made by hand
|
|
128
|
+
first: a staged 0.1.0 from a checkout signed in with `npm login`, which
|
|
129
|
+
was never approved, since 0.1.1 replaced it. 0.1.1 was the first CI
|
|
130
|
+
published.
|
|
131
|
+
|
|
132
|
+
Infra keeps answering the versions apps are on: a change to a call is
|
|
133
|
+
additive, and a call Infra retires answers 410 `retired` with a message
|
|
134
|
+
saying what to do, which every version shows its team.
|
|
135
|
+
|
|
136
|
+
## Working on it
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
pnpm install
|
|
140
|
+
pnpm check # biome, the typecheck, the tests and the build
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
To try a change in a template before it's released, point the template's
|
|
144
|
+
checkout at this folder (`pnpm add @superforge/infra@link:<path to this
|
|
145
|
+
folder>`), and back to the released version before its PR: a `link:` is
|
|
146
|
+
this computer's alone, and CI can't install it.
|
|
147
|
+
|
|
148
|
+
- `source/index.ts`: everything the package exports.
|
|
149
|
+
- `source/infra.ts`: `Infra`, what an app asks of it, and its results.
|
|
150
|
+
- `source/client.ts`: Infra over HTTP with a project's key, its answers
|
|
151
|
+
parsed and every one logged. No call is retried: a second start sends a
|
|
152
|
+
second email, and a comment sent again is kept twice.
|
|
153
|
+
- `source/answers.ts`: what Infra answers, as zod schemas, which the types
|
|
154
|
+
come from.
|
|
155
|
+
- `source/agents.ts`: Claude's login: where a request reached the app,
|
|
156
|
+
passing Claude's OAuth calls through, what Infra answers about requests
|
|
157
|
+
and connections, and checking a token Infra signed with its public keys.
|
|
158
|
+
- `source/comments.ts`: what Infra takes of a toolbar's comment.
|
|
159
|
+
- `source/stand-in.ts`: the stand-in, and `standInAgentToken`.
|
|
160
|
+
- `source/choose.ts`: which Infra a server calls, and why when it's
|
|
161
|
+
none.
|
|
162
|
+
- `source/results.ts` and `source/primitives.ts`: the result types, and
|
|
163
|
+
what the package would otherwise take from an app's utilities.
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { type JWTVerifyGetKey } from "jose";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { type AgentScope } from "./primitives.js";
|
|
4
|
+
import type { AsyncOperationResult } from "./results.js";
|
|
5
|
+
/**
|
|
6
|
+
* Where a request reached the app: `https://vmdsystem.com`, or plain http
|
|
7
|
+
* on this computer, as a dev server is. Behind Railway's proxy a request's
|
|
8
|
+
* own address says http though the browser's said https, so only the host
|
|
9
|
+
* counts. Every token Claude holds is bound to it, as its issuer, and with
|
|
10
|
+
* the MCP's path (`MCP_PATH`), its audience.
|
|
11
|
+
*/
|
|
12
|
+
export declare function requestOrigin(request_url: string): string;
|
|
13
|
+
/** The header Infra reads the app's origin from, on a call passed through */
|
|
14
|
+
export declare const APP_ORIGIN_HEADER = "App-Origin";
|
|
15
|
+
/**
|
|
16
|
+
* Passes one of Claude's OAuth calls through to Infra's `/v1/oauth/…`
|
|
17
|
+
* (`path`), with the app's key and origin, and answers Infra's answer as
|
|
18
|
+
* it is: its status, its body, and where it sends the browser. The request
|
|
19
|
+
* is Claude's or the browser's, unread.
|
|
20
|
+
*/
|
|
21
|
+
export declare function passThrough({ url, key, send, request, path, origin }: {
|
|
22
|
+
url: string;
|
|
23
|
+
key: string;
|
|
24
|
+
send: typeof fetch;
|
|
25
|
+
request: Request;
|
|
26
|
+
path: string;
|
|
27
|
+
origin: string;
|
|
28
|
+
}): Promise<Response>;
|
|
29
|
+
/**
|
|
30
|
+
* What a call passed through answers when Infra can't be asked, as RFC
|
|
31
|
+
* 6749 has it, so Claude says why login isn't working rather than looping:
|
|
32
|
+
* `message`, which Claude shows, as `error_description`
|
|
33
|
+
*/
|
|
34
|
+
export declare function unavailableOAuth(message: string): Response;
|
|
35
|
+
export declare const agent_request: z.ZodObject<{
|
|
36
|
+
request: z.ZodObject<{
|
|
37
|
+
id: z.ZodString;
|
|
38
|
+
client_name: z.ZodString;
|
|
39
|
+
scopes: z.ZodArray<z.ZodEnum<{
|
|
40
|
+
read: "read";
|
|
41
|
+
act: "act";
|
|
42
|
+
}>>;
|
|
43
|
+
expires_at: z.ZodCoercedDate<unknown>;
|
|
44
|
+
}, z.core.$strip>;
|
|
45
|
+
}, z.core.$strip>;
|
|
46
|
+
export declare const agent_redirect: z.ZodObject<{
|
|
47
|
+
redirect_to: z.ZodURL;
|
|
48
|
+
}, z.core.$strip>;
|
|
49
|
+
export declare const agent_connections: z.ZodObject<{
|
|
50
|
+
connections: z.ZodArray<z.ZodObject<{
|
|
51
|
+
id: z.ZodString;
|
|
52
|
+
email: z.ZodString;
|
|
53
|
+
client_name: z.ZodString;
|
|
54
|
+
scopes: z.ZodArray<z.ZodEnum<{
|
|
55
|
+
read: "read";
|
|
56
|
+
act: "act";
|
|
57
|
+
}>>;
|
|
58
|
+
last_used_at: z.ZodCoercedDate<unknown>;
|
|
59
|
+
created_at: z.ZodCoercedDate<unknown>;
|
|
60
|
+
}, z.core.$strip>>;
|
|
61
|
+
}, z.core.$strip>;
|
|
62
|
+
export declare const agent_ended: z.ZodObject<{
|
|
63
|
+
ended: z.ZodNumber;
|
|
64
|
+
}, z.core.$strip>;
|
|
65
|
+
/** Why Infra refused a call about a request or a connection, in its words */
|
|
66
|
+
export declare const agent_refused: z.ZodObject<{
|
|
67
|
+
error: z.ZodEnum<{
|
|
68
|
+
"not-found": "not-found";
|
|
69
|
+
"not-asked": "not-asked";
|
|
70
|
+
}>;
|
|
71
|
+
message: z.ZodCatch<z.ZodString>;
|
|
72
|
+
}, z.core.$strip>;
|
|
73
|
+
/** Someone's connection, as the Connected list shows it */
|
|
74
|
+
export type AgentConnection = z.output<typeof agent_connections>["connections"][number];
|
|
75
|
+
/** What the app learns from a token Claude brings, once it's checked */
|
|
76
|
+
export type AgentToken = {
|
|
77
|
+
/** Whom Claude acts as: the app checks them against its team */
|
|
78
|
+
email: string;
|
|
79
|
+
/** What they let Claude do */
|
|
80
|
+
scopes: AgentScope[];
|
|
81
|
+
/** Which of their connections it is, to end it */
|
|
82
|
+
connection_id: string;
|
|
83
|
+
};
|
|
84
|
+
/** Where a token's signature is checked: Infra's published public keys */
|
|
85
|
+
export declare function infraKeys(url: string): JWTVerifyGetKey;
|
|
86
|
+
/**
|
|
87
|
+
* Checks a token Claude brings, as Infra's README says to: Infra signed it,
|
|
88
|
+
* for this app's origin and its MCP (`MCP_PATH`), for this project in the key's
|
|
89
|
+
* mode, and it hasn't run out. `project` is the app's own project ID and
|
|
90
|
+
* mode, as `GET /v1/project` answers them.
|
|
91
|
+
*/
|
|
92
|
+
export declare function checkToken(keys: JWTVerifyGetKey, token: string, expected: {
|
|
93
|
+
origin: string;
|
|
94
|
+
project: string;
|
|
95
|
+
mode: "live" | "test";
|
|
96
|
+
}): AsyncOperationResult<AgentToken, "invalid-token">;
|
package/output/agents.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { AGENT_SCOPES, MCP_PATH, SECOND } from "./primitives.js";
|
|
4
|
+
import { CLIENT, CLIENT_HEADER } from "./version.js";
|
|
5
|
+
// Claude logging in to an app's own MCP, through Infra: Infra is the OAuth
|
|
6
|
+
// server, and the app only checks the tokens it signs, with Infra's public
|
|
7
|
+
// keys, never asking Infra (Infra's README, "Claude"). This is what of it
|
|
8
|
+
// the client uses: where the app was reached, which every token is bound
|
|
9
|
+
// to, passing Claude's OAuth calls through, what Infra answers about its
|
|
10
|
+
// requests and connections, and checking a token.
|
|
11
|
+
/**
|
|
12
|
+
* Where a request reached the app: `https://vmdsystem.com`, or plain http
|
|
13
|
+
* on this computer, as a dev server is. Behind Railway's proxy a request's
|
|
14
|
+
* own address says http though the browser's said https, so only the host
|
|
15
|
+
* counts. Every token Claude holds is bound to it, as its issuer, and with
|
|
16
|
+
* the MCP's path (`MCP_PATH`), its audience.
|
|
17
|
+
*/
|
|
18
|
+
export function requestOrigin(request_url) {
|
|
19
|
+
const { host, hostname } = new URL(request_url);
|
|
20
|
+
const local = ["localhost", "127.0.0.1", "[::1]"].includes(hostname) ||
|
|
21
|
+
hostname.endsWith(".localhost");
|
|
22
|
+
return local ? `http://${host}` : `https://${host}`;
|
|
23
|
+
}
|
|
24
|
+
/** The header Infra reads the app's origin from, on a call passed through */
|
|
25
|
+
export const APP_ORIGIN_HEADER = "App-Origin";
|
|
26
|
+
// What a call passed through may carry on, of the request Claude or the
|
|
27
|
+
// browser sent: what the body is, and what Claude accepts back
|
|
28
|
+
const PASSED_HEADERS = ["accept", "content-type"];
|
|
29
|
+
/**
|
|
30
|
+
* Passes one of Claude's OAuth calls through to Infra's `/v1/oauth/…`
|
|
31
|
+
* (`path`), with the app's key and origin, and answers Infra's answer as
|
|
32
|
+
* it is: its status, its body, and where it sends the browser. The request
|
|
33
|
+
* is Claude's or the browser's, unread.
|
|
34
|
+
*/
|
|
35
|
+
export async function passThrough({ url, key, send, request, path, origin }) {
|
|
36
|
+
const { search } = new URL(request.url);
|
|
37
|
+
const headers = new Headers({
|
|
38
|
+
Authorization: `Bearer ${key}`,
|
|
39
|
+
[APP_ORIGIN_HEADER]: origin,
|
|
40
|
+
[CLIENT_HEADER]: CLIENT
|
|
41
|
+
});
|
|
42
|
+
for (const name of PASSED_HEADERS) {
|
|
43
|
+
const value = request.headers.get(name);
|
|
44
|
+
if (value !== null) {
|
|
45
|
+
headers.set(name, value);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const body = request.method === "GET" || request.method === "HEAD"
|
|
49
|
+
? undefined
|
|
50
|
+
: await request.text();
|
|
51
|
+
try {
|
|
52
|
+
const answer = await send(`${url}/oauth${path}${search}`, {
|
|
53
|
+
method: request.method,
|
|
54
|
+
headers,
|
|
55
|
+
body,
|
|
56
|
+
redirect: "manual",
|
|
57
|
+
signal: AbortSignal.timeout(15 * SECOND)
|
|
58
|
+
});
|
|
59
|
+
console.info("Infra answered", {
|
|
60
|
+
call: `/oauth${path}`,
|
|
61
|
+
status: answer.status
|
|
62
|
+
});
|
|
63
|
+
const passed = new Headers({ "Cache-Control": "no-store" });
|
|
64
|
+
for (const name of ["content-type", "location"]) {
|
|
65
|
+
const value = answer.headers.get(name);
|
|
66
|
+
if (value !== null) {
|
|
67
|
+
passed.set(name, value);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return new Response(await answer.arrayBuffer(), {
|
|
71
|
+
status: answer.status,
|
|
72
|
+
headers: passed
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.error("Infra didn't answer", { call: `/oauth${path}`, error });
|
|
77
|
+
return unavailableOAuth("Infra didn't answer this app's call, so Claude can't log in. It may be down; trying again in a few minutes may work.");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* What a call passed through answers when Infra can't be asked, as RFC
|
|
82
|
+
* 6749 has it, so Claude says why login isn't working rather than looping:
|
|
83
|
+
* `message`, which Claude shows, as `error_description`
|
|
84
|
+
*/
|
|
85
|
+
export function unavailableOAuth(message) {
|
|
86
|
+
return Response.json({
|
|
87
|
+
error: "temporarily_unavailable",
|
|
88
|
+
error_description: message,
|
|
89
|
+
message
|
|
90
|
+
}, { status: 503, headers: { "Cache-Control": "no-store" } });
|
|
91
|
+
}
|
|
92
|
+
// What Infra answers about Claude's requests and connections (its README,
|
|
93
|
+
// "Claude"), as the app reads it
|
|
94
|
+
export const agent_request = z.object({
|
|
95
|
+
request: z.object({
|
|
96
|
+
id: z.string(),
|
|
97
|
+
client_name: z.string(),
|
|
98
|
+
scopes: z.array(z.enum(AGENT_SCOPES)),
|
|
99
|
+
expires_at: z.coerce.date()
|
|
100
|
+
})
|
|
101
|
+
});
|
|
102
|
+
export const agent_redirect = z.object({ redirect_to: z.url() });
|
|
103
|
+
export const agent_connections = z.object({
|
|
104
|
+
connections: z.array(z.object({
|
|
105
|
+
id: z.string(),
|
|
106
|
+
email: z.string(),
|
|
107
|
+
client_name: z.string(),
|
|
108
|
+
scopes: z.array(z.enum(AGENT_SCOPES)),
|
|
109
|
+
last_used_at: z.coerce.date(),
|
|
110
|
+
created_at: z.coerce.date()
|
|
111
|
+
}))
|
|
112
|
+
});
|
|
113
|
+
export const agent_ended = z.object({ ended: z.number().int() });
|
|
114
|
+
/** Why Infra refused a call about a request or a connection, in its words */
|
|
115
|
+
export const agent_refused = z.object({
|
|
116
|
+
error: z.enum(["not-found", "not-asked"]),
|
|
117
|
+
message: z
|
|
118
|
+
.string()
|
|
119
|
+
.catch("Infra has no such request or connection for this project.")
|
|
120
|
+
});
|
|
121
|
+
// The claims Infra's tokens carry beyond the standard ones (Infra's README)
|
|
122
|
+
const agent_claims = z.object({
|
|
123
|
+
sub: z.string(),
|
|
124
|
+
scope: z.string(),
|
|
125
|
+
project: z.string(),
|
|
126
|
+
mode: z.enum(["live", "test"]),
|
|
127
|
+
connection: z.string()
|
|
128
|
+
});
|
|
129
|
+
/** Where a token's signature is checked: Infra's published public keys */
|
|
130
|
+
export function infraKeys(url) {
|
|
131
|
+
return createRemoteJWKSet(new URL("/.well-known/jwks.json", new URL(url).origin), { timeoutDuration: 10 * SECOND, cooldownDuration: 30 * SECOND });
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Checks a token Claude brings, as Infra's README says to: Infra signed it,
|
|
135
|
+
* for this app's origin and its MCP (`MCP_PATH`), for this project in the key's
|
|
136
|
+
* mode, and it hasn't run out. `project` is the app's own project ID and
|
|
137
|
+
* mode, as `GET /v1/project` answers them.
|
|
138
|
+
*/
|
|
139
|
+
export async function checkToken(keys, token, expected) {
|
|
140
|
+
try {
|
|
141
|
+
const { payload } = await jwtVerify(token, keys, {
|
|
142
|
+
issuer: expected.origin,
|
|
143
|
+
audience: `${expected.origin}${MCP_PATH}`,
|
|
144
|
+
algorithms: ["ES256"]
|
|
145
|
+
});
|
|
146
|
+
const claims = agent_claims.safeParse(payload);
|
|
147
|
+
if (!claims.success ||
|
|
148
|
+
claims.data.project !== expected.project ||
|
|
149
|
+
claims.data.mode !== expected.mode) {
|
|
150
|
+
console.warn("Agent token not for this app", {
|
|
151
|
+
origin: expected.origin
|
|
152
|
+
});
|
|
153
|
+
return { ok: false, error: "invalid-token" };
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
const said = claims.data.scope.split(" ");
|
|
157
|
+
return {
|
|
158
|
+
ok: true,
|
|
159
|
+
data: {
|
|
160
|
+
email: claims.data.sub,
|
|
161
|
+
scopes: AGENT_SCOPES.filter((scope) => said.includes(scope)),
|
|
162
|
+
connection_id: claims.data.connection
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
console.info("Agent token refused", { origin: expected.origin, error });
|
|
169
|
+
return { ok: false, error: "invalid-token" };
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Which project the key is for, its mode, and its domains, the only hosts
|
|
4
|
+
* besides localhost its login emails link to. An Infra from before it
|
|
5
|
+
* answered the domains answers none here, `null`, which an app shows as
|
|
6
|
+
* not known.
|
|
7
|
+
*/
|
|
8
|
+
export declare const infra_project: z.ZodObject<{
|
|
9
|
+
project: z.ZodObject<{
|
|
10
|
+
id: z.ZodString;
|
|
11
|
+
name: z.ZodString;
|
|
12
|
+
domains: z.ZodDefault<z.ZodNullable<z.ZodArray<z.ZodString>>>;
|
|
13
|
+
}, z.core.$strip>;
|
|
14
|
+
mode: z.ZodEnum<{
|
|
15
|
+
live: "live";
|
|
16
|
+
test: "test";
|
|
17
|
+
}>;
|
|
18
|
+
}, z.core.$strip>;
|
|
19
|
+
/** A login email that went out. */
|
|
20
|
+
export declare const started_login: z.ZodObject<{
|
|
21
|
+
email: z.ZodString;
|
|
22
|
+
with_link: z.ZodBoolean;
|
|
23
|
+
expires_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
24
|
+
test: z.ZodNullable<z.ZodObject<{
|
|
25
|
+
code: z.ZodString;
|
|
26
|
+
link: z.ZodNullable<z.ZodString>;
|
|
27
|
+
}, z.core.$strip>>;
|
|
28
|
+
}, z.core.$strip>;
|
|
29
|
+
/** Why Infra didn't send a login email. */
|
|
30
|
+
export declare const start_refused: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
|
|
31
|
+
error: z.ZodLiteral<"too-soon">;
|
|
32
|
+
retry_after: z.ZodNumber;
|
|
33
|
+
message: z.ZodCatch<z.ZodString>;
|
|
34
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
35
|
+
reason: "too-soon";
|
|
36
|
+
retry_after: number;
|
|
37
|
+
message: string;
|
|
38
|
+
}, {
|
|
39
|
+
error: "too-soon";
|
|
40
|
+
retry_after: number;
|
|
41
|
+
message: string;
|
|
42
|
+
}>>, z.ZodPipe<z.ZodObject<{
|
|
43
|
+
error: z.ZodLiteral<"too-many">;
|
|
44
|
+
limit: z.ZodString;
|
|
45
|
+
retry_after: z.ZodNumber;
|
|
46
|
+
message: z.ZodCatch<z.ZodString>;
|
|
47
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
48
|
+
reason: "too-many";
|
|
49
|
+
limit: string;
|
|
50
|
+
retry_after: number;
|
|
51
|
+
message: string;
|
|
52
|
+
}, {
|
|
53
|
+
error: "too-many";
|
|
54
|
+
limit: string;
|
|
55
|
+
retry_after: number;
|
|
56
|
+
message: string;
|
|
57
|
+
}>>, z.ZodPipe<z.ZodObject<{
|
|
58
|
+
error: z.ZodLiteral<"not-sent">;
|
|
59
|
+
message: z.ZodCatch<z.ZodString>;
|
|
60
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
61
|
+
reason: "not-sent";
|
|
62
|
+
message: string;
|
|
63
|
+
}, {
|
|
64
|
+
error: "not-sent";
|
|
65
|
+
message: string;
|
|
66
|
+
}>>]>;
|
|
67
|
+
/** Whose email the code or the link was. */
|
|
68
|
+
export declare const verified_login: z.ZodObject<{
|
|
69
|
+
email: z.ZodString;
|
|
70
|
+
}, z.core.$strip>;
|
|
71
|
+
/** Why what the visitor brought back didn't log them in. */
|
|
72
|
+
export declare const verify_refused: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
|
|
73
|
+
error: z.ZodLiteral<"wrong-code">;
|
|
74
|
+
attempts_left: z.ZodNumber;
|
|
75
|
+
message: z.ZodCatch<z.ZodString>;
|
|
76
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
77
|
+
reason: "wrong-code";
|
|
78
|
+
attempts_left: number;
|
|
79
|
+
message: string;
|
|
80
|
+
}, {
|
|
81
|
+
error: "wrong-code";
|
|
82
|
+
attempts_left: number;
|
|
83
|
+
message: string;
|
|
84
|
+
}>>, z.ZodPipe<z.ZodObject<{
|
|
85
|
+
error: z.ZodLiteral<"no-code">;
|
|
86
|
+
message: z.ZodCatch<z.ZodString>;
|
|
87
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
88
|
+
reason: "no-code";
|
|
89
|
+
message: string;
|
|
90
|
+
}, {
|
|
91
|
+
error: "no-code";
|
|
92
|
+
message: string;
|
|
93
|
+
}>>, z.ZodPipe<z.ZodObject<{
|
|
94
|
+
error: z.ZodLiteral<"no-link">;
|
|
95
|
+
message: z.ZodCatch<z.ZodString>;
|
|
96
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
97
|
+
reason: "no-link";
|
|
98
|
+
message: string;
|
|
99
|
+
}, {
|
|
100
|
+
error: "no-link";
|
|
101
|
+
message: string;
|
|
102
|
+
}>>]>;
|
|
103
|
+
/** A comment Infra kept: its ID, and when. */
|
|
104
|
+
export declare const kept_comment: z.ZodPipe<z.ZodObject<{
|
|
105
|
+
comment: z.ZodObject<{
|
|
106
|
+
id: z.ZodString;
|
|
107
|
+
created_at: z.ZodPipe<z.ZodISODateTime, z.ZodTransform<Date, string>>;
|
|
108
|
+
}, z.core.$strip>;
|
|
109
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
110
|
+
id: string;
|
|
111
|
+
created_at: Date;
|
|
112
|
+
}, {
|
|
113
|
+
comment: {
|
|
114
|
+
id: string;
|
|
115
|
+
created_at: Date;
|
|
116
|
+
};
|
|
117
|
+
}>>;
|
|
118
|
+
/**
|
|
119
|
+
* Why Infra didn't keep a comment. Either is a bug in what the app sent,
|
|
120
|
+
* so sending it again won't help.
|
|
121
|
+
*/
|
|
122
|
+
export declare const comment_refused: z.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
|
|
123
|
+
error: z.ZodLiteral<"invalid">;
|
|
124
|
+
fields: z.ZodArray<z.ZodString>;
|
|
125
|
+
message: z.ZodCatch<z.ZodString>;
|
|
126
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
127
|
+
reason: "invalid";
|
|
128
|
+
fields: string[];
|
|
129
|
+
message: string;
|
|
130
|
+
}, {
|
|
131
|
+
error: "invalid";
|
|
132
|
+
fields: string[];
|
|
133
|
+
message: string;
|
|
134
|
+
}>>, z.ZodPipe<z.ZodObject<{
|
|
135
|
+
error: z.ZodLiteral<"too-large">;
|
|
136
|
+
limit: z.ZodNumber;
|
|
137
|
+
message: z.ZodCatch<z.ZodString>;
|
|
138
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
139
|
+
reason: "too-large";
|
|
140
|
+
limit: number;
|
|
141
|
+
message: string;
|
|
142
|
+
}, {
|
|
143
|
+
error: "too-large";
|
|
144
|
+
limit: number;
|
|
145
|
+
message: string;
|
|
146
|
+
}>>]>;
|
|
147
|
+
/**
|
|
148
|
+
* Any refusal of Infra's, read only for its words: what the client logs,
|
|
149
|
+
* and what it tells an app about an error it doesn't know
|
|
150
|
+
*/
|
|
151
|
+
export declare const any_refusal: z.ZodObject<{
|
|
152
|
+
error: z.ZodString;
|
|
153
|
+
message: z.ZodOptional<z.ZodString>;
|
|
154
|
+
}, z.core.$strip>;
|