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, 04:45 UTC
| 1 | import { |
| 2 | requireSettingsSecretLiteral, |
| 3 | type SettingsSecretUnresolvedError, |
| 4 | } from "@t3tools/contracts"; |
| 5 | import { normalizeHost } from "@t3tools/source-control-core/server/pullRequestUtils"; |
| 6 | import * as Cache from "effect/Cache"; |
| 7 | import * as Context from "effect/Context"; |
| 8 | import * as Duration from "effect/Duration"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Exit from "effect/Exit"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import * as PlatformError from "effect/PlatformError"; |
| 14 | import * as Redacted from "effect/Redacted"; |
| 15 | import * as Schema from "effect/Schema"; |
| 16 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 17 | import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 18 | import { readSourceControlHostSettings } from "@t3tools/source-control-core/shared/definition"; |
| 19 | |
| 20 | import * as GitCafeClient from "../shared/definition.ts"; |
| 21 | |
| 22 | const TOKEN_TTL = Duration.minutes(5); |
| 23 | const MISSING_TTL = Duration.seconds(10); |
| 24 | |
| 25 | export interface GitCafeCredential { |
| 26 | readonly host: string; |
| 27 | readonly token: Redacted.Redacted<string>; |
| 28 | readonly source: "settings" | "env" | "cafe"; |
| 29 | } |
| 30 | |
| 31 | export class GitCafeCliMissingError extends Schema.TaggedError<GitCafeCliMissingError>()( |
| 32 | "GitCafeCliMissingError", |
| 33 | { host: Schema.String }, |
| 34 | ) { |
| 35 | override get message(): string { |
| 36 | return `No GitCafe credential for ${this.host}: set CAFE_TOKEN, or install the GitCafe CLI and run \`cafe auth login\`.`; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | export class GitCafeNotSignedInError extends Schema.TaggedError<GitCafeNotSignedInError>()( |
| 41 | "GitCafeNotSignedInError", |
| 42 | { host: Schema.String }, |
| 43 | ) { |
| 44 | override get message(): string { |
| 45 | return `No GitCafe credential for ${this.host}: run \`cafe auth login --host https://${this.host}/api\`.`; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | export class GitCafeCliFailedError extends Schema.TaggedError<GitCafeCliFailedError>()( |
| 50 | "GitCafeCliFailedError", |
| 51 | { host: Schema.String, cause: Schema.Defect() }, |
| 52 | ) { |
| 53 | override get message(): string { |
| 54 | return `The GitCafe CLI could not hand over a credential for ${this.host}. Check \`cafe auth status\` on the server.`; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | export class GitCafeCliTimeoutError extends Schema.TaggedError<GitCafeCliTimeoutError>()( |
| 59 | "GitCafeCliTimeoutError", |
| 60 | { host: Schema.String, timeoutMs: Schema.Number, cause: Schema.Defect() }, |
| 61 | ) { |
| 62 | override get message(): string { |
| 63 | return `The GitCafe CLI did not hand over a credential for ${this.host} within ${this.timeoutMs / 1000} seconds. The server may be overloaded; try again.`; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | export type GitCafeCredentialUnavailableError = |
| 68 | | GitCafeCliMissingError |
| 69 | | GitCafeNotSignedInError |
| 70 | | GitCafeCliFailedError |
| 71 | | GitCafeCliTimeoutError |
| 72 | | SettingsSecretUnresolvedError; |
| 73 | |
| 74 | export class GitCafeCredentials extends Context.Service< |
| 75 | GitCafeCredentials, |
| 76 | { |
| 77 | readonly get: ( |
| 78 | host: string, |
| 79 | ) => Effect.Effect<GitCafeCredential, GitCafeCredentialUnavailableError>; |
| 80 | readonly invalidate: (host: string) => Effect.Effect<void>; |
| 81 | readonly cliEnv: ( |
| 82 | host: string, |
| 83 | ) => Effect.Effect<Readonly<Record<string, string>>, SettingsSecretUnresolvedError>; |
| 84 | } |
| 85 | >()("@t3tools/source-control-gitcafe/server/GitCafeCredentials") {} |
| 86 | |
| 87 | const DEFAULT_HOST = "git.cafe"; |
| 88 | |
| 89 | /** Restrict CAFE_TOKEN to CAFE_HOST (default git.cafe), including for staging remotes. */ |
| 90 | export function environmentToken( |
| 91 | host: string, |
| 92 | env: Readonly<Record<string, string | undefined>>, |
| 93 | ): string | null { |
| 94 | const configured = env.CAFE_HOST?.trim(); |
| 95 | const configuredHost = configured |
| 96 | ? URL.parse(configured.includes("://") ? configured : `https://${configured}`)?.host |
| 97 | : DEFAULT_HOST; |
| 98 | if (configuredHost?.toLowerCase() !== normalizeHost(host)) return null; |
| 99 | return env.CAFE_TOKEN?.trim() || null; |
| 100 | } |
| 101 | |
| 102 | function credentialPassword(output: string): string | null { |
| 103 | for (const line of output.split(/\r?\n/u)) { |
| 104 | if (line.startsWith("password=")) return line.slice("password=".length).trim() || null; |
| 105 | } |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | /** @public */ |
| 110 | export const make = Effect.gen(function* () { |
| 111 | const sourceControlHost = yield* SourceControlHost.SourceControlHost; |
| 112 | const { process } = sourceControlHost; |
| 113 | const environment = yield* HostProcess.Environment; |
| 114 | const workingDirectory = yield* HostProcess.WorkingDirectory; |
| 115 | |
| 116 | /** cafe has no auth-token command; its credential helper keeps tokens out of argv/logs. */ |
| 117 | const fromCafe = (host: string) => |
| 118 | process |
| 119 | .run({ |
| 120 | operation: "GitCafeCredentials.get", |
| 121 | command: "cafe", |
| 122 | args: ["auth", "http", "credential", "--host", `https://${host}/api`, "get"], |
| 123 | cwd: workingDirectory, |
| 124 | stdin: `protocol=https\nhost=${host}\n\n`, |
| 125 | env: { CAFE_NO_UPDATE_CHECK: "1" }, |
| 126 | timeoutMs: 10_000, |
| 127 | }) |
| 128 | .pipe( |
| 129 | Effect.mapError((error) => |
| 130 | error._tag === "VcsProcessSpawnError" && |
| 131 | error.cause instanceof PlatformError.PlatformError && |
| 132 | error.cause.reason._tag === "NotFound" |
| 133 | ? new GitCafeCliMissingError({ host }) |
| 134 | : error._tag === "VcsProcessExitError" |
| 135 | ? new GitCafeNotSignedInError({ host }) |
| 136 | : error._tag === "VcsProcessTimeoutError" |
| 137 | ? new GitCafeCliTimeoutError({ host, timeoutMs: error.timeoutMs, cause: error }) |
| 138 | : new GitCafeCliFailedError({ host, cause: error }), |
| 139 | ), |
| 140 | Effect.map((output) => credentialPassword(output.stdout)), |
| 141 | Effect.filterOrFail( |
| 142 | (token): token is string => token !== null, |
| 143 | () => new GitCafeNotSignedInError({ host }), |
| 144 | ), |
| 145 | ); |
| 146 | |
| 147 | const fromSettings = (host: string) => |
| 148 | normalizeHost(host) !== DEFAULT_HOST |
| 149 | ? Effect.succeed(null) |
| 150 | : sourceControlHost.settings.getWithSecrets(GitCafeClient.definition.kind).pipe( |
| 151 | Effect.map((settings) => ({ |
| 152 | token: readSourceControlHostSettings( |
| 153 | GitCafeClient.settings, |
| 154 | settings.sourceControlHosts[GitCafeClient.definition.kind], |
| 155 | ).token, |
| 156 | secretProviders: settings.secretProviders, |
| 157 | })), |
| 158 | Effect.orElseSucceed(() => ({ token: undefined, secretProviders: {} })), |
| 159 | Effect.flatMap(({ token, secretProviders }) => |
| 160 | requireSettingsSecretLiteral(token, secretProviders), |
| 161 | ), |
| 162 | Effect.map((token) => token || null), |
| 163 | ); |
| 164 | |
| 165 | const lookup = Effect.fn("GitCafeCredentials.lookup")(function* ( |
| 166 | host: string, |
| 167 | ): Effect.fn.Return<GitCafeCredential, GitCafeCredentialUnavailableError> { |
| 168 | const fromEnv = environmentToken(host, environment); |
| 169 | const token = fromEnv ?? (yield* fromCafe(host)); |
| 170 | return { host, token: Redacted.make(token), source: fromEnv !== null ? "env" : "cafe" }; |
| 171 | }); |
| 172 | |
| 173 | const cache = yield* Cache.makeWith(lookup, { |
| 174 | capacity: 4, |
| 175 | timeToLive: (exit) => |
| 176 | Exit.isSuccess(exit) |
| 177 | ? TOKEN_TTL |
| 178 | : Exit.findErrorOption(exit).pipe( |
| 179 | Option.exists( |
| 180 | (error) => |
| 181 | error._tag === "GitCafeCliFailedError" || error._tag === "GitCafeCliTimeoutError", |
| 182 | ), |
| 183 | ) |
| 184 | ? Duration.zero |
| 185 | : MISSING_TTL, |
| 186 | }); |
| 187 | |
| 188 | return GitCafeCredentials.of({ |
| 189 | get: Effect.fn("GitCafeCredentials.get")(function (host) { |
| 190 | return fromSettings(host).pipe( |
| 191 | Effect.flatMap( |
| 192 | (saved): Effect.Effect<GitCafeCredential, GitCafeCredentialUnavailableError> => |
| 193 | saved === null |
| 194 | ? Cache.get(cache, normalizeHost(host)) |
| 195 | : Effect.succeed({ |
| 196 | host: normalizeHost(host), |
| 197 | token: Redacted.make(saved), |
| 198 | source: "settings", |
| 199 | }), |
| 200 | ), |
| 201 | ); |
| 202 | }), |
| 203 | invalidate: Effect.fn("GitCafeCredentials.invalidate")(function (host) { |
| 204 | return Cache.invalidate(cache, normalizeHost(host)); |
| 205 | }), |
| 206 | cliEnv: Effect.fn("GitCafeCredentials.cliEnv")(function (host) { |
| 207 | return fromSettings(host).pipe( |
| 208 | Effect.map((saved) => (saved === null ? {} : { CAFE_TOKEN: saved })), |
| 209 | ); |
| 210 | }), |
| 211 | }); |
| 212 | }); |
| 213 | |
| 214 | export const layer = Layer.effect(GitCafeCredentials, make); |