-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
bootstrap.go
110 lines (91 loc) · 2.9 KB
/
bootstrap.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
// Copyright 2017 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"time"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
// Bootstrap is an engine.
type Bootstrap struct {
libkb.Contextified
status keybase1.BootstrapStatus
}
// NewBootstrap creates a Bootstrap engine.
func NewBootstrap(g *libkb.GlobalContext) *Bootstrap {
return &Bootstrap{
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *Bootstrap) Name() string {
return "Bootstrap"
}
// GetPrereqs returns the engine prereqs.
func (e *Bootstrap) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *Bootstrap) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *Bootstrap) SubConsumers() []libkb.UIConsumer {
return nil
}
func (e *Bootstrap) lookupFullname(m libkb.MetaContext, uv keybase1.UserVersion) {
pkgs, err := m.G().UIDMapper.MapUIDsToUsernamePackagesOffline(m.Ctx(), m.G(), []keybase1.UID{uv.Uid}, time.Duration(0))
if err != nil {
m.Warning("UID -> Username failed lookup: %s", err)
return
}
pkg := pkgs[0]
if pkg.NormalizedUsername.IsNil() || pkg.FullName == nil {
m.Debug("Empty username for UID=%s", uv.Uid)
return
}
if !uv.EldestSeqno.Eq(pkg.FullName.EldestSeqno) {
m.Debug("Wrong eldest for username package; got %d but wanted %d", pkg.FullName.EldestSeqno, uv.EldestSeqno)
return
}
e.status.Fullname = pkg.FullName.FullName
}
// Run starts the engine.
func (e *Bootstrap) Run(m libkb.MetaContext) error {
e.status.Registered = e.signedUp(m)
// if any Login engine worked previously, then ActiveDevice will
// be valid:
validActiveDevice := m.G().ActiveDevice.Valid()
// the only way for ActiveDevice to be valid is to be logged in
// (and provisioned)
e.status.LoggedIn = validActiveDevice
if !e.status.LoggedIn {
m.Debug("Bootstrap: not logged in")
return nil
}
m.Debug("Bootstrap: logged in (valid active device)")
var uv keybase1.UserVersion
uv, e.status.DeviceID, e.status.DeviceName, _, _ = e.G().ActiveDevice.AllFields()
e.status.Uid = uv.Uid
e.status.Username = e.G().ActiveDevice.Username(m).String()
m.Debug("Bootstrap status: uid=%s, username=%s, deviceID=%s, deviceName=%s", e.status.Uid, e.status.Username, e.status.DeviceID, e.status.DeviceName)
if chatHelper := e.G().ChatHelper; chatHelper != nil {
e.status.UserReacjis = chatHelper.UserReacjis(m.Ctx(), e.status.Uid.ToBytes())
}
e.lookupFullname(m, uv)
return nil
}
// signedUp is true if there's a uid in config.json.
func (e *Bootstrap) signedUp(m libkb.MetaContext) bool {
cr := m.G().Env.GetConfig()
if cr == nil {
return false
}
if uid := cr.GetUID(); uid.Exists() {
return true
}
return false
}
func (e *Bootstrap) Status() keybase1.BootstrapStatus {
return e.status
}