@superforge/infra 0.1.1 → 0.3.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.
@@ -0,0 +1,38 @@
1
+ import { z } from "zod";
2
+ /** A project's public ID's prefix: `sf_project_` and 20 letters and digits */
3
+ export declare const PROJECT_ID_PREFIX = "sf_project_";
4
+ /** A key's mode: `live` sends for real, `test` answers what it would have */
5
+ export declare const KEY_MODES: readonly ["live", "test"];
6
+ export declare const key_mode: z.ZodEnum<{
7
+ live: "live";
8
+ test: "test";
9
+ }>;
10
+ export type KeyMode = z.infer<typeof key_mode>;
11
+ /**
12
+ * Each mode's key's prefix, then 32 letters and digits. The prefix lets a
13
+ * leak scanner spot one, and tells a test key from a secret one at a
14
+ * glance.
15
+ */
16
+ export declare const KEY_PREFIXES: Record<KeyMode, string>;
17
+ /**
18
+ * `GET /v1/project`: which project the key is for, its mode, and its
19
+ * domains, the only hosts besides localhost a login link goes to and
20
+ * Claude logs in at
21
+ */
22
+ export declare const infra_project: z.ZodObject<{
23
+ project: z.ZodObject<{
24
+ id: z.ZodString;
25
+ name: z.ZodString;
26
+ domains: z.ZodArray<z.ZodString>;
27
+ }, z.core.$strip>;
28
+ mode: z.ZodEnum<{
29
+ live: "live";
30
+ test: "test";
31
+ }>;
32
+ }, z.core.$strip>;
33
+ export type InfraProject = z.output<typeof infra_project>;
34
+ /**
35
+ * The header every call carries, saying which release of @superforge/infra
36
+ * made it (`@superforge/infra/0.3.0`), which Infra logs and keeps per key
37
+ */
38
+ export declare const CLIENT_HEADER = "Infra-Client";
@@ -0,0 +1,36 @@
1
+ import { z } from "zod";
2
+ // Projects and their keys: an app is a project on Infra, and its server
3
+ // calls Infra with one of the project's keys, sent as `Authorization:
4
+ // Bearer <key>`.
5
+ /** A project's public ID's prefix: `sf_project_` and 20 letters and digits */
6
+ export const PROJECT_ID_PREFIX = "sf_project_";
7
+ /** A key's mode: `live` sends for real, `test` answers what it would have */
8
+ export const KEY_MODES = ["live", "test"];
9
+ export const key_mode = z.enum(KEY_MODES);
10
+ /**
11
+ * Each mode's key's prefix, then 32 letters and digits. The prefix lets a
12
+ * leak scanner spot one, and tells a test key from a secret one at a
13
+ * glance.
14
+ */
15
+ export const KEY_PREFIXES = {
16
+ live: "sf_secret_",
17
+ test: "sf_test_"
18
+ };
19
+ /**
20
+ * `GET /v1/project`: which project the key is for, its mode, and its
21
+ * domains, the only hosts besides localhost a login link goes to and
22
+ * Claude logs in at
23
+ */
24
+ export const infra_project = z.object({
25
+ project: z.object({
26
+ id: z.string(),
27
+ name: z.string(),
28
+ domains: z.array(z.string())
29
+ }),
30
+ mode: key_mode
31
+ });
32
+ /**
33
+ * The header every call carries, saying which release of @superforge/infra
34
+ * made it (`@superforge/infra/0.3.0`), which Infra logs and keeps per key
35
+ */
36
+ export const CLIENT_HEADER = "Infra-Client";
package/output/index.d.ts CHANGED
@@ -1,9 +1,8 @@
1
- export { APP_ORIGIN_HEADER, requestOrigin } from "./agents.js";
1
+ export { oauthMetadata, requestOrigin } from "./agents.js";
2
2
  export { chooseInfra, type InfraChoice } from "./choose.js";
3
3
  export { INFRA_URL, infraClient } from "./client.js";
