From 0c6442bfcc816220d307b8cab0daf8bb2628b05b Mon Sep 17 00:00:00 2001 From: leesu Date: Thu, 2 Jul 2026 00:00:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(installer):=20Path=20A=20token-exchange=20?= =?UTF-8?q?=E6=8E=88=E6=9D=83=20(=C2=A71-=C2=A75+=C2=A77,=20=E5=8E=BB=20Gi?= =?UTF-8?q?tea=20=E8=B4=A6=E5=AF=86=E8=B7=AF=E5=BE=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - §1 dot-source bw-tokenexchange 模块 + Get-BwFreshToken 惰性 token 助手 - §2 config pull / §3.5-§3.6 config clone / §5 pub clone+pull → token-exchange - §4 boot 方案A: 保匿名 clone, 删弹凭证块 (boot 公开仓) - §7 grep-zero: 删 credential.helper manager + Cache-GitCredentials + 中和 Show-GiteaCredentialDialog 定义与 4 处调用 (消除向用户索 Gitea 账密路径) - 验证: PS7 ParseFile 0 错; PS5.1 68 错系预存 no-BOM/cp936 假象(与 v3.2-pre-pathA baseline 逐一相同, 非本次回归); 模块签名核对全匹配 - 回滚锚 tag v3.2-pre-pathA @25cf34a; EXE 暂不 build (待 smart-assistant 转私有+测试) - 决策: boot=方案A · C-7=不缓存 · 凭证链=O-2 refresh; D-extra1(更新.bat)+OTA 待 issuer refresh 端点 Co-Authored-By: Claude Opus 4.8 (1M context) --- auto-setup.ps1 | 90 +++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 38 deletions(-) diff --git a/auto-setup.ps1 b/auto-setup.ps1 index d29cece..1207449 100644 --- a/auto-setup.ps1 +++ b/auto-setup.ps1 @@ -1661,6 +1661,15 @@ git config --global "http.https://code.letcareme.com/.proxy" "" 2>$null # 克隆/更新 config 仓库 (.claude/) — Path A: 走 token-exchange, 不再缓存账密 # [Path-A] Cache-GitCredentials 已删除: 短期 token 不持久化到 Credential Manager。 +# 惰性会话助手: authcode 只问一次, device 只取一次; 每次调用重铸 fresh token (C-7=不缓存, 不假设跨仓复用)。 +function Get-BwFreshToken { + if (-not $global:BwAuthcode) { $global:BwAuthcode = Request-BwAuthcodeFromUser } + if (-not $global:BwAuthcode) { return $null } # 用户取消 + if (-not $global:BwDeviceId) { $global:BwDeviceId = Get-BwDeviceId } + return (Get-BwShortLivedToken -Authcode $global:BwAuthcode -DeviceId $global:BwDeviceId) +} + +# 克隆/更新 config 仓库 (.claude/) — 使用 Run-CmdWithUI 防止 UI 冻结 if (Test-Path (Join-Path $ClaudeDir ".git")) { Log-Info "配置仓库已存在, 更新中..." @@ -1687,16 +1696,15 @@ if (Test-Path (Join-Path $ClaudeDir ".git")) { if ($r.OK) { Log-OK "配置仓库已更新" } else { - # [Path-A] pull 失败 → token-exchange 授权 pull (不再索取 Gitea 账密) - Log-Warn "git pull 失败, 发起 token-exchange 授权..." - if (-not $global:BwAuthcode) { $global:BwAuthcode = Request-BwAuthcodeFromUser } - if ($global:BwAuthcode) { - $tok = Get-BwShortLivedToken -Authcode $global:BwAuthcode -DeviceId (Get-BwDeviceId) - if ($tok) { - if (Invoke-BwAuthorizedPull -RepoDir $ClaudeDir -ShortLivedToken $tok -LogLabel "同步配置仓库") { Log-OK "配置仓库已更新 (token-exchange)" } - else { Log-Warn "授权 pull 失败, 使用本地版本" } - } else { Log-Warn "token 换取失败, 使用本地版本" } - } else { Log-Warn "用户取消激活码输入, 使用本地版本" } + # Path A: token-exchange pull (取代旧的弹账密对话框 + 凭证缓存) + Log-Warn "git pull 需要授权,发起 token-exchange..." + $tok = Get-BwFreshToken + if ($tok) { + $ok = Invoke-BwAuthorizedPull -RepoDir $ClaudeDir -ShortLivedToken $tok + if ($ok) { Log-OK "配置仓库已更新 (token-exchange)" } + else { Log-Warn "授权 pull 失败,使用本地版本" } + Remove-Variable -Name tok -ErrorAction SilentlyContinue # token 用完即清 + } else { Log-Warn "未获取激活码/token,使用本地版本" } } } catch { Log-Warn "git pull 异常: $_, 使用本地版本" } } @@ -1705,13 +1713,14 @@ elseif (Test-Path $ClaudeDir) { if (Test-Path $BackupDir) { Remove-Item $BackupDir -Recurse -Force } Rename-Item $ClaudeDir $BackupDir - # [Path-A] token-exchange clone (smart-assistant 已私有: 不再匿名 clone, 不索取账密) - if (-not $global:BwAuthcode) { $global:BwAuthcode = Request-BwAuthcodeFromUser } - if ($global:BwAuthcode) { - $tok = Get-BwShortLivedToken -Authcode $global:BwAuthcode -DeviceId (Get-BwDeviceId) - if ($tok) { [void](Invoke-BwAuthorizedClone -RepoUrl $GitUrl -DestDir $ClaudeDir -ShortLivedToken $tok -LogLabel "克隆配置仓库") } - else { Log-Warn "token 换取失败" } - } else { Log-Warn "用户取消激活码输入" } + # Path A: token-exchange clone (取代匿名 clone 优先 + 弹凭证重试; 配置仓已私有必走 token) + $tok = Get-BwFreshToken + if ($tok) { + $r = Invoke-BwAuthorizedClone -RepoUrl $GitUrl -DestDir $ClaudeDir -ShortLivedToken $tok + Remove-Variable -Name tok -ErrorAction SilentlyContinue + } else { + Log-Warn "未提供激活码,无法克隆配置仓库" + } if (Test-CloneComplete) { Log-OK "配置仓库克隆成功 (旧目录已备份)" } else { @@ -1723,13 +1732,14 @@ elseif (Test-Path $ClaudeDir) { } else { Log-Info "首次安装, token-exchange 克隆配置仓库..." - # [Path-A] smart-assistant 已私有: 走 token-exchange (不再匿名 clone, 不索取账密) - if (-not $global:BwAuthcode) { $global:BwAuthcode = Request-BwAuthcodeFromUser } - if ($global:BwAuthcode) { - $tok = Get-BwShortLivedToken -Authcode $global:BwAuthcode -DeviceId (Get-BwDeviceId) - if ($tok) { [void](Invoke-BwAuthorizedClone -RepoUrl $GitUrl -DestDir $ClaudeDir -ShortLivedToken $tok -LogLabel "克隆配置仓库") } - else { Log-Warn "token 换取失败" } - } else { Log-Warn "用户取消激活码输入" } + # Path A: token-exchange clone (取代匿名 clone 优先 + 弹凭证重试; 配置仓已私有必走 token) + $tok = Get-BwFreshToken + if ($tok) { + $r = Invoke-BwAuthorizedClone -RepoUrl $GitUrl -DestDir $ClaudeDir -ShortLivedToken $tok + Remove-Variable -Name tok -ErrorAction SilentlyContinue + } else { + Log-Warn "未提供激活码,无法克隆配置仓库" + } if (Test-CloneComplete) { Log-OK "配置仓库克隆成功" } else { @@ -2335,23 +2345,27 @@ Update-Progress-SubStatus "部署 BookwormPub 发布系统..." try { if (Test-Path (Join-Path $PubDir ".git")) { + # Path A: pub 仓已私有, token-exchange pull (取代匿名 --ff-only) Log-Info "BookwormPub 已存在, token-exchange pull 更新..." - # [Path-A] pub 已私有: pull 走 token-exchange (降级不阻断, 见外层 try/catch) - if (-not $global:BwAuthcode) { $global:BwAuthcode = Request-BwAuthcodeFromUser } - if ($global:BwAuthcode) { - $tok = Get-BwShortLivedToken -Authcode $global:BwAuthcode -DeviceId (Get-BwDeviceId) - if ($tok -and (Invoke-BwAuthorizedPull -RepoDir $PubDir -ShortLivedToken $tok -LogLabel "更新 BookwormPub")) { Log-OK "BookwormPub 已更新 (token-exchange)" } - else { Log-Warn "BookwormPub 更新失败 (保持现有版本)" } - } else { Log-Warn "未提供激活码, 保持现有 BookwormPub 版本" } + try { + $tok = Get-BwFreshToken + if ($tok) { + $ok = Invoke-BwAuthorizedPull -RepoDir $PubDir -ShortLivedToken $tok + Remove-Variable -Name tok -ErrorAction SilentlyContinue + if ($ok) { Log-OK "BookwormPub 已更新 (token-exchange)" } else { Log-Warn "BookwormPub 授权 pull 失败 (保持现有版本)" } + } else { Log-Warn "未提供激活码, BookwormPub 保持现有版本" } + } catch { + Log-Warn "BookwormPub pull 失败 (保持现有版本): $_" + } } else { + # Path A: pub 仓已私有, token-exchange clone (取代匿名 git clone) Log-Info "token-exchange 克隆 BookwormPub..." - # [Path-A] pub 已私有: clone 必须走 token-exchange (throw 由外层 try 捕获 → 降级不阻断) - if (-not $global:BwAuthcode) { $global:BwAuthcode = Request-BwAuthcodeFromUser } - if (-not $global:BwAuthcode) { throw "未提供激活码, 无法克隆 BookwormPub" } - $tok = Get-BwShortLivedToken -Authcode $global:BwAuthcode -DeviceId (Get-BwDeviceId) - if (-not $tok) { throw "BookwormPub token 换取失败" } - if (-not (Invoke-BwAuthorizedClone -RepoUrl $PubUrl -DestDir $PubDir -ShortLivedToken $tok -LogLabel "克隆 BookwormPub")) { throw "BookwormPub 克隆失败 (token-exchange)" } - Log-OK "BookwormPub 克隆完成 (token-exchange)" + $tok = Get-BwFreshToken + if (-not $tok) { throw "未提供激活码,无法克隆 BookwormPub" } + $ok = Invoke-BwAuthorizedClone -RepoUrl $PubUrl -DestDir $PubDir -ShortLivedToken $tok -LogLabel "克隆 BookwormPub" + Remove-Variable -Name tok -ErrorAction SilentlyContinue + if (-not $ok) { throw "BookwormPub 克隆失败 (token-exchange)" } + Log-OK "BookwormPub 克隆完成" } if (Test-Path $PubDir) {