kopia lustrzana https://github.com/snarfed/bridgy-fed
webfinger: settle on 'me' username, just domain in paths
rodzic
5bb62f4021
commit
86c6934a98
|
@ -1,5 +1,7 @@
|
|||
# coding=utf-8
|
||||
"""Misc common utilities.
|
||||
"""
|
||||
from __future__ import unicode_literals
|
||||
import logging
|
||||
|
||||
from oauth_dropins.webutil import util
|
||||
|
@ -14,6 +16,9 @@ HEADERS = {
|
|||
ATOM_CONTENT_TYPE = 'application/atom+xml'
|
||||
MAGIC_ENVELOPE_CONTENT_TYPE = 'application/magic-envelope+xml'
|
||||
XML_UTF8 = "<?xml version='1.0' encoding='UTF-8'?>\n"
|
||||
USERNAME = 'me'
|
||||
# USERNAME_EMOJI = '🌎' # globe
|
||||
|
||||
|
||||
def requests_get(url, **kwargs):
|
||||
return _requests_fn(util.requests_get, url, **kwargs)
|
||||
|
|
|
@ -5,6 +5,7 @@ TODO: test error handling
|
|||
"""
|
||||
from __future__ import unicode_literals
|
||||
import json
|
||||
import urllib
|
||||
|
||||
import mock
|
||||
from oauth_dropins.webutil import util
|
||||
|
@ -16,6 +17,8 @@ import models
|
|||
import testutil
|
||||
from webfinger import app
|
||||
|
||||
USER = '%s@foo.com' % common.USERNAME
|
||||
|
||||
|
||||
class WebFingerTest(testutil.TestCase):
|
||||
|
||||
|
@ -29,9 +32,9 @@ class WebFingerTest(testutil.TestCase):
|
|||
</a>
|
||||
</body>
|
||||
"""
|
||||
self.key = models.MagicKey.get_or_create('me@foo.com')
|
||||
self.key = models.MagicKey.get_or_create('foo.com')
|
||||
self.expected_webfinger = {
|
||||
'subject': 'acct:me@foo.com',
|
||||
'subject': 'acct:' + USER,
|
||||
'aliases': [
|
||||
'https://foo.com/about-me',
|
||||
'https://foo.com/',
|
||||
|
@ -61,7 +64,7 @@ class WebFingerTest(testutil.TestCase):
|
|||
'href': self.key.href(),
|
||||
}, {
|
||||
'rel': 'salmon',
|
||||
'href': 'http://localhost/me@foo.com/salmon'
|
||||
'href': 'http://localhost/foo.com/salmon'
|
||||
# TODO
|
||||
# }, {
|
||||
# 'rel': 'self',
|
||||
|
@ -98,7 +101,7 @@ class WebFingerTest(testutil.TestCase):
|
|||
def test_user_handler(self, mock_get):
|
||||
mock_get.return_value = requests_response(self.html, url = 'https://foo.com/')
|
||||
|
||||
got = app.get_response('/me@foo.com', headers={'Accept': 'application/json'})
|
||||
got = app.get_response('/foo.com', headers={'Accept': 'application/json'})
|
||||
self.assertEquals(200, got.status_int)
|
||||
self.assertEquals('application/json; charset=utf-8',
|
||||
got.headers['Content-Type'])
|
||||
|
@ -109,7 +112,7 @@ class WebFingerTest(testutil.TestCase):
|
|||
|
||||
# check that magic key is persistent
|
||||
again = json.loads(app.get_response(
|
||||
'/me@foo.com', headers={'Accept': 'application/json'}).body)
|
||||
'/foo.com', headers={'Accept': 'application/json'}).body)
|
||||
self.assertEquals(self.key.href(), again['magic_keys'][0]['value'])
|
||||
|
||||
links = {l['rel']: l['href'] for l in again['links']}
|
||||
|
@ -124,7 +127,7 @@ class WebFingerTest(testutil.TestCase):
|
|||
</div>
|
||||
</body>
|
||||
""")
|
||||
got = app.get_response('/me@foo.com')
|
||||
got = app.get_response('/foo.com')
|
||||
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
||||
timeout=util.HTTP_TIMEOUT)
|
||||
self.assertEquals(400, got.status_int)
|
||||
|
@ -134,10 +137,14 @@ class WebFingerTest(testutil.TestCase):
|
|||
|
||||
@mock.patch('requests.get')
|
||||
def test_webfinger_handler(self, mock_get):
|
||||
mock_get.return_value = requests_response(self.html, url = 'https://foo.com/')
|
||||
got = app.get_response('/.well-known/webfinger?resource=me@foo.com',
|
||||
headers={'Accept': 'application/json'})
|
||||
self.assertEquals(200, got.status_int)
|
||||
self.assertEquals('application/json; charset=utf-8',
|
||||
got.headers['Content-Type'])
|
||||
self.assertEquals(self.expected_webfinger, json.loads(got.body))
|
||||
mock_get.return_value = requests_response(self.html, url='https://foo.com/')
|
||||
|
||||
for resource in ('me@foo.com', 'acct:me@foo.com', 'xyz@foo.com',
|
||||
'foo.com', 'http://foo.com/', 'https://foo.com/'):
|
||||
url = '/.well-known/webfinger?%s' % urllib.urlencode(
|
||||
{'resource': resource})
|
||||
got = app.get_response(url, headers={'Accept': 'application/json'})
|
||||
self.assertEquals(200, got.status_int, got.body)
|
||||
self.assertEquals('application/json; charset=utf-8',
|
||||
got.headers['Content-Type'])
|
||||
self.assertEquals(self.expected_webfinger, json.loads(got.body))
|
||||
|
|
30
webfinger.py
30
webfinger.py
|
@ -33,7 +33,7 @@ class UserHandler(handlers.XrdOrJrdHandler):
|
|||
def template_prefix(self):
|
||||
return 'templates/webfinger_user'
|
||||
|
||||
def template_vars(self, username, domain, url=None):
|
||||
def template_vars(self, domain, url=None):
|
||||
if not url:
|
||||
url = 'http://%s/' % domain
|
||||
|
||||
|
@ -49,8 +49,8 @@ class UserHandler(handlers.XrdOrJrdHandler):
|
|||
Couldn't find a <a href="http://microformats.org/wiki/representative-hcard-parsing">\
|
||||
representative h-card</a> on %s""" % resp.url)
|
||||
|
||||
acct = '%s@%s' % (username, domain)
|
||||
key = models.MagicKey.get_or_create(acct)
|
||||
acct = '%s@%s' % (common.USERNAME, domain)
|
||||
key = models.MagicKey.get_or_create(domain)
|
||||
props = hcard.get('properties', {})
|
||||
urls = util.dedupe_urls(props.get('url', []) + [resp.url])
|
||||
canonical_url = urls[0]
|
||||
|
@ -81,7 +81,7 @@ representative h-card</a> on %s""" % resp.url)
|
|||
'href': key.href(),
|
||||
}, {
|
||||
'rel': 'salmon',
|
||||
'href': '%s/%s/salmon' % (self.request.host_url, acct),
|
||||
'href': '%s/%s/salmon' % (self.request.host_url, domain),
|
||||
}]
|
||||
})
|
||||
logging.info('Returning WebFinger data: %s', json.dumps(data, indent=2))
|
||||
|
@ -94,24 +94,20 @@ class WebfingerHandler(UserHandler):
|
|||
return True
|
||||
|
||||
def template_vars(self):
|
||||
acct = util.get_required_param(self, 'resource')
|
||||
resource = util.get_required_param(self, 'resource')
|
||||
try:
|
||||
username, domain = util.parse_acct_uri(acct)
|
||||
url = 'http://%s/' % domain
|
||||
_, domain = util.parse_acct_uri(resource)
|
||||
except ValueError:
|
||||
# common.error(self, 'Invalid acct: URI %s' % acct)
|
||||
username = 'ryan'
|
||||
domain = 'localhost'
|
||||
url = 'http://localhost/'
|
||||
# domain = 'snarfed.org'
|
||||
# url = 'https://snarfed.org/'
|
||||
if not username:
|
||||
common.error(self, 'No username found in acct: URI %s' % acct)
|
||||
domain = urlparse.urlparse(resource).netloc or resource
|
||||
|
||||
return super(WebfingerHandler, self).template_vars(username, domain, url=url)
|
||||
url = None
|
||||
if resource.startswith('http://') or resource.startswith('https://'):
|
||||
url = resource
|
||||
|
||||
return super(WebfingerHandler, self).template_vars(domain, url=url)
|
||||
|
||||
|
||||
app = webapp2.WSGIApplication([
|
||||
(r'/%s/?' % common.ACCT_RE, UserHandler),
|
||||
(r'/%s/?' % common.DOMAIN_RE, UserHandler),
|
||||
('/.well-known/webfinger', WebfingerHandler),
|
||||
] + handlers.HOST_META_ROUTES, debug=appengine_config.DEBUG)
|
||||
|
|
Ładowanie…
Reference in New Issue