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

Do not allow a deactivated user to login via SSO. #7240

Merged
merged 6 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/7240.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not allow a deactivated user to login via SSO.
34 changes: 30 additions & 4 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ def __init__(self, hs):
self._sso_auth_confirm_template = load_jinja2_templates(
hs.config.sso_redirect_confirm_template_dir, ["sso_auth_confirm.html"],
)[0]
self._sso_account_deactivated_template = load_jinja2_templates(
clokep marked this conversation as resolved.
Show resolved Hide resolved
hs.config.sso_redirect_confirm_template_dir,
["sso_account_deactivated.html"],
)[0]

self._server_name = hs.config.server_name

Expand Down Expand Up @@ -644,9 +648,6 @@ def check_user_exists(self, user_id: str):
Returns:
defer.Deferred: (unicode) canonical_user_id, or None if zero or
multiple matches

Raises:
UserDeactivatedError if a user is found but is deactivated.
"""
res = yield self._find_user_id_and_pwd_hash(user_id)
if res is not None:
Expand Down Expand Up @@ -1099,7 +1100,7 @@ def complete_sso_ui_auth(
request.write(html_bytes)
finish_request(request)

def complete_sso_login(
async def complete_sso_login(
self,
registered_user_id: str,
request: SynapseRequest,
Expand All @@ -1113,6 +1114,31 @@ def complete_sso_login(
client_redirect_url: The URL to which to redirect the user at the end of the
process.
"""
# If the password hash is None, the account has likely been deactivated
clokep marked this conversation as resolved.
Show resolved Hide resolved
deactivated = await self.store.get_user_deactivated_status(registered_user_id)
if deactivated:
html = self._sso_account_deactivated_template.render().encode("utf-8")

request.setResponseCode(403)
request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
request.setHeader(b"Content-Length", b"%d" % (len(html),))
request.write(html)
finish_request(request)
return

self._complete_sso_login(registered_user_id, request, client_redirect_url)

def _complete_sso_login(
self,
registered_user_id: str,
request: SynapseRequest,
client_redirect_url: str,
):
"""
The synchronous portion of complete_sso_login.

This exists purely for backwards compatibility of synapse.module_api.ModuleApi.
"""
# Create a login token
login_token = self.macaroon_gen.generate_short_term_login_token(
registered_user_id
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/cas_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,6 @@ async def handle_ticket(
localpart=localpart, default_display_name=user_display_name
)

self._auth_handler.complete_sso_login(
await self._auth_handler.complete_sso_login(
registered_user_id, request, client_redirect_url
)
2 changes: 1 addition & 1 deletion synapse/handlers/saml_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async def handle_saml_response(self, request):
)

else:
self._auth_handler.complete_sso_login(user_id, request, relay_state)
await self._auth_handler.complete_sso_login(user_id, request, relay_state)

async def _map_saml_response_to_user(
self, resp_bytes: str, client_redirect_url: str
Expand Down
22 changes: 21 additions & 1 deletion synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,33 @@ def complete_sso_login(
want their access token sent to `client_redirect_url`, or redirect them to that
URL with a token directly if the URL matches with one of the whitelisted clients.

This is deprecated in favor of complete_sso_login_async.

Args:
registered_user_id: The MXID that has been registered as a previous step of
of this SSO login.
request: The request to respond to.
client_redirect_url: The URL to which to offer to redirect the user (or to
redirect them directly if whitelisted).
"""
self._auth_handler._complete_sso_login(
registered_user_id, request, client_redirect_url,
)

async def complete_sso_login_async(
self, registered_user_id: str, request: SynapseRequest, client_redirect_url: str
):
"""Complete a SSO login by redirecting the user to a page to confirm whether they
want their access token sent to `client_redirect_url`, or redirect them to that
URL with a token directly if the URL matches with one of the whitelisted clients.

Args:
registered_user_id: The MXID that has been registered as a previous step of
of this SSO login.
request: The request to respond to.
client_redirect_url: The URL to which to offer to redirect the user (or to
redirect them directly if whitelisted).
"""
self._auth_handler.complete_sso_login(
await self._auth_handler.complete_sso_login(
registered_user_id, request, client_redirect_url,
)
10 changes: 10 additions & 0 deletions synapse/res/templates/sso_account_deactivated.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSO account deactivated</title>
</head>
<body>
<p>This account has been deactivated.</p>
</body>
</html>