Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Option from "effect/Option";
3import {
4 ORCHESTRATION_PROTOCOL_HEADER,
5 ORCHESTRATION_PROTOCOL_VERSION_TEXT,
6} from "@t3tools/contracts";
7import { FetchHttpClient, type HttpMethod } from "effect/unstable/http";
8
9import type { PreparedHttpAuthorization } from "../connection/model.ts";
10import type { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
11import { RemoteEnvironmentAuthFetchError } from "../rpc/http.ts";
12
13export interface EnvironmentHttpAuthHeaders {
14 readonly authorization?: string;
15 readonly dpop?: string;
16}
17
18export function withOrchestrationProtocolHeader(
19 headers: EnvironmentHttpAuthHeaders,
20): EnvironmentHttpAuthHeaders & {
21 readonly [ORCHESTRATION_PROTOCOL_HEADER]: typeof ORCHESTRATION_PROTOCOL_VERSION_TEXT;
22} {
23 return {
24 ...headers,
25 [ORCHESTRATION_PROTOCOL_HEADER]: ORCHESTRATION_PROTOCOL_VERSION_TEXT,
26 };
27}
28
29/**
30 * Primary/local environments with no bearer or DPoP credential authenticate the
31 * browser via a session cookie. A cross-origin `fetch` does not send cookies by
32 * default, so those requests must opt into credentialed mode; bearer/DPoP
33 * connections carry their credential in a header and need no cookies. Applied
34 * per-request via `FetchHttpClient.RequestInit`, which the fetch client reads
35 * from the fiber context at request time.
36 */
37export const withEnvironmentCredentials = <A, E, R>(
38 authorization: PreparedHttpAuthorization | null,
39 request: Effect.Effect<A, E, R>,
40): Effect.Effect<A, E, R> =>
41 authorization === null
42 ? request.pipe(Effect.provideService(FetchHttpClient.RequestInit, { credentials: "include" }))
43 : request;
44
45/**
46 * Build the authorization headers for an authenticated environment HTTP
47 * request, matching the credential the connection was prepared with:
48 * - primary/local connections carry no credential,
49 * - bearer connections send a static `Bearer` token,
50 * - relay connections send a `DPoP` access token with a freshly signed proof
51 * bound to this request's method and URL.
52 *
53 * The DPoP signer is passed in (not resolved from context) and is only required
54 * for relay/DPoP connections, so bearer/primary connections work even when no
55 * signer is available.
56 */
57export const buildEnvironmentAuthHeaders = (
58 authorization: PreparedHttpAuthorization | null,
59 method: HttpMethod.HttpMethod,
60 url: string,
61 signer: Option.Option<ManagedRelayDpopSigner["Service"]>,
62): Effect.Effect<EnvironmentHttpAuthHeaders, RemoteEnvironmentAuthFetchError> =>
63 Effect.gen(function* () {
64 if (authorization === null) {
65 return {};
66 }
67 if (authorization._tag === "Bearer") {
68 return { authorization: `Bearer ${authorization.token}` };
69 }
70 if (Option.isNone(signer)) {
71 return yield* new RemoteEnvironmentAuthFetchError({
72 message: "No DPoP signer is available to authorize the environment request.",
73 cause: authorization._tag,
74 });
75 }
76 const proof = yield* signer.value
77 .createProof({ method, url, accessToken: authorization.accessToken })
78 .pipe(
79 Effect.mapError(
80 (cause) =>
81 new RemoteEnvironmentAuthFetchError({
82 message: "Could not create the environment request authorization proof.",
83 cause,
84 }),
85 ),
86 );
87 return { authorization: `DPoP ${authorization.accessToken}`, dpop: proof };
88 });