forked from keybase/client
/
device_history.go
211 lines (185 loc) · 5.21 KB
/
device_history.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"errors"
"fmt"
"time"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol"
)
// DeviceHistory is an engine.
type DeviceHistory struct {
libkb.Contextified
username string
user *libkb.User
devices []keybase1.DeviceDetail
}
// NewDeviceHistory creates a DeviceHistory engine to lookup the
// device history for username.
func NewDeviceHistory(g *libkb.GlobalContext, username string) *DeviceHistory {
return &DeviceHistory{
Contextified: libkb.NewContextified(g),
username: username,
}
}
// NewDeviceHistorySelf creates a DeviceHistory engine to lookup
// the device history of the current user.
func NewDeviceHistorySelf(g *libkb.GlobalContext) *DeviceHistory {
return &DeviceHistory{
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *DeviceHistory) Name() string {
return "DeviceHistory"
}
// GetPrereqs returns the engine prereqs.
func (e *DeviceHistory) Prereqs() Prereqs {
if len(e.username) > 0 {
return Prereqs{}
}
return Prereqs{Session: true}
}
// RequiredUIs returns the required UIs.
func (e *DeviceHistory) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *DeviceHistory) SubConsumers() []libkb.UIConsumer {
return nil
}
// Run starts the engine.
func (e *DeviceHistory) Run(ctx *Context) error {
if err := e.loadUser(); err != nil {
return err
}
if err := e.loadDevices(); err != nil {
return err
}
return nil
}
func (e *DeviceHistory) Devices() []keybase1.DeviceDetail {
return e.devices
}
func (e *DeviceHistory) loadUser() error {
arg := libkb.NewLoadUserPubOptionalArg(e.G())
if len(e.username) == 0 {
arg.Self = true
} else {
arg.Name = e.username
}
u, err := libkb.LoadUser(arg)
if err != nil {
return err
}
e.user = u
return nil
}
func (e *DeviceHistory) loadDevices() error {
ckf := e.user.GetComputedKeyFamily()
if ckf == nil {
return errors.New("nil ComputedKeyFamily for user")
}
ckis := e.user.GetComputedKeyInfos()
if ckis == nil {
return errors.New("nil ComputedKeyInfos for user")
}
for _, d := range ckf.GetAllDevices() {
exp := keybase1.DeviceDetail{Device: *(d.ProtExport())}
cki, ok := ckis.Infos[d.Kid]
if !ok {
return fmt.Errorf("no ComputedKeyInfo for device %s, kid %s", d.ID, d.Kid)
}
if cki.Eldest {
exp.Eldest = true
} else {
prov, err := e.provisioner(d, ckis, cki)
if err != nil {
return err
}
if prov != nil {
exp.Provisioner = prov.ProtExport()
t := keybase1.TimeFromSeconds(cki.DelegatedAt.Unix)
exp.ProvisionedAt = &t
}
}
if cki.RevokedAt != nil {
rt := keybase1.TimeFromSeconds(cki.RevokedAt.Unix)
exp.RevokedAt = &rt
}
if !cki.RevokedBy.IsNil() {
exp.RevokedBy = cki.RevokedBy
if deviceID, ok := ckis.KIDToDeviceID[cki.RevokedBy]; ok {
if device, ok := ckis.Devices[deviceID]; ok {
exp.RevokedByDevice = device.ProtExport()
}
}
}
if e.G().Env.GetDeviceID().Eq(d.ID) {
exp.CurrentDevice = true
}
e.devices = append(e.devices, exp)
}
// Load the last used times, but only if these are your own devices. The
// API won't give you those times for other people's devices.
if e.user.GetNormalizedName().Eq(e.G().Env.GetUsername()) {
lastUsedTimes, err := e.getLastUsedTimes()
if err != nil {
return err
}
for i := range e.devices {
detail := &e.devices[i]
lastUsedTime, ok := lastUsedTimes[detail.Device.DeviceID]
if !ok {
if detail.RevokedAt != nil {
// The server only provides last used times for active devices.
continue
}
return fmt.Errorf("Failed to load last used time for device %s", detail.Device.DeviceID)
}
detail.Device.LastUsedTime = keybase1.TimeFromSeconds(lastUsedTime.Unix())
}
}
return nil
}
func (e *DeviceHistory) provisioner(d *libkb.Device, ckis *libkb.ComputedKeyInfos, info *libkb.ComputedKeyInfo) (*libkb.Device, error) {
for _, v := range info.Delegations {
if v.GetKeyType() != libkb.KIDNaclEddsa {
// only concerned with device history, not pgp provisioners
continue
}
did, ok := ckis.KIDToDeviceID[v]
if !ok {
return nil, fmt.Errorf("device %s provisioned by kid %s, but couldn't find matching device ID in ComputedKeyInfos", d.ID, v)
}
prov, ok := ckis.Devices[did]
if !ok {
return nil, fmt.Errorf("device %s provisioned by device %s, but couldn't find matching device in ComputedKeyInfos", d.ID, did)
}
return prov, nil
}
return nil, nil
}
func (e *DeviceHistory) getLastUsedTimes() (map[keybase1.DeviceID]time.Time, error) {
uid := e.G().GetMyUID()
var err error
var devs libkb.DeviceKeyMap
aerr := e.G().LoginState().Account(func(a *libkb.Account) {
if err = libkb.RunSyncer(a.SecretSyncer(), uid, a.LoggedIn(), a.LocalSession()); err != nil {
return
}
devs, err = a.SecretSyncer().ActiveDevices(libkb.AllDeviceTypes)
}, "DeviceHistory - ")
if aerr != nil {
return nil, aerr
}
if err != nil {
return nil, err
}
ret := map[keybase1.DeviceID]time.Time{}
for deviceID, dev := range devs {
ret[deviceID] = time.Unix(dev.LastUsedTime, 0)
}
return ret, nil
}