14
14
# limitations under the License.
15
15
import logging
16
16
import random
17
- import re
18
17
from typing import TYPE_CHECKING , List , Optional , Tuple
19
18
20
19
from twisted .web .server import Request
@@ -469,22 +468,13 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
469
468
else :
470
469
should_issue_refresh_token = False
471
470
472
- # We don't care about usernames for this deployment. In fact, the act
473
- # of checking whether they exist already can leak metadata about
474
- # which users are already registered.
475
- #
476
- # Usernames are already derived via the provided email.
477
- # So, if they're not necessary, just ignore them.
478
- #
479
- # (we do still allow appservices to set them below)
471
+ # Pull out the provided username and do basic sanity checks early since
472
+ # the auth layer will store these in sessions.
480
473
desired_username = None
481
-
482
- desired_display_name = body .get ("display_name" )
483
-
484
- # We need to retrieve the password early in order to pass it to
485
- # application service registration
486
- # This is specific to shadow server registration of users via an AS
487
- password = body .pop ("password" , None )
474
+ if "username" in body :
475
+ if not isinstance (body ["username" ], str ) or len (body ["username" ]) > 512 :
476
+ raise SynapseError (400 , "Invalid username" )
477
+ desired_username = body ["username" ]
488
478
489
479
# fork off as soon as possible for ASes which have completely
490
480
# different registration flows to normal users
@@ -503,7 +493,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
503
493
# Set the desired user according to the AS API (which uses the
504
494
# 'user' key not 'username'). Since this is a new addition, we'll
505
495
# fallback to 'username' if they gave one.
506
- desired_username = body .get ("user" , body . get ( "username" ) )
496
+ desired_username = body .get ("user" , desired_username )
507
497
508
498
# XXX we should check that desired_username is valid. Currently
509
499
# we give appservices carte blanche for any insanity in mxids,
@@ -533,6 +523,16 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
533
523
if not self ._registration_enabled :
534
524
raise SynapseError (403 , "Registration has been disabled" , Codes .FORBIDDEN )
535
525
526
+ # For regular registration, convert the provided username to lowercase
527
+ # before attempting to register it. This should mean that people who try
528
+ # to register with upper-case in their usernames don't get a nasty surprise.
529
+ #
530
+ # Note that we treat usernames case-insensitively in login, so they are
531
+ # free to carry on imagining that their username is CrAzYh4cKeR if that
532
+ # keeps them happy.
533
+ if desired_username is not None :
534
+ desired_username = desired_username .lower ()
535
+
536
536
# Check if this account is upgrading from a guest account.
537
537
guest_access_token = body .get ("guest_access_token" , None )
538
538
@@ -541,6 +541,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
541
541
# Note that we remove the password from the body since the auth layer
542
542
# will store the body in the session and we don't want a plaintext
543
543
# password store there.
544
+ password = body .pop ("password" , None )
544
545
if password is not None :
545
546
if not isinstance (password , str ) or len (password ) > 512 :
546
547
raise SynapseError (400 , "Invalid password" )
@@ -626,15 +627,6 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
626
627
Codes .THREEPID_DENIED ,
627
628
)
628
629
629
- existingUid = await self .store .get_user_id_by_threepid (
630
- medium , address
631
- )
632
-
633
- if existingUid is not None :
634
- raise SynapseError (
635
- 400 , "%s is already in use" % medium , Codes .THREEPID_IN_USE
636
- )
637
-
638
630
if registered_user_id is not None :
639
631
logger .info (
640
632
"Already registered user ID %r for this session" , registered_user_id
@@ -703,6 +695,20 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
703
695
session_id
704
696
)
705
697
698
+ # TODO: This won't be needed anymore once https://github.com/matrix-org/matrix-dinsic/issues/793
699
+ # is resolved.
700
+ desired_display_name = body .get ("display_name" )
701
+ if auth_result :
702
+ if LoginType .EMAIL_IDENTITY in auth_result :
703
+ address = auth_result [LoginType .EMAIL_IDENTITY ]["address" ]
704
+ if (
705
+ self .hs .config .registration .register_just_use_email_for_display_name
706
+ ):
707
+ desired_display_name = address
708
+ else :
709
+ # Custom mapping between email address and display name
710
+ desired_display_name = _map_email_to_displayname (address )
711
+
706
712
registered_user_id = await self .registration_handler .register_user (
707
713
localpart = desired_username ,
708
714
password_hash = password_hash ,
0 commit comments