跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

account_device.go

152 行 · 4.2 KB历史下载
1package workbench
2
3import (
4 "crypto/aes"
5 "crypto/cipher"
6 "crypto/rand"
7 "crypto/sha256"
8 "encoding/base64"
9 "encoding/hex"
10 "errors"
11 "maps"
12)
13
14type accountDevice struct {
15 ID string `json:"id"`
16 Nonce []byte `json:"nonce"`
17 SealedKey []byte `json:"sealed_key"`
18}
19type deviceCredential struct {
20 ID string `json:"id"`
21 Secret string `json:"secret"`
22}
23
24func validDeviceKind(kind string) bool { return kind == "automatic" || kind == "biometric" }
25func fmtAccountBinding(path string) string {
26 sum := sha256.Sum256([]byte(path))
27 return hex.EncodeToString(sum[:])
28}
29func deviceAEAD(secret []byte) (cipher.AEAD, error) {
30 block, err := aes.NewCipher(secret)
31 if err != nil {
32 return nil, err
33 }
34 return cipher.NewGCM(block)
35}
36func (a *localAccount) enrollDevice(kind string) (deviceCredential, error) {
37 a.mu.Lock()
38 defer a.mu.Unlock()
39 if !validDeviceKind(kind) || len(a.key) != 32 {
40 return deviceCredential{}, errors.New("请先解锁 Cage,再设置快捷登录")
41 }
42 secret := make([]byte, 32)
43 if _, err := rand.Read(secret); err != nil {
44 return deviceCredential{}, err
45 }
46 defer clear(secret)
47 id := make([]byte, 16)
48 if _, err := rand.Read(id); err != nil {
49 return deviceCredential{}, err
50 }
51 device := accountDevice{ID: hex.EncodeToString(id), Nonce: make([]byte, 12)}
52 if _, err := rand.Read(device.Nonce); err != nil {
53 return deviceCredential{}, err
54 }
55 aead, _ := deviceAEAD(secret)
56 device.SealedKey = aead.Seal(nil, device.Nonce, a.key, []byte(kind+":"+device.ID))
57 record := *a.record
58 record.Devices = maps.Clone(record.Devices)
59 if record.Devices == nil {
60 record.Devices = map[string]accountDevice{}
61 }
62 record.Devices[kind] = device
63 if err := writePrivate(a.path, record); err != nil {
64 return deviceCredential{}, errors.New("快捷登录未能保存,请重试")
65 }
66 a.record = &record
67 return deviceCredential{ID: device.ID, Secret: base64.RawStdEncoding.EncodeToString(secret)}, nil
68}
69func (a *localAccount) revokeDevice(kind string) error {
70 a.mu.Lock()
71 defer a.mu.Unlock()
72 if !validDeviceKind(kind) || len(a.key) != 32 {
73 return errors.New("请先解锁 Cage")
74 }
75 record := *a.record
76 record.Devices = maps.Clone(record.Devices)
77 delete(record.Devices, kind)
78 if err := writePrivate(a.path, record); err != nil {
79 return errors.New("快捷登录未能关闭,请重试")
80 }
81 a.record = &record
82 return nil
83}
84
85// Called with a.mu held. The disk flag prevents reopening the app or restarting
86// the controller from turning an explicit lock into automatic login.
87func (a *localAccount) clearAuthenticationRequirement() error {
88 if !a.record.RequireAuthentication {
89 return nil
90 }
91 record := *a.record
92 record.RequireAuthentication = false
93 if err := writePrivate(a.path, record); err != nil {
94 return errors.New("无法保存解锁状态,请重试")
95 }
96 a.record = &record
97 return nil
98}
99func (a *localAccount) unlockDevice(kind string, credential deviceCredential) error {
100 a.mu.Lock()
101 defer a.mu.Unlock()
102 if a.record == nil || !validDeviceKind(kind) {
103 return errors.New("快捷登录不可用,请使用密码")
104 }
105 if kind == "automatic" && a.record.RequireAuthentication {
106 return errors.New("Cage 已手动锁定,请使用指纹或密码")
107 }
108 device, ok := a.record.Devices[kind]
109 if !ok || device.ID != credential.ID {
110 return errors.New("快捷登录授权已失效,请使用密码")
111 }
112 secret, err := base64.RawStdEncoding.DecodeString(credential.Secret)
113 if err != nil || len(secret) != 32 {
114 return errors.New("快捷登录凭据无效")
115 }
116 defer clear(secret)
117 aead, _ := deviceAEAD(secret)
118 key, err := aead.Open(nil, device.Nonce, device.SealedKey, []byte(kind+":"+device.ID))
119 if err != nil {
120 return errors.New("快捷登录凭据无效")
121 }
122 if err = a.clearAuthenticationRequirement(); err != nil {
123 clear(key)
124 return err
125 }
126 a.setKey(key)
127 return nil
128}
129func (a *localAccount) lockAccess(logout bool) error {
130 a.mu.Lock()
131 defer a.mu.Unlock()
132 if a.record == nil {
133 return nil
134 }
135 record := *a.record
136 record.RequireAuthentication = true
137 if logout {
138 record.Devices = nil
139 record.TrustedUnlock = nil
140 }
141 if err := writePrivate(a.path, record); err != nil {
142 return errors.New("无法保存账户锁定状态,请重试")
143 }
144 a.record = &record
145 if a.cancel != nil {
146 a.cancel()
147 a.cancel = nil
148 }
149 clear(a.key)
150 a.key = nil
151 return nil
152}