forked from keybase/client
/
login_provisioned_device.go
204 lines (177 loc) · 6.25 KB
/
login_provisioned_device.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"github.com/keybase/client/go/libkb"
)
// LoginProvisionedDevice is an engine that tries to login using the
// current device, if there is an existing provisioned device.
type LoginProvisionedDevice struct {
libkb.Contextified
username string
SecretStoreOnly bool // this should only be set by the service on its startup login attempt
}
// newLoginCurrentDevice creates a loginProvisionedDevice engine.
func NewLoginProvisionedDevice(g *libkb.GlobalContext, username string) *LoginProvisionedDevice {
return &LoginProvisionedDevice{
username: username,
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *LoginProvisionedDevice) Name() string {
return "LoginProvisionedDevice"
}
// GetPrereqs returns the engine prereqs.
func (e *LoginProvisionedDevice) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *LoginProvisionedDevice) RequiredUIs() []libkb.UIKind {
if e.SecretStoreOnly {
return []libkb.UIKind{}
}
return []libkb.UIKind{
libkb.LoginUIKind,
libkb.SecretUIKind,
}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *LoginProvisionedDevice) SubConsumers() []libkb.UIConsumer {
return nil
}
func (e *LoginProvisionedDevice) Run(ctx *Context) error {
if err := e.run(ctx); err != nil {
return err
}
e.G().Log.Debug("LoginProvisionedDevice success, sending login notification")
e.G().NotifyRouter.HandleLogin(string(e.G().Env.GetUsername()))
e.G().Log.Debug("LoginProvisionedDevice success, calling login hooks")
e.G().CallLoginHooks()
return nil
}
func (e *LoginProvisionedDevice) run(ctx *Context) error {
// already logged in?
in, err := e.G().LoginState().LoggedInProvisionedCheck()
if err == nil && in {
if len(e.username) == 0 || e.G().Env.GetUsername() == libkb.NewNormalizedUsername(e.username) {
// already logged in, make sure to unlock device keys
var partialCopy *libkb.User
err = e.G().GetFullSelfer().WithSelf(ctx.NetContext, func(user *libkb.User) error {
// We don't want to hold onto the full cached user during
// the whole `unlockDeviceKey` run below, which touches
// a lot of (potentially reentrant) code. A partial copy
// will suffice. We won't need the non-copied fields
// (like the sigchain and ID table).
partialCopy = user.PartialCopy()
return nil
})
if err != nil {
return err
}
return e.unlockDeviceKeys(ctx, partialCopy)
}
}
var config *libkb.UserConfig
loadUserArg := libkb.LoadUserArg{
PublicKeyOptional: true,
ForceReload: true,
}
var nu libkb.NormalizedUsername
if len(e.username) == 0 {
e.G().Log.Debug("| using current username")
config, err = e.G().Env.GetConfig().GetUserConfig()
loadUserArg.Self = true
} else {
e.G().Log.Debug("| using new username %s", e.username)
nu = libkb.NewNormalizedUsername(e.username)
config, err = e.G().Env.GetConfig().GetUserConfigForUsername(nu)
loadUserArg.Name = e.username
}
if err != nil {
e.G().Log.Debug("error getting user config: %s (%T)", err, err)
return errNoConfig
}
if config == nil {
e.G().Log.Debug("user config is nil")
return errNoConfig
}
deviceID := config.GetDeviceID()
if deviceID.IsNil() {
e.G().Log.Debug("no device in user config")
return errNoDevice
}
// Make sure the device ID is still valid.
me, err := libkb.LoadUser(loadUserArg)
if err != nil {
e.G().Log.Debug("error loading user profile: %#v", err)
return err
}
if !me.HasDeviceInCurrentInstall(deviceID) {
e.G().Log.Debug("current device is not valid")
// If our config file is showing that we have a bogus
// deviceID (maybe from our account before an account reset),
// then we'll delete it from the config file here, so later parts
// of provisioning aren't confused by this device ID.
err := e.G().Env.GetConfigWriter().NukeUser(nu)
if err != nil {
e.G().Log.Warning("Error clearing user config: %s", err)
}
return errNoDevice
}
// set e.username so that LoginUI never needs to ask for it
e.username = me.GetName()
// at this point, there is a user config either for the current user or for e.username
// and it has a device id, so this should be a provisioned device. Thus, they should
// just login normally.
var afterLogin = func(lctx libkb.LoginContext) error {
if err := lctx.LocalSession().SetDeviceProvisioned(e.G().Env.GetDeviceID()); err != nil {
// not a fatal error, session will stay in memory
e.G().Log.Warning("error saving session file: %s", err)
}
return nil
}
if e.SecretStoreOnly {
if err := e.G().LoginState().LoginWithStoredSecret(e.username, afterLogin); err != nil {
return err
}
} else {
if err := e.G().LoginState().LoginWithPrompt(e.username, ctx.LoginUI, ctx.SecretUI, afterLogin); err != nil {
return err
}
}
// login was successful, unlock the device keys
err = e.unlockDeviceKeys(ctx, me)
if err != nil {
return err
}
return nil
}
func (e *LoginProvisionedDevice) unlockDeviceKeys(ctx *Context, me *libkb.User) error {
// CORE-5876 idea that lksec will be unusable if reachablity state is NO
// and the user changed passphrase with a different device since it won't
// be able to sync the new server half.
if e.G().ConnectivityMonitor.IsConnected(ctx.NetContext) != libkb.ConnectivityMonitorYes {
e.G().Log.Debug("LoginProvisionedDevice: in unlockDeviceKeys, ConnectivityMonitor says not reachable, check to make sure")
if err := e.G().ConnectivityMonitor.CheckReachability(ctx.NetContext); err != nil {
e.G().Log.Debug("error checking reachability: %s", err)
} else {
connected := e.G().ConnectivityMonitor.IsConnected(ctx.NetContext)
e.G().Log.Debug("after CheckReachability(), IsConnected() => %v (connected? %v)", connected, connected == libkb.ConnectivityMonitorYes)
}
}
ska := libkb.SecretKeyArg{
Me: me,
KeyType: libkb.DeviceSigningKeyType,
}
_, err := e.G().Keyrings.GetSecretKeyWithPrompt(ctx.SecretKeyPromptArg(ska, "unlock device keys"))
if err != nil {
return err
}
ska.KeyType = libkb.DeviceEncryptionKeyType
_, err = e.G().Keyrings.GetSecretKeyWithPrompt(ctx.SecretKeyPromptArg(ska, "unlock device keys"))
if err != nil {
return err
}
return nil
}