Line data Source code
1 : /**
2 : * Decides the credential headers for one request, and nothing else: callers
3 : * send, retry, and track scope. A cookie session authorizes ingest itself;
4 : * every other service takes a Bearer workspace token. Firebase and API-key
5 : * principals keep the single header they send today, whatever the target.
6 : */
7 : import { name, version } from '#package.json' with { type: 'json' }
8 :
9 : import type { WebSession } from './sessionContracts'
10 :
11 : export type RequestTarget = 'ingest' | 'resource'
12 :
13 : export type RequestPrincipal =
14 : | { readonly kind: 'session'; readonly session: WebSession }
15 : | { readonly kind: 'firebase'; readonly getIdToken: () => Promise<string> }
16 : | { readonly kind: 'apiKey'; readonly apiKey: string }
17 :
18 : export interface RequestAuthInput {
19 : readonly target: RequestTarget
20 : readonly method: string
21 : /** Absent means the personal workspace. */
22 : readonly workspaceId?: string
23 : }
24 :
25 : export interface RequestAuthorization {
26 : readonly headers: Readonly<Record<string, string>>
27 : /** Set only for a cookie request, which must carry the session cookie. */
28 : readonly credentials?: 'include'
29 : }
30 :
31 : export type RequestAuthorizer = (
32 : principal: RequestPrincipal,
33 : input: RequestAuthInput
34 : ) => Promise<RequestAuthorization>
35 :
36 : export interface RequestAuthOptions {
37 : /** A workspace token for a session principal; minting is the caller's. */
38 : readonly getWorkspaceToken: (
39 : workspaceId: string | undefined
40 : ) => Promise<string>
41 : /**
42 : * The Desktop per-installation id, sent as `X-Comfy-Install-Id`; null on
43 : * plain web.
44 : */
45 : readonly getInstallId?: () => string | null | undefined
46 : }
47 :
48 2356 : export const COMFY_CLIENT = `${name}/${version}`
49 :
50 2356 : const SAFE_METHODS: ReadonlySet<string> = new Set(['GET', 'HEAD', 'OPTIONS'])
51 :
52 0 : function sessionHeaders(
53 : session: WebSession,
54 : { method, workspaceId }: RequestAuthInput,
55 : installId?: string | null
56 : ): Record<string, string> {
57 0 : return {
58 : 'X-Comfy-Client': COMFY_CLIENT,
59 : ...(installId ? { 'X-Comfy-Install-Id': installId } : {}),
60 : ...(workspaceId === undefined
61 : ? {}
62 : : { 'X-Comfy-Workspace-ID': workspaceId }),
63 : ...(SAFE_METHODS.has(method.toUpperCase())
64 : ? {}
65 : : { 'X-CSRF-Token': session.csrfToken })
66 : }
67 : }
68 :
69 0 : export function createRequestAuthorizer({
70 : getWorkspaceToken,
71 : getInstallId
72 : }: RequestAuthOptions): RequestAuthorizer {
73 0 : return async function authorize(
74 : principal,
75 : input
76 : ): Promise<RequestAuthorization> {
77 0 : switch (principal.kind) {
78 : case 'apiKey':
79 0 : return { headers: { 'X-API-KEY': principal.apiKey } }
80 : case 'firebase':
81 0 : return {
82 : headers: { Authorization: `Bearer ${await principal.getIdToken()}` }
83 : }
84 : case 'session':
85 0 : if (input.target === 'resource') {
86 : const token = await getWorkspaceToken(input.workspaceId)
87 0 : return { headers: { Authorization: `Bearer ${token}` } }
88 : }
89 0 : return {
90 : headers: sessionHeaders(principal.session, input, getInstallId?.()),
91 : credentials: 'include'
92 : }
93 : }
94 : }
95 : }
|