From 6c29b9d4b8e3cc4d6b2e3c937dd11a2badabc2b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Clouet?= Date: Sat, 29 Feb 2020 14:43:23 +0100 Subject: [PATCH 1/2] Added the ability to provide credentials under the form of a custom auth-string for some matrix instances. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Benoît Clouet --- matrix_client/api.py | 8 ++++++++ matrix_client/client.py | 45 +++++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index aae52f99..9400ae0b 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -180,6 +180,14 @@ def login(self, login_type, **kwargs): return self._send("POST", "/login", content) + def login(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..259733f1 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(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( + 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. From b94973b9f17697ae393aa4305cb877b52b8c1fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Clouet?= Date: Sat, 29 Feb 2020 15:14:34 +0100 Subject: [PATCH 2/2] Changed the api for complying with flake8 rules. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Benoît Clouet --- matrix_client/api.py | 5 +++-- matrix_client/client.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/matrix_client/api.py b/matrix_client/api.py index 9400ae0b..bf74b89b 100644 --- a/matrix_client/api.py +++ b/matrix_client/api.py @@ -180,11 +180,12 @@ def login(self, login_type, **kwargs): return self._send("POST", "/login", content) - def login(self, auth_string): + 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. + auth_string(str): The auth string to user for authenticating. + The string is built elsewhere. """ return self._send("POST", "/login", auth_string) diff --git a/matrix_client/client.py b/matrix_client/client.py index 259733f1..c183d68d 100644 --- a/matrix_client/client.py +++ b/matrix_client/client.py @@ -280,7 +280,7 @@ def login(self, username, password, limit=10, sync=True, device_id=None): self.parse_login_response(response, sync, limit) return self.token - def login(self, auth_string, limit=10, sync=True): + def login_with_custom_string(self, auth_string, limit=10, sync=True): """Login to the homeserver using a manually crafted auth string. Args: @@ -307,7 +307,7 @@ def login(self, auth_string, limit=10, sync=True): Raises: MatrixRequestError """ - response = self.api.login( + response = self.api.login_with_custom_string( auth_string ) self.parse_login_response(response, sync, limit)