aboutsummaryrefslogtreecommitdiff
path: root/controller/controller.go
blob: 91ac267a31b8ef6ca084ef9408407ed4cd5cb60a (plain)
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
package controller

import (
	"bytes"
	"context"
	"crypto/x509"
	"encoding/gob"
	"encoding/json"
	"encoding/pem"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"

	"git.capotej.com/capotej/communique/config"
	"git.capotej.com/capotej/communique/delivery"
	"git.capotej.com/capotej/communique/models"
	"git.capotej.com/capotej/communique/tools"
	"git.capotej.com/capotej/communique/urls"
	"git.capotej.com/capotej/communique/views"
	"github.com/go-fed/activity/streams"
	"github.com/go-fed/activity/streams/vocab"
	"go.uber.org/zap"
)

type Handler struct {
	handlerCfg config.Handler
}

type Controller struct {
	cfg        config.Config
	persister  *models.Persister
	handlerMap map[string]Handler
	log        *zap.SugaredLogger
	delivery   *delivery.Signed
}

func NewController(cfg config.Config, persister *models.Persister, log *zap.SugaredLogger) (*Controller, error) {
	reg := Controller{cfg: cfg, persister: persister, log: log}
	reg.handlerMap = make(map[string]Handler)
	var err error
	for _, v := range cfg.Handlers {
		reg.handlerMap[v.Name] = Handler{handlerCfg: v}
		err = generateKeypairIfNeeded(v, persister)
		if err != nil {
			return nil, err
		}
		err = persistAvatarIfFound(v, persister)
		if err != nil {
			return nil, err
		}
	}
	reg.delivery, err = delivery.NewSigned(persister)
	if err != nil {
		return nil, err
	}
	return &reg, nil
}

func generateKeypairIfNeeded(v config.Handler, p *models.Persister) error {
	kp, err := models.CreateKeypair(v)
	if err != nil {
		return err
	}
	err = p.Store(kp)
	if err != nil {
		return err
	}
	return nil
}

func persistAvatarIfFound(v config.Handler, p *models.Persister) error {
	if v.AvatarUrl == "" || v.AvatarContentType == "" {
		return nil
	}
	resp, err := http.Get(v.AvatarUrl)
	if resp.StatusCode != 200 {
		return fmt.Errorf("request to avatarUrl %s failed", v.AvatarUrl)
	}
	contentType := resp.Header.Get("content-type")
	if contentType != v.AvatarContentType {
		return fmt.Errorf("avatarUrl response content-type '%s' does match avatarContentType '%s'", contentType, v.AvatarContentType)
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	if len(body) == 0 {
		return fmt.Errorf("avatarUrl response was empty")
	}
	kp, err := models.CreateAvatar(v, v.AvatarContentType, body)
	if err != nil {
		return err
	}
	err = p.Store(kp)
	if err != nil {
		return err
	}
	return nil
}

func (r *Controller) Actor(name string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	aso := models.NewKeypair(handler.handlerCfg)
	result, err := r.persister.Find(aso)
	if err != nil {
		return nil, err
	}
	buf := bytes.NewBuffer(result)
	dec := gob.NewDecoder(buf)
	var keypair models.Keypair
	err = dec.Decode(&keypair)
	if err != nil {
		return nil, err
	}
	privKey := &keypair.PrivateKey
	pemdata := pem.EncodeToMemory(
		&pem.Block{
			Type:  "RSA PUBLIC KEY",
			Bytes: x509.MarshalPKCS1PublicKey(&privKey.PublicKey),
		},
	)
	return views.RenderActor(handler.handlerCfg.Name, r.cfg.Domain, string(pemdata), handler.handlerCfg.AvatarContentType, handler.handlerCfg.Summary)
}

func (r *Controller) ActorAvatar(name string) ([]byte, string, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, "", nil
	}
	aso := models.NewAvatar(handler.handlerCfg)
	result, err := r.persister.Find(aso)
	if err != nil {
		return nil, "", err
	}
	buf := bytes.NewBuffer(result)
	dec := gob.NewDecoder(buf)
	var avatar models.Avatar
	err = dec.Decode(&avatar)
	if err != nil {
		return nil, "", err
	}
	return avatar.Bytes, handler.handlerCfg.AvatarContentType, nil
}

