def test_allowed_appservice_via_can_requester_do_action(self): appservice = ApplicationService( None, "example.com", id="foo", rate_limited=False, sender="@as:example.com", ) as_requester = create_requester("@user:example.com", app_service=appservice) limiter = Ratelimiter( store=self.hs.get_datastore(), clock=None, rate_hz=0.1, burst_count=1 ) allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(as_requester, _time_now_s=0) ) self.assertTrue(allowed) self.assertEquals(-1, time_allowed) allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(as_requester, _time_now_s=5) ) self.assertTrue(allowed) self.assertEquals(-1, time_allowed) allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(as_requester, _time_now_s=10) ) self.assertTrue(allowed) self.assertEquals(-1, time_allowed)
def test_pruning(self): limiter = Ratelimiter(clock=None, rate_hz=0.1, burst_count=1) limiter.can_do_action(key="test_id_1", _time_now_s=0) self.assertIn("test_id_1", limiter.actions) limiter.can_do_action(key="test_id_2", _time_now_s=10) self.assertNotIn("test_id_1", limiter.actions)
def test_allowed_via_can_do_action(self): limiter = Ratelimiter(clock=None, rate_hz=0.1, burst_count=1) allowed, time_allowed = limiter.can_do_action(key="test_id", _time_now_s=0) self.assertTrue(allowed) self.assertEquals(10.0, time_allowed) allowed, time_allowed = limiter.can_do_action(key="test_id", _time_now_s=5) self.assertFalse(allowed) self.assertEquals(10.0, time_allowed) allowed, time_allowed = limiter.can_do_action(key="test_id", _time_now_s=10) self.assertTrue(allowed) self.assertEquals(20.0, time_allowed)
def test_pruning(self): limiter = Ratelimiter() allowed, time_allowed = limiter.can_do_action( key="test_id_1", time_now_s=0, rate_hz=0.1, burst_count=1 ) self.assertIn("test_id_1", limiter.message_counts) allowed, time_allowed = limiter.can_do_action( key="test_id_2", time_now_s=10, rate_hz=0.1, burst_count=1 ) self.assertNotIn("test_id_1", limiter.message_counts)
def test_pruning(self): limiter = Ratelimiter(store=self.hs.get_datastores().main, clock=None, rate_hz=0.1, burst_count=1) self.get_success_or_raise( limiter.can_do_action(None, key="test_id_1", _time_now_s=0)) self.assertIn("test_id_1", limiter.actions) self.get_success_or_raise( limiter.can_do_action(None, key="test_id_2", _time_now_s=10)) self.assertNotIn("test_id_1", limiter.actions)
def test_pruning(self): limiter = Ratelimiter() allowed, time_allowed = limiter.can_do_action(key="test_id_1", time_now_s=0, rate_hz=0.1, burst_count=1) self.assertIn("test_id_1", limiter.message_counts) allowed, time_allowed = limiter.can_do_action(key="test_id_2", time_now_s=10, rate_hz=0.1, burst_count=1) self.assertNotIn("test_id_1", limiter.message_counts)
def test_allowed_via_can_do_action_and_overriding_parameters(self): """Test that we can override options of can_do_action that would otherwise fail an action """ # Create a Ratelimiter with a very low allowed rate_hz and burst_count limiter = Ratelimiter(store=self.hs.get_datastores().main, clock=None, rate_hz=0.1, burst_count=1) # First attempt should be allowed allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action( None, ("test_id", ), _time_now_s=0, )) self.assertTrue(allowed) self.assertEqual(10.0, time_allowed) # Second attempt, 1s later, will fail allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action( None, ("test_id", ), _time_now_s=1, )) self.assertFalse(allowed) self.assertEqual(10.0, time_allowed) # But, if we allow 10 actions/sec for this request, we should be allowed # to continue. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, ("test_id", ), _time_now_s=1, rate_hz=10.0)) self.assertTrue(allowed) self.assertEqual(1.1, time_allowed) # Similarly if we allow a burst of 10 actions allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, ("test_id", ), _time_now_s=1, burst_count=10)) self.assertTrue(allowed) self.assertEqual(1.0, time_allowed)
def test_allowed_via_can_do_action(self): limiter = Ratelimiter(store=self.hs.get_datastores().main, clock=None, rate_hz=0.1, burst_count=1) allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", _time_now_s=0)) self.assertTrue(allowed) self.assertEqual(10.0, time_allowed) allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", _time_now_s=5)) self.assertFalse(allowed) self.assertEqual(10.0, time_allowed) allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", _time_now_s=10)) self.assertTrue(allowed) self.assertEqual(20.0, time_allowed)
def test_allowed(self): limiter = Ratelimiter() allowed, time_allowed = limiter.can_do_action( key="test_id", time_now_s=0, rate_hz=0.1, burst_count=1 ) self.assertTrue(allowed) self.assertEquals(10.0, time_allowed) allowed, time_allowed = limiter.can_do_action( key="test_id", time_now_s=5, rate_hz=0.1, burst_count=1 ) self.assertFalse(allowed) self.assertEquals(10.0, time_allowed) allowed, time_allowed = limiter.can_do_action( key="test_id", time_now_s=10, rate_hz=0.1, burst_count=1 ) self.assertTrue(allowed) self.assertEquals(20.0, time_allowed)
def test_allowed(self): limiter = Ratelimiter() allowed, time_allowed = limiter.can_do_action(key="test_id", time_now_s=0, rate_hz=0.1, burst_count=1) self.assertTrue(allowed) self.assertEquals(10.0, time_allowed) allowed, time_allowed = limiter.can_do_action(key="test_id", time_now_s=5, rate_hz=0.1, burst_count=1) self.assertFalse(allowed) self.assertEquals(10.0, time_allowed) allowed, time_allowed = limiter.can_do_action(key="test_id", time_now_s=10, rate_hz=0.1, burst_count=1) self.assertTrue(allowed) self.assertEquals(20.0, time_allowed)
def test_multiple_actions(self): limiter = Ratelimiter( store=self.hs.get_datastore(), clock=None, rate_hz=0.1, burst_count=3 ) # Test that 4 actions aren't allowed with a maximum burst of 3. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=4, _time_now_s=0) ) self.assertFalse(allowed) # Test that 3 actions are allowed with a maximum burst of 3. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=3, _time_now_s=0) ) self.assertTrue(allowed) self.assertEquals(10.0, time_allowed) # Test that, after doing these 3 actions, we can't do any more action without # waiting. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=1, _time_now_s=0) ) self.assertFalse(allowed) self.assertEquals(10.0, time_allowed) # Test that after waiting we can do only 1 action. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action( None, key="test_id", update=False, n_actions=1, _time_now_s=10, ) ) self.assertTrue(allowed) # The time allowed is the current time because we could still repeat the action # once. self.assertEquals(10.0, time_allowed) allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=2, _time_now_s=10) ) self.assertFalse(allowed) # The time allowed doesn't change despite allowed being False because, while we # don't allow 2 actions, we could still do 1. self.assertEquals(10.0, time_allowed) # Test that after waiting a bit more we can do 2 actions. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=2, _time_now_s=20) ) self.assertTrue(allowed) # The time allowed is the current time because we could still repeat the action # once. self.assertEquals(20.0, time_allowed)
class DeviceMessageHandler: def __init__(self, hs: "HomeServer"): """ Args: hs: server """ self.store = hs.get_datastore() self.notifier = hs.get_notifier() self.is_mine = hs.is_mine # We only need to poke the federation sender explicitly if its on the # same instance. Other federation sender instances will get notified by # `synapse.app.generic_worker.FederationSenderHandler` when it sees it # in the to-device replication stream. self.federation_sender = None if hs.should_send_federation(): self.federation_sender = hs.get_federation_sender() # If we can handle the to device EDUs we do so, otherwise we route them # to the appropriate worker. if hs.get_instance_name() in hs.config.worker.writers.to_device: hs.get_federation_registry().register_edu_handler( "m.direct_to_device", self.on_direct_to_device_edu) else: hs.get_federation_registry().register_instances_for_edu( "m.direct_to_device", hs.config.worker.writers.to_device, ) # The handler to call when we think a user's device list might be out of # sync. We do all device list resyncing on the master instance, so if # we're on a worker we hit the device resync replication API. if hs.config.worker.worker_app is None: self._user_device_resync = ( hs.get_device_handler().device_list_updater.user_device_resync) else: self._user_device_resync = ( ReplicationUserDevicesResyncRestServlet.make_client(hs)) self._ratelimiter = Ratelimiter( clock=hs.get_clock(), rate_hz=hs.config.rc_key_requests.per_second, burst_count=hs.config.rc_key_requests.burst_count, ) async def on_direct_to_device_edu(self, origin: str, content: JsonDict) -> None: local_messages = {} sender_user_id = content["sender"] if origin != get_domain_from_id(sender_user_id): logger.warning( "Dropping device message from %r with spoofed sender %r", origin, sender_user_id, ) message_type = content["type"] message_id = content["message_id"] for user_id, by_device in content["messages"].items(): # we use UserID.from_string to catch invalid user ids if not self.is_mine(UserID.from_string(user_id)): logger.warning("Request for keys for non-local user %s", user_id) raise SynapseError(400, "Not a user here") if not by_device: continue messages_by_device = { device_id: { "content": message_content, "type": message_type, "sender": sender_user_id, } for device_id, message_content in by_device.items() } local_messages[user_id] = messages_by_device await self._check_for_unknown_devices(message_type, sender_user_id, by_device) stream_id = await self.store.add_messages_from_remote_to_device_inbox( origin, message_id, local_messages) self.notifier.on_new_event("to_device_key", stream_id, users=local_messages.keys()) async def _check_for_unknown_devices( self, message_type: str, sender_user_id: str, by_device: Dict[str, Dict[str, Any]], ) -> None: """Checks inbound device messages for unknown remote devices, and if found marks the remote cache for the user as stale. """ if message_type != "m.room_key_request": return # Get the sending device IDs requesting_device_ids = set() for message_content in by_device.values(): device_id = message_content.get("requesting_device_id") requesting_device_ids.add(device_id) # Check if we are tracking the devices of the remote user. room_ids = await self.store.get_rooms_for_user(sender_user_id) if not room_ids: logger.info( "Received device message from remote device we don't" " share a room with: %s %s", sender_user_id, requesting_device_ids, ) return # If we are tracking check that we know about the sending # devices. cached_devices = await self.store.get_cached_devices_for_user( sender_user_id) unknown_devices = requesting_device_ids - set(cached_devices) if unknown_devices: logger.info( "Received device message from remote device not in our cache: %s %s", sender_user_id, unknown_devices, ) await self.store.mark_remote_user_device_cache_as_stale( sender_user_id) # Immediately attempt a resync in the background run_in_background(self._user_device_resync, user_id=sender_user_id) async def send_device_message( self, requester: Requester, message_type: str, messages: Dict[str, Dict[str, JsonDict]], ) -> None: sender_user_id = requester.user.to_string() set_tag("number_of_messages", len(messages)) set_tag("sender", sender_user_id) local_messages = {} remote_messages = {} # type: Dict[str, Dict[str, Dict[str, JsonDict]]] for user_id, by_device in messages.items(): # Ratelimit local cross-user key requests by the sending device. if (message_type == EduTypes.RoomKeyRequest and user_id != sender_user_id and self._ratelimiter.can_do_action( (sender_user_id, requester.device_id))): continue # we use UserID.from_string to catch invalid user ids if self.is_mine(UserID.from_string(user_id)): messages_by_device = { device_id: { "content": message_content, "type": message_type, "sender": sender_user_id, } for device_id, message_content in by_device.items() } if messages_by_device: local_messages[user_id] = messages_by_device else: destination = get_domain_from_id(user_id) remote_messages.setdefault(destination, {})[user_id] = by_device message_id = random_string(16) context = get_active_span_text_map() remote_edu_contents = {} for destination, messages in remote_messages.items(): with start_active_span("to_device_for_user"): set_tag("destination", destination) remote_edu_contents[destination] = { "messages": messages, "sender": sender_user_id, "type": message_type, "message_id": message_id, "org.matrix.opentracing_context": json_encoder.encode(context), } log_kv({"local_messages": local_messages}) stream_id = await self.store.add_messages_to_device_inbox( local_messages, remote_edu_contents) self.notifier.on_new_event("to_device_key", stream_id, users=local_messages.keys()) log_kv({"remote_messages": remote_messages}) if self.federation_sender: for destination in remote_messages.keys(): # Enqueue a new federation transaction to send the new # device messages to each remote destination. self.federation_sender.send_device_messages(destination)
class AuthHandler(BaseHandler): SESSION_EXPIRE_MS = 48 * 60 * 60 * 1000 def __init__(self, hs): """ Args: hs (synapse.server.HomeServer): """ super(AuthHandler, self).__init__(hs) self.checkers = {} # type: dict[str, UserInteractiveAuthChecker] for auth_checker_class in INTERACTIVE_AUTH_CHECKERS: inst = auth_checker_class(hs) if inst.is_enabled(): self.checkers[inst.AUTH_TYPE] = inst self.bcrypt_rounds = hs.config.bcrypt_rounds # This is not a cache per se, but a store of all current sessions that # expire after N hours self.sessions = ExpiringCache( cache_name="register_sessions", clock=hs.get_clock(), expiry_ms=self.SESSION_EXPIRE_MS, reset_expiry_on_get=True, ) account_handler = ModuleApi(hs, self) self.password_providers = [ module(config=config, account_handler=account_handler) for module, config in hs.config.password_providers ] logger.info("Extra password_providers: %r", self.password_providers) self.hs = hs # FIXME better possibility to access registrationHandler later? self.macaroon_gen = hs.get_macaroon_generator() self._password_enabled = hs.config.password_enabled # we keep this as a list despite the O(N^2) implication so that we can # keep PASSWORD first and avoid confusing clients which pick the first # type in the list. (NB that the spec doesn't require us to do so and # clients which favour types that they don't understand over those that # they do are technically broken) login_types = [] if self._password_enabled: login_types.append(LoginType.PASSWORD) for provider in self.password_providers: if hasattr(provider, "get_supported_login_types"): for t in provider.get_supported_login_types().keys(): if t not in login_types: login_types.append(t) self._supported_login_types = login_types # Ratelimiter for failed auth during UIA. Uses same ratelimit config # as per `rc_login.failed_attempts`. self._failed_uia_attempts_ratelimiter = Ratelimiter() self._clock = self.hs.get_clock() @defer.inlineCallbacks def validate_user_via_ui_auth(self, requester, request_body, clientip): """ Checks that the user is who they claim to be, via a UI auth. This is used for things like device deletion and password reset where the user already has a valid access token, but we want to double-check that it isn't stolen by re-authenticating them. Args: requester (Requester): The user, as given by the access token request_body (dict): The body of the request sent by the client clientip (str): The IP address of the client. Returns: defer.Deferred[dict]: the parameters for this request (which may have been given only in a previous call). Raises: InteractiveAuthIncompleteError if the client has not yet completed any of the permitted login flows AuthError if the client has completed a login flow, and it gives a different user to `requester` LimitExceededError if the ratelimiter's failed request count for this user is too high to proceed """ user_id = requester.user.to_string() # Check if we should be ratelimited due to too many previous failed attempts self._failed_uia_attempts_ratelimiter.ratelimit( user_id, time_now_s=self._clock.time(), rate_hz=self.hs.config.rc_login_failed_attempts.per_second, burst_count=self.hs.config.rc_login_failed_attempts.burst_count, update=False, ) # build a list of supported flows flows = [[login_type] for login_type in self._supported_login_types] try: result, params, _ = yield self.check_auth(flows, request_body, clientip) except LoginError: # Update the ratelimite to say we failed (`can_do_action` doesn't raise). self._failed_uia_attempts_ratelimiter.can_do_action( user_id, time_now_s=self._clock.time(), rate_hz=self.hs.config.rc_login_failed_attempts.per_second, burst_count=self.hs.config.rc_login_failed_attempts. burst_count, update=True, ) raise # find the completed login type for login_type in self._supported_login_types: if login_type not in result: continue user_id = result[login_type] break else: # this can't happen raise Exception( "check_auth returned True but no successful login type") # check that the UI auth matched the access token if user_id != requester.user.to_string(): raise AuthError(403, "Invalid auth") return params def get_enabled_auth_types(self): """Return the enabled user-interactive authentication types Returns the UI-Auth types which are supported by the homeserver's current config. """ return self.checkers.keys() @defer.inlineCallbacks def check_auth(self, flows, clientdict, clientip): """ Takes a dictionary sent by the client in the login / registration protocol and handles the User-Interactive Auth flow. As a side effect, this function fills in the 'creds' key on the user's session with a map, which maps each auth-type (str) to the relevant identity authenticated by that auth-type (mostly str, but for captcha, bool). If no auth flows have been completed successfully, raises an InteractiveAuthIncompleteError. To handle this, you can use synapse.rest.client.v2_alpha._base.interactive_auth_handler as a decorator. Args: flows (list): A list of login flows. Each flow is an ordered list of strings representing auth-types. At least one full flow must be completed in order for auth to be successful. clientdict: The dictionary from the client root level, not the 'auth' key: this method prompts for auth if none is sent. clientip (str): The IP address of the client. Returns: defer.Deferred[dict, dict, str]: a deferred tuple of (creds, params, session_id). 'creds' contains the authenticated credentials of each stage. 'params' contains the parameters for this request (which may have been given only in a previous call). 'session_id' is the ID of this session, either passed in by the client or assigned by this call Raises: InteractiveAuthIncompleteError if the client has not yet completed all the stages in any of the permitted flows. """ authdict = None sid = None if clientdict and "auth" in clientdict: authdict = clientdict["auth"] del clientdict["auth"] if "session" in authdict: sid = authdict["session"] session = self._get_session_info(sid) if len(clientdict) > 0: # This was designed to allow the client to omit the parameters # and just supply the session in subsequent calls so it split # auth between devices by just sharing the session, (eg. so you # could continue registration from your phone having clicked the # email auth link on there). It's probably too open to abuse # because it lets unauthenticated clients store arbitrary objects # on a homeserver. # Revisit: Assumimg the REST APIs do sensible validation, the data # isn't arbintrary. session["clientdict"] = clientdict self._save_session(session) elif "clientdict" in session: clientdict = session["clientdict"] if not authdict: raise InteractiveAuthIncompleteError( self._auth_dict_for_flows(flows, session)) if "creds" not in session: session["creds"] = {} creds = session["creds"] # check auth type currently being presented errordict = {} if "type" in authdict: login_type = authdict["type"] try: result = yield self._check_auth_dict(authdict, clientip) if result: creds[login_type] = result self._save_session(session) except LoginError as e: if login_type == LoginType.EMAIL_IDENTITY: # riot used to have a bug where it would request a new # validation token (thus sending a new email) each time it # got a 401 with a 'flows' field. # (https://github.com/vector-im/vector-web/issues/2447). # # Grandfather in the old behaviour for now to avoid # breaking old riot deployments. raise # this step failed. Merge the error dict into the response # so that the client can have another go. errordict = e.error_dict() for f in flows: if len(set(f) - set(creds)) == 0: # it's very useful to know what args are stored, but this can # include the password in the case of registering, so only log # the keys (confusingly, clientdict may contain a password # param, creds is just what the user authed as for UI auth # and is not sensitive). logger.info( "Auth completed with creds: %r. Client dict has keys: %r", creds, list(clientdict), ) return creds, clientdict, session["id"] ret = self._auth_dict_for_flows(flows, session) ret["completed"] = list(creds) ret.update(errordict) raise InteractiveAuthIncompleteError(ret) @defer.inlineCallbacks def add_oob_auth(self, stagetype, authdict, clientip): """ Adds the result of out-of-band authentication into an existing auth session. Currently used for adding the result of fallback auth. """ if stagetype not in self.checkers: raise LoginError(400, "", Codes.MISSING_PARAM) if "session" not in authdict: raise LoginError(400, "", Codes.MISSING_PARAM) sess = self._get_session_info(authdict["session"]) if "creds" not in sess: sess["creds"] = {} creds = sess["creds"] result = yield self.checkers[stagetype].check_auth(authdict, clientip) if result: creds[stagetype] = result self._save_session(sess) return True return False def get_session_id(self, clientdict): """ Gets the session ID for a client given the client dictionary Args: clientdict: The dictionary sent by the client in the request Returns: str|None: The string session ID the client sent. If the client did not send a session ID, returns None. """ sid = None if clientdict and "auth" in clientdict: authdict = clientdict["auth"] if "session" in authdict: sid = authdict["session"] return sid def set_session_data(self, session_id, key, value): """ Store a key-value pair into the sessions data associated with this request. This data is stored server-side and cannot be modified by the client. Args: session_id (string): The ID of this session as returned from check_auth key (string): The key to store the data under value (any): The data to store """ sess = self._get_session_info(session_id) sess.setdefault("serverdict", {})[key] = value self._save_session(sess) def get_session_data(self, session_id, key, default=None): """ Retrieve data stored with set_session_data Args: session_id (string): The ID of this session as returned from check_auth key (string): The key to store the data under default (any): Value to return if the key has not been set """ sess = self._get_session_info(session_id) return sess.setdefault("serverdict", {}).get(key, default) @defer.inlineCallbacks def _check_auth_dict(self, authdict, clientip): """Attempt to validate the auth dict provided by a client Args: authdict (object): auth dict provided by the client clientip (str): IP address of the client Returns: Deferred: result of the stage verification. Raises: StoreError if there was a problem accessing the database SynapseError if there was a problem with the request LoginError if there was an authentication problem. """ login_type = authdict["type"] checker = self.checkers.get(login_type) if checker is not None: res = yield checker.check_auth(authdict, clientip=clientip) return res # build a v1-login-style dict out of the authdict and fall back to the # v1 code user_id = authdict.get("user") if user_id is None: raise SynapseError(400, "", Codes.MISSING_PARAM) (canonical_id, callback) = yield self.validate_login(user_id, authdict) return canonical_id def _get_params_recaptcha(self): return {"public_key": self.hs.config.recaptcha_public_key} def _get_params_terms(self): return { "policies": { "privacy_policy": { "version": self.hs.config.user_consent_version, "en": { "name": self.hs.config.user_consent_policy_name, "url": "%s_matrix/consent?v=%s" % ( self.hs.config.public_baseurl, self.hs.config.user_consent_version, ), }, } } } def _auth_dict_for_flows(self, flows, session): public_flows = [] for f in flows: public_flows.append(f) get_params = { LoginType.RECAPTCHA: self._get_params_recaptcha, LoginType.TERMS: self._get_params_terms, } params = {} for f in public_flows: for stage in f: if stage in get_params and stage not in params: params[stage] = get_params[stage]() return { "session": session["id"], "flows": [{ "stages": f } for f in public_flows], "params": params, } def _get_session_info(self, session_id): if session_id not in self.sessions: session_id = None if not session_id: # create a new session while session_id is None or session_id in self.sessions: session_id = stringutils.random_string(24) self.sessions[session_id] = {"id": session_id} return self.sessions[session_id] @defer.inlineCallbacks def get_access_token_for_user_id(self, user_id, device_id, valid_until_ms): """ Creates a new access token for the user with the given user ID. The user is assumed to have been authenticated by some other machanism (e.g. CAS), and the user_id converted to the canonical case. The device will be recorded in the table if it is not there already. Args: user_id (str): canonical User ID device_id (str|None): the device ID to associate with the tokens. None to leave the tokens unassociated with a device (deprecated: we should always have a device ID) valid_until_ms (int|None): when the token is valid until. None for no expiry. Returns: The access token for the user's session. Raises: StoreError if there was a problem storing the token. """ fmt_expiry = "" if valid_until_ms is not None: fmt_expiry = time.strftime(" until %Y-%m-%d %H:%M:%S", time.localtime(valid_until_ms / 1000.0)) logger.info("Logging in user %s on device %s%s", user_id, device_id, fmt_expiry) yield self.auth.check_auth_blocking(user_id) access_token = self.macaroon_gen.generate_access_token(user_id) yield self.store.add_access_token_to_user(user_id, access_token, device_id, valid_until_ms) # the device *should* have been registered before we got here; however, # it's possible we raced against a DELETE operation. The thing we # really don't want is active access_tokens without a record of the # device, so we double-check it here. if device_id is not None: try: yield self.store.get_device(user_id, device_id) except StoreError: yield self.store.delete_access_token(access_token) raise StoreError(400, "Login raced against device deletion") return access_token @defer.inlineCallbacks def check_user_exists(self, user_id): """ Checks to see if a user with the given id exists. Will check case insensitively, but return None if there are multiple inexact matches. Args: (unicode|bytes) user_id: complete @user:id Returns: defer.Deferred: (unicode) canonical_user_id, or None if zero or multiple matches Raises: UserDeactivatedError if a user is found but is deactivated. """ res = yield self._find_user_id_and_pwd_hash(user_id) if res is not None: return res[0] return None @defer.inlineCallbacks def _find_user_id_and_pwd_hash(self, user_id): """Checks to see if a user with the given id exists. Will check case insensitively, but will return None if there are multiple inexact matches. Returns: tuple: A 2-tuple of `(canonical_user_id, password_hash)` None: if there is not exactly one match """ user_infos = yield self.store.get_users_by_id_case_insensitive(user_id) result = None if not user_infos: logger.warning("Attempted to login as %s but they do not exist", user_id) elif len(user_infos) == 1: # a single match (possibly not exact) result = user_infos.popitem() elif user_id in user_infos: # multiple matches, but one is exact result = (user_id, user_infos[user_id]) else: # multiple matches, none of them exact logger.warning( "Attempted to login as %s but it matches more than one user " "inexactly: %r", user_id, user_infos.keys(), ) return result def get_supported_login_types(self): """Get a the login types supported for the /login API By default this is just 'm.login.password' (unless password_enabled is False in the config file), but password auth providers can provide other login types. Returns: Iterable[str]: login types """ return self._supported_login_types @defer.inlineCallbacks def validate_login(self, username, login_submission): """Authenticates the user for the /login API Also used by the user-interactive auth flow to validate m.login.password auth types. Args: username (str): username supplied by the user login_submission (dict): the whole of the login submission (including 'type' and other relevant fields) Returns: Deferred[str, func]: canonical user id, and optional callback to be called once the access token and device id are issued Raises: StoreError if there was a problem accessing the database SynapseError if there was a problem with the request LoginError if there was an authentication problem. """ if username.startswith("@"): qualified_user_id = username else: qualified_user_id = UserID(username, self.hs.hostname).to_string() login_type = login_submission.get("type") known_login_type = False # special case to check for "password" for the check_password interface # for the auth providers password = login_submission.get("password") if login_type == LoginType.PASSWORD: if not self._password_enabled: raise SynapseError(400, "Password login has been disabled.") if not password: raise SynapseError(400, "Missing parameter: password") for provider in self.password_providers: if hasattr(provider, "check_password") and login_type == LoginType.PASSWORD: known_login_type = True is_valid = yield provider.check_password( qualified_user_id, password) if is_valid: return qualified_user_id, None if not hasattr(provider, "get_supported_login_types") or not hasattr( provider, "check_auth"): # this password provider doesn't understand custom login types continue supported_login_types = provider.get_supported_login_types() if login_type not in supported_login_types: # this password provider doesn't understand this login type continue known_login_type = True login_fields = supported_login_types[login_type] missing_fields = [] login_dict = {} for f in login_fields: if f not in login_submission: missing_fields.append(f) else: login_dict[f] = login_submission[f] if missing_fields: raise SynapseError( 400, "Missing parameters for login type %s: %s" % (login_type, missing_fields), ) result = yield provider.check_auth(username, login_type, login_dict) if result: if isinstance(result, str): result = (result, None) return result if login_type == LoginType.PASSWORD and self.hs.config.password_localdb_enabled: known_login_type = True canonical_user_id = yield self._check_local_password( qualified_user_id, password) if canonical_user_id: return canonical_user_id, None if not known_login_type: raise SynapseError(400, "Unknown login type %s" % login_type) # We raise a 403 here, but note that if we're doing user-interactive # login, it turns all LoginErrors into a 401 anyway. raise LoginError(403, "Invalid password", errcode=Codes.FORBIDDEN) @defer.inlineCallbacks def check_password_provider_3pid(self, medium, address, password): """Check if a password provider is able to validate a thirdparty login Args: medium (str): The medium of the 3pid (ex. email). address (str): The address of the 3pid (ex. [email protected]). password (str): The password of the user. Returns: Deferred[(str|None, func|None)]: A tuple of `(user_id, callback)`. If authentication is successful, `user_id` is a `str` containing the authenticated, canonical user ID. `callback` is then either a function to be later run after the server has completed login/registration, or `None`. If authentication was unsuccessful, `user_id` and `callback` are both `None`. """ for provider in self.password_providers: if hasattr(provider, "check_3pid_auth"): # This function is able to return a deferred that either # resolves None, meaning authentication failure, or upon # success, to a str (which is the user_id) or a tuple of # (user_id, callback_func), where callback_func should be run # after we've finished everything else result = yield provider.check_3pid_auth( medium, address, password) if result: # Check if the return value is a str or a tuple if isinstance(result, str): # If it's a str, set callback function to None result = (result, None) return result return None, None @defer.inlineCallbacks def _check_local_password(self, user_id, password): """Authenticate a user against the local password database. user_id is checked case insensitively, but will return None if there are multiple inexact matches. Args: user_id (unicode): complete @user:id password (unicode): the provided password Returns: Deferred[unicode] the canonical_user_id, or Deferred[None] if unknown user/bad password """ lookupres = yield self._find_user_id_and_pwd_hash(user_id) if not lookupres: return None (user_id, password_hash) = lookupres # If the password hash is None, the account has likely been deactivated if not password_hash: deactivated = yield self.store.get_user_deactivated_status(user_id) if deactivated: raise UserDeactivatedError("This account has been deactivated") result = yield self.validate_hash(password, password_hash) if not result: logger.warning("Failed password login for user %s", user_id) return None return user_id @defer.inlineCallbacks def validate_short_term_login_token_and_get_user_id(self, login_token): auth_api = self.hs.get_auth() user_id = None try: macaroon = pymacaroons.Macaroon.deserialize(login_token) user_id = auth_api.get_user_id_from_macaroon(macaroon) auth_api.validate_macaroon(macaroon, "login", user_id) except Exception: raise AuthError(403, "Invalid token", errcode=Codes.FORBIDDEN) yield self.auth.check_auth_blocking(user_id) return user_id @defer.inlineCallbacks def delete_access_token(self, access_token): """Invalidate a single access token Args: access_token (str): access token to be deleted Returns: Deferred """ user_info = yield self.auth.get_user_by_access_token(access_token) yield self.store.delete_access_token(access_token) # see if any of our auth providers want to know about this for provider in self.password_providers: if hasattr(provider, "on_logged_out"): yield provider.on_logged_out( user_id=str(user_info["user"]), device_id=user_info["device_id"], access_token=access_token, ) # delete pushers associated with this access token if user_info["token_id"] is not None: yield self.hs.get_pusherpool().remove_pushers_by_access_token( str(user_info["user"]), (user_info["token_id"], )) @defer.inlineCallbacks def delete_access_tokens_for_user(self, user_id, except_token_id=None, device_id=None): """Invalidate access tokens belonging to a user Args: user_id (str): ID of user the tokens belong to except_token_id (str|None): access_token ID which should *not* be deleted device_id (str|None): ID of device the tokens are associated with. If None, tokens associated with any device (or no device) will be deleted Returns: Deferred """ tokens_and_devices = yield self.store.user_delete_access_tokens( user_id, except_token_id=except_token_id, device_id=device_id) # see if any of our auth providers want to know about this for provider in self.password_providers: if hasattr(provider, "on_logged_out"): for token, token_id, device_id in tokens_and_devices: yield provider.on_logged_out(user_id=user_id, device_id=device_id, access_token=token) # delete pushers associated with the access tokens yield self.hs.get_pusherpool().remove_pushers_by_access_token( user_id, (token_id for _, token_id, _ in tokens_and_devices)) @defer.inlineCallbacks def add_threepid(self, user_id, medium, address, validated_at): # 'Canonicalise' email addresses down to lower case. # We've now moving towards the homeserver being the entity that # is responsible for validating threepids used for resetting passwords # on accounts, so in future Synapse will gain knowledge of specific # types (mediums) of threepid. For now, we still use the existing # infrastructure, but this is the start of synapse gaining knowledge # of specific types of threepid (and fixes the fact that checking # for the presence of an email address during password reset was # case sensitive). if medium == "email": address = address.lower() yield self.store.user_add_threepid(user_id, medium, address, validated_at, self.hs.get_clock().time_msec()) @defer.inlineCallbacks def delete_threepid(self, user_id, medium, address, id_server=None): """Attempts to unbind the 3pid on the identity servers and deletes it from the local database. Args: user_id (str) medium (str) address (str) id_server (str|None): Use the given identity server when unbinding any threepids. If None then will attempt to unbind using the identity server specified when binding (if known). Returns: Deferred[bool]: Returns True if successfully unbound the 3pid on the identity server, False if identity server doesn't support the unbind API. """ # 'Canonicalise' email addresses as per above if medium == "email": address = address.lower() identity_handler = self.hs.get_handlers().identity_handler result = yield identity_handler.try_unbind_threepid( user_id, { "medium": medium, "address": address, "id_server": id_server }) yield self.store.user_delete_threepid(user_id, medium, address) return result def _save_session(self, session): # TODO: Persistent storage logger.debug("Saving session %s", session) session["last_used"] = self.hs.get_clock().time_msec() self.sessions[session["id"]] = session def hash(self, password): """Computes a secure hash of password. Args: password (unicode): Password to hash. Returns: Deferred(unicode): Hashed password. """ def _do_hash(): # Normalise the Unicode in the password pw = unicodedata.normalize("NFKC", password) return bcrypt.hashpw( pw.encode("utf8") + self.hs.config.password_pepper.encode("utf8"), bcrypt.gensalt(self.bcrypt_rounds), ).decode("ascii") return defer_to_thread(self.hs.get_reactor(), _do_hash) def validate_hash(self, password, stored_hash): """Validates that self.hash(password) == stored_hash. Args: password (unicode): Password to hash. stored_hash (bytes): Expected hash value. Returns: Deferred(bool): Whether self.hash(password) == stored_hash. """ def _do_validate_hash(): # Normalise the Unicode in the password pw = unicodedata.normalize("NFKC", password) return bcrypt.checkpw( pw.encode("utf8") + self.hs.config.password_pepper.encode("utf8"), stored_hash, ) if stored_hash: if not isinstance(stored_hash, bytes): stored_hash = stored_hash.encode("ascii") return defer_to_thread(self.hs.get_reactor(), _do_validate_hash) else: return defer.succeed(False)
class LoginRestServlet(RestServlet): PATTERNS = client_patterns("/login$", v1=True) CAS_TYPE = "m.login.cas" SSO_TYPE = "m.login.sso" TOKEN_TYPE = "m.login.token" JWT_TYPE = "m.login.jwt" def __init__(self, hs): super(LoginRestServlet, self).__init__() self.hs = hs self.jwt_enabled = hs.config.jwt_enabled self.jwt_secret = hs.config.jwt_secret self.jwt_algorithm = hs.config.jwt_algorithm self.saml2_enabled = hs.config.saml2_enabled self.cas_enabled = hs.config.cas_enabled self.auth_handler = self.hs.get_auth_handler() self.registration_handler = hs.get_registration_handler() self.handlers = hs.get_handlers() self._clock = hs.get_clock() self._well_known_builder = WellKnownBuilder(hs) self._address_ratelimiter = Ratelimiter() self._account_ratelimiter = Ratelimiter() self._failed_attempts_ratelimiter = Ratelimiter() def on_GET(self, request): flows = [] if self.jwt_enabled: flows.append({"type": LoginRestServlet.JWT_TYPE}) if self.saml2_enabled: flows.append({"type": LoginRestServlet.SSO_TYPE}) flows.append({"type": LoginRestServlet.TOKEN_TYPE}) if self.cas_enabled: flows.append({"type": LoginRestServlet.SSO_TYPE}) # we advertise CAS for backwards compat, though MSC1721 renamed it # to SSO. flows.append({"type": LoginRestServlet.CAS_TYPE}) # While its valid for us to advertise this login type generally, # synapse currently only gives out these tokens as part of the # CAS login flow. # Generally we don't want to advertise login flows that clients # don't know how to implement, since they (currently) will always # fall back to the fallback API if they don't understand one of the # login flow types returned. flows.append({"type": LoginRestServlet.TOKEN_TYPE}) flows.extend(({ "type": t } for t in self.auth_handler.get_supported_login_types())) return 200, {"flows": flows} def on_OPTIONS(self, request): return 200, {} async def on_POST(self, request): self._address_ratelimiter.ratelimit( request.getClientIP(), time_now_s=self.hs.clock.time(), rate_hz=self.hs.config.rc_login_address.per_second, burst_count=self.hs.config.rc_login_address.burst_count, update=True, ) login_submission = parse_json_object_from_request(request) try: if self.jwt_enabled and (login_submission["type"] == LoginRestServlet.JWT_TYPE): result = await self.do_jwt_login(login_submission) elif login_submission["type"] == LoginRestServlet.TOKEN_TYPE: result = await self.do_token_login(login_submission) else: result = await self._do_other_login(login_submission) except KeyError: raise SynapseError(400, "Missing JSON keys.") well_known_data = self._well_known_builder.get_well_known() if well_known_data: result["well_known"] = well_known_data return 200, result async def _do_other_login(self, login_submission): """Handle non-token/saml/jwt logins Args: login_submission: Returns: dict: HTTP response """ # Log the request we got, but only certain fields to minimise the chance of # logging someone's password (even if they accidentally put it in the wrong # field) logger.info( "Got login request with identifier: %r, medium: %r, address: %r, user: %r", login_submission.get("identifier"), login_submission.get("medium"), login_submission.get("address"), login_submission.get("user"), ) login_submission_legacy_convert(login_submission) if "identifier" not in login_submission: raise SynapseError(400, "Missing param: identifier") identifier = login_submission["identifier"] if "type" not in identifier: raise SynapseError(400, "Login identifier has no type") # convert phone type identifiers to generic threepids if identifier["type"] == "m.id.phone": identifier = login_id_thirdparty_from_phone(identifier) # convert threepid identifiers to user IDs if identifier["type"] == "m.id.thirdparty": address = identifier.get("address") medium = identifier.get("medium") if medium is None or address is None: raise SynapseError(400, "Invalid thirdparty identifier") if medium == "email": # For emails, transform the address to lowercase. # We store all email addreses as lowercase in the DB. # (See add_threepid in synapse/handlers/auth.py) address = address.lower() # We also apply account rate limiting using the 3PID as a key, as # otherwise using 3PID bypasses the ratelimiting based on user ID. self._failed_attempts_ratelimiter.ratelimit( (medium, address), time_now_s=self._clock.time(), rate_hz=self.hs.config.rc_login_failed_attempts.per_second, burst_count=self.hs.config.rc_login_failed_attempts. burst_count, update=False, ) # Check for login providers that support 3pid login types ( canonical_user_id, callback_3pid, ) = await self.auth_handler.check_password_provider_3pid( medium, address, login_submission["password"]) if canonical_user_id: # Authentication through password provider and 3pid succeeded result = await self._complete_login(canonical_user_id, login_submission, callback_3pid) return result # No password providers were able to handle this 3pid # Check local store user_id = await self.hs.get_datastore().get_user_id_by_threepid( medium, address) if not user_id: logger.warning("unknown 3pid identifier medium %s, address %r", medium, address) # We mark that we've failed to log in here, as # `check_password_provider_3pid` might have returned `None` due # to an incorrect password, rather than the account not # existing. # # If it returned None but the 3PID was bound then we won't hit # this code path, which is fine as then the per-user ratelimit # will kick in below. self._failed_attempts_ratelimiter.can_do_action( (medium, address), time_now_s=self._clock.time(), rate_hz=self.hs.config.rc_login_failed_attempts.per_second, burst_count=self.hs.config.rc_login_failed_attempts. burst_count, update=True, ) raise LoginError(403, "", errcode=Codes.FORBIDDEN) identifier = {"type": "m.id.user", "user": user_id} # by this point, the identifier should be an m.id.user: if it's anything # else, we haven't understood it. if identifier["type"] != "m.id.user": raise SynapseError(400, "Unknown login identifier type") if "user" not in identifier: raise SynapseError(400, "User identifier is missing 'user' key") if identifier["user"].startswith("@"): qualified_user_id = identifier["user"] else: qualified_user_id = UserID(identifier["user"], self.hs.hostname).to_string() # Check if we've hit the failed ratelimit (but don't update it) self._failed_attempts_ratelimiter.ratelimit( qualified_user_id.lower(), time_now_s=self._clock.time(), rate_hz=self.hs.config.rc_login_failed_attempts.per_second, burst_count=self.hs.config.rc_login_failed_attempts.burst_count, update=False, ) try: canonical_user_id, callback = await self.auth_handler.validate_login( identifier["user"], login_submission) except LoginError: # The user has failed to log in, so we need to update the rate # limiter. Using `can_do_action` avoids us raising a ratelimit # exception and masking the LoginError. The actual ratelimiting # should have happened above. self._failed_attempts_ratelimiter.can_do_action( qualified_user_id.lower(), time_now_s=self._clock.time(), rate_hz=self.hs.config.rc_login_failed_attempts.per_second, burst_count=self.hs.config.rc_login_failed_attempts. burst_count, update=True, ) raise result = await self._complete_login(canonical_user_id, login_submission, callback) return result async def _complete_login(self, user_id, login_submission, callback=None, create_non_existant_users=False): """Called when we've successfully authed the user and now need to actually login them in (e.g. create devices). This gets called on all succesful logins. Applies the ratelimiting for succesful login attempts against an account. Args: user_id (str): ID of the user to register. login_submission (dict): Dictionary of login information. callback (func|None): Callback function to run after registration. create_non_existant_users (bool): Whether to create the user if they don't exist. Defaults to False. Returns: result (Dict[str,str]): Dictionary of account information after successful registration. """ # Before we actually log them in we check if they've already logged in # too often. This happens here rather than before as we don't # necessarily know the user before now. self._account_ratelimiter.ratelimit( user_id.lower(), time_now_s=self._clock.time(), rate_hz=self.hs.config.rc_login_account.per_second, burst_count=self.hs.config.rc_login_account.burst_count, update=True, ) if create_non_existant_users: user_id = await self.auth_handler.check_user_exists(user_id) if not user_id: user_id = await self.registration_handler.register_user( localpart=UserID.from_string(user_id).localpart) device_id = login_submission.get("device_id") initial_display_name = login_submission.get( "initial_device_display_name") device_id, access_token = await self.registration_handler.register_device( user_id, device_id, initial_display_name) result = { "user_id": user_id, "access_token": access_token, "home_server": self.hs.hostname, "device_id": device_id, } if callback is not None: await callback(result) return result async def do_token_login(self, login_submission): token = login_submission["token"] auth_handler = self.auth_handler user_id = await auth_handler.validate_short_term_login_token_and_get_user_id( token) result = await self._complete_login(user_id, login_submission) return result async def do_jwt_login(self, login_submission): token = login_submission.get("token", None) if token is None: raise LoginError(401, "Token field for JWT is missing", errcode=Codes.UNAUTHORIZED) import jwt from jwt.exceptions import InvalidTokenError try: payload = jwt.decode(token, self.jwt_secret, algorithms=[self.jwt_algorithm]) except jwt.ExpiredSignatureError: raise LoginError(401, "JWT expired", errcode=Codes.UNAUTHORIZED) except InvalidTokenError: raise LoginError(401, "Invalid JWT", errcode=Codes.UNAUTHORIZED) user = payload.get("sub", None) if user is None: raise LoginError(401, "Invalid JWT", errcode=Codes.UNAUTHORIZED) user_id = UserID(user, self.hs.hostname).to_string() result = await self._complete_login(user_id, login_submission, create_non_existant_users=True) return result
class AuthHandler(BaseHandler): SESSION_EXPIRE_MS = 48 * 60 * 60 * 1000 def __init__(self, hs): """ Args: hs (synapse.server.HomeServer): """ super(AuthHandler, self).__init__(hs) self.checkers = {} # type: Dict[str, UserInteractiveAuthChecker] for auth_checker_class in INTERACTIVE_AUTH_CHECKERS: inst = auth_checker_class(hs) if inst.is_enabled(): self.checkers[inst.AUTH_TYPE] = inst # type: ignore self.bcrypt_rounds = hs.config.bcrypt_rounds account_handler = ModuleApi(hs, self) self.password_providers = [ module(config=config, account_handler=account_handler) for module, config in hs.config.password_providers ] logger.info("Extra password_providers: %r", self.password_providers) self.hs = hs # FIXME better possibility to access registrationHandler later? self.macaroon_gen = hs.get_macaroon_generator() self._password_enabled = hs.config.password_enabled self._sso_enabled = (hs.config.cas_enabled or hs.config.saml2_enabled or hs.config.oidc_enabled) # we keep this as a list despite the O(N^2) implication so that we can # keep PASSWORD first and avoid confusing clients which pick the first # type in the list. (NB that the spec doesn't require us to do so and # clients which favour types that they don't understand over those that # they do are technically broken) login_types = [] if self._password_enabled: login_types.append(LoginType.PASSWORD) for provider in self.password_providers: if hasattr(provider, "get_supported_login_types"): for t in provider.get_supported_login_types().keys(): if t not in login_types: login_types.append(t) self._supported_login_types = login_types # Login types and UI Auth types have a heavy overlap, but are not # necessarily identical. Login types have SSO (and other login types) # added in the rest layer, see synapse.rest.client.v1.login.LoginRestServerlet.on_GET. ui_auth_types = login_types.copy() if self._sso_enabled: ui_auth_types.append(LoginType.SSO) self._supported_ui_auth_types = ui_auth_types # Ratelimiter for failed auth during UIA. Uses same ratelimit config # as per `rc_login.failed_attempts`. self._failed_uia_attempts_ratelimiter = Ratelimiter() self._clock = self.hs.get_clock() # Expire old UI auth sessions after a period of time. if hs.config.worker_app is None: self._clock.looping_call( run_as_background_process, 5 * 60 * 1000, "expire_old_sessions", self._expire_old_sessions, ) # Load the SSO HTML templates. # The following template is shown to the user during a client login via SSO, # after the SSO completes and before redirecting them back to their client. # It notifies the user they are about to give access to their matrix account # to the client. self._sso_redirect_confirm_template = load_jinja2_templates( hs.config.sso_template_dir, ["sso_redirect_confirm.html"], )[0] # The following template is shown during user interactive authentication # in the fallback auth scenario. It notifies the user that they are # authenticating for an operation to occur on their account. self._sso_auth_confirm_template = load_jinja2_templates( hs.config.sso_template_dir, ["sso_auth_confirm.html"], )[0] # The following template is shown after a successful user interactive # authentication session. It tells the user they can close the window. self._sso_auth_success_template = hs.config.sso_auth_success_template # The following template is shown during the SSO authentication process if # the account is deactivated. self._sso_account_deactivated_template = ( hs.config.sso_account_deactivated_template) self._server_name = hs.config.server_name # cast to tuple for use with str.startswith self._whitelisted_sso_clients = tuple(hs.config.sso_client_whitelist) async def validate_user_via_ui_auth( self, requester: Requester, request: SynapseRequest, request_body: Dict[str, Any], clientip: str, description: str, ) -> dict: """ Checks that the user is who they claim to be, via a UI auth. This is used for things like device deletion and password reset where the user already has a valid access token, but we want to double-check that it isn't stolen by re-authenticating them. Args: requester: The user, as given by the access token request: The request sent by the client. request_body: The body of the request sent by the client clientip: The IP address of the client. description: A human readable string to be displayed to the user that describes the operation happening on their account. Returns: The parameters for this request (which may have been given only in a previous call). Raises: InteractiveAuthIncompleteError if the client has not yet completed any of the permitted login flows AuthError if the client has completed a login flow, and it gives a different user to `requester` LimitExceededError if the ratelimiter's failed request count for this user is too high to proceed """ user_id = requester.user.to_string() # Check if we should be ratelimited due to too many previous failed attempts self._failed_uia_attempts_ratelimiter.ratelimit( user_id, time_now_s=self._clock.time(), rate_hz=self.hs.config.rc_login_failed_attempts.per_second, burst_count=self.hs.config.rc_login_failed_attempts.burst_count, update=False, ) # build a list of supported flows flows = [[login_type] for login_type in self._supported_ui_auth_types] try: result, params, _ = await self.check_auth(flows, request, request_body, clientip, description) except LoginError: # Update the ratelimite to say we failed (`can_do_action` doesn't raise). self._failed_uia_attempts_ratelimiter.can_do_action( user_id, time_now_s=self._clock.time(), rate_hz=self.hs.config.rc_login_failed_attempts.per_second, burst_count=self.hs.config.rc_login_failed_attempts. burst_count, update=True, ) raise # find the completed login type for login_type in self._supported_ui_auth_types: if login_type not in result: continue user_id = result[login_type] break else: # this can't happen raise Exception( "check_auth returned True but no successful login type") # check that the UI auth matched the access token if user_id != requester.user.to_string(): raise AuthError(403, "Invalid auth") return params def get_enabled_auth_types(self): """Return the enabled user-interactive authentication types Returns the UI-Auth types which are supported by the homeserver's current config. """ return self.checkers.keys() async def check_auth( self, flows: List[List[str]], request: SynapseRequest, clientdict: Dict[str, Any], clientip: str, description: str, ) -> Tuple[dict, dict, str]: """ Takes a dictionary sent by the client in the login / registration protocol and handles the User-Interactive Auth flow. If no auth flows have been completed successfully, raises an InteractiveAuthIncompleteError. To handle this, you can use synapse.rest.client.v2_alpha._base.interactive_auth_handler as a decorator. Args: flows: A list of login flows. Each flow is an ordered list of strings representing auth-types. At least one full flow must be completed in order for auth to be successful. request: The request sent by the client. clientdict: The dictionary from the client root level, not the 'auth' key: this method prompts for auth if none is sent. clientip: The IP address of the client. description: A human readable string to be displayed to the user that describes the operation happening on their account. Returns: A tuple of (creds, params, session_id). 'creds' contains the authenticated credentials of each stage. 'params' contains the parameters for this request (which may have been given only in a previous call). 'session_id' is the ID of this session, either passed in by the client or assigned by this call Raises: InteractiveAuthIncompleteError if the client has not yet completed all the stages in any of the permitted flows. """ authdict = None sid = None # type: Optional[str] if clientdict and "auth" in clientdict: authdict = clientdict["auth"] del clientdict["auth"] if "session" in authdict: sid = authdict["session"] # Convert the URI and method to strings. uri = request.uri.decode("utf-8") method = request.uri.decode("utf-8") # If there's no session ID, create a new session. if not sid: session = await self.store.create_ui_auth_session( clientdict, uri, method, description) else: try: session = await self.store.get_ui_auth_session(sid) except StoreError: raise SynapseError(400, "Unknown session ID: %s" % (sid, )) # If the client provides parameters, update what is persisted, # otherwise use whatever was last provided. # # This was designed to allow the client to omit the parameters # and just supply the session in subsequent calls so it split # auth between devices by just sharing the session, (eg. so you # could continue registration from your phone having clicked the # email auth link on there). It's probably too open to abuse # because it lets unauthenticated clients store arbitrary objects # on a homeserver. # # Revisit: Assuming the REST APIs do sensible validation, the data # isn't arbitrary. # # Note that the registration endpoint explicitly removes the # "initial_device_display_name" parameter if it is provided # without a "password" parameter. See the changes to # synapse.rest.client.v2_alpha.register.RegisterRestServlet.on_POST # in commit 544722bad23fc31056b9240189c3cbbbf0ffd3f9. if not clientdict: clientdict = session.clientdict # Ensure that the queried operation does not vary between stages of # the UI authentication session. This is done by generating a stable # comparator and storing it during the initial query. Subsequent # queries ensure that this comparator has not changed. # # The comparator is based on the requested URI and HTTP method. The # client dict (minus the auth dict) should also be checked, but some # clients are not spec compliant, just warn for now if the client # dict changes. if (session.uri, session.method) != (uri, method): raise SynapseError( 403, "Requested operation has changed during the UI authentication session.", ) if session.clientdict != clientdict: logger.warning( "Requested operation has changed during the UI " "authentication session. A future version of Synapse " "will remove this capability.") # For backwards compatibility, changes to the client dict are # persisted as clients modify them throughout their user interactive # authentication flow. await self.store.set_ui_auth_clientdict(sid, clientdict) if not authdict: raise InteractiveAuthIncompleteError( self._auth_dict_for_flows(flows, session.session_id)) # check auth type currently being presented errordict = {} # type: Dict[str, Any] if "type" in authdict: login_type = authdict["type"] # type: str try: result = await self._check_auth_dict(authdict, clientip) if result: await self.store.mark_ui_auth_stage_complete( session.session_id, login_type, result) except LoginError as e: if login_type == LoginType.EMAIL_IDENTITY: # riot used to have a bug where it would request a new # validation token (thus sending a new email) each time it # got a 401 with a 'flows' field. # (https://github.com/vector-im/vector-web/issues/2447). # # Grandfather in the old behaviour for now to avoid # breaking old riot deployments. raise # this step failed. Merge the error dict into the response # so that the client can have another go. errordict = e.error_dict() creds = await self.store.get_completed_ui_auth_stages( session.session_id) for f in flows: if len(set(f) - set(creds)) == 0: # it's very useful to know what args are stored, but this can # include the password in the case of registering, so only log # the keys (confusingly, clientdict may contain a password # param, creds is just what the user authed as for UI auth # and is not sensitive). logger.info( "Auth completed with creds: %r. Client dict has keys: %r", creds, list(clientdict), ) return creds, clientdict, session.session_id ret = self._auth_dict_for_flows(flows, session.session_id) ret["completed"] = list(creds) ret.update(errordict) raise InteractiveAuthIncompleteError(ret) async def add_oob_auth(self, stagetype: str, authdict: Dict[str, Any], clientip: str) -> bool: """ Adds the result of out-of-band authentication into an existing auth session. Currently used for adding the result of fallback auth. """ if stagetype not in self.checkers: raise LoginError(400, "", Codes.MISSING_PARAM) if "session" not in authdict: raise LoginError(400, "", Codes.MISSING_PARAM) result = await self.checkers[stagetype].check_auth(authdict, clientip) if result: await self.store.mark_ui_auth_stage_complete( authdict["session"], stagetype, result) return True return False def get_session_id(self, clientdict: Dict[str, Any]) -> Optional[str]: """ Gets the session ID for a client given the client dictionary Args: clientdict: The dictionary sent by the client in the request Returns: The string session ID the client sent. If the client did not send a session ID, returns None. """ sid = None if clientdict and "auth" in clientdict: authdict = clientdict["auth"] if "session" in authdict: sid = authdict["session"] return sid async def set_session_data(self, session_id: str, key: str, value: Any) -> None: """ Store a key-value pair into the sessions data associated with this request. This data is stored server-side and cannot be modified by the client. Args: session_id: The ID of this session as returned from check_auth key: The key to store the data under value: The data to store """ try: await self.store.set_ui_auth_session_data(session_id, key, value) except StoreError: raise SynapseError(400, "Unknown session ID: %s" % (session_id, )) async def get_session_data(self, session_id: str, key: str, default: Optional[Any] = None) -> Any: """ Retrieve data stored with set_session_data Args: session_id: The ID of this session as returned from check_auth key: The key to store the data under default: Value to return if the key has not been set """ try: return await self.store.get_ui_auth_session_data( session_id, key, default) except StoreError: raise SynapseError(400, "Unknown session ID: %s" % (session_id, )) async def _expire_old_sessions(self): """ Invalidate any user interactive authentication sessions that have expired. """ now = self._clock.time_msec() expiration_time = now - self.SESSION_EXPIRE_MS await self.store.delete_old_ui_auth_sessions(expiration_time) async def _check_auth_dict(self, authdict: Dict[str, Any], clientip: str) -> Union[Dict[str, Any], str]: """Attempt to validate the auth dict provided by a client Args: authdict: auth dict provided by the client clientip: IP address of the client Returns: Result of the stage verification. Raises: StoreError if there was a problem accessing the database SynapseError if there was a problem with the request LoginError if there was an authentication problem. """ login_type = authdict["type"] checker = self.checkers.get(login_type) if checker is not None: res = await checker.check_auth(authdict, clientip=clientip) return res # build a v1-login-style dict out of the authdict and fall back to the # v1 code user_id = authdict.get("user") if user_id is None: raise SynapseError(400, "", Codes.MISSING_PARAM) (canonical_id, callback) = await self.validate_login(user_id, authdict) return canonical_id def _get_params_recaptcha(self) -> dict: return {"public_key": self.hs.config.recaptcha_public_key} def _get_params_terms(self) -> dict: return { "policies": { "privacy_policy": { "version": self.hs.config.user_consent_version, "en": { "name": self.hs.config.user_consent_policy_name, "url": "%s_matrix/consent?v=%s" % ( self.hs.config.public_baseurl, self.hs.config.user_consent_version, ), }, } } } def _auth_dict_for_flows( self, flows: List[List[str]], session_id: str, ) -> Dict[str, Any]: public_flows = [] for f in flows: public_flows.append(f) get_params = { LoginType.RECAPTCHA: self._get_params_recaptcha, LoginType.TERMS: self._get_params_terms, } params = {} # type: Dict[str, Any] for f in public_flows: for stage in f: if stage in get_params and stage not in params: params[stage] = get_params[stage]() return { "session": session_id, "flows": [{ "stages": f } for f in public_flows], "params": params, } async def get_access_token_for_user_id(self, user_id: str, device_id: Optional[str], valid_until_ms: Optional[int]): """ Creates a new access token for the user with the given user ID. The user is assumed to have been authenticated by some other machanism (e.g. CAS), and the user_id converted to the canonical case. The device will be recorded in the table if it is not there already. Args: user_id: canonical User ID device_id: the device ID to associate with the tokens. None to leave the tokens unassociated with a device (deprecated: we should always have a device ID) valid_until_ms: when the token is valid until. None for no expiry. Returns: The access token for the user's session. Raises: StoreError if there was a problem storing the token. """ fmt_expiry = "" if valid_until_ms is not None: fmt_expiry = time.strftime(" until %Y-%m-%d %H:%M:%S", time.localtime(valid_until_ms / 1000.0)) logger.info("Logging in user %s on device %s%s", user_id, device_id, fmt_expiry) await self.auth.check_auth_blocking(user_id) access_token = self.macaroon_gen.generate_access_token(user_id) await self.store.add_access_token_to_user(user_id, access_token, device_id, valid_until_ms) # the device *should* have been registered before we got here; however, # it's possible we raced against a DELETE operation. The thing we # really don't want is active access_tokens without a record of the # device, so we double-check it here. if device_id is not None: try: await self.store.get_device(user_id, device_id) except StoreError: await self.store.delete_access_token(access_token) raise StoreError(400, "Login raced against device deletion") return access_token async def check_user_exists(self, user_id: str) -> Optional[str]: """ Checks to see if a user with the given id exists. Will check case insensitively, but return None if there are multiple inexact matches. Args: user_id: complete @user:id Returns: The canonical_user_id, or None if zero or multiple matches """ res = await self._find_user_id_and_pwd_hash(user_id) if res is not None: return res[0] return None async def _find_user_id_and_pwd_hash( self, user_id: str) -> Optional[Tuple[str, str]]: """Checks to see if a user with the given id exists. Will check case insensitively, but will return None if there are multiple inexact matches. Returns: A 2-tuple of `(canonical_user_id, password_hash)` or `None` if there is not exactly one match """ user_infos = await self.store.get_users_by_id_case_insensitive(user_id) result = None if not user_infos: logger.warning("Attempted to login as %s but they do not exist", user_id) elif len(user_infos) == 1: # a single match (possibly not exact) result = user_infos.popitem() elif user_id in user_infos: # multiple matches, but one is exact result = (user_id, user_infos[user_id]) else: # multiple matches, none of them exact logger.warning( "Attempted to login as %s but it matches more than one user " "inexactly: %r", user_id, user_infos.keys(), ) return result def get_supported_login_types(self) -> Iterable[str]: """Get a the login types supported for the /login API By default this is just 'm.login.password' (unless password_enabled is False in the config file), but password auth providers can provide other login types. Returns: login types """ return self._supported_login_types async def validate_login( self, username: str, login_submission: Dict[str, Any] ) -> Tuple[str, Optional[Callable[[Dict[str, str]], None]]]: """Authenticates the user for the /login API Also used by the user-interactive auth flow to validate m.login.password auth types. Args: username: username supplied by the user login_submission: the whole of the login submission (including 'type' and other relevant fields) Returns: A tuple of the canonical user id, and optional callback to be called once the access token and device id are issued Raises: StoreError if there was a problem accessing the database SynapseError if there was a problem with the request LoginError if there was an authentication problem. """ if username.startswith("@"): qualified_user_id = username else: qualified_user_id = UserID(username, self.hs.hostname).to_string() login_type = login_submission.get("type") known_login_type = False # special case to check for "password" for the check_password interface # for the auth providers password = login_submission.get("password") if login_type == LoginType.PASSWORD: if not self._password_enabled: raise SynapseError(400, "Password login has been disabled.") if not password: raise SynapseError(400, "Missing parameter: password") for provider in self.password_providers: if hasattr(provider, "check_password") and login_type == LoginType.PASSWORD: known_login_type = True is_valid = await provider.check_password( qualified_user_id, password) if is_valid: return qualified_user_id, None if not hasattr(provider, "get_supported_login_types") or not hasattr( provider, "check_auth"): # this password provider doesn't understand custom login types continue supported_login_types = provider.get_supported_login_types() if login_type not in supported_login_types: # this password provider doesn't understand this login type continue known_login_type = True login_fields = supported_login_types[login_type] missing_fields = [] login_dict = {} for f in login_fields: if f not in login_submission: missing_fields.append(f) else: login_dict[f] = login_submission[f] if missing_fields: raise SynapseError( 400, "Missing parameters for login type %s: %s" % (login_type, missing_fields), ) result = await provider.check_auth(username, login_type, login_dict) if result: if isinstance(result, str): result = (result, None) return result if login_type == LoginType.PASSWORD and self.hs.config.password_localdb_enabled: known_login_type = True canonical_user_id = await self._check_local_password( qualified_user_id, password # type: ignore ) if canonical_user_id: return canonical_user_id, None if not known_login_type: raise SynapseError(400, "Unknown login type %s" % login_type) # We raise a 403 here, but note that if we're doing user-interactive # login, it turns all LoginErrors into a 401 anyway. raise LoginError(403, "Invalid password", errcode=Codes.FORBIDDEN) async def check_password_provider_3pid( self, medium: str, address: str, password: str ) -> Tuple[Optional[str], Optional[Callable[[Dict[str, str]], None]]]: """Check if a password provider is able to validate a thirdparty login Args: medium: The medium of the 3pid (ex. email). address: The address of the 3pid (ex. [email protected]). password: The password of the user. Returns: A tuple of `(user_id, callback)`. If authentication is successful, `user_id`is the authenticated, canonical user ID. `callback` is then either a function to be later run after the server has completed login/registration, or `None`. If authentication was unsuccessful, `user_id` and `callback` are both `None`. """ for provider in self.password_providers: if hasattr(provider, "check_3pid_auth"): # This function is able to return a deferred that either # resolves None, meaning authentication failure, or upon # success, to a str (which is the user_id) or a tuple of # (user_id, callback_func), where callback_func should be run # after we've finished everything else result = await provider.check_3pid_auth( medium, address, password) if result: # Check if the return value is a str or a tuple if isinstance(result, str): # If it's a str, set callback function to None result = (result, None) return result return None, None async def _check_local_password(self, user_id: str, password: str) -> Optional[str]: """Authenticate a user against the local password database. user_id is checked case insensitively, but will return None if there are multiple inexact matches. Args: user_id: complete @user:id password: the provided password Returns: The canonical_user_id, or None if unknown user/bad password """ lookupres = await self._find_user_id_and_pwd_hash(user_id) if not lookupres: return None (user_id, password_hash) = lookupres # If the password hash is None, the account has likely been deactivated if not password_hash: deactivated = await self.store.get_user_deactivated_status(user_id) if deactivated: raise UserDeactivatedError("This account has been deactivated") result = await self.validate_hash(password, password_hash) if not result: logger.warning("Failed password login for user %s", user_id) return None return user_id async def validate_short_term_login_token_and_get_user_id( self, login_token: str): auth_api = self.hs.get_auth() user_id = None try: macaroon = pymacaroons.Macaroon.deserialize(login_token) user_id = auth_api.get_user_id_from_macaroon(macaroon) auth_api.validate_macaroon(macaroon, "login", user_id) except Exception: raise AuthError(403, "Invalid token", errcode=Codes.FORBIDDEN) await self.auth.check_auth_blocking(user_id) return user_id async def delete_access_token(self, access_token: str): """Invalidate a single access token Args: access_token: access token to be deleted """ user_info = await self.auth.get_user_by_access_token(access_token) await self.store.delete_access_token(access_token) # see if any of our auth providers want to know about this for provider in self.password_providers: if hasattr(provider, "on_logged_out"): await provider.on_logged_out( user_id=str(user_info["user"]), device_id=user_info["device_id"], access_token=access_token, ) # delete pushers associated with this access token if user_info["token_id"] is not None: await self.hs.get_pusherpool().remove_pushers_by_access_token( str(user_info["user"]), (user_info["token_id"], )) async def delete_access_tokens_for_user( self, user_id: str, except_token_id: Optional[str] = None, device_id: Optional[str] = None, ): """Invalidate access tokens belonging to a user Args: user_id: ID of user the tokens belong to except_token_id: access_token ID which should *not* be deleted device_id: ID of device the tokens are associated with. If None, tokens associated with any device (or no device) will be deleted """ tokens_and_devices = await self.store.user_delete_access_tokens( user_id, except_token_id=except_token_id, device_id=device_id) # see if any of our auth providers want to know about this for provider in self.password_providers: if hasattr(provider, "on_logged_out"): for token, token_id, device_id in tokens_and_devices: await provider.on_logged_out(user_id=user_id, device_id=device_id, access_token=token) # delete pushers associated with the access tokens await self.hs.get_pusherpool().remove_pushers_by_access_token( user_id, (token_id for _, token_id, _ in tokens_and_devices)) async def add_threepid(self, user_id: str, medium: str, address: str, validated_at: int): # check if medium has a valid value if medium not in ["email", "msisdn"]: raise SynapseError( code=400, msg=("'%s' is not a valid value for 'medium'" % (medium, )), errcode=Codes.INVALID_PARAM, ) # 'Canonicalise' email addresses down to lower case. # We've now moving towards the homeserver being the entity that # is responsible for validating threepids used for resetting passwords # on accounts, so in future Synapse will gain knowledge of specific # types (mediums) of threepid. For now, we still use the existing # infrastructure, but this is the start of synapse gaining knowledge # of specific types of threepid (and fixes the fact that checking # for the presence of an email address during password reset was # case sensitive). if medium == "email": address = address.lower() await self.store.user_add_threepid(user_id, medium, address, validated_at, self.hs.get_clock().time_msec()) async def delete_threepid(self, user_id: str, medium: str, address: str, id_server: Optional[str] = None) -> bool: """Attempts to unbind the 3pid on the identity servers and deletes it from the local database. Args: user_id: ID of user to remove the 3pid from. medium: The medium of the 3pid being removed: "email" or "msisdn". address: The 3pid address to remove. id_server: Use the given identity server when unbinding any threepids. If None then will attempt to unbind using the identity server specified when binding (if known). Returns: Returns True if successfully unbound the 3pid on the identity server, False if identity server doesn't support the unbind API. """ # 'Canonicalise' email addresses as per above if medium == "email": address = address.lower() identity_handler = self.hs.get_handlers().identity_handler result = await identity_handler.try_unbind_threepid( user_id, { "medium": medium, "address": address, "id_server": id_server }) await self.store.user_delete_threepid(user_id, medium, address) return result async def hash(self, password: str) -> str: """Computes a secure hash of password. Args: password: Password to hash. Returns: Hashed password. """ def _do_hash(): # Normalise the Unicode in the password pw = unicodedata.normalize("NFKC", password) return bcrypt.hashpw( pw.encode("utf8") + self.hs.config.password_pepper.encode("utf8"), bcrypt.gensalt(self.bcrypt_rounds), ).decode("ascii") return await defer_to_thread(self.hs.get_reactor(), _do_hash) async def validate_hash(self, password: str, stored_hash: Union[bytes, str]) -> bool: """Validates that self.hash(password) == stored_hash. Args: password: Password to hash. stored_hash: Expected hash value. Returns: Whether self.hash(password) == stored_hash. """ def _do_validate_hash(): # Normalise the Unicode in the password pw = unicodedata.normalize("NFKC", password) return bcrypt.checkpw( pw.encode("utf8") + self.hs.config.password_pepper.encode("utf8"), stored_hash, ) if stored_hash: if not isinstance(stored_hash, bytes): stored_hash = stored_hash.encode("ascii") return await defer_to_thread(self.hs.get_reactor(), _do_validate_hash) else: return False async def start_sso_ui_auth(self, redirect_url: str, session_id: str) -> str: """ Get the HTML for the SSO redirect confirmation page. Args: redirect_url: The URL to redirect to the SSO provider. session_id: The user interactive authentication session ID. Returns: The HTML to render. """ try: session = await self.store.get_ui_auth_session(session_id) except StoreError: raise SynapseError(400, "Unknown session ID: %s" % (session_id, )) return self._sso_auth_confirm_template.render( description=session.description, redirect_url=redirect_url, ) async def complete_sso_ui_auth( self, registered_user_id: str, session_id: str, request: SynapseRequest, ): """Having figured out a mxid for this user, complete the HTTP request Args: registered_user_id: The registered user ID to complete SSO login for. request: The request to complete. client_redirect_url: The URL to which to redirect the user at the end of the process. """ # Mark the stage of the authentication as successful. # Save the user who authenticated with SSO, this will be used to ensure # that the account be modified is also the person who logged in. await self.store.mark_ui_auth_stage_complete(session_id, LoginType.SSO, registered_user_id) # Render the HTML and return. html_bytes = self._sso_auth_success_template.encode("utf-8") request.setResponseCode(200) request.setHeader(b"Content-Type", b"text/html; charset=utf-8") request.setHeader(b"Content-Length", b"%d" % (len(html_bytes), )) request.write(html_bytes) finish_request(request) async def complete_sso_login( self, registered_user_id: str, request: SynapseRequest, client_redirect_url: str, ): """Having figured out a mxid for this user, complete the HTTP request Args: registered_user_id: The registered user ID to complete SSO login for. request: The request to complete. client_redirect_url: The URL to which to redirect the user at the end of the process. """ # If the account has been deactivated, do not proceed with the login # flow. deactivated = await self.store.get_user_deactivated_status( registered_user_id) if deactivated: html_bytes = self._sso_account_deactivated_template.encode("utf-8") request.setResponseCode(403) request.setHeader(b"Content-Type", b"text/html; charset=utf-8") request.setHeader(b"Content-Length", b"%d" % (len(html_bytes), )) request.write(html_bytes) finish_request(request) return self._complete_sso_login(registered_user_id, request, client_redirect_url) def _complete_sso_login( self, registered_user_id: str, request: SynapseRequest, client_redirect_url: str, ): """ The synchronous portion of complete_sso_login. This exists purely for backwards compatibility of synapse.module_api.ModuleApi. """ # Create a login token login_token = self.macaroon_gen.generate_short_term_login_token( registered_user_id) # Append the login token to the original redirect URL (i.e. with its query # parameters kept intact) to build the URL to which the template needs to # redirect the users once they have clicked on the confirmation link. redirect_url = self.add_query_param_to_url(client_redirect_url, "loginToken", login_token) # if the client is whitelisted, we can redirect straight to it if client_redirect_url.startswith(self._whitelisted_sso_clients): request.redirect(redirect_url) finish_request(request) return # Otherwise, serve the redirect confirmation page. # Remove the query parameters from the redirect URL to get a shorter version of # it. This is only to display a human-readable URL in the template, but not the # URL we redirect users to. redirect_url_no_params = client_redirect_url.split("?")[0] html_bytes = self._sso_redirect_confirm_template.render( display_url=redirect_url_no_params, redirect_url=redirect_url, server_name=self._server_name, ).encode("utf-8") request.setResponseCode(200) request.setHeader(b"Content-Type", b"text/html; charset=utf-8") request.setHeader(b"Content-Length", b"%d" % (len(html_bytes), )) request.write(html_bytes) finish_request(request) @staticmethod def add_query_param_to_url(url: str, param_name: str, param: Any): url_parts = list(urllib.parse.urlparse(url)) query = dict(urllib.parse.parse_qsl(url_parts[4])) query.update({param_name: param}) url_parts[4] = urllib.parse.urlencode(query) return urllib.parse.urlunparse(url_parts)
class FederationHandlerRegistry: """Allows classes to register themselves as handlers for a given EDU or query type for incoming federation traffic. """ def __init__(self, hs: "HomeServer"): self.config = hs.config self.clock = hs.get_clock() self._instance_name = hs.get_instance_name() # These are safe to load in monolith mode, but will explode if we try # and use them. However we have guards before we use them to ensure that # we don't route to ourselves, and in monolith mode that will always be # the case. self._get_query_client = ReplicationGetQueryRestServlet.make_client(hs) self._send_edu = ReplicationFederationSendEduRestServlet.make_client( hs) self.edu_handlers = ( {}) # type: Dict[str, Callable[[str, dict], Awaitable[None]]] self.query_handlers = { } # type: Dict[str, Callable[[dict], Awaitable[None]]] # Map from type to instance names that we should route EDU handling to. # We randomly choose one instance from the list to route to for each new # EDU received. self._edu_type_to_instance = {} # type: Dict[str, List[str]] # A rate limiter for incoming room key requests per origin. self._room_key_request_rate_limiter = Ratelimiter( clock=self.clock, rate_hz=self.config.rc_key_requests.per_second, burst_count=self.config.rc_key_requests.burst_count, ) def register_edu_handler(self, edu_type: str, handler: Callable[[str, JsonDict], Awaitable[None]]): """Sets the handler callable that will be used to handle an incoming federation EDU of the given type. Args: edu_type: The type of the incoming EDU to register handler for handler: A callable invoked on incoming EDU of the given type. The arguments are the origin server name and the EDU contents. """ if edu_type in self.edu_handlers: raise KeyError("Already have an EDU handler for %s" % (edu_type, )) logger.info("Registering federation EDU handler for %r", edu_type) self.edu_handlers[edu_type] = handler def register_query_handler(self, query_type: str, handler: Callable[[dict], defer.Deferred]): """Sets the handler callable that will be used to handle an incoming federation query of the given type. Args: query_type: Category name of the query, which should match the string used by make_query. handler: Invoked to handle incoming queries of this type. The return will be yielded on and the result used as the response to the query request. """ if query_type in self.query_handlers: raise KeyError("Already have a Query handler for %s" % (query_type, )) logger.info("Registering federation query handler for %r", query_type) self.query_handlers[query_type] = handler def register_instance_for_edu(self, edu_type: str, instance_name: str): """Register that the EDU handler is on a different instance than master.""" self._edu_type_to_instance[edu_type] = [instance_name] def register_instances_for_edu(self, edu_type: str, instance_names: List[str]): """Register that the EDU handler is on multiple instances.""" self._edu_type_to_instance[edu_type] = instance_names async def on_edu(self, edu_type: str, origin: str, content: dict): if not self.config.use_presence and edu_type == EduTypes.Presence: return # If the incoming room key requests from a particular origin are over # the limit, drop them. if (edu_type == EduTypes.RoomKeyRequest and not self._room_key_request_rate_limiter.can_do_action(origin)): return # Check if we have a handler on this instance handler = self.edu_handlers.get(edu_type) if handler: with start_active_span_from_edu(content, "handle_edu"): try: await handler(origin, content) except SynapseError as e: logger.info("Failed to handle edu %r: %r", edu_type, e) except Exception: logger.exception("Failed to handle edu %r", edu_type) return # Check if we can route it somewhere else that isn't us instances = self._edu_type_to_instance.get(edu_type, ["master"]) if self._instance_name not in instances: # Pick an instance randomly so that we don't overload one. route_to = random.choice(instances) try: await self._send_edu( instance_name=route_to, edu_type=edu_type, origin=origin, content=content, ) except SynapseError as e: logger.info("Failed to handle edu %r: %r", edu_type, e) except Exception: logger.exception("Failed to handle edu %r", edu_type) return # Oh well, let's just log and move on. logger.warning("No handler registered for EDU type %s", edu_type) async def on_query(self, query_type: str, args: dict): handler = self.query_handlers.get(query_type) if handler: return await handler(args) # Check if we can route it somewhere else that isn't us if self._instance_name == "master": return await self._get_query_client(query_type=query_type, args=args) # Uh oh, no handler! Let's raise an exception so the request returns an # error. logger.warning("No handler registered for query type %s", query_type) raise NotFoundError("No handler for Query type '%s'" % (query_type, ))
class LoginRestServlet(RestServlet): PATTERNS = client_patterns("/login$", v1=True) CAS_TYPE = "m.login.cas" SSO_TYPE = "m.login.sso" TOKEN_TYPE = "m.login.token" JWT_TYPE = "org.matrix.login.jwt" JWT_TYPE_DEPRECATED = "m.login.jwt" APPSERVICE_TYPE = "uk.half-shot.msc2778.login.application_service" def __init__(self, hs): super().__init__() self.hs = hs # JWT configuration variables. self.jwt_enabled = hs.config.jwt_enabled self.jwt_secret = hs.config.jwt_secret self.jwt_algorithm = hs.config.jwt_algorithm self.jwt_issuer = hs.config.jwt_issuer self.jwt_audiences = hs.config.jwt_audiences # SSO configuration. self.saml2_enabled = hs.config.saml2_enabled self.cas_enabled = hs.config.cas_enabled self.oidc_enabled = hs.config.oidc_enabled self.auth = hs.get_auth() self.auth_handler = self.hs.get_auth_handler() self.registration_handler = hs.get_registration_handler() self._well_known_builder = WellKnownBuilder(hs) self._address_ratelimiter = Ratelimiter( clock=hs.get_clock(), rate_hz=self.hs.config.rc_login_address.per_second, burst_count=self.hs.config.rc_login_address.burst_count, ) self._account_ratelimiter = Ratelimiter( clock=hs.get_clock(), rate_hz=self.hs.config.rc_login_account.per_second, burst_count=self.hs.config.rc_login_account.burst_count, ) self._failed_attempts_ratelimiter = Ratelimiter( clock=hs.get_clock(), rate_hz=self.hs.config.rc_login_failed_attempts.per_second, burst_count=self.hs.config.rc_login_failed_attempts.burst_count, ) def on_GET(self, request: SynapseRequest): flows = [] if self.jwt_enabled: flows.append({"type": LoginRestServlet.JWT_TYPE}) flows.append({"type": LoginRestServlet.JWT_TYPE_DEPRECATED}) if self.cas_enabled: # we advertise CAS for backwards compat, though MSC1721 renamed it # to SSO. flows.append({"type": LoginRestServlet.CAS_TYPE}) if self.cas_enabled or self.saml2_enabled or self.oidc_enabled: flows.append({"type": LoginRestServlet.SSO_TYPE}) # While its valid for us to advertise this login type generally, # synapse currently only gives out these tokens as part of the # SSO login flow. # Generally we don't want to advertise login flows that clients # don't know how to implement, since they (currently) will always # fall back to the fallback API if they don't understand one of the # login flow types returned. flows.append({"type": LoginRestServlet.TOKEN_TYPE}) flows.extend(({ "type": t } for t in self.auth_handler.get_supported_login_types())) flows.append({"type": LoginRestServlet.APPSERVICE_TYPE}) return 200, {"flows": flows} async def on_POST(self, request: SynapseRequest): self._address_ratelimiter.ratelimit(request.getClientIP()) login_submission = parse_json_object_from_request(request) try: if login_submission["type"] == LoginRestServlet.APPSERVICE_TYPE: appservice = self.auth.get_appservice_by_req(request) result = await self._do_appservice_login( login_submission, appservice) elif self.jwt_enabled and ( login_submission["type"] == LoginRestServlet.JWT_TYPE or login_submission["type"] == LoginRestServlet.JWT_TYPE_DEPRECATED): result = await self._do_jwt_login(login_submission) elif login_submission["type"] == LoginRestServlet.TOKEN_TYPE: result = await self._do_token_login(login_submission) else: result = await self._do_other_login(login_submission) except KeyError: raise SynapseError(400, "Missing JSON keys.") well_known_data = self._well_known_builder.get_well_known() if well_known_data: result["well_known"] = well_known_data return 200, result def _get_qualified_user_id(self, identifier): if identifier["type"] != "m.id.user": raise SynapseError(400, "Unknown login identifier type") if "user" not in identifier: raise SynapseError(400, "User identifier is missing 'user' key") if identifier["user"].startswith("@"): return identifier["user"] else: return UserID(identifier["user"], self.hs.hostname).to_string() async def _do_appservice_login(self, login_submission: JsonDict, appservice: ApplicationService): logger.info( "Got appservice login request with identifier: %r", login_submission.get("identifier"), ) identifier = convert_client_dict_legacy_fields_to_identifier( login_submission) qualified_user_id = self._get_qualified_user_id(identifier) if not appservice.is_interested_in_user(qualified_user_id): raise LoginError(403, "Invalid access_token", errcode=Codes.FORBIDDEN) return await self._complete_login(qualified_user_id, login_submission) async def _do_other_login(self, login_submission: JsonDict) -> Dict[str, str]: """Handle non-token/saml/jwt logins Args: login_submission: Returns: HTTP response """ # Log the request we got, but only certain fields to minimise the chance of # logging someone's password (even if they accidentally put it in the wrong # field) logger.info( "Got login request with identifier: %r, medium: %r, address: %r, user: %r", login_submission.get("identifier"), login_submission.get("medium"), login_submission.get("address"), login_submission.get("user"), ) identifier = convert_client_dict_legacy_fields_to_identifier( login_submission) # convert phone type identifiers to generic threepids if identifier["type"] == "m.id.phone": identifier = login_id_phone_to_thirdparty(identifier) # convert threepid identifiers to user IDs if identifier["type"] == "m.id.thirdparty": address = identifier.get("address") medium = identifier.get("medium") if medium is None or address is None: raise SynapseError(400, "Invalid thirdparty identifier") # For emails, canonicalise the address. # We store all email addresses canonicalised in the DB. # (See add_threepid in synapse/handlers/auth.py) if medium == "email": try: address = canonicalise_email(address) except ValueError as e: raise SynapseError(400, str(e)) # We also apply account rate limiting using the 3PID as a key, as # otherwise using 3PID bypasses the ratelimiting based on user ID. self._failed_attempts_ratelimiter.ratelimit((medium, address), update=False) # Check for login providers that support 3pid login types ( canonical_user_id, callback_3pid, ) = await self.auth_handler.check_password_provider_3pid( medium, address, login_submission["password"]) if canonical_user_id: # Authentication through password provider and 3pid succeeded result = await self._complete_login(canonical_user_id, login_submission, callback_3pid) return result # No password providers were able to handle this 3pid # Check local store user_id = await self.hs.get_datastore().get_user_id_by_threepid( medium, address) if not user_id: logger.warning("unknown 3pid identifier medium %s, address %r", medium, address) # We mark that we've failed to log in here, as # `check_password_provider_3pid` might have returned `None` due # to an incorrect password, rather than the account not # existing. # # If it returned None but the 3PID was bound then we won't hit # this code path, which is fine as then the per-user ratelimit # will kick in below. self._failed_attempts_ratelimiter.can_do_action( (medium, address)) raise LoginError(403, "", errcode=Codes.FORBIDDEN) identifier = {"type": "m.id.user", "user": user_id} # by this point, the identifier should be an m.id.user: if it's anything # else, we haven't understood it. qualified_user_id = self._get_qualified_user_id(identifier) # Check if we've hit the failed ratelimit (but don't update it) self._failed_attempts_ratelimiter.ratelimit(qualified_user_id.lower(), update=False) try: canonical_user_id, callback = await self.auth_handler.validate_login( identifier["user"], login_submission) except LoginError: # The user has failed to log in, so we need to update the rate # limiter. Using `can_do_action` avoids us raising a ratelimit # exception and masking the LoginError. The actual ratelimiting # should have happened above. self._failed_attempts_ratelimiter.can_do_action( qualified_user_id.lower()) raise result = await self._complete_login(canonical_user_id, login_submission, callback) return result async def _complete_login( self, user_id: str, login_submission: JsonDict, callback: Optional[Callable[[Dict[str, str]], Awaitable[None]]] = None, create_non_existent_users: bool = False, ) -> Dict[str, str]: """Called when we've successfully authed the user and now need to actually login them in (e.g. create devices). This gets called on all successful logins. Applies the ratelimiting for successful login attempts against an account. Args: user_id: ID of the user to register. login_submission: Dictionary of login information. callback: Callback function to run after login. create_non_existent_users: Whether to create the user if they don't exist. Defaults to False. Returns: result: Dictionary of account information after successful login. """ # Before we actually log them in we check if they've already logged in # too often. This happens here rather than before as we don't # necessarily know the user before now. self._account_ratelimiter.ratelimit(user_id.lower()) if create_non_existent_users: canonical_uid = await self.auth_handler.check_user_exists(user_id) if not canonical_uid: canonical_uid = await self.registration_handler.register_user( localpart=UserID.from_string(user_id).localpart) user_id = canonical_uid device_id = login_submission.get("device_id") initial_display_name = login_submission.get( "initial_device_display_name") device_id, access_token = await self.registration_handler.register_device( user_id, device_id, initial_display_name) result = { "user_id": user_id, "access_token": access_token, "home_server": self.hs.hostname, "device_id": device_id, } if callback is not None: await callback(result) return result async def _do_token_login(self, login_submission: JsonDict) -> Dict[str, str]: """ Handle the final stage of SSO login. Args: login_submission: The JSON request body. Returns: The body of the JSON response. """ token = login_submission["token"] auth_handler = self.auth_handler user_id = await auth_handler.validate_short_term_login_token_and_get_user_id( token) return await self._complete_login( user_id, login_submission, self.auth_handler._sso_login_callback) async def _do_jwt_login(self, login_submission: JsonDict) -> Dict[str, str]: token = login_submission.get("token", None) if token is None: raise LoginError(403, "Token field for JWT is missing", errcode=Codes.FORBIDDEN) import jwt try: payload = jwt.decode( token, self.jwt_secret, algorithms=[self.jwt_algorithm], issuer=self.jwt_issuer, audience=self.jwt_audiences, ) except jwt.PyJWTError as e: # A JWT error occurred, return some info back to the client. raise LoginError( 403, "JWT validation failed: %s" % (str(e), ), errcode=Codes.FORBIDDEN, ) user = payload.get("sub", None) if user is None: raise LoginError(403, "Invalid JWT", errcode=Codes.FORBIDDEN) user_id = UserID(user, self.hs.hostname).to_string() result = await self._complete_login(user_id, login_submission, create_non_existent_users=True) return result
def test_multiple_actions(self): limiter = Ratelimiter(store=self.hs.get_datastores().main, clock=None, rate_hz=0.1, burst_count=3) # Test that 4 actions aren't allowed with a maximum burst of 3. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=4, _time_now_s=0)) self.assertFalse(allowed) # Test that 3 actions are allowed with a maximum burst of 3. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=3, _time_now_s=0)) self.assertTrue(allowed) self.assertEqual(10.0, time_allowed) # Test that, after doing these 3 actions, we can't do any more actions without # waiting. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=1, _time_now_s=0)) self.assertFalse(allowed) self.assertEqual(10.0, time_allowed) # Test that after waiting we would be able to do only 1 action. # Note that we don't actually do it (update=False) here. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action( None, key="test_id", update=False, n_actions=1, _time_now_s=10, )) self.assertTrue(allowed) # We would be able to do the 5th action at t=20. self.assertEqual(20.0, time_allowed) # Attempt (but fail) to perform TWO actions at t=10. # Those would be the 4th and 5th actions. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=2, _time_now_s=10)) self.assertFalse(allowed) # The returned time allowed for the next action is now even though we weren't # allowed to perform the action because whilst we don't allow 2 actions, # we could still do 1. self.assertEqual(10.0, time_allowed) # Test that after waiting until t=20, we can do perform 2 actions. # These are the 4th and 5th actions. allowed, time_allowed = self.get_success_or_raise( limiter.can_do_action(None, key="test_id", n_actions=2, _time_now_s=20)) self.assertTrue(allowed) # We would be able to do the 6th action at t=30. self.assertEqual(30.0, time_allowed)
class RoomMemberHandler(object): # TODO(paul): This handler currently contains a messy conflation of # low-level API that works on UserID objects and so on, and REST-level # API that takes ID strings and returns pagination chunks. These concerns # ought to be separated out a lot better. __metaclass__ = abc.ABCMeta def __init__(self, hs: "HomeServer"): self.hs = hs self.store = hs.get_datastore() self.auth = hs.get_auth() self.state_handler = hs.get_state_handler() self.config = hs.config self.federation_handler = hs.get_handlers().federation_handler self.directory_handler = hs.get_handlers().directory_handler self.identity_handler = hs.get_handlers().identity_handler self.registration_handler = hs.get_registration_handler() self.profile_handler = hs.get_profile_handler() self.event_creation_handler = hs.get_event_creation_handler() self.member_linearizer = Linearizer(name="member") self.clock = hs.get_clock() self.spam_checker = hs.get_spam_checker() self.third_party_event_rules = hs.get_third_party_event_rules() self._server_notices_mxid = self.config.server_notices_mxid self._enable_lookup = hs.config.enable_3pid_lookup self.allow_per_room_profiles = self.config.allow_per_room_profiles self._event_stream_writer_instance = hs.config.worker.writers.events self._is_on_event_persistence_instance = ( self._event_stream_writer_instance == hs.get_instance_name()) if self._is_on_event_persistence_instance: self.persist_event_storage = hs.get_storage().persistence self._join_rate_limiter_local = Ratelimiter( clock=self.clock, rate_hz=hs.config.ratelimiting.rc_joins_local.per_second, burst_count=hs.config.ratelimiting.rc_joins_local.burst_count, ) self._join_rate_limiter_remote = Ratelimiter( clock=self.clock, rate_hz=hs.config.ratelimiting.rc_joins_remote.per_second, burst_count=hs.config.ratelimiting.rc_joins_remote.burst_count, ) # This is only used to get at ratelimit function, and # maybe_kick_guest_users. It's fine there are multiple of these as # it doesn't store state. self.base_handler = BaseHandler(hs) @abc.abstractmethod async def _remote_join( self, requester: Requester, remote_room_hosts: List[str], room_id: str, user: UserID, content: dict, ) -> Tuple[str, int]: """Try and join a room that this server is not in Args: requester remote_room_hosts: List of servers that can be used to join via. room_id: Room that we are trying to join user: User who is trying to join content: A dict that should be used as the content of the join event. """ raise NotImplementedError() @abc.abstractmethod async def remote_reject_invite( self, invite_event_id: str, txn_id: Optional[str], requester: Requester, content: JsonDict, ) -> Tuple[str, int]: """ Rejects an out-of-band invite we have received from a remote server Args: invite_event_id: ID of the invite to be rejected txn_id: optional transaction ID supplied by the client requester: user making the rejection request, according to the access token content: additional content to include in the rejection event. Normally an empty dict. Returns: event id, stream_id of the leave event """ raise NotImplementedError() @abc.abstractmethod async def _user_joined_room(self, target: UserID, room_id: str) -> None: """Notifies distributor on master process that the user has joined the room. Args: target room_id """ raise NotImplementedError() @abc.abstractmethod async def _user_left_room(self, target: UserID, room_id: str) -> None: """Notifies distributor on master process that the user has left the room. Args: target room_id """ raise NotImplementedError() async def _local_membership_update( self, requester: Requester, target: UserID, room_id: str, membership: str, prev_event_ids: Collection[str], txn_id: Optional[str] = None, ratelimit: bool = True, content: Optional[dict] = None, require_consent: bool = True, ) -> Tuple[str, int]: user_id = target.to_string() if content is None: content = {} content["membership"] = membership if requester.is_guest: content["kind"] = "guest" event, context = await self.event_creation_handler.create_event( requester, { "type": EventTypes.Member, "content": content, "room_id": room_id, "sender": requester.user.to_string(), "state_key": user_id, # For backwards compatibility: "membership": membership, }, token_id=requester.access_token_id, txn_id=txn_id, prev_event_ids=prev_event_ids, require_consent=require_consent, ) # Check if this event matches the previous membership event for the user. duplicate = await self.event_creation_handler.deduplicate_state_event( event, context) if duplicate is not None: # Discard the new event since this membership change is a no-op. _, stream_id = await self.store.get_event_ordering( duplicate.event_id) return duplicate.event_id, stream_id stream_id = await self.event_creation_handler.handle_new_client_event( requester, event, context, extra_users=[target], ratelimit=ratelimit, ) prev_state_ids = await context.get_prev_state_ids() prev_member_event_id = prev_state_ids.get((EventTypes.Member, user_id), None) if event.membership == Membership.JOIN: # Only fire user_joined_room if the user has actually joined the # room. Don't bother if the user is just changing their profile # info. newly_joined = True if prev_member_event_id: prev_member_event = await self.store.get_event( prev_member_event_id) newly_joined = prev_member_event.membership != Membership.JOIN if newly_joined: await self._user_joined_room(target, room_id) elif event.membership == Membership.LEAVE: if prev_member_event_id: prev_member_event = await self.store.get_event( prev_member_event_id) if prev_member_event.membership == Membership.JOIN: await self._user_left_room(target, room_id) return event.event_id, stream_id async def copy_room_tags_and_direct_to_room(self, old_room_id, new_room_id, user_id) -> None: """Copies the tags and direct room state from one room to another. Args: old_room_id: The room ID of the old room. new_room_id: The room ID of the new room. user_id: The user's ID. """ # Retrieve user account data for predecessor room user_account_data, _ = await self.store.get_account_data_for_user( user_id) # Copy direct message state if applicable direct_rooms = user_account_data.get("m.direct", {}) # Check which key this room is under if isinstance(direct_rooms, dict): for key, room_id_list in direct_rooms.items(): if old_room_id in room_id_list and new_room_id not in room_id_list: # Add new room_id to this key direct_rooms[key].append(new_room_id) # Save back to user's m.direct account data await self.store.add_account_data_for_user( user_id, "m.direct", direct_rooms) break # Copy room tags if applicable room_tags = await self.store.get_tags_for_room(user_id, old_room_id) # Copy each room tag to the new room for tag, tag_content in room_tags.items(): await self.store.add_tag_to_room(user_id, new_room_id, tag, tag_content) async def update_membership( self, requester: Requester, target: UserID, room_id: str, action: str, txn_id: Optional[str] = None, remote_room_hosts: Optional[List[str]] = None, third_party_signed: Optional[dict] = None, ratelimit: bool = True, content: Optional[dict] = None, require_consent: bool = True, ) -> Tuple[str, int]: key = (room_id, ) with (await self.member_linearizer.queue(key)): result = await self._update_membership( requester, target, room_id, action, txn_id=txn_id, remote_room_hosts=remote_room_hosts, third_party_signed=third_party_signed, ratelimit=ratelimit, content=content, require_consent=require_consent, ) return result async def _update_membership( self, requester: Requester, target: UserID, room_id: str, action: str, txn_id: Optional[str] = None, remote_room_hosts: Optional[List[str]] = None, third_party_signed: Optional[dict] = None, ratelimit: bool = True, content: Optional[dict] = None, require_consent: bool = True, ) -> Tuple[str, int]: content_specified = bool(content) if content is None: content = {} else: # We do a copy here as we potentially change some keys # later on. content = dict(content) if not self.allow_per_room_profiles: # Strip profile data, knowing that new profile data will be added to the # event's content in event_creation_handler.create_event() using the target's # global profile. content.pop("displayname", None) content.pop("avatar_url", None) effective_membership_state = action if action in ["kick", "unban"]: effective_membership_state = "leave" # if this is a join with a 3pid signature, we may need to turn a 3pid # invite into a normal invite before we can handle the join. if third_party_signed is not None: await self.federation_handler.exchange_third_party_invite( third_party_signed["sender"], target.to_string(), room_id, third_party_signed, ) if not remote_room_hosts: remote_room_hosts = [] if effective_membership_state not in ("leave", "ban"): is_blocked = await self.store.is_room_blocked(room_id) if is_blocked: raise SynapseError( 403, "This room has been blocked on this server") if effective_membership_state == Membership.INVITE: # block any attempts to invite the server notices mxid if target.to_string() == self._server_notices_mxid: raise SynapseError(HTTPStatus.FORBIDDEN, "Cannot invite this user") block_invite = False if (self._server_notices_mxid is not None and requester.user.to_string() == self._server_notices_mxid): # allow the server notices mxid to send invites is_requester_admin = True else: is_requester_admin = await self.auth.is_server_admin( requester.user) if not is_requester_admin: if self.config.block_non_admin_invites: logger.info( "Blocking invite: user is not admin and non-admin " "invites disabled") block_invite = True if not self.spam_checker.user_may_invite( requester.user.to_string(), target.to_string(), room_id): logger.info("Blocking invite due to spam checker") block_invite = True if block_invite: raise SynapseError( 403, "Invites have been disabled on this server") latest_event_ids = await self.store.get_prev_events_for_room(room_id) current_state_ids = await self.state_handler.get_current_state_ids( room_id, latest_event_ids=latest_event_ids) # TODO: Refactor into dictionary of explicitly allowed transitions # between old and new state, with specific error messages for some # transitions and generic otherwise old_state_id = current_state_ids.get( (EventTypes.Member, target.to_string())) if old_state_id: old_state = await self.store.get_event(old_state_id, allow_none=True) old_membership = old_state.content.get( "membership") if old_state else None if action == "unban" and old_membership != "ban": raise SynapseError( 403, "Cannot unban user who was not banned" " (membership=%s)" % old_membership, errcode=Codes.BAD_STATE, ) if old_membership == "ban" and action != "unban": raise SynapseError( 403, "Cannot %s user who was banned" % (action, ), errcode=Codes.BAD_STATE, ) if old_state: same_content = content == old_state.content same_membership = old_membership == effective_membership_state same_sender = requester.user.to_string() == old_state.sender if same_sender and same_membership and same_content: _, stream_id = await self.store.get_event_ordering( old_state.event_id) return ( old_state.event_id, stream_id, ) if old_membership in ["ban", "leave"] and action == "kick": raise AuthError(403, "The target user is not in the room") # we don't allow people to reject invites to the server notice # room, but they can leave it once they are joined. if (old_membership == Membership.INVITE and effective_membership_state == Membership.LEAVE): is_blocked = await self._is_server_notice_room(room_id) if is_blocked: raise SynapseError( HTTPStatus.FORBIDDEN, "You cannot reject this invite", errcode=Codes.CANNOT_LEAVE_SERVER_NOTICE_ROOM, ) else: if action == "kick": raise AuthError(403, "The target user is not in the room") is_host_in_room = await self._is_host_in_room(current_state_ids) if effective_membership_state == Membership.JOIN: if requester.is_guest: guest_can_join = await self._can_guest_join(current_state_ids) if not guest_can_join: # This should be an auth check, but guests are a local concept, # so don't really fit into the general auth process. raise AuthError(403, "Guest access not allowed") if is_host_in_room: time_now_s = self.clock.time() allowed, time_allowed = self._join_rate_limiter_local.can_do_action( requester.user.to_string(), ) if not allowed: raise LimitExceededError( retry_after_ms=int(1000 * (time_allowed - time_now_s))) else: time_now_s = self.clock.time() allowed, time_allowed = self._join_rate_limiter_remote.can_do_action( requester.user.to_string(), ) if not allowed: raise LimitExceededError( retry_after_ms=int(1000 * (time_allowed - time_now_s))) inviter = await self._get_inviter(target.to_string(), room_id) if inviter and not self.hs.is_mine(inviter): remote_room_hosts.append(inviter.domain) content["membership"] = Membership.JOIN profile = self.profile_handler if not content_specified: content["displayname"] = await profile.get_displayname( target) content["avatar_url"] = await profile.get_avatar_url(target ) if requester.is_guest: content["kind"] = "guest" remote_join_response = await self._remote_join( requester, remote_room_hosts, room_id, target, content) return remote_join_response elif effective_membership_state == Membership.LEAVE: if not is_host_in_room: # perhaps we've been invited invite = await self.store.get_invite_for_local_user_in_room( user_id=target.to_string(), room_id=room_id) # type: Optional[RoomsForUser] if not invite: logger.info( "%s sent a leave request to %s, but that is not an active room " "on this server, and there is no pending invite", target, room_id, ) raise SynapseError(404, "Not a known room") logger.info("%s rejects invite to %s from %s", target, room_id, invite.sender) if not self.hs.is_mine_id(invite.sender): # send the rejection to the inviter's HS (with fallback to # local event) return await self.remote_reject_invite( invite.event_id, txn_id, requester, content, ) # the inviter was on our server, but has now left. Carry on # with the normal rejection codepath, which will also send the # rejection out to any other servers we believe are still in the room. # thanks to overzealous cleaning up of event_forward_extremities in # `delete_old_current_state_events`, it's possible to end up with no # forward extremities here. If that happens, let's just hang the # rejection off the invite event. # # see: https://github.com/matrix-org/synapse/issues/7139 if len(latest_event_ids) == 0: latest_event_ids = [invite.event_id] return await self._local_membership_update( requester=requester, target=target, room_id=room_id, membership=effective_membership_state, txn_id=txn_id, ratelimit=ratelimit, prev_event_ids=latest_event_ids, content=content, require_consent=require_consent, ) async def transfer_room_state_on_room_upgrade(self, old_room_id: str, room_id: str) -> None: """Upon our server becoming aware of an upgraded room, either by upgrading a room ourselves or joining one, we can transfer over information from the previous room. Copies user state (tags/push rules) for every local user that was in the old room, as well as migrating the room directory state. Args: old_room_id: The ID of the old room room_id: The ID of the new room """ logger.info("Transferring room state from %s to %s", old_room_id, room_id) # Find all local users that were in the old room and copy over each user's state users = await self.store.get_users_in_room(old_room_id) await self.copy_user_state_on_room_upgrade(old_room_id, room_id, users) # Add new room to the room directory if the old room was there # Remove old room from the room directory old_room = await self.store.get_room(old_room_id) if old_room and old_room["is_public"]: await self.store.set_room_is_public(old_room_id, False) await self.store.set_room_is_public(room_id, True) # Transfer alias mappings in the room directory await self.store.update_aliases_for_room(old_room_id, room_id) # Check if any groups we own contain the predecessor room local_group_ids = await self.store.get_local_groups_for_room( old_room_id) for group_id in local_group_ids: # Add new the new room to those groups await self.store.add_room_to_group(group_id, room_id, old_room["is_public"]) # Remove the old room from those groups await self.store.remove_room_from_group(group_id, old_room_id) async def copy_user_state_on_room_upgrade(self, old_room_id: str, new_room_id: str, user_ids: Iterable[str]) -> None: """Copy user-specific information when they join a new room when that new room is the result of a room upgrade Args: old_room_id: The ID of upgraded room new_room_id: The ID of the new room user_ids: User IDs to copy state for """ logger.debug( "Copying over room tags and push rules from %s to %s for users %s", old_room_id, new_room_id, user_ids, ) for user_id in user_ids: try: # It is an upgraded room. Copy over old tags await self.copy_room_tags_and_direct_to_room( old_room_id, new_room_id, user_id) # Copy over push rules await self.store.copy_push_rules_from_room_to_room_for_user( old_room_id, new_room_id, user_id) except Exception: logger.exception( "Error copying tags and/or push rules from rooms %s to %s for user %s. " "Skipping...", old_room_id, new_room_id, user_id, ) continue async def send_membership_event( self, requester: Requester, event: EventBase, context: EventContext, ratelimit: bool = True, ): """ Change the membership status of a user in a room. Args: requester: The local user who requested the membership event. If None, certain checks, like whether this homeserver can act as the sender, will be skipped. event: The membership event. context: The context of the event. ratelimit: Whether to rate limit this request. Raises: SynapseError if there was a problem changing the membership. """ target_user = UserID.from_string(event.state_key) room_id = event.room_id if requester is not None: sender = UserID.from_string(event.sender) assert (sender == requester.user ), "Sender (%s) must be same as requester (%s)" % ( sender, requester.user) assert self.hs.is_mine( sender), "Sender must be our own: %s" % (sender, ) else: requester = types.create_requester(target_user) prev_event = await self.event_creation_handler.deduplicate_state_event( event, context) if prev_event is not None: return prev_state_ids = await context.get_prev_state_ids() if event.membership == Membership.JOIN: if requester.is_guest: guest_can_join = await self._can_guest_join(prev_state_ids) if not guest_can_join: # This should be an auth check, but guests are a local concept, # so don't really fit into the general auth process. raise AuthError(403, "Guest access not allowed") if event.membership not in (Membership.LEAVE, Membership.BAN): is_blocked = await self.store.is_room_blocked(room_id) if is_blocked: raise SynapseError( 403, "This room has been blocked on this server") await self.event_creation_handler.handle_new_client_event( requester, event, context, extra_users=[target_user], ratelimit=ratelimit) prev_member_event_id = prev_state_ids.get( (EventTypes.Member, event.state_key), None) if event.membership == Membership.JOIN: # Only fire user_joined_room if the user has actually joined the # room. Don't bother if the user is just changing their profile # info. newly_joined = True if prev_member_event_id: prev_member_event = await self.store.get_event( prev_member_event_id) newly_joined = prev_member_event.membership != Membership.JOIN if newly_joined: await self._user_joined_room(target_user, room_id) elif event.membership == Membership.LEAVE: if prev_member_event_id: prev_member_event = await self.store.get_event( prev_member_event_id) if prev_member_event.membership == Membership.JOIN: await self._user_left_room(target_user, room_id) async def _can_guest_join( self, current_state_ids: Dict[Tuple[str, str], str]) -> bool: """ Returns whether a guest can join a room based on its current state. """ guest_access_id = current_state_ids.get((EventTypes.GuestAccess, ""), None) if not guest_access_id: return False guest_access = await self.store.get_event(guest_access_id) return (guest_access and guest_access.content and "guest_access" in guest_access.content and guest_access.content["guest_access"] == "can_join") async def lookup_room_alias( self, room_alias: RoomAlias) -> Tuple[RoomID, List[str]]: """ Get the room ID associated with a room alias. Args: room_alias: The alias to look up. Returns: A tuple of: The room ID as a RoomID object. Hosts likely to be participating in the room ([str]). Raises: SynapseError if room alias could not be found. """ directory_handler = self.directory_handler mapping = await directory_handler.get_association(room_alias) if not mapping: raise SynapseError(404, "No such room alias") room_id = mapping["room_id"] servers = mapping["servers"] # put the server which owns the alias at the front of the server list. if room_alias.domain in servers: servers.remove(room_alias.domain) servers.insert(0, room_alias.domain) return RoomID.from_string(room_id), servers async def _get_inviter(self, user_id: str, room_id: str) -> Optional[UserID]: invite = await self.store.get_invite_for_local_user_in_room( user_id=user_id, room_id=room_id) if invite: return UserID.from_string(invite.sender) return None async def do_3pid_invite( self, room_id: str, inviter: UserID, medium: str, address: str, id_server: str, requester: Requester, txn_id: Optional[str], id_access_token: Optional[str] = None, ) -> int: if self.config.block_non_admin_invites: is_requester_admin = await self.auth.is_server_admin(requester.user ) if not is_requester_admin: raise SynapseError( 403, "Invites have been disabled on this server", Codes.FORBIDDEN) # We need to rate limit *before* we send out any 3PID invites, so we # can't just rely on the standard ratelimiting of events. await self.base_handler.ratelimit(requester) can_invite = await self.third_party_event_rules.check_threepid_can_be_invited( medium, address, room_id) if not can_invite: raise SynapseError( 403, "This third-party identifier can not be invited in this room", Codes.FORBIDDEN, ) if not self._enable_lookup: raise SynapseError( 403, "Looking up third-party identifiers is denied from this server" ) invitee = await self.identity_handler.lookup_3pid( id_server, medium, address, id_access_token) if invitee: _, stream_id = await self.update_membership( requester, UserID.from_string(invitee), room_id, "invite", txn_id=txn_id) else: stream_id = await self._make_and_store_3pid_invite( requester, id_server, medium, address, room_id, inviter, txn_id=txn_id, id_access_token=id_access_token, ) return stream_id async def _make_and_store_3pid_invite( self, requester: Requester, id_server: str, medium: str, address: str, room_id: str, user: UserID, txn_id: Optional[str], id_access_token: Optional[str] = None, ) -> int: room_state = await self.state_handler.get_current_state(room_id) inviter_display_name = "" inviter_avatar_url = "" member_event = room_state.get((EventTypes.Member, user.to_string())) if member_event: inviter_display_name = member_event.content.get("displayname", "") inviter_avatar_url = member_event.content.get("avatar_url", "") # if user has no display name, default to their MXID if not inviter_display_name: inviter_display_name = user.to_string() canonical_room_alias = "" canonical_alias_event = room_state.get((EventTypes.CanonicalAlias, "")) if canonical_alias_event: canonical_room_alias = canonical_alias_event.content.get( "alias", "") room_name = "" room_name_event = room_state.get((EventTypes.Name, "")) if room_name_event: room_name = room_name_event.content.get("name", "") room_join_rules = "" join_rules_event = room_state.get((EventTypes.JoinRules, "")) if join_rules_event: room_join_rules = join_rules_event.content.get("join_rule", "") room_avatar_url = "" room_avatar_event = room_state.get((EventTypes.RoomAvatar, "")) if room_avatar_event: room_avatar_url = room_avatar_event.content.get("url", "") ( token, public_keys, fallback_public_key, display_name, ) = await self.identity_handler.ask_id_server_for_third_party_invite( requester=requester, id_server=id_server, medium=medium, address=address, room_id=room_id, inviter_user_id=user.to_string(), room_alias=canonical_room_alias, room_avatar_url=room_avatar_url, room_join_rules=room_join_rules, room_name=room_name, inviter_display_name=inviter_display_name, inviter_avatar_url=inviter_avatar_url, id_access_token=id_access_token, ) ( event, stream_id, ) = await self.event_creation_handler.create_and_send_nonmember_event( requester, { "type": EventTypes.ThirdPartyInvite, "content": { "display_name": display_name, "public_keys": public_keys, # For backwards compatibility: "key_validity_url": fallback_public_key["key_validity_url"], "public_key": fallback_public_key["public_key"], }, "room_id": room_id, "sender": user.to_string(), "state_key": token, }, ratelimit=False, txn_id=txn_id, ) return stream_id async def _is_host_in_room( self, current_state_ids: Dict[Tuple[str, str], str]) -> bool: # Have we just created the room, and is this about to be the very # first member event? create_event_id = current_state_ids.get(("m.room.create", "")) if len(current_state_ids) == 1 and create_event_id: # We can only get here if we're in the process of creating the room return True for etype, state_key in current_state_ids: if etype != EventTypes.Member or not self.hs.is_mine_id(state_key): continue event_id = current_state_ids[(etype, state_key)] event = await self.store.get_event(event_id, allow_none=True) if not event: continue if event.membership == Membership.JOIN: return True return False async def _is_server_notice_room(self, room_id: str) -> bool: if self._server_notices_mxid is None: return False user_ids = await self.store.get_users_in_room(room_id) return self._server_notices_mxid in user_ids