feat(installer): 三级凭证自动检测, 重装免输授权码

Phase 4 凭证加载优先级:
1. User 环境变量 (上次安装已永久写入) → 直接加载, 完全跳过授权码
2. Registry DPAPI 缓存 → 加载 + 写 User 环境变量
3. 授权码输入 → 解密 + 写 User 环境变量

解决: 用户重装/升级时不再被要求重新输入授权码,
旧凭证自动检测并沿用. "全自动检测+修复"闭环.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
bookworm 2026-04-10 11:07:54 +08:00
parent b58538bbae
commit d20d9d91a7

View File

@ -952,9 +952,25 @@ Log-Phase 4 "凭证解密"
$secretsDecrypted = $false $secretsDecrypted = $false
# 先查缓存 # 优先级 1: User 级环境变量已有 (上次安装已永久写入)
if (Get-CachedSecrets) { $existingKey = [System.Environment]::GetEnvironmentVariable("ANTHROPIC_API_KEY", "User")
Log-OK "从本日缓存加载凭证 (免密)" $existingUrl = [System.Environment]::GetEnvironmentVariable("ANTHROPIC_BASE_URL", "User")
if ($existingKey) {
# 注入到当前 Process (User 环境变量新终端才生效, 当前进程需手动加载)
$env:ANTHROPIC_API_KEY = $existingKey
if ($existingUrl) { $env:ANTHROPIC_BASE_URL = $existingUrl }
# 加载其他 Key (如果有)
foreach ($k in $CacheAllowedKeys) {
$v = [System.Environment]::GetEnvironmentVariable($k, "User")
if ($v) { [System.Environment]::SetEnvironmentVariable($k, $v, "Process") }
}
Log-OK "从系统环境变量加载凭证 (已有安装记录, 免输授权码)"
$secretsDecrypted = $true
}
# 优先级 2: Registry DPAPI 缓存
if (-not $secretsDecrypted -and (Get-CachedSecrets)) {
Log-OK "从 Registry 缓存加载凭证"
$secretsDecrypted = $true $secretsDecrypted = $true
} }
# 再解密 (缓存命中则跳过) # 再解密 (缓存命中则跳过)