forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlf_handle.go
595 lines (530 loc) · 17.7 KB
/
tlf_handle.go
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
// This file has the type for TlfHandles and offline functionality.
import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/keybase/client/go/externals"
"github.com/keybase/client/go/kbfs/kbfscodec"
"github.com/keybase/client/go/kbfs/tlf"
kbname "github.com/keybase/client/go/kbun"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// TlfHandle contains all the info in a tlf.Handle as well as
// additional info. This doesn't embed tlf.Handle to avoid having to
// keep track of data in multiple places.
type TlfHandle struct {
// If this is not Private, resolvedReaders and unresolvedReaders
// should both be nil.
tlfType tlf.Type
resolvedWriters map[keybase1.UserOrTeamID]kbname.NormalizedUsername
resolvedReaders map[keybase1.UserOrTeamID]kbname.NormalizedUsername
// Both unresolvedWriters and unresolvedReaders are stored in
// sorted order.
unresolvedWriters []keybase1.SocialAssertion
unresolvedReaders []keybase1.SocialAssertion
conflictInfo *tlf.HandleExtension
finalizedInfo *tlf.HandleExtension
// name can be computed from the other fields, but is cached
// for speed.
name tlf.CanonicalName
// If we know the TLF ID at the time this handle is constructed
// (e.g., because this handle is backed by an implicit team), we
// store the TLF ID here so that we can look the TLF up from the
// mdserver using the ID, instead of the handle.
tlfID tlf.ID
}
// Type returns the type of the TLF this TlfHandle represents.
func (h TlfHandle) Type() tlf.Type {
return h.tlfType
}
// IsBackedByTeam returns true if h represents a TLF backed by a team. It could
// be either a SingleTeam TLF or a private/public TLF backed by an implicit
// team.
func (h TlfHandle) IsBackedByTeam() bool {
if len(h.resolvedWriters) != 1 ||
len(h.resolvedReaders) != 0 ||
len(h.unresolvedReaders) != 0 ||
len(h.unresolvedWriters) != 0 {
return false
}
return h.FirstResolvedWriter().IsTeamOrSubteam()
}
// TypeForKeying returns the keying type for the handle h.
func (h TlfHandle) TypeForKeying() tlf.KeyingType {
if h.IsBackedByTeam() {
return tlf.TeamKeying
}
return h.Type().ToKeyingType()
}
// TlfID returns the TLF ID corresponding to this handle, if it's
// known. If it's wasn't known at the time the handle was
// constructed, tlf.NullID is returned.
func (h TlfHandle) TlfID() tlf.ID {
return h.tlfID
}
// IsWriter returns whether or not the given user is a writer for the
// top-level folder represented by this TlfHandle.
func (h TlfHandle) IsWriter(user keybase1.UID) bool {
// TODO(KBFS-2185) relax this?
if h.TypeForKeying() == tlf.TeamKeying {
panic("Can't check whether a user is a writer on a team TLF")
}
_, ok := h.resolvedWriters[user.AsUserOrTeam()]
return ok
}
// IsReader returns whether or not the given user is a reader for the
// top-level folder represented by this TlfHandle.
func (h TlfHandle) IsReader(user keybase1.UID) bool {
// TODO(KBFS-2185) relax this?
if h.TypeForKeying() == tlf.TeamKeying {
panic("Can't check whether a user is a reader on a team TLF")
}
if h.TypeForKeying() == tlf.PublicKeying || h.IsWriter(user) {
return true
}
_, ok := h.resolvedReaders[user.AsUserOrTeam()]
return ok
}
// ResolvedUsersMap returns a map of resolved users from uid to usernames.
func (h TlfHandle) ResolvedUsersMap() map[keybase1.UserOrTeamID]kbname.NormalizedUsername {
m := make(map[keybase1.UserOrTeamID]kbname.NormalizedUsername,
len(h.resolvedReaders)+len(h.resolvedWriters))
for k, v := range h.resolvedReaders {
m[k] = v
}
for k, v := range h.resolvedWriters {
m[k] = v
}
return m
}
func (h TlfHandle) unsortedResolvedWriters() []keybase1.UserOrTeamID {
if len(h.resolvedWriters) == 0 {
return nil
}
writers := make([]keybase1.UserOrTeamID, 0, len(h.resolvedWriters))
for r := range h.resolvedWriters {
writers = append(writers, r)
}
return writers
}
// ResolvedWriters returns the handle's resolved writer IDs in sorted
// order.
func (h TlfHandle) ResolvedWriters() []keybase1.UserOrTeamID {
writers := h.unsortedResolvedWriters()
sort.Sort(tlf.UIDList(writers))
return writers
}
// FirstResolvedWriter returns the handle's first resolved writer ID
// (when sorted). For SingleTeam handles, this returns the team to
// which the TLF belongs.
func (h TlfHandle) FirstResolvedWriter() keybase1.UserOrTeamID {
return h.ResolvedWriters()[0]
}
func (h TlfHandle) unsortedResolvedReaders() []keybase1.UserOrTeamID {
if len(h.resolvedReaders) == 0 {
return nil
}
readers := make([]keybase1.UserOrTeamID, 0, len(h.resolvedReaders))
for r := range h.resolvedReaders {
readers = append(readers, r)
}
return readers
}
// ResolvedReaders returns the handle's resolved reader IDs in sorted
// order. If the handle is public, nil will be returned.
func (h TlfHandle) ResolvedReaders() []keybase1.UserOrTeamID {
readers := h.unsortedResolvedReaders()
sort.Sort(tlf.UIDList(readers))
return readers
}
// UnresolvedWriters returns the handle's unresolved writers in sorted
// order.
func (h TlfHandle) UnresolvedWriters() []keybase1.SocialAssertion {
if len(h.unresolvedWriters) == 0 {
return nil
}
unresolvedWriters := make([]keybase1.SocialAssertion, len(h.unresolvedWriters))
copy(unresolvedWriters, h.unresolvedWriters)
return unresolvedWriters
}
// UnresolvedReaders returns the handle's unresolved readers in sorted
// order. If the handle is public, nil will be returned.
func (h TlfHandle) UnresolvedReaders() []keybase1.SocialAssertion {
if len(h.unresolvedReaders) == 0 {
return nil
}
unresolvedReaders := make([]keybase1.SocialAssertion, len(h.unresolvedReaders))
copy(unresolvedReaders, h.unresolvedReaders)
return unresolvedReaders
}
// ConflictInfo returns the handle's conflict info, if any.
func (h TlfHandle) ConflictInfo() *tlf.HandleExtension {
if h.conflictInfo == nil {
return nil
}
conflictInfoCopy := *h.conflictInfo
return &conflictInfoCopy
}
func (h TlfHandle) recomputeNameWithExtensions() tlf.CanonicalName {
components := strings.Split(string(h.name), tlf.HandleExtensionSep)
newName := components[0]
extensionList := tlf.HandleExtensionList(h.Extensions())
sort.Sort(extensionList)
if h.IsBackedByTeam() {
newName += extensionList.SuffixForTeamHandle()
} else {
newName += extensionList.Suffix()
}
return tlf.CanonicalName(newName)
}
// WithUpdatedConflictInfo returns a new handle with the conflict info set to
// the given one, if the existing one is nil. (In this case, the given one may
// also be nil.) Otherwise, the given conflict info must match the existing
// one.
func (h TlfHandle) WithUpdatedConflictInfo(
codec kbfscodec.Codec, info *tlf.HandleExtension) (*TlfHandle, error) {
newHandle := h.deepCopy()
if newHandle.conflictInfo == nil {
if info == nil {
// Nothing to do.
return newHandle, nil
}
conflictInfoCopy := *info
newHandle.conflictInfo = &conflictInfoCopy
newHandle.name = newHandle.recomputeNameWithExtensions()
return newHandle, nil
}
// Make sure conflict info is the same; the conflict info for
// a TLF, once set, is immutable and should never change.
equal, err := kbfscodec.Equal(codec, newHandle.conflictInfo, info)
if err != nil {
return newHandle, err
}
if !equal {
return newHandle, tlf.HandleExtensionMismatchError{
Expected: *newHandle.ConflictInfo(),
Actual: info,
}
}
return newHandle, nil
}
// FinalizedInfo returns the handle's finalized info, if any.
func (h TlfHandle) FinalizedInfo() *tlf.HandleExtension {
if h.finalizedInfo == nil {
return nil
}
finalizedInfoCopy := *h.finalizedInfo
return &finalizedInfoCopy
}
// SetFinalizedInfo sets the handle's finalized info to the given one,
// which may be nil.
// TODO: remove this to make TlfHandle fully immutable
func (h *TlfHandle) SetFinalizedInfo(info *tlf.HandleExtension) {
if info == nil {
h.finalizedInfo = nil
} else {
finalizedInfoCopy := *info
h.finalizedInfo = &finalizedInfoCopy
}
h.name = h.recomputeNameWithExtensions()
}
// Extensions returns a list of extensions for the given handle.
func (h TlfHandle) Extensions() (extensions []tlf.HandleExtension) {
if h.ConflictInfo() != nil {
extensions = append(extensions, *h.ConflictInfo())
}
if h.FinalizedInfo() != nil {
extensions = append(extensions, *h.FinalizedInfo())
}
return extensions
}
func init() {
if reflect.ValueOf(TlfHandle{}).NumField() != 9 {
panic(errors.New(
"Unexpected number of fields in TlfHandle; " +
"please update TlfHandle.Equals() for your " +
"new or removed field"))
}
}
// EqualsIgnoreName returns whether h and other contain the same info ignoring the name.
func (h TlfHandle) EqualsIgnoreName(
codec kbfscodec.Codec, other TlfHandle) (bool, error) {
if h.tlfType != other.tlfType {
return false, nil
}
if h.tlfID != other.tlfID {
return false, nil
}
if !reflect.DeepEqual(h.resolvedWriters, other.resolvedWriters) {
return false, nil
}
if !reflect.DeepEqual(h.resolvedReaders, other.resolvedReaders) {
return false, nil
}
if !reflect.DeepEqual(h.unresolvedWriters, other.unresolvedWriters) {
return false, nil
}
if !reflect.DeepEqual(h.unresolvedReaders, other.unresolvedReaders) {
return false, nil
}
eq, err := kbfscodec.Equal(codec, h.conflictInfo, other.conflictInfo)
if err != nil {
return false, err
}
if !eq {
return false, nil
}
eq, err = kbfscodec.Equal(codec, h.finalizedInfo, other.finalizedInfo)
if err != nil {
return false, err
}
return eq, nil
}
// Equals returns whether h and other contain the same info.
func (h TlfHandle) Equals(
codec kbfscodec.Codec, other TlfHandle) (bool, error) {
eq, err := h.EqualsIgnoreName(codec, other)
if err != nil {
return false, err
}
if eq && h.name != other.name {
return false, nil
}
return eq, nil
}
// ToBareHandle returns a tlf.Handle corresponding to this handle.
func (h TlfHandle) ToBareHandle() (tlf.Handle, error) {
var readers []keybase1.UserOrTeamID
switch h.TypeForKeying() {
case tlf.PublicKeying:
readers = []keybase1.UserOrTeamID{
keybase1.UserOrTeamID(keybase1.PUBLIC_UID)}
case tlf.TeamKeying:
// Leave readers blank.
default:
readers = h.unsortedResolvedReaders()
}
return tlf.MakeHandle(
h.unsortedResolvedWriters(), readers,
h.unresolvedWriters, h.unresolvedReaders,
h.Extensions())
}
// ToBareHandleOrBust returns a tlf.Handle corresponding to this
// handle, and panics if there's an error. Used by tests.
func (h TlfHandle) ToBareHandleOrBust() tlf.Handle {
bh, err := h.ToBareHandle()
if err != nil {
panic(err)
}
return bh
}
func (h TlfHandle) deepCopy() *TlfHandle {
hCopy := TlfHandle{
tlfType: h.tlfType,
name: h.name,
unresolvedWriters: h.UnresolvedWriters(),
unresolvedReaders: h.UnresolvedReaders(),
conflictInfo: h.ConflictInfo(),
finalizedInfo: h.FinalizedInfo(),
tlfID: h.tlfID,
}
hCopy.resolvedWriters = make(map[keybase1.UserOrTeamID]kbname.NormalizedUsername, len(h.resolvedWriters))
for k, v := range h.resolvedWriters {
hCopy.resolvedWriters[k] = v
}
hCopy.resolvedReaders = make(map[keybase1.UserOrTeamID]kbname.NormalizedUsername, len(h.resolvedReaders))
for k, v := range h.resolvedReaders {
hCopy.resolvedReaders[k] = v
}
return &hCopy
}
// GetCanonicalName returns the canonical name of this TLF.
func (h *TlfHandle) GetCanonicalName() tlf.CanonicalName {
if h.name == "" {
panic(fmt.Sprintf("TlfHandle %v with no name", h))
}
return h.name
}
// GetCanonicalPath returns the full canonical path of this TLF.
func (h *TlfHandle) GetCanonicalPath() string {
return buildCanonicalPathForTlfName(h.Type(), h.GetCanonicalName())
}
// ToFavorite converts a TlfHandle into a Favorite, suitable for
// Favorites calls.
func (h *TlfHandle) ToFavorite() Favorite {
return Favorite{
Name: string(h.GetCanonicalName()),
Type: h.Type(),
}
}
// ToFavorite converts a TlfHandle into a Favorite, and sets internal
// state about whether the corresponding folder was just created or
// not.
func (h *TlfHandle) toFavToAdd(created bool) favToAdd {
return favToAdd{
Favorite: h.ToFavorite(),
created: created,
}
}
func getSortedUnresolved(unresolved map[keybase1.SocialAssertion]bool) []keybase1.SocialAssertion {
var assertions []keybase1.SocialAssertion
for sa := range unresolved {
assertions = append(assertions, sa)
}
sort.Sort(tlf.SocialAssertionList(assertions))
return assertions
}
// splitAndNormalizeTLFName takes a tlf name as a string
// and tries to normalize it offline. In addition to other
// checks it returns TlfNameNotCanonical if it does not
// look canonical.
// Note that ordering differences do not result in TlfNameNotCanonical
// being returned.
func splitAndNormalizeTLFName(name string, t tlf.Type) (
writerNames, readerNames []string,
extensionSuffix string, err error) {
writerNames, readerNames, extensionSuffix, err = tlf.SplitName(name)
if err != nil {
return nil, nil, "", err
}
if t == tlf.SingleTeam && len(writerNames) != 1 {
// No team folder can have more than one writer.
return nil, nil, "", NoSuchNameError{Name: name}
}
hasReaders := len(readerNames) != 0
if t != tlf.Private && hasReaders {
// No public/team folder can have readers.
return nil, nil, "", NoSuchNameError{Name: name}
}
normalizedName, changes, err := normalizeNamesInTLF(
writerNames, readerNames, t, extensionSuffix)
if err != nil {
return nil, nil, "", err
}
// Check for changes - not just ordering differences here.
if changes {
return nil, nil, "", errors.WithStack(TlfNameNotCanonical{name, normalizedName})
}
return writerNames, readerNames, strings.ToLower(extensionSuffix), nil
}
// TODO: this function can likely be replaced with a call to
// AssertionParseAndOnly when CORE-2967 and CORE-2968 are fixed.
func normalizeAssertionOrName(s string, t tlf.Type) (string, error) {
if kbname.CheckUsername(s) {
return kbname.NewNormalizedUsername(s).String(), nil
}
// TODO: this fails for http and https right now (see CORE-2968).
socialAssertion, isSocialAssertion := externals.NormalizeSocialAssertionStatic(s)
if isSocialAssertion {
if t == tlf.SingleTeam {
return "", fmt.Errorf(
"No social assertions allowed for team TLF: %s", s)
}
return socialAssertion.String(), nil
}
sAssertion := s
if t == tlf.SingleTeam {
sAssertion = "team:" + s
}
if expr, err := externals.AssertionParseAndOnlyStatic(sAssertion); err == nil {
// If the expression only contains a single url, make sure
// it's not a just considered a single keybase username. If
// it is, then some non-username slipped into the default
// "keybase" case and should be considered an error.
urls := expr.CollectUrls(nil)
if len(urls) == 1 && urls[0].IsKeybase() {
return "", NoSuchUserError{s}
}
// Normalize and return. Ideally `AssertionParseAndOnly`
// would normalize for us, but that doesn't work yet, so for
// now we'll just lower-case. This will incorrectly lower
// case http/https/web assertions, as well as case-sensitive
// social assertions in AND expressions. TODO: see CORE-2967.
return strings.ToLower(s), nil
}
return "", BadTLFNameError{s}
}
// normalizeNames normalizes a slice of names and returns
// whether any of them changed.
func normalizeNames(names []string, t tlf.Type) (changesMade bool, err error) {
for i, name := range names {
x, err := normalizeAssertionOrName(name, t)
if err != nil {
return false, err
}
if x != name {
names[i] = x
changesMade = true
}
}
return changesMade, nil
}
// normalizeNamesInTLF takes a split TLF name and, without doing any
// resolutions or identify calls, normalizes all elements of the
// name. It then returns the normalized name and a boolean flag
// whether any names were modified.
// This modifies the slices passed as arguments.
func normalizeNamesInTLF(writerNames, readerNames []string,
t tlf.Type, extensionSuffix string) (normalizedName string,
changesMade bool, err error) {
changesMade, err = normalizeNames(writerNames, t)
if err != nil {
return "", false, err
}
sort.Strings(writerNames)
normalizedName = strings.Join(writerNames, ",")
if len(readerNames) > 0 {
rchanges, err := normalizeNames(readerNames, t)
if err != nil {
return "", false, err
}
changesMade = changesMade || rchanges
sort.Strings(readerNames)
normalizedName += tlf.ReaderSep + strings.Join(readerNames, ",")
}
if len(extensionSuffix) != 0 {
// This *should* be normalized already but make sure. I can see not
// doing so might surprise a caller.
nExt := strings.ToLower(extensionSuffix)
normalizedName += tlf.HandleExtensionSep + nExt
changesMade = changesMade || nExt != extensionSuffix
}
return normalizedName, changesMade, nil
}
// CheckTlfHandleOffline does light checks whether a TLF handle looks ok,
// it avoids all network calls.
func CheckTlfHandleOffline(
ctx context.Context, name string, t tlf.Type) error {
_, _, _, err := splitAndNormalizeTLFName(name, t)
return err
}
// IsFinal returns whether or not this TlfHandle represents a finalized
// top-level folder.
func (h TlfHandle) IsFinal() bool {
return h.finalizedInfo != nil
}
// IsConflict returns whether or not this TlfHandle represents a conflicted
// top-level folder.
func (h TlfHandle) IsConflict() bool {
return h.conflictInfo != nil
}
// GetPreferredFormat returns a TLF name formatted with the username given
// as the parameter first.
// This calls tlf.CanonicalToPreferredName with the canonical
// tlf name which will be reordered into the preferred format.
// An empty username is allowed here and results in the canonical ordering.
func (h TlfHandle) GetPreferredFormat(
username kbname.NormalizedUsername) tlf.PreferredName {
s, err := tlf.CanonicalToPreferredName(username, h.GetCanonicalName())
if err != nil {
panic("TlfHandle.GetPreferredFormat: Parsing canonical username failed!")
}
return s
}