Repository navigation
Expand file tree
/
Copy pathapprovals.go
More file actions
278 lines (247 loc) · 7.99 KB
/
Copy pathapprovals.go
File metadata and controls
278 lines (247 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
// The chat's door onto answering new devices and keeping the device list: what
// the approve screen and `/devices` reach through tui3.Approvals. It is the
// same Approver and signed directory `codeaf pair approve` uses, pointed at
// this computer's home, so the terminal and the app cannot disagree.
import (
"context"
"errors"
"runtime"
"sort"
"strings"
"sync"
"time"
"github.com/Agent-Field/codeaf/internal/cellsync"
"github.com/Agent-Field/codeaf/internal/chatlist"
"github.com/Agent-Field/codeaf/internal/devname"
"github.com/Agent-Field/codeaf/internal/directory"
"github.com/Agent-Field/codeaf/internal/identity"
"github.com/Agent-Field/codeaf/internal/pair"
"github.com/Agent-Field/codeaf/internal/syncsetup"
"github.com/Agent-Field/codeaf/internal/tui3"
)
// approvalsDoor is the surface's tui3.Approvals.
type approvalsDoor struct {
dir string
mailbox func(relayFlag string) (pair.Mailbox, error)
approver func(pair.Mailbox) (pair.Approver, error)
mu sync.Mutex
asking map[string]pair.Asking // by code: what Pending read, for the answer
}
var _ tui3.Approvals = (*approvalsDoor)(nil)
func newApprovalsDoor(dir string) *approvalsDoor {
return &approvalsDoor{dir: dir, mailbox: pairMailbox, approver: linkApprover(dir), asking: map[string]pair.Asking{}}
}
// toldLine is a refusal said in the sentence every sync surface uses for it, and
// it still is the refusal for errors.Is.
type toldLine struct {
line string
err error
}
func (e toldLine) Error() string { return e.line }
func (e toldLine) Unwrap() error { return e.err }
// told puts the relay's refusals (a stopped computer, a clock that is off) in
// the words a person reads, so a screen that lists devices says why it cannot,
// and the sentence is the one the chat list and the sync notice already say.
// Anything that is not a refusal is returned as it came.
func told(err error) error {
if refusal, ok := cellsync.RefusalOf(err); ok {
line := refusal.Say(err)
if line == chatlist.Removed {
line = chatlist.RemovedChat
}
return toldLine{line: line, err: err}
}
return err
}
func (d *approvalsDoor) who() (pair.Approver, error) {
route, err := d.mailbox("")
if err != nil {
return pair.Approver{}, err
}
who, err := d.approver(route)
if err == nil {
who.BeforeApprove = func(ctx context.Context) error { return ownDeviceRecord(ctx, d.dir, route) }
}
return who, err
}
func (d *approvalsDoor) Pending(ctx context.Context, typed string) (tui3.PendingDevice, error) {
ref, err := pair.ReadLink(typed)
if err != nil {
return tui3.PendingDevice{}, err
}
route, err := d.mailbox("")
if err != nil {
return tui3.PendingDevice{}, err
}
// Refuse before a code or its key can reach another sync service.
if err := pair.CheckLinkService(typed, route.URL); err != nil {
return tui3.PendingDevice{}, err
}
who, err := d.approver(route)
if err != nil {
return tui3.PendingDevice{}, err
}
as, err := who.Look(ctx, ref)
if err != nil {
return tui3.PendingDevice{}, told(err)
}
d.remember(as)
return pendingOf(as), nil
}
func pendingOf(as pair.Asking) tui3.PendingDevice {
return tui3.PendingDevice{
Code: as.Ref.Code, Device: as.DeviceID(), Name: as.Name, Platform: as.Platform,
Check: as.Check, RequestedAt: as.RequestedAt, ExpiresAt: as.ExpiresAt,
}
}
func (d *approvalsDoor) remember(as pair.Asking) {
d.mu.Lock()
defer d.mu.Unlock()
d.asking[as.Ref.Code] = as
}
func (d *approvalsDoor) recall(p tui3.PendingDevice) (pair.Asking, error) {
d.mu.Lock()
defer d.mu.Unlock()
as, ok := d.asking[p.Code]
if !ok {
return pair.Asking{}, errors.New("open that pairing link again before approving")
}
return as, nil
}
func (d *approvalsDoor) Approve(ctx context.Context, p tui3.PendingDevice) error {
return d.answer(p, func(who pair.Approver, as pair.Asking) error {
if err := who.Approve(ctx, as); err != nil {
return err
}
return identity.EndSolo(d.dir)
})
}
func (d *approvalsDoor) Deny(ctx context.Context, p tui3.PendingDevice) error {
return d.answer(p, func(who pair.Approver, as pair.Asking) error { return who.Deny(ctx, as) })
}
// answer runs one decision on the request Pending read, and forgets it after.
func (d *approvalsDoor) answer(p tui3.PendingDevice, decide func(pair.Approver, pair.Asking) error) error {
as, err := d.recall(p)
if err != nil {
return err
}
who, err := d.who()
if err != nil {
return err
}
if err := decide(who, as); err != nil {
return told(err)
}
d.forget(p.Code)
return nil
}
func (d *approvalsDoor) forget(code string) {
d.mu.Lock()
defer d.mu.Unlock()
delete(d.asking, code)
}
// home is this computer's signed directory, its identity and its device id.
func (d *approvalsDoor) home() (directory.Client, identity.Identity, string, error) {
route, err := d.mailbox("")
if err != nil {
return nil, identity.Identity{}, "", err
}
client, id, err := homeDirectory(d.dir, route)
if err != nil {
return nil, id, "", err
}
dev, err := identity.Device(d.dir)
return client, id, dev.ID(), err
}
func (d *approvalsDoor) Devices(ctx context.Context) ([]tui3.DeviceRow, error) {
if syncsetup.Quiet(d.dir) {
return []tui3.DeviceRow{d.ownRow()}, nil
}
client, id, self, err := d.home()
if err != nil {
return nil, err
}
listing, err := client.List(ctx)
if err != nil {
return nil, told(err)
}
rows := rowsOf(listing, directory.MetadataKey(id.CellKey()), self)
foundSelf := false
for _, row := range rows {
foundSelf = foundSelf || row.Self
}
if !foundSelf {
rows = append(rows, d.ownRow())
}
if own, named := devname.Chosen(d.dir); named {
rows = withOwnName(rows, own)
}
return rows, nil
}
// withOwnName puts this computer's own name on its own row. The directory holds
// the name this computer last managed to tell it, which is the old one for a
// person who renamed offline; the name kept here is the one they typed.
func withOwnName(rows []tui3.DeviceRow, own string) []tui3.DeviceRow {
for i := range rows {
if rows[i].Self {
rows[i].Name = own
}
}
return rows
}
func rowsOf(l directory.Listing, key []byte, self string) []tui3.DeviceRow {
rows := make([]tui3.DeviceRow, 0, len(l.Devices))
for id, dev := range l.Devices {
rows = append(rows, rowOf(id, dev, key, self))
}
sort.Slice(rows, func(i, j int) bool {
return rows[i].Name < rows[j].Name || (rows[i].Name == rows[j].Name && rows[i].ID < rows[j].ID)
})
return rows
}
func rowOf(id string, dev directory.Device, key []byte, self string) tui3.DeviceRow {
row := tui3.DeviceRow{ID: id, Name: openName(key, dev.Name), Platform: dev.Platform, Self: id == self, Revoked: dev.Revoked}
if dev.LastSeen > 0 {
row.LastSeen = time.UnixMilli(dev.LastSeen)
}
return row
}
// openName is a sealed name in words, and "a device" when it cannot be opened.
func openName(key []byte, sealed string) string {
if name, err := directory.OpenName(key, sealed); err == nil && strings.TrimSpace(name) != "" {
return devname.Shown(name)
}
return "a device"
}
func (d *approvalsDoor) Revoke(ctx context.Context, id string) error {
client, _, _, err := d.home()
if err != nil {
return err
}
return told(client.Revoke(ctx, id))
}
func (d *approvalsDoor) DeviceName(sealed string) string {
id, err := identity.Load(d.dir)
if err != nil {
return "a new device"
}
return openName(directory.MetadataKey(id.CellKey()), sealed)
}
// Rename names this computer. It is the door's one write of its own record: the
// same function `codeaf devices rename` runs, so the two cannot say different
// things.
func (d *approvalsDoor) Rename(ctx context.Context, typed string) (string, error) {
return renameThisDevice(ctx, d.dir, typed)
}
// ownRow keeps an unpaired computer nameable without waking the sync service.
func (d *approvalsDoor) ownRow() tui3.DeviceRow {
dev, err := identity.LoadDevice(d.dir)
id := ""
if err == nil {
id = dev.ID()
}
return tui3.DeviceRow{ID: id, Name: devname.Name(d.dir), Platform: runtime.GOOS, Self: true}
}
// SyncService names the local pairing service even when the chat runs elsewhere.
func (d *approvalsDoor) SyncService() string { return syncsetup.Resolve(d.dir).URL }