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
package server

import (
	"encoding/json"
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
	"regexp"
	"rss_parrot/dto"
	"rss_parrot/logic"
	"rss_parrot/shared"
)

// Groups together the handlers needed to implement an ActivityPub server.
type apubHandlerGroup struct {
	cfg        *shared.Config
	logger     shared.ILogger
	metrics    logic.IMetrics
	sender     logic.IActivitySender
	sigChecker logic.IHttpSigChecker
	udir       logic.IUserDirectory
	inbox      logic.IInbox
	reResource *regexp.Regexp
}

func NewApubHandlerGroup(
	cfg *shared.Config,
	logger shared.ILogger,
	metrics logic.IMetrics,
	sender logic.IActivitySender,
	sigChecker logic.IHttpSigChecker,
	udir logic.IUserDirectory,
	ibox logic.IInbox,
) IHandlerGroup {
	res := apubHandlerGroup{
		cfg:        cfg,
		logger:     logger,
		metrics:    metrics,
		sender:     sender,
		sigChecker: sigChecker,
		udir:       udir,
		inbox:      ibox,
	}
	res.reResource = regexp.MustCompile("^acct:([^@]+)@([^@]+)$")
	return &res
}

func (hg *apubHandlerGroup) Prefix() string {
	return ""
}

func (hg *apubHandlerGroup) GroupDefs() []handlerDef {
	return []handlerDef{
		{"GET", "/.well-known/webfinger", func(w http.ResponseWriter, r *http.Request) { hg.getWebfinger(w, r) }},
		{"GET", "/u/{user}", func(w http.ResponseWriter, r *http.Request) { hg.getUser(w, r) }},
		{"GET", "/u/{user}/outbox", func(w http.ResponseWriter, r *http.Request) { hg.getUserOutbox(w, r) }},
		{"GET", "/u/{user}/followers", func(w http.ResponseWriter, r *http.Request) { hg.getUserFollowers(w, r) }},
		{"GET", "/u/{user}/following", func(w http.ResponseWriter, r *http.Request) { hg.getUserFollowing(w, r) }},
		{"GET", "/u/{user}/status/{id}", func(w http.ResponseWriter, r *http.Request) { hg.getUserStatus(w, r) }},
		{"POST", "/u/{user}/inbox", func(w http.ResponseWriter, r *http.Request) { hg.postInbox(w, r) }},
		{"POST", "/inbox", func(w http.ResponseWriter, r *http.Request) { hg.postInbox(w, r) }},
	}
}

func (hg *apubHandlerGroup) AuthMW() func(next http.Handler) http.Handler {
	return emptyMW
}

func (hg *apubHandlerGroup) getWebfinger(w http.ResponseWriter, r *http.Request) {

	hg.logger.Infof("Handling webfinger GET: %s", r.URL.Path)
	obs := hg.metrics.StartApubRequestIn("webfinger")
	defer obs.Finish()

	resourceParam := r.URL.Query().Get("resource")
	groups := hg.reResource.FindStringSubmatch(resourceParam)
	if groups == nil {
		hg.logger.Infof("Webfinger: Invalid request; 'resource' param is '%s'", resourceParam)
		writeErrorResponse(w, "Missing or invalid 'resource' param", http.StatusBadRequest)
		return
	}
	user, host := groups[1], groups[2]
	_ = host // TODO check is host for us?

	resp := hg.udir.GetWebfinger(user)

	if resp == nil {
		hg.logger.Infof("Webfinger: No such resource; 'resource' param is '%s'", resourceParam)
		writeErrorResponse(w, "No such resource", http.StatusNotFound)
		return
	}

	writeJsonResponse(hg.logger, w, rtJrdJson, resp)
}

