Repository navigation
Expand file tree
/
Copy pathhttp.ts
More file actions
166 lines (155 loc) · 6.35 KB
/
Copy pathhttp.ts
File metadata and controls
166 lines (155 loc) · 6.35 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
import {
AuthRelayReadScope,
AuthRelayWriteScope,
EnvironmentCloudEndpointUnavailableError,
EnvironmentHttpApi,
EnvironmentHttpBadRequestError,
EnvironmentHttpConflictError,
EnvironmentHttpForbiddenError,
EnvironmentHttpInternalServerError,
EnvironmentHttpUnauthorizedError,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as HttpEffect from "effect/http/HttpEffect";
import { HttpServerRequest, HttpServerResponse } from "effect/http";
import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
import { requireEnvironmentScope } from "../auth/http.ts";
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
import * as ManagedLimits from "../policy/managedLimits.ts";
import * as CloudLink from "./CloudLink.ts";
import * as ConnectReachability from "./ConnectReachability.ts";
import { canLinkHere } from "./linkChecks.ts";
import type { RelayRequestError } from "./relayResponse.ts";
import { traceRelayRequest } from "./traceRelayRequest.ts";
const CLOUD_CREDENTIAL_RESPONSE_HEADERS = {
"cache-control": "no-store",
pragma: "no-cache",
} as const;
const appendCloudCredentialResponseHeaders = HttpEffect.appendPreResponseHandler(
(_request, response) =>
Effect.succeed(HttpServerResponse.setHeaders(response, CLOUD_CREDENTIAL_RESPONSE_HEADERS)),
);
const internalServerError = (error: { readonly message: string }, cause: unknown) =>
Effect.logError(error.message, { cause }).pipe(
Effect.andThen(Effect.fail(new EnvironmentHttpInternalServerError({ message: error.message }))),
);
const relayFailure = (error: RelayRequestError) => {
const message = error.message;
switch (error.rejection) {
case "unauthorized":
return Effect.fail(new EnvironmentHttpUnauthorizedError({ message }));
case "forbidden":
return Effect.fail(new EnvironmentHttpForbiddenError({ message }));
case "rejected":
return Effect.fail(new EnvironmentHttpBadRequestError({ message }));
case "unavailable":
return Effect.fail(new EnvironmentHttpInternalServerError({ message }));
}
};
const badRequest = (error: { readonly message: string }) =>
Effect.fail(new EnvironmentHttpBadRequestError({ message: error.message }));
const unauthorized = (error: { readonly message: string }) =>
Effect.fail(new EnvironmentHttpUnauthorizedError({ message: error.message }));
const conflict = (error: { readonly message: string }) =>
Effect.fail(new EnvironmentHttpConflictError({ message: error.message }));
const connectErrorCases = {
CloudLinkRelayConfigInvalidError: badRequest,
CloudLinkOriginInvalidError: badRequest,
CloudLinkNotLinkedError: badRequest,
CloudLinkAccountMismatchError: conflict,
CloudLinkAuthorizationMissingError: unauthorized,
CloudLinkProofRejectedError: unauthorized,
CloudLinkProofReplayedError: conflict,
CloudLinkTunnelSupersededError: conflict,
ManagedPolicyRefusedError: (error: ManagedLimits.ManagedPolicyRefusedError) =>
Effect.fail(new EnvironmentHttpForbiddenError({ message: error.message })),
CloudLinkInternalError: (error: CloudLink.CloudLinkInternalError) =>
internalServerError(error, error.cause),
RelayRequestError: relayFailure,
} as const;
type ConnectFailure =
| Exclude<CloudLink.CloudLinkError, CloudLink.CloudLinkEndpointUnavailableError>
| EnvironmentAuth.ServerAuthInternalError
| ManagedLimits.ManagedPolicyRefusedError
| RelayRequestError;
const toHttpError = <A, R>(effect: Effect.Effect<A, ConnectFailure, R>) =>
effect.pipe(
Effect.catchTags(connectErrorCases),
Effect.catchIf(EnvironmentAuth.isServerAuthInternalError, (error) =>
internalServerError(error, error),
),
);
const toHttpErrorOrUnavailable = <A, R>(
effect: Effect.Effect<A, ConnectFailure | CloudLink.CloudLinkEndpointUnavailableError, R>,
) =>
effect.pipe(
Effect.catchTags({
...connectErrorCases,
CloudLinkEndpointUnavailableError: (error) =>
Effect.fail(
new EnvironmentCloudEndpointUnavailableError({
message: error.message,
endpointRuntimeStatus: error.endpointRuntimeStatus,
}),
),
}),
Effect.catchIf(EnvironmentAuth.isServerAuthInternalError, (error) =>
internalServerError(error, error),
),
);
export const layer = HttpApiBuilder.group(
EnvironmentHttpApi,
"connect",
Effect.fnUntraced(function* (handlers) {
const cloudLink = yield* CloudLink.CloudLink;
const reachability = yield* ConnectReachability.ConnectReachability;
return handlers
.handle("linkProof", ({ payload }) =>
Effect.gen(function* () {
yield* requireEnvironmentScope(AuthRelayWriteScope);
const request = yield* HttpServerRequest.HttpServerRequest;
const proof = yield* toHttpError(cloudLink.linkProof(payload, request));
yield* appendCloudCredentialResponseHeaders;
return proof;
}),
)
.handle("relayConfig", ({ payload }) =>
requireEnvironmentScope(AuthRelayWriteScope).pipe(
Effect.andThen(toHttpErrorOrUnavailable(cloudLink.applyRelayConfig(payload))),
),
)
.handle("linkState", () =>
Effect.gen(function* () {
const principal = yield* requireEnvironmentScope(AuthRelayReadScope);
const state = yield* toHttpError(cloudLink.linkState());
const request = yield* HttpServerRequest.HttpServerRequest;
return { ...state, linkableHere: canLinkHere(request, principal.subject) };
}),
)
.handle("checkReachability", () =>
requireEnvironmentScope(AuthRelayReadScope).pipe(Effect.andThen(reachability.check)),
)
.handle("unlink", () =>
requireEnvironmentScope(AuthRelayWriteScope).pipe(
Effect.andThen(toHttpError(cloudLink.unlink())),
),
)
.handle("preferences", ({ payload }) =>
requireEnvironmentScope(AuthRelayWriteScope).pipe(
Effect.andThen(toHttpError(cloudLink.updatePreferences(payload))),
),
)
.handle("health", ({ payload }) =>
toHttpError(cloudLink.answerHealthRequest(payload)).pipe(
Effect.tap(() => appendCloudCredentialResponseHeaders),
),
)
.handle("t3MintCredential", ({ payload }) =>
traceRelayRequest(
toHttpError(cloudLink.mintCredential(payload)).pipe(
Effect.tap(() => appendCloudCredentialResponseHeaders),
),
),
);
}),
);