|
23 | 23 |
|
24 | 24 | from synapse.api.errors import SynapseError |
25 | 25 | from synapse.config import ConfigError |
| 26 | +from synapse.http.server import finish_request |
26 | 27 | from synapse.http.servlet import parse_string |
27 | 28 | from synapse.module_api import ModuleApi |
28 | 29 | from synapse.types import ( |
@@ -73,6 +74,8 @@ def __init__(self, hs): |
73 | 74 | # a lock on the mappings |
74 | 75 | self._mapping_lock = Linearizer(name="saml_mapping", clock=self._clock) |
75 | 76 |
|
| 77 | + self._error_html_content = hs.config.saml2_error_html_content |
| 78 | + |
76 | 79 | def handle_redirect_request(self, client_redirect_url): |
77 | 80 | """Handle an incoming request to /login/sso/redirect |
78 | 81 |
|
@@ -114,7 +117,22 @@ async def handle_saml_response(self, request): |
114 | 117 | # the dict. |
115 | 118 | self.expire_sessions() |
116 | 119 |
|
117 | | - user_id = await self._map_saml_response_to_user(resp_bytes, relay_state) |
| 120 | + try: |
| 121 | + user_id = await self._map_saml_response_to_user(resp_bytes, relay_state) |
| 122 | + except Exception as e: |
| 123 | + # If decoding the response or mapping it to a user failed, then log the |
| 124 | + # error and tell the user that something went wrong. |
| 125 | + logger.error(e) |
| 126 | + |
| 127 | + request.setResponseCode(400) |
| 128 | + request.setHeader(b"Content-Type", b"text/html; charset=utf-8") |
| 129 | + request.setHeader( |
| 130 | + b"Content-Length", b"%d" % (len(self._error_html_content),) |
| 131 | + ) |
| 132 | + request.write(self._error_html_content.encode("utf8")) |
| 133 | + finish_request(request) |
| 134 | + return |
| 135 | + |
118 | 136 | self._auth_handler.complete_sso_login(user_id, request, relay_state) |
119 | 137 |
|
120 | 138 | async def _map_saml_response_to_user(self, resp_bytes, client_redirect_url): |
|
0 commit comments