kopia lustrzana https://gitlab.com/jaywink/federation
Add controllers module with a handle_receive method to pass a payload to a protocol.
Suitable protocol will be identified via protocol identify_payload methods.merge-requests/130/head
rodzic
7935417275
commit
001060a37f
|
@ -0,0 +1,23 @@
|
|||
import importlib
|
||||
|
||||
from federation.exceptions import NoSuitableProtocolFoundError
|
||||
|
||||
|
||||
PROTOCOLS = (
|
||||
"diaspora",
|
||||
)
|
||||
|
||||
|
||||
def handle_receive(payload, user=None):
|
||||
"""Takes a payload and passes it to the correct protocol."""
|
||||
protocol = None
|
||||
for protocol_name in PROTOCOLS:
|
||||
protocol = importlib.import_module("federation.protocols.%s.protocol" % protocol_name)
|
||||
if protocol.identify_payload(payload):
|
||||
break
|
||||
|
||||
if protocol:
|
||||
proto_obj = protocol.Protocol()
|
||||
return proto_obj.receive(payload, user)
|
||||
else:
|
||||
raise NoSuitableProtocolFoundError()
|
|
@ -1,5 +1,3 @@
|
|||
|
||||
|
||||
class EncryptedMessageError(Exception):
|
||||
pass
|
||||
|
||||
|
@ -10,3 +8,7 @@ class NoHeaderInMessageError(Exception):
|
|||
|
||||
class NoSenderKeyFoundError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class NoSuitableProtocolFoundError(Exception):
|
||||
pass
|
||||
|
|
|
@ -1,6 +1,18 @@
|
|||
import logging
|
||||
|
||||
|
||||
def identify_payload(payload):
|
||||
"""Each protocol module should define an 'identify_payload' method.
|
||||
|
||||
Args:
|
||||
payload (str) - Payload blob
|
||||
|
||||
Returns:
|
||||
True or False - A boolean whether the payload matches this protocol.
|
||||
"""
|
||||
raise NotImplementedError("Implement in protocol module")
|
||||
|
||||
|
||||
class BaseProtocol(object):
|
||||
|
||||
# Should be implemented by subclasses
|
||||
|
|
|
@ -12,7 +12,15 @@ from federation.exceptions import EncryptedMessageError, NoHeaderInMessageError,
|
|||
from federation.protocols.base import BaseProtocol
|
||||
|
||||
|
||||
class DiasporaProtocol(BaseProtocol):
|
||||
def identify_payload(payload):
|
||||
try:
|
||||
xml = unquote_plus(payload)
|
||||
return xml.find(identify_str = '<diaspora xmlns="%s"' % Protocol.protocol_ns) > -1
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
class Protocol(BaseProtocol):
|
||||
"""Diaspora protocol parts
|
||||
|
||||
Mostly taken from Pyaspora (https://github.com/lukeross/pyaspora).
|
||||
|
@ -22,7 +30,7 @@ class DiasporaProtocol(BaseProtocol):
|
|||
user_agent = 'social-federation/diaspora/0.1'
|
||||
|
||||
def __init__(self, contact_key_fetcher=None, *args, **kwargs):
|
||||
super(DiasporaProtocol, self).__init__()
|
||||
super(Protocol, self).__init__()
|
||||
self.get_contact_key = contact_key_fetcher
|
||||
|
||||
def receive(self, payload, user=None, *args, **kwargs):
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
from base64 import urlsafe_b64decode
|
||||
from lxml import etree
|
||||
import pytest
|
||||
from federation.exceptions import EncryptedMessageError, NoSenderKeyFoundError
|
||||
|
||||
from federation.protocols.diaspora.protocol import DiasporaProtocol
|
||||
from federation.exceptions import EncryptedMessageError, NoSenderKeyFoundError
|
||||
from federation.protocols.diaspora.protocol import Protocol
|
||||
|
||||
|
||||
UNENCRYPTED_DOCUMENT = """<?xml version='1.0'?>
|
||||
|
@ -97,7 +97,7 @@ class TestDiasporaProtocol():
|
|||
assert body == urlsafe_b64decode("{data}".encode("ascii"))
|
||||
|
||||
def init_protocol(self, contact_key_fetcher=None):
|
||||
return DiasporaProtocol(contact_key_fetcher=contact_key_fetcher)
|
||||
return Protocol(contact_key_fetcher=contact_key_fetcher)
|
||||
|
||||
def get_unencrypted_doc(self):
|
||||
return etree.fromstring(UNENCRYPTED_DOCUMENT)
|
||||
|
|
Ładowanie…
Reference in New Issue