diff --git a/matrix_client/api.py b/matrix_client/api.py index aae52f99..bf74b89b 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -180,6 +180,15 @@ def login(self, login_type, **kwargs): return self._send("POST", "/login", content) + def login_with_custom_string(self, auth_string): + """Perform /login. + + Args: + auth_string(str): The auth string to user for authenticating. + The string is built elsewhere. + """ + return self._send("POST", "/login", auth_string) + def logout(self): """Perform /logout. """ diff --git a/matrix_client/client.py b/matrix_client/client.py index af0e08f6..c183d68d 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -277,23 +277,52 @@ def login(self, username, password, limit=10, sync=True, device_id=None): response = self.api.login( "m.login.password", user=username, password=password, device_id=device_id ) + self.parse_login_response(response, sync, limit) + return self.token + + def login_with_custom_string(self, auth_string, limit=10, sync=True): + """Login to the homeserver using a manually crafted auth string. + + Args: + auth_string (str): Valid auth string + Example of valid auth string + --- + {"type":"m.login.password", "user": username, "password": password} + --- + { + "type": "m.login.password", "identifier": { + "type": "m.id.thirdparty", + "medium": "email", + "address": "test@thirdpartyinstance.team" + }, + "password": "mysecretpassword", "initial_device_display_name": "mydevice" + } + limit (int): Deprecated. How many messages to return when syncing. + This will be replaced by a filter API in a later release. + sync (bool): Optional. Whether to initiate a /sync request after logging in. + + Returns: + str: Access token + + Raises: + MatrixRequestError + """ + response = self.api.login_with_custom_string( + auth_string + ) + self.parse_login_response(response, sync, limit) + return self.token + + def parse_login_response(self, response, sync, limit): self.user_id = response["user_id"] self.token = response["access_token"] self.hs = response["home_server"] self.api.token = self.token - self.device_id = response["device_id"] - - if self._encryption: - self.olm_device = OlmDevice( - self.api, self.user_id, self.device_id, **self.encryption_conf) - self.olm_device.upload_identity_keys() - self.olm_device.upload_one_time_keys() if sync: """ Limit Filter """ self.sync_filter = '{ "room": { "timeline" : { "limit" : %i } } }' % limit self._sync() - return self.token def logout(self): """ Logout from the homeserver.