forked from keybase/client
/
pgp_keyfinder.go
109 lines (91 loc) · 2.51 KB
/
pgp_keyfinder.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
// 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"
)
// PGPKeyfinder is an engine to find PGP Keys for users (loaded by
// assertions), possibly tracking them if necessary.
type PGPKeyfinder struct {
arg *PGPKeyfinderArg
uplus []*UserPlusKeys
runerr error
libkb.Contextified
}
type PGPKeyfinderArg struct {
Usernames []string // must be keybase usernames
}
// NewPGPKeyfinder creates a PGPKeyfinder engine.
func NewPGPKeyfinder(g *libkb.GlobalContext, arg *PGPKeyfinderArg) *PGPKeyfinder {
return &PGPKeyfinder{
arg: arg,
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *PGPKeyfinder) Name() string {
return "PGPKeyfinder"
}
// GetPrereqs returns the engine prereqs.
func (e *PGPKeyfinder) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *PGPKeyfinder) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *PGPKeyfinder) SubConsumers() []libkb.UIConsumer {
return []libkb.UIConsumer{}
}
// Run starts the engine.
func (e *PGPKeyfinder) Run(m libkb.MetaContext) error {
e.loadUsers(m)
e.loadKeys(m)
return e.runerr
}
// UsersPlusKeys returns the users found while running the engine,
// plus their pgp keys.
func (e *PGPKeyfinder) UsersPlusKeys() []*UserPlusKeys {
return e.uplus
}
// don't identify or track, just load the users
func (e *PGPKeyfinder) loadUsers(m libkb.MetaContext) {
if e.runerr != nil {
return
}
for _, u := range e.arg.Usernames {
arg := libkb.NewLoadUserByNameArg(e.G(), u)
user, err := libkb.LoadUser(arg)
if err != nil {
e.runerr = err
return
}
e.addUser(user, false)
}
}
func (e *PGPKeyfinder) loadKeys(m libkb.MetaContext) {
if e.runerr != nil {
return
}
// get the pgp keys for all the users
for _, x := range e.uplus {
keys := x.User.GetActivePGPKeys(true)
if len(keys) == 0 {
e.runerr = libkb.NoPGPEncryptionKeyError{
User: x.User.GetName(),
HasKeybaseEncryptionKey: x.User.HasEncryptionSubkey() || (x.User.GetComputedKeyFamily().GetLatestPerUserKey() != nil),
}
return
}
x.Keys = keys
}
}
type UserPlusKeys struct {
User *libkb.User
IsTracked bool
Keys []*libkb.PGPKeyBundle
}
func (e *PGPKeyfinder) addUser(user *libkb.User, tracked bool) {
e.uplus = append(e.uplus, &UserPlusKeys{User: user, IsTracked: tracked})
}