Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to provide credentials under the form of a custom #314

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions matrix_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
45 changes: 37 additions & 8 deletions matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]"
},
"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.
Expand Down