kopia lustrzana https://gitlab.com/jaywink/federation
Implement XRD host-meta generator
rodzic
67ebac03ed
commit
f61c2b576f
|
@ -0,0 +1,46 @@
|
|||
from xrd import XRD, Link
|
||||
|
||||
|
||||
def generate_host_meta(template=None, *args, **kwargs):
|
||||
"""Generate a host-meta 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 = DiasporaHostMeta(*args, **kwargs)
|
||||
else:
|
||||
hostmeta = BaseHostMeta(*args, **kwargs)
|
||||
return hostmeta.render()
|
||||
|
||||
|
||||
class BaseHostMeta(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.xrd = XRD()
|
||||
|
||||
def render(self):
|
||||
return self.xrd.to_xml()
|
||||
|
||||
|
||||
class DiasporaHostMeta(BaseHostMeta):
|
||||
"""Diaspora host-meta.
|
||||
|
||||
NOTE! Diaspora .well-known/host-meta seems to define 'encoding="UTF-8"' from server implementation.
|
||||
The xrd.XRD module does not define this or allow defining it when rendering the XML. This will need to be
|
||||
fixed if that is a problem to Diaspora or other servers.
|
||||
|
||||
Requires keyword args:
|
||||
webfinger_host (str)
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DiasporaHostMeta, self).__init__(*args, **kwargs)
|
||||
link = Link(
|
||||
rel='lrdd',
|
||||
type_='application/xrd+xml',
|
||||
template='%s/webfinger?q={uri}' % kwargs["webfinger_host"]
|
||||
)
|
||||
self.xrd.links.append(link)
|
|
@ -0,0 +1,20 @@
|
|||
import pytest
|
||||
from federation.hostmeta.generators import generate_host_meta
|
||||
|
||||
|
||||
DIASPORA_HOSTMETA = """<?xml version="1.0" ?>
|
||||
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
|
||||
<Link rel="lrdd" template="https://example.com/webfinger?q={uri}" type="application/xrd+xml"/>
|
||||
</XRD>
|
||||
"""
|
||||
|
||||
|
||||
class TestDiasporaHostMetaGenerator(object):
|
||||
|
||||
def test_generate_valid_host_meta(self):
|
||||
hostmeta = generate_host_meta("diaspora", webfinger_host="https://example.com")
|
||||
assert hostmeta.toprettyxml(indent=" ") == DIASPORA_HOSTMETA
|
||||
|
||||
def test_generate_host_meta_requires_webfinger_host(self):
|
||||
with pytest.raises(KeyError):
|
||||
generate_host_meta("diaspora")
|
1
setup.py
1
setup.py
|
@ -16,6 +16,7 @@ setup(
|
|||
"lxml==3.4.4",
|
||||
"pycrypto==2.6.1",
|
||||
"python-dateutil==2.4.2",
|
||||
"python-xrd==0.1",
|
||||
],
|
||||
test_require=[
|
||||
"pytest==2.7.2",
|
||||
|
|
Ładowanie…
Reference in New Issue