-
Notifications
You must be signed in to change notification settings - Fork 25
/
activitypub.py
320 lines (267 loc) 路 10.8 KB
/
activitypub.py
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
"""Handles requests for ActivityPub endpoints: actors, inbox, etc.
"""
from base64 import b64encode
import datetime
from hashlib import sha256
import logging
import re
from flask import request
from google.cloud import ndb
from google.cloud.ndb import OR
from granary import as2
from oauth_dropins.webutil import flask_util, util
from oauth_dropins.webutil.flask_util import error
from oauth_dropins.webutil.util import json_dumps, json_loads
from app import app, cache
import common
from common import redirect_unwrap, redirect_wrap
from models import Activity, Follower, User
from httpsig.requests_auth import HTTPSignatureAuth
logger = logging.getLogger(__name__)
CACHE_TIME = datetime.timedelta(seconds=15)
SUPPORTED_TYPES = (
'Accept',
'Announce',
'Article',
'Audio',
'Create',
'Delete',
'Follow',
'Image',
'Like',
'Note',
'Undo',
'Video',
)
def send(activity, inbox_url, user_domain):
"""Sends an ActivityPub request to an inbox.
Args:
activity: dict, AS2 activity
inbox_url: string
user_domain: string, domain of the bridgy fed user sending the request
Returns:
requests.Response
"""
logger.info(f'Sending AP request from {user_domain}: {json_dumps(activity, indent=2)}')
# prepare HTTP Signature (required by Mastodon)
# https://w3c.github.io/activitypub/#authorization
# https://tools.ietf.org/html/draft-cavage-http-signatures-07
# https://github.com/tootsuite/mastodon/issues/4906#issuecomment-328844846
key_id = request.host_url + user_domain
user = User.get_or_create(user_domain)
auth = HTTPSignatureAuth(secret=user.private_pem(), key_id=key_id,
algorithm='rsa-sha256', sign_header='signature',
headers=('Date', 'Digest', 'Host'))
# deliver to inbox
body = json_dumps(activity).encode()
headers = {
'Content-Type': common.CONTENT_TYPE_AS2,
# required for HTTP Signature
# https://tools.ietf.org/html/draft-cavage-http-signatures-07#section-2.1.3
'Date': datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT'),
# required by Mastodon
# https://github.com/tootsuite/mastodon/pull/14556#issuecomment-674077648
'Digest': 'SHA-256=' + b64encode(sha256(body).digest()).decode(),
'Host': util.domain_from_link(inbox_url, minimize=False),
}
return common.requests_post(inbox_url, data=body, auth=auth,
headers=headers)
@app.get(f'/<regex("{common.DOMAIN_RE}"):domain>')
@flask_util.cached(cache, CACHE_TIME)
def actor(domain):
"""Fetches a domain's h-card and converts to AS2 actor."""
actor = common.actor(domain)
return (actor, {
'Content-Type': common.CONTENT_TYPE_AS2,
'Access-Control-Allow-Origin': '*',
})
@app.post(f'/inbox')
@app.post(f'/<regex("{common.DOMAIN_RE}"):domain>/inbox')
def inbox(domain=None):
"""Handles ActivityPub inbox delivery."""
body = request.get_data(as_text=True)
logger.info(f'Got: {body}')
# parse and validate AS2 activity
try:
activity = request.json
assert activity
except (TypeError, ValueError, AssertionError):
error("Couldn't parse body as JSON", exc_info=True)
logger.info(f'Got: {json_dumps(activity, indent=2)}')
obj = activity.get('object') or {}
if isinstance(obj, str):
obj = {'id': obj}
type = activity.get('type')
if type == 'Accept': # eg in response to a Follow
return '' # noop
if type not in SUPPORTED_TYPES:
error('Sorry, %s activities are not supported yet.' % type,
status=501)
# TODO: verify signature if there is one
if type == 'Undo' and obj.get('type') == 'Follow':
# skip actor fetch below; we don't need it to undo a follow
undo_follow(redirect_unwrap(activity))
return ''
elif type == 'Delete':
# we currently only actually delete followers for Deletes that are sent
# to the shared inbox, not individual users' inboxes, to help scaling
# background: https://github.com/snarfed/bridgy-fed/issues/284
if domain:
logger.info('Skipping Delete sent to individual user inbox')
return 'OK'
id = obj.get('id') if isinstance(obj, dict) else obj
if not isinstance(id, str):
error("Couldn't find id of object to delete")
# assume this is an actor
# https://github.com/snarfed/bridgy-fed/issues/63
logger.info(f'Deactivating Followers with src or dest = {id}')
followers = Follower.query(OR(Follower.src == id,
Follower.dest == id)
).fetch()
for f in followers:
f.status = 'inactive'
ndb.put_multi(followers)
return 'OK'
# fetch actor if necessary so we have name, profile photo, etc
actor = activity.get('actor')
if actor and isinstance(actor, str):
actor = activity['actor'] = common.get_as2(actor).json()
activity_unwrapped = redirect_unwrap(activity)
if type == 'Follow':
return accept_follow(activity, activity_unwrapped)
# send webmentions to each target
as1 = as2.to_as1(activity)
source_as2 = json_dumps(activity_unwrapped)
sent = common.send_webmentions(as1, proxy=True, protocol='activitypub',
source_as2=source_as2)
if not sent and type in ('Create', 'Announce'):
# check that this activity is public. only do this check for Creates,
# not Like, Follow, or other activity types, since Mastodon doesn't
# currently mark those as explicitly public.
if not as2.is_public(activity_unwrapped):
logging.info('Dropping non-public activity')
return ''
# normal post, deliver to BF followers
source = activity.get('url') or activity.get('id')
domains = []
if actor:
actor_id = actor.get('id')
if actor_id:
logging.info(f'Finding followers of {actor_id}')
domains = [f.src for f in
Follower.query(Follower.dest == actor_id,
projection=[Follower.src]).fetch()]
key = Activity(source=source, target='Public', direction='in',
protocol='activitypub', domain=domains, status='complete',
source_as2=source_as2).put()
logging.info(f'Wrote Activity {key} with {len(domains)} follower domains')
return ''
def accept_follow(follow, follow_unwrapped):
"""Replies to an AP Follow request with an Accept request.
Args:
follow: dict, AP Follow activity
follow_unwrapped: dict, same, except with redirect URLs unwrapped
"""
logger.info('Replying to Follow with Accept')
followee = follow.get('object')
followee_unwrapped = follow_unwrapped.get('object')
follower = follow.get('actor')
if not followee or not followee_unwrapped or not follower:
error('Follow activity requires object and actor. Got: %s' % follow)
inbox = follower.get('inbox')
follower_id = follower.get('id')
if not inbox or not follower_id:
error('Follow actor requires id and inbox. Got: %s', follower)
# store Follower
user_domain = util.domain_from_link(followee_unwrapped, minimize=False)
Follower.get_or_create(dest=user_domain, src=follower_id,
last_follow=json_dumps(follow))
# send AP Accept
accept = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': util.tag_uri(request.host, 'accept/%s/%s' % (
(user_domain, follow.get('id')))),
'type': 'Accept',
'actor': followee,
'object': {
'type': 'Follow',
'actor': follower_id,
'object': followee,
}
}
resp = send(accept, inbox, user_domain)
# send webmention
common.send_webmentions(as2.to_as1(follow), proxy=True, protocol='activitypub',
source_as2=json_dumps(follow_unwrapped))
return resp.text, resp.status_code
@ndb.transactional()
def undo_follow(undo_unwrapped):
"""Handles an AP Undo Follow request by deactivating the Follower entity.
Args:
undo_unwrapped: dict, AP Undo activity with redirect URLs unwrapped
"""
logger.info('Undoing Follow')
follow = undo_unwrapped.get('object', {})
follower = follow.get('actor')
followee = follow.get('object')
if not follower or not followee:
error('Undo of Follow requires object with actor and object. Got: %s' % follow)
# deactivate Follower
user_domain = util.domain_from_link(followee, minimize=False)
follower_obj = Follower.get_by_id(Follower._id(dest=user_domain, src=follower))
if follower_obj:
logger.info(f'Marking {follower_obj.key} as inactive')
follower_obj.status = 'inactive'
follower_obj.put()
else:
logger.warning(f'No Follower found for {user_domain} {follower}')
# TODO send webmention with 410 of u-follow
# TODO: unify with following_collection
@app.get(f'/<regex("{common.DOMAIN_RE}"):domain>/followers')
@flask_util.cached(cache, CACHE_TIME)
def followers_collection(domain):
"""ActivityPub Followers collection.
https://www.w3.org/TR/activitypub/#followers
https://www.w3.org/TR/activitypub/#collections
https://www.w3.org/TR/activitystreams-core/#paging
"""
if not User.get_by_id(domain):
return f'User {domain} not found', 404
logger.info(f"Counting {domain}'s followers")
count = Follower.query(
Follower.status == 'active',
Follower.dest == domain,
).count()
ret = {
'@context': 'https://www.w3.org/ns/activitystreams',
'summary': f"{domain}'s followers",
'type': 'Collection',
'totalItems': count,
'items': [], # TODO
}
logger.info(f'Returning {json_dumps(ret, indent=2)}')
return ret
@app.get(f'/<regex("{common.DOMAIN_RE}"):domain>/following')
@flask_util.cached(cache, CACHE_TIME)
def following_collection(domain):
"""ActivityPub Following collection.
https://www.w3.org/TR/activitypub/#following
https://www.w3.org/TR/activitypub/#collections
https://www.w3.org/TR/activitystreams-core/#paging
"""
if not User.get_by_id(domain):
return f'User {domain} not found', 404
logger.info(f"Counting {domain}'s following")
count = Follower.query(
Follower.status == 'active',
Follower.src == domain,
).count()
ret = {
'@context': 'https://www.w3.org/ns/activitystreams',
'summary': f"{domain}'s following",
'type': 'Collection',
'totalItems': count,
'items': [], # TODO
}
logger.info(f'Returning {json_dumps(ret, indent=2)}')
return ret