1717
1818import attr
1919
20+ from synapse .api .errors import RedirectException
2021from synapse .handlers ._base import BaseHandler
2122from synapse .http .server import respond_with_html
2223from synapse .types import UserID , contains_invalid_mxid_characters
2829
2930
3031class 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,9 +190,19 @@ 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.
200+ logger .error (
201+ "Unexpected error when extracting user attributes from SSO response: %s"
202+ % (e ,)
203+ )
183204 raise MappingException (
184- "Could not extract user attributes from SSO response: " + str ( e )
205+ "Could not extract user attributes from SSO response."
185206 )
186207
187208 logger .debug (
0 commit comments