-
Notifications
You must be signed in to change notification settings - Fork 26
/
activityPub.go
653 lines (615 loc) · 20.4 KB
/
activityPub.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package main
import (
"bytes"
"cmp"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"database/sql"
"encoding/json"
"encoding/pem"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
ap "github.com/go-ap/activitypub"
apc "github.com/go-ap/client"
"github.com/go-chi/chi/v5"
"github.com/go-fed/httpsig"
"github.com/google/uuid"
"github.com/samber/lo"
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/contenttype"
)
func (a *goBlog) initActivityPub() error {
if !a.apEnabled() {
// ActivityPub disabled
return nil
}
// Add hooks
a.pPostHooks = append(a.pPostHooks, func(p *post) {
if p.isPublishedSectionPost() && (p.Visibility == visibilityPublic || p.Visibility == visibilityUnlisted) {
a.apCheckMentions(p)
a.apCheckActivityPubReply(p)
a.apPost(p)
}
})
a.pUpdateHooks = append(a.pUpdateHooks, func(p *post) {
if p.isPublishedSectionPost() && (p.Visibility == visibilityPublic || p.Visibility == visibilityUnlisted) {
a.apCheckMentions(p)
a.apCheckActivityPubReply(p)
a.apUpdate(p)
}
})
a.pDeleteHooks = append(a.pDeleteHooks, func(p *post) {
a.apDelete(p)
})
a.pUndeleteHooks = append(a.pUndeleteHooks, func(p *post) {
if p.isPublishedSectionPost() && (p.Visibility == visibilityPublic || p.Visibility == visibilityUnlisted) {
a.apUndelete(p)
}
})
// Prepare webfinger
a.prepareWebfinger()
// Read key and prepare signing
err := a.loadActivityPubPrivateKey()
if err != nil {
return err
}
a.apSigner, _, err = httpsig.NewSigner(
[]httpsig.Algorithm{httpsig.RSA_SHA256},
httpsig.DigestSha256,
[]string{httpsig.RequestTarget, "date", "host", "digest"},
httpsig.Signature,
0,
)
if err != nil {
return err
}
// Init http client
a.apHttpClients = map[string]*apc.C{}
for blog, bc := range a.cfg.Blogs {
a.apHttpClients[blog] = apc.New(
apc.WithHTTPClient(a.httpClient),
apc.WithSignFn(func(r *http.Request) error {
return a.signRequest(r, a.apIri(bc))
}),
)
}
// Init send queue
a.initAPSendQueue()
// Send profile updates
go func() {
// First wait a bit
time.Sleep(time.Second * 10)
// Then send profile update
a.apSendProfileUpdates()
}()
return nil
}
func (a *goBlog) apEnabled() bool {
if a.isPrivate() {
// Private mode, no AP
return false
}
if apc := a.cfg.ActivityPub; apc == nil || !apc.Enabled {
// Disabled
return false
}
return true
}
func (a *goBlog) prepareWebfinger() {
a.apUserHandle = map[string]string{}
a.webfingerResources = map[string]*configBlog{}
a.webfingerAccts = map[string]string{}
for name, blog := range a.cfg.Blogs {
a.apUserHandle[name] = "@" + name + "@" + a.cfg.Server.publicHostname
acct := "acct:" + name + "@" + a.cfg.Server.publicHostname
a.webfingerResources[acct] = blog
a.webfingerResources[a.apIri(blog)] = blog
a.webfingerAccts[a.apIri(blog)] = acct
}
}
func (a *goBlog) apHandleWebfinger(w http.ResponseWriter, r *http.Request) {
blog, ok := a.webfingerResources[r.URL.Query().Get("resource")]
if !ok {
a.serveError(w, r, "Resource not found", http.StatusNotFound)
return
}
apIri := a.apIri(blog)
// Encode
pr, pw := io.Pipe()
go func() {
_ = pw.CloseWithError(json.NewEncoder(pw).Encode(map[string]any{
"subject": a.webfingerAccts[apIri],
"aliases": []string{a.webfingerAccts[apIri], apIri},
"links": []map[string]string{
{
"rel": "self", "type": contenttype.AS, "href": apIri,
},
{
"rel": "http://webfinger.net/rel/profile-page",
"type": "text/html", "href": apIri,
},
},
}))
}()
w.Header().Set(contentType, "application/jrd+json"+contenttype.CharsetUtf8Suffix)
_ = pr.CloseWithError(a.min.Get().Minify(contenttype.JSON, w, pr))
}
const activityPubMentionsParameter = "activitypubmentions"
func (a *goBlog) apCheckMentions(p *post) {
pr, pw := io.Pipe()
go func() {
a.postHtmlToWriter(pw, &postHtmlOptions{p: p})
_ = pw.Close()
}()
links, err := allLinksFromHTML(pr, a.fullPostURL(p))
_ = pr.CloseWithError(err)
if err != nil {
a.error("ActivityPub: Failed to extract links from post", err)
return
}
apc := a.apHttpClients[p.Blog]
mentions := []string{}
for _, link := range links {
act, err := apc.Actor(context.Background(), ap.IRI(link))
if err != nil || act == nil || act.Type != ap.PersonType {
continue
}
mentions = append(mentions, link)
}
if p.Parameters == nil {
p.Parameters = map[string][]string{}
}
p.Parameters[activityPubMentionsParameter] = mentions
_ = a.db.replacePostParam(p.Path, activityPubMentionsParameter, mentions)
}
const activityPubReplyActorParameter = "activitypubreplyactor"
func (a *goBlog) apCheckActivityPubReply(p *post) {
replyLink := a.replyLink(p)
if replyLink == "" {
return
}
apc := a.apHttpClients[p.Blog]
item, err := apc.LoadIRI(ap.IRI(replyLink))
if err != nil || item == nil || !ap.IsObject(item) {
return
}
obj, err := ap.ToObject(item)
if err != nil || obj == nil || obj.GetLink() == "" || obj.AttributedTo == nil || obj.AttributedTo.GetLink() == "" {
return
}
replyLinkActor := []string{obj.AttributedTo.GetLink().String()}
if p.Parameters == nil {
p.Parameters = map[string][]string{}
}
p.Parameters[activityPubReplyActorParameter] = replyLinkActor
_ = a.db.replacePostParam(p.Path, activityPubReplyActorParameter, replyLinkActor)
}
func (a *goBlog) apHandleInbox(w http.ResponseWriter, r *http.Request) {
// Get blog
blogName := chi.URLParam(r, "blog")
blog, ok := a.cfg.Blogs[blogName]
if !ok || blog == nil {
a.serveError(w, r, "Inbox not found", http.StatusNotFound)
return
}
// Verify request
requestActor, err := a.apVerifySignature(r, blogName)
if err != nil {
// Send 401 because signature could not be verified
a.serveError(w, r, err.Error(), http.StatusUnauthorized)
return
}
// Parse activity
body, err := io.ReadAll(r.Body)
if err != nil {
a.serveError(w, r, "Failed to read body", http.StatusBadRequest)
return
}
apItem, err := ap.UnmarshalJSON(body)
if err != nil {
a.serveError(w, r, "Failed to decode body", http.StatusBadRequest)
return
}
// Check if it's an activity
activity, err := ap.ToActivity(apItem)
if err != nil {
a.serveError(w, r, "No activity", http.StatusBadRequest)
return
}
// Check actor
activityActor := activity.Actor.GetLink()
if activity.Actor == nil || (!activity.Actor.IsLink() && !activity.Actor.IsObject()) {
a.serveError(w, r, "Activity has no actor", http.StatusBadRequest)
return
}
if activityActor != requestActor.GetLink() {
a.serveError(w, r, "Request actor isn't activity actor", http.StatusForbidden)
return
}
// Handle activity
switch activity.GetType() {
case ap.FollowType:
a.apAccept(blogName, blog, activity)
case ap.UndoType:
if activity.Object.IsObject() {
objectActivity, err := ap.ToActivity(activity.Object)
if err == nil && objectActivity.GetType() == ap.FollowType && objectActivity.Actor.GetLink() == activityActor {
_ = a.db.apRemoveFollower(blogName, activityActor.String())
}
}
case ap.CreateType, ap.UpdateType:
if activity.Object.IsObject() {
a.apOnCreateUpdate(blog, requestActor, activity)
}
case ap.DeleteType, ap.BlockType:
if activity.Object.GetLink() == activityActor {
_ = a.db.apRemoveFollower(blogName, activityActor.String())
} else {
// Check if comment exists
exists, commentId, err := a.db.commentIdByOriginal(activity.Object.GetLink().String())
if err == nil && exists {
_ = a.db.deleteComment(commentId)
_ = a.db.deleteWebmentionUUrl(activity.Object.GetLink().String())
}
}
case ap.AnnounceType:
if announceTarget := activity.Object.GetLink().String(); announceTarget != "" && strings.HasPrefix(announceTarget, a.cfg.Server.PublicAddress) {
a.sendNotification(fmt.Sprintf("%s announced %s", activityActor, announceTarget))
}
case ap.LikeType:
if likeTarget := activity.Object.GetLink().String(); likeTarget != "" && strings.HasPrefix(likeTarget, a.cfg.Server.PublicAddress) {
a.sendNotification(fmt.Sprintf("%s liked %s", activityActor, likeTarget))
}
}
// Return 200
w.WriteHeader(http.StatusOK)
}
func (a *goBlog) apOnCreateUpdate(blog *configBlog, requestActor *ap.Actor, activity *ap.Activity) {
object, err := ap.ToObject(activity.Object)
if err != nil {
return
}
if object.GetType() != ap.NoteType && object.GetType() != ap.ArticleType {
// ignore other objects for now
return
}
// Get information from the note
noteUri := object.GetLink().String()
actorName := cmp.Or(requestActor.Name.First().Value.String(), apUsername(requestActor))
actorLink := requestActor.GetLink().String()
if requestActor.URL != nil {
actorLink = requestActor.URL.GetLink().String()
}
content := object.Content.First().Value.String()
// Handle reply
if inReplyTo := object.InReplyTo; inReplyTo != nil {
if replyTarget := inReplyTo.GetLink().String(); replyTarget != "" && strings.HasPrefix(replyTarget, a.cfg.Server.PublicAddress) {
if object.To.Contains(ap.PublicNS) || object.CC.Contains(ap.PublicNS) {
// Public reply - comment
_, _, _ = a.createComment(blog, replyTarget, content, actorName, actorLink, noteUri)
return
} else {
// Private reply - notification
buf := bufferpool.Get()
defer bufferpool.Put(buf)
fmt.Fprintf(buf, "New private ActivityPub reply to %s from %s\n", cleanHTMLText(replyTarget), cleanHTMLText(noteUri))
fmt.Fprintf(buf, "Author: %s (%s)", cleanHTMLText(actorName), cleanHTMLText(actorLink))
buf.WriteString("\n\n")
buf.WriteString(cleanHTMLText(content))
a.sendNotification(buf.String())
return
}
}
}
// Handle mention
if blogIri := ap.IRI(a.apIri(blog)); object.To.Contains(blogIri) || object.CC.Contains(blogIri) {
// Notification
buf := bufferpool.Get()
defer bufferpool.Put(buf)
fmt.Fprintf(buf, "New ActivityPub mention on %s\n", cleanHTMLText(noteUri))
fmt.Fprintf(buf, "Author: %s (%s)", cleanHTMLText(actorName), cleanHTMLText(actorLink))
buf.WriteString("\n\n")
buf.WriteString(cleanHTMLText(content))
a.sendNotification(buf.String())
return
}
// Ignore other cases, maybe it's just spam
}
func (a *goBlog) apVerifySignature(r *http.Request, blog string) (*ap.Actor, error) {
verifier, err := httpsig.NewVerifier(r)
if err != nil {
// Error with signature header etc.
return nil, err
}
actor, err := a.apGetRemoteActor(ap.IRI(verifier.KeyId()), blog)
if err != nil || actor == nil {
// Actor not found or something else bad
return nil, errors.New("failed to get actor")
}
if actor.PublicKey.PublicKeyPem == "" {
return nil, errors.New("actor has no public key")
}
block, _ := pem.Decode([]byte(actor.PublicKey.PublicKeyPem))
if block == nil {
return nil, errors.New("public key invalid")
}
pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
// Unable to parse public key
return nil, err
}
return actor, verifier.Verify(pubKey, httpsig.RSA_SHA256)
}
func handleWellKnownHostMeta(w http.ResponseWriter, r *http.Request) {
w.Header().Set(contentType, "application/xrd+xml"+contenttype.CharsetUtf8Suffix)
_, _ = io.WriteString(w, xml.Header)
_, _ = io.WriteString(w, `<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" type="application/xrd+xml" template="https://`+r.Host+`/.well-known/webfinger?resource={uri}"/></XRD>`)
}
func (a *goBlog) apGetFollowersCollectionId(blogName string, blog *configBlog) ap.IRI {
return ap.IRI(a.apIri(blog) + "/activitypub/followers/" + blogName)
}
func (a *goBlog) apShowFollowers(w http.ResponseWriter, r *http.Request) {
blogName := chi.URLParam(r, "blog")
blog, ok := a.cfg.Blogs[blogName]
if !ok || blog == nil {
a.serveError(w, r, "Blog not found", http.StatusNotFound)
return
}
followers, err := a.db.apGetAllFollowers(blogName)
if err != nil {
a.serveError(w, r, "Failed to get followers", http.StatusInternalServerError)
return
}
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
followersCollection := ap.CollectionNew(a.apGetFollowersCollectionId(blogName, blog))
for _, follower := range followers {
followersCollection.Items.Append(ap.IRI(follower.follower))
}
followersCollection.TotalItems = uint(len(followers))
a.serveAPItem(w, r, http.StatusOK, followersCollection)
return
}
a.render(w, r, a.renderActivityPubFollowers, &renderData{
BlogString: blogName,
Data: &activityPubFollowersRenderData{
apUser: fmt.Sprintf("@%s@%s", blogName, a.cfg.Server.publicHostname),
followers: followers,
},
})
}
func (a *goBlog) apGetRemoteActor(iri ap.IRI, blog string) (*ap.Actor, error) {
return a.apHttpClients[blog].Actor(context.Background(), iri)
}
func (db *database) apGetAllInboxes(blog string) (inboxes []string, err error) {
rows, err := db.Query("select distinct inbox from activitypub_followers where blog = @blog", sql.Named("blog", blog))
if err != nil {
return nil, err
}
defer rows.Close()
var inbox string
for rows.Next() {
err = rows.Scan(&inbox)
if err != nil {
return nil, err
}
inboxes = append(inboxes, inbox)
}
return inboxes, nil
}
type apFollower struct {
follower, inbox, username string
}
func (db *database) apGetAllFollowers(blog string) (followers []*apFollower, err error) {
rows, err := db.Query("select follower, inbox, username from activitypub_followers where blog = @blog", sql.Named("blog", blog))
if err != nil {
return nil, err
}
defer rows.Close()
var follower, inbox, username string
for rows.Next() {
err = rows.Scan(&follower, &inbox, &username)
if err != nil {
return nil, err
}
followers = append(followers, &apFollower{follower: follower, inbox: inbox, username: username})
}
return followers, nil
}
func (db *database) apAddFollower(blog, follower, inbox, username string) error {
_, err := db.Exec(
"insert or replace into activitypub_followers (blog, follower, inbox, username) values (@blog, @follower, @inbox, @username)",
sql.Named("blog", blog), sql.Named("follower", follower), sql.Named("inbox", inbox), sql.Named("username", username),
)
return err
}
func (db *database) apRemoveFollower(blog, follower string) error {
_, err := db.Exec("delete from activitypub_followers where blog = @blog and follower = @follower", sql.Named("blog", blog), sql.Named("follower", follower))
return err
}
func (db *database) apRemoveInbox(inbox string) error {
_, err := db.Exec("delete from activitypub_followers where inbox = @inbox", sql.Named("inbox", inbox))
return err
}
func (a *goBlog) apPost(p *post) {
blogConfig := a.getBlogFromPost(p)
c := ap.CreateNew(a.apNewID(blogConfig), a.toAPNote(p))
c.Actor = a.apAPIri(blogConfig)
c.Published = time.Now()
a.apSendToAllFollowers(p.Blog, c, append(p.Parameters[activityPubMentionsParameter], p.firstParameter(activityPubReplyActorParameter))...)
}
func (a *goBlog) apUpdate(p *post) {
blogConfig := a.getBlogFromPost(p)
u := ap.UpdateNew(a.apNewID(blogConfig), a.toAPNote(p))
u.Actor = a.apAPIri(blogConfig)
u.Published = time.Now()
a.apSendToAllFollowers(p.Blog, u, append(p.Parameters[activityPubMentionsParameter], p.firstParameter(activityPubReplyActorParameter))...)
}
func (a *goBlog) apDelete(p *post) {
blogConfig := a.getBlogFromPost(p)
d := ap.DeleteNew(a.apNewID(blogConfig), a.activityPubId(p))
d.Actor = a.apAPIri(blogConfig)
d.Published = time.Now()
a.apSendToAllFollowers(p.Blog, d, append(p.Parameters[activityPubMentionsParameter], p.firstParameter(activityPubReplyActorParameter))...)
}
func (a *goBlog) apUndelete(p *post) {
// The optimal way to do this would be to send a "Undo Delete" activity,
// but that doesn't work with Mastodon yet.
// see:
// https://socialhub.activitypub.rocks/t/soft-deletes-and-restoring-deleted-posts/2318
// https://github.com/mastodon/mastodon/issues/17553
// Update "activityPubVersion" parameter to current timestamp in nanoseconds
p.Parameters[activityPubVersionParam] = []string{fmt.Sprintf("%d", utcNowNanos())}
_ = a.db.replacePostParam(p.Path, activityPubVersionParam, p.Parameters[activityPubVersionParam])
// Post as new post
a.apPost(p)
}
func (a *goBlog) apAccept(blogName string, blog *configBlog, follow *ap.Activity) {
newFollower := follow.Actor.GetLink()
a.info("AcitivyPub: New follow request from follower", "id", newFollower.String())
// Get remote actor
follower, err := a.apGetRemoteActor(newFollower, blogName)
if err != nil || follower == nil {
// Couldn't retrieve remote actor info
a.error("ActivityPub: Failed to retrieve remote actor info", "actor", newFollower)
return
}
// Add or update follower
inbox := follower.Inbox.GetLink()
if endpoints := follower.Endpoints; endpoints != nil && endpoints.SharedInbox != nil && endpoints.SharedInbox.GetLink() != "" {
inbox = endpoints.SharedInbox.GetLink()
}
if inbox == "" {
return
}
username := apUsername(follower)
if err = a.db.apAddFollower(blogName, follower.GetLink().String(), inbox.String(), username); err != nil {
return
}
// Send accept response to the new follower
accept := ap.AcceptNew(a.apNewID(blog), follow)
accept.To.Append(newFollower)
accept.Actor = a.apAPIri(blog)
_ = a.apQueueSendSigned(a.apIri(blog), inbox.String(), accept)
// Notification
a.sendNotification(fmt.Sprintf("%s (%s) started following %s", username, follower.GetLink().String(), a.apIri(blog)))
}
func (a *goBlog) apSendProfileUpdates() {
for blog, config := range a.cfg.Blogs {
person := a.toApPerson(blog)
update := ap.UpdateNew(a.apNewID(config), person)
update.Actor = a.apAPIri(config)
update.Published = time.Now()
update.To.Append(ap.PublicNS, a.apGetFollowersCollectionId(blog, config))
a.apSendToAllFollowers(blog, update)
}
}
func (a *goBlog) apSendToAllFollowers(blog string, activity *ap.Activity, mentions ...string) {
inboxes, err := a.db.apGetAllInboxes(blog)
if err != nil {
a.error("ActivityPub: Failed to retrieve follower inboxes", "err", err)
return
}
for _, m := range mentions {
go func(m string) {
if m == "" {
return
}
apc := a.apHttpClients[blog]
actor, err := apc.Actor(context.Background(), ap.IRI(m))
if err != nil || actor == nil || actor.Inbox == nil || actor.Inbox.GetLink() == "" {
return
}
inbox := actor.Inbox.GetLink().String()
a.apSendTo(a.apIri(a.cfg.Blogs[blog]), activity, inbox)
}(m)
}
a.apSendTo(a.apIri(a.cfg.Blogs[blog]), activity, inboxes...)
}
func (a *goBlog) apSendTo(blogIri string, activity *ap.Activity, inboxes ...string) {
for _, i := range lo.Uniq(inboxes) {
go func(inbox string) {
_ = a.apQueueSendSigned(blogIri, inbox, activity)
}(i)
}
}
func (a *goBlog) apNewID(blog *configBlog) ap.ID {
return ap.ID(a.apIri(blog) + "#" + uuid.NewString())
}
func (a *goBlog) apIri(b *configBlog) string {
return a.getFullAddress(b.getRelativePath(""))
}
func (a *goBlog) apAPIri(b *configBlog) ap.IRI {
return ap.IRI(a.apIri(b))
}
func apRequestIsSuccess(code int) bool {
return code == http.StatusOK || code == http.StatusCreated || code == http.StatusAccepted || code == http.StatusNoContent
}
// Load or generate key for ActivityPub communication
func (a *goBlog) loadActivityPubPrivateKey() error {
// Check if already loaded
if a.apPrivateKey != nil {
return nil
}
// Check if already generated
if keyData, err := a.db.retrievePersistentCache("activitypub_key"); err == nil && keyData != nil {
privateKeyDecoded, _ := pem.Decode(keyData)
if privateKeyDecoded == nil {
a.error("ActivityPub: failed to decode cached private key")
// continue
} else {
key, err := x509.ParsePKCS1PrivateKey(privateKeyDecoded.Bytes)
if err != nil {
return err
}
pubKeyBytes, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
if err != nil {
return err
}
a.apPrivateKey = key
a.apPubKeyBytes = pubKeyBytes
return nil
}
}
// Generate and cache key
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
pubKeyBytes, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
if err != nil {
return err
}
a.apPrivateKey = key
a.apPubKeyBytes = pubKeyBytes
return a.db.cachePersistently(
"activitypub_key",
pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(a.apPrivateKey),
}),
)
}
func (a *goBlog) signRequest(r *http.Request, blogIri string) error {
if date := r.Header.Get("Date"); date == "" {
r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
}
if host := r.Header.Get("Host"); host == "" {
r.Header.Set("Host", r.URL.Host)
}
bodyBuf := bytes.NewBufferString("")
if r.Body != nil {
if _, err := io.Copy(bodyBuf, r.Body); err == nil {
r.Body = io.NopCloser(bodyBuf)
}
}
a.apSignMutex.Lock()
defer a.apSignMutex.Unlock()
return a.apSigner.SignRequest(a.apPrivateKey, blogIri+"#main-key", r, bodyBuf.Bytes())
}