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

Commit 0eccf53

Browse files
authored
Use the SSO handler helpers for CAS registration/login. (#8856)
1 parent 168ba00 commit 0eccf53

File tree

4 files changed

+199
-39
lines changed

4 files changed

+199
-39
lines changed

changelog.d/8856.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Properly store the mapping of external ID to Matrix ID for CAS users.

synapse/handlers/cas_handler.py

+75-37
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from twisted.web.client import PartialDownloadError
2323

2424
from synapse.api.errors import HttpResponseException
25+
from synapse.handlers.sso import MappingException, UserAttributes
2526
from synapse.http.site import SynapseRequest
2627
from synapse.types import UserID, map_username_to_mxid_localpart
2728

@@ -62,6 +63,7 @@ class CasHandler:
6263
def __init__(self, hs: "HomeServer"):
6364
self.hs = hs
6465
self._hostname = hs.hostname
66+
self._store = hs.get_datastore()
6567
self._auth_handler = hs.get_auth_handler()
6668
self._registration_handler = hs.get_registration_handler()
6769

@@ -72,6 +74,9 @@ def __init__(self, hs: "HomeServer"):
7274

7375
self._http_client = hs.get_proxied_http_client()
7476

77+
# identifier for the external_ids table
78+
self._auth_provider_id = "cas"
79+
7580
self._sso_handler = hs.get_sso_handler()
7681

7782
def _build_service_param(self, args: Dict[str, str]) -> str:
@@ -267,6 +272,14 @@ async def _handle_cas_response(
267272
This should be the UI Auth session id.
268273
"""
269274

275+
# first check if we're doing a UIA
276+
if session:
277+
return await self._sso_handler.complete_sso_ui_auth_request(
278+
self._auth_provider_id, cas_response.username, session, request,
279+
)
280+
281+
# otherwise, we're handling a login request.
282+
270283
# Ensure that the attributes of the logged in user meet the required
271284
# attributes.
272285
for required_attribute, required_value in self._cas_required_attributes.items():
@@ -293,54 +306,79 @@ async def _handle_cas_response(
293306
)
294307
return
295308

296-
# Pull out the user-agent and IP from the request.
297-
user_agent = request.get_user_agent("")
298-
ip_address = self.hs.get_ip_from_request(request)
299-
300-
# Get the matrix ID from the CAS username.
301-
user_id = await self._map_cas_user_to_matrix_user(
302-
cas_response, user_agent, ip_address
303-
)
309+
# Call the mapper to register/login the user
304310

305-
if session:
306-
await self._auth_handler.complete_sso_ui_auth(
307-
user_id, session, request,
308-
)
309-
else:
310-
# If this not a UI auth request than there must be a redirect URL.
311-
assert client_redirect_url
311+
# If this not a UI auth request than there must be a redirect URL.
312+
assert client_redirect_url is not None
312313

313-
await self._auth_handler.complete_sso_login(
314-
user_id, request, client_redirect_url
315-
)
314+
try:
315+
await self._complete_cas_login(cas_response, request, client_redirect_url)
316+
except MappingException as e:
317+
logger.exception("Could not map user")
318+
self._sso_handler.render_error(request, "mapping_error", str(e))
316319

317-
async def _map_cas_user_to_matrix_user(
318-
self, cas_response: CasResponse, user_agent: str, ip_address: str,
319-
) -> str:
320+
async def _complete_cas_login(
321+
self,
322+
cas_response: CasResponse,
323+
request: SynapseRequest,
324+
client_redirect_url: str,
325+
) -> None:
320326
"""
321-
Given a CAS username, retrieve the user ID for it and possibly register the user.
327+
Given a CAS response, complete the login flow
328+
329+
Retrieves the remote user ID, registers the user if necessary, and serves
330+
a redirect back to the client with a login-token.
322331
323332
Args:
324333
cas_response: The parsed CAS response.
325-
user_agent: The user agent of the client making the request.
326-
ip_address: The IP address of the client making the request.
334+
request: The request to respond to
335+
client_redirect_url: The redirect URL passed in by the client.
327336
328-
Returns:
329-
The user ID associated with this response.
337+
Raises:
338+
MappingException if there was a problem mapping the response to a user.
339+
RedirectException: some mapping providers may raise this if they need
340+
to redirect to an interstitial page.
330341
"""
331-
342+
# Note that CAS does not support a mapping provider, so the logic is hard-coded.
332343
localpart = map_username_to_mxid_localpart(cas_response.username)
333-
user_id = UserID(localpart, self._hostname).to_string()
334-
registered_user_id = await self._auth_handler.check_user_exists(user_id)
335344

336-
displayname = cas_response.attributes.get(self._cas_displayname_attribute, None)
345+
async def cas_response_to_user_attributes(failures: int) -> UserAttributes:
346+
"""
347+
Map from CAS attributes to user attributes.
348+
"""
349+
# Due to the grandfathering logic matching any previously registered
350+
# mxids it isn't expected for there to be any failures.
351+
if failures:
352+
raise RuntimeError("CAS is not expected to de-duplicate Matrix IDs")
353+
354+
display_name = cas_response.attributes.get(
355+
self._cas_displayname_attribute, None
356+
)
357+
358+
return UserAttributes(localpart=localpart, display_name=display_name)
337359

338-
# If the user does not exist, register it.
339-
if not registered_user_id:
340-
registered_user_id = await self._registration_handler.register_user(
341-
localpart=localpart,
342-
default_display_name=displayname,
343-
user_agent_ips=[(user_agent, ip_address)],
360+
async def grandfather_existing_users() -> Optional[str]:
361+
# Since CAS did not always use the user_external_ids table, always
362+
# to attempt to map to existing users.
363+
user_id = UserID(localpart, self._hostname).to_string()
364+
365+
logger.debug(
366+
"Looking for existing account based on mapped %s", user_id,
344367
)
345368

346-
return registered_user_id
369+
users = await self._store.get_users_by_id_case_insensitive(user_id)
370+
if users:
371+
registered_user_id = list(users.keys())[0]
372+
logger.info("Grandfathering mapping to %s", registered_user_id)
373+
return registered_user_id
374+
375+
return None
376+
377+
await self._sso_handler.complete_sso_login_request(
378+
self._auth_provider_id,
379+
cas_response.username,
380+
request,
381+
client_redirect_url,
382+
cas_response_to_user_attributes,
383+
grandfather_existing_users,
384+
)

synapse/handlers/sso.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ async def complete_sso_login_request(
173173
request: SynapseRequest,
174174
client_redirect_url: str,
175175
sso_to_matrix_id_mapper: Callable[[int], Awaitable[UserAttributes]],
176-
grandfather_existing_users: Optional[Callable[[], Awaitable[Optional[str]]]],
176+
grandfather_existing_users: Callable[[], Awaitable[Optional[str]]],
177177
extra_login_attributes: Optional[JsonDict] = None,
178178
) -> None:
179179
"""
@@ -241,7 +241,7 @@ async def complete_sso_login_request(
241241
)
242242

243243
# Check for grandfathering of users.
244-
if not user_id and grandfather_existing_users:
244+
if not user_id:
245245
user_id = await grandfather_existing_users()
246246
if user_id:
247247
# Future logins should also match this user ID.

tests/handlers/test_cas.py

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Copyright 2020 The Matrix.org Foundation C.I.C.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from mock import Mock
15+
16+
from synapse.handlers.cas_handler import CasResponse
17+
18+
from tests.test_utils import simple_async_mock
19+
from tests.unittest import HomeserverTestCase
20+
21+
# These are a few constants that are used as config parameters in the tests.
22+
BASE_URL = "https://synapse/"
23+
SERVER_URL = "https://issuer/"
24+
25+
26+
class CasHandlerTestCase(HomeserverTestCase):
27+
def default_config(self):
28+
config = super().default_config()
29+
config["public_baseurl"] = BASE_URL
30+
cas_config = {
31+
"enabled": True,
32+
"server_url": SERVER_URL,
33+
"service_url": BASE_URL,
34+
}
35+
config["cas_config"] = cas_config
36+
37+
return config
38+
39+
def make_homeserver(self, reactor, clock):
40+
hs = self.setup_test_homeserver()
41+
42+
self.handler = hs.get_cas_handler()
43+
44+
# Reduce the number of attempts when generating MXIDs.
45+
sso_handler = hs.get_sso_handler()
46+
sso_handler._MAP_USERNAME_RETRIES = 3
47+
48+
return hs
49+
50+
def test_map_cas_user_to_user(self):
51+
"""Ensure that mapping the CAS user returned from a provider to an MXID works properly."""
52+
53+
# stub out the auth handler
54+
auth_handler = self.hs.get_auth_handler()
55+
auth_handler.complete_sso_login = simple_async_mock()
56+
57+
cas_response = CasResponse("test_user", {})
58+
request = _mock_request()
59+
self.get_success(
60+
self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
61+
)
62+
63+
# check that the auth handler got called as expected
64+
auth_handler.complete_sso_login.assert_called_once_with(
65+
"@test_user:test", request, "redirect_uri", None
66+
)
67+
68+
def test_map_cas_user_to_existing_user(self):
69+
"""Existing users can log in with CAS account."""
70+
store = self.hs.get_datastore()
71+
self.get_success(
72+
store.register_user(user_id="@test_user:test", password_hash=None)
73+
)
74+
75+
# stub out the auth handler
76+
auth_handler = self.hs.get_auth_handler()
77+
auth_handler.complete_sso_login = simple_async_mock()
78+
79+
# Map a user via SSO.
80+
cas_response = CasResponse("test_user", {})
81+
request = _mock_request()
82+
self.get_success(
83+
self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
84+
)
85+
86+
# check that the auth handler got called as expected
87+
auth_handler.complete_sso_login.assert_called_once_with(
88+
"@test_user:test", request, "redirect_uri", None
89+
)
90+
91+
# Subsequent calls should map to the same mxid.
92+
auth_handler.complete_sso_login.reset_mock()
93+
self.get_success(
94+
self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
95+
)
96+
auth_handler.complete_sso_login.assert_called_once_with(
97+
"@test_user:test", request, "redirect_uri", None
98+
)
99+
100+
def test_map_cas_user_to_invalid_localpart(self):
101+
"""CAS automaps invalid characters to base-64 encoding."""
102+
103+
# stub out the auth handler
104+
auth_handler = self.hs.get_auth_handler()
105+
auth_handler.complete_sso_login = simple_async_mock()
106+
107+
cas_response = CasResponse("föö", {})
108+
request = _mock_request()
109+
self.get_success(
110+
self.handler._handle_cas_response(request, cas_response, "redirect_uri", "")
111+
)
112+
113+
# check that the auth handler got called as expected
114+
auth_handler.complete_sso_login.assert_called_once_with(
115+
"@f=c3=b6=c3=b6:test", request, "redirect_uri", None
116+
)
117+
118+
119+
def _mock_request():
120+
"""Returns a mock which will stand in as a SynapseRequest"""
121+
return Mock(spec=["getClientIP", "get_user_agent"])

0 commit comments

Comments
 (0)