Skip to content

Commit 6a909aa

Browse files
Consolidate SSO redirects through /_matrix/client/v3/login/sso/redirect(/{idpId}) (#17972)
Consolidate SSO redirects through `/_matrix/client/v3/login/sso/redirect(/{idpId})` Spawning from element-hq/sbg#421 (comment) where we have a proxy that intercepts responses to `/_matrix/client/v3/login/sso/redirect(/{idpId})` in order to upgrade them to use OAuth 2.0 Pushed Authorization Requests (PAR). Instead of needing to intercept multiple endpoints that redirect to the authorization endpoint, it seems better to just have Synapse consolidate to a single flow. ### Testing strategy 1. Create a new OAuth application. I'll be using GitHub for example but there are [many options](https://github.com/matrix-org/synapse/blob/be65a8ec0195955c15fdb179c9158b187638e39a/docs/openid.md). Visit https://github.com/settings/developers -> **New OAuth App** - Application name: `Synapse local testing` - Homepage URL: `http://localhost:8008` - Authorization callback URL: `http://localhost:8008/_synapse/client/oidc/callback` 1. Update your Synapse `homeserver.yaml` ```yaml server_name: "my.synapse.server" public_baseurl: http://localhost:8008/ listeners: - port: 8008 bind_addresses: [ #'::1', '127.0.0.1' ] tls: false type: http x_forwarded: true resources: - names: [client, federation, metrics] compress: false # SSO login testing oidc_providers: - idp_id: github idp_name: Github idp_brand: "github" # optional: styling hint for clients discover: false issuer: "https://github.com/" client_id: "xxx" # TO BE FILLED client_secret: "xxx" # TO BE FILLED authorization_endpoint: "https://github.com/login/oauth/authorize" token_endpoint: "https://github.com/login/oauth/access_token" userinfo_endpoint: "https://api.github.com/user" scopes: ["read:user"] user_mapping_provider: config: subject_claim: "id" localpart_template: "{{ user.login }}" display_name_template: "{{ user.name }}" ``` 1. Start Synapse: `poetry run synapse_homeserver --config-path homeserver.yaml` 1. Visit `http://localhost:8008/_synapse/client/pick_idp?redirectUrl=http%3A%2F%2Fexample.com` 1. Choose GitHub 1. Notice that you're redirected to GitHub to sign in (`https://github.com/login/oauth/authorize?...`) Tested locally and works: 1. `http://localhost:8008/_synapse/client/pick_idp?idp=oidc-github&redirectUrl=http%3A//example.com` -> 1. `http://localhost:8008/_matrix/client/v3/login/sso/redirect/oidc-github?redirectUrl=http://example.com` -> 1. `https://github.com/login/oauth/authorize?response_type=code&client_id=xxx&redirect_uri=http%3A%2F%2Flocalhost%3A8008%2F_synapse%2Fclient%2Foidc%2Fcallback&scope=read%3Auser&state=xxx&nonce=xxx`
1 parent d80cd57 commit 6a909aa

File tree

8 files changed

+262
-28
lines changed

8 files changed

+262
-28
lines changed

changelog.d/17972.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Consolidate SSO redirects through `/_matrix/client/v3/login/sso/redirect(/{idpId})`.

synapse/api/urls.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
import hmac
2525
from hashlib import sha256
26-
from urllib.parse import urlencode
26+
from typing import Optional
27+
from urllib.parse import urlencode, urljoin
2728

2829
from synapse.config import ConfigError
2930
from synapse.config.homeserver import HomeServerConfig
@@ -66,3 +67,42 @@ def build_user_consent_uri(self, user_id: str) -> str:
6667
urlencode({"u": user_id, "h": mac}),
6768
)
6869
return consent_uri
70+
71+
72+
class LoginSSORedirectURIBuilder:
73+
def __init__(self, hs_config: HomeServerConfig):
74+
self._public_baseurl = hs_config.server.public_baseurl
75+
76+
def build_login_sso_redirect_uri(
77+
self, *, idp_id: Optional[str], client_redirect_url: str
78+
) -> str:
79+
"""Build a `/login/sso/redirect` URI for the given identity provider.
80+
81+
Builds `/_matrix/client/v3/login/sso/redirect/{idpId}?redirectUrl=xxx` when `idp_id` is specified.
82+
Otherwise, builds `/_matrix/client/v3/login/sso/redirect?redirectUrl=xxx` when `idp_id` is `None`.
83+
84+
Args:
85+
idp_id: Optional ID of the identity provider
86+
client_redirect_url: URL to redirect the user to after login
87+
88+
Returns
89+
The URI to follow when choosing a specific identity provider.
90+
"""
91+
base_url = urljoin(
92+
self._public_baseurl,
93+
f"{CLIENT_API_PREFIX}/v3/login/sso/redirect",
94+
)
95+
96+
serialized_query_parameters = urlencode({"redirectUrl": client_redirect_url})
97+
98+
if idp_id:
99+
resultant_url = urljoin(
100+
# We have to add a trailing slash to the base URL to ensure that the
101+
# last path segment is not stripped away when joining with another path.
102+
f"{base_url}/",
103+
f"{idp_id}?{serialized_query_parameters}",
104+
)
105+
else:
106+
resultant_url = f"{base_url}?{serialized_query_parameters}"
107+
108+
return resultant_url

