This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
/
services.go
154 lines (130 loc) · 4.11 KB
/
services.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"fmt"
"regexp"
"strings"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
jsonw "github.com/keybase/go-jsonw"
)
//=============================================================================
func MakeProofChecker(c ExternalServicesCollector, l RemoteProofChainLink) (ProofChecker, ProofError) {
if c == nil {
return nil, NewProofError(keybase1.ProofStatus_UNKNOWN_TYPE,
"No proof services configured")
}
k := l.TableKey()
st := c.GetServiceType(k)
if st == nil {
return nil, NewProofError(keybase1.ProofStatus_UNKNOWN_TYPE,
"No proof service for type: %s", k)
}
pc := st.MakeProofChecker(l)
if pc == nil {
return nil, NewProofError(keybase1.ProofStatus_UNKNOWN_TYPE,
"No proof checker for type: %s", k)
}
return pc, nil
}
//=============================================================================
type BaseServiceType struct{}
func (t BaseServiceType) BaseCheckProofTextShort(text string, id keybase1.SigID, med bool) error {
blocks := FindBase64Snippets(text)
var target string
if med {
target = id.ToMediumID()
} else {
target = id.ToShortID()
}
for _, b := range blocks {
if len(b) < len(target) {
continue
}
if b != target {
return WrongSigError{b}
}
// found match:
return nil
}
return NotFoundError{"Couldn't find signature ID " + target + " in text"}
}
func (t BaseServiceType) BaseRecheckProofPosting(tryNumber int, status keybase1.ProofStatus) (warning *Markup, err error) {
warning = FmtMarkup("Couldn't find posted proof.")
return
}
func (t BaseServiceType) BaseToServiceJSON(st ServiceType, un string) *jsonw.Wrapper {
ret := jsonw.NewDictionary()
ret.SetKey("name", jsonw.NewString(st.GetTypeName()))
ret.SetKey("username", jsonw.NewString(un))
return ret
}
func (t BaseServiceType) BaseGetProofType(st ServiceType) string {
return "web_service_binding." + st.GetTypeName()
}
func (t BaseServiceType) BaseAllStringKeys(st ServiceType) []string {
return []string{st.GetTypeName()}
}
func (t BaseServiceType) LastWriterWins() bool { return true }
func (t BaseServiceType) PreProofCheck(ProofContext, string) (*Markup, error) { return nil, nil }
func (t BaseServiceType) PreProofWarning(remotename string) *Markup { return nil }
func (t BaseServiceType) FormatProofText(ctx ProofContext, ppr *PostProofRes) (string, error) {
return ppr.Text, nil
}
func (t BaseServiceType) BaseCheckProofTextFull(text string, id keybase1.SigID, sig string) (err error) {
blocks := FindBase64Blocks(text)
target := FindFirstBase64Block(sig)
if len(target) == 0 {
err = BadSigError{"Generated sig was invalid"}
return
}
found := false
for _, b := range blocks {
if len(b) < 80 {
continue
}
if b != target {
err = WrongSigError{b}
return
}
found = true
}
if !found {
err = NotFoundError{"Couldn't find signature ID " + target + " in text"}
}
return
}
var urlRxx = regexp.MustCompile(`https://(\S+)`)
func (t BaseServiceType) BaseCheckProofForURL(text string, id keybase1.SigID) (err error) {
target := id.ToMediumID()
urls := urlRxx.FindAllString(text, -1)
found := false
for _, u := range urls {
if strings.HasSuffix(u, target) {
found = true
}
}
if !found {
err = NotFoundError{"Didn't find a URL with suffix '" + target + "'"}
}
return
}
func (t BaseServiceType) GetAPIArgKey() string {
return "remote_username"
}
func (t BaseServiceType) IsDevelOnly() bool { return false }
//=============================================================================
type assertionContext struct {
esc ExternalServicesCollector
}
func MakeAssertionContext(s ExternalServicesCollector) AssertionContext {
return assertionContext{esc: s}
}
func (a assertionContext) NormalizeSocialName(service string, username string) (string, error) {
st := a.esc.GetServiceType(service)
if st == nil {
return "", fmt.Errorf("Unknown social network: %s", service)
}
return st.NormalizeUsername(username)
}
//=============================================================================