@@ -114,6 +114,7 @@ def __init__(self, hs: "HomeServer"):
114
114
hs .config .oidc_user_mapping_provider_config
115
115
) # type: OidcMappingProvider
116
116
self ._skip_verification = hs .config .oidc_skip_verification # type: bool
117
+ self ._allow_existing_users = hs .config .oidc_allow_existing_users # type: bool
117
118
118
119
self ._http_client = hs .get_proxied_http_client ()
119
120
self ._auth_handler = hs .get_auth_handler ()
@@ -849,7 +850,8 @@ async def _map_userinfo_to_user(
849
850
If we don't find the user that way, we should register the user,
850
851
mapping the localpart and the display name from the UserInfo.
851
852
852
- If a user already exists with the mxid we've mapped, raise an exception.
853
+ If a user already exists with the mxid we've mapped and allow_existing_users
854
+ is disabled, raise an exception.
853
855
854
856
Args:
855
857
userinfo: an object representing the user
@@ -905,21 +907,31 @@ async def _map_userinfo_to_user(
905
907
906
908
localpart = map_username_to_mxid_localpart (attributes ["localpart" ])
907
909
908
- user_id = UserID (localpart , self ._hostname )
909
- if await self ._datastore .get_users_by_id_case_insensitive (user_id .to_string ()):
910
- # This mxid is taken
911
- raise MappingException (
912
- "mxid '{}' is already taken" .format (user_id .to_string ())
910
+ user_id = UserID (localpart , self ._hostname ).to_string ()
911
+ users = await self ._datastore .get_users_by_id_case_insensitive (user_id )
912
+ if users :
913
+ if self ._allow_existing_users :
914
+ if len (users ) == 1 :
915
+ registered_user_id = next (iter (users ))
916
+ elif user_id in users :
917
+ registered_user_id = user_id
918
+ else :
919
+ raise MappingException (
920
+ "Attempted to login as '{}' but it matches more than one user inexactly: {}" .format (
921
+ user_id , list (users .keys ())
922
+ )
923
+ )
924
+ else :
925
+ # This mxid is taken
926
+ raise MappingException ("mxid '{}' is already taken" .format (user_id ))
927
+ else :
928
+ # It's the first time this user is logging in and the mapped mxid was
929
+ # not taken, register the user
930
+ registered_user_id = await self ._registration_handler .register_user (
931
+ localpart = localpart ,
932
+ default_display_name = attributes ["display_name" ],
933
+ user_agent_ips = (user_agent , ip_address ),
913
934
)
914
-
915
- # It's the first time this user is logging in and the mapped mxid was
916
- # not taken, register the user
917
- registered_user_id = await self ._registration_handler .register_user (
918
- localpart = localpart ,
919
- default_display_name = attributes ["display_name" ],
920
- user_agent_ips = (user_agent , ip_address ),
921
- )
922
-
923
935
await self ._datastore .record_user_external_id (
924
936
self ._auth_provider_id , remote_user_id , registered_user_id ,
925
937
)
0 commit comments