forked from keybase/client
/
identify_outcome.go
254 lines (217 loc) · 5.85 KB
/
identify_outcome.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"fmt"
"sort"
"github.com/keybase/client/go/gregor"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
jsonw "github.com/keybase/go-jsonw"
)
type IdentifyOutcome struct {
Username NormalizedUsername
Error error
KeyDiffs []TrackDiff
Revoked []TrackDiff
RevokedDetails []keybase1.RevokedProof
ProofChecks []*LinkCheckResult
Warnings []Warning
TrackUsed *TrackLookup
TrackEqual bool // Whether the track statement was equal to what we saw
TrackOptions keybase1.TrackOptions
Reason keybase1.IdentifyReason
ResponsibleGregorItem gregor.Item
}
func NewIdentifyOutcome() *IdentifyOutcome {
return &IdentifyOutcome{}
}
func NewIdentifyOutcomeWithUsername(u NormalizedUsername) *IdentifyOutcome {
return &IdentifyOutcome{Username: u}
}
func (i *IdentifyOutcome) remoteProofLinks() *RemoteProofLinks {
rpl := NewRemoteProofLinks()
for _, p := range i.ProofChecks {
rpl.Insert(p.link, p.err)
}
return rpl
}
func (i *IdentifyOutcome) ActiveProofs() []RemoteProofChainLink {
return i.remoteProofLinks().Active()
}
func (i *IdentifyOutcome) AddProofsToSet(existing *ProofSet, okStates []keybase1.ProofState) {
i.remoteProofLinks().AddProofsToSet(existing, okStates)
}
func (i *IdentifyOutcome) TrackSet() *TrackSet {
return i.remoteProofLinks().TrackSet()
}
func (i *IdentifyOutcome) ProofChecksSorted() []*LinkCheckResult {
// Treat DNS and Web as the same type, and sort them together
// in the same bucket.
dnsToWeb := func(t keybase1.ProofType) keybase1.ProofType {
if t == keybase1.ProofType_DNS {
return keybase1.ProofType_GENERIC_WEB_SITE
}
return t
}
m := make(map[keybase1.ProofType][]*LinkCheckResult)
for _, p := range i.ProofChecks {
pt := dnsToWeb(p.link.GetProofType())
m[pt] = append(m[pt], p)
}
var res []*LinkCheckResult
for _, pt := range RemoteServiceOrder {
pc, ok := m[pt]
if !ok {
continue
}
sort.Sort(byDisplayString(pc))
res = append(res, pc...)
}
return res
}
// Revoked proofs are those we used to look for but are gone!
func (i IdentifyOutcome) NumRevoked() int {
return len(i.Revoked)
}
// The number of proofs that failed.
func (i IdentifyOutcome) NumProofFailures() int {
nfails := 0
for _, c := range i.ProofChecks {
if c.err != nil {
nfails++
}
}
return nfails
}
// The number of proofs that actually worked
func (i IdentifyOutcome) NumProofSuccesses() int {
nsucc := 0
for _, c := range i.ProofChecks {
if c.err == nil {
nsucc++
}
}
return nsucc
}
// A "Track Failure" is when we previously tracked this user, and
// some aspect of their proof changed. Like their key changed, or
// they changed Twitter names
func (i IdentifyOutcome) NumTrackFailures() int {
ntf := 0
check := func(d TrackDiff) bool {
return d != nil && d.BreaksTracking()
}
for _, c := range i.ProofChecks {
if check(c.diff) || check(c.remoteDiff) {
ntf++
}
}
for _, k := range i.KeyDiffs {
if check(k) {
ntf++
}
}
return ntf
}
// A "Track Change" isn't necessary a failure, maybe they upgraded
// a proof from HTTP to HTTPS. But we still should retrack if we can.
func (i IdentifyOutcome) NumTrackChanges() int {
ntc := 0
check := func(d TrackDiff) bool {
return d != nil && !d.IsSameAsTracked()
}
for _, c := range i.ProofChecks {
if check(c.diff) || check(c.remoteDiff) {
ntc++
}
}
for _, k := range i.KeyDiffs {
if check(k) {
ntc++
}
}
return ntc
}
func (i IdentifyOutcome) TrackStatus() keybase1.TrackStatus {
if i.NumRevoked() > 0 {
return keybase1.TrackStatus_UPDATE_BROKEN_REVOKED
}
if i.NumTrackFailures() > 0 {
return keybase1.TrackStatus_UPDATE_BROKEN_FAILED_PROOFS
}
if i.TrackUsed != nil {
if i.NumTrackChanges() > 0 {
return keybase1.TrackStatus_UPDATE_NEW_PROOFS
}
if i.NumTrackChanges() == 0 {
return keybase1.TrackStatus_UPDATE_OK
}
}
if i.NumProofSuccesses() == 0 {
return keybase1.TrackStatus_NEW_ZERO_PROOFS
}
if i.NumProofFailures() > 0 {
return keybase1.TrackStatus_NEW_FAIL_PROOFS
}
return keybase1.TrackStatus_NEW_OK
}
func (i IdentifyOutcome) IsOK() bool {
switch i.TrackStatus() {
case keybase1.TrackStatus_UPDATE_NEW_PROOFS:
return true
case keybase1.TrackStatus_UPDATE_OK:
return true
case keybase1.TrackStatus_NEW_ZERO_PROOFS:
return true
case keybase1.TrackStatus_NEW_OK:
return true
default:
return false
}
}
func (i IdentifyOutcome) TrackingStatement() *jsonw.Wrapper {
return i.remoteProofLinks().TrackingStatement()
}
func (i IdentifyOutcome) GetErrorAndWarnings(strict bool) (warnings Warnings, err error) {
if i.Error != nil {
err = i.Error
return warnings, err
}
var probs []string
softErr := func(s string) {
if strict {
probs = append(probs, s)
} else {
warnings.Push(StringWarning(s))
}
}
for _, revoked := range i.Revoked {
softErr(revoked.ToDisplayString())
}
if nfails := i.NumProofFailures(); nfails > 0 {
p := fmt.Sprintf("PROBLEM: %d proof%s failed remote checks", nfails, GiveMeAnS(nfails))
softErr(p)
}
if ntf := i.NumTrackFailures(); ntf > 0 {
probs = append(probs,
fmt.Sprintf("%d followed proof%s failed",
ntf, GiveMeAnS(ntf)))
}
if len(probs) > 0 {
err = IdentifySummaryError{i.Username, probs}
}
return warnings, err
}
func (i IdentifyOutcome) GetError() error {
_, e := i.GetErrorAndWarnings(true)
return e
}
func (i IdentifyOutcome) GetErrorLax() (Warnings, error) {
return i.GetErrorAndWarnings(false)
}
type byDisplayString []*LinkCheckResult
func (a byDisplayString) Len() int { return len(a) }
func (a byDisplayString) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byDisplayString) Less(i, j int) bool {
return a[i].link.ToDisplayString() < a[j].link.ToDisplayString()
}