kopia lustrzana https://gitlab.com/jaywink/federation
Identify Matrix AS payloads
rodzic
524e5efbfd
commit
d52c21e8bc
|
@ -1,5 +1,6 @@
|
|||
import importlib
|
||||
from typing import Union, TYPE_CHECKING, Any
|
||||
from types import ModuleType
|
||||
from typing import Union, TYPE_CHECKING
|
||||
|
||||
from federation.exceptions import NoSuitableProtocolFoundError
|
||||
|
||||
|
@ -11,11 +12,12 @@ __version__ = "0.22.0-dev"
|
|||
PROTOCOLS = (
|
||||
"activitypub",
|
||||
"diaspora",
|
||||
"matrix",
|
||||
)
|
||||
|
||||
|
||||
def identify_protocol(method, value):
|
||||
# type: (str, Union[str, RequestType]) -> str
|
||||
# type: (str, Union[str, RequestType]) -> ModuleType
|
||||
"""
|
||||
Loop through protocols, import the protocol module and try to identify the id or request.
|
||||
"""
|
||||
|
@ -27,10 +29,10 @@ def identify_protocol(method, value):
|
|||
raise NoSuitableProtocolFoundError()
|
||||
|
||||
|
||||
def identify_protocol_by_id(id: str):
|
||||
return identify_protocol('id', id)
|
||||
def identify_protocol_by_id(identifier: str) -> ModuleType:
|
||||
return identify_protocol('id', identifier)
|
||||
|
||||
|
||||
def identify_protocol_by_request(request):
|
||||
# type: (RequestType) -> Any
|
||||
# type: (RequestType) -> ModuleType
|
||||
return identify_protocol('request', request)
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import Callable, Tuple, Union, Dict
|
||||
|
||||
# noinspection PyPackageRequirements
|
||||
from Crypto.PublicKey.RSA import RsaKey
|
||||
|
||||
from federation.entities.mixins import BaseEntity
|
||||
from federation.types import UserType, RequestType
|
||||
from federation.utils.text import decode_if_bytes
|
||||
|
||||
logger = logging.getLogger('federation')
|
||||
|
||||
PROTOCOL_NAME = "activitypub"
|
||||
|
||||
|
||||
def identify_id(identifier: str) -> bool:
|
||||
"""
|
||||
Try to identify whether this is a Matrix identifier.
|
||||
|
||||
TODO fix, not entirely correct..
|
||||
"""
|
||||
return re.match(r'^[@#!].*:.*$', identifier, flags=re.IGNORECASE) is not None
|
||||
|
||||
|
||||
def identify_request(request: RequestType) -> bool:
|
||||
"""
|
||||
Try to identify whether this is a Matrix request
|
||||
"""
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
data = json.loads(decode_if_bytes(request.body))
|
||||
if "events" in data:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
class Protocol:
|
||||
actor = None
|
||||
get_contact_key = None
|
||||
payload = None
|
||||
request = None
|
||||
user = None
|
||||
|
||||
def build_send(self, entity: BaseEntity, from_user: UserType, to_user_key: RsaKey = None) -> Union[str, Dict]:
|
||||
"""
|
||||
Build POST data for sending out to the homeserver.
|
||||
|
||||
:param entity: The outbound ready entity for this protocol.
|
||||
:param from_user: The user sending this payload. Must have ``private_key`` and ``id`` properties.
|
||||
:param to_user_key: (Optional) Public key of user we're sending a private payload to.
|
||||
:returns: dict or string depending on if private or public payload.
|
||||
"""
|
||||
# TODO TBD
|
||||
return {}
|
||||
|
||||
def extract_actor(self):
|
||||
# TODO TBD
|
||||
pass
|
||||
|
||||
def receive(
|
||||
self,
|
||||
request: RequestType,
|
||||
user: UserType = None,
|
||||
sender_key_fetcher: Callable[[str], str] = None,
|
||||
skip_author_verification: bool = False) -> Tuple[str, dict]:
|
||||
"""
|
||||
Receive a request.
|
||||
|
||||
Matrix appservices will deliver 1+ events at a time.
|
||||
"""
|
||||
# TODO TBD
|
||||
return self.actor, self.payload
|
|
@ -0,0 +1,28 @@
|
|||
import json
|
||||
|
||||
from federation.protocols.matrix.protocol import identify_request, identify_id
|
||||
from federation.types import RequestType
|
||||
|
||||
|
||||
def test_identify_id():
|
||||
assert identify_id('foobar') is False
|
||||
assert identify_id('foobar@example.com') is False
|
||||
assert identify_id('foobar@example.com:8000') is False
|
||||
assert identify_id('http://foobar@example.com') is False
|
||||
assert identify_id('https://foobar@example.com') is False
|
||||
assert identify_id('@foobar:domain.tld') is True
|
||||
assert identify_id('#foobar:domain.tld') is True
|
||||
assert identify_id('!foobar:domain.tld') is True
|
||||
|
||||
|
||||
class TestIdentifyRequest:
|
||||
def test_identifies_matrix_request(self):
|
||||
assert identify_request(RequestType(body=json.dumps('{"events": []}')))
|
||||
assert identify_request(RequestType(body=json.dumps('{"events": []}').encode('utf-8')))
|
||||
|
||||
def test_passes_gracefully_for_non_matrix_request(self):
|
||||
assert not identify_request(RequestType(body='foo'))
|
||||
assert not identify_request(RequestType(body='<xml></<xml>'))
|
||||
assert not identify_request(RequestType(body=b'<xml></<xml>'))
|
||||
assert not identify_request(RequestType(body=json.dumps('{"@context": "foo"}')))
|
||||
assert not identify_request(RequestType(body=json.dumps('{"@context": "foo"}').encode('utf-8')))
|
Ładowanie…
Reference in New Issue