Implement webfinger support

merge-requests/130/head
Jason Robinson 2015-07-12 17:03:31 +03:00
rodzic f359ac8247
commit 414da9d901
2 zmienionych plików z 118 dodań i 2 usunięć

Wyświetl plik

@ -1,4 +1,5 @@
from xrd import XRD, Link
from base64 import b64encode
from xrd import XRD, Link, Element
def generate_host_meta(template=None, *args, **kwargs):
@ -18,6 +19,23 @@ def generate_host_meta(template=None, *args, **kwargs):
return hostmeta.render()
def generate_legacy_webfinger(template=None, *args, **kwargs):
"""Generate a legacy webfinger XRD document.
Args:
template (str, optional) - Ready template to fill with args, for example "diaspora".
**kwargs - Template specific key-value pairs to fill in, see classes.
Returns:
str - XRD document
"""
if template == "diaspora":
hostmeta = DiasporaWebFinger(*args, **kwargs)
else:
hostmeta = BaseLegacyWebFinger(*args, **kwargs)
return hostmeta.render()
class BaseHostMeta(object):
def __init__(self, *args, **kwargs):
self.xrd = XRD()
@ -40,3 +58,70 @@ class DiasporaHostMeta(BaseHostMeta):
template='%s/webfinger?q={uri}' % kwargs["webfinger_host"]
)
self.xrd.links.append(link)
class BaseLegacyWebFinger(BaseHostMeta):
"""Legacy XRD WebFinger.
See: https://code.google.com/p/webfinger/wiki/WebFingerProtocol
"""
def __init__(self, address, *args, **kwargs):
super(BaseLegacyWebFinger, self).__init__(*args, **kwargs)
subject = Element("Subject", "acct:%s" % address)
self.xrd.elements.append(subject)
class DiasporaWebFinger(BaseLegacyWebFinger):
"""Diaspora version of legacy WebFinger.
Requires keyword args:
handle (str) - eg user@domain.tld
host (str) - eg https://domain.tld
guid (str) - guid of user
public_key (str) - public key
"""
def __init__(self, handle, host, guid, public_key, *args, **kwargs):
super(DiasporaWebFinger, self).__init__(handle, *args, **kwargs)
self.xrd.elements.append(Element("Alias", "%s/people/%s" % (
host, guid
)))
username = handle.split("@")[0]
self.xrd.links.append(Link(
rel="http://microformats.org/profile/hcard",
type_="text/html",
href="%s/hcard/users/%s" %(
host, username
)
))
self.xrd.links.append(Link(
rel="http://joindiaspora.com/seed_location",
type_="text/html",
href=host
))
self.xrd.links.append(Link(
rel="http://joindiaspora.com/guid",
type_="text/html",
href=guid
))
self.xrd.links.append(Link(
rel="http://webfinger.net/rel/profile-page",
type_="text/html",
href="%s/u/%s" % (
host, username
)
))
self.xrd.links.append(Link(
rel="http://schemas.google.com/g/2010#updates-from",
type_="application/atom+xml",
href="%s/public/%s.atom" % (
host, username
)
))
# Base64 the key
# See https://wiki.diasporafoundation.org/Federation_Protocol_Overview#Diaspora_Public_Key
base64_key = b64encode(bytes(public_key, encoding="UTF-8")).decode("ascii")
self.xrd.links.append(Link(
rel="diaspora-public-key",
type_="RSA",
href=base64_key
))

Wyświetl plik

@ -1,5 +1,6 @@
import pytest
from federation.hostmeta.generators import generate_host_meta
from federation.hostmeta.generators import generate_host_meta, generate_legacy_webfinger
DIASPORA_HOSTMETA = """<?xml version="1.0" encoding="UTF-8"?>
@ -8,6 +9,19 @@ DIASPORA_HOSTMETA = """<?xml version="1.0" encoding="UTF-8"?>
</XRD>
"""
DIASPORA_WEBFINGER = """<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
<Subject>acct:user@server.example</Subject>
<Alias>https://server.example/people/0123456789abcdef</Alias>
<Link href="https://server.example/hcard/users/user" rel="http://microformats.org/profile/hcard" type="text/html"/>
<Link href="https://server.example" rel="http://joindiaspora.com/seed_location" type="text/html"/>
<Link href="0123456789abcdef" rel="http://joindiaspora.com/guid" type="text/html"/>
<Link href="https://server.example/u/user" rel="http://webfinger.net/rel/profile-page" type="text/html"/>
<Link href="https://server.example/public/user.atom" rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml"/>
<Link href="QUJDREVGPT0=" rel="diaspora-public-key" type="RSA"/>
</XRD>
"""
class TestDiasporaHostMetaGenerator(object):
@ -18,3 +32,20 @@ class TestDiasporaHostMetaGenerator(object):
def test_generate_host_meta_requires_webfinger_host(self):
with pytest.raises(KeyError):
generate_host_meta("diaspora")
class TestDiasporaWebFingerGenerator(object):
def test_generate_valid_webfinger(self):
webfinger = generate_legacy_webfinger(
"diaspora",
handle="user@server.example",
host="https://server.example",
guid="0123456789abcdef",
public_key="ABCDEF=="
)
assert webfinger.decode("UTF-8") == DIASPORA_WEBFINGER
def test_diaspora_webfinger_raises_on_missing_arguments(self):
with pytest.raises(TypeError):
generate_legacy_webfinger("diaspora")