This repository was 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
add the register_mxid_from_3pid setting #3096
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
969ed2e
add the register_mxid_from_3pid setting (untested)
ara4n 240e940
handle medium checks correctly
ara4n b22a53e
turn @'s to -'s rather than .'s
ara4n 79b2583
Merge branch 'dinsic' into matthew/derive-mxid-from-3pid
ara4n 32e4420
improve mxid & displayname selection for register_mxid_from_3pid
ara4n 2992125
special case msisdns when deriving mxids from 3pids
ara4n 383c4ae
Merge branch 'dinsic' into matthew/derive-mxid-from-3pid
ara4n 5c74ab4
fix user_id / user confusion
ara4n 41b987c
unbreak 3pid deletion
907a62d
fix strip_invalid_mxid_characters
debf045
fix user in user regexp
ara4n fb47ce3
make set_profile_* an upsert rather than update, now create_profile i…
ara4n b3e346f
don't pass a requester if we don't have one to set_displayname
ara4n 6372dff
remove create_profile from tests
ara4n 9f2fd29
fix double negative
ara4n 0783801
unbreak tests
ara4n 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
| from twisted.internet import defer | ||
|
|
||
| import synapse | ||
| import synapse.types | ||
| from synapse import types | ||
| from synapse.api.auth import get_access_token_from_request, has_access_token | ||
| from synapse.api.constants import LoginType | ||
| from synapse.api.errors import SynapseError, Codes, UnrecognizedRequestError | ||
|
|
@@ -31,6 +31,8 @@ | |
|
|
||
| import logging | ||
| import hmac | ||
| import re | ||
| from string import capwords | ||
| from hashlib import sha1 | ||
| from synapse.util.async import run_on_reactor | ||
| from synapse.util.ratelimitutils import FederationRateLimiter | ||
|
|
@@ -222,6 +224,8 @@ def on_POST(self, request): | |
| raise SynapseError(400, "Invalid username") | ||
| desired_username = body['username'] | ||
|
|
||
| desired_display_name = None | ||
|
|
||
| appservice = None | ||
| if has_access_token(request): | ||
| appservice = yield self.auth.get_appservice_by_req(request) | ||
|
|
@@ -297,13 +301,6 @@ def on_POST(self, request): | |
| session_id, "registered_user_id", None | ||
| ) | ||
|
|
||
| if desired_username is not None: | ||
| yield self.registration_handler.check_username( | ||
| desired_username, | ||
| guest_access_token=guest_access_token, | ||
| assigned_user_id=registered_user_id, | ||
| ) | ||
|
|
||
| # Only give msisdn flows if the x_show_msisdn flag is given: | ||
| # this is a hack to work around the fact that clients were shipped | ||
| # that use fallback registration if they see any flows that they don't | ||
|
|
@@ -376,6 +373,69 @@ def on_POST(self, request): | |
| Codes.THREEPID_DENIED, | ||
| ) | ||
|
|
||
| if self.hs.config.register_mxid_from_3pid: | ||
| # override the desired_username based on the 3PID if any. | ||
| # reset it first to avoid folks picking their own username. | ||
| desired_username = None | ||
|
|
||
| # we should have an auth_result at this point if we're going to progress | ||
| # to register the user (i.e. we haven't picked up a registered_user_id | ||
| # from our session store), in which case get ready and gen the | ||
| # desired_username | ||
| if auth_result: | ||
| if ( | ||
| ( | ||
| self.hs.config.register_mxid_from_3pid == 'email' and | ||
| LoginType.EMAIL_IDENTITY in auth_result | ||
| ) or ( | ||
| self.hs.config.register_mxid_from_3pid == 'msisdn' and | ||
| LoginType.MSISDN in auth_result | ||
| ) | ||
| ): | ||
| address = auth_result[login_type]['address'] | ||
| desired_username = types.strip_invalid_mxid_characters( | ||
| address.replace('@', '-').lower() | ||
| ) | ||
|
|
||
| # find a unique mxid for the account, suffixing numbers | ||
| # if needed | ||
| while True: | ||
| try: | ||
| yield self.registration_handler.check_username( | ||
| desired_username, | ||
| guest_access_token=guest_access_token, | ||
| assigned_user_id=registered_user_id, | ||
| ) | ||
| # if we got this far we passed the check. | ||
| break | ||
| except SynapseError as e: | ||
| if e.errcode == Codes.USER_IN_USE: | ||
| m = re.match(r'^(.*)(\d+)$', desired_username) | ||
| if m: | ||
| desired_username = m.group(1) + str( | ||
| int(m.group(2)) + 1 | ||
| ) | ||
| else: | ||
| desired_username += "1" | ||
| else: | ||
| # something else went wrong. | ||
| break | ||
|
|
||
| # XXX: a nasty heuristic to turn an email address into | ||
| # a displayname, as part of register_mxid_from_3pid | ||
| parts = address.replace('.', ' ').split('@') | ||
|
Member
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. You're doing this for msisdns too - is this going to fail for these (presumably there just won't be a parts[1]) |
||
| desired_display_name = ( | ||
| capwords(parts[0]) + | ||
| " [" + capwords(parts[1].split(' ')[0]) + "]" | ||
| ) | ||
|
|
||
| if desired_username is not None: | ||
| yield self.registration_handler.check_username( | ||
| desired_username, | ||
| guest_access_token=guest_access_token, | ||
| assigned_user_id=registered_user_id, | ||
| ) | ||
|
|
||
| if registered_user_id is not None: | ||
| logger.info( | ||
| "Already registered user ID %r for this session", | ||
|
|
@@ -390,10 +450,18 @@ def on_POST(self, request): | |
| raise SynapseError(400, "Missing password.", | ||
| Codes.MISSING_PARAM) | ||
|
|
||
| desired_username = params.get("username", None) | ||
| if not self.hs.config.register_mxid_from_3pid: | ||
| desired_username = params.get("username", None) | ||
| else: | ||
| # we keep the original desired_username derived from the 3pid above | ||
| pass | ||
|
|
||
| new_password = params.get("password", None) | ||
| guest_access_token = params.get("guest_access_token", None) | ||
|
|
||
| # XXX: don't we need to validate these for length etc like we did on | ||
| # the ones from the JSON body earlier on in the method? | ||
|
|
||
| if desired_username is not None: | ||
| desired_username = desired_username.lower() | ||
|
|
||
|
|
@@ -402,6 +470,7 @@ def on_POST(self, request): | |
| password=new_password, | ||
| guest_access_token=guest_access_token, | ||
| generate_token=False, | ||
| display_name=desired_display_name, | ||
| ) | ||
|
|
||
| # remember that we've now registered that user account, and with | ||
|
|
||
This file contains hidden or 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 hidden or 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.
Adding a '1' on the end could be a bit confusing for msisdns