File size: 9,324 Bytes
7d9eccd 064bfd6 7d9eccd 064bfd6 7d9eccd 064bfd6 7d9eccd 064bfd6 7d9eccd 064bfd6 eb16a76 64efe5d eb16a76 064bfd6 7d9eccd 64efe5d eb16a76 7d9eccd 064bfd6 7d9eccd 064bfd6 64efe5d eb16a76 064bfd6 | 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | import { useCallback, useEffect, useState } from 'react'
import { getIsNonInteractiveSession } from '../bootstrap/state.js'
import { verifyApiKey } from '../services/api/claude.js'
import { getGlobalConfig } from '../utils/config.js'
import {
getAnthropicApiKeyWithSource,
getApiKeyFromApiKeyHelper,
isAnthropicAuthEnabled,
isClaudeAISubscriber,
getConfiguredAuthProvider,
getConfiguredAuthProviderFromFile,
getOpenAIAuthTokens,
getLocalBaseUrl,
getLocalModelName,
} from '../utils/auth.js'
import { useAppState } from '../state/AppState.js'
export type VerificationStatus =
| 'loading'
| 'valid'
| 'invalid'
| 'missing'
| 'error'
export type ApiKeyVerificationResult = {
status: VerificationStatus
reverify: () => Promise<void>
error: Error | null
}
export function useApiKeyVerification(): ApiKeyVerificationResult {
const [status, setStatus] = useState<VerificationStatus>(() => {
const authProvider = getConfiguredAuthProviderFromFile()
// Check Local authentication
if (authProvider === 'local') {
const baseUrl = getLocalBaseUrl()
const modelName = getLocalModelName()
if (baseUrl && modelName) {
return 'valid'
}
return 'missing'
}
// Check OpenAI authentication
if (authProvider === 'openai') {
const config = getGlobalConfig()
const hasApiKey = !!config.openAiApiKey
const hasAccessToken = !!config.openAiAccessToken
if (hasApiKey || hasAccessToken) {
return 'valid'
}
return 'missing'
}
// Check OpenRouter authentication
if (authProvider === 'openrouter') {
const config = getGlobalConfig()
const hasApiKey = !!config.openRouterApiKey
if (hasApiKey) {
return 'valid'
}
return 'missing'
}
// Check OpenCode Zen authentication
// OpenCode Zen uses free models by default, no API key required
if (authProvider === 'opencode') {
return 'valid'
}
// Check NVIDIA authentication
if (authProvider === 'nvidia') {
const config = getGlobalConfig()
const hasApiKey = !!config.nvidiaApiKey
if (hasApiKey) {
return 'valid'
}
return 'missing'
}
// Anthropic authentication
if (!isAnthropicAuthEnabled() || isClaudeAISubscriber()) {
return 'valid'
}
// Use skipRetrievingKeyFromApiKeyHelper to avoid executing apiKeyHelper
// before trust dialog is shown (security: prevents RCE via settings.json)
const { key, source } = getAnthropicApiKeyWithSource({
skipRetrievingKeyFromApiKeyHelper: true,
})
// If apiKeyHelper is configured, we have a key source even though we
// haven't executed it yet - return 'loading' to indicate we'll verify later
if (key || source === 'apiKeyHelper') {
return 'loading'
}
return 'missing'
})
const [error, setError] = useState<Error | null>(null)
// Watch authVersion changes to re-verify API key
const authVersion = useAppState(s => s.authVersion)
useEffect(() => {
// When authVersion changes, re-verify the API key
const reverify = async (): Promise<void> => {
const authProvider = getConfiguredAuthProviderFromFile()
console.log('[useApiKeyVerification] authVersion changed, authProvider:', authProvider)
// Check Local authentication
if (authProvider === 'local') {
// Force re-read config by reading directly from file
const baseUrl = getLocalBaseUrl()
const modelName = getLocalModelName()
console.log('[useApiKeyVerification] Local - baseUrl:', baseUrl, 'modelName:', modelName)
if (baseUrl && modelName) {
setStatus('valid')
} else {
setStatus('missing')
}
return
}
// Check OpenAI authentication
if (authProvider === 'openai') {
const { getGlobalConfig: getConfig } = await import('../utils/config.js')
const freshConfig = getConfig()
const hasApiKey = !!freshConfig.openAiApiKey
const hasAccessToken = !!freshConfig.openAiAccessToken
if (hasApiKey || hasAccessToken) {
setStatus('valid')
} else {
setStatus('missing')
}
return
}
// Check OpenRouter authentication
if (authProvider === 'openrouter') {
const { getGlobalConfig: getConfig } = await import('../utils/config.js')
const freshConfig = getConfig()
const hasApiKey = !!freshConfig.openRouterApiKey
if (hasApiKey) {
setStatus('valid')
} else {
setStatus('missing')
}
return
}
// Check OpenCode Zen authentication
if (authProvider === 'opencode') {
setStatus('valid')
return
}
// Check NVIDIA authentication
if (authProvider === 'nvidia') {
const { getGlobalConfig: getConfig } = await import('../utils/config.js')
const freshConfig = getConfig()
const hasApiKey = !!freshConfig.nvidiaApiKey
if (hasApiKey) {
setStatus('valid')
} else {
setStatus('missing')
}
return
}
// Anthropic authentication
if (!isAnthropicAuthEnabled() || isClaudeAISubscriber()) {
setStatus('valid')
return
}
// Warm the apiKeyHelper cache (no-op if not configured), then read from
// all sources. getAnthropicApiKeyWithSource() reads the now-warm cache.
await getApiKeyFromApiKeyHelper(getIsNonInteractiveSession())
const { key: apiKey, source } = getAnthropicApiKeyWithSource()
if (!apiKey) {
if (source === 'apiKeyHelper') {
setStatus('error')
setError(new Error('API key helper did not return a valid key'))
return
}
const newStatus = 'missing'
setStatus(newStatus)
return
}
try {
const isValid = await verifyApiKey(apiKey, false)
const newStatus = isValid ? 'valid' : 'invalid'
setStatus(newStatus)
return
} catch (error) {
// This happens when there an error response from the API but it's not an invalid API key error
// In this case, we still mark the API key as invalid - but we also log the error so we can
// display it to the user to be more helpful
setError(error as Error)
const newStatus = 'error'
setStatus(newStatus)
return
}
}
void reverify()
}, [authVersion])
const verify = useCallback(async (): Promise<void> => {
const authProvider = getConfiguredAuthProviderFromFile()
// Check Local authentication
if (authProvider === 'local') {
const baseUrl = getLocalBaseUrl()
const modelName = getLocalModelName()
if (baseUrl && modelName) {
setStatus('valid')
} else {
setStatus('missing')
}
return
}
// Check OpenAI authentication
if (authProvider === 'openai') {
const config = getGlobalConfig()
const hasApiKey = !!config.openAiApiKey
const hasAccessToken = !!config.openAiAccessToken
if (hasApiKey || hasAccessToken) {
setStatus('valid')
} else {
setStatus('missing')
}
return
}
// Check OpenRouter authentication
if (authProvider === 'openrouter') {
const config = getGlobalConfig()
const hasApiKey = !!config.openRouterApiKey
if (hasApiKey) {
setStatus('valid')
} else {
setStatus('missing')
}
return
}
// Check OpenCode Zen authentication
if (authProvider === 'opencode') {
setStatus('valid')
return
}
// Check NVIDIA authentication
if (authProvider === 'nvidia') {
const config = getGlobalConfig()
const hasApiKey = !!config.nvidiaApiKey
if (hasApiKey) {
setStatus('valid')
} else {
setStatus('missing')
}
return
}
// Anthropic authentication
if (!isAnthropicAuthEnabled() || isClaudeAISubscriber()) {
setStatus('valid')
return
}
// Warm the apiKeyHelper cache (no-op if not configured), then read from
// all sources. getAnthropicApiKeyWithSource() reads the now-warm cache.
await getApiKeyFromApiKeyHelper(getIsNonInteractiveSession())
const { key: apiKey, source } = getAnthropicApiKeyWithSource()
if (!apiKey) {
if (source === 'apiKeyHelper') {
setStatus('error')
setError(new Error('API key helper did not return a valid key'))
return
}
const newStatus = 'missing'
setStatus(newStatus)
return
}
try {
const isValid = await verifyApiKey(apiKey, false)
const newStatus = isValid ? 'valid' : 'invalid'
setStatus(newStatus)
return
} catch (error) {
// This happens when there an error response from the API but it's not an invalid API key error
// In this case, we still mark the API key as invalid - but we also log the error so we can
// display it to the user to be more helpful
setError(error as Error)
const newStatus = 'error'
setStatus(newStatus)
return
}
}, [])
return {
status,
reverify: verify,
error,
}
}
|