From 786173d270ec97318c74c658f82cc92ab49dff79 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Mon, 23 Oct 2017 21:49:43 -0700 Subject: [PATCH] activitypub: point actor ids to bridgy fed, not source web site ...since we want to serve them as AS2. for tootsuite/mastodon#5500 --- appengine_config.py | 5 +++++ common.py | 45 +++++++++++++++++++++++++++++----------- test/test_activitypub.py | 4 ++-- test/test_webmention.py | 3 +++ 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/appengine_config.py b/appengine_config.py index 430cf460..6d686139 100644 --- a/appengine_config.py +++ b/appengine_config.py @@ -13,6 +13,11 @@ except ValueError as e: from granary.appengine_config import * + +if os.environ.get('SERVER_SOFTWARE', '').startswith('Google App Engine/'): + HOST = 'fed.brid.gy' + HOST_URL = '%s://%s' % (SCHEME, HOST) + # Make requests and urllib3 play nice with App Engine. # https://github.com/snarfed/bridgy/issues/396 # http://stackoverflow.com/questions/34574740 diff --git a/common.py b/common.py index f827c49d..7c6aba0c 100644 --- a/common.py +++ b/common.py @@ -10,11 +10,12 @@ import urlparse from bs4 import BeautifulSoup from granary import as2 -from oauth_dropins.webutil import util +from oauth_dropins.webutil import handlers, util import requests from webmentiontools import send from webob import exc +import appengine_config from models import Response DOMAIN_RE = r'([^/]+\.[^/]+)' @@ -56,6 +57,8 @@ SUPPORTED_VERBS = ( 'update', ) +canonicalize_domain = handlers.redirect('bridgy-federated.appspot.com', 'fed.brid.gy') + def requests_get(url, **kwargs): return _requests_fn(util.requests_get, url, **kwargs) @@ -218,18 +221,22 @@ def postprocess_as2(activity, target=None, key=None): key: MagicKey, optional. populated into publicKey field if provided. """ type = activity.get('type') - if type == 'Person' and not activity.get('publicKey'): - # underspecified, inferred from this issue and Mastodon's implementation: - # https://github.com/w3c/activitypub/issues/203#issuecomment-297553229 - # https://github.com/tootsuite/mastodon/blob/bc2c263504e584e154384ecc2d804aeb1afb1ba3/app/services/activitypub/process_account_service.rb#L77 - activity['publicKey'] = { - 'publicKeyPem': key.public_pem(), - } + + # actor objects if type == 'Person': - activity.setdefault('preferredUsername', USERNAME) - attr = activity.get('attributedTo') - if attr: - attr[0].setdefault('preferredUsername', USERNAME) + postprocess_as2_actor(activity) + if not activity.get('publicKey'): + # underspecified, inferred from this issue and Mastodon's implementation: + # https://github.com/w3c/activitypub/issues/203#issuecomment-297553229 + # https://github.com/tootsuite/mastodon/blob/bc2c263504e584e154384ecc2d804aeb1afb1ba3/app/services/activitypub/process_account_service.rb#L77 + activity['publicKey'] = { + 'publicKeyPem': key.public_pem(), + } + return activity + + for actor in (util.get_list(activity, 'attributedTo') + + util.get_list(activity, 'actor')): + postprocess_as2_actor(actor) # inReplyTo: singly valued, prefer id over url target_id = target.get('id') if target else None @@ -269,3 +276,17 @@ def postprocess_as2(activity, target=None, key=None): } return util.trim_nulls(activity) + + +def postprocess_as2_actor(actor): + """Prepare an AS2 actor object to be served or sent via ActivityPub. + + Args: + actor: dict, AS2 actor object + """ + actor.setdefault('preferredUsername', USERNAME) + + url = actor.get('url') + if url: + actor['id'] = '%s/%s' % (appengine_config.HOST_URL, + urlparse.urlparse(url).netloc) diff --git a/test/test_activitypub.py b/test/test_activitypub.py index f17680cc..ec642800 100644 --- a/test/test_activitypub.py +++ b/test/test_activitypub.py @@ -27,7 +27,7 @@ class ActivityPubTest(testutil.TestCase): def test_actor_handler(self, mock_get, _): mock_get.return_value = requests_response(""" -Mrs. ☕ Foo +Mrs. ☕ Foo """, url='https://foo.com/') @@ -41,7 +41,7 @@ class ActivityPubTest(testutil.TestCase): 'type' : 'Person', 'name': 'Mrs. ☕ Foo', 'preferredUsername': 'me', - 'id': 'https://foo.com/about-me', + 'id': 'http://localhost/foo.com', 'url': 'https://foo.com/about-me', 'inbox': 'http://localhost/foo.com/inbox', 'publicKey': { diff --git a/test/test_webmention.py b/test/test_webmention.py index 7e690058..21f5781f 100644 --- a/test/test_webmention.py +++ b/test/test_webmention.py @@ -117,8 +117,10 @@ class WebmentionTest(testutil.TestCase): 'cc': [AS2_PUBLIC_AUDIENCE, 'tag:orig,2017:as2'], 'actor': { 'type': 'Person', + 'id': 'http://localhost/orig', 'url': 'http://orig', 'name': 'Ms. ☕ Baz', + 'preferredUsername': 'me', }, } @@ -161,6 +163,7 @@ class WebmentionTest(testutil.TestCase): ], 'attributedTo': [{ 'type': 'Person', + 'id': 'http://localhost/orig', 'url': 'http://orig', 'preferredUsername': 'me', 'name': 'Ms. ☕ Baz',