func (hg *apubHandlerGroup) getUser(w http.ResponseWriter, r *http.Request) {

	hg.logger.Infof("Handling user GET: %s", r.URL.Path)
	obs := hg.metrics.StartApubRequestIn("user")
	defer obs.Finish()
	userName := mux.Vars(r)["user"]

	if !acceptsJson(r) {
		idb := shared.IdBuilder{Host: hg.cfg.Host}
		profileUrl := idb.UserProfile(userName)
		hg.logger.Infof("No application/json in accept header; redirecting to: '%s'", profileUrl)
		http.Redirect(w, r, profileUrl, http.StatusSeeOther)
		return
	}

	userInfo := hg.udir.GetUserInfo(userName)

	if userInfo == nil {
		hg.logger.Infof("Info requested for unknown user: '%s'", userName)
		writeErrorResponse(w, "No such user", http.StatusNotFound)
		return
	}

	writeJsonResponse(hg.logger, w, rtActivityJson, userInfo)
}

func (hg *apubHandlerGroup) getUserStatus(w http.ResponseWriter, r *http.Request) {

	hg.logger.Infof("Handling user status GET: %s", r.URL.Path)
	obs := hg.metrics.StartApubRequestIn("user/status")
	defer obs.Finish()

	userName := mux.Vars(r)["user"]
	statusId := mux.Vars(r)["id"]

	if !acceptsJson(r) {
		idb := shared.IdBuilder{Host: hg.cfg.Host}
		profileUrl := idb.UserProfile(userName)
		hg.logger.Infof("No application/json in accept header; redirecting to: '%s'", profileUrl)
		http.Redirect(w, r, profileUrl, http.StatusSeeOther)
		return
	}

	var err error
	var note *dto.Note
	if note, err = hg.udir.GetUserStatus(userName, statusId); err != nil {
		hg.logger.Infof("Error retrieving status %s/%s: %v", userName, statusId, err)
		writeErrorResponse(w, internalErrorStr, http.StatusInternalServerError)
		return
	}

	if note == nil {
		hg.logger.Infof("User status not found: %s/%s", userName, statusId)
		writeErrorResponse(w, "User or status not found", http.StatusNotFound)
		return
	}

	note.Context = "https://www.w3.org/ns/activitystreams"

	writeJsonResponse(hg.logger, w, rtActivityJson, note)
}

func (hg *apubHandlerGroup) getUserOutbox(w http.ResponseWriter, r *http.Request) {

	hg.logger.Infof("Handling user outbox GET: %s", r.URL.Path)
	obs := hg.metrics.StartApubRequestIn("user/outbox")
	defer obs.Finish()

	userName := mux.Vars(r)["user"]
	summary := hg.udir.GetOutboxSummary(userName)
	if summary == nil {
		hg.logger.Infof("Outbox requested for unknown user: '%s'", userName)
		writeErrorResponse(w, "No such user", http.StatusNotFound)
		return
	}
	writeJsonResponse(hg.logger, w, rtActivityJson, summary)
}

func (hg *apubHandlerGroup) getUserFollowers(w http.ResponseWriter, r *http.Request) {

	hg.logger.Infof("Handling user followers GET: %s", r.URL.Path)
	obs := hg.metrics.StartApubRequestIn("user/followers")
	defer obs.Finish()

	userName := mux.Vars(r)["user"]
	summary := hg.udir.GetFollowersSummary(userName)
	if summary == nil {
		hg.logger.Infof("Followers requested for unknown user: '%s'", userName)
		writeErrorResponse(w, "No such user", http.StatusNotFound)
		return
	}
	writeJsonResponse(hg.logger, w, rtActivityJson, summary)
}

func (hg *apubHandlerGroup) getUserFollowing(w http.ResponseWriter, r *http.Request) {

	hg.logger.Infof("Handling user following GET: %s", r.URL.Path)
	obs := hg.metrics.StartApubRequestIn("user/following")
	defer obs.Finish()

	userName := mux.Vars(r)["user"]
	summary := hg.udir.GetFollowingSummary(userName)
	if summary == nil {
		hg.logger.Infof("Following requested for unknown user: '%s'", userName)
		writeErrorResponse(w, "No such user", http.StatusNotFound)
		return
	}
	writeJsonResponse(hg.logger, w, rtActivityJson, summary)
}

