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

Commit 112f6bd

Browse files
committed
Merge tag 'v1.24.0rc2' into develop
Synapse 1.24.0rc2 (2020-12-04) ============================== Bugfixes -------- - Fix a regression in v1.24.0rc1 which failed to allow SAML mapping providers which were unable to redirect users to an additional page. ([\#8878](#8878)) Internal Changes ---------------- - Add support for the `prometheus_client` newer than 0.9.0. Contributed by Jordan Bancino. ([\#8875](#8875))
2 parents 6e4f71c + 2602514 commit 112f6bd

File tree

9 files changed

+81
-18
lines changed

9 files changed

+81
-18
lines changed

CHANGES.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
Synapse 1.24.0rc2 (2020-12-04)
2+
==============================
3+
4+
Bugfixes
5+
--------
6+
7+
- Fix a regression in v1.24.0rc1 which failed to allow SAML mapping providers which were unable to redirect users to an additional page. ([\#8878](https://github.com/matrix-org/synapse/issues/8878))
8+
9+
10+
Internal Changes
11+
----------------
12+
13+
- Add support for the `prometheus_client` newer than 0.9.0. Contributed by Jordan Bancino. ([\#8875](https://github.com/matrix-org/synapse/issues/8875))
14+
15+
116
Synapse 1.24.0rc1 (2020-12-02)
217
==============================
318

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ RUN pip install --prefix="/install" --no-warn-script-location \
3737
jaeger-client \
3838
opentracing \
3939
# Match the version constraints of Synapse
40-
"prometheus_client>=0.4.0,<0.9.0" \
40+
"prometheus_client>=0.4.0" \
4141
psycopg2 \
4242
pycparser \
4343
pyrsistent \

docs/sso_mapping_providers.md

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

174181
### Default SAML Mapping Provider
175182

synapse/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
except ImportError:
4949
pass
5050

51-
__version__ = "1.24.0rc1"
51+
__version__ = "1.24.0rc2"
5252

5353
if bool(os.environ.get("SYNAPSE_TEST_PATCH_LOG_CONTEXTS", False)):
5454
# We import here so that we don't have to install a bunch of deps when

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)",

synapse/python_dependencies.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
# Note that these both represent runtime dependencies (and the versions
4141
# installed are checked at runtime).
4242
#
43+
# Also note that we replicate these constraints in the Synapse Dockerfile while
44+
# pre-installing dependencies. If these constraints are updated here, the same
45+
# change should be made in the Dockerfile.
46+
#
4347
# [1] https://pip.pypa.io/en/stable/reference/pip_install/#requirement-specifiers.
4448

4549
REQUIREMENTS = [
@@ -69,14 +73,7 @@
6973
"msgpack>=0.5.2",
7074
"phonenumbers>=8.2.0",
7175
# we use GaugeHistogramMetric, which was added in prom-client 0.4.0.
72-
# prom-client has a history of breaking backwards compatibility between
73-
# minor versions (https://github.com/prometheus/client_python/issues/317),
74-
# so we also pin the minor version.
75-
#
76-
# Note that we replicate these constraints in the Synapse Dockerfile while
77-
# pre-installing dependencies. If these constraints are updated here, the
78-
# same change should be made in the Dockerfile.
79-
"prometheus_client>=0.4.0,<0.9.0",
76+
"prometheus_client>=0.4.0",
8077
# we use attr.validators.deep_iterable, which arrived in 19.1.0 (Note:
8178
# Fedora 31 only has 19.1, so if we want to upgrade we should wait until 33
8279
# is out in November.)

tests/handlers/test_oidc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,7 @@ def test_map_userinfo_to_user(self):
690690
MappingException,
691691
)
692692
self.assertEqual(
693-
str(e.value),
694-
"Could not extract user attributes from SSO response: Mapping provider does not support de-duplicating Matrix IDs",
693+
str(e.value), "Mapping provider does not support de-duplicating Matrix IDs",
695694
)
696695

697696
@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)