提交记录修改docs/getting-started.md+6 −0看文件
修改internal/workbench/account.go+6 −2看文件
修改internal/workbench/account_device.go+1 −0看文件
修改internal/workbench/account_store.go+28 −13看文件
修改internal/workbench/desktop_gateway.go+4 −0看文件
修改internal/workbench/desktop_local.go+4 −0看文件
修改internal/workbench/server.go+2 −1看文件
新增internal/workbench/trusted_login.go+271 −0看文件
新增internal/workbench/trusted_login_test.go+171 −0看文件
修改internal/workbench/vault_store.go+3 −0看文件
修改ui/src/account.css+2 −0看文件
修改ui/src/account.jsx+86 −15看文件
| @@ -47,6 +47,12 @@ Windows 安装器中选择的是程序目录。如果希望镜像下载和虚拟 | ||
| 47 | 47 | |
| 48 | 48 | 首次安装和工具升级会自动选择下载来源,无需手动补缓存。中科大的 code-server 镜像只保留最近版本;Cage 固定的版本缺失时,会直接从 GitHub 下载该版本。下载仍经过当前选择的 Cage 代理。初始虚拟磁盘的容量上限为 24 GiB,下载流量与实际占用取决于架构、软件包版本和已有缓存。以后启动会复用已安装的环境。需要 Claude Desktop 时,在工作台单独安装,届时才访问 Anthropic 的软件源。 |
| 49 | 49 | |
| 50 | 新增:### 受信任的本机登录 | |
| 51 | 新增: | |
| 52 | 新增:首次在完整桌面版打开空配置,选择「信任此电脑并进入」,以后同一系统用户打开 Cage 可自动进入。也可以选择「使用用户名和密码」。已有密码账户沿用原来的登录方式,也可在「设置 → 账户 → 信任此电脑」验证原密码后开启自动登录;手动锁定仍需验证,退出账户会撤销此项信任。远程连接不能使用本机信任登录;Claude 服务仍需自己的授权。 | |
| 53 | 新增: | |
| 54 | 新增:受信任模式依赖操作系统的用户登录。离开电脑时请锁定系统。需要 Cage 独立锁屏、远程密码登录,或查看、复制保险箱密码时,在「设置 → 账户」设置密码即可转换;工作区内部密钥和已有数据继续使用。备份免账号工作区时,还需备份系统用户配置目录(Windows 使用 `%LOCALAPPDATA%`)下的 `Cage/trusted-local` 私有信任凭据,并以私有权限保存。 | |
| 55 | 新增: | |
| 50 | 56 | ## 2. 导入并选择代理 |
| 51 | 57 | |
| 52 | 58 | 进入工作台后点击“配置网络与代理”。之后也可以从左下角“设置 → 网络与代理”进入。没有节点时点击“添加来源”,选择一种导入方式: |
| @@ -5,7 +5,7 @@ import ( | ||
| 5 | 5 | ) |
| 6 | 6 | |
| 7 | 7 | func (s *Server) accountStatus(w http.ResponseWriter, r *http.Request) { |
| 8 | 删除: v := s.account.view() | |
| 8 | 新增: v := s.account.viewFor(s.hasTrustedView(r)) | |
| 9 | 9 | v.BackgroundRunning = s.vmRunning() |
| 10 | 10 | jsonReply(w, v) |
| 11 | 11 | } |
| @@ -21,11 +21,15 @@ func (s *Server) accountAction(w http.ResponseWriter, r *http.Request) { | ||
| 21 | 21 | return |
| 22 | 22 | } |
| 23 | 23 | if action != "create" && action != "unlock" && action != "device-unlock" { |
| 24 | 删除: if _, ok := s.account.access(); !ok { | |
| 24 | 新增: if _, ok := s.account.accessFor(s.hasTrustedView(r)); !ok { | |
| 25 | 25 | http.Error(w, "请先解锁 Cage", http.StatusLocked) |
| 26 | 26 | return |
| 27 | 27 | } |
| 28 | 28 | } |
| 29 | 新增: if (action == "lock" || action == "logout" || action == "device-enroll") && s.account.view().TrustedLocal { | |
| 30 | 新增: http.Error(w, "请先在本机的「设置 → 账户」设置密码", http.StatusConflict) | |
| 31 | 新增: return | |
| 32 | 新增: } | |
| 29 | 33 | var input struct { |
| 30 | 34 | Username string `json:"username"` |
| 31 | 35 | Password string `json:"password"` |
| @@ -136,6 +136,7 @@ func (a *localAccount) lockAccess(logout bool) error { | ||
| 136 | 136 | record.RequireAuthentication = true |
| 137 | 137 | if logout { |
| 138 | 138 | record.Devices = nil |
| 139 | 新增: record.TrustedUnlock = nil | |
| 139 | 140 | } |
| 140 | 141 | if err := writePrivate(a.path, record); err != nil { |
| 141 | 142 | return errors.New("无法保存账户锁定状态,请重试") |
| @@ -24,6 +24,8 @@ import ( | ||
| 24 | 24 | ) |
| 25 | 25 | |
| 26 | 26 | type accountRecord struct { |
| 27 | 新增: TrustedDevice string `json:"trusted_device,omitempty"` | |
| 28 | 新增: TrustedUnlock *accountDevice `json:"trusted_unlock,omitempty"` | |
| 27 | 29 | Version int `json:"version"` |
| 28 | 30 | Username string `json:"username"` |
| 29 | 31 | Avatar string `json:"avatar,omitempty"` |
| @@ -35,17 +37,20 @@ type accountRecord struct { | ||
| 35 | 37 | } |
| 36 | 38 | |
| 37 | 39 | type localAccount struct { |
| 38 | 删除: mu sync.Mutex | |
| 39 | 删除: path string | |
| 40 | 删除: record *accountRecord | |
| 41 | 删除: key []byte | |
| 42 | 删除: vaultLease string | |
| 43 | 删除: epoch context.Context | |
| 44 | 删除: cancel context.CancelFunc | |
| 45 | 删除: nextAttempt time.Time | |
| 40 | 新增: mu sync.Mutex | |
| 41 | 新增: path string | |
| 42 | 新增: record *accountRecord | |
| 43 | 新增: trustedSession bool | |
| 44 | 新增: key []byte | |
| 45 | 新增: vaultLease string | |
| 46 | 新增: epoch context.Context | |
| 47 | 新增: cancel context.CancelFunc | |
| 48 | 新增: nextAttempt time.Time | |
| 46 | 49 | } |
| 47 | 50 | |
| 48 | 51 | type accountView struct { |
| 52 | 新增: TrustedLocal bool `json:"trusted_local,omitempty"` | |
| 53 | 新增: TrustedLogin bool `json:"trusted_login,omitempty"` | |
| 49 | 54 | Exists bool `json:"exists"` |
| 50 | 55 | Unlocked bool `json:"unlocked"` |
| 51 | 56 | Username string `json:"username"` |
| @@ -69,9 +74,12 @@ func loadLocalAccount(profile string) (*localAccount, error) { | ||
| 69 | 74 | return nil, errors.New("本地账户文件权限不安全,请恢复私有文件权限") |
| 70 | 75 | } |
| 71 | 76 | var record accountRecord |
| 72 | 删除: if len(b) > 256<<10 || json.Unmarshal(b, &record) != nil || record.Version != 1 || len(record.Salt) != 16 || len(record.Nonce) != 12 || len(record.SealedKey) != 48 || validAccountName(record.Username) != nil { | |
| 77 | 新增: if len(b) > 256<<10 || json.Unmarshal(b, &record) != nil || record.Version != 1 || !validTrustedID(record.TrustedDevice) || len(record.Salt) != 16 || len(record.Nonce) != 12 || len(record.SealedKey) != 48 || validAccountName(record.Username) != nil { | |
| 73 | 78 | return nil, errors.New("本地账户文件损坏,请从备份恢复;已有数据未修改") |
| 74 | 79 | } |
| 80 | 新增: if device := record.TrustedUnlock; device != nil && (len(device.ID) != 64 || !validTrustedID(device.ID) || len(device.Nonce) != 12 || len(device.SealedKey) != 48 || record.TrustedDevice != "") { | |
| 81 | 新增: return nil, errors.New("本机信任记录损坏,请从备份恢复") | |
| 82 | 新增: } | |
| 75 | 83 | a.record = &record |
| 76 | 84 | for kind, device := range record.Devices { |
| 77 | 85 | if !validDeviceKind(kind) || len(device.ID) != 32 || len(device.Nonce) != 12 || len(device.SealedKey) != 48 { |
| @@ -133,15 +141,19 @@ func openAccountKey(record *accountRecord, password string) ([]byte, error) { | ||
| 133 | 141 | return key, nil |
| 134 | 142 | } |
| 135 | 143 | |
| 136 | 删除:func (a *localAccount) view() accountView { | |
| 144 | 新增:func (a *localAccount) view() accountView { return a.viewFor(true) } | |
| 145 | 新增: | |
| 146 | 新增:func (a *localAccount) viewFor(desktop bool) accountView { | |
| 137 | 147 | if a == nil { |
| 138 | 148 | return accountView{} |
| 139 | 149 | } |
| 140 | 150 | a.mu.Lock() |
| 141 | 151 | defer a.mu.Unlock() |
| 142 | 删除: v := accountView{Exists: a.record != nil, Unlocked: len(a.key) == 32} | |
| 152 | 新增: v := accountView{Exists: a.record != nil, Unlocked: len(a.key) == 32 && (!a.trustedSession || desktop)} | |
| 143 | 153 | v.DeviceBinding = fmtAccountBinding(a.path) |
| 144 | 154 | if a.record != nil { |
| 155 | 新增: v.TrustedLocal = a.record.TrustedDevice != "" | |
| 156 | 新增: v.TrustedLogin = a.record.TrustedUnlock != nil | |
| 145 | 157 | v.Username = a.record.Username |
| 146 | 158 | v.Avatar = a.record.Avatar |
| 147 | 159 | v.RequireAuthentication = a.record.RequireAuthentication |
| @@ -153,13 +165,15 @@ func (a *localAccount) view() accountView { | ||
| 153 | 165 | return v |
| 154 | 166 | } |
| 155 | 167 | |
| 156 | 删除:func (a *localAccount) access() (context.Context, bool) { | |
| 168 | 新增:func (a *localAccount) access() (context.Context, bool) { return a.accessFor(true) } | |
| 169 | 新增: | |
| 170 | 新增:func (a *localAccount) accessFor(desktop bool) (context.Context, bool) { | |
| 157 | 171 | if a == nil { |
| 158 | 172 | return nil, false |
| 159 | 173 | } |
| 160 | 174 | a.mu.Lock() |
| 161 | 175 | defer a.mu.Unlock() |
| 162 | 删除: return a.epoch, len(a.key) == 32 | |
| 176 | 新增: return a.epoch, len(a.key) == 32 && (!a.trustedSession || desktop) | |
| 163 | 177 | } |
| 164 | 178 | |
| 165 | 179 | func (a *localAccount) setKey(key []byte) { |
| @@ -168,6 +182,7 @@ func (a *localAccount) setKey(key []byte) { | ||
| 168 | 182 | } |
| 169 | 183 | clear(a.key) |
| 170 | 184 | a.key = key |
| 185 | 新增: a.trustedSession = false | |
| 171 | 186 | a.vaultLease = randomToken() |
| 172 | 187 | a.epoch, a.cancel = context.WithCancel(context.Background()) |
| 173 | 188 | } |
| @@ -465,6 +465,10 @@ func (d *desktopGateway) close() { | ||
| 465 | 465 | } |
| 466 | 466 | |
| 467 | 467 | func (d *desktopGateway) handle(path string, w http.ResponseWriter, r *http.Request) bool { |
| 468 | 新增: if path == "trusted-login" { | |
| 469 | 新增: d.trustedLogin(w, r) | |
| 470 | 新增: return true | |
| 471 | 新增: } | |
| 468 | 472 | if path == "local-status" && r.Method == http.MethodGet { |
| 469 | 473 | if d.local == nil { |
| 470 | 474 | jsonReply(w, DesktopLocalStatus{Known: true}) |
| @@ -307,6 +307,10 @@ func (d *desktopLocal) localSession(life, bootstrap context.Context, c *controll | ||
| 307 | 307 | return nil, errors.New("本机工作台版本或环境身份不符") |
| 308 | 308 | } |
| 309 | 309 | manifest := remotehost.PublicManifest{APIProtocol: m.APIProtocol, InstanceID: m.InstanceID, Version: m.Version, Platform: m.Platform, Runtime: m.Runtime, Capabilities: m.Capabilities} |
| 310 | 新增: c.client.Jar.SetCookies(u, []*http.Cookie{{ | |
| 311 | 新增: Name: cookieName + "_" + u.Port() + "-desktop", | |
| 312 | 新增: Value: trustedViewProof(c.record.Token), Path: "/", HttpOnly: true, SameSite: http.SameSiteStrictMode, | |
| 313 | 新增: }}) | |
| 310 | 314 | return remotehost.LocalSession(life, c.origin, manifest, c.client.Jar) |
| 311 | 315 | } |
| 312 | 316 | |
| @@ -264,6 +264,7 @@ func (s *Server) handler() http.Handler { | ||
| 264 | 264 | mux.HandleFunc("POST /api/handoff", s.handoffEntry) |
| 265 | 265 | } else { |
| 266 | 266 | mux.HandleFunc("POST /api/handoff", s.handoff) |
| 267 | 新增: mux.HandleFunc("POST /api/desktop-local/trusted-login", s.localTrustedLogin) | |
| 267 | 268 | mux.HandleFunc("POST /api/desktop-local/{action}", s.desktopLocalControl) |
| 268 | 269 | } |
| 269 | 270 | if s.desktopEntry { |
| @@ -410,7 +411,7 @@ func (s *Server) handler() http.Handler { | ||
| 410 | 411 | accountRoute := r.URL.Path == "/api/account" || strings.HasPrefix(r.URL.Path, "/api/account/") |
| 411 | 412 | bootstrapRoute := r.URL.Path == "/api/session" || r.URL.Path == "/api/launch" || r.URL.Path == "/api/handoff" || r.URL.Path == "/api/remote/manifest" |
| 412 | 413 | if strings.HasPrefix(r.URL.Path, "/api/") && !accountRoute && !bootstrapRoute && !strings.HasPrefix(r.URL.Path, "/api/diagnostics") && !(r.Method == "GET" && r.URL.Path == "/api/appearance") { |
| 413 | 删除: epoch, unlocked := s.account.access() | |
| 414 | 新增: epoch, unlocked := s.account.accessFor(s.hasTrustedView(r)) | |
| 414 | 415 | if !unlocked { |
| 415 | 416 | http.Error(w, "Cage 已锁定,请先登录", http.StatusLocked) |
| 416 | 417 | return |
| @@ -0,0 +1,271 @@ | ||
| 1 | 新增:package workbench | |
| 2 | 新增: | |
| 3 | 新增:import ( | |
| 4 | 新增: "crypto/rand" | |
| 5 | 新增: "crypto/sha256" | |
| 6 | 新增: "encoding/hex" | |
| 7 | 新增: "encoding/json" | |
| 8 | 新增: "errors" | |
| 9 | 新增: "net/http" | |
| 10 | 新增: "os" | |
| 11 | 新增: "path/filepath" | |
| 12 | 新增: "runtime" | |
| 13 | 新增: "time" | |
| 14 | 新增: | |
| 15 | 新增: "github.com/sylvinus/agent-vm/internal/host" | |
| 16 | 新增:) | |
| 17 | 新增: | |
| 18 | 新增:// Only the local launcher possesses the controller token. Its independent | |
| 19 | 新增:// cookie jar carries this proof; the browser and SSH tickets never receive it. | |
| 20 | 新增:func trustedViewProof(token string) string { | |
| 21 | 新增: digest := sha256.Sum256([]byte("trusted desktop view\x00" + token)) | |
| 22 | 新增: return hex.EncodeToString(digest[:]) | |
| 23 | 新增:} | |
| 24 | 新增:func (s *Server) hasTrustedView(r *http.Request) bool { | |
| 25 | 新增: c, err := r.Cookie(s.cookieName() + "-desktop") | |
| 26 | 新增: return err == nil && equalSecret(c.Value, trustedViewProof(s.token)) | |
| 27 | 新增:} | |
| 28 | 新增: | |
| 29 | 新增:func validTrustedID(id string) bool { | |
| 30 | 新增: if id == "" { | |
| 31 | 新增: return true | |
| 32 | 新增: } | |
| 33 | 新增: _, err := hex.DecodeString(id) | |
| 34 | 新增: return len(id) == 64 && err == nil | |
| 35 | 新增:} | |
| 36 | 新增: | |
| 37 | 新增:type trustedLoginRequest struct { | |
| 38 | 新增: Action string `json:"action"` | |
| 39 | 新增: ConnectionID string `json:"connection_id,omitempty"` | |
| 40 | 新增: Token string `json:"token,omitempty"` | |
| 41 | 新增: InstanceID string `json:"instance_id,omitempty"` | |
| 42 | 新增: Password string `json:"password,omitempty"` | |
| 43 | 新增:} | |
| 44 | 新增: | |
| 45 | 新增:// Domain errors travel as data through the authenticated launcher client, which | |
| 46 | 新增:// deliberately discards HTTP error bodies. Neither credential reaches the web UI. | |
| 47 | 新增:type trustedLoginReply struct { | |
| 48 | 新增: Error string `json:"error,omitempty"` | |
| 49 | 新增:} | |
| 50 | 新增: | |
| 51 | 新增:func (d *desktopGateway) trustedLogin(w http.ResponseWriter, r *http.Request) { | |
| 52 | 新增: if r.Method != http.MethodPost { | |
| 53 | 新增: http.Error(w, "需要 POST 请求", 405) | |
| 54 | 新增: return | |
| 55 | 新增: } | |
| 56 | 新增: var in trustedLoginRequest | |
| 57 | 新增: if !readJSON(w, r, &in) { | |
| 58 | 新增: return | |
| 59 | 新增: } | |
| 60 | 新增: d.change.Lock() | |
| 61 | 新增: defer d.change.Unlock() | |
| 62 | 新增: session, remote, err := d.acquire(in.ConnectionID) | |
| 63 | 新增: local, ok := d.local.(*desktopLocal) | |
| 64 | 新增: if err != nil || remote || !ok { | |
| 65 | 新增: http.Error(w, "请返回当前本机工作台", 409) | |
| 66 | 新增: return | |
| 67 | 新增: } | |
| 68 | 新增: c, err := openController(local.profile) | |
| 69 | 新增: if err != nil { | |
| 70 | 新增: http.Error(w, "本机后台无法连接", 503) | |
| 71 | 新增: return | |
| 72 | 新增: } | |
| 73 | 新增: defer c.close() | |
| 74 | 新增: if c.origin != session.Origin { | |
| 75 | 新增: http.Error(w, "本机后台已切换", 409) | |
| 76 | 新增: return | |
| 77 | 新增: } | |
| 78 | 新增: in.Token, in.InstanceID = c.record.Token, session.Manifest.InstanceID | |
| 79 | 新增: var reply trustedLoginReply | |
| 80 | 新增: status, err := c.callContext(r.Context(), "POST", "/api/desktop-local/trusted-login", in, &reply) | |
| 81 | 新增: if err != nil || status != http.StatusOK { | |
| 82 | 新增: http.Error(w, "本机后台未接受登录请求,请确认已更新 Cage", 409) | |
| 83 | 新增: return | |
| 84 | 新增: } | |
| 85 | 新增: if reply.Error != "" { | |
| 86 | 新增: http.Error(w, reply.Error, 409) | |
| 87 | 新增: return | |
| 88 | 新增: } | |
| 89 | 新增: jsonReply(w, map[string]bool{"ok": true}) | |
| 90 | 新增:} | |
| 91 | 新增: | |
| 92 | 新增:func (s *Server) localTrustedLogin(w http.ResponseWriter, r *http.Request) { | |
| 93 | 新增: var in trustedLoginRequest | |
| 94 | 新增: if !readJSON(w, r, &in) { | |
| 95 | 新增: return | |
| 96 | 新增: } | |
| 97 | 新增: if s.account == nil || !equalSecret(in.Token, s.token) || in.InstanceID != s.instanceID { | |
| 98 | 新增: http.Error(w, "Launcher refused", http.StatusUnauthorized) | |
| 99 | 新增: return | |
| 100 | 新增: } | |
| 101 | 新增: _, finish, err := s.beginActivity(r.Context()) | |
| 102 | 新增: if err != nil { | |
| 103 | 新增: http.Error(w, "后台正在更新", 503) | |
| 104 | 新增: return | |
| 105 | 新增: } | |
| 106 | 新增: defer finish() | |
| 107 | 新增: base, err := os.UserConfigDir() | |
| 108 | 新增: if runtime.GOOS == "windows" { | |
| 109 | 新增: base = os.Getenv("LOCALAPPDATA") | |
| 110 | 新增: } | |
| 111 | 新增: if !filepath.IsAbs(base) { | |
| 112 | 新增: err = errors.New("无法确定本机系统用户配置目录") | |
| 113 | 新增: } | |
| 114 | 新增: if err == nil { | |
| 115 | 新增: err = s.account.trustedLogin(in.Action, in.Password, filepath.Join(base, "Cage", "trusted-local")) | |
| 116 | 新增: } | |
| 117 | 新增: reply := trustedLoginReply{} | |
| 118 | 新增: if err != nil { | |
| 119 | 新增: reply.Error = err.Error() | |
| 120 | 新增: } | |
| 121 | 新增: jsonReply(w, reply) | |
| 122 | 新增:} | |
| 123 | 新增: | |
| 124 | 新增:func (a *localAccount) trustedLogin(action, password, devices string) error { | |
| 125 | 新增: a.mu.Lock() | |
| 126 | 新增: defer a.mu.Unlock() | |
| 127 | 新增: switch action { | |
| 128 | 新增: case "create": | |
| 129 | 新增: if a.record != nil { | |
| 130 | 新增: return errors.New("已有账户,请使用原登录方式") | |
| 131 | 新增: } | |
| 132 | 新增: record := &accountRecord{Version: 1, Username: "本机用户", TrustedDevice: randomToken()} | |
| 133 | 新增: secret := randomToken() | |
| 134 | 新增: key := make([]byte, 32) | |
| 135 | 新增: if _, err := rand.Read(key); err != nil { | |
| 136 | 新增: return err | |
| 137 | 新增: } | |
| 138 | 新增: defer clear(key) | |
| 139 | 新增: if err := sealAccountKey(record, secret, key); err != nil { | |
| 140 | 新增: return err | |
| 141 | 新增: } | |
| 142 | 新增: path := filepath.Join(devices, record.TrustedDevice+".json") | |
| 143 | 新增: // Store the wrapping credential first. A crash cannot publish a record | |
| 144 | 新增: // whose credential has not been persisted. | |
| 145 | 新增: if err := writePrivate(path, secret); err != nil { | |
| 146 | 新增: return errors.New("无法保存当前系统用户的信任凭据") | |
| 147 | 新增: } | |
| 148 | 新增: if err := writePrivate(a.path, record); err != nil { | |
| 149 | 新增: _ = os.Remove(path) | |
| 150 | 新增: return errors.New("无法保存本机工作区") | |
| 151 | 新增: } | |
| 152 | 新增: a.record = record | |
| 153 | 新增: a.setKey(append([]byte(nil), key...)) | |
| 154 | 新增: a.trustedSession = true | |
| 155 | 新增: case "enable": | |
| 156 | 新增: if a.record == nil || a.record.TrustedDevice != "" || len(a.key) != 32 { | |
| 157 | 新增: return errors.New("请先用本机账户登录") | |
| 158 | 新增: } | |
| 159 | 新增: if time.Now().Before(a.nextAttempt) { | |
| 160 | 新增: return errors.New("请稍后再试") | |
| 161 | 新增: } | |
| 162 | 新增: key, err := openAccountKey(a.record, password) | |
| 163 | 新增: if err != nil { | |
| 164 | 新增: a.nextAttempt = time.Now().Add(time.Second) | |
| 165 | 新增: return err | |
| 166 | 新增: } | |
| 167 | 新增: defer clear(key) | |
| 168 | 新增: a.nextAttempt = time.Time{} | |
| 169 | 新增: if a.record.TrustedUnlock != nil { | |
| 170 | 新增: return nil | |
| 171 | 新增: } | |
| 172 | 新增: secret := randomToken() | |
| 173 | 新增: raw, err := hex.DecodeString(secret) | |
| 174 | 新增: if err != nil { | |
| 175 | 新增: return err | |
| 176 | 新增: } | |
| 177 | 新增: defer clear(raw) | |
| 178 | 新增: aead, err := deviceAEAD(raw) | |
| 179 | 新增: if err != nil { | |
| 180 | 新增: return err | |
| 181 | 新增: } | |
| 182 | 新增: device := accountDevice{ID: randomToken(), Nonce: make([]byte, aead.NonceSize())} | |
| 183 | 新增: if _, err = rand.Read(device.Nonce); err != nil { | |
| 184 | 新增: return err | |
| 185 | 新增: } | |
| 186 | 新增: device.SealedKey = aead.Seal(nil, device.Nonce, key, []byte("trusted:"+device.ID)) | |
| 187 | 新增: record := *a.record | |
| 188 | 新增: record.TrustedUnlock = &device | |
| 189 | 新增: path := filepath.Join(devices, device.ID+".json") | |
| 190 | 新增: if err = writePrivate(path, secret); err != nil { | |
| 191 | 新增: return errors.New("无法保存本机信任凭据") | |
| 192 | 新增: } | |
| 193 | 新增: if err = writePrivate(a.path, &record); err != nil { | |
| 194 | 新增: _ = os.Remove(path) | |
| 195 | 新增: return errors.New("本机信任未能保存") | |
| 196 | 新增: } | |
| 197 | 新增: a.record = &record | |
| 198 | 新增: case "disable": | |
| 199 | 新增: if a.record == nil || len(a.key) != 32 { | |
| 200 | 新增: return errors.New("请先解锁 Cage") | |
| 201 | 新增: } | |
| 202 | 新增: if a.record.TrustedUnlock == nil { | |
| 203 | 新增: return nil | |
| 204 | 新增: } | |
| 205 | 新增: record := *a.record | |
| 206 | 新增: record.TrustedUnlock = nil | |
| 207 | 新增: if err := writePrivate(a.path, &record); err != nil { | |
| 208 | 新增: return errors.New("本机信任未能关闭") | |
| 209 | 新增: } | |
| 210 | 新增: a.record = &record | |
| 211 | 新增: case "resume": | |
| 212 | 新增: if a.record == nil || (a.record.TrustedDevice == "" && a.record.TrustedUnlock == nil) || a.record.RequireAuthentication { | |
| 213 | 新增: return errors.New("请使用原有账户验证方式") | |
| 214 | 新增: } | |
| 215 | 新增: if len(a.key) == 32 { | |
| 216 | 新增: return nil | |
| 217 | 新增: } | |
| 218 | 新增: id := a.record.TrustedDevice | |
| 219 | 新增: if a.record.TrustedUnlock != nil { | |
| 220 | 新增: id = a.record.TrustedUnlock.ID | |
| 221 | 新增: } | |
| 222 | 新增: path := filepath.Join(devices, id+".json") | |
| 223 | 新增: if err := host.CheckPrivateFile(path); err != nil { | |
| 224 | 新增: return errors.New("找不到当前系统用户的私有信任凭据,请恢复备份") | |
| 225 | 新增: } | |
| 226 | 新增: data, err := os.ReadFile(path) | |
| 227 | 新增: var secret string | |
| 228 | 新增: if err != nil || len(data) > 128 || json.Unmarshal(data, &secret) != nil || len(secret) != 64 { | |
| 229 | 新增: return errors.New("本机信任凭据无法读取,请恢复备份") | |
| 230 | 新增: } | |
| 231 | 新增: var key []byte | |
| 232 | 新增: if device := a.record.TrustedUnlock; device != nil { | |
| 233 | 新增: raw, decodeErr := hex.DecodeString(secret) | |
| 234 | 新增: if decodeErr != nil { | |
| 235 | 新增: return errors.New("本机信任凭据无效") | |
| 236 | 新增: } | |
| 237 | 新增: defer clear(raw) | |
| 238 | 新增: aead, cipherErr := deviceAEAD(raw) | |
| 239 | 新增: if cipherErr != nil { | |
| 240 | 新增: return cipherErr | |
| 241 | 新增: } | |
| 242 | 新增: key, err = aead.Open(nil, device.Nonce, device.SealedKey, []byte("trusted:"+device.ID)) | |
| 243 | 新增: } else { | |
| 244 | 新增: key, err = openAccountKey(a.record, secret) | |
| 245 | 新增: } | |
| 246 | 新增: if err != nil { | |
| 247 | 新增: return errors.New("信任凭据与工作区不匹配,请恢复备份") | |
| 248 | 新增: } | |
| 249 | 新增: a.setKey(key) | |
| 250 | 新增: a.trustedSession = true | |
| 251 | 新增: case "protect": | |
| 252 | 新增: if a.record == nil || a.record.TrustedDevice == "" || len(a.key) != 32 { | |
| 253 | 新增: return errors.New("请先在受信任的本机进入工作区") | |
| 254 | 新增: } | |
| 255 | 新增: if err := validAccountPassword(password); err != nil { | |
| 256 | 新增: return err | |
| 257 | 新增: } | |
| 258 | 新增: record := *a.record | |
| 259 | 新增: record.TrustedDevice = "" | |
| 260 | 新增: if err := sealAccountKey(&record, password, a.key); err != nil { | |
| 261 | 新增: return err | |
| 262 | 新增: } | |
| 263 | 新增: if err := writePrivate(a.path, &record); err != nil { | |
| 264 | 新增: return errors.New("密码未能保存,仍可使用本机登录") | |
| 265 | 新增: } | |
| 266 | 新增: a.record = &record | |
| 267 | 新增: default: | |
| 268 | 新增: return errors.New("未知的信任登录操作") | |
| 269 | 新增: } | |
| 270 | 新增: return nil | |
| 271 | 新增:} | |
| @@ -0,0 +1,171 @@ | ||
| 1 | 新增:package workbench | |
| 2 | 新增: | |
| 3 | 新增:import ( | |
| 4 | 新增: "bytes" | |
| 5 | 新增: "context" | |
| 6 | 新增: "encoding/json" | |
| 7 | 新增: "net" | |
| 8 | 新增: "net/http" | |
| 9 | 新增: "net/http/httptest" | |
| 10 | 新增: "os" | |
| 11 | 新增: "path/filepath" | |
| 12 | 新增: "strconv" | |
| 13 | 新增: "testing" | |
| 14 | 新增: "time" | |
| 15 | 新增:) | |
| 16 | 新增: | |
| 17 | 新增:func TestTrustedLocalLifecycle(t *testing.T) { | |
| 18 | 新增: profile, devices := t.TempDir(), t.TempDir() | |
| 19 | 新增: a, err := loadLocalAccount(profile) | |
| 20 | 新增: if err != nil { | |
| 21 | 新增: t.Fatal(err) | |
| 22 | 新增: } | |
| 23 | 新增: if err = a.trustedLogin("create", "", devices); err != nil { | |
| 24 | 新增: t.Fatal(err) | |
| 25 | 新增: } | |
| 26 | 新增: original := bytes.Clone(a.key) | |
| 27 | 新增: a.lock() | |
| 28 | 新增: a, err = loadLocalAccount(profile) | |
| 29 | 新增: if err != nil { | |
| 30 | 新增: t.Fatal(err) | |
| 31 | 新增: } | |
| 32 | 新增: if err = a.trustedLogin("resume", "", t.TempDir()); err == nil { | |
| 33 | 新增: t.Fatal("untrusted OS profile opened workspace") | |
| 34 | 新增: } | |
| 35 | 新增: if err = a.trustedLogin("resume", "", devices); err != nil || !bytes.Equal(original, a.key) { | |
| 36 | 新增: t.Fatal("restart lost workspace", err) | |
| 37 | 新增: } | |
| 38 | 新增: if err = a.trustedLogin("protect", "123", devices); err == nil { | |
| 39 | 新增: t.Fatal("accepted short password") | |
| 40 | 新增: } | |
| 41 | 新增: if err = a.trustedLogin("protect", "first-password", devices); err != nil { | |
| 42 | 新增: t.Fatal(err) | |
| 43 | 新增: } | |
| 44 | 新增: if err = a.trustedLogin("create", "", devices); err == nil { | |
| 45 | 新增: t.Fatal("overwrote password account") | |
| 46 | 新增: } | |
| 47 | 新增: a.lock() | |
| 48 | 新增: if err = a.trustedLogin("resume", "", devices); err == nil { | |
| 49 | 新增: t.Fatal("stale device grant") | |
| 50 | 新增: } | |
| 51 | 新增: if err = a.unlock("first-password"); err != nil || !bytes.Equal(original, a.key) { | |
| 52 | 新增: t.Fatal("password conversion lost workspace", err) | |
| 53 | 新增: } | |
| 54 | 新增: if err = a.trustedLogin("enable", "first-password", devices); err != nil { | |
| 55 | 新增: t.Fatal(err) | |
| 56 | 新增: } | |
| 57 | 新增: a.lock() | |
| 58 | 新增: if err = a.trustedLogin("resume", "", devices); err != nil || !bytes.Equal(original, a.key) { | |
| 59 | 新增: t.Fatal("password account failed automatic unlock", err) | |
| 60 | 新增: } | |
| 61 | 新增: if err = a.lockAccess(false); err != nil { | |
| 62 | 新增: t.Fatal(err) | |
| 63 | 新增: } | |
| 64 | 新增: if err = a.trustedLogin("resume", "", devices); err == nil { | |
| 65 | 新增: t.Fatal("ignored explicit lock") | |
| 66 | 新增: } | |
| 67 | 新增: if err = a.unlock("first-password"); err != nil { | |
| 68 | 新增: t.Fatal(err) | |
| 69 | 新增: } | |
| 70 | 新增: if err = a.lockAccess(true); err != nil { | |
| 71 | 新增: t.Fatal(err) | |
| 72 | 新增: } | |
| 73 | 新增: a, err = loadLocalAccount(profile) | |
| 74 | 新增: if err != nil { | |
| 75 | 新增: t.Fatal(err) | |
| 76 | 新增: } | |
| 77 | 新增: if a.view().TrustedLogin || a.trustedLogin("resume", "", devices) == nil { | |
| 78 | 新增: t.Fatal("logout retained trust") | |
| 79 | 新增: } | |
| 80 | 新增:} | |
| 81 | 新增: | |
| 82 | 新增:func TestTrustedLocalLauncherBoundary(t *testing.T) { | |
| 83 | 新增: s := testServer(t) | |
| 84 | 新增: if w := request(s, "POST", "/api/desktop-local/trusted-login", s.origin, `{"action":"resume"}`, true); w.Code != http.StatusUnauthorized { | |
| 85 | 新增: t.Fatalf("ordinary session reached launcher action: %d", w.Code) | |
| 86 | 新增: } | |
| 87 | 新增: if w := request(s, "POST", "/api/account/trusted-login", s.origin, `{}`, true); w.Code != http.StatusNotFound { | |
| 88 | 新增: t.Fatalf("ordinary account route accepted trusted action: %d", w.Code) | |
| 89 | 新增: } | |
| 90 | 新增:} | |
| 91 | 新增: | |
| 92 | 新增:func TestTrustedLocalUnlockIsPrivateToDesktop(t *testing.T) { | |
| 93 | 新增: s := testServer(t) | |
| 94 | 新增: a, err := loadLocalAccount(s.profile) | |
| 95 | 新增: if err != nil { | |
| 96 | 新增: t.Fatal(err) | |
| 97 | 新增: } | |
| 98 | 新增: s.account = a | |
| 99 | 新增: if err = a.trustedLogin("create", "", t.TempDir()); err != nil { | |
| 100 | 新增: t.Fatal(err) | |
| 101 | 新增: } | |
| 102 | 新增: if !a.viewFor(true).Unlocked || a.viewFor(false).Unlocked { | |
| 103 | 新增: t.Fatal("trusted login escaped desktop view") | |
| 104 | 新增: } | |
| 105 | 新增: if response := request(s, "GET", "/api/favorites", "", "", true); response.Code != http.StatusLocked { | |
| 106 | 新增: t.Fatalf("session cookie alone opened favorites: %d", response.Code) | |
| 107 | 新增: } | |
| 108 | 新增: if response := request(s, "POST", "/api/account/profile", s.origin, `{"username":"Different"}`, true); response.Code != http.StatusLocked { | |
| 109 | 新增: t.Fatalf("session cookie alone changed profile: %d", response.Code) | |
| 110 | 新增: } | |
| 111 | 新增: req, _ := http.NewRequest("GET", s.origin, nil) | |
| 112 | 新增: req.AddCookie(&http.Cookie{Name: s.cookieName() + "-desktop", Value: trustedViewProof(s.token)}) | |
| 113 | 新增: if !s.hasTrustedView(req) { | |
| 114 | 新增: t.Fatal("local launcher proof was refused") | |
| 115 | 新增: } | |
| 116 | 新增:} | |
| 117 | 新增: | |
| 118 | 新增:func TestTrustedLocalDesktopEntryAndWorkload(t *testing.T) { | |
| 119 | 新增: for _, env := range []string{"LOCALAPPDATA", "XDG_CONFIG_HOME", "HOME"} { | |
| 120 | 新增: t.Setenv(env, t.TempDir()) | |
| 121 | 新增: } | |
| 122 | 新增: workload := testServer(t) | |
| 123 | 新增: var err error | |
| 124 | 新增: workload.account, err = loadLocalAccount(workload.profile) | |
| 125 | 新增: if err != nil { | |
| 126 | 新增: t.Fatal(err) | |
| 127 | 新增: } | |
| 128 | 新增: workload.instanceID = randomToken() | |
| 129 | 新增: server := httptest.NewUnstartedServer(workload.handler()) | |
| 130 | 新增: port := server.Listener.Addr().(*net.TCPAddr).Port | |
| 131 | 新增: workload.origin = "http://localhost:" + strconv.Itoa(port) | |
| 132 | 新增: server.Start() | |
| 133 | 新增: defer server.Close() | |
| 134 | 新增: record := controlFile{Port: port, PID: os.Getpid(), Token: workload.token} | |
| 135 | 新增: if err = writePrivate(filepath.Join(workload.profile, "control.json"), record); err != nil { | |
| 136 | 新增: t.Fatal(err) | |
| 137 | 新增: } | |
| 138 | 新增: c, err := openController(workload.profile) | |
| 139 | 新增: if err != nil { | |
| 140 | 新增: t.Fatal(err) | |
| 141 | 新增: } | |
| 142 | 新增: defer c.close() | |
| 143 | 新增: ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | |
| 144 | 新增: defer cancel() | |
| 145 | 新增: local := &desktopLocal{profile: workload.profile} | |
| 146 | 新增: session, err := local.localSession(ctx, ctx, c, desktopLocalReply{Identity: workload.identity, InstanceID: workload.instanceID}) | |
| 147 | 新增: if err != nil { | |
| 148 | 新增: t.Fatal(err) | |
| 149 | 新增: } | |
| 150 | 新增: defer session.Close() | |
| 151 | 新增: gateway := testServer(t) | |
| 152 | 新增: gateway.remote = newDesktopGateway(gateway.profile, local) | |
| 153 | 新增: d := gateway.remote.desktop | |
| 154 | 新增: d.active = &desktopTarget{id: d.generation, mode: "local", ctx: ctx, session: session} | |
| 155 | 新增: body, _ := json.Marshal(trustedLoginRequest{Action: "create", ConnectionID: d.generation}) | |
| 156 | 新增: w := request(gateway, "POST", "/api/client/trusted-login", gateway.origin, string(body), true) | |
| 157 | 新增: if w.Code != http.StatusOK { | |
| 158 | 新增: t.Fatalf("trust creation through gateway: %d %s", w.Code, w.Body.String()) | |
| 159 | 新增: } | |
| 160 | 新增: read := gatewayRequest(gateway, "/api/account", d.generation) | |
| 161 | 新增: var view accountView | |
| 162 | 新增: if read.Code != 200 || json.Unmarshal(read.Body.Bytes(), &view) != nil || !view.Unlocked || !view.TrustedLocal { | |
| 163 | 新增: t.Fatalf("local gateway cannot see unlocked account: %s", read.Body.String()) | |
| 164 | 新增: } | |
| 165 | 新增: workload.account.lock() | |
| 166 | 新增: body, _ = json.Marshal(trustedLoginRequest{Action: "resume", ConnectionID: d.generation}) | |
| 167 | 新增: w = request(gateway, "POST", "/api/client/trusted-login", gateway.origin, string(body), true) | |
| 168 | 新增: if w.Code != http.StatusOK || !workload.account.view().Unlocked { | |
| 169 | 新增: t.Fatalf("local resume failed: %d %s", w.Code, w.Body.String()) | |
| 170 | 新增: } | |
| 171 | 新增:} | |
| @@ -163,6 +163,9 @@ func (db *vaultData) save(entry vaultEntry) error { | ||
| 163 | 163 | func (a *localAccount) verifyVaultPassword(password string) error { |
| 164 | 164 | a.mu.Lock() |
| 165 | 165 | defer a.mu.Unlock() |
| 166 | 新增: if a.record != nil && a.record.TrustedDevice != "" { | |
| 167 | 新增: return errors.New("查看或复制密码前,请在本机的「设置 → 账户」设置密码") | |
| 168 | 新增: } | |
| 166 | 169 | if len(a.key) != 32 { |
| 167 | 170 | return errors.New("请先解锁 Cage") |
| 168 | 171 | } |
| @@ -321,3 +321,5 @@ | ||
| 321 | 321 | .account-device-option small { color: var(--muted); font-size: 12px; line-height: 1.6; } |
| 322 | 322 | |
| 323 | 323 | .account-trigger[aria-expanded="false"] > svg { transform: rotate(180deg); } |
| 324 | 新增: | |
| 325 | 新增:.account-trust-choice { display: grid; gap: 12px; margin: 16px 0; } | |
| @@ -18,12 +18,16 @@ import { | ||
| 18 | 18 | Settings2, |
| 19 | 19 | } from "lucide-react"; |
| 20 | 20 | import { api, native, nativeAvailable } from "./api"; |
| 21 | 删除:import { isRemoteClient } from "./client-context.mjs"; | |
| 21 | 新增:import { getClientContext, isRemoteClient } from "./client-context.mjs"; | |
| 22 | 22 | import logo from "./logo.png"; |
| 23 | 23 | import "./account.css"; |
| 24 | 24 | |
| 25 | 25 | const AccountContext = createContext(null); |
| 26 | 26 | export const useAccount = () => useContext(AccountContext); |
| 27 | 新增:const localDesktop = () => getClientContext().desktop_gateway && !isRemoteClient(); | |
| 28 | 新增:const trustedLogin = (action, password) => api("/client/trusted-login", { | |
| 29 | 新增: action, password, connection_id: getClientContext().connection_id, | |
| 30 | 新增:}); | |
| 27 | 31 | const credentialPresent = (status) => status === "available" || status === "authentication_required"; |
| 28 | 32 | |
| 29 | 33 | export function AccountProvider({ children }) { |
| @@ -180,6 +184,20 @@ export function AccountGate({ children }) { | ||
| 180 | 184 | const [error, setError] = useState(""); |
| 181 | 185 | const automaticAttempt = useRef(""); |
| 182 | 186 | const gateAcknowledged = useRef(false); |
| 187 | 新增: const [passwordSetup, setPasswordSetup] = useState(false); | |
| 188 | 新增: const trustedAttempt = useRef(false); | |
| 189 | 新增: const enterTrusted = async (action) => { | |
| 190 | 新增: setBusy(true); setError(""); | |
| 191 | 新增: try { await trustedLogin(action); await refresh(); } | |
| 192 | 新增: catch (e) { setError(e.message); } | |
| 193 | 新增: finally { setBusy(false); } | |
| 194 | 新增: }; | |
| 195 | 新增: useEffect(() => { | |
| 196 | 新增: if (account?.unlocked) { trustedAttempt.current = false; return; } | |
| 197 | 新增: if (!(account?.trusted_local || account?.trusted_login) || !localDesktop() || trustedAttempt.current || account.require_authentication) return; | |
| 198 | 新增: trustedAttempt.current = true; | |
| 199 | 新增: void enterTrusted("resume"); | |
| 200 | 新增: }, [account?.unlocked, account?.trusted_local, account?.trusted_login, account?.require_authentication]); | |
| 183 | 201 | useEffect(() => { |
| 184 | 202 | if (!account || account.unlocked || connectionError || gateAcknowledged.current || !nativeAvailable()) return; |
| 185 | 203 | // A working lock/setup screen is a valid upgraded UI. Waiting for the |
| @@ -190,7 +208,7 @@ export function AccountGate({ children }) { | ||
| 190 | 208 | }, [Boolean(account), account?.unlocked, connectionError]); |
| 191 | 209 | useEffect(() => { |
| 192 | 210 | const id = account?.devices?.automatic; |
| 193 | 删除: if (!id || account.unlocked || account.require_authentication || !deviceSupport?.supported || deviceSupport.device_status?.automatic !== "available" || automaticAttempt.current === id) return; | |
| 211 | 新增: if ((account?.trusted_login && localDesktop()) || !id || account.unlocked || account.require_authentication || !deviceSupport?.supported || deviceSupport.device_status?.automatic !== "available" || automaticAttempt.current === id) return; | |
| 194 | 212 | automaticAttempt.current = id; |
| 195 | 213 | setBusy(true); |
| 196 | 214 | deviceAction("unlock", "automatic").catch((e) => setError(e.message)).finally(() => setBusy(false)); |
| @@ -258,9 +276,9 @@ export function AccountGate({ children }) { | ||
| 258 | 276 | </h1> |
| 259 | 277 | <p className="account-entry-intro"> |
| 260 | 278 | {creating |
| 261 | 删除: ? isRemoteClient() ? "在云端创建账户,保护这台主机上的工作与登录状态。" : "创建一个本地账户,收好你的工作与登录状态。" | |
| 279 | 新增: ? isRemoteClient() ? "在云端创建账户,保护这台主机上的工作与登录状态。" : localDesktop() && !passwordSetup ? "选择这台电脑的登录方式。" : "创建一个本地账户,收好你的工作与登录状态。" | |
| 262 | 280 | : account |
| 263 | 删除: ? "输入密码,继续你的工作。" | |
| 281 | 新增: ? account.trusted_local ? "本机工作区" : "输入密码,继续你的工作。" | |
| 264 | 282 | : isRemoteClient() ? "正在读取云端账户…" : "正在读取本机账户…"} |
| 265 | 283 | </p> |
| 266 | 284 | {account?.background_running && ( |
| @@ -269,7 +287,23 @@ export function AccountGate({ children }) { | ||
| 269 | 287 | 后台环境仍在运行,任务没有停止 |
| 270 | 288 | </div> |
| 271 | 289 | )} |
| 272 | 删除: {account && ( | |
| 290 | 新增: {localDesktop() && creating && ( | |
| 291 | 新增: <div className="account-trust-choice"> | |
| 292 | 新增: {!passwordSetup && <> | |
| 293 | 新增: <p>信任此电脑后,下次使用当前系统用户打开 Cage 会直接进入。</p> | |
| 294 | 新增: <button className="primary account-submit" disabled={busy} onClick={() => enterTrusted("create")}>信任此电脑并进入</button> | |
| 295 | 新增: </>} | |
| 296 | 新增: <button className="text-button" disabled={busy} onClick={() => setPasswordSetup(!passwordSetup)}> | |
| 297 | 新增: {passwordSetup ? "返回本机免账号登录" : "使用用户名和密码"} | |
| 298 | 新增: </button> | |
| 299 | 新增: </div> | |
| 300 | 新增: )} | |
| 301 | 新增: {account?.trusted_local && <div className="account-trust-choice"> | |
| 302 | 新增: <p>{localDesktop() ? "使用当前系统用户的本机信任凭据进入工作区。" : "请回到受信任的桌面设置账户密码,再从此处登录。"}</p> | |
| 303 | 新增: {localDesktop() && <button className="quiet-button" disabled={busy} onClick={() => enterTrusted("resume")}>{busy ? "正在进入…" : "重新进入本机工作区"}</button>} | |
| 304 | 新增: </div>} | |
| 305 | 新增: {(account?.trusted_local || (creating && localDesktop() && !passwordSetup)) && error && <p className="account-error" role="alert">{error}</p>} | |
| 306 | 新增: {account && !account.trusted_local && (!creating || !localDesktop() || passwordSetup) && ( | |
| 273 | 307 | <form onSubmit={submit}> |
| 274 | 308 | <fieldset disabled={busy}> |
| 275 | 309 | {creating && ( |
| @@ -352,11 +386,11 @@ export function AccountGate({ children }) { | ||
| 352 | 386 | <LockKeyhole size={14} /> |
| 353 | 387 | {isRemoteClient() ? "账户资料保存在当前云端主机" : "账户资料仅保存在这台设备上"} |
| 354 | 388 | </p> |
| 355 | 删除: <p className="account-recovery-note"> | |
| 389 | 新增: {(!localDesktop() || passwordSetup || account?.exists) && !account?.trusted_local && <p className="account-recovery-note"> | |
| 356 | 390 | {creating |
| 357 | 391 | ? "密码至少 8 个字符,用于保护应用登录凭据。请妥善保存,当前不支持在线找回。" |
| 358 | 392 | : isRemoteClient() ? "请输入云端 Cage 账户密码;当前不支持在线找回。" : "请输入本地账户密码;当前不支持在线找回。"} |
| 359 | 删除: </p> | |
| 393 | 新增: </p>} | |
| 360 | 394 | </section> |
| 361 | 395 | {nativeAvailable() && ( |
| 362 | 396 | <button |
| @@ -425,7 +459,7 @@ export function AccountMenu({ onSettings, onConfirm, onStatus, busy }) { | ||
| 425 | 459 | <AccountAvatar account={account} /> |
| 426 | 460 | <span className="account-trigger-name"> |
| 427 | 461 | <strong>{account.username}</strong> |
| 428 | 删除: <small>{isRemoteClient() ? "云端账户" : "本地账户"}</small> | |
| 462 | 新增: <small>{account.trusted_local ? "受信任的本机" : isRemoteClient() ? "云端账户" : "本地账户"}</small> | |
| 429 | 463 | </span> |
| 430 | 464 | <ChevronDown size={15} /> |
| 431 | 465 | </button> |
| @@ -450,6 +484,9 @@ export function AccountMenu({ onSettings, onConfirm, onStatus, busy }) { | ||
| 450 | 484 | <Settings2 size={16} /> |
| 451 | 485 | 设置 |
| 452 | 486 | </button> |
| 487 | 新增: {account.trusted_local ? <button role="menuitem" onClick={() => { setOpen(false); onSettings("account"); }}> | |
| 488 | 新增: <LockKeyhole size={16} /> 设置账户密码 | |
| 489 | 新增: </button> : <> | |
| 453 | 490 | <div className="account-menu-divider" /> |
| 454 | 491 | <button role="menuitem" onClick={() => perform("lock")}> |
| 455 | 492 | <LockKeyhole size={16} /> |
| @@ -462,7 +499,7 @@ export function AccountMenu({ onSettings, onConfirm, onStatus, busy }) { | ||
| 462 | 499 | > |
| 463 | 500 | <LogOut size={16} /> |
| 464 | 501 | 退出账户 |
| 465 | 删除: </button> | |
| 502 | 新增: </button></>} | |
| 466 | 503 | </div> |
| 467 | 504 | )} |
| 468 | 505 | </div> |
| @@ -470,7 +507,7 @@ export function AccountMenu({ onSettings, onConfirm, onStatus, busy }) { | ||
| 470 | 507 | } |
| 471 | 508 | |
| 472 | 509 | export function AccountSettings({ busy: environmentBusy, onDirty }) { |
| 473 | 删除: const { account, act } = useAccount(); | |
| 510 | 新增: const { account, act, refresh } = useAccount(); | |
| 474 | 511 | const [name, setName] = useState(account.username), |
| 475 | 512 | [avatar, setAvatar] = useState(account.avatar || ""); |
| 476 | 513 | const [oldPassword, setOldPassword] = useState(""), |
| @@ -519,6 +556,13 @@ export function AccountSettings({ busy: environmentBusy, onDirty }) { | ||
| 519 | 556 | setError(""); |
| 520 | 557 | setMessage(""); |
| 521 | 558 | try { |
| 559 | 新增: if (changingPassword && account.trusted_local) { | |
| 560 | 新增: await trustedLogin("protect", password); | |
| 561 | 新增: await refresh(); | |
| 562 | 新增: setPassword(""); setConfirmation(""); | |
| 563 | 新增: setMessage("已设置密码,下次打开 Cage 时使用密码登录。"); | |
| 564 | 新增: return; | |
| 565 | 新增: } | |
| 522 | 566 | const value = await act( |
| 523 | 567 | changingPassword ? "password" : "profile", |
| 524 | 568 | changingPassword |
| @@ -628,11 +672,11 @@ export function AccountSettings({ busy: environmentBusy, onDirty }) { | ||
| 628 | 672 | <p className="field-help"> |
| 629 | 673 | 修改密码不会清除 Claude 的登录状态。{isRemoteClient() ? "密码用于解锁当前云端 Cage,没有在线找回。" : "密码仅用于本机解锁,没有在线找回。"} |
| 630 | 674 | </p> |
| 631 | 删除: <PasswordField | |
| 675 | 新增: {!account.trusted_local && <PasswordField | |
| 632 | 676 | label="当前密码" |
| 633 | 677 | value={oldPassword} |
| 634 | 678 | onChange={setOldPassword} |
| 635 | 删除: /> | |
| 679 | 新增: />} | |
| 636 | 680 | <PasswordField |
| 637 | 681 | label="新密码" |
| 638 | 682 | value={password} |
| @@ -647,15 +691,16 @@ export function AccountSettings({ busy: environmentBusy, onDirty }) { | ||
| 647 | 691 | /> |
| 648 | 692 | <button |
| 649 | 693 | className="quiet-button" |
| 650 | 删除: disabled={busy || !oldPassword || !password || !confirmation} | |
| 694 | 新增: disabled={busy || (!account.trusted_local && !oldPassword) || !password || !confirmation} | |
| 651 | 695 | > |
| 652 | 删除: 修改密码 | |
| 696 | 新增: {account.trusted_local ? "设置密码并关闭免账号登录" : "修改密码"} | |
| 653 | 697 | </button> |
| 654 | 698 | </fieldset> |
| 655 | 699 | </form> |
| 656 | 700 | |
| 657 | 701 | {feedbackTarget === "password" && feedback} |
| 658 | 删除: <AccountDeviceSettings /> | |
| 702 | 新增: {localDesktop() && !account.trusted_local && <TrustedDeviceSettings />} | |
| 703 | 新增: {!account.trusted_local && <AccountDeviceSettings />} | |
| 659 | 704 | <p className="account-settings-note"> |
| 660 | 705 | <LockKeyhole size={15} /> |
| 661 | 706 | {isRemoteClient() ? "账户、应用资料与登录凭据保存在当前云端主机。" : "一个本地账户,一个持久工作环境。资料与凭据不上传到 Cage 服务器。"} |
| @@ -664,6 +709,32 @@ export function AccountSettings({ busy: environmentBusy, onDirty }) { | ||
| 664 | 709 | ); |
| 665 | 710 | } |
| 666 | 711 | |
| 712 | 新增:function TrustedDeviceSettings() { | |
| 713 | 新增: const { account, refresh } = useAccount(); | |
| 714 | 新增: const [password, setPassword] = useState(""); | |
| 715 | 新增: const [message, setMessage] = useState(""); | |
| 716 | 新增: const [busy, setBusy] = useState(false); | |
| 717 | 新增: const update = async (event) => { | |
| 718 | 新增: event.preventDefault(); | |
| 719 | 新增: setBusy(true); setMessage(""); | |
| 720 | 新增: try { | |
| 721 | 新增: await trustedLogin(account.trusted_login ? "disable" : "enable", password); | |
| 722 | 新增: setPassword(""); | |
| 723 | 新增: await refresh(); | |
| 724 | 新增: } catch (error) { setMessage(error.message); } | |
| 725 | 新增: finally { setBusy(false); } | |
| 726 | 新增: }; | |
| 727 | 新增: return <form onSubmit={update}> | |
| 728 | 新增: <fieldset disabled={busy}> | |
| 729 | 新增: <h4>信任此电脑</h4> | |
| 730 | 新增: <p className="field-help">{account.trusted_login ? "已开启本机自动登录。手动锁定仍需验证,退出账户会撤销信任。" : "验证一次账户密码,下次用当前系统用户打开时自动进入。"}</p> | |
| 731 | 新增: {!account.trusted_login && <PasswordField label="验证账户密码" value={password} onChange={setPassword} autoComplete="current-password" />} | |
| 732 | 新增: <button className="quiet-button" disabled={!account.trusted_login && !password}>{account.trusted_login ? "关闭本机信任" : "信任此电脑"}</button> | |
| 733 | 新增: {message && <p className="account-error" role="alert">{message}</p>} | |
| 734 | 新增: </fieldset> | |
| 735 | 新增: </form>; | |
| 736 | 新增:} | |
| 737 | 新增: | |
| 667 | 738 | function AccountDeviceSettings() { |
| 668 | 739 | const { account, deviceSupport, deviceAction, recheckDevices } = useAccount(); |
| 669 | 740 | const [busy, setBusy] = useState(false); |

