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

Commit 22c6c19

Browse files
authored
Fix a regression that mapping providers should be able to redirect users. (#8878)
This was broken in #8801.
1 parent 295c209 commit 22c6c19

File tree

6 files changed

+60
-8
lines changed

6 files changed

+60
-8
lines changed

changelog.d/8878.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a regression in v1.24.0rc1 which failed to allow SAML mapping providers which were unable to redirect users to an additional page.

docs/sso_mapping_providers.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ A custom mapping provider must specify the following methods:
168168
the value of `mxid_localpart`.
169169
* `emails` - A list of emails for the new user. If not provided, will
170170
default to an empty list.
171+
172+
Alternatively it can raise a `synapse.api.errors.RedirectException` to
173+
redirect the user to another page. This is useful to prompt the user for
174+
additional information, e.g. if you want them to provide their own username.
175+
It is the responsibility of the mapping provider to either redirect back
176+
to `client_redirect_url` (including any additional information) or to
177+
complete registration using methods from the `ModuleApi`.
171178

172179
### Default SAML Mapping Provider
173180

synapse/handlers/oidc_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ async def oidc_response_to_user_attributes(failures: int) -> UserAttributes:
888888
# continue to already be in use. Note that the error raised is
889889
# arbitrary and will get turned into a MappingException.
890890
if failures:
891-
raise RuntimeError(
891+
raise MappingException(
892892
"Mapping provider does not support de-duplicating Matrix IDs"
893893
)
894894

synapse/handlers/sso.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import attr
1919

20+
from synapse.api.errors import RedirectException
2021
from synapse.handlers._base import BaseHandler
2122
from synapse.http.server import respond_with_html
2223
from synapse.types import UserID, contains_invalid_mxid_characters
@@ -28,7 +29,9 @@
2829

2930

3031
class MappingException(Exception):
31-
"""Used to catch errors when mapping the UserInfo object
32+
"""Used to catch errors when mapping an SSO response to user attributes.
33+
34+
Note that the msg that is raised is shown to end-users.
3235
"""
3336

3437

@@ -145,6 +148,14 @@ async def get_mxid_from_sso(
145148
sso_to_matrix_id_mapper: A callable to generate the user attributes.
146149
The only parameter is an integer which represents the amount of
147150
times the returned mxid localpart mapping has failed.
151+
152+
It is expected that the mapper can raise two exceptions, which
153+
will get passed through to the caller:
154+
155+
MappingException if there was a problem mapping the response
156+
to the user.
157+
RedirectException to redirect to an additional page (e.g.
158+
to prompt the user for more information).
148159
grandfather_existing_users: A callable which can return an previously
149160
existing matrix ID. The SSO ID is then linked to the returned
150161
matrix ID.
@@ -154,8 +165,8 @@ async def get_mxid_from_sso(
154165
155166
Raises:
156167
MappingException if there was a problem mapping the response to a user.
157-
RedirectException: some mapping providers may raise this if they need
158-
to redirect to an interstitial page.
168+
RedirectException: if the mapping provider needs to redirect the user
169+
to an additional page. (e.g. to prompt for more information)
159170
160171
"""
161172
# first of all, check if we already have a mapping for this user
@@ -179,10 +190,16 @@ async def get_mxid_from_sso(
179190
for i in range(self._MAP_USERNAME_RETRIES):
180191
try:
181192
attributes = await sso_to_matrix_id_mapper(i)
193+
except (RedirectException, MappingException):
194+
# Mapping providers are allowed to issue a redirect (e.g. to ask
195+
# the user for more information) and can issue a mapping exception
196+
# if a name cannot be generated.
197+
raise
182198
except Exception as e:
199+
# Any other exception is unexpected.
183200
raise MappingException(
184-
"Could not extract user attributes from SSO response: " + str(e)
185-
)
201+
"Could not extract user attributes from SSO response."
202+
) from e
186203

187204
logger.debug(
188205
"Retrieved user attributes from user mapping provider: %r (attempt %d)",

tests/handlers/test_oidc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,7 @@ def test_map_userinfo_to_user(self):
705705
MappingException,
706706
)
707707
self.assertEqual(
708-
str(e.value),
709-
"Could not extract user attributes from SSO response: Mapping provider does not support de-duplicating Matrix IDs",
708+
str(e.value), "Mapping provider does not support de-duplicating Matrix IDs",
710709
)
711710

712711
@override_config({"oidc_config": {"allow_existing_users": True}})

tests/handlers/test_saml.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import attr
1616

17+
from synapse.api.errors import RedirectException
1718
from synapse.handlers.sso import MappingException
1819

1920
from tests.unittest import HomeserverTestCase, override_config
@@ -49,6 +50,13 @@ def saml_response_to_user_attributes(
4950
return {"mxid_localpart": localpart, "displayname": None}
5051

5152

53+
class TestRedirectMappingProvider(TestMappingProvider):
54+
def saml_response_to_user_attributes(
55+
self, saml_response, failures, client_redirect_url
56+
):
57+
raise RedirectException(b"https://custom-saml-redirect/")
58+
59+
5260
class SamlHandlerTestCase(HomeserverTestCase):
5361
def default_config(self):
5462
config = super().default_config()
@@ -166,3 +174,23 @@ def test_map_saml_response_to_user_retries(self):
166174
self.assertEqual(
167175
str(e.value), "Unable to generate a Matrix ID from the SSO response"
168176
)
177+
178+
@override_config(
179+
{
180+
"saml2_config": {
181+
"user_mapping_provider": {
182+
"module": __name__ + ".TestRedirectMappingProvider"
183+
},
184+
}
185+
}
186+
)
187+
def test_map_saml_response_redirect(self):
188+
saml_response = FakeAuthnResponse({"uid": "test", "username": "test_user"})
189+
redirect_url = ""
190+
e = self.get_failure(
191+
self.handler._map_saml_response_to_user(
192+
saml_response, redirect_url, "user-agent", "10.10.10.10"
193+
),
194+
RedirectException,
195+
)
196+
self.assertEqual(e.value.location, b"https://custom-saml-redirect/")

0 commit comments

Comments
 (0)