Repository navigation
Expand file tree
/
Copy pathenvironmentHttpAuth.ts
More file actions
223 lines (212 loc) · 8.03 KB
/
Copy pathenvironmentHttpAuth.ts
File metadata and controls
223 lines (212 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import {
ORCHESTRATION_PROTOCOL_HEADER,
ORCHESTRATION_PROTOCOL_VERSION_TEXT,
} from "@t3tools/contracts";
import * as Result from "effect/Result";
import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
import { FetchHttpClient, type HttpMethod } from "effect/http";
import type { RemoteEnvironmentAuthorization } from "../authorization/service.ts";
import type { PreparedConnection, PreparedHttpAuthorization } from "../connection/model.ts";
import type { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
import {
executeEnvironmentHttpRequest,
makeEnvironmentHttpApiGroupClient,
makeEnvironmentHttpApiUrlBuilder,
RemoteEnvironmentAuthFetchError,
RemoteEnvironmentAuthTimeoutError,
type RemoteEnvironmentRequestError,
} from "../rpc/http.ts";
export interface EnvironmentHttpAuthHeaders {
readonly authorization?: string;
readonly dpop?: string;
}
export function withOrchestrationProtocolHeader(
headers: EnvironmentHttpAuthHeaders,
): EnvironmentHttpAuthHeaders & {
readonly [ORCHESTRATION_PROTOCOL_HEADER]: typeof ORCHESTRATION_PROTOCOL_VERSION_TEXT;
} {
return {
...headers,
[ORCHESTRATION_PROTOCOL_HEADER]: ORCHESTRATION_PROTOCOL_VERSION_TEXT,
};
}
/**
* Cookie-authenticated environments need credentialed mode for cross-origin fetches; bearer/DPoP uses headers.
* `FetchHttpClient` reads `RequestInit` from the fiber context per request.
*/
const withEnvironmentCredentials = <A, E, R>(
authorization: PreparedHttpAuthorization | null,
request: Effect.Effect<A, E, R>,
): Effect.Effect<A, E, R> =>
authorization === null
? request.pipe(Effect.provideService(FetchHttpClient.RequestInit, { credentials: "include" }))
: request;
const buildEnvironmentAuthHeaders = Effect.fn("environmentHttpAuth.buildEnvironmentAuthHeaders")(
function* (
authorization: PreparedHttpAuthorization | null,
method: HttpMethod.HttpMethod,
url: string,
signer: Option.Option<ManagedRelayDpopSigner["Service"]>,
): Effect.fn.Return<EnvironmentHttpAuthHeaders, RemoteEnvironmentAuthFetchError> {
if (authorization === null) {
return {};
}
if (authorization._tag === "Bearer") {
return { authorization: `Bearer ${authorization.token}` };
}
if (Option.isNone(signer)) {
return yield* new RemoteEnvironmentAuthFetchError({
message: "No DPoP signer is available to authorize the environment request.",
cause: authorization._tag,
});
}
const proof = yield* signer.value
.createProof({ method, url, accessToken: authorization.accessToken })
.pipe(
Effect.mapError(
(cause) =>
new RemoteEnvironmentAuthFetchError({
message: "Could not create the environment request authorization proof.",
cause,
}),
),
);
return { authorization: `DPoP ${authorization.accessToken}`, dpop: proof };
},
);
/**
* A DPoP request is T3 Connect work, so its span starts an exported trace that
* the environment continues; its local caller's span would leave that trace
* without a root.
*/
export const executeAuthenticatedEnvironmentHttpRequest = <
Group extends Parameters<typeof makeEnvironmentHttpApiGroupClient>[1],
A,
E,
R,
>(
input: Parameters<typeof executeEnvironmentRequest<Group, A, E, R>>[0],
) =>
input.prepared.httpAuthorization?._tag === "Dpop"
? executeEnvironmentRequest(input).pipe(
Effect.withSpan(ENVIRONMENT_REQUEST_SPAN, { root: true }),
withRelayClientTracing,
)
: executeEnvironmentRequest(input).pipe(Effect.withSpan(ENVIRONMENT_REQUEST_SPAN));
const ENVIRONMENT_REQUEST_SPAN = "clientRuntime.state.executeAuthenticatedEnvironmentHttpRequest";
const executeEnvironmentRequest = Effect.fnUntraced(function* <
Group extends Parameters<typeof makeEnvironmentHttpApiGroupClient>[1],
A,
E,
R,
>(input: {
readonly prepared: PreparedConnection;
readonly signer: Option.Option<ManagedRelayDpopSigner["Service"]>;
readonly remoteAuthorization?: Option.Option<RemoteEnvironmentAuthorization["Service"]>;
readonly method: HttpMethod.HttpMethod;
/**
* The URL to sign, from the group's contract URL builder. It encodes path
* params exactly like the request client, so the DPoP proof matches the
* URL actually sent.
*/
readonly url: (urls: ReturnType<typeof makeEnvironmentHttpApiUrlBuilder>[Group]) => string;
readonly timeoutMs: number;
readonly group: Group;
readonly request: (input: {
readonly client: Effect.Success<ReturnType<typeof makeEnvironmentHttpApiGroupClient<Group>>>;
readonly headers: EnvironmentHttpAuthHeaders;
}) => Effect.Effect<A, E, R>;
/** Some endpoints report rejected credentials in a successful response. */
readonly isUnauthorizedResponse?: (response: NoInfer<A>) => boolean;
}): Effect.fn.Return<
A,
RemoteEnvironmentRequestError,
Effect.Services<ReturnType<typeof makeEnvironmentHttpApiGroupClient<Group>>> | R
> {
let httpBaseUrl = input.prepared.httpBaseUrl;
const requestUrlFor = (baseUrl: string) =>
input.url(makeEnvironmentHttpApiUrlBuilder(baseUrl)[input.group]);
return yield* Effect.gen(function* () {
let rejectedAccessToken: string | undefined;
for (;;) {
let authorization = input.prepared.httpAuthorization;
if (authorization?._tag === "Dpop") {
const remote = input.remoteAuthorization;
if (remote === undefined || Option.isNone(remote)) {
return yield* new RemoteEnvironmentAuthFetchError({
message: "No relay authorization service is available for the environment request.",
cause: input.prepared.target._tag,
});
}
const current = yield* remote.value
.authorizeDpopHttp({
expectedEnvironmentId: input.prepared.environmentId,
...(rejectedAccessToken === undefined ? {} : { rejectedAccessToken }),
})
.pipe(
Effect.mapError(
(cause) =>
new RemoteEnvironmentAuthFetchError({
message: "Could not authorize the environment request.",
cause,
}),
),
);
// A learned direct route keeps its own origin; only the token renews.
if (input.prepared.target._tag === "RelayConnectionTarget") {
httpBaseUrl = current.httpBaseUrl;
}
authorization = current.httpAuthorization;
}
const requestUrl = requestUrlFor(httpBaseUrl);
const client = yield* makeEnvironmentHttpApiGroupClient(httpBaseUrl, input.group);
const headers = yield* buildEnvironmentAuthHeaders(
authorization,
input.method,
requestUrl,
input.signer,
);
const result = yield* executeEnvironmentHttpRequest(
requestUrl,
input.timeoutMs,
withEnvironmentCredentials(authorization, input.request({ client, headers })),
).pipe(Effect.result);
if (Result.isFailure(result)) {
if (
authorization?._tag === "Dpop" &&
rejectedAccessToken === undefined &&
result.failure._tag === "EnvironmentAuthInvalidError" &&
result.failure.reason === "invalid_credential"
) {
rejectedAccessToken = authorization.accessToken;
continue;
}
return yield* result.failure;
}
if (
authorization?._tag === "Dpop" &&
input.isUnauthorizedResponse?.(result.success) === true
) {
if (rejectedAccessToken === undefined) {
rejectedAccessToken = authorization.accessToken;
continue;
}
return yield* new RemoteEnvironmentAuthFetchError({
message: "The environment rejected the renewed session authorization.",
cause: result.success,
});
}
return result.success;
}
}).pipe(
Effect.timeoutOrElse({
duration: input.timeoutMs,
orElse: () =>
Effect.fail(
new RemoteEnvironmentAuthTimeoutError(requestUrlFor(httpBaseUrl), input.timeoutMs),
),
}),
);
});