@mycadet/cli 0.13.0-dev.202610221419.8e700c5 → 0.13.0-dev.202610231403.3079a3f
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/node_modules/@mycadet/assistant-api/events/conversation-error.ts +1 -1
- package/node_modules/@mycadet/assistant-api/node_modules/@mycadet/service-contracts/src/cloud-credential.ts +5 -5
- package/node_modules/@mycadet/assistant-api/node_modules/@mycadet/service-contracts/src/remote-web-pairing.ts +6 -6
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/service-contracts/src/cloud-credential.ts +5 -5
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/service-contracts/src/remote-web-pairing.ts +6 -6
- package/node_modules/@mycadet/local-mode/src/__tests__/pair.test.ts +2 -1
- package/node_modules/@mycadet/local-mode/src/gateway-bearer.test.ts +157 -0
- package/node_modules/@mycadet/local-mode/src/gateway-bearer.ts +133 -0
- package/node_modules/@mycadet/local-mode/src/index.ts +5 -0
- package/node_modules/@mycadet/local-mode/src/lockfile-contract.test.ts +0 -1
- package/node_modules/@mycadet/local-mode/src/lockfile-contract.ts +0 -9
- package/node_modules/@mycadet/local-mode/src/lockfile.test.ts +0 -1
- package/node_modules/@mycadet/local-mode/src/pair.ts +3 -5
- package/node_modules/@mycadet/local-mode/src/status.ts +0 -1
- package/node_modules/@mycadet/service-contracts/src/cloud-credential.ts +5 -5
- package/node_modules/@mycadet/service-contracts/src/remote-web-pairing.ts +6 -6
- package/package.json +1 -1
- package/src/__tests__/assistant-client-cloud-auth.test.ts +2 -19
- package/src/__tests__/assistant-config.test.ts +0 -12
- package/src/__tests__/devices.test.ts +15 -9
- package/src/__tests__/gateway-token.test.ts +0 -2
- package/src/__tests__/ingress-config.test.ts +0 -48
- package/src/__tests__/ngrok-spawn.test.ts +0 -86
- package/src/__tests__/pair.test.ts +56 -466
- package/src/__tests__/retire.test.ts +1 -4
- package/src/__tests__/setup.test.ts +23 -4
- package/src/__tests__/sleep.test.ts +9 -2
- package/src/__tests__/upgrade-local.test.ts +9 -6
- package/src/__tests__/wake.test.ts +9 -5
- package/src/commands/app.ts +1 -1
- package/src/commands/client.ts +16 -15
- package/src/commands/debug.ts +2 -2
- package/src/commands/devices.ts +9 -10
- package/src/commands/gateway/token.ts +2 -1
- package/src/commands/logs.ts +2 -2
- package/src/commands/pair.ts +28 -103
- package/src/commands/ps.ts +11 -11
- package/src/commands/retire.ts +5 -9
- package/src/commands/setup.ts +3 -2
- package/src/commands/sleep.ts +2 -1
- package/src/commands/terminal.ts +2 -2
- package/src/commands/tunnel.ts +3 -58
- package/src/commands/upgrade.ts +1 -5
- package/src/commands/use.ts +3 -3
- package/src/index.ts +2 -2
- package/src/lib/assistant-client.ts +6 -30
- package/src/lib/assistant-config.ts +9 -14
- package/src/lib/cloud-client.ts +4 -4
- package/src/lib/hatch-local.ts +0 -3
- package/src/lib/ingress-config.ts +0 -33
- package/src/lib/lan-ip.ts +50 -0
- package/src/lib/live-voice/connection.ts +6 -5
- package/src/lib/local.ts +1 -50
- package/src/lib/nginx-ingress.ts +3 -3
- package/src/lib/runtime-url.ts +20 -18
- package/src/lib/tunnel.ts +29 -30
- package/src/lib/tunnels/gateway-notify.ts +3 -3
- package/src/lib/tunnels/ngrok.ts +13 -73
|
@@ -36,7 +36,7 @@ export const ConversationErrorCodeSchema = z.enum([
|
|
|
36
36
|
"PROVIDER_WEB_SEARCH",
|
|
37
37
|
"PROVIDER_NOT_CONFIGURED",
|
|
38
38
|
"PROVIDER_INVALID_KEY",
|
|
39
|
-
"
|
|
39
|
+
"CLOUD_KEY_INVALID",
|
|
40
40
|
"CONTEXT_TOO_LARGE",
|
|
41
41
|
"BUDGET_YIELD_UNRECOVERED",
|
|
42
42
|
"MAX_TOKENS_REACHED",
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* credential still authenticates.
|
|
4
4
|
*
|
|
5
5
|
* The daemon route is the authoritative serving side:
|
|
6
|
-
* - `POST /v1/
|
|
6
|
+
* - `POST /v1/cloud/verify-credential`
|
|
7
7
|
* (`assistant/src/runtime/routes/cloud-routes.ts`)
|
|
8
8
|
*
|
|
9
9
|
* The daemon's platform client produces the verdict, the route reports it,
|
|
@@ -32,10 +32,10 @@ export type CloudCredentialVerificationStatus = z.infer<
|
|
|
32
32
|
typeof CloudCredentialVerificationStatusSchema
|
|
33
33
|
>;
|
|
34
34
|
|
|
35
|
-
/** `POST /v1/
|
|
36
|
-
export const
|
|
35
|
+
/** `POST /v1/cloud/verify-credential` response body. */
|
|
36
|
+
export const CloudVerifyCredentialResponseSchema = z.object({
|
|
37
37
|
status: CloudCredentialVerificationStatusSchema,
|
|
38
38
|
});
|
|
39
|
-
export type
|
|
40
|
-
typeof
|
|
39
|
+
export type CloudVerifyCredentialResponse = z.infer<
|
|
40
|
+
typeof CloudVerifyCredentialResponseSchema
|
|
41
41
|
>;
|
|
@@ -37,12 +37,6 @@
|
|
|
37
37
|
*/
|
|
38
38
|
export const REMOTE_WEB_PAIRING_CODE_TTL_MS = 10 * 60 * 1000;
|
|
39
39
|
|
|
40
|
-
/** `POST /v1/remote-web/pairing-challenge` request body. */
|
|
41
|
-
export interface RemoteWebPairingChallengeRequest {
|
|
42
|
-
/** Public https base URL the scanning device can reach the assistant at. */
|
|
43
|
-
publicBaseUrl: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
40
|
/** `POST /v1/remote-web/pairing-challenge` success response body (200). */
|
|
47
41
|
export interface RemoteWebPairingChallengeResponse {
|
|
48
42
|
/** Opaque secret the paired device exchanges for a token. */
|
|
@@ -51,6 +45,12 @@ export interface RemoteWebPairingChallengeResponse {
|
|
|
51
45
|
userCode: string;
|
|
52
46
|
/** URL the device opens to complete pairing. */
|
|
53
47
|
verificationUri: string;
|
|
48
|
+
/**
|
|
49
|
+
* The base URL the challenge was minted for (echoed into verificationUri).
|
|
50
|
+
* The gateway self-sources it — the request's own host through the trusted
|
|
51
|
+
* edge, else its recorded ingress URL; callers cannot request a URL.
|
|
52
|
+
*/
|
|
53
|
+
publicBaseUrl: string;
|
|
54
54
|
/** ISO-8601 instant the challenge expires. */
|
|
55
55
|
expiresAt: string;
|
|
56
56
|
/** Seconds until the challenge expires. */
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* credential still authenticates.
|
|
4
4
|
*
|
|
5
5
|
* The daemon route is the authoritative serving side:
|
|
6
|
-
* - `POST /v1/
|
|
6
|
+
* - `POST /v1/cloud/verify-credential`
|
|
7
7
|
* (`assistant/src/runtime/routes/cloud-routes.ts`)
|
|
8
8
|
*
|
|
9
9
|
* The daemon's platform client produces the verdict, the route reports it,
|
|
@@ -32,10 +32,10 @@ export type CloudCredentialVerificationStatus = z.infer<
|
|
|
32
32
|
typeof CloudCredentialVerificationStatusSchema
|
|
33
33
|
>;
|
|
34
34
|
|
|
35
|
-
/** `POST /v1/
|
|
36
|
-
export const
|
|
35
|
+
/** `POST /v1/cloud/verify-credential` response body. */
|
|
36
|
+
export const CloudVerifyCredentialResponseSchema = z.object({
|
|
37
37
|
status: CloudCredentialVerificationStatusSchema,
|
|
38
38
|
});
|
|
39
|
-
export type
|
|
40
|
-
typeof
|
|
39
|
+
export type CloudVerifyCredentialResponse = z.infer<
|
|
40
|
+
typeof CloudVerifyCredentialResponseSchema
|
|
41
41
|
>;
|
|
@@ -37,12 +37,6 @@
|
|
|
37
37
|
*/
|
|
38
38
|
export const REMOTE_WEB_PAIRING_CODE_TTL_MS = 10 * 60 * 1000;
|
|
39
39
|
|
|
40
|
-
/** `POST /v1/remote-web/pairing-challenge` request body. */
|
|
41
|
-
export interface RemoteWebPairingChallengeRequest {
|
|
42
|
-
/** Public https base URL the scanning device can reach the assistant at. */
|
|
43
|
-
publicBaseUrl: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
40
|
/** `POST /v1/remote-web/pairing-challenge` success response body (200). */
|
|
47
41
|
export interface RemoteWebPairingChallengeResponse {
|
|
48
42
|
/** Opaque secret the paired device exchanges for a token. */
|
|
@@ -51,6 +45,12 @@ export interface RemoteWebPairingChallengeResponse {
|
|
|
51
45
|
userCode: string;
|
|
52
46
|
/** URL the device opens to complete pairing. */
|
|
53
47
|
verificationUri: string;
|
|
48
|
+
/**
|
|
49
|
+
* The base URL the challenge was minted for (echoed into verificationUri).
|
|
50
|
+
* The gateway self-sources it — the request's own host through the trusted
|
|
51
|
+
* edge, else its recorded ingress URL; callers cannot request a URL.
|
|
52
|
+
*/
|
|
53
|
+
publicBaseUrl: string;
|
|
54
54
|
/** ISO-8601 instant the challenge expires. */
|
|
55
55
|
expiresAt: string;
|
|
56
56
|
/** Seconds until the challenge expires. */
|
|
@@ -149,6 +149,7 @@ const challengeBody = (
|
|
|
149
149
|
): Record<string, unknown> => ({
|
|
150
150
|
deviceCode: "device-code-abc",
|
|
151
151
|
userCode: "ABCD-EFGH",
|
|
152
|
+
publicBaseUrl: "https://gw.example.com",
|
|
152
153
|
verificationUri: "https://gw.example.com/assistant/pair",
|
|
153
154
|
expiresAt: new Date(Date.now() + 600_000).toISOString(),
|
|
154
155
|
expiresInSeconds: 600,
|
|
@@ -720,7 +721,7 @@ describe("pairingStart", () => {
|
|
|
720
721
|
expect(fetchCalls).toEqual([
|
|
721
722
|
{
|
|
722
723
|
url: "https://gw.example.com/v1/remote-web/pairing-challenge",
|
|
723
|
-
body: {
|
|
724
|
+
body: {},
|
|
724
725
|
redirect: "error",
|
|
725
726
|
signal: expect.any(AbortSignal) as unknown as AbortSignal,
|
|
726
727
|
},
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
clearLoopbackGatewayBearerCache,
|
|
5
|
+
loopbackGatewayBearer,
|
|
6
|
+
} from "./gateway-bearer";
|
|
7
|
+
import type { TokenResult } from "./guardian-token";
|
|
8
|
+
|
|
9
|
+
const realFetch = globalThis.fetch;
|
|
10
|
+
|
|
11
|
+
let leaseResult: TokenResult = { ok: true, accessToken: "guardian-tok" };
|
|
12
|
+
let mintResponses: Response[] = [];
|
|
13
|
+
let mintCalls: Array<{ url: string; init?: RequestInit }> = [];
|
|
14
|
+
|
|
15
|
+
const lease = mock(async (): Promise<TokenResult> => leaseResult);
|
|
16
|
+
|
|
17
|
+
// The mint fetch is the I/O seam: recorded and answered per test.
|
|
18
|
+
globalThis.fetch = (async (
|
|
19
|
+
input: RequestInfo | URL,
|
|
20
|
+
init?: RequestInit,
|
|
21
|
+
): Promise<Response> => {
|
|
22
|
+
mintCalls.push({ url: String(input), init });
|
|
23
|
+
const response = mintResponses.shift();
|
|
24
|
+
if (!response) {
|
|
25
|
+
throw new TypeError("no scripted mint response");
|
|
26
|
+
}
|
|
27
|
+
return response;
|
|
28
|
+
}) as unknown as typeof fetch;
|
|
29
|
+
|
|
30
|
+
const mintOk = (token = "minted-bearer", expiresAt?: number) =>
|
|
31
|
+
new Response(
|
|
32
|
+
JSON.stringify({
|
|
33
|
+
token,
|
|
34
|
+
expiresAt: expiresAt ?? Math.floor(Date.now() / 1000) + 3600,
|
|
35
|
+
}),
|
|
36
|
+
{ headers: { "content-type": "application/json" } },
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
clearLoopbackGatewayBearerCache();
|
|
41
|
+
lease.mockClear();
|
|
42
|
+
leaseResult = { ok: true, accessToken: "guardian-tok" };
|
|
43
|
+
mintCalls = [];
|
|
44
|
+
mintResponses = [mintOk()];
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
clearLoopbackGatewayBearerCache();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("loopbackGatewayBearer", () => {
|
|
52
|
+
test("mints against the loopback gateway with the guardian lease", async () => {
|
|
53
|
+
const bearer = await loopbackGatewayBearer(8080, lease);
|
|
54
|
+
|
|
55
|
+
expect(bearer).toBe("minted-bearer");
|
|
56
|
+
expect(mintCalls).toHaveLength(1);
|
|
57
|
+
expect(mintCalls[0].url).toBe("http://127.0.0.1:8080/auth/token");
|
|
58
|
+
expect(mintCalls[0].init?.method).toBe("POST");
|
|
59
|
+
const headers = new Headers(mintCalls[0].init?.headers);
|
|
60
|
+
// The mint route is Origin-gated (a CSRF defense for browsers); the
|
|
61
|
+
// server-to-server hop presents the loopback web origin it dials.
|
|
62
|
+
expect(headers.get("origin")).toBe("http://127.0.0.1:8080");
|
|
63
|
+
expect(headers.get("authorization")).toBe("Bearer guardian-tok");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("caches the mint per port so a burst shares one lease", async () => {
|
|
67
|
+
await loopbackGatewayBearer(8080, lease);
|
|
68
|
+
const again = await loopbackGatewayBearer(8080, lease);
|
|
69
|
+
|
|
70
|
+
expect(again).toBe("minted-bearer");
|
|
71
|
+
expect(mintCalls).toHaveLength(1);
|
|
72
|
+
expect(lease).toHaveBeenCalledTimes(1);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("ports cache independently", async () => {
|
|
76
|
+
mintResponses = [mintOk("bearer-8080"), mintOk("bearer-8081")];
|
|
77
|
+
await loopbackGatewayBearer(8080, lease);
|
|
78
|
+
const other = await loopbackGatewayBearer(8081, lease);
|
|
79
|
+
|
|
80
|
+
expect(other).toBe("bearer-8081");
|
|
81
|
+
expect(mintCalls).toHaveLength(2);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a bearer inside the freshness margin is reused; an expired one re-mints", async () => {
|
|
85
|
+
// Expired an hour ago: the first call must mint.
|
|
86
|
+
mintResponses = [
|
|
87
|
+
mintOk("stale-bearer", Math.floor(Date.now() / 1000) - 3600),
|
|
88
|
+
];
|
|
89
|
+
const stale = await loopbackGatewayBearer(8080, lease);
|
|
90
|
+
expect(stale).toBe("stale-bearer");
|
|
91
|
+
|
|
92
|
+
// Within the margin (expires in 30s): the cached mint is reused.
|
|
93
|
+
mintResponses = [
|
|
94
|
+
mintOk("fresh-bearer", Math.floor(Date.now() / 1000) + 30),
|
|
95
|
+
];
|
|
96
|
+
const fresh = await loopbackGatewayBearer(8080, lease, { forceMint: true });
|
|
97
|
+
expect(fresh).toBe("fresh-bearer");
|
|
98
|
+
|
|
99
|
+
// The margin itself (60s) keeps a 90s-lived bearer cacheable.
|
|
100
|
+
mintResponses = [
|
|
101
|
+
mintOk("margin-bearer", Math.floor(Date.now() / 1000) + 90),
|
|
102
|
+
];
|
|
103
|
+
await loopbackGatewayBearer(8080, lease, { forceMint: true });
|
|
104
|
+
const cached = await loopbackGatewayBearer(8080, lease);
|
|
105
|
+
expect(cached).toBe("margin-bearer");
|
|
106
|
+
expect(mintCalls).toHaveLength(3);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("forceMint skips the cache and replaces it", async () => {
|
|
110
|
+
await loopbackGatewayBearer(8080, lease);
|
|
111
|
+
mintResponses = [mintOk("rotated-bearer")];
|
|
112
|
+
|
|
113
|
+
const rotated = await loopbackGatewayBearer(8080, lease, {
|
|
114
|
+
forceMint: true,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
expect(rotated).toBe("rotated-bearer");
|
|
118
|
+
expect(mintCalls).toHaveLength(2);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("a failed guardian lease yields no bearer", async () => {
|
|
122
|
+
leaseResult = { ok: false, status: 404, error: "Guardian token not found" };
|
|
123
|
+
|
|
124
|
+
const bearer = await loopbackGatewayBearer(8080, lease);
|
|
125
|
+
|
|
126
|
+
expect(bearer).toBeNull();
|
|
127
|
+
expect(mintCalls).toHaveLength(0);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("a rejected mint yields no bearer", async () => {
|
|
131
|
+
mintResponses = [new Response("Unauthorized", { status: 401 })];
|
|
132
|
+
|
|
133
|
+
const bearer = await loopbackGatewayBearer(8080, lease);
|
|
134
|
+
|
|
135
|
+
expect(bearer).toBeNull();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("a malformed mint body yields no bearer", async () => {
|
|
139
|
+
mintResponses = [
|
|
140
|
+
new Response(JSON.stringify({ token: 5 }), { status: 200 }),
|
|
141
|
+
];
|
|
142
|
+
|
|
143
|
+
const bearer = await loopbackGatewayBearer(8080, lease);
|
|
144
|
+
|
|
145
|
+
expect(bearer).toBeNull();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("a thrown lease yields no bearer", async () => {
|
|
149
|
+
const throwingLease = mock(async (): Promise<TokenResult> => {
|
|
150
|
+
throw new Error("host down");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const bearer = await loopbackGatewayBearer(8080, throwingLease);
|
|
154
|
+
|
|
155
|
+
expect(bearer).toBeNull();
|
|
156
|
+
});
|
|
157
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { TokenResult } from "./guardian-token";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Host-side custody of the local gateway bearer.
|
|
5
|
+
*
|
|
6
|
+
* The desktop main process and the dev-server middleware are the trusted
|
|
7
|
+
* hosts of the local data plane: each can read the on-disk guardian token
|
|
8
|
+
* (via {@link GuardianTokenLease}) and mint a gateway actor bearer against
|
|
9
|
+
* the loopback gateway's `/auth/token`. Minting here keeps both the
|
|
10
|
+
* guardian token and the minted bearer out of the renderer — the browser
|
|
11
|
+
* never sees either credential, so nothing token-shaped lands in
|
|
12
|
+
* localStorage.
|
|
13
|
+
*
|
|
14
|
+
* The minted bearer is cached per gateway port with the same 60-second
|
|
15
|
+
* expiry margin the client previously applied to its own mint, so a burst
|
|
16
|
+
* of forwarded requests shares one mint instead of paying the guardian
|
|
17
|
+
* lease per request.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** The loopback gateway's `/auth/token` mint response. */
|
|
21
|
+
interface MintedGatewayBearer {
|
|
22
|
+
token: string;
|
|
23
|
+
expiresAtEpochSeconds: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Lease the guardian access token from the trusted host's disk/CLI
|
|
28
|
+
* machinery. Mirrors the never-reject contract of the host credential
|
|
29
|
+
* routes: a failure resolves with the structured result rather than
|
|
30
|
+
* throwing.
|
|
31
|
+
*/
|
|
32
|
+
export type GuardianTokenLease = () => Promise<TokenResult>;
|
|
33
|
+
|
|
34
|
+
const FRESHNESS_MARGIN_SECONDS = 60;
|
|
35
|
+
|
|
36
|
+
const bearerCache = new Map<number, MintedGatewayBearer>();
|
|
37
|
+
|
|
38
|
+
function isFresh(bearer: MintedGatewayBearer): boolean {
|
|
39
|
+
return (
|
|
40
|
+
Date.now() / 1000 < bearer.expiresAtEpochSeconds - FRESHNESS_MARGIN_SECONDS
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @internal Exposed for test teardown only: the cache is module state. */
|
|
45
|
+
export function clearLoopbackGatewayBearerCache(): void {
|
|
46
|
+
bearerCache.clear();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function mintBearer(
|
|
50
|
+
port: number,
|
|
51
|
+
leaseGuardianToken: GuardianTokenLease,
|
|
52
|
+
fetcher: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>,
|
|
53
|
+
): Promise<MintedGatewayBearer | null> {
|
|
54
|
+
let guardianToken: string;
|
|
55
|
+
try {
|
|
56
|
+
const lease = await leaseGuardianToken();
|
|
57
|
+
if (!lease.ok) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
guardianToken = lease.accessToken;
|
|
61
|
+
} catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
// The mint route is loopback-peer-gated and Origin-gated (a CSRF
|
|
66
|
+
// defense for browsers); this server-to-server hop presents the same
|
|
67
|
+
// loopback web origin the desktop forward's rewrite already produced.
|
|
68
|
+
const response = await fetcher(`http://127.0.0.1:${port}/auth/token`, {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: {
|
|
71
|
+
origin: `http://127.0.0.1:${port}`,
|
|
72
|
+
authorization: `Bearer ${guardianToken}`,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const body = (await response.json().catch(() => null)) as {
|
|
79
|
+
token?: unknown;
|
|
80
|
+
expiresAt?: unknown;
|
|
81
|
+
} | null;
|
|
82
|
+
if (
|
|
83
|
+
typeof body?.token !== "string" ||
|
|
84
|
+
typeof body.expiresAt !== "number" ||
|
|
85
|
+
!Number.isFinite(body.expiresAt)
|
|
86
|
+
) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
return { token: body.token, expiresAtEpochSeconds: body.expiresAt };
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface LoopbackGatewayBearerOptions {
|
|
96
|
+
/** Skip the cache and mint fresh — the 401 re-mint path. */
|
|
97
|
+
forceMint?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Injectable for tests; defaults to the global fetch. A plain callable —
|
|
100
|
+
* the Electron forward threads its own recorded fetcher through.
|
|
101
|
+
*/
|
|
102
|
+
fetcher?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* A bearer for forwarded requests to the loopback gateway on `port`, minted
|
|
107
|
+
* host-side and cached until near expiry. `null` when the guardian lease or
|
|
108
|
+
* the mint fails — callers forward without a bearer and the gateway's own
|
|
109
|
+
* response (401 on protected routes, an unauthenticated `readyz`) surfaces
|
|
110
|
+
* the truth.
|
|
111
|
+
*/
|
|
112
|
+
export async function loopbackGatewayBearer(
|
|
113
|
+
port: number,
|
|
114
|
+
leaseGuardianToken: GuardianTokenLease,
|
|
115
|
+
options: LoopbackGatewayBearerOptions = {},
|
|
116
|
+
): Promise<string | null> {
|
|
117
|
+
if (!options.forceMint) {
|
|
118
|
+
const cached = bearerCache.get(port);
|
|
119
|
+
if (cached && isFresh(cached)) {
|
|
120
|
+
return cached.token;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const minted = await mintBearer(
|
|
124
|
+
port,
|
|
125
|
+
leaseGuardianToken,
|
|
126
|
+
options.fetcher ?? fetch,
|
|
127
|
+
);
|
|
128
|
+
if (!minted) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
bearerCache.set(port, minted);
|
|
132
|
+
return minted.token;
|
|
133
|
+
}
|
|
@@ -131,6 +131,11 @@ export {
|
|
|
131
131
|
sanitizePairedForwardHeaders,
|
|
132
132
|
FLATTENED_FIRST_SEGMENTS,
|
|
133
133
|
} from "./gateway-proxy";
|
|
134
|
+
export {
|
|
135
|
+
loopbackGatewayBearer,
|
|
136
|
+
clearLoopbackGatewayBearerCache,
|
|
137
|
+
} from "./gateway-bearer";
|
|
138
|
+
export type { GuardianTokenLease } from "./gateway-bearer";
|
|
134
139
|
export type {
|
|
135
140
|
GatewayTarget,
|
|
136
141
|
GatewayParseResult,
|
|
@@ -133,10 +133,6 @@ export const FLATTENED_FIRST_SEGMENTS = new Set<string>([
|
|
|
133
133
|
"contact-channels",
|
|
134
134
|
]);
|
|
135
135
|
|
|
136
|
-
export function gatewayProxyUrl(assistantId: string): string {
|
|
137
|
-
return `/assistant/__gateway/${encodeURIComponent(assistantId)}`;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
136
|
/**
|
|
141
137
|
* User-facing hosting label for a paired entry: "Paired · <hostname>" when
|
|
142
138
|
* `runtimeUrl` names the remote host, plain "Paired" otherwise. Shared by the
|
|
@@ -263,11 +259,6 @@ export const LockfileAssistantSchema = z.object({
|
|
|
263
259
|
/** Platform organization UUID used for a self-hosted local assistant registration. */
|
|
264
260
|
cloudOrganizationId: z.string().optional(),
|
|
265
261
|
resources: LocalAssistantResourcesSchema.optional(),
|
|
266
|
-
/**
|
|
267
|
-
* Loopback URL for same-machine health checks (e.g. `http://127.0.0.1:7831`).
|
|
268
|
-
* Avoids mDNS resolution issues when the machine checks its own gateway.
|
|
269
|
-
*/
|
|
270
|
-
localUrl: z.string().optional(),
|
|
271
262
|
/** Helper token for cloud assistants' API bearer; host-only secret. */
|
|
272
263
|
bearerToken: z.string().optional(),
|
|
273
264
|
/** PID of the file watcher process for docker instances hatched with --watch. */
|
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
tunnelProviderWebsiteName,
|
|
10
10
|
type PairingFailureReason,
|
|
11
11
|
type PublicBaseUrlRejection,
|
|
12
|
-
type RemoteWebPairingChallengeRequest,
|
|
13
12
|
type RemoteWebPairingChallengeResponse,
|
|
14
13
|
type RemoteWebPairingTokenPendingResponse,
|
|
15
14
|
type RemoteWebPairingTokenRequest,
|
|
@@ -907,9 +906,7 @@ export async function pairingStart(
|
|
|
907
906
|
const posted = await postPairingRequest(
|
|
908
907
|
parsed.publicBaseUrl,
|
|
909
908
|
PAIRING_CHALLENGE_PATH,
|
|
910
|
-
{
|
|
911
|
-
publicBaseUrl: parsed.publicBaseUrl,
|
|
912
|
-
} satisfies RemoteWebPairingChallengeRequest,
|
|
909
|
+
{},
|
|
913
910
|
);
|
|
914
911
|
if (!posted.ok) {
|
|
915
912
|
return posted.failure;
|
|
@@ -926,7 +923,8 @@ export async function pairingStart(
|
|
|
926
923
|
const challenge = posted.body;
|
|
927
924
|
return openPairingSession(
|
|
928
925
|
{
|
|
929
|
-
|
|
926
|
+
// The gateway self-sourced the URL; its echo is what it minted for.
|
|
927
|
+
publicBaseUrl: challenge.publicBaseUrl,
|
|
930
928
|
deviceCode: challenge.deviceCode,
|
|
931
929
|
deviceId,
|
|
932
930
|
// A challenge expires somewhere inside the code's own TTL, so that TTL
|
|
@@ -321,7 +321,6 @@ function resolveStatusResources(
|
|
|
321
321
|
ports.daemon,
|
|
322
322
|
gatewayPort:
|
|
323
323
|
firstNumber(entry.resources?.gatewayPort, rawResources?.gatewayPort) ??
|
|
324
|
-
parsePortFromUrl(rawEntry?.localUrl) ??
|
|
325
324
|
parsePortFromUrl(rawEntry?.runtimeUrl ?? entry.runtimeUrl) ??
|
|
326
325
|
ports.gateway,
|
|
327
326
|
};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* credential still authenticates.
|
|
4
4
|
*
|
|
5
5
|
* The daemon route is the authoritative serving side:
|
|
6
|
-
* - `POST /v1/
|
|
6
|
+
* - `POST /v1/cloud/verify-credential`
|
|
7
7
|
* (`assistant/src/runtime/routes/cloud-routes.ts`)
|
|
8
8
|
*
|
|
9
9
|
* The daemon's platform client produces the verdict, the route reports it,
|
|
@@ -32,10 +32,10 @@ export type CloudCredentialVerificationStatus = z.infer<
|
|
|
32
32
|
typeof CloudCredentialVerificationStatusSchema
|
|
33
33
|
>;
|
|
34
34
|
|
|
35
|
-
/** `POST /v1/
|
|
36
|
-
export const
|
|
35
|
+
/** `POST /v1/cloud/verify-credential` response body. */
|
|
36
|
+
export const CloudVerifyCredentialResponseSchema = z.object({
|
|
37
37
|
status: CloudCredentialVerificationStatusSchema,
|
|
38
38
|
});
|
|
39
|
-
export type
|
|
40
|
-
typeof
|
|
39
|
+
export type CloudVerifyCredentialResponse = z.infer<
|
|
40
|
+
typeof CloudVerifyCredentialResponseSchema
|
|
41
41
|
>;
|
|
@@ -37,12 +37,6 @@
|
|
|
37
37
|
*/
|
|
38
38
|
export const REMOTE_WEB_PAIRING_CODE_TTL_MS = 10 * 60 * 1000;
|
|
39
39
|
|
|
40
|
-
/** `POST /v1/remote-web/pairing-challenge` request body. */
|
|
41
|
-
export interface RemoteWebPairingChallengeRequest {
|
|
42
|
-
/** Public https base URL the scanning device can reach the assistant at. */
|
|
43
|
-
publicBaseUrl: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
40
|
/** `POST /v1/remote-web/pairing-challenge` success response body (200). */
|
|
47
41
|
export interface RemoteWebPairingChallengeResponse {
|
|
48
42
|
/** Opaque secret the paired device exchanges for a token. */
|
|
@@ -51,6 +45,12 @@ export interface RemoteWebPairingChallengeResponse {
|
|
|
51
45
|
userCode: string;
|
|
52
46
|
/** URL the device opens to complete pairing. */
|
|
53
47
|
verificationUri: string;
|
|
48
|
+
/**
|
|
49
|
+
* The base URL the challenge was minted for (echoed into verificationUri).
|
|
50
|
+
* The gateway self-sources it — the request's own host through the trusted
|
|
51
|
+
* edge, else its recorded ingress URL; callers cannot request a URL.
|
|
52
|
+
*/
|
|
53
|
+
publicBaseUrl: string;
|
|
54
54
|
/** ISO-8601 instant the challenge expires. */
|
|
55
55
|
expiresAt: string;
|
|
56
56
|
/** Seconds until the challenge expires. */
|
package/package.json
CHANGED
|
@@ -166,30 +166,13 @@ describe("AssistantClient cloud-managed auth", () => {
|
|
|
166
166
|
expect(calls).toHaveLength(0);
|
|
167
167
|
});
|
|
168
168
|
|
|
169
|
-
test("
|
|
170
|
-
seedCloud("sess-tok-4");
|
|
171
|
-
const calls = stubFetch(() => new Response("", { status: 200 }));
|
|
172
|
-
|
|
173
|
-
expect(
|
|
174
|
-
() =>
|
|
175
|
-
new AssistantClient({
|
|
176
|
-
assistantId: ASSISTANT_ID,
|
|
177
|
-
runtimeUrl: "https://attacker.example.com",
|
|
178
|
-
}),
|
|
179
|
-
).toThrow("Refusing to send cloud credentials");
|
|
180
|
-
expect(calls).toHaveLength(0);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
test("allows a runtimeUrl override that matches the entry's platform host", async () => {
|
|
169
|
+
test("dials the entry's recorded cloud host", async () => {
|
|
184
170
|
seedCloud("sess-tok-5");
|
|
185
171
|
stubFetch((url) =>
|
|
186
172
|
isOrgFetch(url) ? orgResponse() : new Response("", { status: 200 }),
|
|
187
173
|
);
|
|
188
174
|
|
|
189
|
-
const client = new AssistantClient({
|
|
190
|
-
assistantId: ASSISTANT_ID,
|
|
191
|
-
runtimeUrl: `${PLATFORM_URL}/`, // trailing slash must not count as a mismatch
|
|
192
|
-
});
|
|
175
|
+
const client = new AssistantClient({ assistantId: ASSISTANT_ID });
|
|
193
176
|
|
|
194
177
|
expect(client.runtimeUrl).toBe(PLATFORM_URL);
|
|
195
178
|
expect((await client.get("/healthz/")).status).toBe(200);
|
|
@@ -27,7 +27,6 @@ import {
|
|
|
27
27
|
loadLatestAssistant,
|
|
28
28
|
findAssistantByName,
|
|
29
29
|
formatAssistantLookupError,
|
|
30
|
-
formatAssistantReference,
|
|
31
30
|
getAssistantDisplayName,
|
|
32
31
|
lookupAssistantByIdentifier,
|
|
33
32
|
removeAssistantEntry,
|
|
@@ -217,17 +216,6 @@ describe("assistant-config", () => {
|
|
|
217
216
|
);
|
|
218
217
|
});
|
|
219
218
|
|
|
220
|
-
test("formatAssistantReference includes distinct display name and id", () => {
|
|
221
|
-
expect(
|
|
222
|
-
formatAssistantReference(
|
|
223
|
-
makeEntry("assistant-1", undefined, { name: "Alice" }),
|
|
224
|
-
),
|
|
225
|
-
).toBe("Alice (assistant-1)");
|
|
226
|
-
expect(formatAssistantReference(makeEntry("assistant-2"))).toBe(
|
|
227
|
-
"assistant-2",
|
|
228
|
-
);
|
|
229
|
-
});
|
|
230
|
-
|
|
231
219
|
test("findAssistantByName returns null for non-existent name", () => {
|
|
232
220
|
writeLockfile({ assistants: [makeEntry("alpha")] });
|
|
233
221
|
expect(findAssistantByName("missing")).toBeNull();
|