4
- export { COMMENT_BODY_LIMIT, filed_comment, LONGEST_COMMENT } from "./comments.js";
5
- export type { AgentConnection, AgentRefused, AgentRequest, AgentToken, CommentRefused, FiledComment, Infra, InfraProject, InfraUnavailable, KeptComment, LoginProof, StartedLogin, StartRefused, VerifyRefused } from "./infra.js";
6
- export { AGENT_SCOPES, type AgentScope, MCP_PATH } from "./primitives.js";
4
+ export * from "./contract/index.js";
5
+ export type { AgentToken, Infra, InfraUnavailable } from "./infra.js";
7
6
  export type { AsyncOperationResult, OperationResult } from "./results.js";
8
7
  export { standInAgentToken, standInInfra, type WaitingLogin } from "./stand-in.js";
9
- export { CLIENT_HEADER, PACKAGE_VERSION } from "./version.js";
8
+ export { PACKAGE_VERSION } from "./version.js";
package/output/index.js CHANGED
@@ -1,17 +1,19 @@
1
1
  // @superforge/infra: Superforge Infra, the backend services Foundation and
2
2
  // Skylevel apps run on, as an app's server calls it. README.md has how an
3
- // app uses it.
3
+ // app uses it. Infra's contract, every call's request and answer as zod
4
+ // schemas, is here too, and on its own at `@superforge/infra/contract`,
5
+ // which needs nothing of Node's, for a page.
4
6
  // Claude's login to an app's MCP: where a request reached the app, which
5
- // every token is bound to, and the path its MCP answers at
6
- export { APP_ORIGIN_HEADER, requestOrigin } from "./agents.js";
7
+ // every token is bound to, and the OAuth server's metadata
8
+ export { oauthMetadata, requestOrigin } from "./agents.js";
7
9
  // Calling Infra: the real one over HTTP with a project's key, the
8
10
  // stand-in, Infra's test mode in memory, and choosing between them by how
9
11
  // the server runs and the keys it has
10
12
  export { chooseInfra } from "./choose.js";
11
13
  export { INFRA_URL, infraClient } from "./client.js";
12
- // What Infra takes of a comment from an app's toolbar
13
- export { COMMENT_BODY_LIMIT, filed_comment, LONGEST_COMMENT } from "./comments.js";
14
- export { AGENT_SCOPES, MCP_PATH } from "./primitives.js";
14
+ // Infra's contract (contract/): every call's request and answer, every
15
+ // reason it refuses one, and the constants they're made of
16
+ export * from "./contract/index.js";
15
17
  export { standInAgentToken, standInInfra } from "./stand-in.js";
16
- // Which version this is, which every call tells Infra in `Infra-Client`
17
- export { CLIENT_HEADER, PACKAGE_VERSION } from "./version.js";
18
+ // Which release this is, which every call tells Infra in `Infra-Client`
19
+ export { PACKAGE_VERSION } from "./version.js";
package/output/infra.d.ts CHANGED
@@ -1,97 +1,83 @@
1
- import type { z } from "zod";
2
- import type { AgentConnection, AgentToken, agent_refused, agent_request } from "./agents.js";
3
- import type { comment_refused, infra_project, kept_comment, start_refused, started_login, verified_login, verify_refused } from "./answers.js";
4
- import type { FiledComment } from "./comments.js";
5
- import type { AgentScope } from "./primitives.js";
1
+ import type { AgentAllow, AgentConnection, AgentRefused, AgentRequest, CommentRefused, FiledComment, InfraProject, KeptComment, LoginProof, LoginStart, OAuthCall, StartedLogin, StartRefused, VerifiedLogin, VerifyRefused } from "./contract/index.js";
6
2
  import type { AsyncOperationResult } from "./results.js";
7
- /** A code typed with the address it went to, or a link's token */
8
- export type LoginProof = {
9
- email: string;
10
- code: string;
11
- } | {
12
- token: string;
13
- };
14
3
  /** What an app asks of Infra, whichever answers: the real one or the stand-in */
