-
Notifications
You must be signed in to change notification settings - Fork 1
/
proof_support_web.go
185 lines (153 loc) · 5.26 KB
/
proof_support_web.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package externals
import (
"fmt"
"net/url"
"regexp"
"strings"
libkb "github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
jsonw "github.com/keybase/go-jsonw"
)
//=============================================================================
// Web
//
type WebChecker struct {
proof libkb.RemoteProofChainLink
}
var _ libkb.ProofChecker = (*WebChecker)(nil)
var webKeybaseFiles = []string{".well-known/keybase.txt", "keybase.txt"}
func NewWebChecker(p libkb.RemoteProofChainLink) (*WebChecker, libkb.ProofError) {
return &WebChecker{p}, nil
}
func (rc *WebChecker) GetTorError() libkb.ProofError {
urlBase := rc.proof.ToDisplayString()
u, err := url.Parse(urlBase)
if err != nil || u.Scheme != "https" {
return libkb.ProofErrorHTTPOverTor
}
return nil
}
func (rc *WebChecker) CheckStatus(ctx libkb.ProofContext, h libkb.SigHint, pcm libkb.ProofCheckerMode, pvlU libkb.PvlUnparsed) libkb.ProofError {
if pcm != libkb.ProofCheckerModeActive {
ctx.GetLog().CDebugf(ctx.GetNetContext(), "Web check skipped since proof checking was not in active mode (%s)", h.GetAPIURL())
return libkb.ProofErrorUnchecked
}
return CheckProofPvl(ctx, keybase1.ProofType_GENERIC_WEB_SITE, rc.proof, h, pvlU)
}
//
//=============================================================================
type WebServiceType struct{ libkb.BaseServiceType }
func (t WebServiceType) AllStringKeys() []string { return []string{"web", "http", "https"} }
func (t WebServiceType) PrimaryStringKeys() []string { return []string{"https", "http"} }
func (t WebServiceType) NormalizeUsername(s string) (ret string, err error) {
// The username is just the (lowercased) hostname.
if !libkb.IsValidHostname(s) {
return "", libkb.NewInvalidHostnameError(s)
}
return strings.ToLower(s), nil
}
func ParseWeb(s string) (hostname string, prot string, err error) {
rxx := regexp.MustCompile("^(http(s?))://(.*)$")
if v := rxx.FindStringSubmatch(s); v != nil {
s = v[3]
prot = v[1]
}
if !libkb.IsValidHostname(s) {
err = libkb.NewInvalidHostnameError(s)
} else {
hostname = s
}
return
}
func (t WebServiceType) NormalizeRemoteName(ctx libkb.ProofContext, s string) (ret string, err error) {
// The remote name is a full (case-preserved) URL.
var prot, host string
if host, prot, err = ParseWeb(s); err != nil {
return
}
var res *libkb.APIRes
res, err = ctx.GetAPI().Get(libkb.APIArg{
Endpoint: "remotes/check",
SessionType: libkb.APISessionTypeREQUIRED,
Args: libkb.HTTPArgs{
"hostname": libkb.S{Val: host},
},
})
if err != nil {
return
}
var found string
found, err = res.Body.AtPath("results.first").GetString()
if err != nil {
err = libkb.NewWebUnreachableError(host)
return
}
if len(prot) > 0 && prot == "https" && found != "https:" {
msg := fmt.Sprintf("You specified HTTPS for %s but only HTTP is available", host)
err = libkb.NewProtocolDowngradeError(msg)
return
}
ret = found + "//" + host
return
}
func (t WebServiceType) GetPrompt() string {
return "Web site to check"
}
func (t WebServiceType) ToServiceJSON(un string) *jsonw.Wrapper {
h, p, _ := ParseWeb(un)
ret := jsonw.NewDictionary()
ret.SetKey("protocol", jsonw.NewString(p+":"))
ret.SetKey("hostname", jsonw.NewString(h))
return ret
}
func (t WebServiceType) MarkupFilenames(un string, mkp *libkb.Markup) {
mkp.Append(`<ul>`)
first := true
for _, f := range webKeybaseFiles {
var bullet string
if first {
bullet = " "
first = false
} else {
bullet = "OR "
}
mkp.Append(`<li bullet="` + bullet + `"><url>` + un + "/" + f + `</url></li>`)
}
mkp.Append(`</ul>`)
}
func (t WebServiceType) PreProofWarning(un string) *libkb.Markup {
mkp := libkb.FmtMarkup(`<p>You will be asked to post a file to:</p>`)
t.MarkupFilenames(un, mkp)
return mkp
}
func (t WebServiceType) PostInstructions(un string) *libkb.Markup {
mkp := libkb.FmtMarkup(`<p>Make the following file available at:</p>`)
t.MarkupFilenames(un, mkp)
return mkp
}
func (t WebServiceType) DisplayName(un string) string { return "Web" }
func (t WebServiceType) GetTypeName() string { return "web" }
func (t WebServiceType) RecheckProofPosting(tryNumber int, status keybase1.ProofStatus, _ string) (warning *libkb.Markup, err error) {
if status == keybase1.ProofStatus_PERMISSION_DENIED {
warning = libkb.FmtMarkup("Permission denied! Make sure your proof page is <strong>public</strong>.")
} else {
warning, err = t.BaseRecheckProofPosting(tryNumber, status)
}
return
}
func (t WebServiceType) GetProofType() string { return "web_service_binding.generic" }
func (t WebServiceType) CheckProofText(text string, id keybase1.SigID, sig string) (err error) {
return t.BaseCheckProofTextFull(text, id, sig)
}
func (t WebServiceType) GetAPIArgKey() string { return "remote_host" }
func (t WebServiceType) LastWriterWins() bool { return false }
func (t WebServiceType) MakeProofChecker(l libkb.RemoteProofChainLink) libkb.ProofChecker {
return &WebChecker{l}
}
//=============================================================================
func init() {
externalServices.Register(WebServiceType{})
}
//=============================================================================