forked from keybase/client
/
login_offline.go
199 lines (171 loc) · 5.48 KB
/
login_offline.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
package engine
import (
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
type LoginOffline struct {
libkb.Contextified
}
func NewLoginOffline(g *libkb.GlobalContext) *LoginOffline {
return &LoginOffline{
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *LoginOffline) Name() string {
return "LoginOffline"
}
// Prereqs returns the engine prereqs.
func (e *LoginOffline) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *LoginOffline) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *LoginOffline) SubConsumers() []libkb.UIConsumer {
return nil
}
func (e *LoginOffline) Run(ctx *Context) error {
if err := e.run(ctx); err != nil {
return err
}
e.G().Log.Debug("LoginOffline success, sending login notification")
e.G().NotifyRouter.HandleLogin(string(e.G().Env.GetUsername()))
e.G().Log.Debug("LoginOffline success, calling login hooks")
e.G().CallLoginHooks()
return nil
}
func (e *LoginOffline) run(ctx *Context) error {
var gerr error
aerr := e.G().LoginState().Account(func(a *libkb.Account) {
var in bool
in, gerr = a.LoggedInProvisioned()
if gerr != nil {
e.G().Log.Debug("LoginOffline: LoggedInProvisioned error: %s", gerr)
return
}
if !in {
e.G().Log.Debug("LoginOffline: LoggedInProvisioned says not logged in")
gerr = libkb.LoginRequiredError{}
return
}
// current user has a valid session file
e.G().Log.Debug("LoginOffline: current user has a valid session file")
// check ActiveDevice cache
uid, deviceID, _, sigKey, encKey := e.G().ActiveDevice.AllFields()
if sigKey != nil && encKey != nil {
if uid.Equal(a.GetUID()) && deviceID.Eq(a.GetDeviceID()) {
// since they match, good to go
e.G().Log.Debug("LoginOffline: found cached device keys in ActiveDevice")
return
}
}
// nothing cached, so need to load the locked keys and unlock them
// with secret store
uid = e.G().Env.GetUID()
deviceID = e.G().Env.GetDeviceIDForUID(uid)
// use the UPAKLoader with StaleOK, CachedOnly in order to get cached upak
arg := libkb.NewLoadUserByUIDArg(ctx.NetContext, e.G(), uid).WithPublicKeyOptional().WithStaleOK(true).WithCachedOnly().WithLoginContext(a)
upak, _, err := e.G().GetUPAKLoader().Load(arg)
if err != nil {
e.G().Log.Debug("LoginOffline: upak.Load err: %s", err)
// if user load fails, login required
gerr = libkb.LoginRequiredError{}
return
}
// find the sibkey
var sibkey *keybase1.PublicKey
for _, key := range upak.Base.DeviceKeys {
if key.DeviceID.Eq(deviceID) && key.IsSibkey == true {
e.G().Log.Debug("LoginOffline: device sibkey match: %+v", key)
sibkey = &key
break
}
}
if sibkey == nil {
e.G().Log.Debug("LoginOffline: no sibkey found in upak.Base.DeviceKeys")
gerr = libkb.NewLoginOfflineError("no sibkey found")
return
}
// find the subkey
var subkey *keybase1.PublicKey
for _, key := range upak.Base.DeviceKeys {
if !key.IsSibkey && key.ParentID == sibkey.KID.String() {
e.G().Log.Debug("LoginOffline: subkey match: %+v", key)
subkey = &key
break
}
}
if subkey == nil {
e.G().Log.Debug("LoginOffline: no subkey found in upak.Base.DeviceKeys")
gerr = libkb.NewLoginOfflineError("no subkey found")
return
}
// load the keyring file
username := libkb.NewNormalizedUsername(upak.Base.Username)
kr, err := libkb.LoadSKBKeyring(username, e.G())
if err != nil {
e.G().Log.Debug("LoginOffline: error loading keyring for %s: %s", username, err)
gerr = err
return
}
// get the locked keys out of the keyring
lockedSibkey := kr.LookupByKid(sibkey.KID)
if lockedSibkey == nil {
e.G().Log.Debug("LoginOffline: no locked sibkey with KID %s", sibkey.KID)
gerr = libkb.NewLoginOfflineError("no locked sibkey found in keyring")
return
}
lockedSibkey.SetUID(uid)
lockedSubkey := kr.LookupByKid(subkey.KID)
if lockedSubkey == nil {
e.G().Log.Debug("LoginOffline: no locked subkey with KID %s", subkey.KID)
gerr = libkb.NewLoginOfflineError("no locked subkey found in keyring")
return
}
lockedSubkey.SetUID(uid)
// unlock the keys with the secret store
secretStore := libkb.NewSecretStore(e.G(), username)
unlockedSibkey, err := lockedSibkey.UnlockNoPrompt(a, secretStore)
if err != nil {
e.G().Log.Debug("LoginOffline: failed to unlock sibkey: %s", err)
gerr = err
return
}
unlockedSubkey, err := lockedSubkey.UnlockNoPrompt(a, secretStore)
if err != nil {
e.G().Log.Debug("LoginOffline: failed to unlock subkey: %s", err)
gerr = err
return
}
device := &libkb.Device{
ID: deviceID,
Kid: sibkey.KID,
Description: &sibkey.DeviceDescription,
}
// cache the unlocked secret keys
ska := libkb.SecretKeyArg{KeyType: libkb.DeviceSigningKeyType}
if err := a.SetCachedSecretKey(ska, unlockedSibkey, device); err != nil {
e.G().Log.Debug("LoginOffline: failed to cache sibkey: %s", err)
gerr = err
return
}
ska = libkb.SecretKeyArg{KeyType: libkb.DeviceEncryptionKeyType}
if err := a.SetCachedSecretKey(ska, unlockedSubkey, device); err != nil {
e.G().Log.Debug("LoginOffline: failed to cache subkey: %s", err)
gerr = err
return
}
}, "LoginOffline")
if aerr != nil {
e.G().Log.Debug("LoginOffline: LoginState account error: %s", aerr)
return aerr
}
if gerr != nil {
return gerr
}
e.G().Log.Debug("LoginOffline: run success")
return nil
}