The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Output from "alchemy/Output"; |
| 2 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 3 | import { Self } from "alchemy/Self"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import type * as HttpClient from "effect/http/HttpClient"; |
| 6 | import type * as Redacted from "effect/Redacted"; |
| 7 | import { AccountApiToken } from "../ApiToken/AccountApiToken.ts"; |
| 8 | import type { PermissionGroupRef } from "../ApiToken/Common.ts"; |
| 9 | import type { Credentials } from "../Credentials.ts"; |
| 10 | import { requireLiveResource } from "../HttpBindingDev.ts"; |
| 11 | import { authorizeWith } from "../HttpClientUtils.ts"; |
| 12 | import type { Zone } from "../Zone/Zone.ts"; |
| 13 | |
| 14 | /** |
| 15 | * Shared scaffolding for the HTTP-backed DNS bindings. |
| 16 | * |
| 17 | * Creates a least-privilege {@link AccountApiToken} scoped to the requested |
| 18 | * zone, binds its `value` into the host Worker at deploy time (guarded by |
| 19 | * `__ALCHEMY_RUNTIME__` so it is a no-op once running inside the deployed |
| 20 | * Worker), then delegates to `makeClient` with the bound token and the zone's |
| 21 | * `zoneId`. |
| 22 | * |
| 23 | * The zone's `zoneId` is bound into the Worker at init time, so the resulting |
| 24 | * client closes over it and callers never pass it per request — the |
| 25 | * provisioned token only grants access to that one zone anyway. |
| 26 | */ |
| 27 | export const makeHttpDnsBinding = <Client>(options: { |
| 28 | permissionGroups: PermissionGroupRef[]; |
| 29 | makeClient: (auth: DnsAuth, zoneId: Effect.Effect<string>) => Client; |
| 30 | }) => |
| 31 | Effect.gen(function* () { |
| 32 | const Token = yield* AccountApiToken; |
| 33 | const self = yield* Self; |
| 34 | |
| 35 | return Effect.fn(function* (zone: Zone) { |
| 36 | if (!globalThis.__ALCHEMY_RUNTIME__) yield* requireLiveResource(zone); |
| 37 | const token = yield* Token(`${self.LogicalId}Token`); |
| 38 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 39 | yield* token.bind`${self.LogicalId}(${zone})`({ |
| 40 | policies: [ |
| 41 | { |
| 42 | effect: "allow", |
| 43 | permissionGroups: options.permissionGroups, |
| 44 | resources: zone.zoneId.pipe( |
| 45 | Output.flatMap( |
| 46 | (zoneId) => Output.interpolate`com.cloudflare.api.account.zone.${zoneId}`, |
| 47 | ), |
| 48 | Output.map((zoneId) => ({ |
| 49 | [zoneId]: "*", |
| 50 | })), |
| 51 | ), |
| 52 | }, |
| 53 | ], |
| 54 | }); |
| 55 | } |
| 56 | const bound = { |
| 57 | value: yield* token.value, |
| 58 | } satisfies Token; |
| 59 | const zoneId = yield* zone.zoneId; |
| 60 | return options.makeClient(makeDnsAuth(bound), zoneId); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | /** |
| 65 | * Injectable auth for the DNS HTTP client builders. Both the scoped-token |
| 66 | * (`*Http`) and current-credentials (`*Local`) variants supply an `authorize` |
| 67 | * that provides `Credentials` + `HttpClient` to a raw SDK op, so the client |
| 68 | * builders are agnostic to how creds are obtained. DNS record ops are |
| 69 | * zone-scoped (the `zoneId` is passed alongside), so no `accountId` is needed. |
| 70 | */ |
| 71 | export interface DnsAuth { |
| 72 | authorize: <A, E>( |
| 73 | eff: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>, |
| 74 | ) => Effect.Effect<A, E, RuntimeContext>; |
| 75 | } |
| 76 | |
| 77 | /** Build a scoped-token {@link DnsAuth} from a bound {@link Token}. */ |
| 78 | export const makeDnsAuth = (token: Token): DnsAuth => ({ |
| 79 | authorize: authorizeWith(token), |
| 80 | }); |
| 81 | |
| 82 | /** |
| 83 | * Runtime accessor for a DNS binding's token, obtained by binding the |
| 84 | * {@link AccountApiToken}'s `value` output in the Worker's Init phase. Reads the |
| 85 | * value back from the Worker's environment at runtime. DNS record operations |
| 86 | * are zone-scoped (the `zoneId` is passed per call), so the account id is not |
| 87 | * needed at runtime. |
| 88 | */ |
| 89 | export interface Token { |
| 90 | /** The token's plaintext value (injected as a `secret_text` binding). */ |
| 91 | value: Effect.Effect<Redacted.Redacted<string>>; |
| 92 | } |