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

authHttpHandlers.ts

103 lines3.8 KB

1import {
2 AuthSessionId,
3 EnvironmentAuthenticatedAuth,
4 EnvironmentAuthenticatedPrincipal,
5 EnvironmentAuthHttpApi,
6 EnvironmentId,
7 EnvironmentMetadataHttpApi,
8 type AuthEnvironmentScope,
9 type ExecutionEnvironmentDescriptor,
10 type ServerAuthDescriptor,
11} from "@t3tools/contracts";
12import * as DateTime from "effect/DateTime";
13import * as Effect from "effect/Effect";
14import * as Layer from "effect/Layer";
15import { HttpRouter, HttpServer } from "effect/unstable/http";
16import * as HttpApi from "effect/unstable/httpapi/HttpApi";
17import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
18import { http } from "msw";
19​
20const BrowserEnvironmentHttpApi = HttpApi.make("browserEnvironment")
21 .add(EnvironmentMetadataHttpApi)
22 .add(EnvironmentAuthHttpApi);
23​
24const TEST_SESSION_EXPIRES_AT = DateTime.makeUnsafe("2026-05-01T12:00:00.000Z");
25const TEST_ENVIRONMENT_DESCRIPTOR: ExecutionEnvironmentDescriptor = {
26 environmentId: EnvironmentId.make("environment-local"),
27 label: "Local environment",
28 platform: {
29 os: "darwin",
30 arch: "arm64",
31 },
32 serverVersion: "0.0.0-test",
33 capabilities: {
34 repositoryIdentity: true,
35 },
36};
37​
38const unexpectedEndpoint = (endpoint: string) =>
39 Effect.die(new Error(`Unexpected browser environment HTTP endpoint: ${endpoint}`));
40​
41export function createAuthenticatedSessionHandlers(getAuthDescriptor: () => ServerAuthDescriptor) {
42 const authenticatedAuthLayer = Layer.succeed(EnvironmentAuthenticatedAuth, (httpEffect) =>
43 httpEffect.pipe(
44 Effect.provideService(EnvironmentAuthenticatedPrincipal, {
45 sessionId: AuthSessionId.make("browser-session"),
46 subject: "browser-client",
47 method: "browser-session-cookie",
48 scopes: new Set<AuthEnvironmentScope>(),
49 expiresAt: TEST_SESSION_EXPIRES_AT,
50 }),
51 ),
52 );
53 const metadataLayer = HttpApiBuilder.group(BrowserEnvironmentHttpApi, "metadata", (handlers) =>
54 handlers.handle("descriptor", () => Effect.succeed(TEST_ENVIRONMENT_DESCRIPTOR)),
55 );
56 const authLayer = HttpApiBuilder.group(BrowserEnvironmentHttpApi, "auth", (handlers) =>
57 handlers
58 .handle("session", () =>
59 Effect.succeed({
60 authenticated: true,
61 auth: getAuthDescriptor(),
62 sessionMethod: "browser-session-cookie",
63 expiresAt: TEST_SESSION_EXPIRES_AT,
64 }),
65 )
66 .handle("browserSession", () =>
67 Effect.succeed({
68 authenticated: true,
69 scopes: [
70 "orchestration:read",
71 "orchestration:operate",
72 "terminal:operate",
73 "review:write",
74 "relay:read",
75 ],
76 sessionMethod: "browser-session-cookie",
77 expiresAt: TEST_SESSION_EXPIRES_AT,
78 }),
79 )
80 .handle("token", () => unexpectedEndpoint("auth.token"))
81 .handle("webSocketTicket", () => unexpectedEndpoint("auth.webSocketTicket"))
82 .handle("pairingCredential", () => unexpectedEndpoint("auth.pairingCredential"))
83 .handle("pairingLinks", () => unexpectedEndpoint("auth.pairingLinks"))
84 .handle("revokePairingLink", () => unexpectedEndpoint("auth.revokePairingLink"))
85 .handle("clients", () => unexpectedEndpoint("auth.clients"))
86 .handle("revokeClient", () => unexpectedEndpoint("auth.revokeClient"))
87 .handle("revokeOtherClients", () => unexpectedEndpoint("auth.revokeOtherClients")),
88 ).pipe(Layer.provide(authenticatedAuthLayer));
89 const { handler } = HttpRouter.toWebHandler(
90 HttpApiBuilder.layer(BrowserEnvironmentHttpApi).pipe(
91 Layer.provide(metadataLayer),
92 Layer.provide(authLayer),
93 Layer.provide(authenticatedAuthLayer),
94 Layer.provide(HttpServer.layerServices),
95 ),
96 { disableLogger: true },
97 );
98​
99 return [
100 http.all("*/.well-known/t3/environment", ({ request }) => handler(request)),
101 http.all("*/api/auth/*", ({ request }) => handler(request)),
102 ] as const;