The best way to control Claude Code, Codex and more from any computer or phone

main
ts 69 lines 2.13 KB
Raw
1 import { DEFAULT_API_BASE_URL } from "@distilled.cloud/hetzner";
2 import { getEnv, getEnvRedactedRequired } from "alchemy/Auth/Env";
3 import {
4 makeStoredAuthProvider,
5 storedSecret,
6 storedValueText,
7 type StoredAuthConfig,
8 } from "alchemy/Auth/StoredAuthProvider";
9 import * as Effect from "effect/Effect";
10 import * as Redacted from "effect/Redacted";
11
12 export const HETZNER_AUTH_PROVIDER_NAME = "Hetzner";
13 export const HCLOUD_TOKEN_ENV = "HCLOUD_TOKEN";
14 export const HCLOUD_ENDPOINT_ENV = "HCLOUD_ENDPOINT";
15
16 export type HetznerAuthConfig = StoredAuthConfig;
17
18 export type HetznerResolvedCredentials = {
19 type: "token";
20 token: Redacted.Redacted<string>;
21 apiBaseUrl: string;
22 source: { type: HetznerAuthConfig["method"] | "env"; details?: string };
23 };
24
25 const hetznerAuth = makeStoredAuthProvider<HetznerResolvedCredentials>({
26 provider: HETZNER_AUTH_PROVIDER_NAME,
27 fields: [
28 { name: "token", label: "Hetzner Cloud API Token", secret: true },
29 {
30 name: "apiBaseUrl",
31 label: "Hetzner API endpoint",
32 optional: true,
33 placeholder: DEFAULT_API_BASE_URL,
34 },
35 ],
36 toResolved: (values) => ({
37 type: "token",
38 token: storedSecret(values.token) ?? Redacted.make(""),
39 apiBaseUrl: storedValueText(values.apiBaseUrl) ?? DEFAULT_API_BASE_URL,
40 source: { type: "stored" },
41 }),
42 readEnvironment: Effect.all({
43 token: getEnvRedactedRequired(HCLOUD_TOKEN_ENV),
44 apiBaseUrl: getEnv(HCLOUD_ENDPOINT_ENV),
45 }).pipe(
46 Effect.map(({ token, apiBaseUrl }) => ({
47 type: "token" as const,
48 token,
49 apiBaseUrl: apiBaseUrl ?? DEFAULT_API_BASE_URL,
50 source: {
51 type: "env" as const,
52 details: apiBaseUrl ? `${HCLOUD_TOKEN_ENV}, ${HCLOUD_ENDPOINT_ENV}` : HCLOUD_TOKEN_ENV,
53 },
54 })),
55 ),
56 environment: [
57 { name: HCLOUD_TOKEN_ENV, required: true, secret: true },
58 { name: HCLOUD_ENDPOINT_ENV, required: false },
59 ],
60 });
61
62 /**
63 * Layer that registers the Hetzner {@link AuthProvider} into the
64 * {@link AuthProviders} registry.
65 *
66 * Auth is a Hetzner Cloud API token (`HCLOUD_TOKEN`). An optional
67 * `HCLOUD_ENDPOINT` overrides the API root.
68 */
69 export const HetznerAuth = hetznerAuth.layer;