forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_keys_api.go
118 lines (99 loc) · 2.68 KB
/
user_keys_api.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
package auth
import (
libkb "github.com/keybase/client/go/libkb"
logger "github.com/keybase/client/go/logger"
keybase1 "github.com/keybase/client/go/protocol"
context "golang.org/x/net/context"
"time"
)
const (
pollWait = 5 * time.Second
)
type pubsubMessageInner struct {
UID keybase1.UID `json:"uid"`
}
type pubsubMessageOuter struct {
SyncStamp int `json:"sync_stamp"`
Message pubsubMessageInner `json:"message"`
}
type serverState struct {
InstanceID string `json:"instance_id"`
LatestSyncStamp int `json:"latest_sync_stamp"`
}
type pubsubResponse struct {
ServerState serverState `json:"server_state"`
Messages []pubsubMessageOuter `json:"messages"`
}
var _ UserKeyAPIer = (*userKeyAPI)(nil)
type userKeysResPublicKeys struct {
Sibkeys []keybase1.KID `json:"sibkeys"`
Subkeys []keybase1.KID `json:"subkeys"`
}
type userKeyRes struct {
Username string `json:"username"`
PublicKeys userKeysResPublicKeys `json:"public_keys"`
}
type userKeyAPI struct {
log logger.Logger
api libkb.API
lastSyncPoint int
instanceID string
}
func (u *userKeyAPI) GetUser(ctx context.Context, uid keybase1.UID) (un libkb.NormalizedUsername, kids []keybase1.KID, err error) {
u.log.Debug("+ GetUser")
defer func() {
u.log.Debug("- GetUser -> %v", err)
}()
var ukr userKeyRes
err = u.api.GetDecode(libkb.APIArg{
Endpoint: "user/keys",
Args: libkb.HTTPArgs{
"uid": libkb.S{Val: uid.String()},
},
}, &ukr)
if err != nil {
return "", nil, err
}
un = libkb.NewNormalizedUsername(ukr.Username)
for _, k := range ukr.PublicKeys.Sibkeys {
kids = append(kids, k)
}
for _, k := range ukr.PublicKeys.Subkeys {
kids = append(kids, k)
}
return un, kids, nil
}
func (u *userKeyAPI) PollForChanges(ctx context.Context) (uids []keybase1.UID, err error) {
u.log.Debug("+ poll")
defer func() {
u.log.Debug("- poll -> %v", err)
}()
select {
case <-ctx.Done():
return nil, ErrCanceled
case <-time.After(pollWait):
}
var psb pubsubResponse
args := libkb.HTTPArgs{
"feed": libkb.S{Val: "user.key_change"},
"last_sync_stamp": libkb.I{Val: u.lastSyncPoint},
"instance_id": libkb.S{Val: u.instanceID},
}
err = u.api.GetDecode(libkb.APIArg{
Endpoint: "pubsub/poll",
Args: args,
}, &psb)
if err != nil {
return uids, err
}
for _, message := range psb.Messages {
uids = append(uids, message.Message.UID)
}
u.lastSyncPoint = psb.ServerState.LatestSyncStamp
u.instanceID = psb.ServerState.InstanceID
return uids, err
}
// NewUserKeyAPIer returns a UserKeyAPIer implementation.
func NewUserKeyAPIer(log logger.Logger, api libkb.API) UserKeyAPIer {
return &userKeyAPI{log: log, api: api}
}