func (hg *apubHandlerGroup) postInbox(w http.ResponseWriter, r *http.Request) {

	var err error
	hg.logger.Infof("Handling user inbox POST: %s", r.URL.Path)
	userName := mux.Vars(r)["user"]

	if userName == "" {
		obs := hg.metrics.StartApubRequestIn("inbox")
		defer obs.Finish()
	} else {
		obs := hg.metrics.StartApubRequestIn("user/inbox")
		defer obs.Finish()
	}

	bodyBytes := readBody(hg.logger, w, r)
	if bodyBytes == nil {
		hg.logger.Info("Empty request body")
		writeErrorResponse(w, "Request body must not be empty", http.StatusBadRequest)
		return
	}

	// DBG
	//hg.logger.Debug(string(bodyBytes))

	// First, parse a rudimentary version of the activity to check signature, find out activity type
	var act dto.ActivityInBase
	if err = json.Unmarshal(bodyBytes, &act); err != nil {
		hg.logger.Infof("Invalid JSON in request body: %v: %s", err, string(bodyBytes))
		writeErrorResponse(w, "Request body is not valid JSON", http.StatusBadRequest)
		return
	}

	// Verify signature
	var senderInfo *dto.UserInfo
	var sigProblem string
	senderInfo, sigProblem, err = hg.sigChecker.Check(act.Actor, w, r)

	if err != nil {
		hg.logger.Errorf("Unexpected error trying to verify signature: %v", err)
		writeErrorResponse(w, internalErrorStr, http.StatusInternalServerError)
		return
	}

	if sigProblem != "" {
		if act.Type == "Delete" {
			hg.logger.Infof("Ignoring Delete request with unverified actor signature")
			writeJsonResponse(hg.logger, w, rtActivityJson, "OK")
		} else {
			hg.logger.Warnf("Incorrectly signed inbox POST request: %s", sigProblem)
			msg := fmt.Sprintf("Invalid HTTP signature: %s", sigProblem)
			writeErrorResponse(w, msg, http.StatusUnauthorized)
		}
		return
	}

	hg.processActivity(userName, bodyBytes, senderInfo, act, w)
}

func (hg *apubHandlerGroup) processActivity(
	userName string,
	bodyBytes []byte,
	senderInfo *dto.UserInfo,
	act dto.ActivityInBase,
	w http.ResponseWriter,
) {

	var err error

	// Find out Object's type if object field is, well, an object
	// If yes, grab object type field
	objectType := ""
	if objMap, ok := act.Object.(map[string]interface{}); ok {
		if objTypeStr, ok := objMap["type"].(string); ok {
			objectType = objTypeStr
		}
	}

	// Handle different activities
	var reqProblem string
	if act.Type == "Follow" {
		reqProblem, err = hg.inbox.HandleFollow(userName, senderInfo, bodyBytes)
	} else if act.Type == "Undo" {
		reqProblem, err = hg.inbox.HandleUndo(userName, senderInfo, bodyBytes)
	} else if act.Type == "Create" {
		if objectType == "Note" {
			reqProblem, err = hg.inbox.HandleCreateNote(act, senderInfo, bodyBytes)
		}
	}

	if err != nil {
		hg.logger.Errorf("Error handling inbox activity: %v", err)
		writeErrorResponse(w, internalErrorStr, http.StatusInternalServerError)
		return
	}

	if reqProblem != "" {
		hg.logger.Infof("Invalid '%s' request: %s", act.Type, reqProblem)
		msg := fmt.Sprintf("Bad request: %s", reqProblem)
		writeErrorResponse(w, msg, http.StatusBadRequest)
		return
	}

	writeJsonResponse(hg.logger, w, rtActivityJson, "OK")
}