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

deviceHubAccess.ts

69 lines2.9 KB

1/**
2 * `<img>`, `EventSource`, and `WebSocket` cannot set bearer or DPoP headers, so bearer and DPoP
3 * connections mint a short-lived WebSocket ticket and pass it as `wsTicket`, the same way the
4 * app's own `/ws` upgrade authenticates. Callers fetch a fresh one each time they (re)connect a
5 * stream.
6 */
7import * as Effect from "effect/Effect";
8import * as Redacted from "effect/Redacted";
9import type { HttpClient } from "effect/http";
10​
11import { issueWebSocketTicketWithFallback } from "../authorization/remote.ts";
12import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
13import type { PreparedConnection } from "../connection/model.ts";
14import { environmentEndpointUrl } from "../environment/endpoint.ts";
15import * as ManagedRelay from "../relay/managedRelay.ts";
16import type { RemoteEnvironmentRequestError } from "../rpc/http.ts";
17import { executeAuthenticatedEnvironmentHttpRequest } from "./environmentHttpAuth.ts";
18import type { DeviceHubAccess } from "../device/hubAccess.ts";
19​
20export { type DeviceHubAccess, withDeviceHubQuery } from "../device/hubAccess.ts";
21​
22const TICKET_TIMEOUT_MS = 8_000;
23​
24/** Reusable across media requests until it expires; only for connections that send credentials. */
25export const mintMediaTicket = Effect.fn("clientRuntime.state.mintMediaTicket")(function* (
26 prepared: PreparedConnection,
27) {
28 const signer = yield* Effect.serviceOption(ManagedRelay.ManagedRelayDpopSigner);
29 const remoteAuthorization = yield* Effect.serviceOption(
30 RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization,
31 );
32 // Each attempt signs its own URL, so the bare-path fallback carries a matching DPoP proof.
33 return yield* issueWebSocketTicketWithFallback("media", (use) =>
34 executeAuthenticatedEnvironmentHttpRequest({
35 prepared,
36 signer,
37 remoteAuthorization,
38 group: "auth",
39 method: "POST",
40 url: (urls) =>
41 use === undefined ? urls.webSocketTicket() : urls.webSocketTicketFor({ params: { use } }),
42 timeoutMs: TICKET_TIMEOUT_MS,
43 request: ({ client, headers }) =>
44 use === undefined
45 ? client.webSocketTicket({ headers })
46 : client.webSocketTicketFor({ headers, params: { use } }),
47 }),
48 );
49});
50​
51export const resolveDeviceHubAccess = Effect.fn("clientRuntime.state.resolveDeviceHubAccess")(
52 function* (input: {
53 readonly prepared: PreparedConnection;
54 readonly hubBasePath: string;
55 }): Effect.fn.Return<DeviceHubAccess, RemoteEnvironmentRequestError, HttpClient.HttpClient> {
56 const httpBase = environmentEndpointUrl(input.prepared.httpBaseUrl, input.hubBasePath);
57 const wsBase = httpBase.replace(/^http/, "ws");
58 if (input.prepared.httpAuthorization === null) {
59 return { httpBase, wsBase, query: {}, credentials: true };
60 }
61 const ticket = yield* mintMediaTicket(input.prepared);
62 return {
63 httpBase,
64 wsBase,
65 query: { wsTicket: Redacted.value(ticket.ticket) },
66 credentials: false,
67 };
68 },
69);