|
21 | 21 | import logging
|
22 | 22 | from typing import TYPE_CHECKING
|
23 | 23 |
|
| 24 | +from synapse.api.urls import LoginSSORedirectURIBuilder |
24 | 25 | from synapse.http.server import (
|
25 | 26 | DirectServeHtmlResource,
|
26 | 27 | finish_request,
|
@@ -49,32 +50,32 @@ def __init__(self, hs: "HomeServer"):
|
49 | 50 | hs.config.sso.sso_login_idp_picker_template
|
50 | 51 | )
|
51 | 52 | self._server_name = hs.hostname
|
| 53 | + self._public_baseurl = hs.config.server.public_baseurl |
| 54 | + self._login_sso_redirect_url_builder = LoginSSORedirectURIBuilder(hs.config) |
52 | 55 |
|
53 | 56 | async def _async_render_GET(self, request: SynapseRequest) -> None:
|
54 | 57 | client_redirect_url = parse_string(
|
55 | 58 | request, "redirectUrl", required=True, encoding="utf-8"
|
56 | 59 | )
|
57 | 60 | idp = parse_string(request, "idp", required=False)
|
58 | 61 |
|
59 |
| - # if we need to pick an IdP, do so |
| 62 | + # If we need to pick an IdP, do so |
60 | 63 | if not idp:
|
61 | 64 | return await self._serve_id_picker(request, client_redirect_url)
|
62 | 65 |
|
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 |
70 | 75 | )
|
71 |
| - return |
72 |
| - |
73 |
| - sso_url = await auth_provider.handle_redirect_request( |
74 |
| - request, client_redirect_url.encode("utf8") |
75 | 76 | )
|
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) |
78 | 79 | finish_request(request)
|
79 | 80 |
|
80 | 81 | async def _serve_id_picker(
|
|
0 commit comments