Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit abd04b6

Browse files
authored
Allow existing users to login via OpenID Connect. (#8345)
Co-authored-by: Benjamin Koch <bbbsnowball@gmail.com> This adds configuration flags that will match a user to pre-existing users when logging in via OpenID Connect. This is useful when switching to an existing SSO system.
1 parent 3e87d79 commit abd04b6

File tree

6 files changed

+76
-17
lines changed

6 files changed

+76
-17
lines changed

changelog.d/8345.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a configuration option that allows existing users to log in with OpenID Connect. Contributed by @BBBSnowball and @OmmyZhang.

docs/sample_config.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,11 @@ oidc_config:
16891689
#
16901690
#skip_verification: true
16911691

1692+
# Uncomment to allow a user logging in via OIDC to match a pre-existing account instead
1693+
# of failing. This could be used if switching from password logins to OIDC. Defaults to false.
1694+
#
1695+
#allow_existing_users: true
1696+
16921697
# An external module can be provided here as a custom solution to mapping
16931698
# attributes returned from a OIDC provider onto a matrix user.
16941699
#

synapse/config/oidc_config.py

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def read_config(self, config, **kwargs):
5656
self.oidc_userinfo_endpoint = oidc_config.get("userinfo_endpoint")
5757
self.oidc_jwks_uri = oidc_config.get("jwks_uri")
5858
self.oidc_skip_verification = oidc_config.get("skip_verification", False)
59+
self.oidc_allow_existing_users = oidc_config.get("allow_existing_users", False)
5960

6061
ump_config = oidc_config.get("user_mapping_provider", {})
6162
ump_config.setdefault("module", DEFAULT_USER_MAPPING_PROVIDER)
@@ -158,6 +159,11 @@ def generate_config_section(self, config_dir_path, server_name, **kwargs):
158159
#
159160
#skip_verification: true
160161
162+
# Uncomment to allow a user logging in via OIDC to match a pre-existing account instead
163+
# of failing. This could be used if switching from password logins to OIDC. Defaults to false.
164+
#
165+
#allow_existing_users: true
166+
161167
# An external module can be provided here as a custom solution to mapping
162168
# attributes returned from a OIDC provider onto a matrix user.
163169
#

synapse/handlers/oidc_handler.py

+27-15
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def __init__(self, hs: "HomeServer"):
114114
hs.config.oidc_user_mapping_provider_config
115115
) # type: OidcMappingProvider
116116
self._skip_verification = hs.config.oidc_skip_verification # type: bool
117+
self._allow_existing_users = hs.config.oidc_allow_existing_users # type: bool
117118

118119
self._http_client = hs.get_proxied_http_client()
119120
self._auth_handler = hs.get_auth_handler()
@@ -849,7 +850,8 @@ async def _map_userinfo_to_user(
849850
If we don't find the user that way, we should register the user,
850851
mapping the localpart and the display name from the UserInfo.
851852
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.
853855
854856
Args:
855857
userinfo: an object representing the user
@@ -905,21 +907,31 @@ async def _map_userinfo_to_user(
905907

906908
localpart = map_username_to_mxid_localpart(attributes["localpart"])
907909

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),
913934
)
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-
923935
await self._datastore.record_user_external_id(
924936
self._auth_provider_id, remote_user_id, registered_user_id,
925937
)

synapse/storage/databases/main/registration.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,15 @@ def f(txn):
393393

394394
async def get_user_by_external_id(
395395
self, auth_provider: str, external_id: str
396-
) -> str:
396+
) -> Optional[str]:
397397
"""Look up a user by their external auth id
398398
399399
Args:
400400
auth_provider: identifier for the remote auth provider
401401
external_id: id on that system
402402
403403
Returns:
404-
str|None: the mxid of the user, or None if they are not known
404+
the mxid of the user, or None if they are not known
405405
"""
406406
return await self.db_pool.simple_select_one_onecol(
407407
table="user_external_ids",

tests/handlers/test_oidc.py

+35
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,38 @@ def test_map_userinfo_to_user(self):
617617
)
618618
)
619619
self.assertEqual(mxid, "@test_user_2:test")
620+
621+
# Test if the mxid is already taken
622+
store = self.hs.get_datastore()
623+
user3 = UserID.from_string("@test_user_3:test")
624+
self.get_success(
625+
store.register_user(user_id=user3.to_string(), password_hash=None)
626+
)
627+
userinfo = {"sub": "test3", "username": "test_user_3"}
628+
e = self.get_failure(
629+
self.handler._map_userinfo_to_user(
630+
userinfo, token, "user-agent", "10.10.10.10"
631+
),
632+
MappingException,
633+
)
634+
self.assertEqual(str(e.value), "mxid '@test_user_3:test' is already taken")
635+
636+
@override_config({"oidc_config": {"allow_existing_users": True}})
637+
def test_map_userinfo_to_existing_user(self):
638+
"""Existing users can log in with OpenID Connect when allow_existing_users is True."""
639+
store = self.hs.get_datastore()
640+
user4 = UserID.from_string("@test_user_4:test")
641+
self.get_success(
642+
store.register_user(user_id=user4.to_string(), password_hash=None)
643+
)
644+
userinfo = {
645+
"sub": "test4",
646+
"username": "test_user_4",
647+
}
648+
token = {}
649+
mxid = self.get_success(
650+
self.handler._map_userinfo_to_user(
651+
userinfo, token, "user-agent", "10.10.10.10"
652+
)
653+
)
654+
self.assertEqual(mxid, "@test_user_4:test")

0 commit comments

Comments
 (0)