2020-03-26 15:05:26 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2020-03-26 15:05:26 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import logging
|
2020-12-18 13:09:45 -05:00
|
|
|
import urllib.parse
|
2021-02-11 10:05:15 -05:00
|
|
|
from typing import TYPE_CHECKING, Dict, List, Optional
|
2020-07-05 11:32:02 -04:00
|
|
|
from xml.etree import ElementTree as ET
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-12-18 13:09:45 -05:00
|
|
|
import attr
|
|
|
|
|
2020-03-26 15:05:26 -04:00
|
|
|
from twisted.web.client import PartialDownloadError
|
|
|
|
|
2020-12-18 13:09:45 -05:00
|
|
|
from synapse.api.errors import HttpResponseException
|
2021-01-03 11:25:44 -05:00
|
|
|
from synapse.handlers.sso import MappingException, UserAttributes
|
2020-03-26 15:05:26 -04:00
|
|
|
from synapse.http.site import SynapseRequest
|
|
|
|
from synapse.types import UserID, map_username_to_mxid_localpart
|
|
|
|
|
2020-11-23 13:28:03 -05:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2020-11-23 13:28:03 -05:00
|
|
|
|
2020-03-26 15:05:26 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-12-18 13:09:45 -05:00
|
|
|
class CasError(Exception):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Used to catch errors when validating the CAS ticket."""
|
2020-12-18 13:09:45 -05:00
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
def __init__(self, error: str, error_description: Optional[str] = None):
|
2020-12-18 13:09:45 -05:00
|
|
|
self.error = error
|
|
|
|
self.error_description = error_description
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
def __str__(self) -> str:
|
2020-12-18 13:09:45 -05:00
|
|
|
if self.error_description:
|
2021-07-19 10:28:05 -04:00
|
|
|
return f"{self.error}: {self.error_description}"
|
2020-12-18 13:09:45 -05:00
|
|
|
return self.error
|
|
|
|
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
2020-12-18 13:09:45 -05:00
|
|
|
class CasResponse:
|
2021-09-20 08:56:23 -04:00
|
|
|
username: str
|
|
|
|
attributes: Dict[str, List[Optional[str]]]
|
2020-12-18 13:09:45 -05:00
|
|
|
|
|
|
|
|
2020-03-26 15:05:26 -04:00
|
|
|
class CasHandler:
|
|
|
|
"""
|
|
|
|
Utility class for to handle the response from a CAS SSO service.
|
|
|
|
|
|
|
|
Args:
|
2020-11-23 13:28:03 -05:00
|
|
|
hs
|
2020-03-26 15:05:26 -04:00
|
|
|
"""
|
|
|
|
|
2020-11-23 13:28:03 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-08-20 15:42:58 -04:00
|
|
|
self.hs = hs
|
2020-03-26 15:05:26 -04:00
|
|
|
self._hostname = hs.hostname
|
2022-02-23 06:04:02 -05:00
|
|
|
self._store = hs.get_datastores().main
|
2020-03-26 15:05:26 -04:00
|
|
|
self._auth_handler = hs.get_auth_handler()
|
|
|
|
self._registration_handler = hs.get_registration_handler()
|
|
|
|
|
2021-09-23 07:13:34 -04:00
|
|
|
self._cas_server_url = hs.config.cas.cas_server_url
|
|
|
|
self._cas_service_url = hs.config.cas.cas_service_url
|
2023-08-24 16:11:23 -04:00
|
|
|
self._cas_protocol_version = hs.config.cas.cas_protocol_version
|
2021-09-23 07:13:34 -04:00
|
|
|
self._cas_displayname_attribute = hs.config.cas.cas_displayname_attribute
|
|
|
|
self._cas_required_attributes = hs.config.cas.cas_required_attributes
|
2023-09-06 14:32:24 -04:00
|
|
|
self._cas_enable_registration = hs.config.cas.cas_enable_registration
|
2024-05-14 08:55:32 -04:00
|
|
|
self._cas_allow_numeric_ids = hs.config.cas.cas_allow_numeric_ids
|
|
|
|
self._cas_numeric_ids_prefix = hs.config.cas.cas_numeric_ids_prefix
|
2020-03-26 15:05:26 -04:00
|
|
|
|
|
|
|
self._http_client = hs.get_proxied_http_client()
|
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
# identifier for the external_ids table
|
2021-01-04 13:13:49 -05:00
|
|
|
self.idp_id = "cas"
|
2021-01-03 11:25:44 -05:00
|
|
|
|
2021-01-05 06:25:28 -05:00
|
|
|
# user-facing name of this auth provider
|
2023-08-11 16:15:17 -04:00
|
|
|
self.idp_name = hs.config.cas.idp_name
|
2021-01-05 06:25:28 -05:00
|
|
|
|
2023-08-11 16:15:17 -04:00
|
|
|
# MXC URI for icon for this auth provider
|
|
|
|
self.idp_icon = hs.config.cas.idp_icon
|
|
|
|
|
|
|
|
# optional brand identifier for this auth provider
|
|
|
|
self.idp_brand = hs.config.cas.idp_brand
|
2021-01-20 08:15:14 -05:00
|
|
|
|
2020-12-18 13:09:45 -05:00
|
|
|
self._sso_handler = hs.get_sso_handler()
|
|
|
|
|
2021-01-04 13:13:49 -05:00
|
|
|
self._sso_handler.register_identity_provider(self)
|
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
def _build_service_param(self, args: Dict[str, str]) -> str:
|
|
|
|
"""
|
|
|
|
Generates a value to use as the "service" parameter when redirecting or
|
|
|
|
querying the CAS service.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
args: Additional arguments to include in the final redirect URL.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The URL to use as a "service" parameter.
|
|
|
|
"""
|
2021-02-16 17:32:34 -05:00
|
|
|
return "%s?%s" % (
|
|
|
|
self._cas_service_url,
|
|
|
|
urllib.parse.urlencode(args),
|
|
|
|
)
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
async def _validate_ticket(
|
|
|
|
self, ticket: str, service_args: Dict[str, str]
|
2020-12-18 13:09:45 -05:00
|
|
|
) -> CasResponse:
|
2020-03-26 15:05:26 -04:00
|
|
|
"""
|
2020-12-18 13:09:45 -05:00
|
|
|
Validate a CAS ticket with the server, and return the parsed the response.
|
2020-03-26 15:05:26 -04:00
|
|
|
|
|
|
|
Args:
|
2020-04-03 15:35:05 -04:00
|
|
|
ticket: The CAS ticket from the client.
|
|
|
|
service_args: Additional arguments to include in the service URL.
|
2021-01-04 13:13:49 -05:00
|
|
|
Should be the same as those passed to `handle_redirect_request`.
|
2020-12-18 13:09:45 -05:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
CasError: If there's an error parsing the CAS response.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The parsed CAS response.
|
2020-03-26 15:05:26 -04:00
|
|
|
"""
|
2023-08-24 16:11:23 -04:00
|
|
|
if self._cas_protocol_version == 3:
|
|
|
|
uri = self._cas_server_url + "/p3/proxyValidate"
|
|
|
|
else:
|
|
|
|
uri = self._cas_server_url + "/proxyValidate"
|
2020-04-03 15:35:05 -04:00
|
|
|
args = {
|
|
|
|
"ticket": ticket,
|
|
|
|
"service": self._build_service_param(service_args),
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
body = await self._http_client.get_raw(uri, args)
|
|
|
|
except PartialDownloadError as pde:
|
|
|
|
# Twisted raises this error if the connection is closed,
|
|
|
|
# even if that's being used old-http style to signal end-of-data
|
2022-10-03 09:26:49 -04:00
|
|
|
# Assertion is for mypy's benefit. Error.response is Optional[bytes],
|
|
|
|
# but a PartialDownloadError should always have a non-None response.
|
|
|
|
assert pde.response is not None
|
2020-04-03 15:35:05 -04:00
|
|
|
body = pde.response
|
2020-12-18 13:09:45 -05:00
|
|
|
except HttpResponseException as e:
|
|
|
|
description = (
|
2021-09-20 08:56:23 -04:00
|
|
|
'Authorization server responded with a "{status}" error '
|
|
|
|
"while exchanging the authorization code."
|
|
|
|
).format(status=e.code)
|
2020-12-18 13:09:45 -05:00
|
|
|
raise CasError("server_error", description) from e
|
2020-04-03 15:35:05 -04:00
|
|
|
|
2020-12-18 13:09:45 -05:00
|
|
|
return self._parse_cas_response(body)
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-12-18 13:09:45 -05:00
|
|
|
def _parse_cas_response(self, cas_response_body: bytes) -> CasResponse:
|
2020-03-26 15:05:26 -04:00
|
|
|
"""
|
|
|
|
Retrieve the user and other parameters from the CAS response.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
cas_response_body: The response from the CAS query.
|
|
|
|
|
2020-12-18 13:09:45 -05:00
|
|
|
Raises:
|
|
|
|
CasError: If there's an error parsing the CAS response.
|
|
|
|
|
2020-03-26 15:05:26 -04:00
|
|
|
Returns:
|
2020-12-18 13:09:45 -05:00
|
|
|
The parsed CAS response.
|
2020-03-26 15:05:26 -04:00
|
|
|
"""
|
2020-12-18 13:09:45 -05:00
|
|
|
|
|
|
|
# Ensure the response is valid.
|
|
|
|
root = ET.fromstring(cas_response_body)
|
|
|
|
if not root.tag.endswith("serviceResponse"):
|
|
|
|
raise CasError(
|
|
|
|
"missing_service_response",
|
|
|
|
"root of CAS response is not serviceResponse",
|
|
|
|
)
|
|
|
|
|
|
|
|
success = root[0].tag.endswith("authenticationSuccess")
|
|
|
|
if not success:
|
|
|
|
raise CasError("unsucessful_response", "Unsuccessful CAS response")
|
|
|
|
|
|
|
|
# Iterate through the nodes and pull out the user and any extra attributes.
|
2020-03-26 15:05:26 -04:00
|
|
|
user = None
|
2021-07-16 13:22:36 -04:00
|
|
|
attributes: Dict[str, List[Optional[str]]] = {}
|
2020-12-18 13:09:45 -05:00
|
|
|
for child in root[0]:
|
|
|
|
if child.tag.endswith("user"):
|
|
|
|
user = child.text
|
2024-05-14 08:55:32 -04:00
|
|
|
# if numeric user IDs are allowed and username is numeric then we add the prefix so Synapse can handle it
|
|
|
|
if self._cas_allow_numeric_ids and user is not None and user.isdigit():
|
|
|
|
user = f"{self._cas_numeric_ids_prefix}{user}"
|
2020-12-18 13:09:45 -05:00
|
|
|
if child.tag.endswith("attributes"):
|
|
|
|
for attribute in child:
|
|
|
|
# ElementTree library expands the namespace in
|
|
|
|
# attribute tags to the full URL of the namespace.
|
|
|
|
# We don't care about namespace here and it will always
|
|
|
|
# be encased in curly braces, so we remove them.
|
|
|
|
tag = attribute.tag
|
|
|
|
if "}" in tag:
|
|
|
|
tag = tag.split("}")[1]
|
2021-02-11 10:05:15 -05:00
|
|
|
attributes.setdefault(tag, []).append(attribute.text)
|
2020-12-18 13:09:45 -05:00
|
|
|
|
|
|
|
# Ensure a user was found.
|
|
|
|
if user is None:
|
|
|
|
raise CasError("no_user", "CAS response does not contain user")
|
|
|
|
|
|
|
|
return CasResponse(user, attributes)
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2021-01-04 13:13:49 -05:00
|
|
|
async def handle_redirect_request(
|
|
|
|
self,
|
|
|
|
request: SynapseRequest,
|
|
|
|
client_redirect_url: Optional[bytes],
|
|
|
|
ui_auth_session_id: Optional[str] = None,
|
|
|
|
) -> str:
|
|
|
|
"""Generates a URL for the CAS server where the client should be redirected.
|
2020-03-26 15:05:26 -04:00
|
|
|
|
|
|
|
Args:
|
2021-01-04 13:13:49 -05:00
|
|
|
request: the incoming HTTP request
|
|
|
|
client_redirect_url: the URL that we should redirect the
|
|
|
|
client to after login (or None for UI Auth).
|
|
|
|
ui_auth_session_id: The session ID of the ongoing UI Auth (or
|
|
|
|
None if this is a login).
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
Returns:
|
2021-01-04 13:13:49 -05:00
|
|
|
URL to redirect to
|
2020-04-03 15:35:05 -04:00
|
|
|
"""
|
2021-01-04 13:13:49 -05:00
|
|
|
|
|
|
|
if ui_auth_session_id:
|
|
|
|
service_args = {"session": ui_auth_session_id}
|
|
|
|
else:
|
|
|
|
assert client_redirect_url
|
|
|
|
service_args = {"redirectUrl": client_redirect_url.decode("utf8")}
|
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
args = urllib.parse.urlencode(
|
|
|
|
{"service": self._build_service_param(service_args)}
|
|
|
|
)
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
return "%s/login?%s" % (self._cas_server_url, args)
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
async def handle_ticket(
|
|
|
|
self,
|
|
|
|
request: SynapseRequest,
|
|
|
|
ticket: str,
|
|
|
|
client_redirect_url: Optional[str],
|
|
|
|
session: Optional[str],
|
|
|
|
) -> None:
|
2020-03-26 15:05:26 -04:00
|
|
|
"""
|
2020-04-03 15:35:05 -04:00
|
|
|
Called once the user has successfully authenticated with the SSO.
|
|
|
|
Validates a CAS ticket sent by the client and completes the auth process.
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
If the user interactive authentication session is provided, marks the
|
|
|
|
UI Auth session as complete, then returns an HTML page notifying the
|
|
|
|
user they are done.
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
Otherwise, this registers the user if necessary, and then returns a
|
|
|
|
redirect (with a login token) to the client.
|
2020-03-26 15:05:26 -04:00
|
|
|
|
|
|
|
Args:
|
2020-04-03 15:35:05 -04:00
|
|
|
request: the incoming request from the browser. We'll
|
|
|
|
respond to it with a redirect or an HTML page.
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
ticket: The CAS ticket provided by the client.
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
client_redirect_url: the redirectUrl parameter from the `/cas/ticket` HTTP request, if given.
|
|
|
|
This should be the same as the redirectUrl from the original `/login/sso/redirect` request.
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
session: The session parameter from the `/cas/ticket` HTTP request, if given.
|
|
|
|
This should be the UI Auth session id.
|
2020-03-26 15:05:26 -04:00
|
|
|
"""
|
2020-04-03 15:35:05 -04:00
|
|
|
args = {}
|
|
|
|
if client_redirect_url:
|
|
|
|
args["redirectUrl"] = client_redirect_url
|
|
|
|
if session:
|
|
|
|
args["session"] = session
|
2020-12-18 13:09:45 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
cas_response = await self._validate_ticket(ticket, args)
|
|
|
|
except CasError as e:
|
|
|
|
logger.exception("Could not validate ticket")
|
|
|
|
self._sso_handler.render_error(request, e.error, e.error_description, 401)
|
|
|
|
return
|
|
|
|
|
|
|
|
await self._handle_cas_response(
|
|
|
|
request, cas_response, client_redirect_url, session
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _handle_cas_response(
|
|
|
|
self,
|
|
|
|
request: SynapseRequest,
|
|
|
|
cas_response: CasResponse,
|
|
|
|
client_redirect_url: Optional[str],
|
|
|
|
session: Optional[str],
|
|
|
|
) -> None:
|
|
|
|
"""Handle a CAS response to a ticket request.
|
|
|
|
|
|
|
|
Assumes that the response has been validated. Maps the user onto an MXID,
|
|
|
|
registering them if necessary, and returns a response to the browser.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request: the incoming request from the browser. We'll respond to it with an
|
|
|
|
HTML page or a redirect
|
|
|
|
|
|
|
|
cas_response: The parsed CAS response.
|
|
|
|
|
|
|
|
client_redirect_url: the redirectUrl parameter from the `/cas/ticket` HTTP request, if given.
|
|
|
|
This should be the same as the redirectUrl from the original `/login/sso/redirect` request.
|
|
|
|
|
|
|
|
session: The session parameter from the `/cas/ticket` HTTP request, if given.
|
|
|
|
This should be the UI Auth session id.
|
|
|
|
"""
|
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
# first check if we're doing a UIA
|
|
|
|
if session:
|
|
|
|
return await self._sso_handler.complete_sso_ui_auth_request(
|
2021-02-16 17:32:34 -05:00
|
|
|
self.idp_id,
|
|
|
|
cas_response.username,
|
|
|
|
session,
|
|
|
|
request,
|
2021-01-03 11:25:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
# otherwise, we're handling a login request.
|
|
|
|
|
2020-12-18 13:09:45 -05:00
|
|
|
# Ensure that the attributes of the logged in user meet the required
|
|
|
|
# attributes.
|
2021-02-11 10:05:15 -05:00
|
|
|
if not self._sso_handler.check_required_attributes(
|
|
|
|
request, cas_response.attributes, self._cas_required_attributes
|
|
|
|
):
|
|
|
|
return
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
# Call the mapper to register/login the user
|
2020-03-26 15:05:26 -04:00
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
# If this not a UI auth request than there must be a redirect URL.
|
|
|
|
assert client_redirect_url is not None
|
2020-04-03 15:35:05 -04:00
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
try:
|
|
|
|
await self._complete_cas_login(cas_response, request, client_redirect_url)
|
|
|
|
except MappingException as e:
|
|
|
|
logger.exception("Could not map user")
|
|
|
|
self._sso_handler.render_error(request, "mapping_error", str(e))
|
2020-11-23 13:28:03 -05:00
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
async def _complete_cas_login(
|
|
|
|
self,
|
|
|
|
cas_response: CasResponse,
|
|
|
|
request: SynapseRequest,
|
|
|
|
client_redirect_url: str,
|
|
|
|
) -> None:
|
2020-11-23 13:28:03 -05:00
|
|
|
"""
|
2021-01-03 11:25:44 -05:00
|
|
|
Given a CAS response, complete the login flow
|
|
|
|
|
|
|
|
Retrieves the remote user ID, registers the user if necessary, and serves
|
|
|
|
a redirect back to the client with a login-token.
|
2020-11-23 13:28:03 -05:00
|
|
|
|
|
|
|
Args:
|
2020-12-18 13:09:45 -05:00
|
|
|
cas_response: The parsed CAS response.
|
2021-01-03 11:25:44 -05:00
|
|
|
request: The request to respond to
|
|
|
|
client_redirect_url: The redirect URL passed in by the client.
|
2020-11-23 13:28:03 -05:00
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
Raises:
|
|
|
|
MappingException if there was a problem mapping the response to a user.
|
|
|
|
RedirectException: some mapping providers may raise this if they need
|
|
|
|
to redirect to an interstitial page.
|
2020-11-23 13:28:03 -05:00
|
|
|
"""
|
2021-01-03 11:25:44 -05:00
|
|
|
# Note that CAS does not support a mapping provider, so the logic is hard-coded.
|
2020-12-18 13:09:45 -05:00
|
|
|
localpart = map_username_to_mxid_localpart(cas_response.username)
|
2020-11-23 13:28:03 -05:00
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
async def cas_response_to_user_attributes(failures: int) -> UserAttributes:
|
|
|
|
"""
|
|
|
|
Map from CAS attributes to user attributes.
|
|
|
|
"""
|
|
|
|
# Due to the grandfathering logic matching any previously registered
|
|
|
|
# mxids it isn't expected for there to be any failures.
|
|
|
|
if failures:
|
|
|
|
raise RuntimeError("CAS is not expected to de-duplicate Matrix IDs")
|
|
|
|
|
2021-02-11 10:05:15 -05:00
|
|
|
# Arbitrarily use the first attribute found.
|
2021-01-03 11:25:44 -05:00
|
|
|
display_name = cas_response.attributes.get(
|
2021-02-11 10:05:15 -05:00
|
|
|
self._cas_displayname_attribute, [None]
|
|
|
|
)[0]
|
2021-01-03 11:25:44 -05:00
|
|
|
|
|
|
|
return UserAttributes(localpart=localpart, display_name=display_name)
|
2020-12-18 13:09:45 -05:00
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
async def grandfather_existing_users() -> Optional[str]:
|
|
|
|
# Since CAS did not always use the user_external_ids table, always
|
|
|
|
# to attempt to map to existing users.
|
|
|
|
user_id = UserID(localpart, self._hostname).to_string()
|
|
|
|
|
|
|
|
logger.debug(
|
2021-02-16 17:32:34 -05:00
|
|
|
"Looking for existing account based on mapped %s",
|
|
|
|
user_id,
|
2020-11-23 13:28:03 -05:00
|
|
|
)
|
|
|
|
|
2021-01-03 11:25:44 -05:00
|
|
|
users = await self._store.get_users_by_id_case_insensitive(user_id)
|
|
|
|
if users:
|
|
|
|
registered_user_id = list(users.keys())[0]
|
|
|
|
logger.info("Grandfathering mapping to %s", registered_user_id)
|
|
|
|
return registered_user_id
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
await self._sso_handler.complete_sso_login_request(
|
2021-01-04 13:13:49 -05:00
|
|
|
self.idp_id,
|
2021-01-03 11:25:44 -05:00
|
|
|
cas_response.username,
|
|
|
|
request,
|
|
|
|
client_redirect_url,
|
|
|
|
cas_response_to_user_attributes,
|
|
|
|
grandfather_existing_users,
|
2023-09-06 14:32:24 -04:00
|
|
|
registration_enabled=self._cas_enable_registration,
|
2021-01-03 11:25:44 -05:00
|
|
|
)
|