This repository has been 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
Implement MSC2858 support #9183
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
591b75b
Add some type annotations to HttpServer interface
richvdh 6daa63a
Advertise identity providers for login, per MSC2858.
richvdh 8158d2c
Allow clients to pick an IdP per MSC2858
richvdh 5c0db41
changelog
richvdh ad6ebf3
Fix mypy fail
richvdh b587fd0
Update changelog.d/9183.feature
richvdh 25939d2
Move msc2858 to an `experimental` config section
richvdh 0ba5153
switch around ExperimentalConfig logic
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 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 @@ | ||
Add experimental support for allowing clients to pick an SSO Identity Provider ([MSC2858](https://github.com/matrix-org/matrix-doc/pull/2858). |
This file contains 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 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,33 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2021 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. | ||
|
||
from synapse.config._base import Config | ||
from synapse.types import JsonDict | ||
|
||
|
||
class ExperimentalConfig(Config): | ||
"""Config section for enabling experimental features""" | ||
|
||
section = "experimental" | ||
|
||
# MSC2858 (multiple SSO identity providers) | ||
msc2858_enabled = False | ||
|
||
def read_config(self, config: JsonDict, **kwargs): | ||
experimental = config.get("experimental_features") | ||
if not experimental: | ||
return | ||
|
||
self.msc2858_enabled = experimental.get("msc2858_enabled", False) |
This file contains 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 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 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 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 |
---|---|---|
|
@@ -19,7 +19,8 @@ | |
from synapse.api.errors import Codes, LoginError, SynapseError | ||
from synapse.api.ratelimiting import Ratelimiter | ||
from synapse.appservice import ApplicationService | ||
from synapse.http.server import finish_request | ||
from synapse.handlers.sso import SsoIdentityProvider | ||
from synapse.http.server import HttpServer, finish_request | ||
from synapse.http.servlet import ( | ||
RestServlet, | ||
parse_json_object_from_request, | ||
|
@@ -60,11 +61,14 @@ def __init__(self, hs: "HomeServer"): | |
self.saml2_enabled = hs.config.saml2_enabled | ||
self.cas_enabled = hs.config.cas_enabled | ||
self.oidc_enabled = hs.config.oidc_enabled | ||
self._msc2858_enabled = hs.config.experimental.msc2858_enabled | ||
|
||
self.auth = hs.get_auth() | ||
|
||
self.auth_handler = self.hs.get_auth_handler() | ||
self.registration_handler = hs.get_registration_handler() | ||
self._sso_handler = hs.get_sso_handler() | ||
|
||
self._well_known_builder = WellKnownBuilder(hs) | ||
self._address_ratelimiter = Ratelimiter( | ||
clock=hs.get_clock(), | ||
|
@@ -89,8 +93,17 @@ def on_GET(self, request: SynapseRequest): | |
flows.append({"type": LoginRestServlet.CAS_TYPE}) | ||
|
||
if self.cas_enabled or self.saml2_enabled or self.oidc_enabled: | ||
flows.append({"type": LoginRestServlet.SSO_TYPE}) | ||
# While its valid for us to advertise this login type generally, | ||
sso_flow = {"type": LoginRestServlet.SSO_TYPE} # type: JsonDict | ||
|
||
if self._msc2858_enabled: | ||
sso_flow["org.matrix.msc2858.identity_providers"] = [ | ||
_get_auth_flow_dict_for_idp(idp) | ||
for idp in self._sso_handler.get_identity_providers().values() | ||
] | ||
|
||
flows.append(sso_flow) | ||
|
||
# While it's valid for us to advertise this login type generally, | ||
# synapse currently only gives out these tokens as part of the | ||
# SSO login flow. | ||
# Generally we don't want to advertise login flows that clients | ||
|
@@ -311,8 +324,20 @@ async def _do_jwt_login(self, login_submission: JsonDict) -> Dict[str, str]: | |
return result | ||
|
||
|
||
def _get_auth_flow_dict_for_idp(idp: SsoIdentityProvider) -> JsonDict: | ||
"""Return an entry for the login flow dict | ||
|
||
Returns an entry suitable for inclusion in "identity_providers" in the | ||
response to GET /_matrix/client/r0/login | ||
""" | ||
e = {"id": idp.idp_id, "name": idp.idp_name} # type: JsonDict | ||
if idp.idp_icon: | ||
e["icon"] = idp.idp_icon | ||
return e | ||
|
||
|
||
class SsoRedirectServlet(RestServlet): | ||
PATTERNS = client_patterns("/login/(cas|sso)/redirect", v1=True) | ||
PATTERNS = client_patterns("/login/(cas|sso)/redirect$", v1=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the absence of the $ was a minor bug. |
||
|
||
def __init__(self, hs: "HomeServer"): | ||
# make sure that the relevant handlers are instantiated, so that they | ||
|
@@ -324,13 +349,31 @@ def __init__(self, hs: "HomeServer"): | |
if hs.config.oidc_enabled: | ||
hs.get_oidc_handler() | ||
self._sso_handler = hs.get_sso_handler() | ||
self._msc2858_enabled = hs.config.experimental.msc2858_enabled | ||
|
||
def register(self, http_server: HttpServer) -> None: | ||
super().register(http_server) | ||
if self._msc2858_enabled: | ||
# expose additional endpoint for MSC2858 support | ||
http_server.register_paths( | ||
"GET", | ||
client_patterns( | ||
"/org.matrix.msc2858/login/sso/redirect/(?P<idp_id>[A-Za-z0-9_.~-]+)$", | ||
releases=(), | ||
unstable=True, | ||
), | ||
self.on_GET, | ||
self.__class__.__name__, | ||
) | ||
|
||
async def on_GET(self, request: SynapseRequest): | ||
async def on_GET( | ||
self, request: SynapseRequest, idp_id: Optional[str] = None | ||
) -> None: | ||
client_redirect_url = parse_string( | ||
request, "redirectUrl", required=True, encoding=None | ||
) | ||
sso_url = await self._sso_handler.handle_redirect_request( | ||
request, client_redirect_url | ||
request, client_redirect_url, idp_id, | ||
) | ||
logger.info("Redirecting to %s", sso_url) | ||
request.redirect(sso_url) | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not just do
experimental = config.get("experimental_features") or {}
, rather than returning here? I find it a bit confusing to definemsc2858_enabled
in multiple places (with potentially different defaults)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, fair