-
Notifications
You must be signed in to change notification settings - Fork 11
/
federation.go
289 lines (255 loc) · 7.34 KB
/
federation.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
package readas
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"github.com/writeas/activity/streams"
"github.com/writeas/go-webfinger"
"github.com/writeas/httpsig"
"github.com/writeas/impart"
"github.com/writeas/web-core/activitypub"
"github.com/writeas/web-core/activitystreams"
"io/ioutil"
"net/http"
"net/http/httputil"
"strings"
)
var (
verifier *httpsig.Verifier
)
func initFederation(app *app) {
verifier = httpsig.NewSigHeaderVerifier(keyGetter{app})
}
type keyGetter struct {
app *app
}
func (kg keyGetter) GetKey(id string) interface{} {
k, err := kg.app.getActorKey(id)
if err != nil {
logError("Unable to get key: %v", err)
return nil
}
pubKey, err := activitypub.DecodePublicKey(k)
if err != nil {
logError("Unable to decode key: %v", err)
return err
}
return pubKey
}
func verifyRequest(app *app, r *http.Request) error {
return verifier.Verify(r)
}
type WebfingerResult struct {
ActorIRI string
Username string
Host string
}
func makeActivityPost(p *activitystreams.Person, url string, m interface{}) error {
logInfo("POST %s", url)
b, err := json.Marshal(m)
if err != nil {
return err
}
r, _ := http.NewRequest("POST", url, bytes.NewBuffer(b))
r.Header.Add("Content-Type", "application/activity+json")
r.Header.Set("User-Agent", userAgent)
h := sha256.New()
h.Write(b)
r.Header.Add("Digest", "SHA-256="+base64.StdEncoding.EncodeToString(h.Sum(nil)))
// Sign using the 'Signature' header
privKey, err := activitypub.DecodePrivateKey(p.GetPrivKey())
if err != nil {
return err
}
signer := httpsig.NewSigner(p.PublicKey.ID, privKey, httpsig.RSASHA256, []string{"(request-target)", "date", "host", "digest"})
err = signer.SignSigHeader(r)
if err != nil {
logError("Can't sign: %v", err)
}
dump, err := httputil.DumpRequestOut(r, true)
if err != nil {
logError("Can't dump: %v", err)
} else {
logInfo("%s", dump)
}
resp, err := http.DefaultClient.Do(r)
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if resp == nil {
logInfo("No status or response.")
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
logInfo("Status : %s", resp.Status)
logInfo("Response: %s", body)
}
return nil
}
func resolveIRI(url string) ([]byte, error) {
logInfo("GET %s", url)
r, _ := http.NewRequest("GET", url, nil)
r.Header.Add("Accept", "application/activity+json")
r.Header.Set("User-Agent", userAgent)
dump, err := httputil.DumpRequestOut(r, true)
if err != nil {
logError("Can't dump: %v", err)
} else {
logInfo("%s", dump)
}
resp, err := http.DefaultClient.Do(r)
if err != nil {
return nil, err
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
logInfo("Status : %s", resp.Status)
logInfo("Response: %s", body)
return body, nil
}
func doWebfinger(host, username string) (*WebfingerResult, error) {
url := "https://" + host + "/.well-known/webfinger?resource=acct:" + username + "@" + host
logInfo("Webfinger: %s", url)
r, _ := http.NewRequest("GET", url, nil)
r.Header.Set("User-Agent", userAgent)
dump, err := httputil.DumpRequestOut(r, true)
if err != nil {
logError("Can't dump: %v", err)
} else {
logInfo("%s", dump)
}
resp, err := http.DefaultClient.Do(r)
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
logInfo("Status : %s", resp.Status)
logInfo("Response: %s", body)
if resp.StatusCode != 200 {
return nil, impart.HTTPError{resp.StatusCode, ""}
}
resource := webfinger.Resource{}
if err := json.Unmarshal(body, &resource); err != nil {
logError("Unable to unmarshal webfinger: %v", err)
}
var actorIRI string
// TODO: ensure subject matches
// TODO: store text/html URL here?
for _, l := range resource.Links {
if l.Type != "application/activity+json" || l.Rel != "self" {
continue
}
actorIRI = l.HRef
break
}
subj := resource.Subject
if strings.HasPrefix(subj, "acct:") {
subj = subj[len("acct:"):]
}
handleItems := strings.Split(subj, "@")
return &WebfingerResult{
Username: handleItems[0],
Host: handleItems[1],
ActorIRI: actorIRI,
}, nil
}
func fetchActor(app *app, actorIRI string) (*activitystreams.Person, *User, error) {
logInfo("Fetching actor %s locally", actorIRI)
actor := &activitystreams.Person{}
remoteUser, err := app.getActor(actorIRI)
if err != nil {
if iErr, ok := err.(impart.HTTPError); ok {
if iErr.Status == http.StatusNotFound {
// Fetch remote actor
logInfo("Not found; fetching actor %s remotely", actorIRI)
actorResp, err := resolveIRI(actorIRI)
if err != nil {
logError("Unable to get actor! %v", err)
return nil, nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't fetch actor."}
}
if err := json.Unmarshal(actorResp, &actor); err != nil {
// FIXME: Hubzilla has an object for the Actor's url: cannot unmarshal object into Go struct field Person.url of type string
logError("Unable to unmarshal actor! %v", err)
return nil, nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't parse actor."}
}
} else {
return nil, nil, err
}
} else {
return nil, nil, err
}
} else {
actor = remoteUser.AsPerson()
}
// TODO: don't return all three; just (*User, error)
return actor, remoteUser, nil
}
// TODO: rename this to something better; it doesn't just fetch, but also adds posts
func fetchActorOutbox(app *app, outbox string) error {
logInfo("Fetching actor outbox: " + outbox)
outRes, err := resolveIRI(outbox)
if err != nil {
logError("Unable to get outbox! %v", err)
return impart.HTTPError{http.StatusInternalServerError, "Couldn't fetch outbox."}
}
coll := &activitystreams.OrderedCollection{}
if err := json.Unmarshal(outRes, &coll); err != nil {
logError("Unable to unmarshal outbox! %v", err)
return impart.HTTPError{http.StatusInternalServerError, "Couldn't parse outbox."}
}
if coll.First == "" {
// Not the OrderedCollection we were expecting, so quit now and don't fetch any posts
// TODO: parse the OrderedCollection `items` property and import those posts
return nil
}
logInfo("Fetching actor outbox page: " + coll.First)
outPageRes, err := resolveIRI(coll.First)
if err != nil {
logError("Unable to get outbox page! %v", err)
return impart.HTTPError{http.StatusInternalServerError, "Couldn't fetch outbox page."}
}
var collPageMap map[string]interface{}
if err := json.Unmarshal(outPageRes, &collPageMap); err != nil {
return err
}
res := &streams.Resolver{
OrderedCollectionPageCallback: func(p *streams.OrderedCollectionPage) error {
itemsLen := p.LenOrderedItems()
logInfo("Ordered items: %d", itemsLen)
if itemsLen > 0 {
// Add posts in reverse order they're listed, since they're in reverse-chronological order
for i := itemsLen - 1; i >= 0; i-- {
_, err = p.ResolveOrderedItems(&streams.Resolver{
CreateCallback: func(f *streams.Create) error {
// Create post in database
return handleCreateCallback(app, f)
},
}, i)
if err != nil {
logError("Unable to resolve item: %v", err)
}
}
}
return nil
},
}
if err := res.Deserialize(collPageMap); err != nil {
logError("Unable to resolve OrderedCollectionPage: %v", err)
return err
}
return nil
}