@superforge/infra 0.1.0 → 0.2.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/README.md +50 -33
- package/output/agents.d.ts +50 -69
- package/output/agents.js +120 -85
- package/output/choose.d.ts +3 -1
- package/output/choose.js +20 -13
- package/output/client.d.ts +10 -9
- package/output/client.js +85 -106
- package/output/contract/agents.d.ts +203 -0
- package/output/contract/agents.js +94 -0
- package/output/contract/comments.d.ts +101 -0
- package/output/contract/comments.js +66 -0
- package/output/contract/errors.d.ts +123 -0
- package/output/contract/errors.js +61 -0
- package/output/contract/index.d.ts +6 -0
- package/output/contract/index.js +11 -0
- package/output/contract/login.d.ts +152 -0
- package/output/contract/login.js +93 -0
- package/output/contract/people.d.ts +9 -0
- package/output/contract/people.js +16 -0
- package/output/contract/project.d.ts +38 -0
- package/output/contract/project.js +36 -0
- package/output/index.d.ts +4 -4
- package/output/index.js +10 -6
- package/output/infra.d.ts +44 -58
- package/output/primitives.d.ts +0 -8
- package/output/primitives.js +0 -7
- package/output/stand-in.d.ts +18 -16
- package/output/stand-in.js +74 -39
- package/output/version.d.ts +4 -0
- package/output/version.js +10 -0
- package/package.json +5 -1
- package/output/answers.d.ts +0 -154
- package/output/answers.js +0 -153
- package/output/comments.d.ts +0 -37
- package/output/comments.js +0 -58
package/README.md
CHANGED
|
@@ -33,9 +33,10 @@ const { infra } = chooseInfra({
|
|
|
33
33
|
})
|
|
34
34
|
|
|
35
35
|
const started = await infra.startLogin({
|
|
36
|
-
|
|
36
|
+
channel: "email",
|
|
37
|
+
address: "ada@example.com",
|
|
37
38
|
ip: visitor_ip,
|
|
38
|
-
|
|
39
|
+
link_to: "https://vmdsystem.com/login"
|
|
39
40
|
})
|
|
40
41
|
if (!started.ok) {
|
|
41
42
|
console.warn("Login didn't start", started.error)
|
|
@@ -60,8 +61,11 @@ nor takes a test key, which sends no email; a dev server never takes a
|
|
|
60
61
|
secret key, so it can't email real people. A dev server reads its test key
|
|
61
62
|
from the checkout's `.env` too, and a git worktree with no `.env` of its
|
|
62
63
|
own reads its main checkout's, so the key is set once per computer.
|
|
63
|
-
`INFRA_URL
|
|
64
|
-
checkout's own
|
|
64
|
+
Only a dev server reads `INFRA_URL`, which points it at another Infra,
|
|
65
|
+
like a superforge checkout's own (`http://infra.localhost:3002/v1`),
|
|
66
|
+
while Infra itself is worked on: a build always calls Infra's own
|
|
67
|
+
address, so no variable can send its secret key, or anyone's login,
|
|
68
|
+
anywhere else.
|
|
65
69
|
|
|
66
70
|
### The stand-in, for tests
|
|
67
71
|
|
|
@@ -74,11 +78,12 @@ never reach Infra.
|
|
|
74
78
|
|
|
75
79
|
## When something goes wrong
|
|
76
80
|
|
|
77
|
-
Every refusal
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
Every refusal is Infra's own `error`, as it answered it, in the shape a
|
|
82
|
+
template's API answers too: `reason`, for code to branch on, `message`,
|
|
83
|
+
what went wrong and what fixes it in plain words, and any fields the call
|
|
84
|
+
adds, like `retry_after`. Infra's contract has every reason
|
|
85
|
+
(`INFRA_ERRORS`). When the package couldn't ask Infra, it says why in
|
|
86
|
+
the same shape:
|
|
82
87
|
|
|
83
88
|
```ts
|
|
84
89
|
{
|
|
@@ -87,22 +92,27 @@ of a shrug:
|
|
|
87
92
|
}
|
|
88
93
|
```
|
|
89
94
|
|
|
90
|
-
- **A refusal the app handles** (`
|
|
91
|
-
|
|
95
|
+
- **A refusal the app handles** (`rate-limited`, `wrong-code`,
|
|
96
|
+
`no-link`…) is Infra's `error` as it is.
|
|
92
97
|
- **`unavailable`** is everything else: no key in a build, a key Infra
|
|
93
98
|
refused, an Infra that didn't answer or answered something that isn't
|
|
94
|
-
JSON,
|
|
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.
|
|
99
|
+
JSON, or an answer outside the contract. Its `message` says which.
|
|
98
100
|
- **Every answer is logged** with its call and status, and every refusal
|
|
99
|
-
with its `
|
|
101
|
+
with its `reason` and `message` (`Infra refused a call`), so an app's
|
|
100
102
|
logs say why without anyone reproducing it.
|
|
101
103
|
|
|
102
104
|
A message is for a person: it can be reworded any time, so code branches on
|
|
103
105
|
`reason`, never on `message`. Show it to the app's own team; a visitor
|
|
104
106
|
should only learn that login isn't working.
|
|
105
107
|
|
|
108
|
+
## What every call tells Infra
|
|
109
|
+
|
|
110
|
+
Every call, Claude's OAuth calls passed through included, carries
|
|
111
|
+
`Infra-Client: @superforge/infra/<version>`, so Infra knows which version
|
|
112
|
+
each app is on, and can say when one's out of date (`PACKAGE_VERSION` and
|
|
113
|
+
`CLIENT_HEADER` are exported). The version is written in
|
|
114
|
+
`source/version.ts`, which a test checks against `package.json`.
|
|
115
|
+
|
|
106
116
|
## Releases
|
|
107
117
|
|
|
108
118
|
Each release is a version on npm, `@superforge/infra@x.y.z`, published by
|
|
@@ -116,16 +126,17 @@ dependency (Dependabot opens a PR a month), never by copying code.
|
|
|
116
126
|
CI signs in to npm with trusted publishing: npm trusts this repository's
|
|
117
127
|
`ci.yml` to publish the package (the package's settings on npmjs.com,
|
|
118
128
|
"Trusted publisher"), so no npm token is kept anywhere. npm only lets a
|
|
119
|
-
package that exists trust a workflow, so
|
|
120
|
-
checkout signed in with `npm login
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
+
package that exists trust a workflow, so the package was made by hand
|
|
130
|
+
first: a staged 0.1.0 from a checkout signed in with `npm login`, which
|
|
131
|
+
was never approved, since 0.1.1 replaced it. 0.1.1 was the first CI
|
|
132
|
+
published.
|
|
133
|
+
|
|
134
|
+
From v1 on, Infra keeps answering every version apps are on, the way
|
|
135
|
+
Stripe does: one implementation, of the latest contract, and an adapter
|
|
136
|
+
for each breaking change, which turns an older client's request into
|
|
137
|
+
today's and today's answer back into the one it knows, by the version in
|
|
138
|
+
`Infra-Client`. Until v1, Infra keeps none: an app takes the latest
|
|
139
|
+
release.
|
|
129
140
|
|
|
130
141
|
## Working on it
|
|
131
142
|
|
|
@@ -140,16 +151,22 @@ folder>`), and back to the released version before its PR: a `link:` is
|
|
|
140
151
|
this computer's alone, and CI can't install it.
|
|
141
152
|
|
|
142
153
|
- `source/index.ts`: everything the package exports.
|
|
154
|
+
- `source/contract/`: Infra's contract, exported on its own at
|
|
155
|
+
`@superforge/infra/contract`, with nothing of Node's: every call's
|
|
156
|
+
request and answer as zod schemas, which Infra checks what it's sent
|
|
157
|
+
with, and every reason it refuses one (`errors.ts`), a file per area:
|
|
158
|
+
`login.ts`, `comments.ts`, `agents.ts`, `project.ts` and `people.ts`
|
|
159
|
+
(`user_id`, the app's ID for one of its people).
|
|
143
160
|
- `source/infra.ts`: `Infra`, what an app asks of it, and its results.
|
|
144
161
|
- `source/client.ts`: Infra over HTTP with a project's key, its answers
|
|
145
|
-
parsed and every one logged. No call is retried: a
|
|
146
|
-
second
|
|
147
|
-
|
|
148
|
-
come from.
|
|
162
|
+
parsed with the contract and every one logged. No call is retried: a
|
|
163
|
+
second start sends a second message, and a comment sent again is kept
|
|
164
|
+
twice.
|
|
149
165
|
- `source/agents.ts`: Claude's login: where a request reached the app,
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
166
|
+
the OAuth server's metadata, built here, Claude's other OAuth calls,
|
|
167
|
+
passed on to Infra, the authorize request sending the browser to the
|
|
168
|
+
app's Allow page, and checking a token Infra signed with its public
|
|
169
|
+
keys.
|
|
153
170
|
- `source/stand-in.ts`: the stand-in, and `standInAgentToken`.
|
|
154
171
|
- `source/choose.ts`: which Infra a server calls, and why when it's
|
|
155
172
|
none.
|
package/output/agents.d.ts
CHANGED
|
@@ -1,96 +1,77 @@
|
|
|
1
1
|
import { type JWTVerifyGetKey } from "jose";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { type AppPaths, type KeyMode, type OAuthCall } from "./contract/index.js";
|
|
3
|
+
import type { AgentToken } from "./infra.js";
|
|
4
4
|
import type { AsyncOperationResult } from "./results.js";
|
|
5
5
|
/**
|
|
6
6
|
* Where a request reached the app: `https://vmdsystem.com`, or plain http
|
|
7
7
|
* on this computer, as a dev server is. Behind Railway's proxy a request's
|
|
8
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
|
|
10
|
-
* the MCP's path (`MCP_PATH`), its audience.
|
|
9
|
+
* counts. Every token Claude holds is bound to it, as its issuer.
|
|
11
10
|
*/
|
|
12
11
|
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
12
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
13
|
+
* The OAuth server's metadata (RFC 8414), which Claude reads at the app's
|
|
14
|
+
* `/.well-known/oauth-authorization-server`: the app is the issuer, its
|
|
15
|
+
* own addresses the server's, and Infra's public keys the ones its tokens
|
|
16
|
+
* are signed with. It's the same for every app but for those, so the
|
|
17
|
+
* client builds it, with no call to Infra.
|
|
20
18
|
*/
|
|
21
|
-
export declare function
|
|
19
|
+
export declare function oauthMetadata({ origin, paths, infra_url }: {
|
|
20
|
+
origin: string;
|
|
21
|
+
paths: AppPaths;
|
|
22
|
+
infra_url: string;
|
|
23
|
+
}): {
|
|
24
|
+
issuer: string;
|
|
25
|
+
authorization_endpoint: string;
|
|
26
|
+
token_endpoint: string;
|
|
27
|
+
registration_endpoint: string;
|
|
28
|
+
revocation_endpoint: string;
|
|
29
|
+
jwks_uri: string;
|
|
30
|
+
response_types_supported: string[];
|
|
31
|
+
grant_types_supported: string[];
|
|
32
|
+
code_challenge_methods_supported: string[];
|
|
33
|
+
token_endpoint_auth_methods_supported: string[];
|
|
34
|
+
revocation_endpoint_auth_methods_supported: string[];
|
|
35
|
+
scopes_supported: ("read" | "act")[];
|
|
36
|
+
client_id_metadata_document_supported: boolean;
|
|
37
|
+
authorization_response_iss_parameter_supported: boolean;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Answers one of Claude's OAuth calls to the app (`call`), its request
|
|
41
|
+
* unread:
|
|
42
|
+
*
|
|
43
|
+
* - `metadata`: built here (`oauthMetadata`)
|
|
44
|
+
* - `authorize`: Infra saves the request, then the browser goes to the
|
|
45
|
+
* app's Allow page, `<paths.allow>/<request id>`, or back to Claude with
|
|
46
|
+
* an error. One whose return address isn't Claude's is answered 400 in
|
|
47
|
+
* plain words, never redirected, so the app is never an open redirect.
|
|
48
|
+
* - `token`, `register`, `revoke`: Infra's answer, as it is (RFC 6749's)
|
|
49
|
+
*/
|
|
50
|
+
export declare function passOAuth({ url, key, send, paths, request, call, origin }: {
|
|
22
51
|
url: string;
|
|
23
52
|
key: string;
|
|
24
53
|
send: typeof fetch;
|
|
54
|
+
paths: AppPaths;
|
|
25
55
|
request: Request;
|
|
26
|
-
|
|
56
|
+
call: OAuthCall | "metadata";
|
|
27
57
|
origin: string;
|
|
28
58
|
}): Promise<Response>;
|
|
29
59
|
/**
|
|
30
|
-
* What
|
|
31
|
-
* 6749 has it, so Claude says why login isn't working rather than
|
|
32
|
-
* `
|
|
60
|
+
* What one of Claude's OAuth calls answers when Infra can't be asked, as
|
|
61
|
+
* RFC 6749 has it, so Claude says why login isn't working rather than
|
|
62
|
+
* looping: why, as `error_description`, which Claude shows
|
|
33
63
|
*/
|
|
34
64
|
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
65
|
/** Where a token's signature is checked: Infra's published public keys */
|
|
85
66
|
export declare function infraKeys(url: string): JWTVerifyGetKey;
|
|
86
67
|
/**
|
|
87
|
-
* Checks a token Claude brings, as Infra's README says to: Infra signed
|
|
88
|
-
* for this app's origin
|
|
89
|
-
*
|
|
90
|
-
* mode, as `GET /v1/project` answers them.
|
|
68
|
+
* Checks a token Claude brings, as Infra's README says to: Infra signed
|
|
69
|
+
* it, for this app's `origin`, for `resource`, the address it was brought
|
|
70
|
+
* to, for this project in the key's mode, and it hasn't run out
|
|
91
71
|
*/
|
|
92
72
|
export declare function checkToken(keys: JWTVerifyGetKey, token: string, expected: {
|
|
93
73
|
origin: string;
|
|
74
|
+
resource: string;
|
|
94
75
|
project: string;
|
|
95
|
-
mode:
|
|
76
|
+
mode: KeyMode;
|
|
96
77
|
}): AsyncOperationResult<AgentToken, "invalid-token">;
|
package/output/agents.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { AGENT_SCOPES, APP_ORIGIN_HEADER, agent_authorized, agent_claims, authorize_refused, CLIENT_HEADER, infra_failure } from "./contract/index.js";
|
|
3
|
+
import { SECOND } from "./primitives.js";
|
|
4
|
+
import { CLIENT } from "./version.js";
|
|
4
5
|
// Claude logging in to an app's own MCP, through Infra: Infra is the OAuth
|
|
5
6
|
// server, and the app only checks the tokens it signs, with Infra's public
|
|
6
7
|
// keys, never asking Infra (Infra's README, "Claude"). This is what of it
|
|
7
|
-
// the client
|
|
8
|
-
//
|
|
9
|
-
// requests and connections, and checking a token.
|
|
8
|
+
// the client does: where the app was reached, the server's metadata, the
|
|
9
|
+
// OAuth calls Claude makes, passed on to Infra, and checking a token.
|
|
10
10
|
/**
|
|
11
11
|
* Where a request reached the app: `https://vmdsystem.com`, or plain http
|
|
12
12
|
* on this computer, as a dev server is. Behind Railway's proxy a request's
|
|
13
13
|
* own address says http though the browser's said https, so only the host
|
|
14
|
-
* counts. Every token Claude holds is bound to it, as its issuer
|
|
15
|
-
* the MCP's path (`MCP_PATH`), its audience.
|
|
14
|
+
* counts. Every token Claude holds is bound to it, as its issuer.
|
|
16
15
|
*/
|
|
17
16
|
export function requestOrigin(request_url) {
|
|
18
17
|
const { host, hostname } = new URL(request_url);
|
|
@@ -20,22 +19,58 @@ export function requestOrigin(request_url) {
|
|
|
20
19
|
hostname.endsWith(".localhost");
|
|
21
20
|
return local ? `http://${host}` : `https://${host}`;
|
|
22
21
|
}
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
/**
|
|
23
|
+
* The OAuth server's metadata (RFC 8414), which Claude reads at the app's
|
|
24
|
+
* `/.well-known/oauth-authorization-server`: the app is the issuer, its
|
|
25
|
+
* own addresses the server's, and Infra's public keys the ones its tokens
|
|
26
|
+
* are signed with. It's the same for every app but for those, so the
|
|
27
|
+
* client builds it, with no call to Infra.
|
|
28
|
+
*/
|
|
29
|
+
export function oauthMetadata({ origin, paths, infra_url }) {
|
|
30
|
+
const oauth = `${origin}${paths.oauth}`;
|
|
31
|
+
return {
|
|
32
|
+
issuer: origin,
|
|
33
|
+
authorization_endpoint: `${oauth}/authorize`,
|
|
34
|
+
token_endpoint: `${oauth}/token`,
|
|
35
|
+
registration_endpoint: `${oauth}/register`,
|
|
36
|
+
revocation_endpoint: `${oauth}/revoke`,
|
|
37
|
+
jwks_uri: new URL("/.well-known/jwks.json", new URL(infra_url).origin)
|
|
38
|
+
.href,
|
|
39
|
+
response_types_supported: ["code"],
|
|
40
|
+
grant_types_supported: ["authorization_code", "refresh_token"],
|
|
41
|
+
code_challenge_methods_supported: ["S256"],
|
|
42
|
+
token_endpoint_auth_methods_supported: ["none"],
|
|
43
|
+
revocation_endpoint_auth_methods_supported: ["none"],
|
|
44
|
+
scopes_supported: [...AGENT_SCOPES],
|
|
45
|
+
client_id_metadata_document_supported: true,
|
|
46
|
+
authorization_response_iss_parameter_supported: true
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
// What a call passed on may carry, of the request Claude or the browser
|
|
50
|
+
// sent: what the body is, and what Claude accepts back
|
|
27
51
|
const PASSED_HEADERS = ["accept", "content-type"];
|
|
28
52
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
53
|
+
* Answers one of Claude's OAuth calls to the app (`call`), its request
|
|
54
|
+
* unread:
|
|
55
|
+
*
|
|
56
|
+
* - `metadata`: built here (`oauthMetadata`)
|
|
57
|
+
* - `authorize`: Infra saves the request, then the browser goes to the
|
|
58
|
+
* app's Allow page, `<paths.allow>/<request id>`, or back to Claude with
|
|
59
|
+
* an error. One whose return address isn't Claude's is answered 400 in
|
|
60
|
+
* plain words, never redirected, so the app is never an open redirect.
|
|
61
|
+
* - `token`, `register`, `revoke`: Infra's answer, as it is (RFC 6749's)
|
|
33
62
|
*/
|
|
34
|
-
export async function
|
|
63
|
+
export async function passOAuth({ url, key, send, paths, request, call, origin }) {
|
|
64
|
+
if (call === "metadata") {
|
|
65
|
+
return Response.json(oauthMetadata({ origin, paths, infra_url: url }), {
|
|
66
|
+
headers: { "Cache-Control": "no-store" }
|
|
67
|
+
});
|
|
68
|
+
}
|
|
35
69
|
const { search } = new URL(request.url);
|
|
36
70
|
const headers = new Headers({
|
|
37
71
|
Authorization: `Bearer ${key}`,
|
|
38
|
-
[APP_ORIGIN_HEADER]: origin
|
|
72
|
+
[APP_ORIGIN_HEADER]: origin,
|
|
73
|
+
[CLIENT_HEADER]: CLIENT
|
|
39
74
|
});
|
|
40
75
|
for (const name of PASSED_HEADERS) {
|
|
41
76
|
const value = request.headers.get(name);
|
|
@@ -46,99 +81,99 @@ export async function passThrough({ url, key, send, request, path, origin }) {
|
|
|
46
81
|
const body = request.method === "GET" || request.method === "HEAD"
|
|
47
82
|
? undefined
|
|
48
83
|
: await request.text();
|
|
84
|
+
const path = `/agents/oauth/${call}`;
|
|
85
|
+
let answer;
|
|
49
86
|
try {
|
|
50
|
-
|
|
87
|
+
answer = await send(`${url}${path}${search}`, {
|
|
51
88
|
method: request.method,
|
|
52
89
|
headers,
|
|
53
90
|
body,
|
|
54
91
|
redirect: "manual",
|
|
55
92
|
signal: AbortSignal.timeout(15 * SECOND)
|
|
56
93
|
});
|
|
57
|
-
console.info("Infra answered", {
|
|
58
|
-
call: `/oauth${path}`,
|
|
59
|
-
status: answer.status
|
|
60
|
-
});
|
|
61
|
-
const passed = new Headers({ "Cache-Control": "no-store" });
|
|
62
|
-
for (const name of ["content-type", "location"]) {
|
|
63
|
-
const value = answer.headers.get(name);
|
|
64
|
-
if (value !== null) {
|
|
65
|
-
passed.set(name, value);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return new Response(await answer.arrayBuffer(), {
|
|
69
|
-
status: answer.status,
|
|
70
|
-
headers: passed
|
|
71
|
-
});
|
|
72
94
|
}
|
|
73
95
|
catch (error) {
|
|
74
|
-
console.error("Infra didn't answer", { call:
|
|
96
|
+
console.error("Infra didn't answer", { call: path, error });
|
|
75
97
|
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.");
|
|
76
98
|
}
|
|
99
|
+
console.info("Infra answered", { call: path, status: answer.status });
|
|
100
|
+
if (call === "authorize") {
|
|
101
|
+
return await authorized(answer, origin, paths);
|
|
102
|
+
}
|
|
103
|
+
const passed = new Headers({ "Cache-Control": "no-store" });
|
|
104
|
+
const type = answer.headers.get("content-type");
|
|
105
|
+
if (type !== null) {
|
|
106
|
+
passed.set("content-type", type);
|
|
107
|
+
}
|
|
108
|
+
return new Response(await answer.arrayBuffer(), {
|
|
109
|
+
status: answer.status,
|
|
110
|
+
headers: passed
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
// Where the browser goes from Claude's authorize request, by what Infra
|
|
114
|
+
// answered: the app's Allow page, back to Claude, or nowhere, with why
|
|
115
|
+
async function authorized(answer, origin, paths) {
|
|
116
|
+
const json = await answer.json().catch((error) => {
|
|
117
|
+
console.error("Infra's authorize answer isn't JSON", {
|
|
118
|
+
status: answer.status,
|
|
119
|
+
error
|
|
120
|
+
});
|
|
121
|
+
return null;
|
|
122
|
+
});
|
|
123
|
+
const made = agent_authorized.safeParse(json);
|
|
124
|
+
if (answer.status === 200 && made.success) {
|
|
125
|
+
const to = "request" in made.data
|
|
126
|
+
? `${origin}${paths.allow}/${made.data.request.id}`
|
|
127
|
+
: made.data.redirect_to;
|
|
128
|
+
return new Response(null, {
|
|
129
|
+
status: 302,
|
|
130
|
+
headers: { Location: to, "Cache-Control": "no-store" }
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
const refused = authorize_refused.safeParse(json);
|
|
134
|
+
if (refused.success) {
|
|
135
|
+
console.warn("Claude's authorize request refused", {
|
|
136
|
+
reason: refused.data.reason
|
|
137
|
+
});
|
|
138
|
+
return new Response(refused.data.message, {
|
|
139
|
+
status: 400,
|
|
140
|
+
headers: {
|
|
141
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
142
|
+
"Cache-Control": "no-store"
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
const failed = infra_failure.safeParse(json);
|
|
147
|
+
console.error("Infra's authorize answer doesn't fit", {
|
|
148
|
+
status: answer.status,
|
|
149
|
+
answer: json
|
|
150
|
+
});
|
|
151
|
+
return unavailableOAuth(failed.success
|
|
152
|
+
? failed.data.error.message
|
|
153
|
+
: `Infra answered Claude's authorize request with ${answer.status}, in a shape this version of @superforge/infra can't read.`);
|
|
77
154
|
}
|
|
78
155
|
/**
|
|
79
|
-
* What
|
|
80
|
-
* 6749 has it, so Claude says why login isn't working rather than
|
|
81
|
-
* `
|
|
156
|
+
* What one of Claude's OAuth calls answers when Infra can't be asked, as
|
|
157
|
+
* RFC 6749 has it, so Claude says why login isn't working rather than
|
|
158
|
+
* looping: why, as `error_description`, which Claude shows
|
|
82
159
|
*/
|
|
83
160
|
export function unavailableOAuth(message) {
|
|
84
|
-
return Response.json({
|
|
85
|
-
error: "temporarily_unavailable",
|
|
86
|
-
error_description: message,
|
|
87
|
-
message
|
|
88
|
-
}, { status: 503, headers: { "Cache-Control": "no-store" } });
|
|
161
|
+
return Response.json({ error: "temporarily_unavailable", error_description: message }, { status: 503, headers: { "Cache-Control": "no-store" } });
|
|
89
162
|
}
|
|
90
|
-
// What Infra answers about Claude's requests and connections (its README,
|
|
91
|
-
// "Claude"), as the app reads it
|
|
92
|
-
export const agent_request = z.object({
|
|
93
|
-
request: z.object({
|
|
94
|
-
id: z.string(),
|
|
95
|
-
client_name: z.string(),
|
|
96
|
-
scopes: z.array(z.enum(AGENT_SCOPES)),
|
|
97
|
-
expires_at: z.coerce.date()
|
|
98
|
-
})
|
|
99
|
-
});
|
|
100
|
-
export const agent_redirect = z.object({ redirect_to: z.url() });
|
|
101
|
-
export const agent_connections = z.object({
|
|
102
|
-
connections: z.array(z.object({
|
|
103
|
-
id: z.string(),
|
|
104
|
-
email: z.string(),
|
|
105
|
-
client_name: z.string(),
|
|
106
|
-
scopes: z.array(z.enum(AGENT_SCOPES)),
|
|
107
|
-
last_used_at: z.coerce.date(),
|
|
108
|
-
created_at: z.coerce.date()
|
|
109
|
-
}))
|
|
110
|
-
});
|
|
111
|
-
export const agent_ended = z.object({ ended: z.number().int() });
|
|
112
|
-
/** Why Infra refused a call about a request or a connection, in its words */
|
|
113
|
-
export const agent_refused = z.object({
|
|
114
|
-
error: z.enum(["not-found", "not-asked"]),
|
|
115
|
-
message: z
|
|
116
|
-
.string()
|
|
117
|
-
.catch("Infra has no such request or connection for this project.")
|
|
118
|
-
});
|
|
119
|
-
// The claims Infra's tokens carry beyond the standard ones (Infra's README)
|
|
120
|
-
const agent_claims = z.object({
|
|
121
|
-
sub: z.string(),
|
|
122
|
-
scope: z.string(),
|
|
123
|
-
project: z.string(),
|
|
124
|
-
mode: z.enum(["live", "test"]),
|
|
125
|
-
connection: z.string()
|
|
126
|
-
});
|
|
127
163
|
/** Where a token's signature is checked: Infra's published public keys */
|
|
128
164
|
export function infraKeys(url) {
|
|
129
165
|
return createRemoteJWKSet(new URL("/.well-known/jwks.json", new URL(url).origin), { timeoutDuration: 10 * SECOND, cooldownDuration: 30 * SECOND });
|
|
130
166
|
}
|
|
131
167
|
/**
|
|
132
|
-
* Checks a token Claude brings, as Infra's README says to: Infra signed
|
|
133
|
-
* for this app's origin
|
|
134
|
-
*
|
|
135
|
-
* mode, as `GET /v1/project` answers them.
|
|
168
|
+
* Checks a token Claude brings, as Infra's README says to: Infra signed
|
|
169
|
+
* it, for this app's `origin`, for `resource`, the address it was brought
|
|
170
|
+
* to, for this project in the key's mode, and it hasn't run out
|
|
136
171
|
*/
|
|
137
172
|
export async function checkToken(keys, token, expected) {
|
|
138
173
|
try {
|
|
139
174
|
const { payload } = await jwtVerify(token, keys, {
|
|
140
175
|
issuer: expected.origin,
|
|
141
|
-
audience:
|
|
176
|
+
audience: expected.resource,
|
|
142
177
|
algorithms: ["ES256"]
|
|
143
178
|
});
|
|
144
179
|
const claims = agent_claims.safeParse(payload);
|
|
@@ -155,7 +190,7 @@ export async function checkToken(keys, token, expected) {
|
|
|
155
190
|
return {
|
|
156
191
|
ok: true,
|
|
157
192
|
data: {
|
|
158
|
-
|
|
193
|
+
user_id: claims.data.sub,
|
|
159
194
|
scopes: AGENT_SCOPES.filter((scope) => said.includes(scope)),
|
|
160
195
|
connection_id: claims.data.connection
|
|
161
196
|
}
|
package/output/choose.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type AppPaths } from "./contract/index.js";
|
|
1
2
|
import type { Infra } from "./infra.js";
|
|
2
3
|
/** Which Infra a server calls, as `chooseInfra` chose it. */
|
|
3
4
|
export type InfraChoice = {
|
|
@@ -5,9 +6,10 @@ export type InfraChoice = {
|
|
|
5
6
|
using: "secret key" | "test key" | "stand-in" | "nothing";
|
|
6
7
|
};
|
|
7
8
|
/** The Infra a server calls, by how it runs and what it's been given. */
|
|
8
|
-
export declare function chooseInfra({ development, environment, checkout, domains }: {
|
|
9
|
+
export declare function chooseInfra({ development, environment, checkout, domains, paths }: {
|
|
9
10
|
development: boolean;
|
|
10
11
|
environment: Record<string, string | undefined>;
|
|
11
12
|
checkout: string;
|
|
12
13
|
domains?: string[];
|
|
14
|
+
paths?: AppPaths;
|
|
13
15
|
}): InfraChoice;
|
package/output/choose.js
CHANGED
|
@@ -4,6 +4,7 @@ import { parseEnv } from "node:util";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import { unavailableOAuth } from "./agents.js";
|
|
6
6
|
import { INFRA_URL, infraClient } from "./client.js";
|
|
7
|
+
import { APP_PATHS } from "./contract/index.js";
|
|
7
8
|
import { standInInfra } from "./stand-in.js";
|
|
8
9
|
// Which Infra the server calls, by what it runs as and the keys it has:
|
|
9
10
|
//
|
|
@@ -23,11 +24,13 @@ import { standInInfra } from "./stand-in.js";
|
|
|
23
24
|
const SECRET_KEY = /^sf_secret_[0-9A-Za-z]{32}$/;
|
|
24
25
|
const TEST_KEY = /^sf_test_[0-9A-Za-z]{32}$/;
|
|
25
26
|
// What the server reads to reach Infra, by the variables' names: the
|
|
26
|
-
// secret key Railway holds, a checkout's test key
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
27
|
+
// secret key Railway holds, and a checkout's test key. A dev server reads
|
|
28
|
+
// them from the checkout's `.env` too (`checkoutVariables`), under whatever
|
|
29
|
+
// the shell sets, and only a dev server reads `INFRA_URL`, which points it
|
|
30
|
+
// at another Infra while Infra itself is worked on, like a superforge
|
|
31
|
+
// checkout's own: a build always calls Infra's own address, so no variable
|
|
32
|
+
// can send its secret key, and every login, anywhere else. One set empty,
|
|
33
|
+
// as `.env.example` has them, is unset.
|
|
31
34
|
const unset_when_empty = (value) => typeof value === "string" && value.trim() === "" ? undefined : value;
|
|
32
35
|
const infra_environment = z.object({
|
|
33
36
|
INFRA_SECRET_KEY: z.preprocess(unset_when_empty, z.string().trim().optional()),
|
|
@@ -35,29 +38,28 @@ const infra_environment = z.object({
|
|
|
35
38
|
INFRA_URL: z.preprocess(unset_when_empty, z.url().optional())
|
|
36
39
|
});
|
|
37
40
|
/** The Infra a server calls, by how it runs and what it's been given. */
|
|
38
|
-
export function chooseInfra({ development, environment, checkout, domains = [] }) {
|
|
41
|
+
export function chooseInfra({ development, environment, checkout, domains = [], paths = APP_PATHS }) {
|
|
42
|
+
// A build reads only the keys, whatever INFRA_URL says
|
|
39
43
|
const parsed = infra_environment.safeParse(development
|
|
40
44
|
? { ...checkoutVariables(checkout), ...environment }
|
|
41
|
-
: environment);
|
|
45
|
+
: { ...environment, INFRA_URL: undefined });
|
|
42
46
|
if (!parsed.success) {
|
|
43
47
|
const names = parsed.error.issues.map((issue) => issue.path.join("."));
|
|
44
48
|
console.error("Infra can't be reached: these aren't set right", {
|
|
45
49
|
names
|
|
46
50
|
});
|
|
47
51
|
return {
|
|
48
|
-
infra: unavailableInfra(`Infra can't be reached: ${names.join(", ")} ${names.length === 1 ? "isn't" : "aren't"} set right${development ? " in the checkout's .env" : " on the app's service in Railway"}
|
|
52
|
+
infra: unavailableInfra(`Infra can't be reached: ${names.join(", ")} ${names.length === 1 ? "isn't" : "aren't"} set right${development ? " in the checkout's .env" : " on the app's service in Railway"}.`),
|
|
49
53
|
using: "nothing"
|
|
50
54
|
};
|
|
51
55
|
}
|
|
52
56
|
const { INFRA_SECRET_KEY } = parsed.data;
|
|
53
|
-
// Without a slash at the end, since each call's path starts with one
|
|
54
|
-
const url = (parsed.data.INFRA_URL ?? INFRA_URL).replace(/\/+$/, "");
|
|
55
57
|
if (!development) {
|
|
56
58
|
if (INFRA_SECRET_KEY !== undefined &&
|
|
57
59
|
SECRET_KEY.test(INFRA_SECRET_KEY)) {
|
|
58
|
-
console.info("Infra: calling it with the secret key"
|
|
60
|
+
console.info("Infra: calling it with the secret key");
|
|
59
61
|
return {
|
|
60
|
-
infra: infraClient({ key: INFRA_SECRET_KEY,
|
|
62
|
+
infra: infraClient({ key: INFRA_SECRET_KEY, paths }),
|
|
61
63
|
using: "secret key"
|
|
62
64
|
};
|
|
63
65
|
}
|
|
@@ -67,10 +69,15 @@ export function chooseInfra({ development, environment, checkout, domains = [] }
|
|
|
67
69
|
return { infra: unavailableInfra(message), using: "nothing" };
|
|
68
70
|
}
|
|
69
71
|
}
|
|
72
|
+
// Without a slash at the end, since each call's path starts with one
|
|
73
|
+
const url = (parsed.data.INFRA_URL ?? INFRA_URL).replace(/\/+$/, "");
|
|
70
74
|
const test_key = parsed.data.INFRA_TEST_KEY;
|
|
71
75
|
if (test_key !== undefined && TEST_KEY.test(test_key)) {
|
|
72
76
|
console.info("Infra: calling it with a test key", { url });
|
|
73
|
-
return {
|
|
77
|
+
return {
|
|
78
|
+
infra: infraClient({ key: test_key, url, paths }),
|
|
79
|
+
using: "test key"
|
|
80
|
+
};
|
|
74
81
|
}
|
|
75
82
|
else {
|
|
76
83
|
if (test_key !== undefined) {
|