| 1 | import { env } from "cloudflare:workers"; |
| 2 | |
| 3 | import type { User } from "@g1t/contracts"; |
| 4 | |
| 5 | import { getViewer, roleIn } from "./session.server"; |
| 6 | import { identity } from "./services.server"; |
| 7 | import { issueTicket, socketPath, ticketViewer } from "./socket-ticket"; |
| 8 | import { bearerToken, websiteUser } from "./website-token"; |
| 9 | |
| 10 | let isolateSecret: string | null = null; |
| 11 | |
| 12 | /** |
| 13 | * What tickets are sealed with: the site's `USERCONTENT_KEY`, from which |
| 14 | * lib/socket-ticket.ts derives a key of their own. Without it (a local |
| 15 | * run), a key made for this isolate, which is enough where one process |
| 16 | * serves the site. |
| 17 | */ |
| 18 | export function ticketSecret(): string { |
| 19 | if (env.USERCONTENT_KEY) return env.USERCONTENT_KEY; |
| 20 | if (!isolateSecret) { |
| 21 | const bytes = crypto.getRandomValues(new Uint8Array(32)); |
| 22 | isolateSecret = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(""); |
| 23 | } |
| 24 | return isolateSecret; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * A socket ticket minted with a page, for a page opened with an access |
| 29 | * token: what `GET /-/live/ticket?path=` would answer, so the page's |
| 30 | * first socket opens without that round trip (lib/live-socket.ts |
| 31 | * `offerTicket`). Null for a session (its sockets carry the cookie) and |
| 32 | * for a path that is not one of the site's sockets, or not the viewer's |
| 33 | * workspace's. |
| 34 | */ |
| 35 | export async function socketTicketFor(context: Parameters<typeof getViewer>[0], request: Request, path: string): Promise<{ ticket: string; expires_at: string } | null> { |
| 36 | const viewer = getViewer(context); |
| 37 | const token = bearerToken(request); |
| 38 | if (!viewer || !token || !viewer.token?.website) return null; |
| 39 | const socket = socketPath(path); |
| 40 | if (!socket || (socket.workspace && !roleIn(viewer, socket.workspace))) return null; |
| 41 | return issueTicket(ticketSecret(), { token, userId: viewer.id, path: socket.path }); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Who opens a live socket: the session or token the request carries, as |
| 46 | * on any page, or else the person a socket ticket was made for |
| 47 | * (lib/socket-ticket.ts), whose token is checked again now. |
| 48 | */ |
| 49 | export async function socketViewer(context: Parameters<typeof getViewer>[0], request: Request): Promise<User | null> { |
| 50 | const viewer = getViewer(context); |
| 51 | if (viewer) return viewer; |
| 52 | return ticketViewer(request, ticketSecret(), async (token) => websiteUser(await identity.userForAccessToken(token))); |
| 53 | } |