Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

deviceHubAccess.ts

73 lines3.2 KB

1/**
2 * Credentials for the Device panel's media requests.
3 *
4 * The panel reaches simulator streams through `/api/device-hub/*` on the
5 * environment origin. `<img>`, `EventSource`, and `WebSocket` cannot set
6 * bearer or DPoP headers, so bearer and DPoP connections mint a
7 * short-lived WebSocket ticket and pass it as `wsTicket`, the same way the
8 * app's own `/ws` upgrade authenticates. Cookie sessions send the cookie.
9 *
10 * A ticket lives five minutes server-side and is bound to the session, not
11 * to one request, so one ticket covers everything a panel opens at once.
12 * Callers fetch a fresh one each time they (re)connect a stream.
13 */
14import * as Effect from "effect/Effect";
15import type { HttpClient } from "effect/unstable/http";
16​
17import { RemoteEnvironmentAuthorization } from "../authorization/service.ts";
18import type { PreparedConnection } from "../connection/model.ts";
19import { environmentEndpointUrl } from "../environment/endpoint.ts";
20import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
21import type { RemoteEnvironmentRequestError } from "../rpc/http.ts";
22import { executeAuthenticatedEnvironmentHttpRequest } from "./environmentHttpAuth.ts";
23​
24const TICKET_TIMEOUT_MS = 8_000;
25​
26export interface DeviceHubAccess {
27 /** Absolute origin-relative base, e.g. `https://env.example/api/device-hub`. */
28 readonly httpBase: string;
29 /** Same base with the `ws(s)` scheme. */
30 readonly wsBase: string;
31 /** Query parameters to append to every hub request; empty for cookie sessions. */
32 readonly query: Readonly<Record<string, string>>;
33 /** Whether requests must include cookies (same-origin session). */
34 readonly credentials: boolean;
36​
37export const resolveDeviceHubAccess = Effect.fn("clientRuntime.state.resolveDeviceHubAccess")(
38 function* (input: {
39 readonly prepared: PreparedConnection;
40 readonly hubBasePath: string;
41 }): Effect.fn.Return<DeviceHubAccess, RemoteEnvironmentRequestError, HttpClient.HttpClient> {
42 const httpBase = environmentEndpointUrl(input.prepared.httpBaseUrl, input.hubBasePath);
43 const wsBase = httpBase.replace(/^http/, "ws");
44 if (input.prepared.httpAuthorization === null) {
45 return { httpBase, wsBase, query: {}, credentials: true };
46 }
47 const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
48 const remoteAuthorization = yield* Effect.serviceOption(RemoteEnvironmentAuthorization);
49 const ticket = yield* executeAuthenticatedEnvironmentHttpRequest({
50 prepared: input.prepared,
51 signer,
52 remoteAuthorization,
53 group: "auth",
54 method: "POST",
55 url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, "/api/auth/websocket-ticket"),
56 timeoutMs: TICKET_TIMEOUT_MS,
57 request: ({ client, headers }) => client.webSocketTicket({ headers }),
58 });
59 return {
60 httpBase,
61 wsBase,
62 query: { wsTicket: ticket.ticket },
63 credentials: false,
64 };
65 },
66);
67​
68export const withDeviceHubQuery = (url: string, access: DeviceHubAccess): string => {
69 const entries = Object.entries(access.query);
70 if (entries.length === 0) return url;
71 const separator = url.includes("?") ? "&" : "?";
72 return `${url}${separator}${new URLSearchParams(entries).toString()}`;
73};