synapse/config/cas.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#
2121
#
2222

23-
from typing import Any, List
23+
from typing import Any, List, Optional
2424

2525
from synapse.config.sso import SsoAttributeRequirement
2626
from synapse.types import JsonDict
@@ -46,7 +46,9 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
4646

4747
# TODO Update this to a _synapse URL.
4848
public_baseurl = self.root.server.public_baseurl
49-
self.cas_service_url = public_baseurl + "_matrix/client/r0/login/cas/ticket"
49+
self.cas_service_url: Optional[str] = (
50+
public_baseurl + "_matrix/client/r0/login/cas/ticket"
51+
)
5052

5153
self.cas_protocol_version = cas_config.get("protocol_version")
5254
if (

synapse/config/server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,14 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
332332
logger.info("Using default public_baseurl %s", public_baseurl)
333333
else:
334334
self.serve_client_wellknown = True
335+
# Ensure that public_baseurl ends with a trailing slash
335336
if public_baseurl[-1] != "/":
336337
public_baseurl += "/"
338+
339+
# Scrutinize user-provided config
340+
if not isinstance(public_baseurl, str):
341+
raise ConfigError("Must be a string", ("public_baseurl",))
342+
337343
self.public_baseurl = public_baseurl
338344

339345
# check that public_baseurl is valid

synapse/rest/synapse/client/pick_idp.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import logging
2222
from typing import TYPE_CHECKING
2323

24+
from synapse.api.urls import LoginSSORedirectURIBuilder
2425
from synapse.http.server import (
2526
DirectServeHtmlResource,
2627
finish_request,
@@ -49,32 +50,32 @@ def __init__(self, hs: "HomeServer"):
4950
hs.config.sso.sso_login_idp_picker_template
5051
)
5152
self._server_name = hs.hostname
53+
self._public_baseurl = hs.config.server.public_baseurl
54+
self._login_sso_redirect_url_builder = LoginSSORedirectURIBuilder(hs.config)
5255

5356
async def _async_render_GET(self, request: SynapseRequest) -> None:
5457
client_redirect_url = parse_string(
5558
request, "redirectUrl", required=True, encoding="utf-8"
5659
)
5760
idp = parse_string(request, "idp", required=False)
5861

59-
# if we need to pick an IdP, do so
62+
# If we need to pick an IdP, do so
6063
if not idp:
6164
return await self._serve_id_picker(request, client_redirect_url)
6265

63-
# otherwise, redirect to the IdP's redirect URI
64-
providers = self._sso_handler.get_identity_providers()
65-
auth_provider = providers.get(idp)
66-
if not auth_provider:
67-
logger.info("Unknown idp %r", idp)
68-
self._sso_handler.render_error(
69-
request, "unknown_idp", "Unknown identity provider ID"
66+
# Otherwise, redirect to the login SSO redirect endpoint for the given IdP
67+
# (which will in turn take us to the the IdP's redirect URI).
68+
#
69+
# We could go directly to the IdP's redirect URI, but this way we ensure that
70+
# the user goes through the same logic as normal flow. Additionally, if a proxy
71+
# needs to intercept the request, it only needs to intercept the one endpoint.
72+
sso_login_redirect_url = (
73+
self._login_sso_redirect_url_builder.build_login_sso_redirect_uri(
74+
idp_id=idp, client_redirect_url=client_redirect_url
7075
)
71-
return
72-
73-
sso_url = await auth_provider.handle_redirect_request(
74-
request, client_redirect_url.encode("utf8")
7576
)
76-
logger.info("Redirecting to %s", sso_url)
77-
request.redirect(sso_url)
77+
logger.info("Redirecting to %s", sso_login_redirect_url)
78+
request.redirect(sso_login_redirect_url)
7879
finish_request(request)
7980

8081
async def _serve_id_picker(

tests/api/test_urls.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#
2+
# This file is licensed under the Affero General Public License (AGPL) version 3.
3+
#
4+
# Copyright (C) 2024 New Vector, Ltd
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Affero General Public License as
8+
# published by the Free Software Foundation, either version 3 of the
9+
# License, or (at your option) any later version.
10+
#
11+
# See the GNU Affero General Public License for more details:
12+
# <https://www.gnu.org/licenses/agpl-3.0.html>.
13+
#
14+
15+
16+
from twisted.test.proto_helpers import MemoryReactor
17+
18+
from synapse.api.urls import LoginSSORedirectURIBuilder
19+
from synapse.server import HomeServer
20+
from synapse.util import Clock
21+
22+
from tests.unittest import HomeserverTestCase
23+
24+
# a (valid) url with some annoying characters in. %3D is =, %26 is &, %2B is +
25+
TRICKY_TEST_CLIENT_REDIRECT_URL = 'https://x?<ab c>&q"+%3D%2B"="fö%26=o"'
26+
27+
28+
class LoginSSORedirectURIBuilderTestCase(HomeserverTestCase):
29+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
30+
self.login_sso_redirect_url_builder = LoginSSORedirectURIBuilder(hs.config)
31+
32+
def test_no_idp_id(self) -> None:
33+
self.assertEqual(
34+
self.login_sso_redirect_url_builder.build_login_sso_redirect_uri(
35+
idp_id=None, client_redirect_url="http://example.com/redirect"
36+
),
37+
"https://test/_matrix/client/v3/login/sso/redirect?redirectUrl=http%3A%2F%2Fexample.com%2Fredirect",
38+
)
39+
40+
def test_explicit_idp_id(self) -> None:
41+
self.assertEqual(
42+
self.login_sso_redirect_url_builder.build_login_sso_redirect_uri(
43+
idp_id="oidc-github", client_redirect_url="http://example.com/redirect"
44+
),
45+
"https://test/_matrix/client/v3/login/sso/redirect/oidc-github?redirectUrl=http%3A%2F%2Fexample.com%2Fredirect",
46+
)
47+
48+
def test_tricky_redirect_uri(self) -> None:
49+
self.assertEqual(
50+
self.login_sso_redirect_url_builder.build_login_sso_redirect_uri(
51+
idp_id="oidc-github",
52+
client_redirect_url=TRICKY_TEST_CLIENT_REDIRECT_URL,
53+
),
54+
"https://test/_matrix/client/v3/login/sso/redirect/oidc-github?redirectUrl=https%3A%2F%2Fx%3F%3Cab+c%3E%26q%22%2B%253D%252B%22%3D%22f%C3%B6%2526%3Do%22",
55+
)

0 commit comments

Comments
 (0)