This repository was archived by the owner on Apr 26, 2024. It is now read-only.
Repository navigation
Complete the SAML2 implementation #5422
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dc3e586
SAML2 Improvements and redirect stuff
galexrt 69a43d9
Merge remote-tracking branch 'origin/develop' into rav/saml2_client
richvdh 4260492
Code cleanups and simplifications.
richvdh b55e884
Merge tag 'v1.0.0rc3' into rav/saml2_client
richvdh 880005c
changelog
richvdh a4daa89
Merge branch 'develop' into rav/saml2_client
richvdh 0ade403
Revert redundant changes to static js
richvdh 3705322
Move all the saml stuff out to a centralised handler
richvdh 36f4953
Add support for tracking SAML2 sessions.
richvdh a0acfcc
update sample config
richvdh 28db0ae
cleanups
richvdh dde4118
update sample config
richvdh 79b9d90
rename BaseSSORedirectServlet for consistency
richvdh 3bcb13e
Address review comments
richvdh b4fd86a
Merge branch 'develop' into rav/saml2_client
richvdh 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 @@ | ||
| Fully support SAML2 authentication. Contributed by [Alexander Trost](https://github.com/galexrt) - thank you! |
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
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
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,123 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright 2019 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 attr | ||
| import saml2 | ||
| from saml2.client import Saml2Client | ||
|
|
||
| from synapse.api.errors import SynapseError | ||
| from synapse.http.servlet import parse_string | ||
| from synapse.rest.client.v1.login import SSOAuthHandler | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class SamlHandler: | ||
| def __init__(self, hs): | ||
| self._saml_client = Saml2Client(hs.config.saml2_sp_config) | ||
| self._sso_auth_handler = SSOAuthHandler(hs) | ||
|
|
||
| # a map from saml session id to Saml2SessionData object | ||
| self._outstanding_requests_dict = {} | ||
|
|
||
| self._clock = hs.get_clock() | ||
| self._saml2_session_lifetime = hs.config.saml2_session_lifetime | ||
|
|
||
| def handle_redirect_request(self, client_redirect_url): | ||
| """Handle an incoming request to /login/sso/redirect | ||
|
|
||
| Args: | ||
| client_redirect_url (bytes): the URL that we should redirect the | ||
| client to when everything is done | ||
|
|
||
| Returns: | ||
| bytes: URL to redirect to | ||
| """ | ||
| reqid, info = self._saml_client.prepare_for_authenticate( | ||
| relay_state=client_redirect_url | ||
| ) | ||
|
|
||
| now = self._clock.time_msec() | ||
| self._outstanding_requests_dict[reqid] = Saml2SessionData(creation_time=now) | ||
|
|
||
| for key, value in info["headers"]: | ||
| if key == "Location": | ||
| return value | ||
|
|
||
| # this shouldn't happen! | ||
| raise Exception("prepare_for_authenticate didn't return a Location header") | ||
|
|
||
| def handle_saml_response(self, request): | ||
| """Handle an incoming request to /_matrix/saml2/authn_response | ||
|
|
||
| Args: | ||
| request (SynapseRequest): the incoming request from the browser. We'll | ||
| respond to it with a redirect. | ||
|
|
||
| Returns: | ||
| Deferred[none]: Completes once we have handled the request. | ||
| """ | ||
| resp_bytes = parse_string(request, "SAMLResponse", required=True) | ||
| relay_state = parse_string(request, "RelayState", required=True) | ||
|
|
||
| # expire outstanding sessions before parse_authn_request_response checks | ||
| # the dict. | ||
| self.expire_sessions() | ||
|
|
||
| try: | ||
| saml2_auth = self._saml_client.parse_authn_request_response( | ||
| resp_bytes, | ||
| saml2.BINDING_HTTP_POST, | ||
| outstanding=self._outstanding_requests_dict, | ||
| ) | ||
| except Exception as e: | ||
| logger.warning("Exception parsing SAML2 response: %s", e) | ||
| raise SynapseError(400, "Unable to parse SAML2 response: %s" % (e,)) | ||
|
|
||
| if saml2_auth.not_signed: | ||
| logger.warning("SAML2 response was not signed") | ||
| raise SynapseError(400, "SAML2 response was not signed") | ||
|
|
||
| if "uid" not in saml2_auth.ava: | ||
| logger.warning("SAML2 response lacks a 'uid' attestation") | ||
| raise SynapseError(400, "uid not in SAML2 response") | ||
|
|
||
| self._outstanding_requests_dict.pop(saml2_auth.in_response_to, None) | ||
|
|
||
| username = saml2_auth.ava["uid"][0] | ||
| displayName = saml2_auth.ava.get("displayName", [None])[0] | ||
|
|
||
| return self._sso_auth_handler.on_successful_auth( | ||
| username, request, relay_state, user_display_name=displayName | ||
| ) | ||
|
|
||
| def expire_sessions(self): | ||
| expire_before = self._clock.time_msec() - self._saml2_session_lifetime | ||
| to_expire = set() | ||
| for reqid, data in self._outstanding_requests_dict.items(): | ||
| if data.creation_time < expire_before: | ||
| to_expire.add(reqid) | ||
| for reqid in to_expire: | ||
| logger.debug("Expiring session id %s", reqid) | ||
| del self._outstanding_requests_dict[reqid] | ||
|
|
||
|
|
||
| @attr.s | ||
| class Saml2SessionData: | ||
| """Data we track about SAML2 sessions""" | ||
|
|
||
| # time the session was created, in milliseconds | ||
| creation_time = attr.ib() | ||
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
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.