This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Refactor the CAS code to move the logic to a handler #7136
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1d3d6d4
Create a CAS handler.
clokep 41a204b
Add typing information.
clokep c034544
Add a changelog entry.
clokep fd0ce13
Remove unused class variable.
clokep 3d7e16d
Handle feedback given in #6634.
clokep e0b1992
Do not return an awaitable.
clokep 00dd903
Add docstrings / change function signatures.
clokep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Refactored the CAS authentication logic to a separate class. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2020 The Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
import xml.etree.ElementTree as ET | ||
from typing import Awaitable, Dict, Optional, Tuple | ||
|
||
from six.moves import urllib | ||
|
||
from twisted.web.client import PartialDownloadError | ||
|
||
from synapse.api.errors import Codes, LoginError | ||
from synapse.http.servlet import parse_string | ||
from synapse.http.site import SynapseRequest | ||
from synapse.types import UserID, map_username_to_mxid_localpart | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CasHandler: | ||
""" | ||
Utility class for to handle the response from a CAS SSO service. | ||
|
||
Args: | ||
hs (synapse.server.HomeServer) | ||
""" | ||
|
||
def __init__(self, hs): | ||
self._hostname = hs.hostname | ||
self._auth_handler = hs.get_auth_handler() | ||
self._registration_handler = hs.get_registration_handler() | ||
|
||
self._cas_server_url = hs.config.cas_server_url | ||
self._cas_service_url = hs.config.cas_service_url | ||
self._cas_displayname_attribute = hs.config.cas_displayname_attribute | ||
self._cas_required_attributes = hs.config.cas_required_attributes | ||
|
||
self._http_client = hs.get_proxied_http_client() | ||
|
||
# cast to tuple for use with str.startswith | ||
self._whitelisted_sso_clients = tuple(hs.config.sso_client_whitelist) | ||
|
||
def _build_service_param(self, client_redirect_url: str) -> str: | ||
return "%s%s?redirectUrl=%s" % ( | ||
self._cas_service_url, | ||
"/_matrix/client/r0/login/cas/ticket", | ||
urllib.parse.quote(client_redirect_url, safe=""), | ||
) | ||
|
||
def _handle_cas_response( | ||
self, request: SynapseRequest, cas_response_body: str, client_redirect_url: str | ||
) -> Awaitable: | ||
user, attributes = self._parse_cas_response(cas_response_body) | ||
displayname = attributes.pop(self._cas_displayname_attribute, None) | ||
|
||
for required_attribute, required_value in self._cas_required_attributes.items(): | ||
# If required attribute was not in CAS Response - Forbidden | ||
if required_attribute not in attributes: | ||
raise LoginError(401, "Unauthorized", errcode=Codes.UNAUTHORIZED) | ||
|
||
# Also need to check value | ||
if required_value is not None: | ||
actual_value = attributes[required_attribute] | ||
# If required attribute value does not match expected - Forbidden | ||
if required_value != actual_value: | ||
raise LoginError(401, "Unauthorized", errcode=Codes.UNAUTHORIZED) | ||
|
||
return self._on_successful_auth(user, request, client_redirect_url, displayname) | ||
|
||
def _parse_cas_response( | ||
self, cas_response_body: str | ||
) -> Tuple[str, Dict[str, Optional[str]]]: | ||
user = None | ||
attributes = {} | ||
try: | ||
root = ET.fromstring(cas_response_body) | ||
if not root.tag.endswith("serviceResponse"): | ||
raise Exception("root of CAS response is not serviceResponse") | ||
success = root[0].tag.endswith("authenticationSuccess") | ||
for child in root[0]: | ||
if child.tag.endswith("user"): | ||
user = child.text | ||
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] | ||
attributes[tag] = attribute.text | ||
if user is None: | ||
raise Exception("CAS response does not contain user") | ||
except Exception: | ||
logger.exception("Error parsing CAS response") | ||
raise LoginError(401, "Invalid CAS response", errcode=Codes.UNAUTHORIZED) | ||
if not success: | ||
raise LoginError( | ||
401, "Unsuccessful CAS response", errcode=Codes.UNAUTHORIZED | ||
) | ||
return user, attributes | ||
|
||
async def _on_successful_auth( | ||
self, | ||
username: str, | ||
request: SynapseRequest, | ||
client_redirect_url: str, | ||
user_display_name: Optional[str] = None, | ||
) -> None: | ||
"""Called once the user has successfully authenticated with the SSO. | ||
|
||
Registers the user if necessary, and then returns a redirect (with | ||
a login token) to the client. | ||
|
||
Args: | ||
username: the remote user id. We'll map this onto | ||
something sane for a MXID localpath. | ||
|
||
request: the incoming request from the browser. We'll | ||
respond to it with a redirect. | ||
|
||
client_redirect_url: the redirect_url the client gave us when | ||
it first started the process. | ||
|
||
user_display_name: if set, and we have to register a new user, | ||
we will set their displayname to this. | ||
|
||
Returns: | ||
Completes once we have handled the request. | ||
clokep marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
""" | ||
localpart = map_username_to_mxid_localpart(username) | ||
user_id = UserID(localpart, self._hostname).to_string() | ||
registered_user_id = await self._auth_handler.check_user_exists(user_id) | ||
if not registered_user_id: | ||
registered_user_id = await self._registration_handler.register_user( | ||
localpart=localpart, default_display_name=user_display_name | ||
) | ||
|
||
self._auth_handler.complete_sso_login( | ||
registered_user_id, request, client_redirect_url | ||
) | ||
|
||
def handle_redirect_request(self, client_redirect_url: str) -> str: | ||
args = urllib.parse.urlencode( | ||
{"service": self._build_service_param(client_redirect_url)} | ||
) | ||
|
||
return "%s/login?%s" % (self._cas_server_url, args) | ||
|
||
async def handle_ticket_request(self, request: SynapseRequest): | ||
client_redirect_url = parse_string(request, "redirectUrl", required=True) | ||
uri = self._cas_server_url + "/proxyValidate" | ||
args = { | ||
"ticket": parse_string(request, "ticket", required=True), | ||
"service": self._build_service_param(client_redirect_url), | ||
} | ||
clokep marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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 | ||
body = pde.response | ||
|
||
await self._handle_cas_response(request, body, client_redirect_url) | ||
clokep marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.