func (r *Controller) OutboxCollection(name string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	aso := models.NewOutboxItem(handler.handlerCfg)
	page, err := r.persister.Collect(aso)
	if err != nil {
		return nil, err
	}
	var outboxItems []models.OutboxItem
	for _, v := range page { //TODO pagination
		buf := bytes.NewBuffer(v)
		dec := gob.NewDecoder(buf)
		var outboxItem models.OutboxItem
		err = dec.Decode(&outboxItem)
		if err != nil {
			return nil, err
		}
		outboxItems = append(outboxItems, outboxItem)
	}
	return views.RenderOutboxCollection(handler.handlerCfg.Name, r.cfg.Domain, outboxItems)
}

func (r *Controller) Following(name string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	profile, err := urls.UrlActor(name, r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	following, err := urls.UrlFollowing(name, r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	result := make(map[string]interface{})
	result["@context"] = "https://www.w3.org/ns/activitystreams"
	result["attributedTo"] = profile.String()
	result["id"] = following.String()
	result["totalItems"] = 0
	result["orderedItems"] = []bool{}
	result["type"] = "OrderedCollection"
	return result, nil
}

func (r *Controller) Followers(name string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	profile, err := urls.UrlActor(name, r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	followers, err := urls.UrlFollowers(name, r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	result := make(map[string]interface{})
	result["@context"] = "https://www.w3.org/ns/activitystreams"
	result["attributedTo"] = profile.String()
	result["id"] = followers.String()
	result["totalItems"] = 0
	result["orderedItems"] = []bool{}
	result["type"] = "OrderedCollection"
	return result, nil
}

func (r *Controller) Inbox(name string, req *http.Request, payload []byte) error {
	handler := r.findByName(name)
	if handler == nil {
		return nil
	}

	domainUrl, err := url.Parse(r.cfg.Domain)
	if err != nil {
		return err
	}

	logger := r.log.With("type", "inbox")

	if req.Host != domainUrl.Host {
		logger.Warnf("%s != %s, configured domain should match incoming Host: header otherwise things may not work right. You'll need to enable 'ProxyPreserveHost' (for apache2) or proxy_set_header Host $host; (for nginx)", req.Host, r.cfg.Domain)
	}

	ctx := context.Background()

	person, verifyError := tools.VerifyRequest(ctx, req, r.log)
	if verifyError != nil {
		return verifyError
	}

	var followData map[string]interface{}
	err = json.Unmarshal(payload, &followData)
	if err != nil {
		return err
	}

	actorUrl, err := urls.UrlActor(handler.handlerCfg.Name, r.cfg.Domain)
	if err != nil {
		return err
	}

	resolver, err := streams.NewJSONResolver(func(c context.Context, follow vocab.ActivityStreamsFollow) error {
		// Follow
		idProp := person.GetJSONLDId()
		idPropUrl := idProp.Get()
		inboxProp := person.GetActivityStreamsInbox()
		inboxUrl := inboxProp.GetIRI()
		logger.With("actor", idPropUrl).With("inbox", inboxUrl).Debugf("follow")

		actorKeyUrl, err := urls.UrlActorKey(handler.handlerCfg.Name, r.cfg.Domain)
		if err != nil {
			return err
		}
		err = r.deliverAcceptToInbox(inboxUrl, actorUrl, actorKeyUrl, follow, handler.handlerCfg)
		if err != nil {
			return err
		}
		return r.subscribeActorToHandler(handler.handlerCfg, inboxUrl.String())
	}, func(c context.Context, note vocab.ActivityStreamsUndo) error {
		// Unfollow
		idProp := person.GetJSONLDId()
		idPropUrl := idProp.Get()
		inboxProp := person.GetActivityStreamsInbox()
		inboxUrl := inboxProp.GetIRI()
		logger.With("actor", idPropUrl).With("inbox", inboxUrl).Debugf("unfollow/undo")
		return r.unsubscribeActorToHandler(handler.handlerCfg, inboxUrl.String())
	})
	err = resolver.Resolve(ctx, followData)

	return err
}
func (r *Controller) subscribeActorToHandler(handler config.Handler, inboxUrl string) error {
	aso, err := models.CreateSubscription(handler, inboxUrl)
	r.persister.Store(aso)
	return err
}

func (r *Controller) unsubscribeActorToHandler(handler config.Handler, inboxUrl string) error {
	aso, err := models.CreateSubscription(handler, inboxUrl)
	r.persister.Delete(aso)
	return err
}

// TODO should probably be in its own delivery package
func (r *Controller) deliverAcceptToInbox(url, actorUrl, actorKeyUrl *url.URL, follow vocab.ActivityStreamsFollow, handler config.Handler) error {
	accept := streams.NewActivityStreamsAccept()
	actorProp := streams.NewActivityStreamsActorProperty()
	actorProp.AppendIRI(actorUrl)
	accept.SetActivityStreamsActor(actorProp)
	objProp := streams.NewActivityStreamsObjectProperty()
	objProp.AppendActivityStreamsFollow(follow)
	accept.SetActivityStreamsObject(objProp)
	payload, err := streams.Serialize(accept)
	if err != nil {
		return err
	}
	jsonData, err := json.Marshal(payload)
	if err != nil {
		return err
	}

	request, err := r.delivery.SignedRequest(handler, jsonData, url, actorKeyUrl)
	if err != nil {
		return err
	}

	r.log.With(
		"type",
		"delivery",
	).With(
		"inbox",
		url.String(),
	).With(
		"digest",
		request.Header.Get("digest"),
	).With(
		"signature",
		request.Header.Get("signature"),
	).With(
		"actor",
		actorUrl.String(),
	).Debug("sending signed accept request")

	client := &http.Client{}
	response, err := client.Do(request)
	if err != nil {
		return fmt.Errorf("could not send accept request: %w", err)
	}
	responseBody, err := io.ReadAll(response.Body)
	defer response.Body.Close()
	r.log.With("type", "delivery").With("response", responseBody).With("status", response.Status).Debugf("remote inbox response received")
	return err
}

func (r *Controller) ActivityOrNote(activityOrNote, name, id string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	lookup := models.NewOutboxItem(handler.handlerCfg)
	lookup.Id = []byte(id)
	result, err := r.persister.Find(lookup)
	if err != nil {
		return nil, err
	}
	if result == nil {
		return nil, nil
	}
	buf := bytes.NewBuffer(result)
	dec := gob.NewDecoder(buf)
	var outboxItem models.OutboxItem
	err = dec.Decode(&outboxItem)
	if err != nil {
		return nil, err
	}
	if activityOrNote == "activity" {
		return views.RenderActivity(handler.handlerCfg.Name, r.cfg.Domain, outboxItem)
	}
	return views.RenderNote(handler.handlerCfg.Name, r.cfg.Domain, outboxItem)
}

// This has to handle various lookup formats:
// ?resource=acct:actor@domain
// ?resource=actor@domain
// ?resource=actor
// ?resource=acct:actor
func (r *Controller) Webfinger(fqn string) (*views.WebfingerResource, error) {
	// Strip away acct: prefix, if found
	fqn = strings.TrimPrefix(fqn, "acct:")

	// Strip away @domain suffix, if found
	domainUrl, err := url.Parse(r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	hostname := "@" + domainUrl.Hostname()
	fqn = strings.TrimSuffix(fqn, hostname)

	// We should just have $actor left
	handler := r.findByName(fqn)
	if handler == nil {
		return nil, nil
	}
	return views.RenderWebfinger(handler.handlerCfg.Name, r.cfg.Domain, hostname)
}

func (r *Controller) findByName(name string) *Handler {
	handler, ok := r.handlerMap[name]
	if !ok {
		return nil
	}
	return &handler
}