fix: clear provider auth entries on delete (#616)
This commit is contained in:
@@ -8,6 +8,27 @@ import { logger } from '../../services/logger'
|
||||
|
||||
const OPTIONAL_API_KEY_PROVIDERS = new Set(['cliproxyapi'])
|
||||
|
||||
async function clearStoredAuthProvider(poolKey: string) {
|
||||
try {
|
||||
const authPath = getActiveAuthPath()
|
||||
if (!existsSync(authPath)) return
|
||||
|
||||
const auth = JSON.parse(readFileSync(authPath, 'utf-8'))
|
||||
let changed = false
|
||||
if (auth.providers && Object.prototype.hasOwnProperty.call(auth.providers, poolKey)) {
|
||||
delete auth.providers[poolKey]
|
||||
changed = true
|
||||
}
|
||||
if (auth.credential_pool && Object.prototype.hasOwnProperty.call(auth.credential_pool, poolKey)) {
|
||||
delete auth.credential_pool[poolKey]
|
||||
changed = true
|
||||
}
|
||||
if (changed) {
|
||||
await writeFile(authPath, JSON.stringify(auth, null, 2) + '\n', 'utf-8')
|
||||
}
|
||||
} catch (err: any) { logger.error(err, 'Failed to clear auth credentials for %s', poolKey) }
|
||||
}
|
||||
|
||||
function buildProviderEntry(name: string, base_url: string, api_key: string, model: string, context_length?: number) {
|
||||
const entry: any = { name, base_url, api_key, model }
|
||||
if (context_length && context_length > 0) {
|
||||
@@ -150,24 +171,16 @@ export async function remove(ctx: any) {
|
||||
if (idx === -1) {
|
||||
ctx.status = 404; ctx.body = { error: `Custom provider "${poolKey}" not found` }; return
|
||||
}
|
||||
(config.custom_providers as any[]).splice(idx, 1)
|
||||
;(config.custom_providers as any[]).splice(idx, 1)
|
||||
await writeConfigYaml(config)
|
||||
await clearStoredAuthProvider(poolKey)
|
||||
} else {
|
||||
const envMapping = PROVIDER_ENV_MAP[poolKey]
|
||||
if (envMapping?.api_key_env) {
|
||||
await saveEnvValue(envMapping.api_key_env, '')
|
||||
if (envMapping.base_url_env) { await saveEnvValue(envMapping.base_url_env, '') }
|
||||
} else if (!envMapping?.api_key_env) {
|
||||
try {
|
||||
const authPath = getActiveAuthPath()
|
||||
if (existsSync(authPath)) {
|
||||
const auth = JSON.parse(readFileSync(authPath, 'utf-8'))
|
||||
if (auth.providers?.[poolKey]) { delete auth.providers[poolKey] }
|
||||
if (auth.credential_pool?.[poolKey]) { delete auth.credential_pool[poolKey] }
|
||||
await writeFile(authPath, JSON.stringify(auth, null, 2) + '\n', 'utf-8')
|
||||
}
|
||||
} catch (err: any) { logger.error(err, 'Failed to clear OAuth tokens for %s', poolKey) }
|
||||
}
|
||||
await clearStoredAuthProvider(poolKey)
|
||||
}
|
||||
const currentProvider = config.model?.provider
|
||||
if (currentProvider === poolKey) {
|
||||
|
||||
Reference in New Issue
Block a user