summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-10-16 16:56:51 +0100
committerErik Johnston <erik@matrix.org>2014-10-16 16:56:51 +0100
commit1116f5330ec80533954026f67018e0db190cbae0 (patch)
tree6f559db890c6940b431c8aacb0836a24f0dbcf6e /synapse
parentBegin making auth use event.old_state_events (diff)
downloadsynapse-1116f5330ec80533954026f67018e0db190cbae0.tar.xz
Start implementing the invite/join dance. Continue moving auth to use event.state_events
Diffstat (limited to '')
-rw-r--r--synapse/api/auth.py16
-rw-r--r--synapse/federation/replication.py22
-rw-r--r--synapse/federation/transport.py34
-rw-r--r--synapse/handlers/federation.py83
4 files changed, 133 insertions, 22 deletions
diff --git a/synapse/api/auth.py b/synapse/api/auth.py
index d951cb265b..12ddef1b00 100644
--- a/synapse/api/auth.py
+++ b/synapse/api/auth.py
@@ -21,7 +21,7 @@ from synapse.api.constants import Membership, JoinRules
 from synapse.api.errors import AuthError, StoreError, Codes, SynapseError
 from synapse.api.events.room import (
     RoomMemberEvent, RoomPowerLevelsEvent, RoomRedactionEvent,
-    RoomJoinRulesEvent, RoomOpsPowerLevelsEvent,
+    RoomJoinRulesEvent, RoomOpsPowerLevelsEvent, InviteJoinEvent,
 )
 from synapse.util.logutils import log_function
 
@@ -56,7 +56,8 @@ class Auth(object):
                     defer.returnValue(allowed)
                     return
 
-                self.check_event_sender_in_room(event)
+                if not event.type == InviteJoinEvent.TYPE:
+                    self.check_event_sender_in_room(event)
 
                 if is_state:
                     # TODO (erikj): This really only should be called for *new*
@@ -115,11 +116,6 @@ class Auth(object):
     def is_membership_change_allowed(self, event):
         target_user_id = event.state_key
 
-        # does this room even exist
-        room = yield self.store.get_room(event.room_id)
-        if not room:
-            raise AuthError(403, "Room does not exist")
-
         # get info about the caller
         key = (RoomMemberEvent.TYPE, event.user_id, )
         caller = event.old_state_events.get(key)
@@ -170,7 +166,7 @@ class Auth(object):
             # joined: It's a NOOP
             if event.user_id != target_user_id:
                 raise AuthError(403, "Cannot force another user to join.")
-            elif join_rule == JoinRules.PUBLIC or room.is_public:
+            elif join_rule == JoinRules.PUBLIC:
                 pass
             elif join_rule == JoinRules.INVITE:
                 if (
@@ -215,9 +211,9 @@ class Auth(object):
         power_level_event = event.old_state_events.get(key)
         level = None
         if power_level_event:
-            level = power_level_event.content[user_id]
+            level = power_level_event.content.get(user_id)
             if not level:
-                level = power_level_event.content["default"]
+                level = power_level_event.content.get("default", 0)
 
         return level
 
diff --git a/synapse/federation/replication.py b/synapse/federation/replication.py
index 2346d55045..08c29dece5 100644
--- a/synapse/federation/replication.py
+++ b/synapse/federation/replication.py
@@ -393,9 +393,25 @@ class ReplicationLayer(object):
             response = yield self.query_handlers[query_type](args)
             defer.returnValue((200, response))
         else:
-            defer.returnValue((404, "No handler for Query type '%s'"
-                % (query_type)
-            ))
+            defer.returnValue(
+                (404, "No handler for Query type '%s'" % (query_type, ))
+            )
+
+    def on_make_join_request(self, context, user_id):
+        return self.handler.on_make_join_request(context, user_id)
+
+    @defer.inlineCallbacks
+    def on_send_join_request(self, origin, content):
+        pdu = Pdu(**content)
+        state = yield self.handler.on_send_join_request(origin, pdu)
+        defer.returnValue((200, self._transaction_from_pdus(state).get_dict()))
+
+    def make_join(self, destination, context, user_id):
+        return self.transport_layer.make_join(
+            destination=destination,
+            context=context,
+            user_id=user_id,
+        )
 
     @defer.inlineCallbacks
     @log_function
diff --git a/synapse/federation/transport.py b/synapse/federation/transport.py
index 755eee8cf6..4f552272e6 100644
--- a/synapse/federation/transport.py
+++ b/synapse/federation/transport.py
@@ -198,6 +198,19 @@ class TransportLayer(object):
         defer.returnValue(response)
 
     @defer.inlineCallbacks
+    @log_function
+    def make_join(self, destination, context, user_id, retry_on_dns_fail=True):
+        path = PREFIX + "/make_join/%s/%s" % (context, user_id,)
+
+        response = yield self.client.get_json(
+            destination=destination,
+            path=path,
+            retry_on_dns_fail=retry_on_dns_fail,
+        )
+
+        defer.returnValue(response)
+
+    @defer.inlineCallbacks
     def _authenticate_request(self, request):
         json_request = {
             "method": request.method,
@@ -353,6 +366,12 @@ class TransportLayer(object):
             )
         )
 
