From c47e07ab943b7c71b22d9ba07b143bc8dc0ba94d Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 4 Oct 2017 07:12:03 -0700 Subject: [PATCH] webfinger: check domain home page too; preserve port in MagicKey domain --- webfinger.py | 26 ++++++++++++++++---------- webmention.py | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/webfinger.py b/webfinger.py index addc96b..5120160 100644 --- a/webfinger.py +++ b/webfinger.py @@ -36,23 +36,29 @@ class UserHandler(handlers.XrdOrJrdHandler): return 'templates/webfinger_user' def template_vars(self, domain, url=None): - if not url: - url = 'http://%s/' % domain + assert domain - # TODO: unify with activitypub - resp = common.requests_get(url) - parsed = BeautifulSoup(resp.content, from_encoding=resp.encoding) - mf2 = mf2py.parse(parsed, url=resp.url) - # logging.debug('Parsed mf2 for %s: %s', resp.url, json.dumps(mf2, indent=2)) + # find representative h-card. try url, then url's home page, then domain + urls = ['http://%s/' % domain] + if url: + urls = [url, urlparse.urljoin(url, '/')] + urls - hcard = mf2util.representative_hcard(mf2, resp.url) - logging.info('Representative h-card: %s', json.dumps(hcard, indent=2)) - if not hcard: + for candidate in urls: + resp = common.requests_get(candidate) + parsed = BeautifulSoup(resp.content, from_encoding=resp.encoding) + mf2 = mf2py.parse(parsed, url=resp.url) + # logging.debug('Parsed mf2 for %s: %s', resp.url, json.dumps(mf2, indent=2)) + hcard = mf2util.representative_hcard(mf2, resp.url) + if hcard: + logging.info('Representative h-card: %s', json.dumps(hcard, indent=2)) + break + else: common.error(self, """\ Couldn't find a \ representative h-card on %s""" % resp.url) acct = '%s@%s' % (common.USERNAME, domain) + logging.info('Generating WebFinger data for %s', acct) key = models.MagicKey.get_or_create(domain) props = hcard.get('properties', {}) urls = util.dedupe_urls(props.get('url', []) + [resp.url]) diff --git a/webmention.py b/webmention.py index 8881e3c..c3702d9 100644 --- a/webmention.py +++ b/webmention.py @@ -178,7 +178,7 @@ class WebmentionHandler(webapp2.RequestHandler): logging.info('Converted %s to Atom:\n%s', source_url, entry) # sign reply and wrap in magic envelope - domain = urlparse.urlparse(source_url).netloc.split(':')[0] + domain = urlparse.urlparse(source_url).netloc key = models.MagicKey.get_or_create(domain) logging.info('Using key for %s: %s', domain, key) magic_envelope = magicsigs.magic_envelope(