This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Support 3PID login in password providers #4931
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
94e01f9
Support 3PID authentication in password auth providers
anoadragon453 674e2f6
Cleanup methods
anoadragon453 5e0f1af
Support email/displayname binding
anoadragon453 cb4d9b8
Ability to register multiple emails
anoadragon453 faa6441
Add changelog
anoadragon453 7b28dab
isort lint
anoadragon453 b1ef710
Update documentation
anoadragon453 0a10208
Address concerns
anoadragon453 9f09393
Move email support to a separate PR
anoadragon453 de605cb
Fix comment
anoadragon453 a42138b
Delete a space
anoadragon453 bbed0ee
Merge branch 'develop' into anoa/ldap_email_support
anoadragon453 8b1b2f2
email, what email
anoadragon453 60e0f7e
Document things
anoadragon453 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add ability for password providers to login/register a user via 3PID (email, phone). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,19 @@ Password auth provider classes may optionally provide the following methods. | |
result from the ``/login`` call (including ``access_token``, ``device_id``, | ||
etc.) | ||
|
||
``someprovider.check_3pid_auth``\(*medium*, *address*, *password*) | ||
|
||
This method, if implemented, is called when a user attempts to register or | ||
log in with a third party identifier, such as email. It is passed the | ||
medium (ex. "email"), an address (ex. "[email protected]") and the user's | ||
password. | ||
|
||
The method should return a Twisted ``Deferred`` object, which resolves to | ||
a ``str`` containing the user's (canonical) User ID if authentication was | ||
successful, and ``None`` if not. The ``Deferred`` can also instead | ||
resolve to a tuple of ``(str, callback)``, where ``callback`` is a | ||
function that is run after login/registration has completed successfully. | ||
|
||
``someprovider.check_password``\(*user_id*, *password*) | ||
|
||
This method provides a simpler interface than ``get_supported_login_types`` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -745,6 +745,37 @@ def validate_login(self, username, login_submission): | |
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, None)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does the result mean? |
||
""" | ||
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): | ||
richvdh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# If it's a str, set callback function to None | ||
result = (result, None) | ||
defer.returnValue(result) | ||
|
||
defer.returnValue((None, None)) | ||
|
||
@defer.inlineCallbacks | ||
def _check_local_password(self, user_id, password): | ||
"""Authenticate a user against the local password database. | ||
|
@@ -756,7 +787,8 @@ def _check_local_password(self, user_id, password): | |
user_id (unicode): complete @user:id | ||
password (unicode): the provided password | ||
Returns: | ||
(unicode) the canonical_user_id, or None if unknown user / bad password | ||
Deferred[unicode] the canonical_user_id, or Deferred[None] if | ||
unknown user/bad password | ||
|
||
Raises: | ||
LimitExceededError if the ratelimiter's login requests count for this | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,14 +73,25 @@ def check_user_exists(self, user_id): | |
""" | ||
return self._auth_handler.check_user_exists(user_id) | ||
|
||
def register(self, localpart): | ||
"""Registers a new user with given localpart | ||
@defer.inlineCallbacks | ||
def register(self, localpart, displayname=None): | ||
"""Registers a new user with given localpart and optional | ||
displayname. | ||
|
||
Args: | ||
localpart (str): The localpart of the new user. | ||
displayname (str|None): The displayname of the new user. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if it is set to None? |
||
|
||
Returns: | ||
Deferred: a 2-tuple of (user_id, access_token) | ||
""" | ||
# Register the user | ||
reg = self.hs.get_registration_handler() | ||
return reg.register(localpart=localpart) | ||
user_id, access_token = yield reg.register( | ||
localpart=localpart, default_display_name=displayname, | ||
) | ||
|
||
defer.returnValue((user_id, access_token)) | ||
|
||
@defer.inlineCallbacks | ||
def invalidate_access_token(self, access_token): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"also instead"
How about:
... rather than duplicating the whole thing. Suggest a separate paragraph for clarity too.