-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
proof_service_generic_social.go
300 lines (253 loc) · 10.2 KB
/
proof_service_generic_social.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package externals
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/keybase/client/go/jsonhelpers"
libkb "github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
jsonw "github.com/keybase/go-jsonw"
)
const kbUsernameKey = "%{kb_username}"
const remoteUsernameKey = "%{username}"
const sigHashKey = "%{sig_hash}"
const kbUaKey = "%{kb_ua}"
//=============================================================================
// Validated configuration from the server
type GenericSocialProofConfig struct {
keybase1.ParamProofServiceConfig
usernameRe *regexp.Regexp
}
func NewGenericSocialProofConfig(g *libkb.GlobalContext, config keybase1.ParamProofServiceConfig) (*GenericSocialProofConfig, error) {
gsConfig := &GenericSocialProofConfig{
ParamProofServiceConfig: config,
}
if err := gsConfig.parseAndValidate(g); err != nil {
return nil, err
}
return gsConfig, nil
}
func (c *GenericSocialProofConfig) parseAndValidate(g *libkb.GlobalContext) (err error) {
if c.usernameRe, err = regexp.Compile(c.UsernameConfig.Re); err != nil {
return err
}
if err = c.validatePrefillURL(); err != nil {
return err
}
if err = c.validateCheckURL(); err != nil {
return err
}
if err = c.validateProfileURL(); err != nil {
return err
}
// In devel, we need to update the config url with the IP for the CI
// container.
if g.Env.GetRunMode() == libkb.DevelRunMode {
serverURI := g.Env.GetServerURI()
c.ProfileUrl = strings.Replace(c.ProfileUrl, libkb.DevelServerURI, serverURI, 1)
c.PrefillUrl = strings.Replace(c.PrefillUrl, libkb.DevelServerURI, serverURI, 1)
c.CheckUrl = strings.Replace(c.CheckUrl, libkb.DevelServerURI, serverURI, 1)
}
return nil
}
func (c *GenericSocialProofConfig) validateProfileURL() error {
if !strings.Contains(c.ProfileUrl, remoteUsernameKey) {
return fmt.Errorf("invalid ProfileUrl: %s, missing: %s", c.ProfileUrl, remoteUsernameKey)
}
return nil
}
func (c *GenericSocialProofConfig) validatePrefillURL() error {
if !strings.Contains(c.PrefillUrl, kbUsernameKey) {
return fmt.Errorf("invalid PrefillUrl: %s, missing: %s", c.PrefillUrl, kbUsernameKey)
}
if !strings.Contains(c.PrefillUrl, sigHashKey) {
return fmt.Errorf("invalid PrefillUrl: %s, missing: %s", c.PrefillUrl, sigHashKey)
}
if !strings.Contains(c.PrefillUrl, kbUaKey) {
return fmt.Errorf("invalid PrefillUrl: %s, missing: %s", c.PrefillUrl, kbUaKey)
}
return nil
}
func (c *GenericSocialProofConfig) validateCheckURL() error {
if !strings.Contains(c.CheckUrl, remoteUsernameKey) {
return fmt.Errorf("invalid CheckUrl: %s, missing: %s", c.CheckUrl, remoteUsernameKey)
}
return nil
}
func (c *GenericSocialProofConfig) profileURLWithValues(remoteUsername string) (string, error) {
url := strings.Replace(c.ProfileUrl, remoteUsernameKey, remoteUsername, 1)
if !strings.Contains(url, remoteUsername) {
return "", fmt.Errorf("Invalid ProfileUrl: %s, missing remoteUsername: %s", url, remoteUsername)
}
return url, nil
}
func (c *GenericSocialProofConfig) prefillURLWithValues(kbUsername string, sigID keybase1.SigID) (string, error) {
url := strings.Replace(c.PrefillUrl, kbUsernameKey, kbUsername, 1)
if !strings.Contains(url, kbUsername) {
return "", fmt.Errorf("Invalid PrefillUrl: %s, missing kbUsername: %s", url, kbUsername)
}
url = strings.Replace(url, sigHashKey, sigID.String(), 1)
if !strings.Contains(url, sigID.String()) {
return "", fmt.Errorf("Invalid PrefillUrl: %s, missing sigHash: %s", url, sigID)
}
url = strings.Replace(url, kbUaKey, libkb.UserAgent, 1)
if !strings.Contains(url, libkb.UserAgent) {
return "", fmt.Errorf("Invalid PrefillUrl: %s, missing kbUa: %s", url, libkb.UserAgent)
}
return url, nil
}
func (c *GenericSocialProofConfig) checkURLWithValues(remoteUsername string) (string, error) {
url := strings.Replace(c.CheckUrl, remoteUsernameKey, remoteUsername, 1)
if !strings.Contains(url, remoteUsername) {
return "", fmt.Errorf("Invalid CheckUrl: %s, missing remoteUsername: %s", url, remoteUsername)
}
return url, nil
}
func (c *GenericSocialProofConfig) validateRemoteUsername(remoteUsername string) error {
uc := c.UsernameConfig
if len(remoteUsername) < uc.Min {
return fmt.Errorf("username must be at least %d characters, was %d", c.UsernameConfig.Min, len(remoteUsername))
} else if len(remoteUsername) > uc.Max {
return fmt.Errorf("username can be at most %d characters, was %d", c.UsernameConfig.Max, len(remoteUsername))
} else if !c.usernameRe.MatchString(remoteUsername) {
return fmt.Errorf("username did not match expected format: %s", c.UsernameConfig.Re)
}
return nil
}
//=============================================================================
// GenericSocialProof
//
type GenericSocialProofChecker struct {
proof libkb.RemoteProofChainLink
config *GenericSocialProofConfig
}
var _ libkb.ProofChecker = (*GenericSocialProofChecker)(nil)
func NewGenericSocialProofChecker(proof libkb.RemoteProofChainLink, config *GenericSocialProofConfig) (*GenericSocialProofChecker, libkb.ProofError) {
return &GenericSocialProofChecker{
proof: proof,
config: config,
}, nil
}
func (rc *GenericSocialProofChecker) GetTorError() libkb.ProofError { return nil }
func (rc *GenericSocialProofChecker) CheckStatus(mctx libkb.MetaContext, _ libkb.SigHint, _ libkb.ProofCheckerMode,
pvlU keybase1.MerkleStoreEntry) (*libkb.SigHint, libkb.ProofError) {
_, sigID, err := libkb.OpenSig(rc.proof.GetArmoredSig())
if err != nil {
return nil, libkb.NewProofError(keybase1.ProofStatus_BAD_SIGNATURE,
"Bad signature: %v", err)
}
remoteUsername := rc.proof.GetRemoteUsername()
if err := rc.config.validateRemoteUsername(remoteUsername); err != nil {
return nil, libkb.NewProofError(keybase1.ProofStatus_BAD_USERNAME,
"remoteUsername %s was invalid: %v", remoteUsername, err)
}
apiURL, err := rc.config.checkURLWithValues(remoteUsername)
if err != nil {
return nil, libkb.NewProofError(keybase1.ProofStatus_BAD_API_URL,
"Bad api url: %v", err)
}
if _, err = url.Parse(apiURL); err != nil {
return nil, libkb.NewProofError(keybase1.ProofStatus_FAILED_PARSE,
"Could not parse url: '%v'", apiURL)
}
res, err := mctx.G().GetExternalAPI().Get(libkb.APIArg{
Endpoint: apiURL,
MetaContext: mctx,
})
if err != nil {
return nil, libkb.XapiError(err, apiURL)
}
// We expect a single result to match which contains an array of proofs.
results, perr := jsonhelpers.AtSelectorPath(res.Body, rc.config.CheckPath, mctx.CDebugf, libkb.NewInvalidPVLSelectorError)
if perrInner, _ := perr.(libkb.ProofError); perrInner != nil {
return nil, perrInner
}
if len(results) != 1 {
return nil, libkb.NewProofError(keybase1.ProofStatus_CONTENT_FAILURE,
"Json selector did not match any values")
}
var proofs []keybase1.ParamProofJSON
if err = results[0].UnmarshalAgain(&proofs); err != nil {
return nil, libkb.NewProofError(keybase1.ProofStatus_CONTENT_FAILURE,
"Json could not be deserialized")
}
var foundProof, foundUsername bool
for _, proof := range proofs {
if proof.KbUsername == rc.proof.GetUsername() && sigID.Equal(proof.SigHash) {
foundProof = true
break
}
// Report if we found any matching usernames but the signature didn't match.
foundUsername = foundUsername || proof.KbUsername == rc.proof.GetUsername()
}
if !foundProof {
if foundUsername {
return nil, libkb.NewProofError(keybase1.ProofStatus_NOT_FOUND,
"Unable to find the proof, signature mismatch")
}
return nil, libkb.NewProofError(keybase1.ProofStatus_NOT_FOUND,
"Unable to find the proof")
}
humanURL, err := rc.config.profileURLWithValues(remoteUsername)
if err != nil {
mctx.CDebugf("Unable to generate humanURL for verifiedSigHint: %v", err)
humanURL = ""
}
verifiedSigHint := libkb.NewVerifiedSigHint(sigID, "" /* remoteID */, apiURL, humanURL, "" /* checkText */)
return verifiedSigHint, nil
}
//=============================================================================
type GenericSocialProofServiceType struct {
libkb.BaseServiceType
config *GenericSocialProofConfig
}
func NewGenericSocialProofServiceType(config *GenericSocialProofConfig) *GenericSocialProofServiceType {
return &GenericSocialProofServiceType{
config: config,
}
}
func (t *GenericSocialProofServiceType) Key() string { return t.GetTypeName() }
func (t *GenericSocialProofServiceType) NormalizeUsername(s string) (string, error) {
if err := t.config.validateRemoteUsername(s); err != nil {
return "", err
}
return strings.ToLower(s), nil
}
func (t *GenericSocialProofServiceType) NormalizeRemoteName(mctx libkb.MetaContext, s string) (ret string, err error) {
return t.NormalizeUsername(s)
}
func (t *GenericSocialProofServiceType) GetPrompt() string {
return fmt.Sprintf("Your username on %s", t.config.DisplayName)
}
func (t *GenericSocialProofServiceType) ToServiceJSON(username string) *jsonw.Wrapper {
return t.BaseToServiceJSON(t, username)
}
func (t *GenericSocialProofServiceType) PostInstructions(username string) *libkb.Markup {
return libkb.FmtMarkup(`Please click on the following link to post to %v:`, t.config.DisplayName)
}
func (t *GenericSocialProofServiceType) DisplayName(username string) string {
return t.config.DisplayName
}
func (t *GenericSocialProofServiceType) GetTypeName() string { return t.config.Domain }
func (t *GenericSocialProofServiceType) RecheckProofPosting(tryNumber int, status keybase1.ProofStatus, _ string) (warning *libkb.Markup, err error) {
return t.BaseRecheckProofPosting(tryNumber, status)
}
func (t *GenericSocialProofServiceType) GetProofType() string {
return libkb.GenericSocialWebServiceBinding
}
func (t *GenericSocialProofServiceType) CheckProofText(text string, id keybase1.SigID, sig string) (err error) {
// We don't rely only any server trust in FormatProofText so there is nothing to verify here.
return nil
}
func (t *GenericSocialProofServiceType) FormatProofText(m libkb.MetaContext, ppr *libkb.PostProofRes,
kbUsername string, sigID keybase1.SigID) (string, error) {
return t.config.prefillURLWithValues(kbUsername, sigID)
}
func (t *GenericSocialProofServiceType) MakeProofChecker(l libkb.RemoteProofChainLink) libkb.ProofChecker {
return &GenericSocialProofChecker{
proof: l,
config: t.config,
}
}
func (t *GenericSocialProofServiceType) IsDevelOnly() bool { return false }