コード例 #1
0
ファイル: entities.py プロジェクト: AdJadeHash/federation
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.inboxes = {
         "private": get_private_endpoint(self.handle, self.guid),
         "public": get_public_endpoint(self.handle),
     }
コード例 #2
0
 def test_correct_endpoint(self):
     endpoint = get_private_endpoint("*****@*****.**", guid="123456")
     assert endpoint == "https://example.com/receive/users/123456"
コード例 #3
0
 def test_raises_value_error_for_non_profile_id(self):
     with pytest.raises(ValueError):
         get_private_endpoint(
             "diaspora://[email protected]/comment/123456")
コード例 #4
0
ファイル: outbound.py プロジェクト: teotikalki/federation
def handle_send(entity, author_user, recipients=None, parent_user=None):
    """Send an entity to remote servers.

    Using this we will build a list of payloads per protocol, after resolving any that need to be guessed or
    looked up over the network. After that, each recipient will get the generated protocol payload delivered.

    Any given user arguments must have ``private_key`` and ``handle`` attributes.

    :arg entity: Entity object to send. Can be a base entity or a protocol specific one.
    :arg author_user: User authoring the object.
    :arg recipients: A list of recipients to delivery to. Each recipient is a tuple
                     containing at minimum the "id", optionally "public key" for private deliveries.
                     Instead of a tuple, for public deliveries the "id" as str is also ok.
                     If public key is provided, Diaspora protocol delivery will be made as an encrypted
                     private delivery.
                     For example
                     [
                         ("diaspora://[email protected]/profile/zyx", <RSAPublicKey object>),
                         ("diaspora://[email protected]/profile/xyz", None),
                         "diaspora://[email protected]/profile/xyz",
                     ]
    :arg parent_user: (Optional) User object of the parent object, if there is one. This must be given for the
                      Diaspora protocol if a parent object exists, so that a proper ``parent_author_signature`` can
                      be generated. If given, the payload will be sent as this user.
    """
    payloads = []
    public_payloads = {
        "diaspora": {
            "payload": None,
            "urls": set(),
        },
    }

    # Generate payloads and collect urls
    for recipient in recipients:
        id = recipient[0] if isinstance(recipient, tuple) else recipient
        public_key = recipient[1] if isinstance(
            recipient, tuple) and len(recipient) > 1 else None
        if public_key:
            # Private payload
            try:
                payload = handle_create_payload(entity,
                                                author_user,
                                                to_user_key=public_key,
                                                parent_user=parent_user)
                payload = json.dumps(payload)
            except Exception as ex:
                logger.error(
                    "handle_send - failed to generate private payload for %s: %s",
                    id, ex)
                continue
            # TODO get_private_endpoint should be imported per protocol
            url = get_private_endpoint(id)
            payloads.append({
                "urls": {url},
                "payload": payload,
                "content_type": "application/json",
            })
        else:
            if not public_payloads["diaspora"]["payload"]:
                public_payloads["diaspora"]["payload"] = handle_create_payload(
                    entity,
                    author_user,
                    parent_user=parent_user,
                )
            # TODO get_public_endpoint should be imported per protocol
            url = get_public_endpoint(id)
            public_payloads["diaspora"]["urls"].add(url)

    # Add public payload
    if public_payloads["diaspora"]["payload"]:
        payloads.append({
            "urls": public_payloads["diaspora"]["urls"],
            "payload": public_payloads["diaspora"]["payload"],
            "content_type": "application/magic-envelope+xml",
        })

    logger.debug("handle_send - %s", payloads)

    # Do actual sending
    for payload in payloads:
        for url in payload["urls"]:
            try:
                send_document(
                    url,
                    payload["payload"],
                    headers={"Content-Type": payload["content_type"]})
            except Exception as ex:
                logger.error(
                    "handle_send - failed to send payload to %s: %s, payload: %s",
                    url, ex, payload["payload"])
コード例 #5
0
 def test_correct_endpoint(self):
     endpoint = get_private_endpoint(
         "diaspora://[email protected]/profile/123456")
     assert endpoint == "https://example.com/receive/users/123456"