The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Duration from "effect/Duration"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpClient from "effect/http/HttpClient"; |
| 4 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 5 | import * as Redacted from "effect/Redacted"; |
| 6 | import * as Schema from "effect/Schema"; |
| 7 | import { getEnv } from "./Env.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Platform-issued OIDC tokens, for exchanging with a secrets manager's OIDC |
| 11 | * identity auth (Doppler service account identities, Infisical machine |
| 12 | * identities) so CI never holds a long-lived credential. |
| 13 | * |
| 14 | * | Platform | Marker variable | Token source | |
| 15 | * | -------------- | ----------------- | ------------------------------------------ | |
| 16 | * | explicit | the caller's env | the variable itself | |
| 17 | * | Vercel | `VERCEL` | `VERCEL_OIDC_TOKEN` | |
| 18 | * | GitHub Actions | `GITHUB_ACTIONS` | `ACTIONS_ID_TOKEN_REQUEST_URL` + token | |
| 19 | * | GitLab CI | `GITLAB_CI` | `SIGSTORE_ID_TOKEN` from `id_tokens` | |
| 20 | * | GCP | `K_SERVICE` etc. | the metadata server's identity endpoint | |
| 21 | */ |
| 22 | export type OidcPlatform = "explicit" | "vercel" | "github-actions" | "gitlab" | "gcp"; |
| 23 | |
| 24 | export interface OidcToken { |
| 25 | readonly platform: OidcPlatform; |
| 26 | readonly token: Redacted.Redacted<string>; |
| 27 | } |
| 28 | |
| 29 | /** Which env vars a provider reserves for its explicit token and audience. */ |
| 30 | export interface OidcTokenEnv { |
| 31 | /** Explicit override for platforms that are not auto-detected. */ |
| 32 | readonly token: string; |
| 33 | /** Optional `aud` claim to request where the platform supports it. */ |
| 34 | readonly audience: string; |
| 35 | } |
| 36 | |
| 37 | /** The platforms {@link detectOidcToken} knows, for error messages. */ |
| 38 | export const SUPPORTED_OIDC_PLATFORMS = |
| 39 | "Vercel, GitHub Actions (needs `permissions: id-token: write`), GitLab CI (needs `id_tokens`), and GCP"; |
| 40 | |
| 41 | /** Metadata and internal endpoints answer fast or not at all. */ |
| 42 | const PROBE_TIMEOUT = Duration.seconds(5); |
| 43 | |
| 44 | const GitHubTokenResponse = Schema.Struct({ |
| 45 | value: Schema.optional(Schema.String), |
| 46 | }); |
| 47 | |
| 48 | /** |
| 49 | * One probe per platform, each gated on a variable the platform's runtime |
| 50 | * always sets, so a laptop never waits on a DNS lookup for an internal |
| 51 | * endpoint. A probe yields the token, or `undefined` when the platform is |
| 52 | * absent or has nothing to give. |
| 53 | */ |
| 54 | const PLATFORMS: ReadonlyArray<{ |
| 55 | readonly name: OidcPlatform; |
| 56 | readonly probe: ( |
| 57 | audience: string | undefined, |
| 58 | ) => Effect.Effect<Redacted.Redacted<string> | undefined, unknown, HttpClient.HttpClient>; |
| 59 | }> = [ |
| 60 | { |
| 61 | name: "vercel", |
| 62 | probe: Effect.fn("oidc.vercel")(function* () { |
| 63 | if (!(yield* getEnv("VERCEL"))) return undefined; |
| 64 | const token = yield* getEnv("VERCEL_OIDC_TOKEN"); |
| 65 | return token ? Redacted.make(token) : undefined; |
| 66 | }), |
| 67 | }, |
| 68 | { |
| 69 | // A per-job request URL and bearer, present only with |
| 70 | // `permissions: id-token: write` on the workflow or job. |
| 71 | name: "github-actions", |
| 72 | probe: Effect.fn("oidc.githubActions")(function* (audience) { |
| 73 | if (!(yield* getEnv("GITHUB_ACTIONS"))) return undefined; |
| 74 | const requestUrl = yield* getEnv("ACTIONS_ID_TOKEN_REQUEST_URL"); |
| 75 | const bearer = yield* getEnv("ACTIONS_ID_TOKEN_REQUEST_TOKEN"); |
| 76 | if (!requestUrl || !bearer) return undefined; |
| 77 | |
| 78 | const url = new URL(requestUrl); |
| 79 | if (audience !== undefined) url.searchParams.set("audience", audience); |
| 80 | const client = yield* HttpClient.HttpClient; |
| 81 | const response = yield* client.get(url, { |
| 82 | headers: { |
| 83 | Authorization: `Bearer ${bearer}`, |
| 84 | Accept: "application/json; api-version=2.0", |
| 85 | }, |
| 86 | }); |
| 87 | if (response.status !== 200) return undefined; |
| 88 | const body = yield* HttpClientResponse.schemaBodyJson(GitHubTokenResponse)(response); |
| 89 | return body.value ? Redacted.make(body.value) : undefined; |
| 90 | }), |
| 91 | }, |
| 92 | { |
| 93 | // The job's `id_tokens` block names the variable; these are the common ones. |
| 94 | name: "gitlab", |
| 95 | probe: Effect.fn("oidc.gitlab")(function* () { |
| 96 | if (!(yield* getEnv("GITLAB_CI"))) return undefined; |
| 97 | const token = yield* getEnv("SIGSTORE_ID_TOKEN"); |
| 98 | return token ? Redacted.make(token) : undefined; |
| 99 | }), |
| 100 | }, |
| 101 | { |
| 102 | // Cloud Run, Cloud Functions, and GCE: the metadata server signs |
| 103 | // identity tokens for the instance's service account. |
| 104 | name: "gcp", |
| 105 | probe: Effect.fn("oidc.gcp")(function* (audience) { |
| 106 | const onGcp = |
| 107 | (yield* getEnv("K_SERVICE")) || |
| 108 | (yield* getEnv("GCE_METADATA_HOST")) || |
| 109 | (yield* getEnv("GOOGLE_CLOUD_PROJECT")); |
| 110 | if (!onGcp) return undefined; |
| 111 | |
| 112 | const url = new URL( |
| 113 | "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity", |
| 114 | ); |
| 115 | if (audience !== undefined) url.searchParams.set("audience", audience); |
| 116 | const client = yield* HttpClient.HttpClient; |
| 117 | const response = yield* client.get(url, { |
| 118 | headers: { "Metadata-Flavor": "Google" }, |
| 119 | }); |
| 120 | if (response.status !== 200) return undefined; |
| 121 | const token = (yield* response.text).trim(); |
| 122 | return token ? Redacted.make(token) : undefined; |
| 123 | }), |
| 124 | }, |
| 125 | ]; |
| 126 | |
| 127 | /** |
| 128 | * Find the OIDC token of the platform this process is running on. The |
| 129 | * caller's explicit variable wins; otherwise platforms are probed in |
| 130 | * order and the first token wins. A probe that fails or hangs counts as |
| 131 | * "not this platform". `undefined` means nothing was detected. |
| 132 | */ |
| 133 | export const detectOidcToken = Effect.fn("detectOidcToken")(function* (env: OidcTokenEnv) { |
| 134 | const explicit = yield* getEnv(env.token); |
| 135 | if (explicit) { |
| 136 | const found: OidcToken = { |
| 137 | platform: "explicit", |
| 138 | token: Redacted.make(explicit), |
| 139 | }; |
| 140 | return found; |
| 141 | } |
| 142 | |
| 143 | const audience = yield* getEnv(env.audience); |
| 144 | for (const platform of PLATFORMS) { |
| 145 | const token = yield* platform.probe(audience).pipe( |
| 146 | Effect.timeout(PROBE_TIMEOUT), |
| 147 | Effect.orElseSucceed(() => undefined), |
| 148 | ); |
| 149 | if (token !== undefined) { |
| 150 | const found: OidcToken = { platform: platform.name, token }; |
| 151 | return found; |
| 152 | } |
| 153 | } |
| 154 | return undefined; |
| 155 | }); |