Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 054c231

Browse files
authored
Use a template for the SSO success page to allow for customization. (#7279)
1 parent 701788a commit 054c231

6 files changed

Lines changed: 66 additions & 37 deletions

File tree

CHANGES.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
Next version
22
============
33

4-
* Two new templates (`sso_auth_confirm.html` and `sso_account_deactivated.html`)
5-
were added to Synapse. If your Synapse is configured to use SSO and a custom
6-
`sso_redirect_confirm_template_dir` configuration then these templates will
7-
need to be duplicated into that directory.
4+
* New templates (`sso_auth_confirm.html`, `sso_auth_success.html`, and
5+
`sso_account_deactivated.html`) were added to Synapse. If your Synapse is
6+
configured to use SSO and a custom `sso_redirect_confirm_template_dir`
7+
configuration then these templates will need to be duplicated into that
8+
directory.
89

910
* Plugins using the `complete_sso_login` method of `synapse.module_api.ModuleApi`
1011
should update to using the async/await version `complete_sso_login_async` which

changelog.d/7279.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support SSO in the user interactive authentication workflow.

synapse/config/sso.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def read_config(self, config, **kwargs):
4343
),
4444
"sso_account_deactivated_template",
4545
)
46+
self.sso_auth_success_template = self.read_file(
47+
os.path.join(
48+
self.sso_redirect_confirm_template_dir, "sso_auth_success.html"
49+
),
50+
"sso_auth_success_template",
51+
)
4652

4753
self.sso_client_whitelist = sso_config.get("client_whitelist") or []
4854

synapse/handlers/auth.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,6 @@
5151
logger = logging.getLogger(__name__)
5252

5353

54-
SUCCESS_TEMPLATE = """
55-
<html>
56-
<head>
57-
<title>Success!</title>
58-
<meta name='viewport' content='width=device-width, initial-scale=1,
59-
user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
60-
<link rel="stylesheet" href="/_matrix/static/client/register/style.css">
61-
<script>
62-
if (window.onAuthDone) {
63-
window.onAuthDone();
64-
} else if (window.opener && window.opener.postMessage) {
65-
window.opener.postMessage("authDone", "*");
66-
}
67-
</script>
68-
</head>
69-
<body>
70-
<div>
71-
<p>Thank you</p>
72-
<p>You may now close this window and return to the application</p>
73-
</div>
74-
</body>
75-
</html>
76-
"""
77-
78-
7954
class AuthHandler(BaseHandler):
8055
SESSION_EXPIRE_MS = 48 * 60 * 60 * 1000
8156

@@ -159,6 +134,11 @@ def __init__(self, hs):
159134
self._sso_auth_confirm_template = load_jinja2_templates(
160135
hs.config.sso_redirect_confirm_template_dir, ["sso_auth_confirm.html"],
161136
)[0]
137+
# The following template is shown after a successful user interactive
138+
# authentication session. It tells the user they can close the window.
139+
self._sso_auth_success_template = hs.config.sso_auth_success_template
140+
# The following template is shown during the SSO authentication process if
141+
# the account is deactivated.
162142
self._sso_account_deactivated_template = (
163143
hs.config.sso_account_deactivated_template
164144
)
@@ -1080,7 +1060,7 @@ def complete_sso_ui_auth(
10801060
self._save_session(sess)
10811061

10821062
# Render the HTML and return.
1083-
html_bytes = SUCCESS_TEMPLATE.encode("utf8")
1063+
html_bytes = self._sso_auth_success_template.encode("utf-8")
10841064
request.setResponseCode(200)
10851065
request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
10861066
request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
@@ -1106,12 +1086,12 @@ async def complete_sso_login(
11061086
# flow.
11071087
deactivated = await self.store.get_user_deactivated_status(registered_user_id)
11081088
if deactivated:
1109-
html = self._sso_account_deactivated_template.encode("utf-8")
1089+
html_bytes = self._sso_account_deactivated_template.encode("utf-8")
11101090

11111091
request.setResponseCode(403)
11121092
request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
1113-
request.setHeader(b"Content-Length", b"%d" % (len(html),))
1114-
request.write(html)
1093+
request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
1094+
request.write(html_bytes)
11151095
finish_request(request)
11161096
return
11171097

@@ -1153,16 +1133,16 @@ def _complete_sso_login(
11531133
# URL we redirect users to.
11541134
redirect_url_no_params = client_redirect_url.split("?")[0]
11551135

1156-
html = self._sso_redirect_confirm_template.render(
1136+
html_bytes = self._sso_redirect_confirm_template.render(
11571137
display_url=redirect_url_no_params,
11581138
redirect_url=redirect_url,
11591139
server_name=self._server_name,
11601140
).encode("utf-8")
11611141

11621142
request.setResponseCode(200)
11631143
request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
1164-
request.setHeader(b"Content-Length", b"%d" % (len(html),))
1165-
request.write(html)
1144+
request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
1145+
request.write(html_bytes)
11661146
finish_request(request)
11671147

11681148
@staticmethod
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html>
2+
<head>
3+
<title>Authentication Successful</title>
4+
<script>
5+
if (window.onAuthDone) {
6+
window.onAuthDone();
7+
} else if (window.opener && window.opener.postMessage) {
8+
window.opener.postMessage("authDone", "*");
9+
}
10+
</script>
11+
</head>
12+
<body>
13+
<div>
14+
<p>Thank you</p>
15+
<p>You may now close this window and return to the application</p>
16+
</div>
17+
</body>
18+
</html>

synapse/rest/client/v2_alpha/auth.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from synapse.api.constants import LoginType
1919
from synapse.api.errors import SynapseError
2020
from synapse.api.urls import CLIENT_API_PREFIX
21-
from synapse.handlers.auth import SUCCESS_TEMPLATE
2221
from synapse.http.server import finish_request
2322
from synapse.http.servlet import RestServlet, parse_string
2423

@@ -90,6 +89,30 @@
9089
</html>
9190
"""
9291

92+
SUCCESS_TEMPLATE = """
93+
<html>
94+
<head>
95+
<title>Success!</title>
96+
<meta name='viewport' content='width=device-width, initial-scale=1,
97+
user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
98+
<link rel="stylesheet" href="/_matrix/static/client/register/style.css">
99+
<script>
100+
if (window.onAuthDone) {
101+
window.onAuthDone();
102+
} else if (window.opener && window.opener.postMessage) {
103+
window.opener.postMessage("authDone", "*");
104+
}
105+
</script>
106+
</head>
107+
<body>
108+
<div>
109+
<p>Thank you</p>
110+
<p>You may now close this window and return to the application</p>
111+
</div>
112+
</body>
113+
</html>
114+
"""
115+
93116

94117
class AuthRestServlet(RestServlet):
95118
"""

0 commit comments

Comments
 (0)