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

environmentHttpAuth.ts

73 lines2.9 KB

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