The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Context from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Redacted from "effect/Redacted"; |
| 6 | import * as Schema from "effect/Schema"; |
| 7 | import path from "pathe"; |
| 8 | import { writeFileAtomic } from "../Util/AtomicFile.ts"; |
| 9 | import { profileCommandHint } from "../Util/interactive.ts"; |
| 10 | import { AuthError } from "./AuthProvider.ts"; |
| 11 | import { profileCredentialsDirPath } from "./Paths.ts"; |
| 12 | import { validateProfileName } from "./Profile.ts"; |
| 13 | |
| 14 | export const credentialsFilePath = (profile: string, provider: string) => |
| 15 | path.join(profileCredentialsDirPath(profile), `${provider}.json`); |
| 16 | |
| 17 | /** |
| 18 | * Service exposing per-profile credential file helpers. All methods have |
| 19 | * `R = never` — the {@link FileSystem.FileSystem} requirement is captured |
| 20 | * by {@link CredentialsStoreLive} when the layer is built. |
| 21 | * |
| 22 | * Reads and writes go through the provider's declared credential schema, so |
| 23 | * a hand-edited or corrupted secrets file surfaces as a typed `AuthError` |
| 24 | * (with a reconfigure hint) instead of propagating malformed data into API |
| 25 | * calls. |
| 26 | */ |
| 27 | export interface CredentialsStoreService { |
| 28 | readonly read: <A, E>( |
| 29 | profile: string, |
| 30 | provider: string, |
| 31 | schema: Schema.Codec<A, E>, |
| 32 | ) => Effect.Effect<A | undefined, AuthError>; |
| 33 | readonly write: <A, E>( |
| 34 | profile: string, |
| 35 | provider: string, |
| 36 | schema: Schema.Codec<A, E>, |
| 37 | credentials: A, |
| 38 | ) => Effect.Effect<void, AuthError>; |
| 39 | readonly delete: (profile: string, provider: string) => Effect.Effect<void, AuthError>; |
| 40 | /** |
| 41 | * Recursively remove the `~/.alchemy/credentials/{profile}` directory |
| 42 | * containing all per-provider secrets for `profile`. No-op if it doesn't exist. |
| 43 | */ |
| 44 | readonly deleteProfile: (profile: string) => Effect.Effect<void, AuthError>; |
| 45 | /** |
| 46 | * The same store rooted at `dirFor(profile)` instead of |
| 47 | * `~/.alchemy/credentials/{profile}` — for caches kept beside a profile |
| 48 | * rather than with its credentials. |
| 49 | */ |
| 50 | readonly scoped: (dirFor: (profile: string) => string) => CredentialsStoreService; |
| 51 | } |
| 52 | |
| 53 | export class CredentialsStore extends Context.Service<CredentialsStore, CredentialsStoreService>()( |
| 54 | "Alchemy::CredentialsStore", |
| 55 | ) {} |
| 56 | |
| 57 | export const CredentialsStoreLive = Layer.effect( |
| 58 | CredentialsStore, |
| 59 | Effect.gen(function* () { |
| 60 | const fs = yield* FileSystem.FileSystem; |
| 61 | |
| 62 | const make = (dirFor: (profile: string) => string): CredentialsStoreService => { |
| 63 | const validateCredentialPath = validateCredentialPathIn(dirFor); |
| 64 | |
| 65 | const read = <A, E>(profile: string, provider: string, schema: Schema.Codec<A, E>) => |
| 66 | Effect.gen(function* () { |
| 67 | const filePath = yield* validateCredentialPath(profile, provider); |
| 68 | const data = yield* fs.readFileString(filePath).pipe( |
| 69 | Effect.catchReason("PlatformError", "NotFound", () => Effect.undefined), |
| 70 | Effect.mapError((cause) => |
| 71 | AuthError.make({ |
| 72 | message: `Could not read credentials at '${filePath}'.`, |
| 73 | cause, |
| 74 | }), |
| 75 | ), |
| 76 | ); |
| 77 | if (data === undefined) return undefined; |
| 78 | const json = yield* Schema.decodeEffect(Schema.fromJsonString(Schema.Unknown))(data).pipe( |
| 79 | Effect.mapError((cause) => |
| 80 | AuthError.make({ |
| 81 | message: `Stored credentials at '${filePath}' contain invalid JSON.`, |
| 82 | cause, |
| 83 | }), |
| 84 | ), |
| 85 | ); |
| 86 | const command = yield* profileCommandHint( |
| 87 | `alchemy profile edit --profile ${profile} --reconfigure ${provider}`, |
| 88 | ); |
| 89 | return yield* Schema.decodeUnknownEffect(schema)(json).pipe( |
| 90 | Effect.mapError((cause) => |
| 91 | AuthError.make({ |
| 92 | message: |
| 93 | `Stored credentials at '${filePath}' do not match the expected shape. ` + |
| 94 | `Run \`${command}\` to replace them.`, |
| 95 | cause, |
| 96 | }), |
| 97 | ), |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | const write = <A, E>( |
| 102 | profile: string, |
| 103 | provider: string, |
| 104 | schema: Schema.Codec<A, E>, |
| 105 | credentials: A, |
| 106 | ) => |
| 107 | Effect.gen(function* () { |
| 108 | const filePath = yield* validateCredentialPath(profile, provider); |
| 109 | const encoded = yield* Schema.encodeEffect(schema)(credentials).pipe( |
| 110 | Effect.mapError((cause) => |
| 111 | AuthError.make({ |
| 112 | message: `Credentials for '${provider}' do not match the declared schema.`, |
| 113 | cause, |
| 114 | }), |
| 115 | ), |
| 116 | ); |
| 117 | yield* Effect.gen(function* () { |
| 118 | const directory = path.dirname(filePath); |
| 119 | yield* fs.makeDirectory(directory, { recursive: true }); |
| 120 | yield* fs.chmod(directory, 0o700); |
| 121 | yield* writeFileAtomic(fs, filePath, JSON.stringify(encoded, null, 2), 0o600); |
| 122 | }).pipe( |
| 123 | Effect.mapError((cause) => |
| 124 | AuthError.make({ |
| 125 | message: `Could not write credentials at '${filePath}'.`, |
| 126 | cause, |
| 127 | }), |
| 128 | ), |
| 129 | ); |
| 130 | }); |
| 131 | |
| 132 | const remove_ = (profile: string, provider: string): Effect.Effect<void, AuthError> => |
| 133 | validateCredentialPath(profile, provider).pipe( |
| 134 | Effect.flatMap((filePath) => |
| 135 | fs.remove(filePath).pipe( |
| 136 | Effect.catchReason("PlatformError", "NotFound", () => Effect.void), |
| 137 | Effect.mapError((cause) => |
| 138 | AuthError.make({ |
| 139 | message: `Could not delete credentials at '${filePath}'.`, |
| 140 | cause, |
| 141 | }), |
| 142 | ), |
| 143 | ), |
| 144 | ), |
| 145 | ); |
| 146 | |
| 147 | const deleteProfile = (profile: string) => |
| 148 | Effect.gen(function* () { |
| 149 | yield* validateProfileName(profile).pipe( |
| 150 | Effect.mapError((cause) => AuthError.make({ message: cause.message, cause })), |
| 151 | ); |
| 152 | yield* fs.remove(dirFor(profile), { recursive: true }).pipe( |
| 153 | Effect.catchReason("PlatformError", "NotFound", () => Effect.void), |
| 154 | Effect.mapError((cause) => |
| 155 | AuthError.make({ |
| 156 | message: `Could not delete credentials for profile '${profile}'.`, |
| 157 | cause, |
| 158 | }), |
| 159 | ), |
| 160 | ); |
| 161 | }); |
| 162 | |
| 163 | return { |
| 164 | read, |
| 165 | write, |
| 166 | delete: remove_, |
| 167 | deleteProfile, |
| 168 | scoped: make, |
| 169 | } satisfies CredentialsStoreService; |
| 170 | }; |
| 171 | |
| 172 | return make(profileCredentialsDirPath); |
| 173 | }), |
| 174 | ); |
| 175 | |
| 176 | const CREDENTIAL_KEY_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/; |
| 177 | |
| 178 | const validateCredentialPathIn = |
| 179 | (dirFor: (profile: string) => string) => (profile: string, provider: string) => |
| 180 | Effect.gen(function* () { |
| 181 | yield* validateProfileName(profile).pipe( |
| 182 | Effect.mapError((cause) => AuthError.make({ message: cause.message, cause })), |
| 183 | ); |
| 184 | if (!CREDENTIAL_KEY_PATTERN.test(provider)) { |
| 185 | return yield* AuthError.make({ |
| 186 | message: `Invalid credential key '${provider}'.`, |
| 187 | }); |
| 188 | } |
| 189 | return path.join(dirFor(profile), `${provider}.json`); |
| 190 | }); |
| 191 | |
| 192 | export function displayRedacted(r: Redacted.Redacted<string>, visibleChars = 4): string { |
| 193 | const raw = Redacted.value(r); |
| 194 | if (raw.length <= visibleChars) return "****"; |
| 195 | return `${raw.slice(0, visibleChars)}****`; |
| 196 | } |