forked from ProverCoderAI/docker-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu-auth-data.ts
More file actions
207 lines (187 loc) · 8.75 KB
/
menu-auth-data.ts
File metadata and controls
207 lines (187 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import * as FileSystem from "@effect/platform/FileSystem"
import * as Path from "@effect/platform/Path"
import { Effect, Match, pipe } from "effect"
import { ensureEnvFile, parseEnvEntries, readEnvText, upsertEnvKey } from "@effect-template/lib/usecases/env-file"
import { type AppError } from "@effect-template/lib/usecases/errors"
import { defaultProjectsRoot } from "@effect-template/lib/usecases/menu-helpers"
import { autoSyncState } from "@effect-template/lib/usecases/state-repo"
import { countAuthAccountEntries } from "./menu-auth-snapshot-builder.js"
import { buildLabeledEnvKey, countKeyEntries, normalizeLabel } from "./menu-labeled-env.js"
import type { AuthFlow, AuthSnapshot, MenuEnv } from "./menu-types.js"
export type AuthMenuAction = AuthFlow | "Refresh" | "Back"
type AuthMenuItem = {
readonly action: AuthMenuAction
readonly label: string
}
export type AuthEnvFlow = Extract<AuthFlow, "GithubRemove" | "GitSet" | "GitRemove">
export type AuthPromptStep = {
readonly key: "label" | "token" | "user" | "apiKey"
readonly label: string
readonly required: boolean
readonly secret: boolean
}
const authMenuItems: ReadonlyArray<AuthMenuItem> = [
{ action: "GithubOauth", label: "GitHub: login via OAuth (web)" },
{ action: "GithubRemove", label: "GitHub: remove token" },
{ action: "GitSet", label: "Git: add/update credentials" },
{ action: "GitRemove", label: "Git: remove credentials" },
{ action: "ClaudeOauth", label: "Claude Code: login via OAuth (web)" },
{ action: "ClaudeLogout", label: "Claude Code: logout (clear cache)" },
{ action: "GeminiOauth", label: "Gemini CLI: login via OAuth (Google account)" },
{ action: "GeminiApiKey", label: "Gemini CLI: set API key" },
{ action: "GeminiLogout", label: "Gemini CLI: logout (clear credentials)" },
{ action: "Refresh", label: "Refresh snapshot" },
{ action: "Back", label: "Back to main menu" }
]
const flowSteps: Readonly<Record<AuthFlow, ReadonlyArray<AuthPromptStep>>> = {
GithubOauth: [
{ key: "label", label: "Label (empty = default)", required: false, secret: false }
],
GithubRemove: [
{ key: "label", label: "Label to remove (empty = default)", required: false, secret: false }
],
GitSet: [
{ key: "label", label: "Label (empty = default)", required: false, secret: false },
{ key: "token", label: "Git auth token", required: true, secret: true },
{ key: "user", label: "Git auth user (empty = x-access-token)", required: false, secret: false }
],
GitRemove: [
{ key: "label", label: "Label to remove (empty = default)", required: false, secret: false }
],
ClaudeOauth: [
{ key: "label", label: "Label (empty = default)", required: false, secret: false }
],
ClaudeLogout: [
{ key: "label", label: "Label to logout (empty = default)", required: false, secret: false }
],
GeminiOauth: [
{ key: "label", label: "Label (empty = default)", required: false, secret: false }
],
GeminiApiKey: [
{ key: "label", label: "Label (empty = default)", required: false, secret: false },
{ key: "apiKey", label: "Gemini API key (from ai.google.dev)", required: true, secret: true }
],
GeminiLogout: [
{ key: "label", label: "Label to logout (empty = default)", required: false, secret: false }
]
}
const flowTitle = (flow: AuthFlow): string =>
Match.value(flow).pipe(
Match.when("GithubOauth", () => "GitHub OAuth"),
Match.when("GithubRemove", () => "GitHub remove"),
Match.when("GitSet", () => "Git credentials"),
Match.when("GitRemove", () => "Git remove"),
Match.when("ClaudeOauth", () => "Claude Code OAuth"),
Match.when("ClaudeLogout", () => "Claude Code logout"),
Match.when("GeminiOauth", () => "Gemini CLI OAuth"),
Match.when("GeminiApiKey", () => "Gemini CLI API key"),
Match.when("GeminiLogout", () => "Gemini CLI logout"),
Match.exhaustive
)
export const successMessage = (flow: AuthFlow, label: string): string =>
Match.value(flow).pipe(
Match.when("GithubOauth", () => `Saved GitHub token (${label}).`),
Match.when("GithubRemove", () => `Removed GitHub token (${label}).`),
Match.when("GitSet", () => `Saved Git credentials (${label}).`),
Match.when("GitRemove", () => `Removed Git credentials (${label}).`),
Match.when("ClaudeOauth", () => `Saved Claude Code login (${label}).`),
Match.when("ClaudeLogout", () => `Logged out Claude Code (${label}).`),
Match.when("GeminiOauth", () => `Saved Gemini CLI OAuth login (${label}).`),
Match.when("GeminiApiKey", () => `Saved Gemini API key (${label}).`),
Match.when("GeminiLogout", () => `Logged out Gemini CLI (${label}).`),
Match.exhaustive
)
const buildGlobalEnvPath = (cwd: string): string => `${defaultProjectsRoot(cwd)}/.orch/env/global.env`
const buildClaudeAuthPath = (cwd: string): string => `${defaultProjectsRoot(cwd)}/.orch/auth/claude`
const buildGeminiAuthPath = (cwd: string): string => `${defaultProjectsRoot(cwd)}/.orch/auth/gemini`
type AuthEnvText = {
readonly fs: FileSystem.FileSystem
readonly path: Path.Path
readonly globalEnvPath: string
readonly claudeAuthPath: string
readonly geminiAuthPath: string
readonly envText: string
}
const loadAuthEnvText = (
cwd: string
): Effect.Effect<AuthEnvText, AppError, MenuEnv> =>
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const globalEnvPath = buildGlobalEnvPath(cwd)
const claudeAuthPath = buildClaudeAuthPath(cwd)
const geminiAuthPath = buildGeminiAuthPath(cwd)
yield* _(ensureEnvFile(fs, path, globalEnvPath))
const envText = yield* _(readEnvText(fs, globalEnvPath))
return { fs, path, globalEnvPath, claudeAuthPath, geminiAuthPath, envText }
})
export const readAuthSnapshot = (
cwd: string
): Effect.Effect<AuthSnapshot, AppError, MenuEnv> =>
pipe(
loadAuthEnvText(cwd),
Effect.flatMap(({ claudeAuthPath, envText, fs, geminiAuthPath, globalEnvPath, path }) =>
countAuthAccountEntries(fs, path, claudeAuthPath, geminiAuthPath).pipe(
Effect.map(({ claudeAuthEntries, geminiAuthEntries }) => ({
globalEnvPath,
claudeAuthPath,
geminiAuthPath,
totalEntries: parseEnvEntries(envText).filter((entry) => entry.value.trim().length > 0).length,
githubTokenEntries: countKeyEntries(envText, "GITHUB_TOKEN"),
gitTokenEntries: countKeyEntries(envText, "GIT_AUTH_TOKEN"),
gitUserEntries: countKeyEntries(envText, "GIT_AUTH_USER"),
claudeAuthEntries,
geminiAuthEntries
}))
)
)
)
export const writeAuthFlow = (
cwd: string,
flow: AuthEnvFlow,
values: Readonly<Record<string, string>>
): Effect.Effect<void, AppError, MenuEnv> =>
pipe(
loadAuthEnvText(cwd),
Effect.flatMap(({ envText, fs, globalEnvPath }) => {
const label = values["label"] ?? ""
const canonicalLabel = (() => {
const normalized = normalizeLabel(label)
return normalized.length === 0 || normalized === "DEFAULT" ? "default" : normalized
})()
const token = (values["token"] ?? "").trim()
const user = (values["user"] ?? "").trim()
const nextText = Match.value(flow).pipe(
Match.when("GithubRemove", () => upsertEnvKey(envText, buildLabeledEnvKey("GITHUB_TOKEN", label), "")),
Match.when("GitSet", () => {
const withToken = upsertEnvKey(envText, buildLabeledEnvKey("GIT_AUTH_TOKEN", label), token)
const resolvedUser = user.length > 0 ? user : "x-access-token"
return upsertEnvKey(withToken, buildLabeledEnvKey("GIT_AUTH_USER", label), resolvedUser)
}),
Match.when("GitRemove", () => {
const withoutToken = upsertEnvKey(envText, buildLabeledEnvKey("GIT_AUTH_TOKEN", label), "")
return upsertEnvKey(withoutToken, buildLabeledEnvKey("GIT_AUTH_USER", label), "")
}),
Match.exhaustive
)
const syncMessage = Match.value(flow).pipe(
Match.when("GithubRemove", () => `chore(state): auth gh logout ${canonicalLabel}`),
Match.when("GitSet", () => `chore(state): auth git ${canonicalLabel}`),
Match.when("GitRemove", () => `chore(state): auth git logout ${canonicalLabel}`),
Match.exhaustive
)
return pipe(
fs.writeFileString(globalEnvPath, nextText),
Effect.zipRight(autoSyncState(syncMessage))
)
}),
Effect.asVoid
)
export const authViewTitle = (flow: AuthFlow): string => flowTitle(flow)
export const authViewSteps = (flow: AuthFlow): ReadonlyArray<AuthPromptStep> => flowSteps[flow]
export const authMenuLabels = (): ReadonlyArray<string> => authMenuItems.map((item) => item.label)
export const authMenuActionByIndex = (index: number): AuthMenuAction | null => {
const item = authMenuItems[index]
return item ? item.action : null
}
export const authMenuSize = (): number => authMenuItems.length