Exemplo n.º 1
0
def parse_diaspora_webfinger(document: str) -> Dict:
    """
    Parse Diaspora webfinger which is either in JSON format (new) or XRD (old).

    https://diaspora.github.io/diaspora_federation/discovery/webfinger.html
    """
    webfinger = {
        "hcard_url": None,
    }
    # noinspection PyBroadException
    try:
        doc = json.loads(document)
        for link in doc["links"]:
            if link["rel"] == "http://microformats.org/profile/hcard":
                webfinger["hcard_url"] = link["href"]
                break
        else:
            logger.warning("parse_diaspora_webfinger: found JSON webfinger but it has no hcard href")
            raise ValueError
    except Exception:
        try:
            xrd = XRD.parse_xrd(document)
            webfinger["hcard_url"] = xrd.find_link(rels="http://microformats.org/profile/hcard").href
        except (xml.parsers.expat.ExpatError, TypeError):
            logger.warning("parse_diaspora_webfinger: found XML webfinger but it fails to parse")
            pass
    return webfinger
Exemplo n.º 2
0
 def setUp(self):
     self.xrd = XRD.parse_xrd("""<?xml version="1.0" ?>
         <XRD xml:id="1234" xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
         	<Property type="mimetype">text/plain</Property>
         	<Property type="none"></Property>
         	<Link template="http://google.com/{uri}">
         		<Title xml:lang="en_us">this is my rel</Title>
         	</Link>
         </XRD>
         """)
Exemplo n.º 3
0
def retrieve_diaspora_host_meta(host):
    """
    Retrieve a remote Diaspora host-meta document.

    :arg host: Host to retrieve from
    :returns: ``XRD`` instance
    """
    document, code, exception = fetch_document(host=host, path="/.well-known/host-meta")
    if exception:
        return None
    xrd = XRD.parse_xrd(document)
    return xrd
Exemplo n.º 4
0
def retrieve_diaspora_host_meta(host):
    """
    Retrieve a remote Diaspora host-meta document.

    :arg host: Host to retrieve from
    :returns: ``XRD`` instance
    """
    document, code, exception = fetch_document(host=host, path="/.well-known/host-meta")
    if exception:
        return None
    xrd = XRD.parse_xrd(document)
    return xrd
Exemplo n.º 5
0
def retrieve_diaspora_host_meta(host):
    """
    Retrieve a remote Diaspora host-meta document.

    Args:
        host (str) - Host to retrieve from

    Returns:
        XRD
    """
    document, code, exception = fetch_document(host=host, path="/.well-known/host-meta")
    if exception:
        return None
    xrd = XRD.parse_xrd(document)
    return xrd
Exemplo n.º 6
0
def retrieve_diaspora_webfinger(handle):
    """
    Retrieve a remote Diaspora webfinger document.

    :arg handle: Remote handle to retrieve
    :returns: ``XRD`` instance
    """
    hostmeta = retrieve_diaspora_host_meta(handle.split("@")[1])
    if not hostmeta:
        return None
    url = hostmeta.find_link(rels="lrdd").template.replace("{uri}", quote(handle))
    document, code, exception = fetch_document(url)
    if exception:
        return None
    xrd = XRD.parse_xrd(document)
    return xrd
Exemplo n.º 7
0
def retrieve_diaspora_webfinger(handle):
    """
    Retrieve a remote Diaspora webfinger document.

    :arg handle: Remote handle to retrieve
    :returns: ``XRD`` instance
    """
    hostmeta = retrieve_diaspora_host_meta(handle.split("@")[1])
    if not hostmeta:
        return None
    url = hostmeta.find_link(rels="lrdd").template.replace("{uri}", quote(handle))
    document, code, exception = fetch_document(url)
    if exception:
        return None
    try:
        xrd = XRD.parse_xrd(document)
    except xml.parsers.expat.ExpatError:
        return None
    return xrd