forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader_ctx.go
226 lines (201 loc) · 7.73 KB
/
loader_ctx.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package teams
import (
"context"
"fmt"
"strings"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
// Things TeamLoader uses that are mocked out for tests.
type LoaderContext interface {
// Get new links from the server.
getNewLinksFromServer(ctx context.Context,
teamID keybase1.TeamID, lows getLinksLows,
readSubteamID *keybase1.TeamID) (*rawTeam, error)
// Get full links from the server.
// Does not guarantee that the server returned the correct links, nor that they are unstubbed.
getLinksFromServer(ctx context.Context,
teamID keybase1.TeamID, requestSeqnos []keybase1.Seqno,
readSubteamID *keybase1.TeamID) (*rawTeam, error)
getMe(context.Context) (keybase1.UserVersion, error)
// Lookup the eldest seqno of a user. Can use the cache.
lookupEldestSeqno(context.Context, keybase1.UID) (keybase1.Seqno, error)
resolveNameToIDUntrusted(context.Context, keybase1.TeamName) (keybase1.TeamID, error)
// Get the current user's per-user-key's derived encryption key (full).
perUserEncryptionKey(ctx context.Context, userSeqno keybase1.Seqno) (*libkb.NaclDHKeyPair, error)
merkleLookup(ctx context.Context, teamID keybase1.TeamID) (r1 keybase1.Seqno, r2 keybase1.LinkID, err error)
merkleLookupTripleAtHashMeta(ctx context.Context, leafID keybase1.UserOrTeamID, hm keybase1.HashMeta) (triple *libkb.MerkleTriple, err error)
forceLinkMapRefreshForUser(ctx context.Context, uid keybase1.UID) (linkMap map[keybase1.Seqno]keybase1.LinkID, err error)
loadKeyV2(ctx context.Context, uid keybase1.UID, kid keybase1.KID) (keybase1.UserVersion, *keybase1.PublicKeyV2NaCl, map[keybase1.Seqno]keybase1.LinkID, error)
}
// The main LoaderContext is G.
type LoaderContextG struct {
libkb.Contextified
}
var _ LoaderContext = (*LoaderContextG)(nil)
func NewLoaderContextFromG(g *libkb.GlobalContext) LoaderContext {
return &LoaderContextG{
Contextified: libkb.NewContextified(g),
}
}
// Get new links from the server.
func (l *LoaderContextG) getNewLinksFromServer(ctx context.Context,
teamID keybase1.TeamID, lows getLinksLows,
readSubteamID *keybase1.TeamID) (*rawTeam, error) {
arg := libkb.NewRetryAPIArg("team/get")
arg.NetContext = ctx
arg.SessionType = libkb.APISessionTypeREQUIRED
arg.Args = libkb.HTTPArgs{
"id": libkb.S{Val: teamID.String()},
"low": libkb.I{Val: int(lows.Seqno)},
// These don't really work yet on the client or server.
// "per_team_key_low": libkb.I{Val: int(lows.PerTeamKey)},
// "reader_key_mask_low": libkb.I{Val: int(lows.PerTeamKey)},
}
if readSubteamID != nil {
arg.Args["read_subteam_id"] = libkb.S{Val: readSubteamID.String()}
}
var rt rawTeam
if err := l.G().API.GetDecode(arg, &rt); err != nil {
return nil, err
}
if !rt.ID.Eq(teamID) {
return nil, fmt.Errorf("server returned wrong team ID: %v != %v", rt.ID, teamID)
}
return &rt, nil
}
// Get full links from the server.
// Does not guarantee that the server returned the correct links, nor that they are unstubbed.
func (l *LoaderContextG) getLinksFromServer(ctx context.Context,
teamID keybase1.TeamID, requestSeqnos []keybase1.Seqno, readSubteamID *keybase1.TeamID) (*rawTeam, error) {
var seqnoStrs []string
for _, seqno := range requestSeqnos {
seqnoStrs = append(seqnoStrs, fmt.Sprintf("%d", int(seqno)))
}
seqnoCommas := strings.Join(seqnoStrs, ",")
arg := libkb.NewRetryAPIArg("team/get")
arg.NetContext = ctx
arg.SessionType = libkb.APISessionTypeREQUIRED
arg.Args = libkb.HTTPArgs{
"id": libkb.S{Val: teamID.String()},
"seqnos": libkb.S{Val: seqnoCommas},
// These don't really work yet on the client or server.
// "per_team_key_low": libkb.I{Val: int(lows.PerTeamKey)},
// "reader_key_mask_low": libkb.I{Val: int(lows.PerTeamKey)},
}
if readSubteamID != nil {
arg.Args["read_subteam_id"] = libkb.S{Val: readSubteamID.String()}
}
var rt rawTeam
if err := l.G().API.GetDecode(arg, &rt); err != nil {
return nil, err
}
if !rt.ID.Eq(teamID) {
return nil, fmt.Errorf("server returned wrong team ID: %v != %v", rt.ID, teamID)
}
return &rt, nil
}
func (l *LoaderContextG) getMe(ctx context.Context) (res keybase1.UserVersion, err error) {
loadMeArg := libkb.NewLoadUserArgBase(l.G()).
WithNetContext(ctx).
WithUID(l.G().Env.GetUID()).
WithSelf(true).
WithPublicKeyOptional()
upak, _, err := l.G().GetUPAKLoader().LoadV2(*loadMeArg)
if err != nil {
return keybase1.UserVersion{}, err
}
return NewUserVersion(upak.Current.Uid, upak.Current.EldestSeqno), nil
}
func (l *LoaderContextG) lookupEldestSeqno(ctx context.Context, uid keybase1.UID) (keybase1.Seqno, error) {
// Lookup the latest eldest seqno for that uid.
// This value may come from a cache.
upak, err := loadUPAK2(ctx, l.G(), uid, false /*forcePoll */)
if err != nil {
return keybase1.Seqno(1), err
}
return upak.Current.EldestSeqno, nil
}
// Resolve a team name to a team ID.
// Will always hit the server for subteams. The server can lie in this return value.
func (l *LoaderContextG) resolveNameToIDUntrusted(ctx context.Context, teamName keybase1.TeamName) (id keybase1.TeamID, err error) {
// For root team names, just hash.
if teamName.IsRootTeam() {
return teamName.ToTeamID(), nil
}
arg := libkb.NewRetryAPIArg("team/get")
arg.NetContext = ctx
arg.SessionType = libkb.APISessionTypeREQUIRED
arg.Args = libkb.HTTPArgs{
"name": libkb.S{Val: teamName.String()},
"lookup_only": libkb.B{Val: true},
}
var rt rawTeam
if err := l.G().API.GetDecode(arg, &rt); err != nil {
return id, err
}
id = rt.ID
if !id.Exists() {
return id, fmt.Errorf("could not resolve team name: %v", teamName.String())
}
return id, nil
}
func (l *LoaderContextG) perUserEncryptionKey(ctx context.Context, userSeqno keybase1.Seqno) (*libkb.NaclDHKeyPair, error) {
kr, err := l.G().GetPerUserKeyring()
if err != nil {
return nil, err
}
// Try to get it locally, if that fails try again after syncing.
encKey, err := kr.GetEncryptionKeyBySeqno(ctx, userSeqno)
if err == nil {
return encKey, err
}
if err := kr.Sync(ctx); err != nil {
return nil, err
}
encKey, err = kr.GetEncryptionKeyBySeqno(ctx, userSeqno)
return encKey, err
}
func (l *LoaderContextG) merkleLookup(ctx context.Context, teamID keybase1.TeamID) (r1 keybase1.Seqno, r2 keybase1.LinkID, err error) {
leaf, err := l.G().GetMerkleClient().LookupTeam(ctx, teamID)
if err != nil {
return r1, r2, err
}
if !leaf.TeamID.Eq(teamID) {
return r1, r2, fmt.Errorf("merkle returned wrong leaf: %v != %v", leaf.TeamID.String(), teamID.String())
}
if leaf.Private == nil {
l.G().Log.CDebugf(ctx, "TeamLoader hidden error: merkle returned nil leaf")
return r1, r2, NewTeamDoesNotExistError(teamID.String())
}
return leaf.Private.Seqno, leaf.Private.LinkID.Export(), nil
}
func (l *LoaderContextG) merkleLookupTripleAtHashMeta(ctx context.Context, leafID keybase1.UserOrTeamID, hm keybase1.HashMeta) (triple *libkb.MerkleTriple, err error) {
leaf, err := l.G().MerkleClient.LookupLeafAtHashMeta(ctx, leafID, hm)
if err != nil {
return nil, err
}
if leafID.IsUser() {
triple = leaf.Public
} else {
triple = leaf.Private
}
return triple, nil
}
func (l *LoaderContextG) forceLinkMapRefreshForUser(ctx context.Context, uid keybase1.UID) (linkMap map[keybase1.Seqno]keybase1.LinkID, err error) {
arg := libkb.NewLoadUserArgBase(l.G()).WithNetContext(ctx).WithUID(uid).WithForcePoll()
upak, _, err := l.G().GetUPAKLoader().LoadV2(*arg)
if err != nil {
return nil, err
}
return upak.SeqnoLinkIDs, nil
}
func (l *LoaderContextG) loadKeyV2(ctx context.Context, uid keybase1.UID, kid keybase1.KID) (
uv keybase1.UserVersion, pubKey *keybase1.PublicKeyV2NaCl, linkMap map[keybase1.Seqno]keybase1.LinkID,
err error) {
user, pubKey, linkMap, err := l.G().GetUPAKLoader().LoadKeyV2(ctx, uid, kid)
if err != nil {
return
}
return user.ToUserVersion(), pubKey, linkMap, nil
}