forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.go
156 lines (134 loc) · 4.15 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
155
156
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"regexp"
"strings"
keybase1 "github.com/keybase/client/go/protocol"
jsonw "github.com/keybase/go-jsonw"
)
type ServiceType interface {
AllStringKeys() []string
CheckUsername(string) error
NormalizeUsername(string) (string, error)
ToChecker() Checker
GetPrompt() string
LastWriterWins() bool
PreProofCheck(username string) (*Markup, error)
PreProofWarning(remotename string) *Markup
ToServiceJSON(remotename string) *jsonw.Wrapper
PostInstructions(remotename string) *Markup
DisplayName(username string) string
RecheckProofPosting(tryNumber int, status keybase1.ProofStatus, username string) (warning *Markup, err error)
GetProofType() string
GetTypeName() string
CheckProofText(text string, id keybase1.SigID, sig string) error
FormatProofText(*PostProofRes) (string, error)
GetAPIArgKey() string
}
var _stDispatch = make(map[string]ServiceType)
func RegisterServiceType(st ServiceType) {
for _, k := range st.AllStringKeys() {
_stDispatch[k] = st
}
}
func GetServiceType(s string) ServiceType {
return _stDispatch[strings.ToLower(s)]
}
//=============================================================================
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) BaseToChecker(st ServiceType, hint string) Checker {
return Checker{
F: func(s string) bool { return (st.CheckUsername(s) == nil) },
Hint: hint,
PreserveSpace: false,
}
}
func (t BaseServiceType) BaseAllStringKeys(st ServiceType) []string {
return []string{st.GetTypeName()}
}
func (t BaseServiceType) LastWriterWins() bool { return true }
func (t BaseServiceType) PreProofCheck(string) (*Markup, error) { return nil, nil }
func (t BaseServiceType) PreProofWarning(remotename string) *Markup { return nil }
func (t BaseServiceType) FormatProofText(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
}
func (t BaseServiceType) NormalizeUsername(s string) (string, error) {
return strings.ToLower(s), nil
}
func (t BaseServiceType) BaseCheckProofForURL(text string, id keybase1.SigID) (err error) {
urlRxx := regexp.MustCompile(`https://(\S+)`)
target := id.ToMediumID()
urls := urlRxx.FindAllString(text, -1)
G.Log.Debug("Found urls %v", urls)
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"
}
//=============================================================================