@mycadet/gateway 0.13.0-dev.202611142107.f5e00a2 → 0.13.0-dev.202611151727.8310b2c

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/knip.json CHANGED
@@ -1,12 +1,6 @@
1
1
  {
2
- "entry": [
3
- "src/**/*.test.ts",
4
- "src/**/__tests__/**/*.ts",
5
- "src/cli/index.ts"
6
- ],
7
- "project": [
8
- "src/**/*.ts"
9
- ],
2
+ "entry": ["src/**/*.test.ts", "src/**/__tests__/**/*.ts", "src/cli/index.ts"],
3
+ "project": ["src/**/*.ts"],
10
4
  "ignoreDependencies": [
11
5
  "@mycadet/assistant-client",
12
6
  "@mycadet/ces-client",
@@ -1,3 +1,5 @@
1
+ import { z } from "zod";
2
+
1
3
  /**
2
4
  * Wire contracts for the remote-web pairing flow — the RFC 8628-style
3
5
  * device-code exchange that connects a browser (or the iOS app) to a
@@ -59,6 +61,16 @@ export interface RemoteWebPairingChallengeResponse {
59
61
  intervalSeconds: number;
60
62
  }
61
63
 
64
+ export const RemoteWebPairingChallengeResponseSchema = z.object({
65
+ deviceCode: z.string(),
66
+ userCode: z.string(),
67
+ verificationUri: z.string(),
68
+ publicBaseUrl: z.string(),
69
+ expiresAt: z.string(),
70
+ expiresInSeconds: z.number(),
71
+ intervalSeconds: z.number(),
72
+ }) satisfies z.ZodType<RemoteWebPairingChallengeResponse>;
73
+
62
74
  /** `POST /v1/remote-web/pairing-verification` request body. */
63
75
  export interface RemoteWebPairingVerificationRequest {
64
76
  /** The `userCode` from the challenge, entered/scanned on the host. */
@@ -47,6 +47,5 @@ export {
47
47
 
48
48
  export {
49
49
  CES_SOCKET_DIR_DEFAULT,
50
- getCesSocketDir,
51
50
  getCesSocketPath,
52
51
  } from "./socket-path.js";
@@ -18,7 +18,7 @@ export const CES_SOCKET_DIR_DEFAULT = "/run/ces-ipc";
18
18
  * The directory holding `ces.sock`: `CES_SOCKET_DIR` when set, otherwise
19
19
  * `/run/ces-ipc`.
20
20
  */
21
- export function getCesSocketDir(): string {
21
+ function getCesSocketDir(): string {
22
22
  return process.env["CES_SOCKET_DIR"]?.trim() || CES_SOCKET_DIR_DEFAULT;
23
23
  }
24
24
 
@@ -1,3 +1,5 @@
1
+ import { z } from "zod";
2
+
1
3
  /**
2
4
  * Wire contracts for the remote-web pairing flow — the RFC 8628-style
3
5
  * device-code exchange that connects a browser (or the iOS app) to a
@@ -59,6 +61,16 @@ export interface RemoteWebPairingChallengeResponse {
59
61
  intervalSeconds: number;
60
62
  }
61
63
 
64
+ export const RemoteWebPairingChallengeResponseSchema = z.object({
65
+ deviceCode: z.string(),
66
+ userCode: z.string(),
67
+ verificationUri: z.string(),
68
+ publicBaseUrl: z.string(),
69
+ expiresAt: z.string(),
70
+ expiresInSeconds: z.number(),
71
+ intervalSeconds: z.number(),
72
+ }) satisfies z.ZodType<RemoteWebPairingChallengeResponse>;
73
+
62
74
  /** `POST /v1/remote-web/pairing-verification` request body. */
63
75
  export interface RemoteWebPairingVerificationRequest {
64
76
  /** The `userCode` from the challenge, entered/scanned on the host. */
@@ -1,3 +1,5 @@
1
+ import { z } from "zod";
2
+
1
3
  /**
2
4
  * Wire contracts for the remote-web pairing flow — the RFC 8628-style
3
5
  * device-code exchange that connects a browser (or the iOS app) to a
@@ -59,6 +61,16 @@ export interface RemoteWebPairingChallengeResponse {
59
61
  intervalSeconds: number;
60
62
  }
61
63
 
64
+ export const RemoteWebPairingChallengeResponseSchema = z.object({
65
+ deviceCode: z.string(),
66
+ userCode: z.string(),
67
+ verificationUri: z.string(),
68
+ publicBaseUrl: z.string(),
69
+ expiresAt: z.string(),
70
+ expiresInSeconds: z.number(),
71
+ intervalSeconds: z.number(),
72
+ }) satisfies z.ZodType<RemoteWebPairingChallengeResponse>;
73
+
62
74
  /** `POST /v1/remote-web/pairing-verification` request body. */
63
75
  export interface RemoteWebPairingVerificationRequest {
64
76
  /** The `userCode` from the challenge, entered/scanned on the host. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mycadet/gateway",
3
- "version": "0.13.0-dev.202611142107.f5e00a2",
3
+ "version": "0.13.0-dev.202611151727.8310b2c",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/vargasjr-dev/mycadet-temp.git"
@@ -498,9 +498,12 @@ describe("remote web pairing token exchange", () => {
498
498
 
499
499
  expect(refresh.status).toBe(200);
500
500
  const body = (await refresh.json()) as Record<string, unknown>;
501
- expect(typeof body.accessToken).toBe("string");
502
- expect(typeof body.refreshToken).toBe("string");
503
- expect(typeof body.refreshAfter).toBe("number");
501
+ // The device refresh answers with the same credential contract the
502
+ // pairing mint emits (ISO strings, deviceId echoed) — one schema parses
503
+ // both.
504
+ expect(body.deviceId).toBe("legacy-device");
505
+ expect(typeof body.refreshAfter).toBe("string");
506
+ expect(typeof body.leasedAt).toBe("string");
504
507
  expect(setCookies(refresh)).toHaveLength(0);
505
508
  });
506
509
 
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The guardian credential contract — one Zod schema that is the single
3
+ * source of truth for the credential's shape on the gateway's wire: the
4
+ * pairing mint and the device-bound refresh both answer with it.
5
+ *
6
+ * This mirrors `packages/local-mode/src/guardian-contract.ts` (same
7
+ * names, same shape): the gateway can't depend on local-mode because
8
+ * local-mode carries operator-facing APIs the gateway must never see.
9
+ * The duplication is temporary — once the gateway client-generation
10
+ * workstream lands, the generated SDK types replace both hand-rolled
11
+ * copies. Delete the leftovers then.
12
+ */
13
+ import { z } from "zod";
14
+
15
+ export const GuardianTokenFileSchema = z.object({
16
+ guardianPrincipalId: z.string(),
17
+ accessToken: z.string(),
18
+ /** ISO date string or epoch-ms number as returned by the gateway. */
19
+ accessTokenExpiresAt: z.union([z.string(), z.number()]),
20
+ refreshToken: z.string(),
21
+ /** ISO date string or epoch-ms number as returned by the gateway. */
22
+ refreshTokenExpiresAt: z.union([z.string(), z.number()]),
23
+ refreshAfter: z.string(),
24
+ /** The paired device's id, echoed by the gateway so the binding persists. */
25
+ deviceId: z.string(),
26
+ /** ISO-8601 instant the credential was leased or rotated. */
27
+ leasedAt: z.string(),
28
+ });
29
+
30
+ export type GuardianTokenData = z.infer<typeof GuardianTokenFileSchema>;
@@ -172,15 +172,33 @@ export async function handleGuardianRefresh(req: Request): Promise<Response> {
172
172
  },
173
173
  "Refresh token rotation succeeded",
174
174
  );
175
- return Response.json(result.result);
175
+ // The device refresh answers with the full guardian credential
176
+ // contract — the same shape the pairing mint emits — so the client
177
+ // parses both with one schema and persists the rotation as-is. The
178
+ // browser arm above keeps its 4-field cookie contract.
179
+ const rotated = result.result;
180
+ return Response.json({
181
+ guardianPrincipalId: rotated.guardianPrincipalId,
182
+ accessToken: rotated.accessToken,
183
+ accessTokenExpiresAt: new Date(
184
+ rotated.accessTokenExpiresAt,
185
+ ).toISOString(),
186
+ refreshToken: rotated.refreshToken,
187
+ refreshTokenExpiresAt: new Date(
188
+ rotated.refreshTokenExpiresAt,
189
+ ).toISOString(),
190
+ refreshAfter: new Date(rotated.refreshAfter).toISOString(),
191
+ // The refresh token is bound to the device that presented it; echo
192
+ // the binding so the client's persisted credential stays whole.
193
+ deviceId,
194
+ leasedAt: new Date().toISOString(),
195
+ });
176
196
  } catch (err) {
177
197
  log.error({ err }, "Guardian refresh failed");
178
198
  return Response.json({ error: "Internal server error" }, { status: 500 });
179
199
  }
180
200
  }
181
201
 
182
-
183
-
184
202
  export const ROUTES: RouteDefinition[] = [
185
203
  {
186
204
  // The refresh endpoint's purpose is to obtain a new access token,
@@ -13,6 +13,7 @@ import {
13
13
  contacts as gwContacts,
14
14
  contactChannels as gwContactChannels,
15
15
  } from "../../db/schema.js";
16
+ import { GuardianTokenData } from "../../guardian-contract.js";
16
17
  import { getLogger } from "../../logger.js";
17
18
 
18
19
  const log = getLogger("pair");
@@ -66,26 +67,6 @@ export async function resolveLocalGuardianPrincipalId(): Promise<string> {
66
67
  return "local";
67
68
  }
68
69
 
69
- /**
70
- * The payload speaks the guardian token file's own field names so the
71
- * leasing side can parse it with that schema directly — no rename hop.
72
- */
73
- export interface DeviceBoundPairPayload {
74
- accessToken: string;
75
- /** ISO-8601 instant the access token expires. */
76
- accessTokenExpiresAt: string;
77
- refreshToken: string;
78
- /** ISO-8601 instant the device-bound refresh token expires. */
79
- refreshTokenExpiresAt: string;
80
- /** ISO-8601 instant after which the client should refresh the session. */
81
- refreshAfter: string;
82
- guardianPrincipalId: string;
83
- /** The paired device's id, echoed so the lease can persist the binding. */
84
- deviceId: string;
85
- /** ISO-8601 instant the credential was leased. */
86
- leasedAt: string;
87
- }
88
-
89
70
  /**
90
71
  * Mint a pairing token for a device
91
72
  */
@@ -93,7 +74,7 @@ export async function mintPairingPayload(opts: {
93
74
  deviceId: string;
94
75
  platform: string;
95
76
  clientReportedName?: string;
96
- }): Promise<DeviceBoundPairPayload> {
77
+ }): Promise<GuardianTokenData> {
97
78
  const guardian = await resolveOrCreateCadetGuardian({
98
79
  allowMintWithEvidence: true,
99
80
  });