+        self.server.register_path(
+            "GET",
+            re.compile("^" + PREFIX + "/make_join/([^/]*)/([^/]*)$"),
+            self._on_make_join_request
+        )
+
     @defer.inlineCallbacks
     @log_function
     def _on_send_request(self, origin, content, query, transaction_id):
@@ -438,7 +457,20 @@ class TransportLayer(object):
         versions = [v.split(",", 1) for v in v_list]
 
         return self.request_handler.on_backfill_request(
-            context, versions, limit)
+            context, versions, limit
+        )
+
+    @log_function
+    def _on_make_join_request(self, origin, content, query, context, user_id):
+        return self.request_handler.on_make_join_request(
+            context, user_id,
+        )
+
+    @log_function
+    def _on_send_join_request(self, origin, content, query):
+        return self.request_handler.on_send_join_request(
+            origin, content,
+        )
 
 
 class TransportReceivedHandler(object):
diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py
index 44bf7def2e..a4f6c739c3 100644
--- a/synapse/handlers/federation.py
+++ b/synapse/handlers/federation.py
@@ -89,7 +89,7 @@ class FederationHandler(BaseHandler):
     @defer.inlineCallbacks
     def on_receive_pdu(self, pdu, backfilled):
         """ Called by the ReplicationLayer when we have a new pdu. We need to
-        do auth checks and put it throught the StateHandler.
+        do auth checks and put it through the StateHandler.
         """
         event = self.pdu_codec.event_from_pdu(pdu)
 
@@ -97,13 +97,17 @@ class FederationHandler(BaseHandler):
 
         yield self.state_handler.annotate_state_groups(event)
 
-        with (yield self.lock_manager.lock(pdu.context)):
-            if event.is_state and not backfilled:
-                is_new_state = yield self.state_handler.handle_new_state(
-                    pdu
-                )
-            else:
-                is_new_state = False
+        logger.debug("Event: %s", event)
+
+        if not backfilled:
+            yield self.auth.check(event, None, raises=True)
+
+        if event.is_state and not backfilled:
+            is_new_state = yield self.state_handler.handle_new_state(
+                pdu
+            )
+        else:
+            is_new_state = False
         # TODO: Implement something in federation that allows us to
         # respond to PDU.
 
@@ -267,6 +271,69 @@ class FederationHandler(BaseHandler):
 
         defer.returnValue(True)
 
+    @defer.inlineCallbacks
+    def on_make_join_request(self, context, user_id):
+        event = self.event_factory.create_event(
+            etype=RoomMemberEvent.TYPE,
+            content={"membership": Membership.JOIN},
+            room_id=context,
+            user_id=user_id,
+            state_key=user_id,
+        )
+
+        snapshot = yield self.store.snapshot_room(
+            event.room_id, event.user_id,
+        )
+        snapshot.fill_out_prev_events(event)
+
+        pdu = self.pdu_codec.pdu_from_event(event)
+
+        defer.returnValue(pdu)
+
+    @defer.inlineCallbacks
+    def on_send_join_request(self, origin, pdu):
+        event = self.pdu_codec.event_from_pdu(pdu)
+
+        yield self.state_handler.annotate_state_groups(event)
+        yield self.auth.check(event, None, raises=True)
+
+        is_new_state = yield self.state_handler.handle_new_state(
+            pdu
+        )
+
+        # FIXME (erikj):  All this is duplicated above :(
+
+        yield self.store.persist_event(
+            event,
+            backfilled=False,
+            is_new_state=is_new_state
+        )
+
+        extra_users = []
+        if event.type == RoomMemberEvent.TYPE:
+            target_user_id = event.state_key
+            target_user = self.hs.parse_userid(target_user_id)
+            extra_users.append(target_user)
+
+        yield self.notifier.on_new_room_event(
+            event, extra_users=extra_users
+        )
+
+        if event.type == RoomMemberEvent.TYPE:
+            if event.membership == Membership.JOIN:
+                user = self.hs.parse_userid(event.state_key)
+                self.distributor.fire(
+                    "user_joined_room", user=user, room_id=event.room_id
+                )
+
+        pdu.destinations = yield self.store.get_joined_hosts_for_room(
+            event.room_id
+        )
+
+        yield self.replication_layer.send_pdu(pdu)
+
+        defer.returnValue(event.state_events.values())
+
     @log_function
     def _on_user_joined(self, user, room_id):
         waiters = self.waiting_for_join_list.get((user.to_string(), room_id), [])