15
4
  export type Infra = {
16
5
  /**
17
- * Which project the key is for, its mode, and the domains its login
18
- * emails link to (`domains`, null from an Infra that doesn't say),
19
- * which Settings shows beside the app's own (an app's Settings).
6
+ * Which project the key is for, its mode, and its domains, the only
7
+ * hosts besides localhost its login links go to and Claude logs in at
20
8
  */
21
9
  project(): AsyncOperationResult<InfraProject, InfraUnavailable>;
22
10
  /**
23
- * Emails `email` a six-digit code, and a link back to the app's
24
- * `/login` when `origin` is one of the project's domains or
25
- * localhost. `ip` is the visitor's, which Infra's limits count.
11
+ * Sends someone a six-digit code, on `channel` to `address`, and a link
12
+ * to `link_to` when it's on one of the project's domains or localhost.
13
+ * `ip` is theirs, which Infra's limits count.
26
14
  */
27
- startLogin(request: {
28
- email: string;
29
- ip: string;
30
- origin: string | null;
31
- }): AsyncOperationResult<StartedLogin, StartRefused | InfraUnavailable>;
15
+ startLogin(request: LoginStart): AsyncOperationResult<StartedLogin, StartRefused | InfraUnavailable>;
32
16
  /**
33
- * Whose email `proof` is: the code typed with the address it went to,
34
- * or the link's token. Either works once.
17
+ * Whose address `proof` is: the code typed with the address it went to,
18
+ * or the link's token alone. Either works once, and spends the other.
35
19
  */
36
- verifyLogin(proof: LoginProof): AsyncOperationResult<z.output<typeof verified_login>, VerifyRefused | InfraUnavailable>;
20
+ verifyLogin(proof: LoginProof): AsyncOperationResult<VerifiedLogin, VerifyRefused | InfraUnavailable>;
37
21
  /**
38
- * Sends Infra a comment someone logged in wrote about an element of a
39
- * page, which the toolbar posted to the app's own /api/comments, with
40
- * who they are (comments.ts). Infra keeps it, a test key's too, and
22
+ * Sends Infra a comment someone on the app's team filed from its
23
+ * toolbar, with who they are. Infra keeps it, a test key's too, and
41
24
  * answers its ID. It's never retried: a comment sent twice is kept
42
25
  * twice.
43
26
  */
44
27
  fileComment(comment: FiledComment): AsyncOperationResult<KeptComment, CommentRefused | InfraUnavailable>;
45
28
  /**
46
- * Passes one of Claude's OAuth calls through to Infra (`path`, after
47
- * `/oauth`: `/authorize`, `/token`, `/metadata`…), with the app's
48
- * key and `origin`, and answers what Infra answers, as it is
29
+ * Answers one of Claude's OAuth calls to the app, Claude's or the
30
+ * browser's `request` unread: the server's metadata, built here, and
31
+ * the rest on Infra, with the app's key and `origin`. Authorize sends
32
+ * the browser to the app's Allow page, or back to Claude.
49
33
  */
50
- passOAuth(request: Request, path: string, origin: string): Promise<Response>;
34
+ passOAuth(request: Request, call: OAuthCall | "metadata", origin: string): Promise<Response>;
51
35
  /** What the Allow page shows of a request Claude made */
52
36
  agentRequest(request_id: string): AsyncOperationResult<AgentRequest, AgentRefused | InfraUnavailable>;
53
37
  /**
54
- * Someone on the team, by their email, allowed Claude's request: where
55
- * their browser goes back to Claude, with the one-time code
38
+ * Someone on the team, by the app's ID for them, allowed Claude's
39
+ * request: where their browser goes back to Claude, with the one-time
40
+ * code
56
41
  */
57
- allowAgent(request_id: string, allowed: {
58
- email: string;
59
- scopes: AgentScope[];
60
- }): AsyncOperationResult<{
42
+ allowAgent(request_id: string, allowed: AgentAllow): AsyncOperationResult<{
61
43
  redirect_to: string;
62
44
  }, AgentRefused | InfraUnavailable>;
63
45
  /** Someone declined it: where their browser goes back to Claude */
64
46
  denyAgent(request_id: string): AsyncOperationResult<{
65
47
  redirect_to: string;
66
48
  }, AgentRefused | InfraUnavailable>;
67
- /** Someone's connections, oldest first, for their Connected list */
68
- agentConnections(email: string): AsyncOperationResult<AgentConnection[], InfraUnavailable>;
49
+ /** Someone's connections, by the app's ID for them, oldest first */
50
+ agentConnections(user_id: string): AsyncOperationResult<AgentConnection[], InfraUnavailable>;
69
51
  /** Ends one connection: Claude can't refresh it */
70
52
  endAgentConnection(connection_id: string): AsyncOperationResult<null, AgentRefused | InfraUnavailable>;
71
53
  /** Ends all of someone's, as taking them off the team does */
72
- endAgentConnections(email: string): AsyncOperationResult<number, InfraUnavailable>;
54
+ endAgentConnections(user_id: string): AsyncOperationResult<number, InfraUnavailable>;
73
55
  /**
74
56
  * Whom a token Claude brings acts as, and what it may do, when Infra
75
- * signed it for this app at `origin`, checked with Infra's public keys
57
+ * signed it for this app at `origin`, for `resource`, the address of
58
+ * the app's MCP it was brought to, checked with Infra's public keys
76
59
  */
77
- checkAgentToken(token: string, origin: string): AsyncOperationResult<AgentToken, "invalid-token" | InfraUnavailable>;
60
+ checkAgentToken(token: string, expected: {
61
+ origin: string;
62
+ resource: string;
63
+ }): AsyncOperationResult<AgentToken, "invalid-token" | InfraUnavailable>;
64
+ };
65
+ /** What the app learns from a token Claude brings, once it's checked */
66
+ export type AgentToken = {
67
+ /** Whom Claude acts as: the app's ID for them, checked against its team */
68
+ user_id: string;
69
+ /** What they let Claude do */
70
+ scopes: AgentAllow["scopes"];
71
+ /** Which of their connections it is, to end it */
72
+ connection_id: string;
78
73
  };
79
- export type InfraProject = z.output<typeof infra_project>;
80
- export type StartedLogin = z.output<typeof started_login>;
81
- export type StartRefused = z.output<typeof start_refused>;
82
- export type VerifyRefused = z.output<typeof verify_refused>;
83
- export type KeptComment = z.output<typeof kept_comment>;
84
- export type CommentRefused = z.output<typeof comment_refused>;
85
- export type AgentRequest = z.output<typeof agent_request>["request"];
86
- export type AgentRefused = z.output<typeof agent_refused>;
87
- export type { AgentConnection, AgentToken, FiledComment };
88
74
  /**
89
75
  * Infra couldn't be asked, and why, in plain words: no key in a build, a
90
- * key it refused, an error this version of the package doesn't know, or an
91
- * answer that didn't come or didn't make sense. `message` is Infra's own
92
- * when it gave one, and says what fixes it when something does. It's
93
- * logged where it happens; an app shows it to its own team, never to a
94
- * visitor, who only learns that login isn't working.
76
+ * key it refused, an Infra that didn't answer, or an answer that doesn't
77
+ * fit the contract. `message` is Infra's own when it gave one, and says
78
+ * what fixes it when something does. It's logged where it happens; an app
79
+ * shows it to its own team, never to a visitor, who only learns that login
80
+ * isn't working.
95
81
  */
96
82
  export type InfraUnavailable = {
97
83
  reason: "unavailable";
@@ -3,11 +3,3 @@ export declare const SECOND = 1000;
3
3
  export declare const MINUTE: number;
4
4
  /** 32 random bytes in base64url: a login link's token, as Infra makes one */
5
5
  export declare function randomToken(): string;
6
- /** What someone on an app's team can let Claude do through its MCP */
7
- export declare const AGENT_SCOPES: readonly ["read", "act"];
8
- export type AgentScope = (typeof AGENT_SCOPES)[number];
9
- /**
10
- * Where an app's MCP answers, which Infra logs Claude in to: the admin's,
11
- * for the app's own team. Every token Claude holds is for it.
12
- */
13
- export declare const MCP_PATH = "/admin/mcp";
@@ -11,10 +11,3 @@ export function randomToken() {
11
11
  .replaceAll("/", "_")
12
12
  .replaceAll("=", "");
13
13
  }
14
- /** What someone on an app's team can let Claude do through its MCP */
15
- export const AGENT_SCOPES = ["read", "act"];
16
- /**
17
- * Where an app's MCP answers, which Infra logs Claude in to: the admin's,
18
- * for the app's own team. Every token Claude holds is for it.
19
- */
20
- export const MCP_PATH = "/admin/mcp";
@@ -1,8 +1,8 @@
1
+ import { type AgentScope } from "./contract/index.js";
1
2
  import type { Infra } from "./infra.js";
2
- import { type AgentScope } from "./primitives.js";
3
- /** A login the stand-in sent, waiting to be verified. */
3
+ /** A login the stand-in sent, waiting to be verified, by its address */
4
4
  export type WaitingLogin = {
5
- email: string;
5
+ address: string;
6
6
  code: string;
7
7
  token: string;
8
8
  attempts: number;
@@ -12,14 +12,14 @@ export type WaitingLogin = {
12
12
  * Infra's test mode, in memory: what a test key gets from the real one,
13
13
  * with no key and no network. Starting a login sends nothing and answers
14
14
  * the code and the link, and verifying checks them as Infra does: each
15
- * works once, for 15 minutes, and a code for five tries. No limits apply,
16
- * as none do to a test key. A comment is checked as Infra checks it, and
17
- * answered an ID, though nothing keeps it, where Infra keeps a test key's.
18
- * Its project has the `domains` it's given, an app's website's. An app's
19
- * tests always use it, so they never reach Infra, and so does a dev server
20
- * with no test key (choose.ts). A build never does: anyone could log in
21
- * as anyone, since the page shows the code. Its refusals say what Infra's
22
- * say.
15
+ * works once, for 15 minutes, a code for five tries, and using either
16
+ * spends both. No limits apply, as none do to a test key. A request is
17
+ * checked with Infra's own contract, as Infra checks it, and a comment is
18
+ * answered an ID, though nothing keeps it. Its project has the `domains`
19
+ * it's given, an app's website's. An app's tests always use it, so they
20
+ * never reach Infra, and so does a dev server with no test key
21
+ * (choose.ts). A build never does: anyone could log in as anyone, since
22
+ * the page shows the code. Its refusals say what Infra's say.
23
23
  *
24
24
  * `waiting` is where it keeps what it sent, by address: its own, or one
25
25
  * that outlives a dev server's reloads. With `logs`, as a dev server has
@@ -31,12 +31,14 @@ export declare function standInInfra({ waiting, logs, domains }?: {
31
31
  domains?: string[];
32
32
  }): Infra;
33
33
  /**
34
- * A token as Infra signs one, for a test to call the API as Claude does:
35
- * acting as `email`, with `scopes`, for the app at `origin` (the tests'
36
- * own is `http://localhost`), which the stand-in's `checkAgentToken` takes
34
+ * A token as Infra signs one, for a test to call the app as Claude does:
35
+ * acting as `user_id`, with `scopes`, for the app at `origin` (the tests'
36
+ * own is `http://localhost`) and the address `resource`, its MCP's, which
37
+ * the stand-in's `checkAgentToken` takes
37
38
  */
38
- export declare function standInAgentToken({ email, scopes, origin, expires_in }: {
39
- email: string;
39
+ export declare function standInAgentToken({ user_id, resource, scopes, origin, expires_in }: {
40
+ user_id: string;
41
+ resource: string;
40
42
  scopes?: AgentScope[];
41
43
  origin?: string;
42
44
  expires_in?: string;
@@ -1,7 +1,8 @@
1
1
  import { createLocalJWKSet, exportJWK, generateKeyPair, SignJWT } from "jose";
2
+ import { z } from "zod";
2
3
  import { checkToken, unavailableOAuth } from "./agents.js";
3
- import { COMMENT_BODY_LIMIT, filed_comment } from "./comments.js";
4
- import { MCP_PATH, MINUTE, randomToken } from "./primitives.js";
4
+ import { COMMENT_BODY_LIMIT, filed_comment, login_start, login_verify } from "./contract/index.js";
5
+ import { MINUTE, randomToken } from "./primitives.js";
5
6
  // How Infra treats a code: it works for 15 minutes and five tries, and a
6
7
  // new one for the same address replaces it, link and all
7
8
  const LIFETIME = 15 * MINUTE;
@@ -10,14 +11,14 @@ const ATTEMPTS = 5;
10
11
  * Infra's test mode, in memory: what a test key gets from the real one,
11
12
  * with no key and no network. Starting a login sends nothing and answers
12
13
  * the code and the link, and verifying checks them as Infra does: each
13
- * works once, for 15 minutes, and a code for five tries. No limits apply,
14
- * as none do to a test key. A comment is checked as Infra checks it, and
15
- * answered an ID, though nothing keeps it, where Infra keeps a test key's.
16
- * Its project has the `domains` it's given, an app's website's. An app's
17
- * tests always use it, so they never reach Infra, and so does a dev server
18
- * with no test key (choose.ts). A build never does: anyone could log in
19
- * as anyone, since the page shows the code. Its refusals say what Infra's
20
- * say.
14
+ * works once, for 15 minutes, a code for five tries, and using either
15
+ * spends both. No limits apply, as none do to a test key. A request is
16
+ * checked with Infra's own contract, as Infra checks it, and a comment is
17
+ * answered an ID, though nothing keeps it. Its project has the `domains`
18
+ * it's given, an app's website's. An app's tests always use it, so they
19
+ * never reach Infra, and so does a dev server with no test key
20
+ * (choose.ts). A build never does: anyone could log in as anyone, since
21
+ * the page shows the code. Its refusals say what Infra's say.
21
22
  *
22
23
  * `waiting` is where it keeps what it sent, by address: its own, or one
23
24
  * that outlives a dev server's reloads. With `logs`, as a dev server has
@@ -30,7 +31,7 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
30
31
  ok: true,
31
32
  data: {
32
33
  project: {
33
- id: "sf_project_stand_in",
34
+ id: STAND_IN_PROJECT,
34
35
  name: "Infra's stand-in",
35
36
  domains
36
37
  },
@@ -38,42 +39,56 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
38
39
  }
39
40
  };
40
41
  },
41
- async startLogin({ email, origin }) {
42
+ async startLogin(request) {
43
+ const parsed = login_start.safeParse(request);
44
+ if (!parsed.success) {
45
+ return misfit(parsed.error);
46
+ }
47
+ const { channel, address, link_to } = parsed.data;
42
48
  const sent = {
43
- email,
49
+ address,
44
50
  code: randomCode(),
45
51
  token: randomToken(),
46
52
  attempts: 0,
47
53
  expires_at: Date.now() + LIFETIME
48
54
  };
49
- waiting.set(email, sent);
55
+ waiting.set(address, sent);
50
56
  return {
51
57
  ok: true,
52
58
  data: {
53
- email,
54
- with_link: origin !== null,
59
+ channel,
60
+ address,
61
+ with_link: link_to !== undefined,
55
62
  expires_at: new Date(sent.expires_at),
56
63
  test: {
57
64
  code: sent.code,
58
- link: origin === null
65
+ link: link_to === undefined
59
66
  ? null
60
- : `${origin}/login#${sent.token}`
67
+ : `${link_to}#${sent.token}`
61
68
  }
62
69
  }
63
70
  };
64
71
  },
65
- async verifyLogin(proof) {
72
+ async verifyLogin(given) {
73
+ const parsed = login_verify.safeParse(given);
74
+ if (!parsed.success) {
75
+ return misfit(parsed.error);
76
+ }
77
+ const proof = parsed.data;
66
78
  if ("token" in proof) {
67
79
  const sent = [...waiting.values()].find((each) => each.token === proof.token);
68
80
  if (sent === undefined || sent.expires_at <= Date.now()) {
69
81
  return { ok: false, error: NO_LINK };
70
82
  }
71
83
  else {
72
- waiting.delete(sent.email);
73
- return { ok: true, data: { email: sent.email } };
84
+ waiting.delete(sent.address);
85
+ return {
86
+ ok: true,
87
+ data: { channel: "email", address: sent.address }
88
+ };
74
89
  }
75
90
  }
76
- const sent = waiting.get(proof.email);
91
+ const sent = waiting.get(proof.address);
77
92
  if (sent === undefined ||
78
93
  sent.expires_at <= Date.now() ||
79
94
  sent.attempts >= ATTEMPTS) {
@@ -82,8 +97,11 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
82
97
  // Each guess counts, the right one too, as Infra counts them
83
98
  sent.attempts += 1;
84
99
  if (sent.code === proof.code) {
85
- waiting.delete(sent.email);
86
- return { ok: true, data: { email: sent.email } };
100
+ waiting.delete(sent.address);
101
+ return {
102
+ ok: true,
103
+ data: { channel: proof.channel, address: sent.address }
104
+ };
87
105
  }
88
106
  else if (sent.attempts < ATTEMPTS) {
89
107
  const left = ATTEMPTS - sent.attempts;
@@ -92,7 +110,7 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
92
110
  error: {
93
111
  reason: "wrong-code",
94
112
  attempts_left: left,
95
- message: `That isn't the code Infra emailed this address. It can be tried ${left === 1 ? "once more" : `${left} more times`}.`
113
+ message: `That isn't the code Infra sent this address. It can be tried ${left === 1 ? "once more" : `${left} more times`}.`
96
114
  }
97
115
  };
98
116
  }
@@ -114,14 +132,17 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
114
132
  }
115
133
  const checked = filed_comment.safeParse(comment);
116
134
  if (!checked.success) {
117
- // The fields that failed, as Infra names them
118
- const fields = checked.error.issues.map((issue) => issue.path.join("."));
135
+ // What didn't fit, as Infra says it
136
+ const issues = checked.error.issues.map((issue) => ({
137
+ path: issue.path.map(String).join("."),
138
+ message: issue.message
139
+ }));
119
140
  return {
120
141
  ok: false,
121
142
  error: {
122
143
  reason: "invalid",
123
- fields,
124
- message: `${fields.join(", ")} didn't fit what this call takes.`
144
+ issues,
145
+ message: `${issues.map((issue) => issue.path).join(", ")} didn't fit what this call takes.`
125
146
  }
126
147
  };
127
148
  }
@@ -157,7 +178,7 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
157
178
  return {
158
179
  ok: false,
159
180
  error: {
160
- error: "not-found",
181
+ reason: "not-found",
161
182
  message: "Claude has no connection here with this ID: the stand-in connects no one."
162
183
  }
163
184
  };
@@ -165,16 +186,29 @@ export function standInInfra({ waiting = new Map(), logs = false, domains = [] }
165
186
  async endAgentConnections() {
166
187
  return { ok: true, data: 0 };
167
188
  },
168
- async checkAgentToken(token, origin) {
189
+ async checkAgentToken(token, expected) {
169
190
  const { public_keys } = await standInKeys();
170
191
  return await checkToken(public_keys, token, {
171
- origin,
192
+ ...expected,
172
193
  project: STAND_IN_PROJECT,
173
194
  mode: "test"
174
195
  });
175
196
  }
176
197
  };
177
198
  }
199
+ // A request that doesn't fit Infra's contract: Infra answers it 400
200
+ // `invalid`, which the client reads as unavailable, since it's a bug in
201
+ // the app, with the fields that didn't fit
202
+ function misfit(error) {
203
+ const fields = error.issues.map((issue) => issue.path.map(String).join("."));
204
+ return {
205
+ ok: false,
206
+ error: {
207
+ reason: "unavailable",
208
+ message: `${fields.join(", ")} didn't fit what this call takes: a bug in the app.`
209
+ }
210
+ };
211
+ }
178
212
  // What the stand-in refuses with, in Infra's words
179
213
  const NO_CODE = {
180
214
  reason: "no-code",
@@ -185,7 +219,7 @@ const NO_LINK = {
185
219
  message: `That link has been used, ran out after ${LIFETIME / MINUTE} minutes, or isn't this project's. Start a new login.`
186
220
  };
187
221
  const NO_REQUEST = {
188
- error: "not-found",
222
+ reason: "not-found",
189
223
  message: "No request of Claude's is waiting here: the stand-in logs no one in. Set INFRA_TEST_KEY to connect Claude to a dev server."
190
224
  };
191
225
  // The project the stand-in's tokens are for
@@ -202,11 +236,12 @@ function standInKeys() {
202
236
  return keys;
203
237
  }
204
238
  /**
205
- * A token as Infra signs one, for a test to call the API as Claude does:
206
- * acting as `email`, with `scopes`, for the app at `origin` (the tests'
207
- * own is `http://localhost`), which the stand-in's `checkAgentToken` takes
239
+ * A token as Infra signs one, for a test to call the app as Claude does:
240
+ * acting as `user_id`, with `scopes`, for the app at `origin` (the tests'
241
+ * own is `http://localhost`) and the address `resource`, its MCP's, which
242
+ * the stand-in's `checkAgentToken` takes
208
243
  */
209
- export async function standInAgentToken({ email, scopes = ["read", "act"], origin = "http://localhost", expires_in = "10m" }) {
244
+ export async function standInAgentToken({ user_id, resource, scopes = ["read", "act"], origin = "http://localhost", expires_in = "10m" }) {
210
245
  const { signing } = await standInKeys();
211
246
  return await new SignJWT({
212
247
  scope: scopes.join(" "),
@@ -216,8 +251,8 @@ export async function standInAgentToken({ email, scopes = ["read", "act"], origi
216
251
  })
217
252
  .setProtectedHeader({ alg: "ES256", kid: "stand-in", typ: "at+jwt" })
218
253
  .setIssuer(origin)
219
- .setAudience(`${origin}${MCP_PATH}`)
220
- .setSubject(email)
254
+ .setAudience(z.url().parse(resource))
255
+ .setSubject(user_id)
221
256
  .setIssuedAt()
222
257
  .setExpirationTime(expires_in)
223
258
  .sign(signing);
@@ -1,6 +1,4 @@
1
1
  /** The package's version, as package.json says it */
2
- export declare const PACKAGE_VERSION = "0.1.1";
3
- /** The header every call to Infra carries, its OAuth calls passed through included */
4
- export declare const CLIENT_HEADER = "Infra-Client";
5
- /** What it says: the package and its version, `@superforge/infra/0.1.1` */
6
- export declare const CLIENT = "@superforge/infra/0.1.1";
2
+ export declare const PACKAGE_VERSION = "0.3.0";
3
+ /** What it says: the package and its version, `@superforge/infra/0.3.0` */
4
+ export declare const CLIENT = "@superforge/infra/0.3.0";
package/output/version.js CHANGED
@@ -5,8 +5,6 @@
5
5
  // package away from its package.json; a test checks the two agree, so a
6
6
  // release that bumps one and not the other fails `pnpm check`.
7
7
  /** The package's version, as package.json says it */
8
- export const PACKAGE_VERSION = "0.1.1";
9
- /** The header every call to Infra carries, its OAuth calls passed through included */
10
- export const CLIENT_HEADER = "Infra-Client";
11
- /** What it says: the package and its version, `@superforge/infra/0.1.1` */
8
+ export const PACKAGE_VERSION = "0.3.0";
9
+ /** What it says: the package and its version, `@superforge/infra/0.3.0` */
12
10
  export const CLIENT = `@superforge/infra/${PACKAGE_VERSION}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superforge/infra",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "Superforge Infra, the backend services Foundation and Skylevel apps run on, as an app's server calls it",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,6 +16,10 @@
16
16
  ".": {
17
17
  "types": "./output/index.d.ts",
18
18
  "default": "./output/index.js"
19
+ },
20
+ "./contract": {
21
+ "types": "./output/contract/index.d.ts",
22
+ "default": "./output/contract/index.js"
19
23
  }
20
24
  },
21
25
  "files": [