forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identify.go
371 lines (305 loc) · 9.54 KB
/
identify.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"fmt"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol"
)
// Identify is an engine to identify a user.
type Identify struct {
arg *IdentifyArg
user *libkb.User
me *libkb.User
userExpr libkb.AssertionExpression
outcome *libkb.IdentifyOutcome
trackInst *libkb.TrackInstructions
trackToken keybase1.TrackToken
selfShortCircuit bool
libkb.Contextified
}
type IdentifyArg struct {
TargetUsername string // The user being identified, leave blank to identify self
WithTracking bool // true if want tracking statement for logged in user on TargetUsername
AllowSelf bool // if we're allowed to id/track ourself
ForceRemoteCheck bool // true: skip proof cache and perform all remote proof checks
// When tracking is being performed, the identify engine is used with a tracking ui.
// These options are sent to the ui based on command line options.
// For normal identify, safe to leave these in their default zero state.
TrackOptions keybase1.TrackOptions
Source keybase1.ClientType
Reason keybase1.IdentifyReason
}
func NewIdentifyArg(targetUsername string, withTracking, forceRemoteCheck bool) *IdentifyArg {
return &IdentifyArg{
TargetUsername: targetUsername,
WithTracking: withTracking,
AllowSelf: true,
ForceRemoteCheck: forceRemoteCheck,
}
}
func NewIdentifyTrackArg(targetUsername string, withTracking, forceRemoteCheck bool, options keybase1.TrackOptions) *IdentifyArg {
return &IdentifyArg{
TargetUsername: targetUsername,
WithTracking: withTracking,
TrackOptions: options,
AllowSelf: false,
ForceRemoteCheck: forceRemoteCheck,
}
}
func (ia *IdentifyArg) SelfID() bool {
return len(ia.TargetUsername) == 0
}
// NewIdentify creates a Identify engine.
func NewIdentify(arg *IdentifyArg, g *libkb.GlobalContext) *Identify {
return &Identify{
arg: arg,
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *Identify) Name() string {
return "Identify"
}
// GetPrereqs returns the engine prereqs.
func (e *Identify) Prereqs() Prereqs {
// if WithTracking is on, we need to be logged in
return Prereqs{Device: e.arg.WithTracking}
}
// RequiredUIs returns the required UIs.
func (e *Identify) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{
libkb.IdentifyUIKind,
}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *Identify) SubConsumers() []libkb.UIConsumer {
return nil
}
// Run starts the engine.
func (e *Identify) Run(ctx *Context) error {
if err := e.loadUser(); err != nil {
return err
}
ok, uid, err := IsLoggedIn(e, ctx)
if err != nil {
return err
}
if ok {
e.me, err = libkb.LoadMeByUID(e.G(), uid)
if err != nil {
return err
}
if e.user.Equal(e.me) {
e.arg.WithTracking = false
if e.arg.Source == keybase1.ClientType_KBFS {
// if this is a self identify from kbfs, then short-circuit the identify process:
return e.shortCircuitSelfID(ctx)
}
} else {
e.arg.WithTracking = true
}
}
ctx.IdentifyUI.Start(e.user.GetName(), e.arg.Reason)
e.outcome, err = e.run(ctx)
if err != nil {
return err
}
// set the flags in the outcome with the track options to
// inform the ui what to do with the remote tracking prompt:
e.outcome.TrackOptions = e.arg.TrackOptions
e.G().Log.Debug("inserting identify outcome for %q in TrackCache", e.user.GetName())
key, err := e.G().TrackCache.Insert(e.outcome)
if err != nil {
return err
}
e.G().Log.Debug("TrackCache key: %q", key)
e.trackToken = key
return nil
}
func (e *Identify) User() *libkb.User {
return e.user
}
func (e *Identify) Outcome() *libkb.IdentifyOutcome {
return e.outcome
}
func (e *Identify) TrackToken() keybase1.TrackToken {
return e.trackToken
}
func (e *Identify) TrackInstructions() *libkb.TrackInstructions {
return e.trackInst
}
func (e *Identify) run(ctx *Context) (*libkb.IdentifyOutcome, error) {
res := libkb.NewIdentifyOutcome()
res.Username = e.user.GetName()
is := libkb.NewIdentifyState(res, e.user)
if e.me != nil && e.user.Equal(e.me) && !e.arg.AllowSelf {
return nil, libkb.SelfTrackError{}
}
if e.arg.WithTracking {
if e.me == nil {
return nil, libkb.LoginRequiredError{Context: "identify with tracking"}
}
tlink, err := e.me.TrackChainLinkFor(e.user.GetName(), e.user.GetUID())
if err != nil {
return nil, err
}
if tlink != nil {
is.SetTrackLookup(tlink)
if ttcl, _ := e.me.TmpTrackChainLinkFor(e.user.GetName(), e.user.GetUID()); ttcl != nil {
is.SetTmpTrackLookup(ttcl)
}
}
}
if !e.user.HasActiveKey() {
return nil, libkb.NoActiveKeyError{Username: e.user.GetName()}
}
ctx.IdentifyUI.ReportLastTrack(libkb.ExportTrackSummary(is.TrackLookup(), e.user.GetName()))
e.G().Log.Debug("+ Identify(%s)", e.user.GetName())
is.Precompute(ctx.IdentifyUI.DisplayKey)
ctx.IdentifyUI.LaunchNetworkChecks(res.ExportToUncheckedIdentity(), e.user.Export())
waiter := e.displayUserCardAsync(ctx)
if err := e.user.IDTable().Identify(is, e.arg.ForceRemoteCheck, ctx.IdentifyUI, nil); err != nil {
return nil, err
}
if err := <-waiter; err != nil {
return nil, err
}
base := e.user.BaseProofSet()
res.AddProofsToSet(base, []keybase1.ProofState{keybase1.ProofState_OK})
if !e.userExpr.MatchSet(*base) {
return nil, fmt.Errorf("User %s didn't match given assertion", e.user.GetName())
}
e.G().Log.Debug("- Identify(%s)", e.user.GetName())
return res, nil
}
func (e *Identify) loadUser() error {
arg, err := e.loadUserArg()
if err != nil {
return err
}
u, err := libkb.LoadUser(*arg)
if err != nil {
return err
}
e.user = u
if arg.Self {
// if this was a self load, need to load an assertion expression
// now that we have the username
if err := e.loadExpr(e.user.GetName()); err != nil {
return err
}
}
return nil
}
func (e *Identify) loadUserArg() (*libkb.LoadUserArg, error) {
arg := libkb.NewLoadUserArg(e.G())
if e.arg.SelfID() {
// loading self
arg.Self = true
return &arg, nil
}
// Use assertions for everything:
if err := e.loadExpr(e.arg.TargetUsername); err != nil {
return nil, err
}
// Next, pop off the 'best' assertion and load the user by it.
// That is, it might be the keybase assertion (if there), or otherwise,
// something that's unique like Twitter or Github, and lastly,
// something like DNS that is more likely ambiguous...
b := libkb.FindBestIdentifyComponent(e.userExpr)
if len(b) == 0 {
return nil, fmt.Errorf("Cannot lookup user with %q", e.arg.TargetUsername)
}
arg.Name = b
arg.PublicKeyOptional = true
return &arg, nil
}
func (e *Identify) loadExpr(assertion string) error {
// Parse assertion but don't allow OR operators, only AND operators
expr, err := libkb.AssertionParseAndOnly(assertion)
if err != nil {
return fmt.Errorf("assertion parse error: %s", err)
}
e.userExpr = expr
return nil
}
func (e *Identify) shortCircuitSelfID(ctx *Context) error {
e.G().Log.Debug("Identify: short-circuiting self identification")
e.selfShortCircuit = true
// don't really need anything but username here
e.outcome = libkb.NewIdentifyOutcome()
e.outcome.Username = e.user.GetName()
return nil
}
// DidShortCircuit returns true if shortCircuitSelfID happened.
func (e *Identify) DidShortCircuit() bool {
return e.selfShortCircuit
}
type card struct {
Status libkb.AppStatus `json:"status"`
FollowSummary struct {
Following int `json:"following"`
Followers int `json:"followers"`
} `json:"follow_summary"`
Profile struct {
FullName string `json:"full_name"`
Location string `json:"location"`
Bio string `json:"bio"`
Website string `json:"website"`
Twitter string `json:"twitter"`
} `json:"profile"`
YouFollowThem bool `json:"you_follow_them"`
TheyFollowYou bool `json:"they_follow_you"`
}
func (c *card) GetAppStatus() *libkb.AppStatus {
return &c.Status
}
func getUserCard(g *libkb.GlobalContext, uid keybase1.UID, useSession bool) (ret *keybase1.UserCard, err error) {
defer g.Trace("getUserCard", func() error { return err })()
arg := libkb.APIArg{
Endpoint: "user/card",
NeedSession: useSession,
Contextified: libkb.NewContextified(g),
Args: libkb.HTTPArgs{"uid": libkb.S{Val: uid.String()}},
}
var card card
if err = g.API.GetDecode(arg, &card); err != nil {
g.Log.Warning("error getting user/card for %s: %s\n", uid, err)
return nil, err
}
g.Log.Debug("user card: %+v", card)
ret = &keybase1.UserCard{
Following: card.FollowSummary.Following,
Followers: card.FollowSummary.Followers,
Uid: uid,
FullName: card.Profile.FullName,
Location: card.Profile.Location,
Bio: card.Profile.Bio,
Website: card.Profile.Website,
Twitter: card.Profile.Twitter,
YouFollowThem: card.YouFollowThem,
TheyFollowYou: card.TheyFollowYou,
}
return ret, nil
}
func displayUserCard(g *libkb.GlobalContext, ctx *Context, uid keybase1.UID, useSession bool) error {
card, err := getUserCard(g, uid, useSession)
if err != nil {
return err
}
if card == nil {
return nil
}
return ctx.IdentifyUI.DisplayUserCard(*card)
}
func displayUserCardAsync(g *libkb.GlobalContext, ctx *Context, uid keybase1.UID, useSession bool) <-chan error {
ch := make(chan error)
go func() {
ch <- displayUserCard(g, ctx, uid, useSession)
}()
return ch
}
func (e *Identify) displayUserCardAsync(ctx *Context) <-chan error {
return displayUserCardAsync(e.G(), ctx, e.user.GetUID(), (e.me != nil))
}