Open History to see the last commit
1import { bootstrapRemoteBearerSession } from "@t3tools/client-runtime/authorization";
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Ref from "effect/Ref";
7import * as Schema from "effect/Schema";
8import * as Semaphore from "effect/Semaphore";
9import * as HttpClient from "effect/unstable/http/HttpClient";
10
11import * as DesktopBackendManager from "./DesktopBackendManager.ts";
12
13export class DesktopLocalEnvironmentAuthBackendNotConfiguredError extends Schema.TaggedErrorClass<DesktopLocalEnvironmentAuthBackendNotConfiguredError>()(
14 "DesktopLocalEnvironmentAuthBackendNotConfiguredError",
15 {},
16) {
17 override get message(): string {
18 return "Local backend is not configured.";
19 }
20}
21
22export class DesktopLocalEnvironmentAuthSessionBootstrapError extends Schema.TaggedErrorClass<DesktopLocalEnvironmentAuthSessionBootstrapError>()(
23 "DesktopLocalEnvironmentAuthSessionBootstrapError",
24 { cause: Schema.Defect() },
25) {
26 override get message(): string {
27 return "Failed to create the local desktop bearer session.";
28 }
29}
30
31export const DesktopLocalEnvironmentAuthError = Schema.Union([
32 DesktopLocalEnvironmentAuthBackendNotConfiguredError,
33 DesktopLocalEnvironmentAuthSessionBootstrapError,
34]);
35export type DesktopLocalEnvironmentAuthError = typeof DesktopLocalEnvironmentAuthError.Type;
36
37export class DesktopLocalEnvironmentAuth extends Context.Service<
38 DesktopLocalEnvironmentAuth,
39 {
40 readonly getBearerToken: Effect.Effect<string, DesktopLocalEnvironmentAuthError>;
41 }
42>()("@t3tools/desktop/backend/DesktopLocalEnvironmentAuth") {}
43
44export const make = Effect.gen(function* () {
45 const backendManager = yield* DesktopBackendManager.DesktopBackendManager;
46 const httpClient = yield* HttpClient.HttpClient;
47 const tokenRef = yield* Ref.make(Option.none<string>());
48 const mutex = yield* Semaphore.make(1);
49
50 const getBearerToken = mutex
51 .withPermits(1)(
52 Effect.gen(function* () {
53 const cached = yield* Ref.get(tokenRef);
54 if (Option.isSome(cached)) {
55 return cached.value;
56 }
57
58 const configOption = yield* backendManager.currentConfig;
59 if (Option.isNone(configOption)) {
60 return yield* new DesktopLocalEnvironmentAuthBackendNotConfiguredError();
61 }
62 const config = configOption.value;
63 const session = yield* bootstrapRemoteBearerSession({
64 httpBaseUrl: config.httpBaseUrl.href,
65 credential: config.bootstrap.desktopBootstrapToken,
66 clientMetadata: {
67 label: "T3 Code Desktop",
68 deviceType: "desktop",
69 },
70 }).pipe(
71 Effect.provideService(HttpClient.HttpClient, httpClient),
72 Effect.mapError(
73 (cause) =>
74 new DesktopLocalEnvironmentAuthSessionBootstrapError({
75 cause,
76 }),
77 ),
78 );
79 yield* Ref.set(tokenRef, Option.some(session.access_token));
80 return session.access_token;
81 }),
82 )
83 .pipe(Effect.withSpan("desktop.localEnvironmentAuth.getBearerToken"));
84
85 return DesktopLocalEnvironmentAuth.of({ getBearerToken });
86});
87
88export const layer = Layer.effect(DesktopLocalEnvironmentAuth, make);