2017-08-20 14:28:40 +00:00
|
|
|
"""Handles requests for Salmon endpoints: actors, inbox, etc.
|
2017-08-20 18:35:14 +00:00
|
|
|
|
|
|
|
https://github.com/salmon-protocol/salmon-protocol/blob/master/draft-panzer-salmon-00.html
|
|
|
|
https://github.com/salmon-protocol/salmon-protocol/blob/master/draft-panzer-magicsig-01.html
|
2017-08-20 14:28:40 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2017-10-17 05:02:37 +00:00
|
|
|
from xml.etree.ElementTree import ParseError
|
2017-08-20 14:28:40 +00:00
|
|
|
|
|
|
|
import appengine_config
|
|
|
|
|
2017-08-20 18:33:00 +00:00
|
|
|
from django_salmon import magicsigs, utils
|
2017-10-15 23:44:29 +00:00
|
|
|
from granary import atom
|
|
|
|
from oauth_dropins.webutil import util
|
2017-08-20 14:28:40 +00:00
|
|
|
import webapp2
|
|
|
|
|
|
|
|
import common
|
|
|
|
|
2017-08-20 18:33:00 +00:00
|
|
|
# from django_salmon.feeds
|
|
|
|
ATOM_NS = 'http://www.w3.org/2005/Atom'
|
|
|
|
ATOM_THREADING_NS = 'http://purl.org/syndication/thread/1.0'
|
|
|
|
|
2017-10-17 05:21:13 +00:00
|
|
|
SUPPORTED_VERBS = (
|
|
|
|
'checkin',
|
|
|
|
'create',
|
2017-10-24 04:23:33 +00:00
|
|
|
'favorite',
|
2017-10-17 05:21:13 +00:00
|
|
|
'like',
|
|
|
|
'share',
|
|
|
|
'tag',
|
|
|
|
'update',
|
|
|
|
)
|
|
|
|
|
2017-08-20 14:28:40 +00:00
|
|
|
|
|
|
|
class SlapHandler(webapp2.RequestHandler):
|
2017-09-03 19:54:10 +00:00
|
|
|
"""Accepts POSTs to /[ACCT]/salmon and converts to outbound webmentions."""
|
2017-08-20 14:28:40 +00:00
|
|
|
|
|
|
|
# TODO: unify with activitypub
|
2017-09-03 19:54:10 +00:00
|
|
|
def post(self, username, domain):
|
2017-08-20 14:28:40 +00:00
|
|
|
logging.info('Got: %s', self.request.body)
|
|
|
|
|
2017-10-17 05:02:37 +00:00
|
|
|
try:
|
|
|
|
parsed = utils.parse_magic_envelope(self.request.body)
|
|
|
|
except ParseError as e:
|
2017-10-17 14:46:42 +00:00
|
|
|
common.error(self, 'Could not parse POST body as XML', exc_info=True)
|
2017-08-20 18:33:00 +00:00
|
|
|
data = utils.decode(parsed['data'])
|
|
|
|
logging.info('Decoded: %s', data)
|
|
|
|
|
2017-10-17 05:21:13 +00:00
|
|
|
# check that we support this activity type
|
|
|
|
try:
|
|
|
|
activity = atom.atom_to_activity(data)
|
|
|
|
except ParseError as e:
|
2017-10-17 14:46:42 +00:00
|
|
|
common.error(self, 'Could not parse envelope data as XML', exc_info=True)
|
2017-10-17 05:21:13 +00:00
|
|
|
|
|
|
|
verb = activity.get('verb')
|
|
|
|
if verb and verb not in SUPPORTED_VERBS:
|
2017-10-23 15:03:45 +00:00
|
|
|
common.error(self, 'Sorry, %s activities are not supported yet.' % verb,
|
2017-10-17 05:21:13 +00:00
|
|
|
status=501)
|
|
|
|
|
|
|
|
# verify author and signature
|
|
|
|
author = util.get_url(activity.get('actor'))
|
2017-08-20 18:33:00 +00:00
|
|
|
if ':' not in author:
|
|
|
|
author = 'acct:%s' % author
|
|
|
|
elif not author.startswith('acct:'):
|
2017-08-23 15:14:51 +00:00
|
|
|
common.error(self, 'Author URI %s has unsupported scheme; expected acct:' % author)
|
2017-08-20 18:33:00 +00:00
|
|
|
|
|
|
|
logging.info('Fetching Salmon key for %s' % author)
|
2017-09-03 19:54:10 +00:00
|
|
|
if not magicsigs.verify(author, data, parsed['sig']):
|
2017-08-23 15:14:51 +00:00
|
|
|
common.error(self, 'Could not verify magic signature.')
|
2017-08-20 18:33:00 +00:00
|
|
|
logging.info('Verified magic signature.')
|
|
|
|
|
2017-09-03 19:54:10 +00:00
|
|
|
# Verify that the timestamp is recent. Required by spec.
|
|
|
|
# I get that this helps prevent spam, but in practice it's a bit silly,
|
|
|
|
# and other major implementations don't (e.g. Mastodon), so forget it.
|
|
|
|
#
|
2017-09-02 03:49:00 +00:00
|
|
|
# updated = utils.parse_updated_from_atom(data)
|
|
|
|
# if not utils.verify_timestamp(updated):
|
|
|
|
# common.error(self, 'Timestamp is more than 1h old.')
|
2017-08-20 18:33:00 +00:00
|
|
|
|
2017-10-16 14:13:43 +00:00
|
|
|
# send webmentions to each target
|
|
|
|
activity = atom.atom_to_activity(data)
|
|
|
|
common.send_webmentions(self, activity, protocol='ostatus', source_atom=data)
|
2017-08-20 14:28:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
app = webapp2.WSGIApplication([
|
2017-09-03 19:54:10 +00:00
|
|
|
(r'/%s/salmon' % common.ACCT_RE, SlapHandler),
|
2017-10-17 05:02:37 +00:00
|
|
|
(r'/()%s/salmon' % common.DOMAIN_RE, SlapHandler),
|
2017-08-20 14:28:40 +00:00
|
|
|
], debug=appengine_config.DEBUG)
|